Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sigma Control API DUT (station/AP) |
Amarnath Hullur Subramanyam | 659a34c | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Qualcomm Atheros, Inc. |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 4 | * All Rights Reserved. |
| 5 | * Licensed under the Clear BSD license. See README for more details. |
| 6 | */ |
| 7 | |
| 8 | #include "sigma_dut.h" |
| 9 | #include <sys/stat.h> |
| 10 | #include "wpa_helpers.h" |
| 11 | |
| 12 | enum driver_type wifi_chip_type = DRIVER_NOT_SET; |
| 13 | enum openwrt_driver_type openwrt_chip_type = OPENWRT_DRIVER_NOT_SET; |
| 14 | |
| 15 | |
| 16 | int file_exists(const char *fname) |
| 17 | { |
| 18 | struct stat s; |
| 19 | return stat(fname, &s) == 0; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | int set_wifi_chip(const char *chip_type) |
| 24 | { |
| 25 | if (!strncmp(chip_type, "WCN", strlen("WCN"))) |
| 26 | wifi_chip_type = DRIVER_WCN; |
| 27 | else if (!strncmp(chip_type, "ATHEROS", strlen("ATHEROS"))) |
| 28 | wifi_chip_type = DRIVER_ATHEROS; |
| 29 | else if (!strncmp(chip_type, "AR6003", strlen("AR6003"))) |
| 30 | wifi_chip_type = DRIVER_AR6003; |
| 31 | else if (strcmp(chip_type, "MAC80211") == 0) |
| 32 | wifi_chip_type = DRIVER_MAC80211; |
| 33 | else if (strcmp(chip_type, "QNXNTO") == 0) |
| 34 | wifi_chip_type = DRIVER_QNXNTO; |
| 35 | else if (strcmp(chip_type, "OPENWRT") == 0) |
| 36 | wifi_chip_type = DRIVER_OPENWRT; |
Sreelakshmi Konamki | b692f10 | 2016-04-26 19:47:00 +0530 | [diff] [blame] | 37 | else if (!strncmp(chip_type, "LINUX-WCN", strlen("LINUX-WCN"))) |
| 38 | wifi_chip_type = DRIVER_LINUX_WCN; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 39 | else |
| 40 | return -1; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | enum driver_type get_driver_type(void) |
| 47 | { |
| 48 | struct stat s; |
| 49 | if (wifi_chip_type == DRIVER_NOT_SET) { |
| 50 | /* Check for 60G driver */ |
| 51 | ssize_t len; |
| 52 | char link[256]; |
| 53 | char buf[256]; |
| 54 | char *ifname = get_station_ifname(); |
| 55 | |
| 56 | snprintf(buf, sizeof(buf), "/sys/class/net/%s/device/driver", |
| 57 | ifname); |
| 58 | len = readlink(buf, link, sizeof(link) - 1); |
| 59 | if (len >= 0) { |
| 60 | link[len] = '\0'; |
| 61 | if (strstr(link, DRIVER_NAME_60G)) |
| 62 | return DRIVER_WIL6210; |
| 63 | } |
| 64 | |
| 65 | if (stat("/sys/module/mac80211", &s) == 0) |
| 66 | return DRIVER_MAC80211; |
| 67 | return DRIVER_ATHEROS; |
| 68 | } |
| 69 | return wifi_chip_type; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | enum openwrt_driver_type get_openwrt_driver_type(void) |
| 74 | { |
| 75 | struct stat s; |
| 76 | |
| 77 | if (openwrt_chip_type == OPENWRT_DRIVER_NOT_SET) { |
Sarvepalli, Rajesh Babu | a62e20a | 2016-07-08 18:10:30 +0530 | [diff] [blame] | 78 | if (stat("/sys/module/umac", &s) == 0) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 79 | openwrt_chip_type = OPENWRT_DRIVER_ATHEROS; |
| 80 | } |
| 81 | |
| 82 | return openwrt_chip_type; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | enum sigma_program sigma_program_to_enum(const char *prog) |
| 87 | { |
| 88 | if (prog == NULL) |
| 89 | return PROGRAM_UNKNOWN; |
| 90 | |
| 91 | if (strcasecmp(prog, "TDLS") == 0) |
| 92 | return PROGRAM_TDLS; |
| 93 | if (strcasecmp(prog, "HS2") == 0) |
| 94 | return PROGRAM_HS2; |
| 95 | if (strcasecmp(prog, "HS2_R2") == 0 || |
| 96 | strcasecmp(prog, "HS2-R2") == 0) |
| 97 | return PROGRAM_HS2_R2; |
| 98 | if (strcasecmp(prog, "WFD") == 0) |
| 99 | return PROGRAM_WFD; |
Amarnath Hullur Subramanyam | 659a34c | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 100 | if (strcasecmp(prog, "DisplayR2") == 0) |
| 101 | return PROGRAM_DISPLAYR2; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 102 | if (strcasecmp(prog, "PMF") == 0) |
| 103 | return PROGRAM_PMF; |
| 104 | if (strcasecmp(prog, "WPS") == 0) |
| 105 | return PROGRAM_WPS; |
| 106 | if (strcasecmp(prog, "11n") == 0) |
| 107 | return PROGRAM_HT; |
| 108 | if (strcasecmp(prog, "VHT") == 0) |
| 109 | return PROGRAM_VHT; |
| 110 | if (strcasecmp(prog, "60GHZ") == 0) |
| 111 | return PROGRAM_60GHZ; |
| 112 | if (strcasecmp(prog, "NAN") == 0) |
| 113 | return PROGRAM_NAN; |
priyadharshini gowthaman | 12dd414 | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 114 | if (strcasecmp(prog, "LOC") == 0) |
| 115 | return PROGRAM_LOC; |
priyadharshini gowthaman | b4e05fc | 2016-10-13 15:02:35 -0700 | [diff] [blame] | 116 | if (strcasecmp(prog, "MBO") == 0) |
| 117 | return PROGRAM_MBO; |
Adil Saeed Musthafa | 33ea287 | 2017-04-10 23:13:24 -0700 | [diff] [blame] | 118 | if (strcasecmp(prog, "IoTLP") == 0) |
| 119 | return PROGRAM_IOTLP; |
Jouni Malinen | 5c3813a | 2017-08-26 13:30:39 +0300 | [diff] [blame] | 120 | if (strcasecmp(prog, "DPP") == 0) |
| 121 | return PROGRAM_DPP; |
Ankita Bajaj | a2cb567 | 2017-10-25 16:08:28 +0530 | [diff] [blame] | 122 | if (strcasecmp(prog, "OCE") == 0) |
| 123 | return PROGRAM_OCE; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 124 | |
| 125 | return PROGRAM_UNKNOWN; |
| 126 | } |
priyadharshini gowthaman | 72462ef | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 127 | |
| 128 | |
| 129 | static int parse_hex(char c) |
| 130 | { |
| 131 | if (c >= '0' && c <= '9') |
| 132 | return c - '0'; |
| 133 | if (c >= 'a' && c <= 'f') |
| 134 | return c - 'a' + 10; |
| 135 | if (c >= 'A' && c <= 'F') |
| 136 | return c - 'A' + 10; |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | static int hex_byte(const char *str) |
| 142 | { |
| 143 | int res1, res2; |
| 144 | |
| 145 | res1 = parse_hex(str[0]); |
| 146 | if (res1 < 0) |
| 147 | return -1; |
| 148 | res2 = parse_hex(str[1]); |
| 149 | if (res2 < 0) |
| 150 | return -1; |
| 151 | return (res1 << 4) | res2; |
| 152 | } |
| 153 | |
| 154 | |
Jouni Malinen | 08cf231 | 2017-09-04 13:39:47 +0300 | [diff] [blame] | 155 | int parse_hexstr(const char *hex, unsigned char *buf, size_t buflen) |
| 156 | { |
| 157 | size_t i; |
| 158 | const char *pos = hex; |
| 159 | |
| 160 | for (i = 0; i < buflen; i++) { |
| 161 | int val; |
| 162 | |
| 163 | if (*pos == '\0') |
| 164 | break; |
| 165 | val = hex_byte(pos); |
| 166 | if (val < 0) |
| 167 | return -1; |
| 168 | buf[i] = val; |
| 169 | pos += 2; |
| 170 | } |
| 171 | |
| 172 | return i; |
| 173 | } |
| 174 | |
| 175 | |
priyadharshini gowthaman | 72462ef | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 176 | int parse_mac_address(struct sigma_dut *dut, const char *arg, |
| 177 | unsigned char *addr) |
| 178 | { |
| 179 | int i; |
| 180 | const char *pos = arg; |
| 181 | |
| 182 | if (strlen(arg) != 17) |
| 183 | goto fail; |
| 184 | |
| 185 | for (i = 0; i < ETH_ALEN; i++) { |
| 186 | int val; |
| 187 | |
| 188 | val = hex_byte(pos); |
| 189 | if (val < 0) |
| 190 | goto fail; |
| 191 | addr[i] = val; |
| 192 | if (i + 1 < ETH_ALEN) { |
| 193 | pos += 2; |
| 194 | if (*pos != ':') |
| 195 | goto fail; |
| 196 | pos++; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | |
| 202 | fail: |
| 203 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 204 | "Invalid MAC address %s (expected format xx:xx:xx:xx:xx:xx)", |
| 205 | arg); |
| 206 | return -1; |
| 207 | } |
Rakesh Sunki | 7192dc4 | 2017-03-30 14:47:55 -0700 | [diff] [blame] | 208 | |
| 209 | |
| 210 | unsigned int channel_to_freq(unsigned int channel) |
| 211 | { |
| 212 | if (channel >= 1 && channel <= 13) |
| 213 | return 2407 + 5 * channel; |
| 214 | if (channel == 14) |
| 215 | return 2484; |
| 216 | if (channel >= 36 && channel <= 165) |
| 217 | return 5000 + 5 * channel; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | unsigned int freq_to_channel(unsigned int freq) |
| 224 | { |
| 225 | if (freq >= 2412 && freq <= 2472) |
| 226 | return (freq - 2407) / 5; |
| 227 | if (freq == 2484) |
| 228 | return 14; |
| 229 | if (freq >= 5180 && freq <= 5825) |
| 230 | return (freq - 5000) / 5; |
| 231 | return 0; |
| 232 | } |
Peng Xu | 769731a | 2017-05-10 17:27:28 -0700 | [diff] [blame] | 233 | |
| 234 | |
Rakesh Sunki | 8f8e74b | 2017-05-16 15:42:12 -0700 | [diff] [blame] | 235 | void convert_mac_addr_to_ipv6_lladdr(u8 *mac_addr, char *ipv6_buf, |
| 236 | size_t buf_len) |
| 237 | { |
| 238 | u8 temp = mac_addr[0] ^ 0x02; |
| 239 | |
| 240 | snprintf(ipv6_buf, buf_len, "fe80::%02x%02x:%02xff:fe%02x:%02x%02x", |
| 241 | temp, mac_addr[1], mac_addr[2], |
| 242 | mac_addr[3], mac_addr[4], mac_addr[5]); |
| 243 | } |
| 244 | |
| 245 | |
Peng Xu | 769731a | 2017-05-10 17:27:28 -0700 | [diff] [blame] | 246 | #ifndef ANDROID |
| 247 | |
| 248 | size_t strlcpy(char *dest, const char *src, size_t siz) |
| 249 | { |
| 250 | const char *s = src; |
| 251 | size_t left = siz; |
| 252 | |
| 253 | if (left) { |
| 254 | /* Copy string up to the maximum size of the dest buffer */ |
| 255 | while (--left != 0) { |
| 256 | if ((*dest++ = *s++) == '\0') |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (left == 0) { |
| 262 | /* Not enough room for the string; force NUL-termination */ |
| 263 | if (siz != 0) |
| 264 | *dest = '\0'; |
| 265 | while (*s++) |
| 266 | ; /* determine total src string length */ |
| 267 | } |
| 268 | |
| 269 | return s - src - 1; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | size_t strlcat(char *dst, const char *str, size_t size) |
| 274 | { |
| 275 | char *pos; |
| 276 | size_t dstlen, srclen, copy; |
| 277 | |
| 278 | srclen = strlen(str); |
| 279 | for (pos = dst; pos - dst < size && *dst; pos++) |
| 280 | ; |
| 281 | dstlen = pos - dst; |
| 282 | if (*dst) |
| 283 | return dstlen + srclen; |
| 284 | if (dstlen + srclen + 1 > size) |
| 285 | copy = size - dstlen - 1; |
| 286 | else |
| 287 | copy = srclen; |
| 288 | memcpy(pos, str, copy); |
| 289 | pos[copy] = '\0'; |
| 290 | return dstlen + srclen; |
| 291 | } |
| 292 | |
| 293 | #endif /* ANDROID */ |