blob: 4bf1fa2f303d82b4c197ad5211922278297f3d20 [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
developer465ca0c2022-11-25 14:30:05 +0800168void set_radionum(wifi_intf_param *intf_param, char *phy_name)
developer91f80742022-10-04 15:20:18 +0800169{
developer50614832022-11-17 20:42:05 +0800170 int radio_num = 0;
171 char *ptr = phy_name;
developer8d8d6302022-10-18 16:36:37 +0800172 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800173
174 while (*ptr) {
175 if (isdigit(*ptr)) {
developer50614832022-11-17 20:42:05 +0800176 phyId = strtoul(ptr, NULL, 10);
177 radio_num = phy_index_to_radio(phyId);
developer465ca0c2022-11-25 14:30:05 +0800178 intf_param->radio_index = radio_num;
developer91f80742022-10-04 15:20:18 +0800179 break;
180 }
181 ptr++;
182 }
183}
184
developer465ca0c2022-11-25 14:30:05 +0800185void set_ssid(wifi_intf_param *intf_param, char *ssid)
developer91f80742022-10-04 15:20:18 +0800186{
developer465ca0c2022-11-25 14:30:05 +0800187 strncpy(intf_param->ssid, ssid, 32);
developer91f80742022-10-04 15:20:18 +0800188}
189
developer465ca0c2022-11-25 14:30:05 +0800190void set_encryption(wifi_intf_param *intf_param, char *encryption_mode)
developer91f80742022-10-04 15:20:18 +0800191{
developerff378f22022-10-13 13:33:57 +0800192 if (strcmp(encryption_mode, "none") == 0) {
developer465ca0c2022-11-25 14:30:05 +0800193 intf_param->security.mode = wifi_security_mode_none;
194 intf_param->security.encr = wifi_encryption_none;
developerff378f22022-10-13 13:33:57 +0800195 }else if(strncmp(encryption_mode, "psk2", 4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800196 intf_param->security.mode = wifi_security_mode_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800197 }else if(strncmp(encryption_mode, "psk-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800198 intf_param->security.mode = wifi_security_mode_wpa_wpa2_personal;
developerff378f22022-10-13 13:33:57 +0800199 }else if(strncmp(encryption_mode, "psk",3) == 0){
developer465ca0c2022-11-25 14:30:05 +0800200 intf_param->security.mode = wifi_security_mode_wpa_personal;
developerff378f22022-10-13 13:33:57 +0800201 }else if(strncmp(encryption_mode, "wpa2",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800202 intf_param->security.mode = wifi_security_mode_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800203 }else if(strncmp(encryption_mode, "wpa-",4) == 0){
developer465ca0c2022-11-25 14:30:05 +0800204 intf_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise;
developerff378f22022-10-13 13:33:57 +0800205 }else if(strcmp(encryption_mode, "sae") == 0){
developer465ca0c2022-11-25 14:30:05 +0800206 intf_param->security.mode = wifi_security_mode_wpa3_personal;
developerff378f22022-10-13 13:33:57 +0800207 }else if(strcmp(encryption_mode, "wpa3") == 0){
developer465ca0c2022-11-25 14:30:05 +0800208 intf_param->security.mode = wifi_security_mode_wpa3_enterprise;
developerff378f22022-10-13 13:33:57 +0800209 }else if(strcmp(encryption_mode, "sae-mixed") == 0){
developer465ca0c2022-11-25 14:30:05 +0800210 intf_param->security.mode = wifi_security_mode_wpa3_transition;
developerc6a27322023-01-13 15:23:06 +0800211 }else if(strcmp(encryption_mode, "owe") == 0){
212 intf_param->security.mode = wifi_security_mode_owe;
developer91f80742022-10-04 15:20:18 +0800213 }
214
developerff378f22022-10-13 13:33:57 +0800215 if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){
developer465ca0c2022-11-25 14:30:05 +0800216 intf_param->security.encr = wifi_encryption_aes_tkip;
developerff378f22022-10-13 13:33:57 +0800217 }else if (strstr(encryption_mode, "tkip")){
developer465ca0c2022-11-25 14:30:05 +0800218 intf_param->security.encr = wifi_encryption_tkip;
developerff378f22022-10-13 13:33:57 +0800219 }else{
developer465ca0c2022-11-25 14:30:05 +0800220 intf_param->security.encr = wifi_encryption_aes;
developer91f80742022-10-04 15:20:18 +0800221 }
developerff378f22022-10-13 13:33:57 +0800222
developerc6a27322023-01-13 15:23:06 +0800223 if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae") || !strcmp(encryption_mode, "owe")){
developer465ca0c2022-11-25 14:30:05 +0800224 intf_param->security.mfp = wifi_mfp_cfg_required;
developerff378f22022-10-13 13:33:57 +0800225 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800226 intf_param->security.mfp = wifi_mfp_cfg_optional;
developerff378f22022-10-13 13:33:57 +0800227 }else{
developer465ca0c2022-11-25 14:30:05 +0800228 intf_param->security.mfp = wifi_mfp_cfg_disabled;
developerff378f22022-10-13 13:33:57 +0800229 }
230
231 if (!strcmp(encryption_mode, "sae")){
developer465ca0c2022-11-25 14:30:05 +0800232 intf_param->security.u.key.type = wifi_security_key_type_sae;
developerff378f22022-10-13 13:33:57 +0800233 }else if (!strcmp(encryption_mode, "sae-mixed")){
developer465ca0c2022-11-25 14:30:05 +0800234 intf_param->security.u.key.type = wifi_security_key_type_psk_sae;
developerff378f22022-10-13 13:33:57 +0800235 }else{
developer465ca0c2022-11-25 14:30:05 +0800236 intf_param->security.u.key.type = wifi_security_key_type_psk;
developer91f80742022-10-04 15:20:18 +0800237 }
developerff378f22022-10-13 13:33:57 +0800238
developer91f80742022-10-04 15:20:18 +0800239}
240
developer465ca0c2022-11-25 14:30:05 +0800241void set_key(wifi_intf_param *intf_param, char *key)
developer91f80742022-10-04 15:20:18 +0800242{
developer465ca0c2022-11-25 14:30:05 +0800243 strncpy(intf_param->security.u.key.key, key, 64);
developer91f80742022-10-04 15:20:18 +0800244}
245
developer7ac3bd52022-12-13 16:19:09 +0800246void set_ifname(wifi_intf_param *intf_param, char *ifname)
247{
248 if (strlen(ifname) > 15)
249 return;
250 strncpy(intf_param->ifname, ifname, strlen(ifname) + 1);
251}
252
developerb2123782022-12-29 09:35:55 +0800253void set_wds(wifi_intf_param *intf_param, char *wds_enable)
254{
255 intf_param->wds_mode = FALSE;
256 if (strncmp(wds_enable, "1", 1) == 0)
257 intf_param->wds_mode = TRUE;
258}
259
developer50614832022-11-17 20:42:05 +0800260int set_interface_bssid(int phy_index, int offset, mac_address_t *bssid)
developerf7e50b02022-10-14 10:07:58 +0800261{
262 FILE *f;
263 char mac_file[64] = {0};
264 char mac_address[20] = {0};
developerf7e50b02022-10-14 10:07:58 +0800265
developer1ac426a2022-12-22 19:44:36 +0800266 sprintf(mac_file, "/sys/class/ieee80211/phy%d/macaddress", phy_index);
developerf7e50b02022-10-14 10:07:58 +0800267 f = fopen(mac_file, "r");
268 if (f == NULL)
269 return -1;
270 fgets(mac_address, 20, f);
271 fclose(f);
272
developer50614832022-11-17 20:42:05 +0800273 mac_addr_aton(&(*bssid)[0], mac_address);
274 (*bssid)[0] += offset*2;
developerf7e50b02022-10-14 10:07:58 +0800275 return 0;
276}
277
developer91f80742022-10-04 15:20:18 +0800278void set_radio_param(wifi_radio_param radio_parameter)
279{
developer91f80742022-10-04 15:20:18 +0800280 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800281 wifi_radio_operationParam_t operationParam = {0};
282
developer8d8d6302022-10-18 16:36:37 +0800283 if(radio_parameter.radio_index == -1)
284 return;
285
developer63d72772022-10-07 09:42:31 +0800286 if (radio_parameter.disabled == TRUE) {
287 wifi_setRadioEnable(radio_parameter.radio_index, FALSE);
288 return;
289 }
developer91f80742022-10-04 15:20:18 +0800290
291 fprintf(stderr, "Start setting radio\n");
developerbf812932022-10-17 17:37:29 +0800292
developer128f8aa2022-12-26 17:09:00 +0800293 if (radio_parameter.txantenna != 0 && radio_parameter.txantenna == radio_parameter.rxantenna) {
294 ret = wifi_setRadioTxChainMask(radio_parameter.radio_index, radio_parameter.txantenna);
295 if (ret != RETURN_OK)
296 fprintf(stderr, "[Set Tx Chain mask failed!!!]\n");
297 }
298
developerbf812932022-10-17 17:37:29 +0800299 // Get current radio setting
developer63d72772022-10-07 09:42:31 +0800300 ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
301 if (ret != RETURN_OK)
302 fprintf(stderr, "[Get OperatingParameters failed!!!]\n");
developerbf812932022-10-17 17:37:29 +0800303 operationParam.enable = TRUE;
developer91f80742022-10-04 15:20:18 +0800304
developer91f80742022-10-04 15:20:18 +0800305 // Channel
developer63d72772022-10-07 09:42:31 +0800306 operationParam.autoChannelEnabled = radio_parameter.auto_channel;
307 operationParam.channel = radio_parameter.channel;
developer91f80742022-10-04 15:20:18 +0800308
developer25e07812022-10-13 15:27:02 +0800309 //bandwidth
310 if (radio_parameter.bandwidth == 20){
311 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ;
312 }else if (radio_parameter.bandwidth == 40){
313 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ;
314 }else if (radio_parameter.bandwidth == 80){
315 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ;
316 }else if (radio_parameter.bandwidth == 160){
317 operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ;
318 }
developer91f80742022-10-04 15:20:18 +0800319
320 // htmode
developer63d72772022-10-07 09:42:31 +0800321 unsigned int mode = 0; // enum wifi_ieee80211Variant_t
developer91f80742022-10-04 15:20:18 +0800322 if (strcmp(radio_parameter.band, "2g") == 0) {
developer63d72772022-10-07 09:42:31 +0800323 mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G;
developer91f80742022-10-04 15:20:18 +0800324 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
325 strcpy(radio_parameter.htmode, "11G");
326
developer63d72772022-10-07 09:42:31 +0800327 if (strstr(radio_parameter.htmode, "HE") != NULL)
328 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX;
developer91f80742022-10-04 15:20:18 +0800329
developer63d72772022-10-07 09:42:31 +0800330 } else if (strcmp(radio_parameter.band, "5g") == 0) {
331 mode |= WIFI_80211_VARIANT_A;
developer91f80742022-10-04 15:20:18 +0800332 if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0)
333 strcpy(radio_parameter.htmode, "11A");
developer63d72772022-10-07 09:42:31 +0800334
335 if (strstr(radio_parameter.htmode, "HE") != NULL)
336 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;
developer8d8d6302022-10-18 16:36:37 +0800337 }else if (strcmp(radio_parameter.band, "6g") == 0) {
338 mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;;
339 }
developer91f80742022-10-04 15:20:18 +0800340
341 if (strstr(radio_parameter.htmode, "VHT") != NULL)
developer63d72772022-10-07 09:42:31 +0800342 mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC;
developer91f80742022-10-04 15:20:18 +0800343 else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL)
developer63d72772022-10-07 09:42:31 +0800344 mode |= WIFI_80211_VARIANT_N;
developer91f80742022-10-04 15:20:18 +0800345
developer63d72772022-10-07 09:42:31 +0800346 operationParam.variant = mode;
developer91f80742022-10-04 15:20:18 +0800347
developer63d72772022-10-07 09:42:31 +0800348 // apply setting
349 ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam);
developer91f80742022-10-04 15:20:18 +0800350 if (ret != RETURN_OK)
developer63d72772022-10-07 09:42:31 +0800351 fprintf(stderr, "[Apply setting failed!!!]\n");
developer91f80742022-10-04 15:20:18 +0800352
developerbb58a932022-11-07 16:58:17 +0800353 // Country
354 fprintf(stderr, "Set Country: %s\n", radio_parameter.country);
355 ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country);
356 if (ret != RETURN_OK)
357 fprintf(stderr, "[Set Country failed!!!]\n");
358 ret = 0;
359
360 // hwmode
361 fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode);
362 ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode);
363 if (ret != RETURN_OK)
364 fprintf(stderr, "[Set hwmode failed!!!]\n");
365 ret = 0;
366
367 // noscan
368 fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan);
369 if(strlen(radio_parameter.noscan)){
370 ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan);
371 if (ret != RETURN_OK)
372 fprintf(stderr, "[Set noscan failed!!!]\n");
373 }
374 ret = 0;
375
developer91f80742022-10-04 15:20:18 +0800376}
377
developer465ca0c2022-11-25 14:30:05 +0800378void set_ap_param(wifi_intf_param ap_param , wifi_vap_info_map_t *map)
developer91f80742022-10-04 15:20:18 +0800379{
380 int ret = 0;
developer63d72772022-10-07 09:42:31 +0800381 int vap_index_in_map = 0;
developer50614832022-11-17 20:42:05 +0800382 int phy_index = 0;
developer63d72772022-10-07 09:42:31 +0800383 wifi_vap_info_t vap_info = {0};
developerbf812932022-10-17 17:37:29 +0800384 BOOL radio_enable = FALSE;
385
developerff42a302022-10-19 17:40:23 +0800386 if(ap_param.radio_index == -1)
387 return;
388
developerbf812932022-10-17 17:37:29 +0800389 wifi_getRadioEnable(ap_param.radio_index, &radio_enable);
390 if (radio_enable == FALSE)
391 return;
developer63d72772022-10-07 09:42:31 +0800392
developerff42a302022-10-19 17:40:23 +0800393
394 // get the index of the map
395 for (int i = 0; i < map->num_vaps; i++) {
396 if (map->vap_array[i].vap_index == ap_param.ap_index) {
397 vap_index_in_map = i;
398 break;
developer63d72772022-10-07 09:42:31 +0800399 }
400 }
401
developerff42a302022-10-19 17:40:23 +0800402
developerf7e50b02022-10-14 10:07:58 +0800403 fprintf(stderr, "Start setting ap\n");
404
developerff42a302022-10-19 17:40:23 +0800405 vap_info = map->vap_array[vap_index_in_map];
developerf7e50b02022-10-14 10:07:58 +0800406 vap_info.u.bss_info.enabled = TRUE;
developer50614832022-11-17 20:42:05 +0800407 phy_index = radio_index_to_phy(vap_info.radio_index);
408 if (set_interface_bssid(phy_index, ap_param.mac_offset, &vap_info.u.bss_info.bssid) == -1) {
developerf7e50b02022-10-14 10:07:58 +0800409 fprintf(stderr, "Get mac address failed.\n");
developer50614832022-11-17 20:42:05 +0800410 return;
developerf7e50b02022-10-14 10:07:58 +0800411 }
developer91f80742022-10-04 15:20:18 +0800412
developer91f80742022-10-04 15:20:18 +0800413 // SSID
developer63d72772022-10-07 09:42:31 +0800414 strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33);
415 vap_info.u.bss_info.ssid[32] = '\0';
developer91f80742022-10-04 15:20:18 +0800416
developer7ac3bd52022-12-13 16:19:09 +0800417 // interface
418 if (strlen(ap_param.ifname) != 0) {
419 strncpy(vap_info.vap_name, ap_param.ifname, 16);
420 vap_info.vap_name[15] = "\0";
421 }
422
developerff378f22022-10-13 13:33:57 +0800423 vap_info.u.bss_info.security.mode = ap_param.security.mode;
424 vap_info.u.bss_info.security.encr = ap_param.security.encr;
425 vap_info.u.bss_info.security.mfp = ap_param.security.mfp;
426 vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type;
427 strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64);
developer8d8d6302022-10-18 16:36:37 +0800428
429
developer63d72772022-10-07 09:42:31 +0800430 // Replace the setting with uci config
developerff42a302022-10-19 17:40:23 +0800431 map->vap_array[vap_index_in_map] = vap_info;
developer91f80742022-10-04 15:20:18 +0800432}
433
developer465ca0c2022-11-25 14:30:05 +0800434void set_sta_param(wifi_intf_param sta_param)
developer50614832022-11-17 20:42:05 +0800435{
436 wifi_sta_network_t *sta = NULL;
437 mac_address_t sta_mac = {0};
438 char sta_mac_str[20] = {0};
439 char key_mgmt[16] = {0};
440 char pairwise[16] = {0};
441 int phy_index = 0;
442
443 sta = calloc(1, sizeof(wifi_sta_network_t));
444
445 phy_index = radio_index_to_phy(sta_param.radio_index);
446 set_interface_bssid(phy_index, sta_param.mac_offset, &sta_mac);
447 mac_addr_ntoa(sta_mac_str, sta_mac);
448 snprintf(sta->ssid, 31, "%s", sta_param.ssid);
449 sta->ssid[31] = '\0';
450 snprintf(sta->psk, 64, "%s", sta_param.password);
451
452 if (sta_param.security.mode == wifi_security_mode_none)
453 strcpy(key_mgmt, "NONE");
454 else if (sta_param.security.mode == wifi_security_mode_wpa3_personal)
455 strcpy(key_mgmt, "SAE");
developerc6a27322023-01-13 15:23:06 +0800456 else if (sta_param.security.mode == wifi_security_mode_owe)
457 strcpy(key_mgmt, "OWE");
developer50614832022-11-17 20:42:05 +0800458 else
459 strcpy(key_mgmt, "WPA-PSK");
460 snprintf(sta->key_mgmt, 64, "%s", key_mgmt);
461
462 if (sta_param.security.encr == wifi_encryption_aes)
463 strcpy(pairwise, "CCMP");
464 else if (sta_param.security.encr == wifi_encryption_tkip)
465 strcpy(pairwise, "TKIP");
466 else
467 strcpy(pairwise, "CCMP TKIP");
468 snprintf(sta->pairwise, 64, "%s", pairwise);
469
470 if (strlen(sta_param.security.u.key.key) > 0)
471 strncpy(sta->psk, sta_param.security.u.key.key, 127);
472 sta->psk[127] = '\0';
473 sta->psk_len = strlen(sta->psk);
474
developerb2123782022-12-29 09:35:55 +0800475 if (sta_param.wds_mode == TRUE)
476 sta->flags |= WIFI_STA_NET_F_4ADDR_MULTI_AP;
477
478 wifi_createSTAInterface(sta_param.sta_index, sta_mac_str, sta_param.wds_mode);
developer50614832022-11-17 20:42:05 +0800479
480 if (wifi_setSTANetworks(sta_param.sta_index, &sta, 1, FALSE) == RETURN_ERR) {
481 fprintf(stderr, "Write to sta %d config file failed\n", sta_param.sta_index);
482 free(sta);
483 return;
484 }
485 free(sta);
486
487 if (wifi_setSTAEnabled(sta_param.sta_index, TRUE) == RETURN_ERR) {
488 fprintf(stderr, "Enable station failed\n");
489 return;
490 }
491}
492
developer91f80742022-10-04 15:20:18 +0800493int apply_uci_config ()
494{
495 struct uci_context *uci_ctx = uci_alloc_context();
496 struct uci_package *uci_pkg = NULL;
497 struct uci_element *e;
498 // struct uci_section *s;
499 const char cfg_name[] = "wireless";
500 int max_radio_num = 0;
501 BOOL parsing_radio = FALSE;
developer8d8d6302022-10-18 16:36:37 +0800502 int apCount[3] = {0};
developer50614832022-11-17 20:42:05 +0800503 int staCount[3] = {0};
developerff42a302022-10-19 17:40:23 +0800504 wifi_vap_info_map_t vap_map[3] = {0};
505 int ret = 0;
506 int i = 0;
developer91f80742022-10-04 15:20:18 +0800507
508 wifi_getMaxRadioNumber(&max_radio_num);
509 fprintf(stderr, "max radio number: %d\n", max_radio_num);
developerff42a302022-10-19 17:40:23 +0800510 for (i = 0; i < max_radio_num ;i++ ){
511 ret = wifi_getRadioVapInfoMap(i, &vap_map[i]);
512 if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap.
513 fprintf(stderr, "[Get vap map failed!!!]\n");
514 vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO;
515 }
516 }
developer91f80742022-10-04 15:20:18 +0800517 if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) {
518 uci_free_context(uci_ctx);
519 fprintf(stderr, "%s: load uci failed.\n", __func__);
520 return RETURN_ERR;
521 }
522
523 uci_foreach_element(&uci_pkg->sections, e) {
524
525 struct uci_section *s = uci_to_section(e);
526 struct uci_element *option = NULL;
527 wifi_radio_param radio_param = {0};
developer465ca0c2022-11-25 14:30:05 +0800528 wifi_intf_param intf_param = {0};
developer8d8d6302022-10-18 16:36:37 +0800529 int phyId = 0;
developer91f80742022-10-04 15:20:18 +0800530 radio_param.radio_index = -1;
developer465ca0c2022-11-25 14:30:05 +0800531 intf_param.ap_index = -1;
developer91f80742022-10-04 15:20:18 +0800532
533 if (strcmp(s->type, "wifi-device") == 0) {
developer8d8d6302022-10-18 16:36:37 +0800534 sscanf(s->e.name, "radio%d", &phyId);
535 radio_param.radio_index = phy_index_to_radio(phyId);
developer91f80742022-10-04 15:20:18 +0800536 parsing_radio = TRUE;
537 fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index);
538 } else if (strcmp(s->type, "wifi-iface") == 0) {
developer91f80742022-10-04 15:20:18 +0800539 parsing_radio = FALSE;
developer91f80742022-10-04 15:20:18 +0800540 }
541
542 uci_foreach_element(&s->options, option) {
543
544 struct uci_option *op = uci_to_option(option);
545 if (parsing_radio == TRUE) {
546 // transform the type from input string and store the value in radio_param.
547 if (strcmp(op->e.name, "channel") == 0)
548 set_channel(&radio_param, op->v.string);
549 else if (strcmp(op->e.name, "hwmode") == 0)
550 set_hwmode(&radio_param, op->v.string);
551 else if (strcmp(op->e.name, "htmode") == 0)
552 set_htmode(&radio_param, op->v.string);
553 else if (strcmp(op->e.name, "disabled") == 0)
554 set_disable(&radio_param, op->v.string);
555 else if (strcmp(op->e.name, "band") == 0)
556 set_band(&radio_param, op->v.string);
557 else if (strcmp(op->e.name, "country") == 0)
558 set_country(&radio_param, op->v.string);
559 else if (strcmp(op->e.name, "noscan") == 0)
developer6feac682022-10-18 17:44:13 +0800560 set_noscan(&radio_param, op->v.string);
developer128f8aa2022-12-26 17:09:00 +0800561 else if (strcmp(op->e.name, "rxantenna") == 0)
562 set_rxant(&radio_param, op->v.string);
563 else if (strcmp(op->e.name, "txantenna") == 0)
564 set_txant(&radio_param, op->v.string);
developer91f80742022-10-04 15:20:18 +0800565 else
566 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
567 } else {
568 // parsing iface
developer8d8d6302022-10-18 16:36:37 +0800569 if (strcmp(op->e.name, "device") == 0){
developer465ca0c2022-11-25 14:30:05 +0800570 set_radionum(&intf_param, op->v.string);
developer50614832022-11-17 20:42:05 +0800571 }else if (strcmp(op->e.name, "mode") == 0){
developer1ac426a2022-12-22 19:44:36 +0800572 intf_param.mac_offset = staCount[intf_param.radio_index] + apCount[intf_param.radio_index];
developer50614832022-11-17 20:42:05 +0800573 if (strncmp(op->v.string, "sta", 3) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800574 intf_param.sta_mode = TRUE;
575 intf_param.sta_index = intf_param.radio_index + staCount[intf_param.radio_index]*max_radio_num;
576 staCount[intf_param.radio_index] ++ ;
577 fprintf(stderr, "\n----- Start parsing sta %d config. -----\n", intf_param.sta_index);
developer50614832022-11-17 20:42:05 +0800578 } else if (strncmp(op->v.string, "ap", 2) == 0) {
developer465ca0c2022-11-25 14:30:05 +0800579 intf_param.sta_mode = FALSE;
580 intf_param.ap_index = intf_param.radio_index + apCount[intf_param.radio_index]*max_radio_num;
581 apCount[intf_param.radio_index] ++ ;
582 fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", intf_param.ap_index);
developer50614832022-11-17 20:42:05 +0800583 }
developer8d8d6302022-10-18 16:36:37 +0800584 }else if (strcmp(op->e.name, "ssid") == 0){
developer465ca0c2022-11-25 14:30:05 +0800585 set_ssid(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800586 }else if (strcmp(op->e.name, "encryption") == 0){
developer465ca0c2022-11-25 14:30:05 +0800587 set_encryption(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800588 }else if (strcmp(op->e.name, "key") == 0){
developer465ca0c2022-11-25 14:30:05 +0800589 set_key(&intf_param, op->v.string);
developer7ac3bd52022-12-13 16:19:09 +0800590 }else if (strcmp(op->e.name, "ifname") == 0){
591 set_ifname(&intf_param, op->v.string);
developerb2123782022-12-29 09:35:55 +0800592 }else if (strcmp(op->e.name, "wds") == 0){
593 set_wds(&intf_param, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800594 }else{
developer91f80742022-10-04 15:20:18 +0800595 fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string);
developer8d8d6302022-10-18 16:36:37 +0800596 }
developer91f80742022-10-04 15:20:18 +0800597 }
598 }
599 if (parsing_radio == TRUE)
600 set_radio_param(radio_param);
developer465ca0c2022-11-25 14:30:05 +0800601 else if (intf_param.sta_mode == TRUE)
602 set_sta_param(intf_param);
developer91f80742022-10-04 15:20:18 +0800603 else
developer465ca0c2022-11-25 14:30:05 +0800604 set_ap_param(intf_param, &vap_map[intf_param.radio_index]);
developer91f80742022-10-04 15:20:18 +0800605 }
developer50614832022-11-17 20:42:05 +0800606 fprintf(stderr, "\n----- Start setting Vaps. -----\n");
developer91f80742022-10-04 15:20:18 +0800607
developerff42a302022-10-19 17:40:23 +0800608 for (i = 0; i < max_radio_num ;i++ ){
609 ret = wifi_createVAP(i, &vap_map[i]);
610 if (ret != RETURN_OK)
611 fprintf(stderr, "[Apply vap setting failed!!!]\n");
612 }
613
developer91f80742022-10-04 15:20:18 +0800614 uci_unload(uci_ctx, uci_pkg);
615 uci_free_context(uci_ctx);
616 return RETURN_OK;
617}
618
619int main(int argc, char **argv)
620{
621 if (argc != 2 || strcmp(argv[1], "reload") != 0) {
622 fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n");
623 return -1;
624 }
625 apply_uci_config();
626 return 0;
627}