Minor code and documentation cleanups
[libjaylink.git] / libjaylink / discovery.c
bloba389f26d30f46db1b12192ffab08b7b58f882c17
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <sys/types.h>
26 #include "libjaylink.h"
27 #include "libjaylink-internal.h"
30 * libusb.h includes windows.h and therefore must be included after anything
31 * that includes winsock2.h.
33 #include <libusb.h>
35 /**
36 * @file
38 * Device discovery.
41 /** @cond PRIVATE */
42 /** USB Vendor ID (VID) of SEGGER products. */
43 #define USB_VENDOR_ID 0x1366
45 /* USB Product IDs (PID) and their corresponding USB addresses. */
46 static const uint16_t pids[][2] = {
47 {0x0101, 0},
48 {0x0102, 1},
49 {0x0103, 2},
50 {0x0104, 3},
51 {0x0105, 0},
52 {0x0107, 0},
53 {0x0108, 0},
54 {0x1010, 0},
55 {0x1011, 0},
56 {0x1012, 0},
57 {0x1013, 0},
58 {0x1014, 0},
59 {0x1015, 0},
60 {0x1016, 0},
61 {0x1017, 0},
62 {0x1018, 0}
65 /** Maximum length of the USB string descriptor for the serial number. */
66 #define USB_SERIAL_NUMBER_LENGTH 12
68 /**
69 * Maximum number of digits in a serial number
71 * The serial number of a device consists of at most 9 digits but user defined
72 * serial numbers are allowed with up to 10 digits.
74 #define MAX_SERIAL_NUMBER_DIGITS 10
75 /** @endcond */
77 static bool parse_serial_number(const char *str, uint32_t *serial_number)
79 size_t length;
81 length = strlen(str);
84 * Skip the first digits which are not part of a valid serial number.
85 * This is necessary because some devices erroneously use random digits
86 * instead of zeros for padding.
88 if (length > MAX_SERIAL_NUMBER_DIGITS)
89 str = str + (length - MAX_SERIAL_NUMBER_DIGITS);
91 if (jaylink_parse_serial_number(str, serial_number) != JAYLINK_OK)
92 return false;
94 return true;
97 static bool compare_devices(const void *data, const void *user_data)
99 const struct jaylink_device *dev;
100 const struct libusb_device *usb_dev;
102 dev = data;
103 usb_dev = user_data;
105 if (dev->usb_dev == usb_dev)
106 return true;
108 return false;
111 static struct jaylink_device *find_device(const struct jaylink_context *ctx,
112 const struct libusb_device *usb_dev)
114 struct list *item;
116 item = list_find_custom(ctx->devs, &compare_devices, usb_dev);
118 if (item)
119 return item->data;
121 return NULL;
124 static struct jaylink_device *probe_device(struct jaylink_context *ctx,
125 struct libusb_device *usb_dev)
127 int ret;
128 struct libusb_device_descriptor desc;
129 struct libusb_device_handle *usb_devh;
130 struct jaylink_device *dev;
131 char buf[USB_SERIAL_NUMBER_LENGTH + 1];
132 uint8_t usb_address;
133 uint32_t serial_number;
134 bool valid_serial_number;
135 bool found_device;
136 size_t i;
138 ret = libusb_get_device_descriptor(usb_dev, &desc);
140 if (ret != LIBUSB_SUCCESS) {
141 log_warn(ctx, "Failed to get device descriptor: %s.",
142 libusb_error_name(ret));
143 return NULL;
146 if (desc.idVendor != USB_VENDOR_ID)
147 return NULL;
149 found_device = false;
151 for (i = 0; i < sizeof(pids) / sizeof(pids[0]); i++) {
152 if (pids[i][0] == desc.idProduct) {
153 found_device = true;
154 usb_address = pids[i][1];
155 break;
159 if (!found_device)
160 return NULL;
162 log_dbg(ctx, "Found device (VID:PID = %04x:%04x, bus:address = "
163 "%03u:%03u).", desc.idVendor, desc.idProduct,
164 libusb_get_bus_number(usb_dev),
165 libusb_get_device_address(usb_dev));
168 * Search for an already allocated device instance for this device and
169 * if found return a reference to it.
171 dev = find_device(ctx, usb_dev);
173 if (dev) {
174 log_dbg(ctx, "Device: USB address = %u.", dev->usb_address);
176 if (dev->valid_serial_number)
177 log_dbg(ctx, "Device: Serial number = %u.",
178 dev->serial_number);
179 else
180 log_dbg(ctx, "Device: Serial number = N/A.");
182 log_dbg(ctx, "Using existing device instance.");
183 return jaylink_ref_device(dev);
186 /* Open the device to be able to retrieve its serial number. */
187 ret = libusb_open(usb_dev, &usb_devh);
189 if (ret != LIBUSB_SUCCESS) {
190 log_warn(ctx, "Failed to open device: %s.",
191 libusb_error_name(ret));
192 return NULL;
195 valid_serial_number = true;
197 ret = libusb_get_string_descriptor_ascii(usb_devh, desc.iSerialNumber,
198 (unsigned char *)buf, USB_SERIAL_NUMBER_LENGTH + 1);
200 libusb_close(usb_devh);
202 if (ret < 0) {
203 log_warn(ctx, "Failed to retrieve serial number: %s.",
204 libusb_error_name(ret));
205 valid_serial_number = false;
208 if (valid_serial_number) {
209 if (!parse_serial_number(buf, &serial_number)) {
210 log_warn(ctx, "Failed to parse serial number.");
211 return NULL;
215 log_dbg(ctx, "Device: USB address = %u.", usb_address);
217 if (valid_serial_number)
218 log_dbg(ctx, "Device: Serial number = %u.", serial_number);
219 else
220 log_dbg(ctx, "Device: Serial number = N/A.");
222 log_dbg(ctx, "Allocating new device instance.");
224 dev = device_allocate(ctx);
226 if (!dev) {
227 log_warn(ctx, "Device instance malloc failed.");
228 return NULL;
231 dev->interface = JAYLINK_HIF_USB;
232 dev->usb_dev = libusb_ref_device(usb_dev);
233 dev->usb_address = usb_address;
234 dev->serial_number = serial_number;
235 dev->valid_serial_number = valid_serial_number;
237 return dev;
240 static int discovery_usb_scan(struct jaylink_context *ctx)
242 ssize_t ret;
243 struct libusb_device **devs;
244 struct jaylink_device *dev;
245 size_t num;
246 size_t i;
248 ret = libusb_get_device_list(ctx->usb_ctx, &devs);
250 if (ret == LIBUSB_ERROR_IO) {
251 log_err(ctx, "Failed to retrieve device list: input/output "
252 "error.");
253 return JAYLINK_ERR_IO;
254 } else if (ret < 0) {
255 log_err(ctx, "Failed to retrieve device list: %s.",
256 libusb_error_name(ret));
257 return JAYLINK_ERR;
260 num = 0;
262 for (i = 0; devs[i]; i++) {
263 dev = probe_device(ctx, devs[i]);
265 if (!dev)
266 continue;
268 ctx->discovered_devs = list_prepend(ctx->discovered_devs, dev);
269 num++;
272 libusb_free_device_list(devs, true);
273 log_dbg(ctx, "Found %zu USB device(s).", num);
275 return JAYLINK_OK;
278 static void clear_discovery_list(struct jaylink_context *ctx)
280 struct list *item;
281 struct list *tmp;
282 struct jaylink_device *dev;
284 item = ctx->discovered_devs;
286 while (item) {
287 dev = (struct jaylink_device *)item->data;
288 jaylink_unref_device(dev);
290 tmp = item;
291 item = item->next;
292 free(tmp);
295 ctx->discovered_devs = NULL;
299 * Scan for devices.
301 * @param[in,out] ctx libjaylink context.
302 * @param[in] ifaces Host interfaces to scan for devices. Use bitwise OR to
303 * specify multiple interfaces, or 0 to use all available
304 * interfaces. See #jaylink_host_interface for a description
305 * of the interfaces.
307 * @retval JAYLINK_OK Success.
308 * @retval JAYLINK_ERR_ARG Invalid arguments.
309 * @retval JAYLINK_ERR_IO Input/output error.
310 * @retval JAYLINK_ERR Other error conditions.
312 * @see jaylink_get_devices()
314 * @since 0.1.0
316 JAYLINK_API int jaylink_discovery_scan(struct jaylink_context *ctx,
317 uint32_t ifaces)
319 int ret;
321 if (!ctx)
322 return JAYLINK_ERR_ARG;
324 (void)ifaces;
326 clear_discovery_list(ctx);
328 ret = discovery_usb_scan(ctx);
330 if (ret != JAYLINK_OK) {
331 log_err(ctx, "USB device discovery failed.");
332 return ret;
335 return JAYLINK_OK;