From 2934c6a618cc78b28ac863e352e17dd93621d5c2 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sun, 12 May 2013 23:19:51 +0200 Subject: [PATCH] Shutdown the connection cleanly. A shutdown command should be send to the device, before the connection is actually closed. In the absence of this command, the device will display an error, even if the data transfer itself was successful! --- src/shearwater_common.c | 12 ++++++++++-- src/shearwater_common.h | 3 +++ src/shearwater_petrel.c | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/shearwater_common.c b/src/shearwater_common.c index fef8460..f3cdc48 100644 --- a/src/shearwater_common.c +++ b/src/shearwater_common.c @@ -256,7 +256,7 @@ shearwater_common_slip_read (shearwater_common_device_t *device, unsigned char d } -static dc_status_t +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_device_t *abstract = (dc_device_t *) device; @@ -283,6 +283,13 @@ shearwater_common_transfer (shearwater_common_device_t *device, const unsigned c return DC_STATUS_TIMEOUT; } + // Return early if no response packet is requested. + if (osize == 0) { + if (actual) + *actual = 0; + return DC_STATUS_SUCCESS; + } + // Receive the response packet. n = shearwater_common_slip_read (device, packet, sizeof (packet)); if (n <= 0 || n > sizeof (packet)) { @@ -309,7 +316,8 @@ shearwater_common_transfer (shearwater_common_device_t *device, const unsigned c } memcpy (output, packet + 4, length - 1); - *actual = length - 1; + if (actual) + *actual = length - 1; return DC_STATUS_SUCCESS; } diff --git a/src/shearwater_common.h b/src/shearwater_common.h index 78aa6c7..dbb717e 100644 --- a/src/shearwater_common.h +++ b/src/shearwater_common.h @@ -40,6 +40,9 @@ shearwater_common_open (shearwater_common_device_t *device, dc_context_t *contex dc_status_t shearwater_common_close (shearwater_common_device_t *device); +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 shearwater_common_download (shearwater_common_device_t *device, dc_buffer_t *buffer, unsigned int address, unsigned int size, unsigned int compression); diff --git a/src/shearwater_petrel.c b/src/shearwater_petrel.c index 5bc761d..ac84848 100644 --- a/src/shearwater_petrel.c +++ b/src/shearwater_petrel.c @@ -101,6 +101,10 @@ shearwater_petrel_device_close (dc_device_t *abstract) dc_status_t rc = DC_STATUS_SUCCESS; shearwater_common_device_t *device = (shearwater_common_device_t *) abstract; + // Shutdown the device. + unsigned char request[] = {0x2E, 0x90, 0x20, 0x00}; + shearwater_common_transfer (device, request, sizeof (request), NULL, 0, NULL); + // Close the device. rc = shearwater_common_close (device);