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