2022-01-25 05:56:12 +08:00
|
|
|
#ifndef SC_USB_H
|
|
|
|
#define SC_USB_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <libusb-1.0/libusb.h>
|
|
|
|
|
2022-01-25 03:02:05 +08:00
|
|
|
#include "util/thread.h"
|
|
|
|
|
2022-01-25 05:56:12 +08:00
|
|
|
struct sc_usb {
|
|
|
|
libusb_context *context;
|
|
|
|
libusb_device_handle *handle;
|
2022-01-25 03:02:05 +08:00
|
|
|
|
|
|
|
const struct sc_usb_callbacks *cbs;
|
|
|
|
void *cbs_userdata;
|
|
|
|
|
|
|
|
bool has_callback_handle;
|
|
|
|
libusb_hotplug_callback_handle callback_handle;
|
|
|
|
|
|
|
|
bool has_libusb_event_thread;
|
|
|
|
sc_thread libusb_event_thread;
|
|
|
|
|
|
|
|
atomic_bool stopped; // only used if cbs != NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sc_usb_callbacks {
|
|
|
|
void (*on_disconnected)(struct sc_usb *usb, void *userdata);
|
2022-01-25 05:56:12 +08:00
|
|
|
};
|
|
|
|
|
2022-01-27 05:02:24 +08:00
|
|
|
struct sc_usb_device {
|
|
|
|
libusb_device *device;
|
|
|
|
char *serial;
|
|
|
|
char *manufacturer;
|
|
|
|
char *product;
|
|
|
|
uint16_t vid;
|
|
|
|
uint16_t pid;
|
2022-02-06 02:34:45 +08:00
|
|
|
bool selected;
|
2022-01-27 05:02:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_usb_device_destroy(struct sc_usb_device *usb_device);
|
|
|
|
|
2022-02-06 02:07:53 +08:00
|
|
|
/**
|
|
|
|
* Move src to dst
|
|
|
|
*
|
|
|
|
* After this call, the content of src is undefined, except that
|
|
|
|
* sc_usb_device_destroy() can be called.
|
|
|
|
*
|
|
|
|
* This is useful to take a device from a list that will be destroyed, without
|
|
|
|
* making unnecessary copies.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sc_usb_device_move(struct sc_usb_device *dst, struct sc_usb_device *src);
|
|
|
|
|
2022-01-27 05:08:55 +08:00
|
|
|
void
|
2022-02-06 01:09:02 +08:00
|
|
|
sc_usb_devices_destroy_all(struct sc_usb_device *usb_devices, size_t count);
|
2022-01-27 05:08:55 +08:00
|
|
|
|
2022-01-25 05:56:12 +08:00
|
|
|
bool
|
2022-01-26 02:10:23 +08:00
|
|
|
sc_usb_init(struct sc_usb *usb);
|
2022-01-25 05:56:12 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
sc_usb_destroy(struct sc_usb *usb);
|
|
|
|
|
2022-02-06 02:12:59 +08:00
|
|
|
bool
|
|
|
|
sc_usb_select_device(struct sc_usb *usb, const char *serial,
|
|
|
|
struct sc_usb_device *out_device);
|
2022-01-26 04:11:32 +08:00
|
|
|
|
2022-01-26 02:10:23 +08:00
|
|
|
bool
|
2022-01-25 03:02:05 +08:00
|
|
|
sc_usb_connect(struct sc_usb *usb, libusb_device *device,
|
|
|
|
const struct sc_usb_callbacks *cbs, void *cbs_userdata);
|
2022-01-26 02:10:23 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
sc_usb_disconnect(struct sc_usb *usb);
|
|
|
|
|
2022-01-25 03:02:05 +08:00
|
|
|
void
|
|
|
|
sc_usb_stop(struct sc_usb *usb);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_usb_join(struct sc_usb *usb);
|
|
|
|
|
2022-01-25 05:56:12 +08:00
|
|
|
#endif
|