Skip to content

Commit

Permalink
Merge pull request #141 from v3rm0n/libusb-preferred-device
Browse files Browse the repository at this point in the history
Libusb preferred device
  • Loading branch information
laamaa authored Apr 18, 2024
2 parents 393a3e1 + 35f6ae7 commit c4925e5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ void close_serial_port() { disconnect(); }

int main(int argc, char *argv[]) {

if(argc == 2 && strcmp(argv[1], "--list") == 0) {
if(argc == 2 && SDL_strcmp(argv[1], "--list") == 0) {
return list_devices();
}

char *preferred_device = NULL;
if (argc == 3 && strcmp(argv[1], "--dev") == 0) {
if (argc == 3 && SDL_strcmp(argv[1], "--dev") == 0) {
preferred_device = argv[2];
SDL_Log("Using preferred device %s.\n", preferred_device);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ int main(int argc, char *argv[]) {
// try to init serial port
int port_inited = init_serial(1, preferred_device);
// if port init was successful, try to enable and reset display
if (port_inited == 1 && enable_and_reset_display(0) == 1) {
if (port_inited == 1 && enable_and_reset_display() == 1) {
// if audio routing is enabled, try to initialize audio devices
if (conf.audio_enabled == 1) {
audio_init(conf.audio_buffer_size, conf.audio_device_name);
Expand Down
2 changes: 1 addition & 1 deletion src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int list_devices() {
struct sp_port *port = port_list[i];

if (detect_m8_serial_device(port)) {
SDL_Log("Found M8 device: %s", sp_get_port_name(port));
printf("Found M8 device: %s", sp_get_port_name(port));
}
}

Expand Down
72 changes: 70 additions & 2 deletions src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,42 @@ static int ep_in_addr = 0x83;
#define ACM_CTRL_DTR 0x01
#define ACM_CTRL_RTS 0x02

#define M8_VID 0x16c0
#define M8_PID 0x048a

libusb_context *ctx = NULL;
libusb_device_handle *devh = NULL;

static int do_exit = 0;

int list_devices() {
int r;
r = libusb_init(&ctx);
if (r < 0) {
SDL_Log("libusb_init failed: %s", libusb_error_name(r));
return 0;
}

libusb_device **device_list = NULL;
int count = libusb_get_device_list(ctx, &device_list);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = device_list[idx];
struct libusb_device_descriptor desc;
int rc = libusb_get_device_descriptor(device, &desc);
if (rc < 0) {
SDL_Log("Error");
libusb_free_device_list(device_list, 1);
return rc;
}

if (desc.idVendor == M8_VID && desc.idProduct == M8_PID) {
printf("Found M8 device: %d:%d\n", libusb_get_port_number(device), libusb_get_bus_number(device));
}
}
libusb_free_device_list(device_list, 1);
return 0;
}

int usb_loop(void *data) {
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
while (!do_exit) {
Expand Down Expand Up @@ -170,7 +201,7 @@ int init_serial_with_file_descriptor(int file_descriptor) {
return init_interface();
}

int init_serial(int verbose) {
int init_serial(int verbose, char *preferred_device) {

if (devh != NULL) {
return 1;
Expand All @@ -182,7 +213,44 @@ int init_serial(int verbose) {
SDL_Log("libusb_init failed: %s", libusb_error_name(r));
return 0;
}
devh = libusb_open_device_with_vid_pid(ctx, 0x16c0, 0x048a);
if(preferred_device == NULL) {
devh = libusb_open_device_with_vid_pid(ctx, M8_VID, M8_PID);
} else {
char *port;
char *saveptr = NULL;
char *bus;
port = SDL_strtokr(preferred_device, ":", &saveptr);
bus = SDL_strtokr(NULL, ":", &saveptr);
libusb_device **device_list = NULL;
int count = libusb_get_device_list(ctx, &device_list);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = device_list[idx];
struct libusb_device_descriptor desc;
r = libusb_get_device_descriptor(device, &desc);
if (r < 0) {
SDL_Log("libusb_get_device_descriptor failed: %s", libusb_error_name(r));
libusb_free_device_list(device_list, 1);
return 0;
}

if (desc.idVendor == M8_VID && desc.idProduct == M8_PID) {
SDL_Log("Searching for port %s and bus %s", port, bus);
if (libusb_get_port_number(device) == SDL_atoi(port) && libusb_get_bus_number(device) == SDL_atoi(bus)) {
SDL_Log("Preferred device found, connecting");
r = libusb_open(device, &devh);
if (r < 0) {
SDL_Log("libusb_open failed: %s", libusb_error_name(r));
return 0;
}
}
}
}
libusb_free_device_list(device_list, 1);
if(devh == NULL) {
SDL_Log("Preferred device %s not found, using first available", preferred_device);
devh = libusb_open_device_with_vid_pid(ctx, M8_VID, M8_PID);
}
}
if (devh == NULL) {
SDL_Log("libusb_open_device_with_vid_pid returned invalid handle");
return 0;
Expand Down
1 change: 0 additions & 1 deletion src/usb_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ int audio_destroy() {
if (rc < 0) {
SDL_Log("Error cancelling transfer: %s\n", libusb_error_name(rc));
}
SDL_free(xfr[i]->buffer);
}

SDL_Log("Freeing interface %d", IFACE_NUM);
Expand Down

0 comments on commit c4925e5

Please sign in to comment.