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