Compare commits
10 commits
fbf59878d6
...
f69011d589
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f69011d589 | ||
![]() |
db4981628f | ||
![]() |
7169533d05 | ||
![]() |
6a6471125b | ||
![]() |
a7cce81e76 | ||
![]() |
ab7d922a7e | ||
![]() |
a4b6335e57 | ||
![]() |
c55b600d9e | ||
![]() |
ef73c41207 | ||
![]() |
3c6da8d2b0 |
|
@ -1,11 +1,14 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
project(eismultiplexer)
|
project(eismultiplexer C)
|
||||||
|
|
||||||
find_package(PkgConfig REQUIRED)
|
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})
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
[comment]: \page README Readme
|
||||||
|
|
||||||
# libeismultiplexer
|
# libeismultiplexer
|
||||||
|
|
||||||
libeismultiplexer is a shared library that allows you to control EISmultiplexer devices.
|
libeismultiplexer is a shared library that allows you to control EISmultiplexer devices.
|
||||||
|
|
|
@ -864,7 +864,7 @@ WARN_LOGFILE =
|
||||||
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
|
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
|
||||||
# Note: If this tag is empty the current directory is searched.
|
# Note: If this tag is empty the current directory is searched.
|
||||||
|
|
||||||
INPUT = @CMAKE_CURRENT_SOURCE_DIR@/eismultiplexer.h @CMAKE_CURRENT_SOURCE_DIR@/doc/mainpage.txt
|
INPUT = @CMAKE_CURRENT_SOURCE_DIR@/eismultiplexer.h @CMAKE_CURRENT_SOURCE_DIR@/doc/mainpage.txt @CMAKE_CURRENT_SOURCE_DIR@/README.md
|
||||||
|
|
||||||
# This tag can be used to specify the character encoding of the source files
|
# This tag can be used to specify the character encoding of the source files
|
||||||
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
|
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*! \mainpage libeismultiplexer manual
|
/*! \mainpage libeismultiplexer manual
|
||||||
|
|
||||||
libeismultiplexer is a shared library that allows you to control EISmultiplexer devices
|
libeismultiplexer is a shared library that allows you to control EISmultiplexer devices. For build instructions please see the \ref README.
|
||||||
|
|
||||||
An API reference can be found here: \subpage API
|
An API reference can be found here: \subpage API
|
||||||
|
|
||||||
|
|
|
@ -34,12 +34,20 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
static void usleep(uint64_t microseconds)
|
uint16_t* eismultiplexer_list_available_devices(size_t* count)
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct usbshm shm = {};
|
||||||
ts.tv_sec = microseconds / 1000000;
|
*count = 0;
|
||||||
ts.tv_nsec = (microseconds % 1000000) * 1000;
|
unsigned char** serials;
|
||||||
nanosleep(&ts, NULL);
|
int ret = usbshm_find(&shm, 0xfe17, 0x07dc, &serials, count);
|
||||||
|
usbshm_close(&shm);
|
||||||
|
if(ret < 0 || *count == 0)
|
||||||
|
return NULL;
|
||||||
|
uint16_t* numeric_serials = malloc(sizeof(*numeric_serials)*(*count));
|
||||||
|
for(size_t i = 0; i < *count; ++i) {
|
||||||
|
numeric_serials[i] = atoi((char*)serials[i]);
|
||||||
|
}
|
||||||
|
return numeric_serials;
|
||||||
}
|
}
|
||||||
|
|
||||||
int eismultiplexer_connect(struct eismultiplexer* muliplexer, uint16_t serial)
|
int eismultiplexer_connect(struct eismultiplexer* muliplexer, uint16_t serial)
|
||||||
|
@ -49,10 +57,6 @@ 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);
|
|
||||||
if(ret)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
unsigned char serialStr[5];
|
unsigned char serialStr[5];
|
||||||
snprintf((char*)serialStr, sizeof(serialStr), "%04hu", serial);
|
snprintf((char*)serialStr, sizeof(serialStr), "%04hu", serial);
|
||||||
|
|
||||||
|
@ -69,10 +73,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,36 +97,42 @@ 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_get_channel_count(struct eismultiplexer* muliplexer, uint16_t *count)
|
||||||
|
{
|
||||||
|
uint8_t buffer[2] = {};
|
||||||
|
int ret = usbshm_read_control_transfer(muliplexer->priv, 7, 0, 0, buffer, 1);
|
||||||
|
*count = buffer[0];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void eismultiplexer_disconnect(struct eismultiplexer* muliplexer)
|
void eismultiplexer_disconnect(struct eismultiplexer* muliplexer)
|
||||||
{
|
{
|
||||||
usbshm_distroy(muliplexer->priv);
|
usbshm_close(muliplexer->priv);
|
||||||
free(muliplexer->priv);
|
free(muliplexer->priv);
|
||||||
muliplexer->priv = NULL;
|
muliplexer->priv = NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@ typedef enum {
|
||||||
CHANNEL_E = (1 << 4),
|
CHANNEL_E = (1 << 4),
|
||||||
CHANNEL_F = (1 << 5),
|
CHANNEL_F = (1 << 5),
|
||||||
CHANNEL_G = (1 << 6),
|
CHANNEL_G = (1 << 6),
|
||||||
|
CHANNEL_H = (1 << 7),
|
||||||
CHANNEL_NONE = 0,
|
CHANNEL_NONE = 0,
|
||||||
} channel_t;
|
} channel_t;
|
||||||
|
|
||||||
|
@ -57,6 +58,13 @@ struct eismultiplexer {
|
||||||
struct usbshm* priv;
|
struct usbshm* priv;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Lists the available eismultiplexer devices
|
||||||
|
* @param count The number of available eismultiplexer devices
|
||||||
|
* @return An newly allocated array of length count with the serial numbers of the available devices to be freed by the user with free()
|
||||||
|
*/
|
||||||
|
uint16_t* eismultiplexer_list_available_devices(size_t* count);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Attempts to connect to a EISmultiplexer device and initializes a eismultiplexer struct
|
* @brief Attempts to connect to a EISmultiplexer device and initializes a eismultiplexer struct
|
||||||
* @param multiplexer pointer to a eismultiplexer struct to initialize
|
* @param multiplexer pointer to a eismultiplexer struct to initialize
|
||||||
|
@ -105,6 +113,14 @@ channel_t eismultiplexer_get_connected(struct eismultiplexer* muliplexer);
|
||||||
*/
|
*/
|
||||||
int eismultiplexer_set_led(struct eismultiplexer* muliplexer, bool on);
|
int eismultiplexer_set_led(struct eismultiplexer* muliplexer, bool on);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the number of channels on the device
|
||||||
|
* @param multiplexer pointer to an eismultiplexer struct
|
||||||
|
* @param count a pointer where the nummber of channels will be stored.
|
||||||
|
* @return 0 on success and < 0 on failure
|
||||||
|
*/
|
||||||
|
int eismultiplexer_get_channel_count(struct eismultiplexer* muliplexer, uint16_t *count);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Disconnects from the eismultiplexer
|
* @brief Disconnects from the eismultiplexer
|
||||||
*/
|
*/
|
||||||
|
|
28
main.c
28
main.c
|
@ -80,6 +80,8 @@ void print_commands(void)
|
||||||
puts("get\t\t\t | get the state of all channels");
|
puts("get\t\t\t | get the state of all channels");
|
||||||
puts("read [ADDRESS] [LENGTH]\t | read from the device eeprom at address");
|
puts("read [ADDRESS] [LENGTH]\t | read from the device eeprom at address");
|
||||||
puts("write [ADDRESS] [LENGTH] | write to the device eeprom at address");
|
puts("write [ADDRESS] [LENGTH] | write to the device eeprom at address");
|
||||||
|
puts("list | list the serial nummbers of the connected devices");
|
||||||
|
puts("channels | get the channel of the device");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int process_commands(char** commands, size_t command_count, struct eismultiplexer* multiplexer, char* name)
|
static int process_commands(char** commands, size_t command_count, struct eismultiplexer* multiplexer, char* name)
|
||||||
|
@ -159,6 +161,32 @@ static int process_commands(char** commands, size_t command_count, struct eismul
|
||||||
printf("Channel %c: %s\n", (char)('A'+i), connected ? "on" : "off");
|
printf("Channel %c: %s\n", (char)('A'+i), connected ? "on" : "off");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if(strcmp(commands[0], "channels") == 0)
|
||||||
|
{
|
||||||
|
uint16_t channels;
|
||||||
|
if(eismultiplexer_get_channel_count(multiplexer, &channels) < 0)
|
||||||
|
{
|
||||||
|
printf("could not get the number of channels\n");
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
printf("The connected device has %u channels\n", channels);
|
||||||
|
}
|
||||||
|
else if(strcmp(commands[0], "list") == 0)
|
||||||
|
{
|
||||||
|
size_t count;
|
||||||
|
uint16_t* serials = eismultiplexer_list_available_devices(&count);
|
||||||
|
if(count == 0)
|
||||||
|
{
|
||||||
|
printf("No devices connected\n");
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Connected devices:\n");
|
||||||
|
for(size_t i = 0; i < count; ++i)
|
||||||
|
printf("%04u\n", serials[i]);
|
||||||
|
|
||||||
|
free(serials);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(command_count != 2)
|
if(command_count != 2)
|
||||||
|
|
|
@ -35,14 +35,14 @@
|
||||||
|
|
||||||
const char *argp_program_version = "eismulitplexer_cli";
|
const char *argp_program_version = "eismulitplexer_cli";
|
||||||
const char *argp_program_bug_address = "<carl@uvos.xyz>";
|
const char *argp_program_bug_address = "<carl@uvos.xyz>";
|
||||||
static char doc[] = "Applicaion to control eismultiplexer devices";
|
static char doc[] = "Application to control eismultiplexer devices";
|
||||||
static char args_doc[] = "COMMAND";
|
static char args_doc[] = "COMMAND";
|
||||||
|
|
||||||
static struct argp_option options[] =
|
static struct argp_option options[] =
|
||||||
{
|
{
|
||||||
{"interactive", 'i', 0, 0, "run in interactive mode" },
|
{"interactive", 'i', 0, 0, "run in interactive mode" },
|
||||||
{"pipe", 'p', 0, 0, "run in pipe mode" },
|
{"pipe", 'p', 0, 0, "run in pipe mode" },
|
||||||
{"serial", 's', "[NUMMBER]", 0, "serial number of device to connect to"},
|
{"serial", 's', "[NUMBER]", 0, "serial number of device to connect to"},
|
||||||
{"list", 'l', 0, 0, "list commands"},
|
{"list", 'l', 0, 0, "list commands"},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
340
usbshm.c
340
usbshm.c
|
@ -25,101 +25,136 @@
|
||||||
* 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 int usbshm_init(struct usbshm* instance)
|
||||||
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)
|
|
||||||
{
|
|
||||||
while(!usbshm_ready(instance))
|
|
||||||
sleep(1);
|
|
||||||
free(instance->priv->transfer);
|
|
||||||
libusb_close(instance->priv->handle);
|
|
||||||
free(instance->priv->buffer);
|
|
||||||
free(instance->priv);
|
|
||||||
if(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)
|
|
||||||
{
|
|
||||||
while(!threadStop)
|
|
||||||
{
|
|
||||||
libusb_handle_events_timeout_completed(NULL, &timeout, NULL);
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool usbshm_isOpen(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;
|
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool usbshm_ready(struct usbshm* instance)
|
|
||||||
|
static void usbshm_exit(void)
|
||||||
{
|
{
|
||||||
return instance->priv->transfer == NULL;
|
if(--object_counter == 0)
|
||||||
|
{
|
||||||
|
libusb_exit(context);
|
||||||
|
context = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
|
void usbshm_close(struct usbshm* instance)
|
||||||
|
{
|
||||||
|
if(!instance->handle)
|
||||||
|
return;
|
||||||
|
pthread_mutex_lock(instance->mutex);
|
||||||
|
libusb_close(instance->handle);
|
||||||
|
if(instance->serial)
|
||||||
|
free(instance->serial);
|
||||||
|
instance->handle = NULL;
|
||||||
|
usbshm_exit();
|
||||||
|
pthread_mutex_unlock(instance->mutex);
|
||||||
|
pthread_mutex_destroy(instance->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
|
bool usbshm_is_open(struct usbshm* instance)
|
||||||
|
{
|
||||||
|
return instance->handle != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
|
int usbshm_find(struct usbshm* instance, int vendorID, int productID, unsigned char*** serials, size_t* count)
|
||||||
|
{
|
||||||
|
int ret = usbshm_init(instance);
|
||||||
|
if(ret < 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
pthread_mutex_lock(instance->mutex);
|
||||||
|
|
||||||
|
libusb_device** list;
|
||||||
|
libusb_device_handle* handle;
|
||||||
|
int lusbcount = libusb_get_device_list(context, &list);
|
||||||
|
int errorCode = 0;
|
||||||
|
|
||||||
|
*count = 0;
|
||||||
|
*serials = malloc(sizeof(*serials)*lusbcount);
|
||||||
|
|
||||||
|
if(lusbcount > 0)
|
||||||
|
{
|
||||||
|
struct libusb_device_descriptor desc = {0};
|
||||||
|
for(int i = 0; i < lusbcount; ++i)
|
||||||
|
{
|
||||||
|
libusb_get_device_descriptor(list[i], &desc);
|
||||||
|
if(desc.idVendor == vendorID && desc.idProduct == productID)
|
||||||
|
{
|
||||||
|
errorCode = libusb_open(list[i], &handle) < 0 ? USBSHM_ERROR_ERR : 0;
|
||||||
|
if(errorCode != USBSHM_ERROR_ERR && handle)
|
||||||
|
{
|
||||||
|
(*serials)[*count] = calloc(16, 1);
|
||||||
|
libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (*serials)[*count], 16);
|
||||||
|
++(*count);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
handle = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Can not list devices\n");
|
||||||
|
pthread_mutex_unlock(instance->mutex);
|
||||||
|
return USBSHM_ERROR_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_mutex_unlock(instance->mutex);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
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;
|
int ret = usbshm_init(instance);
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
if(ret < 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
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,34 +164,43 @@ 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->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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
instance->handle = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
@ -165,7 +209,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
|
||||||
{
|
{
|
||||||
|
@ -174,120 +218,76 @@ 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)
|
__attribute__ ((visibility ("hidden")))
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
void usbshm_reset(struct usbshm* instance)
|
void usbshm_reset(struct usbshm* instance)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(libusbDataMutex);
|
if(!instance->handle)
|
||||||
libusb_reset_device(instance->priv->handle);
|
return;
|
||||||
pthread_mutex_unlock(libusbDataMutex);
|
pthread_mutex_lock(instance->mutex);
|
||||||
|
libusb_reset_device(instance->handle);
|
||||||
|
pthread_mutex_unlock(instance->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
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,
|
__attribute__ ((visibility ("hidden")))
|
||||||
char* buffer, const uint8_t length,
|
int usbshm_write_control_transfer(struct usbshm* instance, const uint8_t request,
|
||||||
|
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(!instance->handle)
|
||||||
|
return USBSHM_ERROR_NOT_CONNECTED;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("hidden")))
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if(!instance->handle)
|
||||||
|
return USBSHM_ERROR_NOT_CONNECTED;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
28
usbshm.h
28
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);
|
int usbshm_find(struct usbshm* instance, int vendorID, int productID, unsigned char*** serials, size_t* count);
|
||||||
|
|
||||||
int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data), void* user_data);
|
|
||||||
|
|
||||||
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);
|
void usbshm_close(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