From f69011d589397470057d1e644ce5d655f6475ca5 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 30 Jun 2025 15:28:48 +0200 Subject: [PATCH] add channel count and device listing functions --- eismultiplexer.c | 24 +++++++++++++++++++++++ eismultiplexer.h | 16 +++++++++++++++ main.c | 28 ++++++++++++++++++++++++++ usbshm.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ usbshm.h | 2 ++ 5 files changed, 121 insertions(+) diff --git a/eismultiplexer.c b/eismultiplexer.c index fc542f5..925b344 100644 --- a/eismultiplexer.c +++ b/eismultiplexer.c @@ -34,6 +34,22 @@ #include #include +uint16_t* eismultiplexer_list_available_devices(size_t* count) +{ + struct usbshm shm = {}; + *count = 0; + unsigned char** serials; + 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 ret; @@ -85,6 +101,14 @@ channel_t eismultiplexer_get_connected(struct eismultiplexer* muliplexer) 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 ret; diff --git a/eismultiplexer.h b/eismultiplexer.h index 5f6f70b..711a318 100644 --- a/eismultiplexer.h +++ b/eismultiplexer.h @@ -50,6 +50,7 @@ typedef enum { CHANNEL_E = (1 << 4), CHANNEL_F = (1 << 5), CHANNEL_G = (1 << 6), + CHANNEL_H = (1 << 7), CHANNEL_NONE = 0, } channel_t; @@ -57,6 +58,13 @@ struct eismultiplexer { 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 * @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); +/** + * @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 */ diff --git a/main.c b/main.c index 73fe93c..13539a4 100644 --- a/main.c +++ b/main.c @@ -80,6 +80,8 @@ void print_commands(void) puts("get\t\t\t | get the state of all channels"); puts("read [ADDRESS] [LENGTH]\t | read from 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) @@ -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"); } } + 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 { if(command_count != 2) diff --git a/usbshm.c b/usbshm.c index 0f8f70c..e588088 100644 --- a/usbshm.c +++ b/usbshm.c @@ -94,6 +94,57 @@ 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) { diff --git a/usbshm.h b/usbshm.h index 1e72b69..3430d22 100644 --- a/usbshm.h +++ b/usbshm.h @@ -48,6 +48,8 @@ struct usbshm { pthread_mutex_t* mutex; }; +int usbshm_find(struct usbshm* instance, int vendorID, int productID, unsigned char*** serials, size_t* count); + int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial); void usbshm_close(struct usbshm* instance);