blob: 292d6e915839472d85fd1319db0064a26394731f [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
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
12enum driver_type wifi_chip_type = DRIVER_NOT_SET;
13enum openwrt_driver_type openwrt_chip_type = OPENWRT_DRIVER_NOT_SET;
14
15
16int file_exists(const char *fname)
17{
18 struct stat s;
19 return stat(fname, &s) == 0;
20}
21
22
23int 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 Konamkib692f102016-04-26 19:47:00 +053037 else if (!strncmp(chip_type, "LINUX-WCN", strlen("LINUX-WCN")))
38 wifi_chip_type = DRIVER_LINUX_WCN;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020039 else
40 return -1;
41
42 return 0;
43}
44
45
46enum 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
73enum 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 Babua62e20a2016-07-08 18:10:30 +053078 if (stat("/sys/module/umac", &s) == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020079 openwrt_chip_type = OPENWRT_DRIVER_ATHEROS;
80 }
81
82 return openwrt_chip_type;
83}
84
85
86enum 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 gowthaman12dd4142016-06-22 22:47:14 -0700112 if (strcasecmp(prog, "LOC") == 0)
113 return PROGRAM_LOC;
priyadharshini gowthamanb4e05fc2016-10-13 15:02:35 -0700114 if (strcasecmp(prog, "MBO") == 0)
115 return PROGRAM_MBO;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200116
117 return PROGRAM_UNKNOWN;
118}
priyadharshini gowthaman72462ef2016-06-22 22:47:14 -0700119
120
121static int parse_hex(char c)
122{
123 if (c >= '0' && c <= '9')
124 return c - '0';
125 if (c >= 'a' && c <= 'f')
126 return c - 'a' + 10;
127 if (c >= 'A' && c <= 'F')
128 return c - 'A' + 10;
129 return -1;
130}
131
132
133static int hex_byte(const char *str)
134{
135 int res1, res2;
136
137 res1 = parse_hex(str[0]);
138 if (res1 < 0)
139 return -1;
140 res2 = parse_hex(str[1]);
141 if (res2 < 0)
142 return -1;
143 return (res1 << 4) | res2;
144}
145
146
147int parse_mac_address(struct sigma_dut *dut, const char *arg,
148 unsigned char *addr)
149{
150 int i;
151 const char *pos = arg;
152
153 if (strlen(arg) != 17)
154 goto fail;
155
156 for (i = 0; i < ETH_ALEN; i++) {
157 int val;
158
159 val = hex_byte(pos);
160 if (val < 0)
161 goto fail;
162 addr[i] = val;
163 if (i + 1 < ETH_ALEN) {
164 pos += 2;
165 if (*pos != ':')
166 goto fail;
167 pos++;
168 }
169 }
170
171 return 0;
172
173fail:
174 sigma_dut_print(dut, DUT_MSG_ERROR,
175 "Invalid MAC address %s (expected format xx:xx:xx:xx:xx:xx)",
176 arg);
177 return -1;
178}