slim down usb_shm to only the things used in libeismulitplexer
This commit is contained in:
parent
ef73c41207
commit
c55b600d9e
|
@ -6,6 +6,9 @@ find_package(PkgConfig REQUIRED)
|
||||||
find_package(Doxygen)
|
find_package(Doxygen)
|
||||||
pkg_check_modules(LIBUSB REQUIRED libusb-1.0)
|
pkg_check_modules(LIBUSB REQUIRED libusb-1.0)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
|
||||||
set(SRC_FILES eismultiplexer.c usbshm.c)
|
set(SRC_FILES eismultiplexer.c usbshm.c)
|
||||||
|
|
||||||
message("Platform " ${CMAKE_SYSTEM_NAME})
|
message("Platform " ${CMAKE_SYSTEM_NAME})
|
||||||
|
|
|
@ -34,14 +34,6 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
static void usleep(uint64_t microseconds)
|
|
||||||
{
|
|
||||||
struct timespec ts;
|
|
||||||
ts.tv_sec = microseconds / 1000000;
|
|
||||||
ts.tv_nsec = (microseconds % 1000000) * 1000;
|
|
||||||
nanosleep(&ts, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int eismultiplexer_connect(struct eismultiplexer* muliplexer, uint16_t serial)
|
int eismultiplexer_connect(struct eismultiplexer* muliplexer, uint16_t serial)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -49,7 +41,7 @@ int eismultiplexer_connect(struct eismultiplexer* muliplexer, uint16_t serial)
|
||||||
if(!muliplexer->priv)
|
if(!muliplexer->priv)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ret = usbshm_init(muliplexer->priv, NULL, muliplexer);
|
ret = usbshm_init(muliplexer->priv);
|
||||||
if(ret)
|
if(ret)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
|
@ -69,10 +61,7 @@ int eismultiplexer_connect_channel_exclusive(struct eismultiplexer* muliplexer,
|
||||||
uint8_t* wValChar = (uint8_t*)&wValue;
|
uint8_t* wValChar = (uint8_t*)&wValue;
|
||||||
wValChar[0] = channel;
|
wValChar[0] = channel;
|
||||||
wValChar[1] = 0;
|
wValChar[1] = 0;
|
||||||
int ret;
|
return usbshm_write_control_transfer(muliplexer->priv, 2, NULL, 0, wValue, 0);
|
||||||
while((ret = usbshm_writeControlTransfer(muliplexer->priv, 2, NULL, 0, wValue, 0)) == USBSHM_ERROR_AGAIN)
|
|
||||||
usleep(1000000);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int eismultiplexer_connect_channel(struct eismultiplexer* muliplexer, channel_t channel)
|
int eismultiplexer_connect_channel(struct eismultiplexer* muliplexer, channel_t channel)
|
||||||
|
@ -96,30 +85,28 @@ int eismultiplexer_disconnect_channel(struct eismultiplexer* muliplexer, channel
|
||||||
channel_t eismultiplexer_get_connected(struct eismultiplexer* muliplexer)
|
channel_t eismultiplexer_get_connected(struct eismultiplexer* muliplexer)
|
||||||
{
|
{
|
||||||
uint8_t buffer[2] = {};
|
uint8_t buffer[2] = {};
|
||||||
usbshm_readControlTransferSync(muliplexer->priv, 3, 0, 0, buffer, 1);
|
usbshm_read_control_transfer(muliplexer->priv, 3, 0, 0, buffer, 1);
|
||||||
return buffer[0];
|
return buffer[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
int eismultiplexer_set_led(struct eismultiplexer* muliplexer, bool on)
|
int eismultiplexer_set_led(struct eismultiplexer* muliplexer, bool on)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
while((ret = usbshm_writeControlTransfer(muliplexer->priv, on, NULL, 0, 0, 0)) == USBSHM_ERROR_AGAIN)
|
ret = usbshm_write_control_transfer(muliplexer->priv, on, NULL, 0, 0, 0);
|
||||||
usleep(1000000);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int eismultiplexer_write_eeprom(struct eismultiplexer* muliplexer, uint16_t addr, uint16_t value)
|
int eismultiplexer_write_eeprom(struct eismultiplexer* muliplexer, uint16_t addr, uint16_t value)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
while((ret = usbshm_writeControlTransfer(muliplexer->priv, 5, NULL, 0, value, addr)) == USBSHM_ERROR_AGAIN)
|
ret = usbshm_write_control_transfer(muliplexer->priv, 5, NULL, 0, value, addr);
|
||||||
usleep(1000000);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t eismultiplexer_read_eeprom(struct eismultiplexer* muliplexer, uint16_t addr)
|
uint16_t eismultiplexer_read_eeprom(struct eismultiplexer* muliplexer, uint16_t addr)
|
||||||
{
|
{
|
||||||
uint8_t buffer[2] = {};
|
uint8_t buffer[2] = {};
|
||||||
usbshm_readControlTransferSync(muliplexer->priv, 4, 0, addr, buffer, 2);
|
usbshm_read_control_transfer(muliplexer->priv, 4, 0, addr, buffer, 2);
|
||||||
return *((uint16_t*)buffer);
|
return *((uint16_t*)buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
244
usbshm.c
244
usbshm.c
|
@ -25,101 +25,66 @@
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef __STDC_ALLOC_LIB__
|
||||||
|
#define __STDC_WANT_LIB_EXT2__ 1
|
||||||
|
#else
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "usbshm.h"
|
#include "usbshm.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdatomic.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
struct usbshm_priv
|
static int object_counter = 0;
|
||||||
{
|
static struct libusb_context* context = NULL;
|
||||||
libusb_device_handle* handle;
|
|
||||||
struct libusb_transfer* transfer;
|
|
||||||
unsigned char* buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
static unsigned objectCounter = 0;
|
|
||||||
static struct timeval timeout = {1, 0};
|
|
||||||
static pthread_mutex_t *libusbDataMutex;
|
|
||||||
static pthread_t libusbThread;
|
|
||||||
static atomic_bool threadStop;
|
|
||||||
|
|
||||||
static void usbshm_transferCallBack(struct libusb_transfer *transfer);
|
|
||||||
|
|
||||||
void usbshm_distroy(struct usbshm* instance)
|
void usbshm_distroy(struct usbshm* instance)
|
||||||
{
|
{
|
||||||
while(!usbshm_ready(instance))
|
pthread_mutex_lock(instance->mutex);
|
||||||
sleep(1);
|
libusb_close(instance->handle);
|
||||||
free(instance->priv->transfer);
|
|
||||||
libusb_close(instance->priv->handle);
|
|
||||||
free(instance->priv->buffer);
|
|
||||||
free(instance->priv);
|
|
||||||
if(instance->serial)
|
if(instance->serial)
|
||||||
free(instance->serial);
|
free(instance->serial);
|
||||||
if(--objectCounter == 0)
|
|
||||||
{
|
|
||||||
threadStop = true;
|
|
||||||
pthread_join(libusbThread, NULL);
|
|
||||||
libusb_exit(NULL);
|
|
||||||
pthread_mutex_destroy(libusbDataMutex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* usbshm_libusbPoll(void* arg)
|
bool usbshm_is_open(struct usbshm* instance)
|
||||||
{
|
{
|
||||||
while(!threadStop)
|
return instance->handle != NULL;
|
||||||
{
|
|
||||||
libusb_handle_events_timeout_completed(NULL, &timeout, NULL);
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool usbshm_isOpen(struct usbshm* instance)
|
int usbshm_init(struct usbshm* instance)
|
||||||
{
|
|
||||||
return instance->priv->handle != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data), void* user_data)
|
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
instance->priv = malloc(sizeof(*instance->priv));
|
if(object_counter == 0)
|
||||||
instance->priv->handle = NULL;
|
ret = libusb_init(&context) < 0 ? USBSHM_ERROR_ERR : 0;
|
||||||
instance->priv->transfer = NULL;
|
if(ret == 0)
|
||||||
instance->priv->buffer = NULL;
|
{
|
||||||
|
++object_counter;
|
||||||
|
instance->handle = NULL;
|
||||||
instance->vendorID = 0;
|
instance->vendorID = 0;
|
||||||
instance->productID = 0;
|
instance->productID = 0;
|
||||||
instance->serial = NULL;
|
instance->serial = NULL;
|
||||||
instance->dataCallback = dataCallback;
|
instance->mutex = malloc(sizeof(*(instance->mutex)));
|
||||||
instance->user_data = user_data;
|
pthread_mutex_init(instance->mutex, NULL);
|
||||||
if(objectCounter == 0)
|
|
||||||
{
|
|
||||||
ret = libusb_init(NULL) < 0 ? USBSHM_ERROR_ERR : 0;
|
|
||||||
libusbDataMutex = malloc(sizeof(*libusbDataMutex));
|
|
||||||
pthread_mutex_init(libusbDataMutex, NULL);
|
|
||||||
pthread_create(&libusbThread, NULL, &usbshm_libusbPoll, NULL);
|
|
||||||
}
|
|
||||||
if(ret == 0)
|
|
||||||
objectCounter++;
|
|
||||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool usbshm_ready(struct usbshm* instance)
|
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
||||||
{
|
|
||||||
return instance->priv->transfer == NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial)
|
int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial)
|
||||||
{
|
{
|
||||||
instance->priv->handle = NULL;
|
if(instance->handle)
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
return USBSHM_ERROR_ALLREADY_CONNECTED;
|
||||||
|
if(!context)
|
||||||
|
return USBSHM_ERROR_NOT_CONNECTED;
|
||||||
|
pthread_mutex_lock(instance->mutex);
|
||||||
libusb_device** list;
|
libusb_device** list;
|
||||||
int count = libusb_get_device_list(NULL, &list);
|
int count = libusb_get_device_list(context, &list);
|
||||||
int errorCode = 0;
|
int errorCode = 0;
|
||||||
if( count > 0)
|
if( count > 0)
|
||||||
{
|
{
|
||||||
|
@ -129,21 +94,21 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi
|
||||||
libusb_get_device_descriptor(list[i], &desc);
|
libusb_get_device_descriptor(list[i], &desc);
|
||||||
if(desc.idVendor == vendorID && desc.idProduct == productID)
|
if(desc.idVendor == vendorID && desc.idProduct == productID)
|
||||||
{
|
{
|
||||||
errorCode = libusb_open(list[i], &instance->priv->handle) < 0 ? USBSHM_ERROR_ERR : 0;
|
errorCode = libusb_open(list[i], &instance->handle) < 0 ? USBSHM_ERROR_ERR : 0;
|
||||||
if(errorCode != USBSHM_ERROR_ERR && instance->priv->handle)
|
if(errorCode != USBSHM_ERROR_ERR && instance->handle)
|
||||||
{
|
{
|
||||||
if(serial)
|
if(serial)
|
||||||
{
|
{
|
||||||
size_t len = strlen((const char*)serial)+1;
|
size_t len = strlen((const char*)serial)+1;
|
||||||
unsigned char* buffer = malloc(len);
|
unsigned char* buffer = malloc(len);
|
||||||
buffer[0] = '\0';
|
buffer[0] = '\0';
|
||||||
libusb_get_string_descriptor_ascii(instance->priv->handle, desc.iSerialNumber, buffer, len);
|
libusb_get_string_descriptor_ascii(instance->handle, desc.iSerialNumber, buffer, len);
|
||||||
int cmp = strcmp((const char*)serial, (const char*)buffer);
|
int cmp = strcmp((const char*)serial, (const char*)buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
if(cmp != 0)
|
if(cmp != 0)
|
||||||
{
|
{
|
||||||
libusb_close(instance->priv->handle);
|
libusb_close(instance->handle);
|
||||||
instance->priv->handle = NULL;
|
instance->handle = NULL;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,7 +116,7 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
instance->priv->handle = NULL;
|
instance->handle = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,11 +124,13 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Can not list devices\n");
|
printf("Can not list devices\n");
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
pthread_mutex_unlock(instance->mutex);
|
||||||
return USBSHM_ERROR_ERR;
|
return USBSHM_ERROR_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(usbshm_isOpen(instance))
|
pthread_mutex_unlock(instance->mutex);
|
||||||
|
|
||||||
|
if(usbshm_is_open(instance))
|
||||||
{
|
{
|
||||||
instance->vendorID = vendorID;
|
instance->vendorID = vendorID;
|
||||||
instance->productID = productID;
|
instance->productID = productID;
|
||||||
|
@ -172,7 +139,7 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi
|
||||||
instance->serial = calloc(strlen((char*)serial), 1);
|
instance->serial = calloc(strlen((char*)serial), 1);
|
||||||
memcpy(instance->serial, serial, strlen((char*)serial));
|
memcpy(instance->serial, serial, strlen((char*)serial));
|
||||||
}
|
}
|
||||||
libusb_set_auto_detach_kernel_driver(instance->priv->handle, true);
|
libusb_set_auto_detach_kernel_driver(instance->handle, true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -181,120 +148,65 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_free_device_list(list, count);
|
libusb_free_device_list(list, count);
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
|
||||||
return errorCode;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool usbshm_usbshm_isOpen(struct usbshm* instance)
|
bool usbshm_usbshm_is_open(struct usbshm* instance)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
pthread_mutex_lock(instance->mutex);
|
||||||
bool ret = instance->priv->handle != NULL;
|
bool ret = instance->handle != NULL;
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
pthread_mutex_unlock(instance->mutex);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usbshm_reset(struct usbshm* instance)
|
void usbshm_reset(struct usbshm* instance)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
pthread_mutex_lock(instance->mutex);
|
||||||
libusb_reset_device(instance->priv->handle);
|
libusb_reset_device(instance->handle);
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
pthread_mutex_unlock(instance->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void usbshm_reopen(struct usbshm* instance)
|
void usbshm_reopen(struct usbshm* instance)
|
||||||
{
|
{
|
||||||
usbshm_reset(instance);
|
usbshm_reset(instance);
|
||||||
libusb_close(instance->priv->handle);
|
libusb_close(instance->handle);
|
||||||
usbshm_open(instance, instance->vendorID, instance->productID, instance->serial);
|
char* serial = strdup((char*)instance->serial);
|
||||||
|
free(instance->serial);
|
||||||
|
usbshm_open(instance, instance->vendorID, instance->productID, (unsigned char*)serial);
|
||||||
}
|
}
|
||||||
|
|
||||||
int usbshm_writeControlTransfer(struct usbshm* instance, const uint8_t request,
|
int usbshm_write_control_transfer(struct usbshm* instance, const uint8_t request,
|
||||||
char* buffer, const uint8_t length,
|
uint8_t* buffer, const uint8_t length,
|
||||||
const uint16_t wValue, const uint16_t wIndex)
|
const uint16_t wValue, const uint16_t wIndex)
|
||||||
{
|
{
|
||||||
if(!usbshm_isOpen(instance))
|
if(!usbshm_is_open(instance))
|
||||||
return USBSHM_ERROR_NOT_CONNECTED;
|
return USBSHM_ERROR_NOT_CONNECTED;
|
||||||
if(length > 8)
|
if(length > 8)
|
||||||
return USBSHM_ERROR_PARAM;
|
return USBSHM_ERROR_PARAM;
|
||||||
if(instance->priv->transfer == NULL)
|
pthread_mutex_lock(instance->mutex);
|
||||||
{
|
int ret = libusb_control_transfer(instance->handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
|
||||||
instance->priv->buffer = malloc(length+8);
|
|
||||||
libusb_fill_control_setup(instance->priv->buffer,
|
|
||||||
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
|
|
||||||
request, wValue, wIndex, length );
|
|
||||||
for(uint8_t i = 0; i < length; ++i)
|
|
||||||
instance->priv->buffer[i+8] = buffer[i];
|
|
||||||
instance->priv->transfer = libusb_alloc_transfer(0);
|
|
||||||
libusb_fill_control_transfer(instance->priv->transfer, instance->priv->handle, instance->priv->buffer,
|
|
||||||
&usbshm_transferCallBack, instance, 100);
|
|
||||||
int ret = libusb_submit_transfer(instance->priv->transfer);
|
|
||||||
if(ret < 0)
|
|
||||||
{
|
|
||||||
free(buffer);
|
|
||||||
libusb_free_transfer(instance->priv->transfer);
|
|
||||||
instance->priv->transfer = NULL;
|
|
||||||
if(ret == LIBUSB_ERROR_NO_DEVICE)
|
|
||||||
usbshm_reopen(instance);
|
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
|
||||||
}
|
|
||||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
|
||||||
}
|
|
||||||
else return USBSHM_ERROR_AGAIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
int usbshm_readControlTransfer(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, const uint16_t length)
|
|
||||||
{
|
|
||||||
if(!usbshm_isOpen(instance))
|
|
||||||
return USBSHM_ERROR_NOT_CONNECTED;
|
|
||||||
if(length > 8)
|
|
||||||
return USBSHM_ERROR_PARAM;
|
|
||||||
if(instance->priv->transfer == NULL)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
|
||||||
instance->priv->buffer = malloc(length+8);
|
|
||||||
libusb_fill_control_setup(instance->priv->buffer, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
|
|
||||||
request, wValue, wIndex, length);
|
|
||||||
instance->priv->transfer = libusb_alloc_transfer(0);
|
|
||||||
libusb_fill_control_transfer(instance->priv->transfer, instance->priv->handle, instance->priv->buffer,
|
|
||||||
&usbshm_transferCallBack, instance, 100);
|
|
||||||
int ret = libusb_submit_transfer(instance->priv->transfer);
|
|
||||||
if(ret < 0)
|
|
||||||
{
|
|
||||||
free(instance->priv->buffer);
|
|
||||||
libusb_free_transfer(instance->priv->transfer);
|
|
||||||
instance->priv->transfer = NULL;
|
|
||||||
if(ret == LIBUSB_ERROR_NO_DEVICE)
|
|
||||||
usbshm_reopen(instance);
|
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
|
||||||
}
|
|
||||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
|
||||||
}
|
|
||||||
else return USBSHM_ERROR_AGAIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void usbshm_transferCallBack(struct libusb_transfer *transfer)
|
|
||||||
{
|
|
||||||
struct usbshm* context = (struct usbshm*)transfer->user_data;
|
|
||||||
if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
|
|
||||||
{
|
|
||||||
printf("Usb transfer failed with %d\n", transfer->status);
|
|
||||||
}
|
|
||||||
else if(transfer->type == LIBUSB_TRANSFER_TYPE_CONTROL)
|
|
||||||
{
|
|
||||||
if(context->dataCallback && transfer->length-8 >= transfer->actual_length)
|
|
||||||
context->dataCallback(transfer->buffer[1], transfer->buffer+8, transfer->actual_length, context->user_data);
|
|
||||||
}
|
|
||||||
free(context->priv->buffer);
|
|
||||||
libusb_free_transfer(context->priv->transfer);
|
|
||||||
context->priv->transfer = NULL;
|
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
int usbshm_readControlTransferSync(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, uint8_t* buffer, const uint16_t length)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
|
||||||
int ret = libusb_control_transfer(instance->priv->handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
|
|
||||||
request, wValue, wIndex, buffer, length, 2000);
|
request, wValue, wIndex, buffer, length, 2000);
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
pthread_mutex_unlock(instance->mutex);
|
||||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
|
||||||
|
if(ret == LIBUSB_ERROR_TIMEOUT)
|
||||||
|
ret = USBSHM_ERROR_TIMEOUT;
|
||||||
|
else if(ret < 0 )
|
||||||
|
ret = USBSHM_ERROR_ERR;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int usbshm_read_control_transfer(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, uint8_t* buffer, const uint16_t length)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(instance->mutex);
|
||||||
|
int ret = libusb_control_transfer(instance->handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
|
||||||
|
request, wValue, wIndex, buffer, length, 2000);
|
||||||
|
pthread_mutex_unlock(instance->mutex);
|
||||||
|
|
||||||
|
if(ret == LIBUSB_ERROR_TIMEOUT)
|
||||||
|
ret = USBSHM_ERROR_TIMEOUT;
|
||||||
|
else if(ret < 0 )
|
||||||
|
ret = USBSHM_ERROR_ERR;
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
24
usbshm.h
24
usbshm.h
|
@ -27,48 +27,42 @@
|
||||||
|
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdatomic.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <time.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct usbshm_priv;
|
struct usbshm_priv;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
USBSHM_ERROR_AGAIN = -1,
|
|
||||||
USBSHM_ERROR_PARAM = -2,
|
USBSHM_ERROR_PARAM = -2,
|
||||||
USBSHM_ERROR_ERR = -3,
|
USBSHM_ERROR_ERR = -3,
|
||||||
USBSHM_ERROR_NOT_CONNECTED = -4,
|
USBSHM_ERROR_NOT_CONNECTED = -4,
|
||||||
|
USBSHM_ERROR_ALLREADY_CONNECTED = -5,
|
||||||
|
USBSHM_ERROR_TIMEOUT = -6
|
||||||
};
|
};
|
||||||
|
|
||||||
struct usbshm {
|
struct usbshm {
|
||||||
struct usbshm_priv* priv;
|
|
||||||
int vendorID;
|
int vendorID;
|
||||||
int productID;
|
int productID;
|
||||||
unsigned char* serial;
|
unsigned char* serial;
|
||||||
void* user_data;
|
libusb_device_handle* handle;
|
||||||
void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data);
|
pthread_mutex_t* mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
void usbshm_distroy(struct usbshm* instance);
|
void usbshm_distroy(struct usbshm* instance);
|
||||||
|
|
||||||
int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data), void* user_data);
|
int usbshm_init(struct usbshm* instance);
|
||||||
|
|
||||||
bool usbshm_ready(struct usbshm* instance);
|
|
||||||
|
|
||||||
int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial);
|
int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial);
|
||||||
|
|
||||||
bool usbshm_isOpen(struct usbshm* instance);
|
bool usbshm_is_open(struct usbshm* instance);
|
||||||
|
|
||||||
void usbshm_reset(struct usbshm* instance);
|
void usbshm_reset(struct usbshm* instance);
|
||||||
|
|
||||||
void usbshm_reopen(struct usbshm* instance);
|
void usbshm_reopen(struct usbshm* instance);
|
||||||
|
|
||||||
int usbshm_writeControlTransfer(struct usbshm* instance, const uint8_t request,
|
int usbshm_write_control_transfer(struct usbshm* instance, const uint8_t request,
|
||||||
char* buffer, const uint8_t length,
|
uint8_t* buffer, const uint8_t length,
|
||||||
const uint16_t wValue, const uint16_t wIndex);
|
const uint16_t wValue, const uint16_t wIndex);
|
||||||
|
|
||||||
int usbshm_readControlTransfer(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, const uint16_t length);
|
int usbshm_read_control_transfer(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, uint8_t* buffer, const uint16_t length);
|
||||||
|
|
||||||
int usbshm_readControlTransferSync(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, uint8_t* buffer, const uint16_t length);
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue