Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sigma Control API DUT (station/AP) |
| 3 | * Copyright (c) 2014-2015, Qualcomm Atheros, Inc. |
| 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; |
| 100 | if (strcasecmp(prog, "PMF") == 0) |
| 101 | return PROGRAM_PMF; |
| 102 | if (strcasecmp(prog, "WPS") == 0) |
| 103 | return PROGRAM_WPS; |
| 104 | if (strcasecmp(prog, "11n") == 0) |
| 105 | return PROGRAM_HT; |
| 106 | if (strcasecmp(prog, "VHT") == 0) |
| 107 | return PROGRAM_VHT; |
| 108 | if (strcasecmp(prog, "60GHZ") == 0) |
| 109 | return PROGRAM_60GHZ; |
| 110 | if (strcasecmp(prog, "NAN") == 0) |
| 111 | return PROGRAM_NAN; |
priyadharshini gowthaman | 12dd414 | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 112 | if (strcasecmp(prog, "LOC") == 0) |
| 113 | return PROGRAM_LOC; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 114 | |
| 115 | return PROGRAM_UNKNOWN; |
| 116 | } |
priyadharshini gowthaman | 72462ef | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | static int parse_hex(char c) |
| 120 | { |
| 121 | if (c >= '0' && c <= '9') |
| 122 | return c - '0'; |
| 123 | if (c >= 'a' && c <= 'f') |
| 124 | return c - 'a' + 10; |
| 125 | if (c >= 'A' && c <= 'F') |
| 126 | return c - 'A' + 10; |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int hex_byte(const char *str) |
| 132 | { |
| 133 | int res1, res2; |
| 134 | |
| 135 | res1 = parse_hex(str[0]); |
| 136 | if (res1 < 0) |
| 137 | return -1; |
| 138 | res2 = parse_hex(str[1]); |
| 139 | if (res2 < 0) |
| 140 | return -1; |
| 141 | return (res1 << 4) | res2; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | int parse_mac_address(struct sigma_dut *dut, const char *arg, |
| 146 | unsigned char *addr) |
| 147 | { |
| 148 | int i; |
| 149 | const char *pos = arg; |
| 150 | |
| 151 | if (strlen(arg) != 17) |
| 152 | goto fail; |
| 153 | |
| 154 | for (i = 0; i < ETH_ALEN; i++) { |
| 155 | int val; |
| 156 | |
| 157 | val = hex_byte(pos); |
| 158 | if (val < 0) |
| 159 | goto fail; |
| 160 | addr[i] = val; |
| 161 | if (i + 1 < ETH_ALEN) { |
| 162 | pos += 2; |
| 163 | if (*pos != ':') |
| 164 | goto fail; |
| 165 | pos++; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | |
| 171 | fail: |
| 172 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 173 | "Invalid MAC address %s (expected format xx:xx:xx:xx:xx:xx)", |
| 174 | arg); |
| 175 | return -1; |
| 176 | } |