static void usb_device_removed(struct usb_monitor_ctx *ctx, libusb_device *dev) { uint8_t path[USB_PATH_MAX]; uint8_t path_len; struct usb_port *port = NULL; usb_helpers_fill_port_array(dev, path, &path_len); port = usb_monitor_lists_find_port_path(ctx, path, path_len); if (!port) return; usb_helpers_reset_port(port); usb_monitor_print_ports(ctx); }
/* libusb-callbacks for when devices are added/removed. It is also called * manually when we detect a hub, since we risk devices being added before we * see for example the YKUSH HID device */ static void usb_device_added(struct usb_monitor_ctx *ctx, libusb_device *dev) { //Check if device is connected to a port we control struct usb_port *port; struct libusb_device_descriptor desc; uint8_t path[USB_PATH_MAX]; uint8_t path_len; libusb_get_device_descriptor(dev, &desc); usb_helpers_fill_port_array(dev, path, &path_len); port = usb_monitor_lists_find_port_path(ctx, path, path_len); if (!port) return; //The enabled check is needed here sine we enable/disable async. So we can //process a disabled request before an add event. Of course, device will //most likely be remove in the next iteration of loop, but still ... if (port->msg_mode == RESET || !port->enabled) return; //Need to check port if it already has a device, since we can risk that we //are called two times for one device if (port->dev && port->dev == dev) return; USB_DEBUG_PRINT_SYSLOG(ctx, LOG_INFO, "Device: %.4x:%.4x added\n", desc.idVendor, desc.idProduct); //We need to configure port. So far, this is all generic port->vp.vid = desc.idVendor; port->vp.pid = desc.idProduct; port->status = PORT_DEV_CONNECTED; port->dev = dev; port->msg_mode = PING; libusb_ref_device(dev); usb_monitor_print_ports(ctx); //Whenever we detect a device, we need to add to timeout to send ping. //However, we need to wait longer than the initial five seconds to let //usb_modeswitch potentially works its magic usb_helpers_start_timeout(port, ADDED_TIMEOUT_SEC); }
uint8_t gpio_handler_add_port(struct usb_monitor_ctx *ctx, char *path, uint8_t gpio_num) { struct gpio_port *port; //Bus + port(s) uint8_t dev_path[USB_PATH_MAX]; uint8_t path_len = 0; if (usb_helpers_convert_char_to_path(path, dev_path, &path_len)) { fprintf(stderr, "Path for GPIO device is too long\n"); return 1; } //TODO: Check if port is already in list if (usb_monitor_lists_find_port_path(ctx, dev_path, path_len)) { fprintf(stderr, "GPIO port already found\n"); return 1; } port = malloc(sizeof(struct gpio_port)); if (port == NULL) { fprintf(stderr, "Could not allocate memory for gpio port\n"); return 1; } memset(port, 0, sizeof(struct gpio_port)); port->output = gpio_print_port; port->update = gpio_update_port; port->timeout = gpio_handle_timeout; usb_helpers_configure_port((struct usb_port*) port, ctx, dev_path, path_len, gpio_num, NULL); return 0; }