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 | |
| 8 | void set_channel(wifi_radio_param *radio_param, char *channel) |
| 9 | { |
| 10 | if (strcmp(channel, "auto") != 0) { |
| 11 | radio_param->auto_channel = FALSE; |
| 12 | radio_param->channel = atoi(channel); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | void set_band(wifi_radio_param *radio_param, char *country) |
| 17 | { |
| 18 | strcpy(radio_param->country, country); |
| 19 | } |
| 20 | |
| 21 | void set_country(wifi_radio_param *radio_param, char *band) |
| 22 | { |
| 23 | strcpy(radio_param->band, band); |
| 24 | } |
| 25 | |
| 26 | void set_hwmode(wifi_radio_param *radio_param, char *hwmode) |
| 27 | { |
| 28 | if (strncmp(hwmode, "11a", 3) == 0) |
| 29 | strcpy(radio_param->hwmode, "a"); |
| 30 | if (strncmp(hwmode, "11b", 3) == 0) |
| 31 | strcpy(radio_param->hwmode, "b"); |
| 32 | if (strncmp(hwmode, "11g", 3) == 0) |
| 33 | strcpy(radio_param->hwmode, "g"); |
| 34 | } |
| 35 | |
| 36 | void set_htmode(wifi_radio_param *radio_param, char *htmode) |
| 37 | { |
| 38 | char tmp[16] = {0}; |
| 39 | char *ptr = htmode; |
| 40 | ULONG bandwidth = 0; |
| 41 | radio_param->bandwidth = 20; |
| 42 | while (*ptr) { |
| 43 | if (isdigit(*ptr)) { |
| 44 | bandwidth = strtoul(ptr, NULL, 10); |
| 45 | radio_param->bandwidth = bandwidth; |
| 46 | break; |
| 47 | } |
| 48 | ptr++; |
| 49 | } |
| 50 | |
| 51 | // HT40 -> 11NGHT40PLUS |
| 52 | // VHT40+ -> 11ACVHT40PLUS |
| 53 | // HE80 -> 11AXHE80 |
| 54 | if (strstr(htmode, "+") != NULL) { |
| 55 | strncpy(tmp, htmode, strlen(htmode) - 1); |
| 56 | strcat(tmp, "PLUS"); |
| 57 | } else if (strstr(htmode, "-") != NULL) { |
| 58 | strncpy(tmp, htmode, strlen(htmode) - 1); |
| 59 | strcat(tmp, "MINUS"); |
| 60 | } else |
| 61 | strcpy(tmp, htmode); |
| 62 | |
| 63 | |
| 64 | if (strstr(htmode, "VHT") != NULL) { |
| 65 | snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AC%s", tmp); |
| 66 | } else if (strstr(htmode, "HT") != NULL && strstr(htmode, "NO") == NULL) { |
| 67 | snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11NG%s", tmp); |
| 68 | } else if (strstr(htmode, "HE") != NULL) { |
| 69 | snprintf(radio_param->htmode, sizeof(radio_param->htmode), "11AX%s", tmp); |
| 70 | } else { // NOHT or NONE should be parsed with the band, so just fill the original string. |
| 71 | strcpy(radio_param->htmode, tmp); |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | void set_disable(wifi_radio_param *radio_param, char *disable) |
| 77 | { |
| 78 | if (strcmp(disable, "1") == 0) |
| 79 | radio_param->disabled = TRUE; |
| 80 | else |
| 81 | radio_param->disabled = FALSE; |
| 82 | } |
| 83 | |
| 84 | void set_radionum(wifi_ap_param *ap_param, char *radio_name) |
| 85 | { |
| 86 | int radio_num; |
| 87 | char *ptr = radio_name; |
| 88 | |
| 89 | while (*ptr) { |
| 90 | if (isdigit(*ptr)) { |
| 91 | radio_num = strtoul(ptr, NULL, 10); |
| 92 | ap_param->radio_index = radio_num; |
| 93 | break; |
| 94 | } |
| 95 | ptr++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void set_ssid(wifi_ap_param *ap_param, char *ssid) |
| 100 | { |
| 101 | strncpy(ap_param->ssid, ssid, 32); |
| 102 | } |
| 103 | |
| 104 | void set_encryption(wifi_ap_param *ap_param, char *encryption_mode) |
| 105 | { |
| 106 | if (strstr(encryption_mode, "3") != NULL) { |
| 107 | strcpy(ap_param->enctyption_mode, "WPA3-"); |
| 108 | } else if (strstr(encryption_mode, "2") != NULL) { |
| 109 | strcpy(ap_param->enctyption_mode, "WPA2-"); |
| 110 | } else if (strstr(encryption_mode, "1") != NULL) { |
| 111 | strcpy(ap_param->enctyption_mode, "WPA-"); |
| 112 | } else if (strstr(encryption_mode, "mix") != NULL) { |
| 113 | strcpy(ap_param->enctyption_mode, "WPA-WPA2-"); |
| 114 | } |
| 115 | |
| 116 | if (strstr(encryption_mode, "psk") != NULL) { |
| 117 | strcat(ap_param->enctyption_mode, "Personal"); |
| 118 | } else if (strstr(encryption_mode, "wpa") != NULL) { |
| 119 | strcat(ap_param->enctyption_mode, "Enterprise"); |
| 120 | } |
| 121 | |
| 122 | if (strcmp(encryption_mode, "none") == 0) { |
| 123 | strcpy(ap_param->enctyption_mode, "None"); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void set_key(wifi_ap_param *ap_param, char *key) |
| 128 | { |
| 129 | strncpy(ap_param->password, key, 64); |
| 130 | } |
| 131 | |
| 132 | void set_radio_param(wifi_radio_param radio_parameter) |
| 133 | { |
| 134 | BOOL enable; |
| 135 | BOOL current; |
| 136 | char config_file[64] = {0}; |
| 137 | int ret = 0; |
| 138 | struct params param; |
| 139 | |
| 140 | fprintf(stderr, "Start setting radio\n"); |
| 141 | sprintf(config_file, "/nvram/hostapd%d.conf", radio_parameter.radio_index); |
| 142 | |
| 143 | // Call hal to set in this function |
| 144 | // Channel |
| 145 | fprintf(stderr, "Set channel: %d, bandwidth: %d\n", radio_parameter.channel, radio_parameter.bandwidth); |
| 146 | if (radio_parameter.auto_channel == TRUE) { |
| 147 | ret = wifi_setRadioAutoChannelEnable(radio_parameter.radio_index, TRUE); |
| 148 | } else { |
| 149 | // wifi_setRadioChannel(radio_parameter.radio_index, radio_parameter.channel); |
| 150 | ret = wifi_pushRadioChannel2(radio_parameter.radio_index, radio_parameter.channel, radio_parameter.bandwidth, 30); |
| 151 | } |
| 152 | if (ret != RETURN_OK) |
| 153 | fprintf(stderr, "[Set channel failed!!!]\n"); |
| 154 | ret = 0; |
| 155 | |
| 156 | // Country |
| 157 | fprintf(stderr, "Set Country: %s\n", radio_parameter.country); |
| 158 | ret = wifi_setRadioCountryCode(radio_parameter.radio_index, radio_parameter.country); |
| 159 | if (ret != RETURN_OK) |
| 160 | fprintf(stderr, "[Set Country failed!!!]\n"); |
| 161 | ret = 0; |
| 162 | |
| 163 | // hwmode |
| 164 | fprintf(stderr, "Set hwmode: %s\n", radio_parameter.hwmode); |
| 165 | ret = wifi_setRadioHwMode(radio_parameter.radio_index, radio_parameter.hwmode); |
| 166 | if (ret != RETURN_OK) |
| 167 | fprintf(stderr, "[Set hwmode failed!!!]\n"); |
| 168 | ret = 0; |
| 169 | |
| 170 | // htmode |
| 171 | wifi_ieee80211_Mode pure_mode; |
| 172 | if (strcmp(radio_parameter.band, "2g") == 0) { |
| 173 | |
| 174 | pure_mode |= WIFI_MODE_B | WIFI_MODE_G; |
| 175 | if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0) |
| 176 | strcpy(radio_parameter.htmode, "11G"); |
| 177 | |
| 178 | } else if (strcmp(radio_parameter.band, "5g") == 0) { |
| 179 | |
| 180 | pure_mode |= WIFI_MODE_A; |
| 181 | if (strcmp(radio_parameter.htmode, "NOHT") == 0 || strcmp(radio_parameter.htmode, "NONE") == 0) |
| 182 | strcpy(radio_parameter.htmode, "11A"); |
| 183 | } |
| 184 | |
| 185 | if (strstr(radio_parameter.htmode, "VHT") != NULL) |
| 186 | pure_mode |= WIFI_MODE_N | WIFI_MODE_AC; |
| 187 | else if (strstr(radio_parameter.htmode, "HT") != NULL && strstr(radio_parameter.htmode, "NO") == NULL) |
| 188 | pure_mode |= WIFI_MODE_N; |
| 189 | else if (strstr(radio_parameter.htmode, "HE") != NULL) |
| 190 | pure_mode |= WIFI_MODE_N | WIFI_MODE_AC | WIFI_MODE_AX; |
| 191 | |
| 192 | radio_parameter.pure_mode = (int)pure_mode; |
| 193 | fprintf(stderr, "Set htmode: %s, puremode: %d\n", radio_parameter.htmode, radio_parameter.pure_mode); |
| 194 | ret = wifi_setRadioMode(radio_parameter.radio_index, radio_parameter.htmode, radio_parameter.pure_mode); |
| 195 | if (ret != RETURN_OK) |
| 196 | fprintf(stderr, "[Set htmode failed!!!]\n"); |
| 197 | ret = 0; |
| 198 | |
| 199 | // disabled |
| 200 | if (radio_parameter.disabled == FALSE) |
| 201 | enable = TRUE; |
| 202 | ret = wifi_getRadioEnable(radio_parameter.radio_index, ¤t); |
| 203 | fprintf(stderr, "radio %d, current: %s, set: %s, getstatus return: %s\n", radio_parameter.radio_index, current ? "enable":"disable", radio_parameter.disabled ? "disable":"enable", (ret==RETURN_OK)? "success": "failed"); |
| 204 | // fprintf(stderr, "In if closure. enable: %d, current: %d.\n", enable, current); |
| 205 | if (enable != current) { |
| 206 | ret = wifi_setRadioEnable(radio_parameter.radio_index, enable); |
| 207 | } |
| 208 | if (ret != RETURN_OK) |
| 209 | fprintf(stderr, "[Set radio %s failed!!!]\n", enable?"enble":"disable"); |
| 210 | ret = 0; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | void set_ap_param(wifi_ap_param ap_param) |
| 215 | { |
| 216 | int ret = 0; |
| 217 | |
| 218 | fprintf(stderr, "Start setting ap\n"); |
| 219 | // Call hal to set in this function |
| 220 | // SSID |
| 221 | fprintf(stderr, "Set SSID: %s\n", ap_param.ssid); |
| 222 | ret = wifi_setSSIDName(ap_param.ap_index, ap_param.ssid); |
| 223 | if (ret != RETURN_OK) |
| 224 | fprintf(stderr, "[Set SSID failed!!!]\n"); |
| 225 | |
| 226 | // wpa and security mode |
| 227 | fprintf(stderr, "Set encryption mode: %s\n", ap_param.enctyption_mode); |
| 228 | ret = wifi_setApSecurityModeEnabled(ap_param.ap_index, ap_param.enctyption_mode); |
| 229 | if (ret != RETURN_OK) |
| 230 | fprintf(stderr, "[Set encryption mode failed!!!]\n"); |
| 231 | |
| 232 | // key |
| 233 | if (strlen(ap_param.password) > 0) { |
| 234 | fprintf(stderr, "Set password: %s\n", ap_param.password); |
| 235 | ret = wifi_setApSecurityKeyPassphrase(ap_param.ap_index, ap_param.password); |
| 236 | if (ret != RETURN_OK) |
| 237 | fprintf(stderr, "[Set password failed!!!]\n"); |
| 238 | } |
| 239 | |
| 240 | // restart ap |
| 241 | wifi_setApEnable(ap_param.ap_index, FALSE); |
| 242 | wifi_setApEnable(ap_param.ap_index, TRUE); |
| 243 | } |
| 244 | |
| 245 | int apply_uci_config () |
| 246 | { |
| 247 | struct uci_context *uci_ctx = uci_alloc_context(); |
| 248 | struct uci_package *uci_pkg = NULL; |
| 249 | struct uci_element *e; |
| 250 | // struct uci_section *s; |
| 251 | const char cfg_name[] = "wireless"; |
| 252 | int max_radio_num = 0; |
| 253 | BOOL parsing_radio = FALSE; |
| 254 | |
| 255 | wifi_getMaxRadioNumber(&max_radio_num); |
| 256 | fprintf(stderr, "max radio number: %d\n", max_radio_num); |
| 257 | if (uci_load(uci_ctx, cfg_name, &uci_pkg) != UCI_OK) { |
| 258 | uci_free_context(uci_ctx); |
| 259 | fprintf(stderr, "%s: load uci failed.\n", __func__); |
| 260 | return RETURN_ERR; |
| 261 | } |
| 262 | |
| 263 | uci_foreach_element(&uci_pkg->sections, e) { |
| 264 | |
| 265 | struct uci_section *s = uci_to_section(e); |
| 266 | struct uci_element *option = NULL; |
| 267 | wifi_radio_param radio_param = {0}; |
| 268 | wifi_ap_param ap_param = {0}; |
| 269 | radio_param.radio_index = -1; |
| 270 | ap_param.ap_index = -1; |
| 271 | |
| 272 | if (strcmp(s->type, "wifi-device") == 0) { |
| 273 | sscanf(s->e.name, "radio%d", &radio_param.radio_index); |
| 274 | parsing_radio = TRUE; |
| 275 | fprintf(stderr, "\n----- Start parsing radio %d config. -----\n", radio_param.radio_index); |
| 276 | } else if (strcmp(s->type, "wifi-iface") == 0) { |
| 277 | sscanf(s->e.name, "default_radio%d", &ap_param.ap_index); |
| 278 | parsing_radio = FALSE; |
| 279 | fprintf(stderr, "\n----- Start parsing ap %d config. -----\n", ap_param.ap_index); |
| 280 | } |
| 281 | |
| 282 | uci_foreach_element(&s->options, option) { |
| 283 | |
| 284 | struct uci_option *op = uci_to_option(option); |
| 285 | if (parsing_radio == TRUE) { |
| 286 | // transform the type from input string and store the value in radio_param. |
| 287 | if (strcmp(op->e.name, "channel") == 0) |
| 288 | set_channel(&radio_param, op->v.string); |
| 289 | else if (strcmp(op->e.name, "hwmode") == 0) |
| 290 | set_hwmode(&radio_param, op->v.string); |
| 291 | else if (strcmp(op->e.name, "htmode") == 0) |
| 292 | set_htmode(&radio_param, op->v.string); |
| 293 | else if (strcmp(op->e.name, "disabled") == 0) |
| 294 | set_disable(&radio_param, op->v.string); |
| 295 | else if (strcmp(op->e.name, "band") == 0) |
| 296 | set_band(&radio_param, op->v.string); |
| 297 | else if (strcmp(op->e.name, "country") == 0) |
| 298 | set_country(&radio_param, op->v.string); |
| 299 | else if (strcmp(op->e.name, "noscan") == 0) |
| 300 | set_band(&radio_param, op->v.string); |
| 301 | else |
| 302 | fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string); |
| 303 | } else { |
| 304 | // parsing iface |
| 305 | if (strcmp(op->e.name, "device") == 0) |
| 306 | set_radionum(&ap_param, op->v.string); |
| 307 | else if (strcmp(op->e.name, "ssid") == 0) |
| 308 | set_ssid(&ap_param, op->v.string); |
| 309 | else if (strcmp(op->e.name, "encryption") == 0) |
| 310 | set_encryption(&ap_param, op->v.string); |
| 311 | else if (strcmp(op->e.name, "key") == 0) |
| 312 | set_key(&ap_param, op->v.string); |
| 313 | else |
| 314 | fprintf(stderr, "[%s %s not set!]\n", op->e.name, op->v.string); |
| 315 | } |
| 316 | } |
| 317 | if (parsing_radio == TRUE) |
| 318 | set_radio_param(radio_param); |
| 319 | else |
| 320 | set_ap_param(ap_param); |
| 321 | } |
| 322 | |
| 323 | uci_unload(uci_ctx, uci_pkg); |
| 324 | uci_free_context(uci_ctx); |
| 325 | return RETURN_OK; |
| 326 | } |
| 327 | |
| 328 | int main(int argc, char **argv) |
| 329 | { |
| 330 | if (argc != 2 || strcmp(argv[1], "reload") != 0) { |
| 331 | fprintf(stderr, "Usage: wifi reload.\nThis tool is only for RDKB MSP/SQC test.\n"); |
| 332 | return -1; |
| 333 | } |
| 334 | apply_uci_config(); |
| 335 | return 0; |
| 336 | } |