developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 1 | #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 | |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 8 | |
| 9 | static int _syscmd(char *cmd, char *retBuf, int retBufSize) |
| 10 | { |
| 11 | FILE *f; |
| 12 | char *ptr = retBuf; |
| 13 | int bufSize=retBufSize, bufbytes=0, readbytes=0, cmd_ret=0; |
| 14 | |
| 15 | |
| 16 | if((f = popen(cmd, "r")) == NULL) { |
| 17 | fprintf(stderr,"\npopen %s error\n", cmd); |
| 18 | return RETURN_ERR; |
| 19 | } |
| 20 | |
| 21 | while(!feof(f)) |
| 22 | { |
| 23 | *ptr = 0; |
| 24 | if(bufSize>=128) { |
| 25 | bufbytes=128; |
| 26 | } else { |
| 27 | bufbytes=bufSize-1; |
| 28 | } |
| 29 | |
| 30 | fgets(ptr,bufbytes,f); |
| 31 | readbytes=strlen(ptr); |
| 32 | |
| 33 | if(!readbytes) |
| 34 | break; |
| 35 | |
| 36 | bufSize-=readbytes; |
| 37 | ptr += readbytes; |
| 38 | } |
| 39 | cmd_ret = pclose(f); |
| 40 | retBuf[retBufSize-1]=0; |
| 41 | |
| 42 | return cmd_ret >> 8; |
| 43 | } |
| 44 | |
| 45 | int phy_index_to_radio(int phyIndex) |
| 46 | { |
| 47 | char cmd[128] = {0}; |
| 48 | char buf[64] = {0}; |
| 49 | int radioIndex = 0; |
| 50 | snprintf(cmd, sizeof(cmd), "ls /tmp | grep phy%d | cut -d '-' -f2 | tr -d '\n'", phyIndex); |
| 51 | _syscmd(cmd, buf, sizeof(buf)); |
| 52 | |
| 53 | if (strlen(buf) == 0 || strstr(buf, "wifi") == NULL) { |
| 54 | fprintf(stderr, "%s: failed to get wifi index\n", __func__); |
| 55 | return RETURN_ERR; |
| 56 | } |
| 57 | sscanf(buf, "wifi%d", &radioIndex); |
| 58 | fprintf(stderr, "%s: radio index = %d \n", __func__, radioIndex); |
| 59 | return radioIndex; |
| 60 | } |
| 61 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 62 | void set_channel(wifi_radio_param *radio_param, char *channel) |
| 63 | { |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 64 | if (strcmp(channel, "auto") == 0) { |
| 65 | radio_param->auto_channel = TRUE; |
| 66 | radio_param->channel = 0; |
| 67 | } else { |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 68 | radio_param->auto_channel = FALSE; |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 69 | radio_param->channel = strtol(channel, NULL, 10); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 70 | } |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 71 | return; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 72 | } |
| 73 | |
developer | 02a4a1b | 2022-10-06 17:16:43 +0800 | [diff] [blame] | 74 | void set_country(wifi_radio_param *radio_param, char *country) |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 75 | { |
| 76 | strcpy(radio_param->country, country); |
| 77 | } |
| 78 | |
developer | 02a4a1b | 2022-10-06 17:16:43 +0800 | [diff] [blame] | 79 | void set_band(wifi_radio_param *radio_param, char *band) |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 80 | { |
| 81 | strcpy(radio_param->band, band); |
| 82 | } |
| 83 | |
developer | 54afa2c | 2022-10-18 17:44:13 +0800 | [diff] [blame] | 84 | void set_noscan(wifi_radio_param *radio_param, char *noscan) |
| 85 | { |
| 86 | snprintf(radio_param->noscan, 2, "%s", noscan); |
| 87 | radio_param->noscan[1] = '\0'; |
| 88 | } |
| 89 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 90 | void set_hwmode(wifi_radio_param *radio_param, char *hwmode) |
| 91 | { |
| 92 | if (strncmp(hwmode, "11a", 3) == 0) |
| 93 | strcpy(radio_param->hwmode, "a"); |
| 94 | if (strncmp(hwmode, "11b", 3) == 0) |
| 95 | strcpy(radio_param->hwmode, "b"); |
| 96 | if (strncmp(hwmode, "11g", 3) == 0) |
| 97 | strcpy(radio_param->hwmode, "g"); |
| 98 | } |
| 99 | |
| 100 | void set_htmode(wifi_radio_param *radio_param, char *htmode) |
| 101 | { |
| 102 | char tmp[16] = {0}; |
| 103 | char *ptr = htmode; |
| 104 | ULONG bandwidth = 0; |
| 105 | radio_param->bandwidth = 20; |
| 106 | while (*ptr) { |
| 107 | if (isdigit(*ptr)) { |
| 108 | bandwidth = strtoul(ptr, NULL, 10); |
| 109 | radio_param->bandwidth = bandwidth; |
| 110 | break; |
| 111 | } |
| 112 | ptr++; |
| 113 | } |
| 114 | |
| 115 | // HT40 -> 11NGHT40PLUS |
| 116 | // VHT40+ -> 11ACVHT40PLUS |
| 117 | // HE80 -> 11AXHE80 |
| 118 | if (strstr(htmode, "+") != NULL) { |
| 119 | strncpy(tmp, htmode, strlen(htmode) - 1); |
| 120 | strcat(tmp, "PLUS"); |
| 121 | } else if (strstr(htmode, "-") != NULL) { |
| 122 | strncpy(tmp, htmode, strlen(htmode) - 1); |
| 123 | strcat(tmp, "MINUS"); |
| 124 | } else |
| 125 | strcpy(tmp, htmode); |
| 126 | |
| 127 | |
| 128 | if (strstr(htmode, "VHT") != NULL) { |
| 129 | snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp); |
| 130 | } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) { |
| 131 | snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp); |
| 132 | } else if (strstr(htmode, "HE") != NULL) { |
| 133 | snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp); |
| 134 | } else { // NOHT or NONE should be parsed with the band, so just fill the original string. |
| 135 | strcpy(radio_param->htmode, tmp); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | void set_disable(wifi_radio_param *radio_param, char *disable) |
| 141 | { |
| 142 | if (strcmp(disable, "1") == 0) |
| 143 | radio_param->disabled = TRUE; |
| 144 | else |
| 145 | radio_param->disabled = FALSE; |
| 146 | } |
| 147 | |
| 148 | void set_radionum(wifi_ap_param *ap_param, char *radio_name) |
| 149 | { |
| 150 | int radio_num; |
| 151 | char *ptr = radio_name; |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 152 | int phyId = 0; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 153 | |
| 154 | while (*ptr) { |
| 155 | if (isdigit(*ptr)) { |
| 156 | radio_num = strtoul(ptr, NULL, 10); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 157 | phyId = phy_index_to_radio(radio_num); |
| 158 | ap_param->radio_index = phyId; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | ptr++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void set_ssid(wifi_ap_param *ap_param, char *ssid) |
| 166 | { |
| 167 | strncpy(ap_param->ssid, ssid, 32); |
| 168 | } |
| 169 | |
| 170 | void set_encryption(wifi_ap_param *ap_param, char *encryption_mode) |
| 171 | { |
developer | 3ddad2f | 2022-10-13 13:33:57 +0800 | [diff] [blame] | 172 | if (strcmp(encryption_mode, "none") == 0) { |
| 173 | ap_param->security.mode = wifi_security_mode_none; |
| 174 | ap_param->security.encr = wifi_encryption_none; |
| 175 | }else if(strncmp(encryption_mode, "psk2", 4) == 0){ |
| 176 | ap_param->security.mode = wifi_security_mode_wpa2_personal; |
| 177 | }else if(strncmp(encryption_mode, "psk-",4) == 0){ |
| 178 | ap_param->security.mode = wifi_security_mode_wpa_wpa2_personal; |
| 179 | }else if(strncmp(encryption_mode, "psk",3) == 0){ |
| 180 | ap_param->security.mode = wifi_security_mode_wpa_personal; |
| 181 | }else if(strncmp(encryption_mode, "wpa2",4) == 0){ |
| 182 | ap_param->security.mode = wifi_security_mode_wpa2_enterprise; |
| 183 | }else if(strncmp(encryption_mode, "wpa-",4) == 0){ |
| 184 | ap_param->security.mode = wifi_security_mode_wpa_wpa2_enterprise; |
| 185 | }else if(strcmp(encryption_mode, "sae") == 0){ |
| 186 | ap_param->security.mode = wifi_security_mode_wpa3_personal; |
| 187 | }else if(strcmp(encryption_mode, "wpa3") == 0){ |
| 188 | ap_param->security.mode = wifi_security_mode_wpa3_enterprise; |
| 189 | }else if(strcmp(encryption_mode, "sae-mixed") == 0){ |
| 190 | ap_param->security.mode = wifi_security_mode_wpa3_transition; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 191 | } |
| 192 | |
developer | 3ddad2f | 2022-10-13 13:33:57 +0800 | [diff] [blame] | 193 | if(strstr(encryption_mode, "tkip") && (strstr(encryption_mode, "ccmp") || strstr(encryption_mode, "aes") )){ |
| 194 | ap_param->security.encr = wifi_encryption_aes_tkip; |
| 195 | }else if (strstr(encryption_mode, "tkip")){ |
| 196 | ap_param->security.encr = wifi_encryption_tkip; |
| 197 | }else{ |
| 198 | ap_param->security.encr = wifi_encryption_aes; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 199 | } |
developer | 3ddad2f | 2022-10-13 13:33:57 +0800 | [diff] [blame] | 200 | |
| 201 | if(!strcmp(encryption_mode, "wpa3") || !strcmp(encryption_mode, "sae")){ |
| 202 | ap_param->security.mfp = wifi_mfp_cfg_required; |
| 203 | }else if (!strcmp(encryption_mode, "sae-mixed")){ |
| 204 | ap_param->security.mfp = wifi_mfp_cfg_optional; |
| 205 | }else{ |
| 206 | ap_param->security.mfp = wifi_mfp_cfg_disabled; |
| 207 | } |
| 208 | |
| 209 | if (!strcmp(encryption_mode, "sae")){ |
| 210 | ap_param->security.u.key.type = wifi_security_key_type_sae; |
| 211 | }else if (!strcmp(encryption_mode, "sae-mixed")){ |
| 212 | ap_param->security.u.key.type = wifi_security_key_type_psk_sae; |
| 213 | }else{ |
| 214 | ap_param->security.u.key.type = wifi_security_key_type_psk; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 215 | } |
developer | 3ddad2f | 2022-10-13 13:33:57 +0800 | [diff] [blame] | 216 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void set_key(wifi_ap_param *ap_param, char *key) |
| 220 | { |
developer | 3ddad2f | 2022-10-13 13:33:57 +0800 | [diff] [blame] | 221 | strncpy(ap_param->security.u.key.key, key, 64); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 222 | } |
| 223 | |
developer | f9b2ef0 | 2022-10-14 10:07:58 +0800 | [diff] [blame] | 224 | int set_ap_bssid(int radio_index, int offset, mac_address_t *bssid) |
| 225 | { |
| 226 | FILE *f; |
| 227 | char mac_file[64] = {0}; |
| 228 | char mac_address[20] = {0}; |
| 229 | char *tmp = NULL; |
| 230 | |
| 231 | sprintf(mac_file, "/sys/class/net/wlan%d/address", radio_index); |
| 232 | f = fopen(mac_file, "r"); |
| 233 | if (f == NULL) |
| 234 | return -1; |
| 235 | fgets(mac_address, 20, f); |
| 236 | fclose(f); |
| 237 | |
| 238 | sscanf(mac_address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &(*bssid)[0], &(*bssid)[1], &(*bssid)[2], &(*bssid)[3], &(*bssid)[4], &(*bssid)[5]); |
| 239 | (*bssid)[0] += (offset + 1)*2; |
| 240 | return 0; |
| 241 | } |
| 242 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 243 | void set_radio_param(wifi_radio_param radio_parameter) |
| 244 | { |
| 245 | BOOL enable; |
| 246 | BOOL current; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 247 | int ret = 0; |
| 248 | struct params param; |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 249 | wifi_radio_operationParam_t operationParam = {0}; |
| 250 | |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 251 | if(radio_parameter.radio_index == -1) |
| 252 | return; |
| 253 | |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 254 | if (radio_parameter.disabled == TRUE) { |
| 255 | wifi_setRadioEnable(radio_parameter.radio_index, FALSE); |
| 256 | return; |
| 257 | } |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 258 | |
| 259 | fprintf(stderr, "Start setting radio\n"); |
developer | 1861581 | 2022-10-17 17:37:29 +0800 | [diff] [blame] | 260 | |
| 261 | wifi_setRadioEnable(radio_parameter.radio_index, TRUE); |
| 262 | sleep(1); |
| 263 | |
developer | 524b81d | 2022-10-20 10:26:25 +0800 | [diff] [blame] | 264 | // Country |
| 265 | fprintf(stderr, "Set Country: %s\n", radio_parameter.country); |
| 266 | ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country); |
| 267 | if (ret != RETURN_OK) |
| 268 | fprintf(stderr, "[Set Country failed!!!]\n"); |
| 269 | ret = 0; |
| 270 | |
| 271 | // hwmode |
| 272 | fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode); |
| 273 | ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode); |
| 274 | if (ret != RETURN_OK) |
| 275 | fprintf(stderr, "[Set hwmode failed!!!]\n"); |
| 276 | ret = 0; |
| 277 | |
| 278 | // noscan |
| 279 | fprintf(stderr, "Set noscan: %s \n", radio_parameter.noscan); |
| 280 | if(strlen(radio_parameter.noscan)){ |
| 281 | ret = wifi_setNoscan(radio_parameter.radio_index, radio_parameter.noscan); |
| 282 | if (ret != RETURN_OK) |
| 283 | fprintf(stderr, "[Set noscan failed!!!]\n"); |
| 284 | } |
| 285 | ret = 0; |
| 286 | |
developer | 1861581 | 2022-10-17 17:37:29 +0800 | [diff] [blame] | 287 | // Get current radio setting |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 288 | ret = wifi_getRadioOperatingParameters(radio_parameter.radio_index, &operationParam); |
| 289 | if (ret != RETURN_OK) |
| 290 | fprintf(stderr, "[Get OperatingParameters failed!!!]\n"); |
developer | 1861581 | 2022-10-17 17:37:29 +0800 | [diff] [blame] | 291 | operationParam.enable = TRUE; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 292 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 293 | // Channel |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 294 | operationParam.autoChannelEnabled = radio_parameter.auto_channel; |
| 295 | operationParam.channel = radio_parameter.channel; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 296 | |
developer | ae3f841 | 2022-10-13 15:27:02 +0800 | [diff] [blame] | 297 | //bandwidth |
| 298 | if (radio_parameter.bandwidth == 20){ |
| 299 | operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_20MHZ; |
| 300 | }else if (radio_parameter.bandwidth == 40){ |
| 301 | operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_40MHZ; |
| 302 | }else if (radio_parameter.bandwidth == 80){ |
| 303 | operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_80MHZ; |
| 304 | }else if (radio_parameter.bandwidth == 160){ |
| 305 | operationParam.channelWidth = WIFI_CHANNELBANDWIDTH_160MHZ; |
| 306 | } |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 307 | |
| 308 | // htmode |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 309 | unsigned int mode = 0; // enum wifi_ieee80211Variant_t |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 310 | if (strcmp(radio_parameter.band, "2g") == 0) { |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 311 | mode |= WIFI_80211_VARIANT_B | WIFI_80211_VARIANT_G; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 312 | if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0) |
| 313 | strcpy(radio_parameter.htmode, "11G"); |
| 314 | |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 315 | if (strstr(radio_parameter.htmode, "HE") != NULL) |
| 316 | mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AX; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 317 | |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 318 | } else if (strcmp(radio_parameter.band, "5g") == 0) { |
| 319 | mode |= WIFI_80211_VARIANT_A; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 320 | if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0) |
| 321 | strcpy(radio_parameter.htmode, "11A"); |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 322 | |
| 323 | if (strstr(radio_parameter.htmode, "HE") != NULL) |
| 324 | mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX; |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 325 | }else if (strcmp(radio_parameter.band, "6g") == 0) { |
| 326 | mode |= WIFI_80211_VARIANT_A | WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC | WIFI_80211_VARIANT_AX;; |
| 327 | } |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 328 | |
| 329 | if (strstr(radio_parameter.htmode, "VHT") != NULL) |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 330 | mode |= WIFI_80211_VARIANT_N | WIFI_80211_VARIANT_AC; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 331 | else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL) |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 332 | mode |= WIFI_80211_VARIANT_N; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 333 | |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 334 | operationParam.variant = mode; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 335 | |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 336 | // apply setting |
| 337 | ret = wifi_setRadioOperatingParameters(radio_parameter.radio_index, &operationParam); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 338 | if (ret != RETURN_OK) |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 339 | fprintf(stderr, "[Apply setting failed!!!]\n"); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 340 | |
| 341 | } |
| 342 | |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 343 | void set_ap_param(wifi_ap_param ap_param , wifi_vap_info_map_t *map) |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 344 | { |
| 345 | int ret = 0; |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 346 | int vap_index_in_map = 0; |
| 347 | wifi_vap_info_t vap_info = {0}; |
developer | 1861581 | 2022-10-17 17:37:29 +0800 | [diff] [blame] | 348 | BOOL radio_enable = FALSE; |
| 349 | |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 350 | if(ap_param.radio_index == -1) |
| 351 | return; |
| 352 | |
developer | 1861581 | 2022-10-17 17:37:29 +0800 | [diff] [blame] | 353 | wifi_getRadioEnable(ap_param.radio_index, &radio_enable); |
| 354 | if (radio_enable == FALSE) |
| 355 | return; |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 356 | |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 357 | |
| 358 | // get the index of the map |
| 359 | for (int i = 0; i < map->num_vaps; i++) { |
| 360 | if (map->vap_array[i].vap_index == ap_param.ap_index) { |
| 361 | vap_index_in_map = i; |
| 362 | break; |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 366 | |
developer | f9b2ef0 | 2022-10-14 10:07:58 +0800 | [diff] [blame] | 367 | fprintf(stderr, "Start setting ap\n"); |
| 368 | |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 369 | vap_info = map->vap_array[vap_index_in_map]; |
developer | f9b2ef0 | 2022-10-14 10:07:58 +0800 | [diff] [blame] | 370 | vap_info.u.bss_info.enabled = TRUE; |
| 371 | if (set_ap_bssid(vap_info.radio_index, vap_index_in_map, &vap_info.u.bss_info.bssid) == -1) { |
| 372 | fprintf(stderr, "Get mac address failed.\n"); |
| 373 | return -1; |
| 374 | } |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 375 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 376 | // SSID |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 377 | strncpy(vap_info.u.bss_info.ssid, ap_param.ssid, 33); |
| 378 | vap_info.u.bss_info.ssid[32] = '\0'; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 379 | |
developer | 3ddad2f | 2022-10-13 13:33:57 +0800 | [diff] [blame] | 380 | vap_info.u.bss_info.security.mode = ap_param.security.mode; |
| 381 | vap_info.u.bss_info.security.encr = ap_param.security.encr; |
| 382 | vap_info.u.bss_info.security.mfp = ap_param.security.mfp; |
| 383 | vap_info.u.bss_info.security.u.key.type = ap_param.security.u.key.type; |
| 384 | strncpy(vap_info.u.bss_info.security.u.key.key, ap_param.security.u.key.key, 64); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 385 | |
| 386 | |
developer | ab98580 | 2022-10-07 09:42:31 +0800 | [diff] [blame] | 387 | // Replace the setting with uci config |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 388 | map->vap_array[vap_index_in_map] = vap_info; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | int apply_uci_config () |
| 392 | { |
| 393 | struct uci_context *uci_ctx = uci_alloc_context(); |
| 394 | struct uci_package *uci_pkg = NULL; |
| 395 | struct uci_element *e; |
| 396 | // struct uci_section *s; |
| 397 | const char cfg_name[] = "wireless"; |
| 398 | int max_radio_num = 0; |
| 399 | BOOL parsing_radio = FALSE; |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 400 | int apCount[3] = {0}; |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 401 | wifi_vap_info_map_t vap_map[3] = {0}; |
| 402 | int ret = 0; |
| 403 | int i = 0; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 404 | |
| 405 | wifi_getMaxRadioNumber(&max_radio_num); |
| 406 | fprintf(stderr, "max radio number: %d\n", max_radio_num); |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 407 | for (i = 0; i < max_radio_num ;i++ ){ |
| 408 | ret = wifi_getRadioVapInfoMap(i, &vap_map[i]); |
| 409 | if (ret != RETURN_OK) { // if failed, we set assume this vap as the first vap. |
| 410 | fprintf(stderr, "[Get vap map failed!!!]\n"); |
| 411 | vap_map[i].num_vaps = MAX_NUM_VAP_PER_RADIO; |
| 412 | } |
| 413 | } |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 414 | if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) { |
| 415 | uci_free_context(uci_ctx); |
| 416 | fprintf(stderr, "%s: load uci failed.\n", __func__); |
| 417 | return RETURN_ERR; |
| 418 | } |
| 419 | |
| 420 | uci_foreach_element(&uci_pkg->sections, e) { |
| 421 | |
| 422 | struct uci_section *s = uci_to_section(e); |
| 423 | struct uci_element *option = NULL; |
| 424 | wifi_radio_param radio_param = {0}; |
| 425 | wifi_ap_param ap_param = {0}; |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 426 | int phyId = 0; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 427 | radio_param.radio_index = -1; |
| 428 | ap_param.ap_index = -1; |
| 429 | |
| 430 | if (strcmp(s->type, "wifi-device") == 0) { |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 431 | sscanf(s->e.name, "radio%d", &phyId); |
| 432 | radio_param.radio_index = phy_index_to_radio(phyId); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 433 | parsing_radio = TRUE; |
| 434 | fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index); |
| 435 | } else if (strcmp(s->type, "wifi-iface") == 0) { |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 436 | parsing_radio = FALSE; |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | uci_foreach_element(&s->options, option) { |
| 440 | |
| 441 | struct uci_option *op = uci_to_option(option); |
| 442 | if (parsing_radio == TRUE) { |
| 443 | // transform the type from input string and store the value in radio_param. |
| 444 | if (strcmp(op->e.name, "channel") == 0) |
| 445 | set_channel(&radio_param, op->v.string); |
| 446 | else if (strcmp(op->e.name, "hwmode") == 0) |
| 447 | set_hwmode(&radio_param, op->v.string); |
| 448 | else if (strcmp(op->e.name, "htmode") == 0) |
| 449 | set_htmode(&radio_param, op->v.string); |
| 450 | else if (strcmp(op->e.name, "disabled") == 0) |
| 451 | set_disable(&radio_param, op->v.string); |
| 452 | else if (strcmp(op->e.name, "band") == 0) |
| 453 | set_band(&radio_param, op->v.string); |
| 454 | else if (strcmp(op->e.name, "country") == 0) |
| 455 | set_country(&radio_param, op->v.string); |
| 456 | else if (strcmp(op->e.name, "noscan") == 0) |
developer | 54afa2c | 2022-10-18 17:44:13 +0800 | [diff] [blame] | 457 | set_noscan(&radio_param, op->v.string); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 458 | else |
| 459 | fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string); |
| 460 | } else { |
| 461 | // parsing iface |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 462 | if (strcmp(op->e.name, "device") == 0){ |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 463 | set_radionum(&ap_param, op->v.string); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 464 | if (ap_param.radio_index != -1){ |
| 465 | ap_param.ap_index = ap_param.radio_index + apCount[ap_param.radio_index]*max_radio_num; |
| 466 | fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index); |
| 467 | apCount[ap_param.radio_index] ++ ; |
| 468 | } |
| 469 | }else if (strcmp(op->e.name, "ssid") == 0){ |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 470 | set_ssid(&ap_param, op->v.string); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 471 | }else if (strcmp(op->e.name, "encryption") == 0){ |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 472 | set_encryption(&ap_param, op->v.string); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 473 | }else if (strcmp(op->e.name, "key") == 0){ |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 474 | set_key(&ap_param, op->v.string); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 475 | }else{ |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 476 | fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string); |
developer | 4c2edbe | 2022-10-18 16:36:37 +0800 | [diff] [blame] | 477 | } |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | if (parsing_radio == TRUE) |
| 481 | set_radio_param(radio_param); |
| 482 | else |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 483 | set_ap_param(ap_param, &vap_map[ap_param.radio_index]); |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 484 | } |
| 485 | |
developer | 41965b5 | 2022-10-19 17:40:23 +0800 | [diff] [blame] | 486 | for (i = 0; i < max_radio_num ;i++ ){ |
| 487 | ret = wifi_createVAP(i, &vap_map[i]); |
| 488 | if (ret != RETURN_OK) |
| 489 | fprintf(stderr, "[Apply vap setting failed!!!]\n"); |
| 490 | } |
| 491 | |
developer | 402bf2a | 2022-10-04 15:20:18 +0800 | [diff] [blame] | 492 | uci_unload(uci_ctx, uci_pkg); |
| 493 | uci_free_context(uci_ctx); |
| 494 | return RETURN_OK; |
| 495 | } |
| 496 | |
| 497 | int main(int argc, char **argv) |
| 498 | { |
| 499 | if (argc != 2 || strcmp(argv[1], "reload") != 0) { |
| 500 | fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n"); |
| 501 | return -1; |
| 502 | } |
| 503 | apply_uci_config(); |
| 504 | return 0; |
| 505 | } |