xref: /wlan-dirver/utils/sigma-dut/basic.c (revision 77dda64736bb372cb56df00d2e6601cdf8e6e971)
1 /*
2  * Sigma Control API DUT (station/AP)
3  * Copyright (c) 2010, Atheros Communications, Inc.
4  * Copyright (c) 2011-2014, 2016-2017, Qualcomm Atheros, Inc.
5  * All Rights Reserved.
6  * Licensed under the Clear BSD license. See README for more details.
7  */
8 
9 #include "sigma_dut.h"
10 #ifdef __linux__
11 #include <sys/stat.h>
12 #include <linux/ethtool.h>
13 #include <linux/netlink.h>
14 #include <linux/sockios.h>
15 #endif /* __linux__ */
16 #include "wpa_helpers.h"
17 #include <sys/ioctl.h>
18 
19 
cmd_ca_get_version(struct sigma_dut * dut,struct sigma_conn * conn,struct sigma_cmd * cmd)20 static enum sigma_cmd_result cmd_ca_get_version(struct sigma_dut *dut,
21 						struct sigma_conn *conn,
22 						struct sigma_cmd *cmd)
23 {
24 	const char *info;
25 
26 	info = get_param(cmd, "TestInfo");
27 	if (info) {
28 		char buf[200];
29 		snprintf(buf, sizeof(buf), "NOTE CAPI:TestInfo:%s", info);
30 		wpa_command(get_main_ifname(dut), buf);
31 	}
32 
33 	send_resp(dut, conn, SIGMA_COMPLETE, "version,1.0");
34 	return STATUS_SENT;
35 }
36 
37 
38 #ifdef __linux__
39 
first_line(char * s)40 static void first_line(char *s)
41 {
42 	while (*s) {
43 		if (*s == '\r' || *s == '\n') {
44 			*s = '\0';
45 			return;
46 		}
47 		s++;
48 	}
49 }
50 
51 
get_ver(const char * cmd,char * buf,size_t buflen)52 void get_ver(const char *cmd, char *buf, size_t buflen)
53 {
54 	FILE *f;
55 	char *pos;
56 
57 	buf[0] = '\0';
58 	f = popen(cmd, "r");
59 	if (f == NULL)
60 		return;
61 	if (fgets(buf, buflen, f))
62 		first_line(buf);
63 	pclose(f);
64 
65 	pos = strstr(buf, " v");
66 	if (pos == NULL)
67 		buf[0] = '\0';
68 	else
69 		memmove(buf, pos + 1, strlen(pos));
70 }
71 
72 #endif /* __linux__ */
73 
74 
cmd_device_get_info(struct sigma_dut * dut,struct sigma_conn * conn,struct sigma_cmd * cmd)75 static enum sigma_cmd_result cmd_device_get_info(struct sigma_dut *dut,
76 						 struct sigma_conn *conn,
77 						 struct sigma_cmd *cmd)
78 {
79 	const char *vendor = "Qualcomm Atheros";
80 	const char *model = "N/A";
81 	const char *version = "N/A";
82 #ifdef __linux__
83 	char model_buf[128];
84 	char ver_buf[512];
85 #endif /* __linux__ */
86 	int res;
87 	char resp[512];
88 
89 #ifdef __linux__
90 	{
91 		char fname[128], path[128];
92 		struct stat s;
93 		FILE *f;
94 		char compat_ver[128];
95 		char wpa_supplicant_ver[128];
96 		char hostapd_ver[128];
97 		char host_fw_ver[128];
98 
99 		snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211",
100 			 get_main_ifname(dut));
101 		if (stat(path, &s) == 0) {
102 			ssize_t res;
103 			char *pos;
104 
105 			res = snprintf(fname, sizeof(fname),
106 				       "/sys/class/net/%s/device/driver",
107 				       get_main_ifname(dut));
108 			if (res < 0 || res >= sizeof(fname)) {
109 				model = "Linux/";
110 			} else if ((res = readlink(fname, path,
111 						   sizeof(path))) < 0) {
112 				model = "Linux/";
113 			} else {
114 				if (res >= (int) sizeof(path))
115 					res = sizeof(path) - 1;
116 				path[res] = '\0';
117 				pos = strrchr(path, '/');
118 				if (pos == NULL)
119 					pos = path;
120 				else
121 					pos++;
122 				res = snprintf(model_buf, sizeof(model_buf),
123 					       "Linux/%s", pos);
124 				if (res >= 0 && res < sizeof(model_buf))
125 					model = model_buf;
126 			}
127 		} else
128 			model = "Linux";
129 
130 		/* TODO: get version from wpa_supplicant (+ driver via wpa_s)
131 		 */
132 
133 		f = fopen("/sys/module/compat/parameters/"
134 			  "backported_kernel_version", "r");
135 		if (f == NULL)
136 			f = fopen("/sys/module/compat/parameters/"
137 				  "compat_version", "r");
138 		if (f) {
139 			if (fgets(compat_ver, sizeof(compat_ver), f) == NULL)
140 				compat_ver[0] = '\0';
141 			else
142 				first_line(compat_ver);
143 			fclose(f);
144 		} else
145 			compat_ver[0] = '\0';
146 
147 		get_ver("./hostapd -v 2>&1", hostapd_ver, sizeof(hostapd_ver));
148 		if (hostapd_ver[0] == '\0')
149 			get_ver("hostapd -v 2>&1", hostapd_ver,
150 				sizeof(hostapd_ver));
151 		get_ver("./wpa_supplicant -v", wpa_supplicant_ver,
152 			sizeof(wpa_supplicant_ver));
153 		if (wpa_supplicant_ver[0] == '\0')
154 			get_ver("wpa_supplicant -v", wpa_supplicant_ver,
155 				sizeof(wpa_supplicant_ver));
156 
157 		host_fw_ver[0] = '\0';
158 		if (get_driver_type(dut) == DRIVER_WCN ||
159 		    get_driver_type(dut) == DRIVER_LINUX_WCN) {
160 			get_ver("iwpriv wlan0 version", host_fw_ver,
161 				sizeof(host_fw_ver));
162 		} else if (get_driver_type(dut) == DRIVER_WIL6210) {
163 			struct ethtool_drvinfo drvinfo;
164 			struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
165 			int fd; /* socket suitable for ethtool ioctl */
166 
167 			memset(&drvinfo, 0, sizeof(drvinfo));
168 			drvinfo.cmd = ETHTOOL_GDRVINFO;
169 
170 			memset(&ifr, 0, sizeof(ifr));
171 			strlcpy(ifr.ifr_name, get_main_ifname(dut),
172 				sizeof(ifr.ifr_name));
173 
174 			fd = socket(AF_INET, SOCK_DGRAM, 0);
175 			if (fd < 0)
176 				fd = socket(AF_NETLINK, SOCK_RAW,
177 					    NETLINK_GENERIC);
178 			if (fd >= 0) {
179 				ifr.ifr_data = (void *) &drvinfo;
180 				if (ioctl(fd, SIOCETHTOOL, &ifr) == 0)
181 					strlcpy(host_fw_ver, drvinfo.fw_version,
182 						sizeof(host_fw_ver));
183 				close(fd);
184 			}
185 		}
186 		res = snprintf(ver_buf, sizeof(ver_buf),
187 			       "drv=%s%s%s%s%s%s%s/sigma=" SIGMA_DUT_VER "%s%s",
188 			       compat_ver,
189 			       wpa_supplicant_ver[0] ? "/wpas=" : "",
190 			       wpa_supplicant_ver,
191 			       hostapd_ver[0] ? "/hapd=" : "",
192 			       hostapd_ver,
193 			       host_fw_ver[0] ? "/wlan=" : "",
194 			       host_fw_ver,
195 			       dut->version ? "@" : "",
196 			       dut->version ? dut->version : "");
197 		if (res < 0 || res >= sizeof(ver_buf))
198 			return ERROR_SEND_STATUS;
199 		version = ver_buf;
200 	}
201 #endif /* __linux__ */
202 
203 	if (dut->vendor_name)
204 		vendor = dut->vendor_name;
205 	if (dut->model_name)
206 		model = dut->model_name;
207 	if (dut->version_name)
208 		version = dut->version_name;
209 	res = snprintf(resp, sizeof(resp), "vendor,%s,model,%s,version,%s",
210 		       vendor, model, version);
211 	if (res < 0 || res >= sizeof(resp))
212 		return ERROR_SEND_STATUS;
213 
214 	send_resp(dut, conn, SIGMA_COMPLETE, resp);
215 	return STATUS_SENT;
216 }
217 
218 
check_device_list_interfaces(struct sigma_cmd * cmd)219 static int check_device_list_interfaces(struct sigma_cmd *cmd)
220 {
221 	if (get_param(cmd, "interfaceType") == NULL)
222 		return -1;
223 	return 0;
224 }
225 
226 
cmd_device_list_interfaces(struct sigma_dut * dut,struct sigma_conn * conn,struct sigma_cmd * cmd)227 static enum sigma_cmd_result cmd_device_list_interfaces(struct sigma_dut *dut,
228 							struct sigma_conn *conn,
229 							struct sigma_cmd *cmd)
230 {
231 	const char *type, *band;
232 	char resp[200];
233 
234 	type = get_param(cmd, "interfaceType");
235 	if (type == NULL)
236 		return -1;
237 	sigma_dut_print(dut, DUT_MSG_DEBUG, "device_list_interfaces - "
238 			"interfaceType=%s", type);
239 	if (strcmp(type, "802.11") != 0)
240 		return ERROR_SEND_STATUS;
241 
242 	band = get_param(cmd, "band");
243 	if (!band) {
244 	} else if (strcasecmp(band, "24g") == 0) {
245 		dut->use_5g = 0;
246 	} else if (strcasecmp(band, "5g") == 0) {
247 		dut->use_5g = 1;
248 	} else {
249 		send_resp(dut, conn, SIGMA_COMPLETE,
250 			  "errorCode,Unsupported band value");
251 		return STATUS_SENT_ERROR;
252 	}
253 	snprintf(resp, sizeof(resp), "interfaceType,802.11,interfaceID,%s",
254 		 get_main_ifname(dut));
255 	send_resp(dut, conn, SIGMA_COMPLETE, resp);
256 	return STATUS_SENT;
257 }
258 
259 
basic_register_cmds(void)260 void basic_register_cmds(void)
261 {
262 	sigma_dut_reg_cmd("ca_get_version", NULL, cmd_ca_get_version);
263 	sigma_dut_reg_cmd("device_get_info", NULL, cmd_device_get_info);
264 	sigma_dut_reg_cmd("device_list_interfaces",
265 			  check_device_list_interfaces,
266 			  cmd_device_list_interfaces);
267 }
268