diff --git a/coincellhell.c b/coincellhell.c index 42ed9d1bcc38e0f0d2213d4e0041502a7379a288..23e42ad4bfc9bded2ac573d5f0cc62d91b4279b5 100644 --- a/coincellhell.c +++ b/coincellhell.c @@ -49,6 +49,8 @@ static void usleep(uint64_t microseconds) int coincellhell_connect(struct coincellhell* hell, uint16_t serial) { + if(!hell) + return -2; int ret; hell->priv = malloc(sizeof(*hell->priv)); if(!hell->priv) @@ -69,6 +71,8 @@ int coincellhell_connect(struct coincellhell* hell, uint16_t serial) int coincellhell_get_temperature(struct coincellhell* hell, uint8_t heater, temperature_sensor_location_t location, float* temperature) { + if(!hell || !hell->priv) + return -2; int16_t temperatureRaw = 0; uint8_t *dataPtr = (uint8_t*)&temperatureRaw; int ret; @@ -80,6 +84,8 @@ int coincellhell_get_temperature(struct coincellhell* hell, uint8_t heater, temp int coincellhell_set_temperature(struct coincellhell* hell, uint8_t heater, float temperature) { + if(!hell || !hell->priv) + return -2; if(temperature*10.0f > INT16_MAX || temperature*10.0f < INT16_MIN) return -6; int ret; @@ -90,6 +96,8 @@ int coincellhell_set_temperature(struct coincellhell* hell, uint8_t heater, floa int coincellhell_get_temperature_setpoint(struct coincellhell* hell, uint8_t heater, float* temperature) { + if(!hell || !hell->priv) + return -2; int16_t temperatureRaw = 0; uint8_t *dataPtr = (uint8_t*)&temperatureRaw; int ret; @@ -104,6 +112,10 @@ int coincellhell_get_state(struct coincellhell* hell, uint8_t heater, struct hea { uint8_t buf[8]; int ret; + + if(!hell || !hell->priv) + return -2; + while((ret = usbshm_readControlTransferSync(hell->priv, COMMAND_HEATER_GET_STATE, 0, heater, buf, 8)) == USBSHM_ERROR_AGAIN) usleep(100000); @@ -143,6 +155,10 @@ int coincellhell_get_state(struct coincellhell* hell, uint8_t heater, struct hea int coincellhell_set_enabled(struct coincellhell* hell, uint8_t heater, bool enabled) { int ret; + + if(!hell || !hell->priv) + return -2; + while((ret = usbshm_writeControlTransfer(hell->priv, COMMAND_HEATER_SET_ENABLED, NULL, 0, enabled, heater)) == USBSHM_ERROR_AGAIN) usleep(100000); return ret; @@ -153,6 +169,10 @@ int coincellhell_check_ready(struct coincellhell* hell, bool* ready) *ready = false; int ret; uint8_t readybits; + + if(!hell || !hell->priv) + return -2; + while((ret = usbshm_readControlTransferSync(hell->priv, COMMAND_READY, 0, 0, &readybits, 1)) == USBSHM_ERROR_AGAIN) usleep(100000); *ready = !(~readybits & 0x0F); @@ -163,8 +183,11 @@ int coincellhell_set_temperature_ramp(struct coincellhell* hell, uint8_t heater, { time_t currentTime = time(NULL); time_t timeDelta = end_time - currentTime; + if(timeDelta < 0) return -1; + if(!hell || !hell->priv) + return -2; char buffer[7]; buffer[0] = heater; @@ -185,6 +208,10 @@ int coincellhell_set_temperature_ramp(struct coincellhell* hell, uint8_t heater, int coincellhell_cancle_ramp(struct coincellhell* hell, uint8_t heater) { int ret; + + if(!hell || !hell->priv) + return -2; + while((ret = usbshm_writeControlTransfer(hell->priv, COMMAND_HEATER_RAMP_CANCLE, NULL, 0, 0, heater)) == USBSHM_ERROR_AGAIN) usleep(100000); return ret; @@ -193,6 +220,10 @@ int coincellhell_cancle_ramp(struct coincellhell* hell, uint8_t heater) int coincellhell_set_led(struct coincellhell* hell, bool on) { int ret; + + if(!hell || !hell->priv) + return -2; + while((ret = usbshm_writeControlTransfer(hell->priv, on ? COMMAND_LED_ON : COMMAND_LED_OFF, NULL, 0, 0, 0)) == USBSHM_ERROR_AGAIN) usleep(100000); return ret; @@ -201,6 +232,10 @@ int coincellhell_set_led(struct coincellhell* hell, bool on) int coincellhell_write_eeprom(struct coincellhell* hell, uint16_t addr, uint16_t value) { int ret; + + if(!hell || !hell->priv) + return -2; + while((ret = usbshm_writeControlTransfer(hell->priv, COMMAND_WRITE_EEPROM, NULL, 0, value, addr)) == USBSHM_ERROR_AGAIN) usleep(100000); return ret; @@ -209,6 +244,10 @@ int coincellhell_write_eeprom(struct coincellhell* hell, uint16_t addr, uint16_t uint16_t coincellhell_read_eeprom(struct coincellhell* hell, uint16_t addr) { uint8_t buffer[2] = {}; + + if(!hell || !hell->priv) + return 0; + usbshm_readControlTransferSync(hell->priv, COMMAND_READ_EEPROM, 0, addr, buffer, 2); return *((uint16_t*)buffer); } @@ -217,6 +256,10 @@ uint16_t coincellhell_read_eeprom(struct coincellhell* hell, uint16_t addr) uint8_t coincellhell_read_oscal(struct coincellhell* hell) { uint8_t oscal; + + if(!hell || !hell->priv) + return 0; + usbshm_readControlTransferSync(hell->priv, COMMAND_READ_OSCAL, 0, 0, &oscal, 1); return oscal; } @@ -224,6 +267,10 @@ uint8_t coincellhell_read_oscal(struct coincellhell* hell) uint32_t coincellhell_get_seconds(struct coincellhell* hell) { uint32_t seconds; + + if(!hell || !hell->priv) + return 0; + usbshm_readControlTransferSync(hell->priv, COMMAND_GET_SECONDS, 0, 0, (uint8_t*)&seconds, 4); return seconds; } @@ -231,12 +278,18 @@ uint32_t coincellhell_get_seconds(struct coincellhell* hell) const uint8_t* coincellhell_get_fw_git_revision(struct coincellhell* hell) { static uint8_t gitrev[9] = {0}; + + if(!hell || !hell->priv) + return gitrev; + usbshm_readControlTransferSync(hell->priv, COMMAND_GET_FIRMWARE_GITREV, 0, 0, gitrev, 8); return gitrev; } int coincellhell_reset(struct coincellhell* hell) { + if(!hell || !hell->priv) + return -1; usbshm_reopen(hell->priv); for(int i = 0; i < 4; ++i) { @@ -249,6 +302,8 @@ int coincellhell_reset(struct coincellhell* hell) void coincellhell_disconnect(struct coincellhell* hell) { + if(!hell || !hell->priv) + return; usbshm_distroy(hell->priv); free(hell->priv); hell->priv = NULL;