blob: 844dc06687914fefcf3da355dc9c01ef38af6738 [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (station/AP)
3 * Copyright (c) 2010, Atheros Communications, Inc.
4 * Copyright (c) 2011-2014, 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#endif /* __linux__ */
13#include "wpa_helpers.h"
14
15
16static int cmd_ca_get_version(struct sigma_dut *dut, struct sigma_conn *conn,
17 struct sigma_cmd *cmd)
18{
19 const char *info;
20
21 info = get_param(cmd, "TestInfo");
22 if (info) {
23 char buf[200];
24 snprintf(buf, sizeof(buf), "NOTE CAPI:TestInfo:%s", info);
25 wpa_command(get_main_ifname(), buf);
26 }
27
28 send_resp(dut, conn, SIGMA_COMPLETE, "version,1.0");
29 return 0;
30}
31
32
33#ifdef __linux__
34
35static void first_line(char *s)
36{
37 while (*s) {
38 if (*s == '\r' || *s == '\n') {
39 *s = '\0';
40 return;
41 }
42 s++;
43 }
44}
45
46
47static void get_ver(const char *cmd, char *buf, size_t buflen)
48{
49 FILE *f;
50 char *pos;
51
52 buf[0] = '\0';
53 f = popen(cmd, "r");
54 if (f == NULL)
55 return;
56 if (fgets(buf, buflen, f))
57 first_line(buf);
58 pclose(f);
59
60 pos = strstr(buf, " v");
61 if (pos == NULL)
62 buf[0] = '\0';
63 else
64 memmove(buf, pos + 1, strlen(pos));
65}
66
67#endif /* __linux__ */
68
69
70static int cmd_device_get_info(struct sigma_dut *dut, struct sigma_conn *conn,
71 struct sigma_cmd *cmd)
72{
Jouni Malinen5db3b102016-08-04 12:27:18 +030073 const char *vendor = "Qualcomm Atheros";
Jouni Malinencd4e3c32015-10-29 12:39:56 +020074 const char *model = "N/A";
75 const char *version = "N/A";
76#ifdef __linux__
77 char model_buf[128];
78 char ver_buf[128];
79#endif /* __linux__ */
80 char resp[200];
81
82#ifdef __linux__
83 {
84 char path[128];
85 struct stat s;
86 FILE *f;
87 char compat_ver[128];
88 char wpa_supplicant_ver[128];
89 char hostapd_ver[128];
90
91 snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211",
92 get_main_ifname());
93 if (stat(path, &s) == 0) {
94 ssize_t res;
95 char *pos;
96 snprintf(path, sizeof(path),
97 "/sys/class/net/%s/device/driver",
98 get_main_ifname());
99 res = readlink(path, path, sizeof(path));
100 if (res < 0)
101 model = "Linux/";
102 else {
103 if (res >= (int) sizeof(path))
104 res = sizeof(path) - 1;
105 path[res] = '\0';
106 pos = strrchr(path, '/');
107 if (pos == NULL)
108 pos = path;
109 else
110 pos++;
111 snprintf(model_buf, sizeof(model_buf),
112 "Linux/%s", pos);
113 model = model_buf;
114 }
115 } else
116 model = "Linux";
117
118 /* TODO: get version from wpa_supplicant (+ driver via wpa_s)
119 */
120
121 f = fopen("/sys/module/compat/parameters/"
122 "backported_kernel_version", "r");
123 if (f == NULL)
124 f = fopen("/sys/module/compat/parameters/"
125 "compat_version", "r");
126 if (f) {
127 if (fgets(compat_ver, sizeof(compat_ver), f) == NULL)
128 compat_ver[0] = '\0';
129 else
130 first_line(compat_ver);
131 fclose(f);
132 } else
133 compat_ver[0] = '\0';
134
135 get_ver("./hostapd -v 2>&1", hostapd_ver, sizeof(hostapd_ver));
136 if (hostapd_ver[0] == '\0')
137 get_ver("hostapd -v 2>&1", hostapd_ver,
138 sizeof(hostapd_ver));
139 get_ver("./wpa_supplicant -v", wpa_supplicant_ver,
140 sizeof(wpa_supplicant_ver));
141 if (wpa_supplicant_ver[0] == '\0')
142 get_ver("wpa_supplicant -v", wpa_supplicant_ver,
143 sizeof(wpa_supplicant_ver));
144
145 snprintf(ver_buf, sizeof(ver_buf),
146 "drv=%s%s%s%s%s/sigma=" SIGMA_DUT_VER "%s%s",
147 compat_ver,
148 wpa_supplicant_ver[0] ? "/wpas=" : "",
149 wpa_supplicant_ver,
150 hostapd_ver[0] ? "/hapd=" : "",
151 hostapd_ver,
152 dut->version ? "@" : "",
153 dut->version ? dut->version : "");
154 version = ver_buf;
155 }
156#endif /* __linux__ */
157
Jouni Malinen5db3b102016-08-04 12:27:18 +0300158 if (dut->vendor_name)
159 vendor = dut->vendor_name;
160 if (dut->model_name)
161 model = dut->model_name;
162 if (dut->version_name)
163 version = dut->version_name;
164 snprintf(resp, sizeof(resp), "vendor,%s,model,%s,version,%s",
165 vendor, model, version);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200166
167 send_resp(dut, conn, SIGMA_COMPLETE, resp);
168 return 0;
169}
170
171
172static int check_device_list_interfaces(struct sigma_cmd *cmd)
173{
174 if (get_param(cmd, "interfaceType") == NULL)
175 return -1;
176 return 0;
177}
178
179
180static int cmd_device_list_interfaces(struct sigma_dut *dut,
181 struct sigma_conn *conn,
182 struct sigma_cmd *cmd)
183{
184 const char *type;
185 char resp[200];
186
187 type = get_param(cmd, "interfaceType");
188 if (type == NULL)
189 return -1;
190 sigma_dut_print(dut, DUT_MSG_DEBUG, "device_list_interfaces - "
191 "interfaceType=%s", type);
192 if (strcmp(type, "802.11") != 0)
193 return -2;
194
195 snprintf(resp, sizeof(resp), "interfaceType,802.11,"
196 "interfaceID,%s", get_main_ifname());
197 send_resp(dut, conn, SIGMA_COMPLETE, resp);
198
199 return 0;
200}
201
202
203void basic_register_cmds(void)
204{
205 sigma_dut_reg_cmd("ca_get_version", NULL, cmd_ca_get_version);
206 sigma_dut_reg_cmd("device_get_info", NULL, cmd_device_get_info);
207 sigma_dut_reg_cmd("device_list_interfaces",
208 check_device_list_interfaces,
209 cmd_device_list_interfaces);
210}