From 8d48986672a2a3f42efdccb3606167d4623e7a7a Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm <philipp@uvos.xyz> Date: Mon, 9 Oct 2023 14:18:13 +0200 Subject: [PATCH] add reset function --- coincellhell.c | 12 ++++++++++++ coincellhell/coincellhell.h | 8 +++++++- usbshm.c | 12 ++++++++++-- usbshm.h | 1 + 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/coincellhell.c b/coincellhell.c index 5417b23..42ed9d1 100644 --- a/coincellhell.c +++ b/coincellhell.c @@ -235,6 +235,18 @@ const uint8_t* coincellhell_get_fw_git_revision(struct coincellhell* hell) return gitrev; } +int coincellhell_reset(struct coincellhell* hell) +{ + usbshm_reopen(hell->priv); + for(int i = 0; i < 4; ++i) + { + int ret = coincellhell_set_enabled(hell, 0, false); + if(ret != 0) + return ret; + } + return 0; +} + void coincellhell_disconnect(struct coincellhell* hell) { usbshm_distroy(hell->priv); diff --git a/coincellhell/coincellhell.h b/coincellhell/coincellhell.h index 3282493..3d48332 100644 --- a/coincellhell/coincellhell.h +++ b/coincellhell/coincellhell.h @@ -117,7 +117,7 @@ int coincellhell_get_state(struct coincellhell* hell, uint8_t heater, struct hea /** * @brief Sets the enabled state for a give heater - * @param heater heater for which to set the temperature + * @param heater heater for which to set the enabled state * @param state A struct where the state will be stored * @return 0 on sucess and < 0 on failure */ @@ -153,6 +153,12 @@ int coincellhell_cancle_ramp(struct coincellhell* hell, uint8_t heater); */ int coincellhell_set_led(struct coincellhell* hell, bool on); +/** + * @brief resets the device + * @param hell pointer to a coincellhell struct + */ +int coincellhell_reset(struct coincellhell* hell); + /** * @brief Disconnects from the coincellhell */ diff --git a/usbshm.c b/usbshm.c index 42ed248..7862072 100644 --- a/usbshm.c +++ b/usbshm.c @@ -60,6 +60,8 @@ void usbshm_distroy(struct usbshm* instance) libusb_close(instance->priv->handle); free(instance->priv->buffer); free(instance->priv); + if(instance->serial) + free(instance->serial); if(--objectCounter == 0) { threadStop = true; @@ -92,6 +94,7 @@ int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, u instance->priv->buffer = NULL; instance->vendorID = 0; instance->productID = 0; + instance->serial = NULL; instance->dataCallback = dataCallback; instance->user_data = user_data; if(objectCounter == 0) @@ -156,6 +159,11 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi { instance->vendorID = vendorID; instance->productID = productID; + if(serial) + { + instance->serial = calloc(strlen((char*)serial), 1); + memcpy(instance->serial, serial, strlen((char*)serial)); + } libusb_set_auto_detach_kernel_driver(instance->priv->handle, true); } else @@ -176,14 +184,14 @@ bool usbshm_usbshm_isOpen(struct usbshm* instance) void usbshm_reset(struct usbshm* instance) { - printf("Usb transfer failed with %u\n", instance->priv->transfer->status); libusb_reset_device(instance->priv->handle); } void usbshm_reopen(struct usbshm* instance) { + usbshm_reset(instance); libusb_close(instance->priv->handle); - usbshm_open(instance, instance->vendorID, instance->productID, NULL); + usbshm_open(instance, instance->vendorID, instance->productID, instance->serial); } int usbshm_writeControlTransfer(struct usbshm* instance, const uint8_t request, diff --git a/usbshm.h b/usbshm.h index d03ad2d..0f44eb2 100644 --- a/usbshm.h +++ b/usbshm.h @@ -45,6 +45,7 @@ struct usbshm { struct usbshm_priv* priv; int vendorID; int productID; + unsigned char* serial; void* user_data; void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data); }; -- GitLab