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