Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sigma Control API DUT (station/AP) |
| 3 | * Copyright (c) 2010, Atheros Communications, Inc. |
Jouni Malinen | 9d7e31d | 2017-12-22 18:55:04 +0200 | [diff] [blame] | 4 | * Copyright (c) 2011-2014, 2016-2017, Qualcomm Atheros, Inc. |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 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 | |
| 16 | static 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 | |
| 35 | static 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 | |
| 47 | static 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 | |
| 70 | static int cmd_device_get_info(struct sigma_dut *dut, struct sigma_conn *conn, |
| 71 | struct sigma_cmd *cmd) |
| 72 | { |
Jouni Malinen | 5db3b10 | 2016-08-04 12:27:18 +0300 | [diff] [blame] | 73 | const char *vendor = "Qualcomm Atheros"; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 74 | const char *model = "N/A"; |
| 75 | const char *version = "N/A"; |
| 76 | #ifdef __linux__ |
| 77 | char model_buf[128]; |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 78 | char ver_buf[256]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 79 | #endif /* __linux__ */ |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 80 | char resp[512]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 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]; |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 90 | char host_fw_ver[128]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 91 | |
| 92 | snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", |
| 93 | get_main_ifname()); |
| 94 | if (stat(path, &s) == 0) { |
| 95 | ssize_t res; |
| 96 | char *pos; |
| 97 | snprintf(path, sizeof(path), |
| 98 | "/sys/class/net/%s/device/driver", |
| 99 | get_main_ifname()); |
| 100 | res = readlink(path, path, sizeof(path)); |
| 101 | if (res < 0) |
| 102 | model = "Linux/"; |
| 103 | else { |
| 104 | if (res >= (int) sizeof(path)) |
| 105 | res = sizeof(path) - 1; |
| 106 | path[res] = '\0'; |
| 107 | pos = strrchr(path, '/'); |
| 108 | if (pos == NULL) |
| 109 | pos = path; |
| 110 | else |
| 111 | pos++; |
| 112 | snprintf(model_buf, sizeof(model_buf), |
| 113 | "Linux/%s", pos); |
| 114 | model = model_buf; |
| 115 | } |
| 116 | } else |
| 117 | model = "Linux"; |
| 118 | |
| 119 | /* TODO: get version from wpa_supplicant (+ driver via wpa_s) |
| 120 | */ |
| 121 | |
| 122 | f = fopen("/sys/module/compat/parameters/" |
| 123 | "backported_kernel_version", "r"); |
| 124 | if (f == NULL) |
| 125 | f = fopen("/sys/module/compat/parameters/" |
| 126 | "compat_version", "r"); |
| 127 | if (f) { |
| 128 | if (fgets(compat_ver, sizeof(compat_ver), f) == NULL) |
| 129 | compat_ver[0] = '\0'; |
| 130 | else |
| 131 | first_line(compat_ver); |
| 132 | fclose(f); |
| 133 | } else |
| 134 | compat_ver[0] = '\0'; |
| 135 | |
| 136 | get_ver("./hostapd -v 2>&1", hostapd_ver, sizeof(hostapd_ver)); |
| 137 | if (hostapd_ver[0] == '\0') |
| 138 | get_ver("hostapd -v 2>&1", hostapd_ver, |
| 139 | sizeof(hostapd_ver)); |
| 140 | get_ver("./wpa_supplicant -v", wpa_supplicant_ver, |
| 141 | sizeof(wpa_supplicant_ver)); |
| 142 | if (wpa_supplicant_ver[0] == '\0') |
| 143 | get_ver("wpa_supplicant -v", wpa_supplicant_ver, |
| 144 | sizeof(wpa_supplicant_ver)); |
| 145 | |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 146 | if (get_driver_type() == DRIVER_WCN || |
| 147 | get_driver_type() == DRIVER_LINUX_WCN) |
| 148 | get_ver("iwpriv wlan0 version", host_fw_ver, |
| 149 | sizeof(host_fw_ver)); |
| 150 | else |
| 151 | host_fw_ver[0] = '\0'; |
| 152 | |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 153 | snprintf(ver_buf, sizeof(ver_buf), |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 154 | "drv=%s%s%s%s%s%s%s/sigma=" SIGMA_DUT_VER "%s%s", |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 155 | compat_ver, |
| 156 | wpa_supplicant_ver[0] ? "/wpas=" : "", |
| 157 | wpa_supplicant_ver, |
| 158 | hostapd_ver[0] ? "/hapd=" : "", |
| 159 | hostapd_ver, |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 160 | host_fw_ver[0] ? "/wlan=" : "", |
| 161 | host_fw_ver, |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 162 | dut->version ? "@" : "", |
| 163 | dut->version ? dut->version : ""); |
| 164 | version = ver_buf; |
| 165 | } |
| 166 | #endif /* __linux__ */ |
| 167 | |
Jouni Malinen | 5db3b10 | 2016-08-04 12:27:18 +0300 | [diff] [blame] | 168 | if (dut->vendor_name) |
| 169 | vendor = dut->vendor_name; |
| 170 | if (dut->model_name) |
| 171 | model = dut->model_name; |
| 172 | if (dut->version_name) |
| 173 | version = dut->version_name; |
| 174 | snprintf(resp, sizeof(resp), "vendor,%s,model,%s,version,%s", |
| 175 | vendor, model, version); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 176 | |
| 177 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static int check_device_list_interfaces(struct sigma_cmd *cmd) |
| 183 | { |
| 184 | if (get_param(cmd, "interfaceType") == NULL) |
| 185 | return -1; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | static int cmd_device_list_interfaces(struct sigma_dut *dut, |
| 191 | struct sigma_conn *conn, |
| 192 | struct sigma_cmd *cmd) |
| 193 | { |
| 194 | const char *type; |
| 195 | char resp[200]; |
| 196 | |
| 197 | type = get_param(cmd, "interfaceType"); |
| 198 | if (type == NULL) |
| 199 | return -1; |
| 200 | sigma_dut_print(dut, DUT_MSG_DEBUG, "device_list_interfaces - " |
| 201 | "interfaceType=%s", type); |
| 202 | if (strcmp(type, "802.11") != 0) |
| 203 | return -2; |
| 204 | |
| 205 | snprintf(resp, sizeof(resp), "interfaceType,802.11," |
| 206 | "interfaceID,%s", get_main_ifname()); |
| 207 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void basic_register_cmds(void) |
| 214 | { |
| 215 | sigma_dut_reg_cmd("ca_get_version", NULL, cmd_ca_get_version); |
| 216 | sigma_dut_reg_cmd("device_get_info", NULL, cmd_device_get_info); |
| 217 | sigma_dut_reg_cmd("device_list_interfaces", |
| 218 | check_device_list_interfaces, |
| 219 | cmd_device_list_interfaces); |
| 220 | } |