From fefb33736af4afddf539a1eff6d2d5d257d57f36 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 2 Jan 2016 00:48:14 +0100 Subject: Host: Made rendering pipeline a bit more flexible --- host/usb.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'host/usb.c') diff --git a/host/usb.c b/host/usb.c index 5e696d5..18b38a8 100644 --- a/host/usb.c +++ b/host/usb.c @@ -32,10 +32,12 @@ static int matelight_cmp_str_desc(libusb_device_handle *dev, uint8_t index, char return strcmp(data, value) == 0; } -matelight_handle *matelight_open(){ +matelight_handle *matelight_open(char *match_serial){ matelight_handle *out = NULL; + libusb_device_handle *handle = NULL; libusb_device** device_list; struct libusb_device_descriptor desc; + ssize_t res = libusb_get_device_list(NULL, &device_list); if(res == 0){ fprintf(stderr, "LIBML: Cannot find any connected matelight\n"); @@ -44,56 +46,49 @@ matelight_handle *matelight_open(){ fprintf(stderr, "LIBML: Error enumerating connected USB devices\n"); goto error; }else{ - out = calloc(res+1, sizeof(matelight_handle)); + out = calloc(1, sizeof(matelight_handle)); if(!out){ fprintf(stderr, "LIBML: Cannot allocate memory\n"); goto error; } - memset(out, 0, (res+1)*sizeof(matelight_handle)); - unsigned int found = 0; for(ssize_t i=0; ihandle = handle; if(matelight_cmp_str_desc(handle, desc.iManufacturer, "Gold & Apple")) - if(matelight_cmp_str_desc(handle, desc.iProduct, "Mate Light")){ + if(matelight_cmp_str_desc(handle, desc.iProduct, "Mate Light")) + if(!match_serial || matelight_cmp_str_desc(handle, desc.iSerialNumber, match_serial)){ #define BUF_SIZE 256 - char *serial = malloc(BUF_SIZE); - if(!serial){ - fprintf(stderr, "LIBML: Cannot allocate memory\n"); + out->serial = malloc(BUF_SIZE); + if(!out->serial){ fprintf(stderr, "LIBML: Cannot allocate memory\n"); goto error; } - if(libusb_get_string_descriptor_ascii(out[found].handle, desc.iSerialNumber, (unsigned char*)serial, BUF_SIZE) < 0){ + if(libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char*)&out->serial, BUF_SIZE) < 0){ fprintf(stderr, "LIBML: Cannot read device string descriptor\n"); goto error; } #undef BUF_SIZE - out[found].serial = serial; - found++; + return out; } } } - out[found].handle = NULL; - out[found].serial = NULL; } libusb_free_device_list(device_list, 1); - return out; + return NULL; error: - if(res>0 && out){ - for(ssize_t i=0; iserial) + free(out->serial); + free(out); + } + if(handle) + libusb_close(handle); libusb_free_device_list(device_list, 1); - return 0; + return NULL; } typedef struct{ @@ -140,7 +135,7 @@ int matelight_send_frame(matelight_handle *ml, void *buf, size_t w, size_t h, fl return 1; } if(transferred != sizeof(frame)){ - fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %d bytes.\n", transferred, sizeof(frame)); + fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %zu bytes.\n", transferred, sizeof(frame)); return 1; } } @@ -153,7 +148,7 @@ int matelight_send_frame(matelight_handle *ml, void *buf, size_t w, size_t h, fl return 1; } if(transferred != sizeof(uint8_t)){ - fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %d bytes.\n", transferred, sizeof(uint8_t)); + fprintf(stderr, "LIBML: Could not transfer all frame data. Only transferred %d out of %zu bytes.\n", transferred, sizeof(uint8_t)); return 1; } return 0; -- cgit