24 #include <sys/types.h> 43 #define USB_VENDOR_ID 0x1366 73 #define USB_SERIAL_NUMBER_LENGTH 12 81 #define MAX_SERIAL_NUMBER_DIGITS 10 95 if (length > MAX_SERIAL_NUMBER_DIGITS)
96 str = str + (length - MAX_SERIAL_NUMBER_DIGITS);
107 const struct libusb_device *usb_dev;
115 if (dev->usb_dev == usb_dev)
122 const struct libusb_device *usb_dev)
135 struct libusb_device *usb_dev)
138 struct libusb_device_descriptor desc;
139 struct libusb_device_handle *usb_devh;
141 char buf[USB_SERIAL_NUMBER_LENGTH + 1];
147 ret = libusb_get_device_descriptor(usb_dev, &desc);
149 if (ret != LIBUSB_SUCCESS) {
150 log_warn(ctx,
"Failed to get device descriptor: %s.",
151 libusb_error_name(ret));
155 if (desc.idVendor != USB_VENDOR_ID)
158 found_device =
false;
160 for (
size_t i = 0; i <
sizeof(pids) /
sizeof(pids[0]); i++) {
161 if (pids[i].pid == desc.idProduct) {
162 usb_address = pids[i].usb_address;
171 log_dbg(ctx,
"Found device (VID:PID = %04x:%04x, bus:address = " 172 "%03u:%03u).", desc.idVendor, desc.idProduct,
173 libusb_get_bus_number(usb_dev),
174 libusb_get_device_address(usb_dev));
183 log_dbg(ctx,
"Device: USB address = %u.", dev->usb_address);
186 log_dbg(ctx,
"Device: Serial number = %u.",
189 log_dbg(ctx,
"Device: Serial number = N/A.");
191 log_dbg(ctx,
"Using existing device instance.");
196 ret = libusb_open(usb_dev, &usb_devh);
198 if (ret != LIBUSB_SUCCESS) {
199 log_warn(ctx,
"Failed to open device: %s.",
200 libusb_error_name(ret));
205 has_serial_number =
true;
207 ret = libusb_get_string_descriptor_ascii(usb_devh, desc.iSerialNumber,
208 (
unsigned char *)buf, USB_SERIAL_NUMBER_LENGTH + 1);
210 libusb_close(usb_devh);
213 log_warn(ctx,
"Failed to retrieve serial number: %s.",
214 libusb_error_name(ret));
215 has_serial_number =
false;
218 if (has_serial_number) {
220 log_warn(ctx,
"Failed to parse serial number.");
225 log_dbg(ctx,
"Device: USB address = %u.", usb_address);
227 if (has_serial_number)
228 log_dbg(ctx,
"Device: Serial number = %u.", serial_number);
230 log_dbg(ctx,
"Device: Serial number = N/A.");
232 log_dbg(ctx,
"Allocating new device instance.");
237 log_warn(ctx,
"Device instance malloc failed.");
242 dev->usb_dev = libusb_ref_device(usb_dev);
253 struct libusb_device **
devs;
257 ret = libusb_get_device_list(ctx->usb_ctx, &devs);
259 if (ret == LIBUSB_ERROR_IO) {
260 log_err(ctx,
"Failed to retrieve device list: input/output " 263 }
else if (ret < 0) {
264 log_err(ctx,
"Failed to retrieve device list: %s.",
265 libusb_error_name(ret));
271 for (
size_t i = 0; devs[i]; i++) {
281 libusb_free_device_list(devs,
true);
282 log_dbg(ctx,
"Found %zu USB device(s).", num);
jaylink_usb_address
USB addresses.
JAYLINK_PRIV int discovery_usb_scan(struct jaylink_context *ctx)
JAYLINK_PRIV struct jaylink_device * device_allocate(struct jaylink_context *ctx)
struct list * discovered_devs
List of recently discovered devices.
JAYLINK_PRIV void log_err(const struct jaylink_context *ctx, const char *format,...)
static libusb_device ** devs
The usb device list.
bool has_serial_number
Indicates whether the serial number is available.
Universal Serial Bus (USB).
USB address 2 (Product ID 0x0103).
Opaque structure representing a device.
struct list * devs
List of allocated device instances.
JAYLINK_API struct jaylink_device * jaylink_ref_device(struct jaylink_device *dev)
Increment the reference count of a device.
JAYLINK_PRIV struct list * list_find_custom(struct list *list, list_compare_callback callback, const void *user_data)
USB address 1 (Product ID 0x0102).
Opaque structure representing a libjaylink context.
#define JAYLINK_PRIV
Macro to mark private libjaylink symbol.
Internal libjaylink header file.
static struct jaylink_device * probe_device(struct jaylink_context *ctx, struct libusb_device *usb_dev)
JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx, const char *format,...)
struct jaylink_context * ctx
libjaylink context.
static bool parse_serial_number(const char *str, uint32_t *serial_number)
static enum jaylink_usb_address usb_address
enum jaylink_host_interface iface
Host interface.
JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx, const char *format,...)
Public libjaylink header file to be used by applications.
JAYLINK_API int jaylink_parse_serial_number(const char *str, uint32_t *serial_number)
Convert a string representation of a serial number to an integer.
USB address 3 (Product ID 0x0104).
static struct jaylink_device * find_device(const struct jaylink_context *ctx, const struct libusb_device *usb_dev)
USB address 0 (Product ID 0x0101).
static uint32_t serial_number
uint32_t serial_number
Serial number of the device.
static bool compare_devices(const void *a, const void *b)
JAYLINK_PRIV struct list * list_prepend(struct list *list, void *data)