diff --git a/src/shearwater_common.c b/src/shearwater_common.c index b809a3c..eabed11 100644 --- a/src/shearwater_common.c +++ b/src/shearwater_common.c @@ -35,7 +35,7 @@ #define ESC_END 0xDC #define ESC_ESC 0xDD -#define EXITCODE(n) ((n) < 0 ? (n) : 0) +#define EXITCODE(n) ((n) < 0 ? DC_STATUS_IO : DC_STATUS_TIMEOUT) dc_status_t shearwater_common_open (shearwater_common_device_t *device, dc_context_t *context, const char *name) @@ -141,7 +141,7 @@ shearwater_common_decompress_xor (unsigned char *data, unsigned int size) } -static int +static dc_status_t shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned char data[], unsigned int size) { int n = 0; @@ -206,12 +206,12 @@ shearwater_common_slip_write (shearwater_common_device_t *device, const unsigned return EXITCODE(n); } - return size; + return DC_STATUS_SUCCESS; } -static int -shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char data[], unsigned int size) +static dc_status_t +shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char data[], unsigned int size, unsigned int *actual) { unsigned int received = 0; @@ -237,7 +237,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d // packets generated by the duplicate END characters which // are sent to try to detect line noise. if (received) - return received; + goto done; else break; case ESC: @@ -267,16 +267,25 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d } } - return received; +done: + + if (received > size) + return DC_STATUS_PROTOCOL; + + if (actual) + *actual = received; + + return DC_STATUS_SUCCESS; } dc_status_t shearwater_common_transfer (shearwater_common_device_t *device, const unsigned char input[], unsigned int isize, unsigned char output[], unsigned int osize, unsigned int *actual) { + dc_status_t status = DC_STATUS_SUCCESS; dc_device_t *abstract = (dc_device_t *) device; unsigned char packet[SZ_PACKET + 4]; - int n = 0; + unsigned int n = 0; if (isize > SZ_PACKET || osize > SZ_PACKET) return DC_STATUS_INVALIDARGS; @@ -292,13 +301,10 @@ shearwater_common_transfer (shearwater_common_device_t *device, const unsigned c memcpy (packet + 4, input, isize); // Send the request packet. - n = shearwater_common_slip_write (device, packet, isize + 4); - if (n != isize + 4) { + status = shearwater_common_slip_write (device, packet, isize + 4); + if (status != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the request packet."); - if (n < 0) - return DC_STATUS_IO; - else - return DC_STATUS_TIMEOUT; + return status; } // Return early if no response packet is requested. @@ -309,15 +315,10 @@ shearwater_common_transfer (shearwater_common_device_t *device, const unsigned c } // Receive the response packet. - n = shearwater_common_slip_read (device, packet, sizeof (packet)); - if (n <= 0 || n > sizeof (packet)) { + status = shearwater_common_slip_read (device, packet, sizeof (packet), &n); + if (status != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to receive the response packet."); - if (n < 0) - return DC_STATUS_IO; - else if (n > sizeof (packet)) - return DC_STATUS_PROTOCOL; - else - return DC_STATUS_TIMEOUT; + return status; } // Validate the packet header.