From d4402aa29606866476f982c6667f3deba94b9593 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 22 Jun 2023 20:42:12 +0200 Subject: [PATCH] Add support for the WDBI function The RDBI (Read Data by Identifier) has a WDBI (Write Data by Identifier) counterpart, which supports changing settings on the dive computer. --- src/shearwater_common.c | 37 +++++++++++++++++++++++++++++++++++++ src/shearwater_common.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/src/shearwater_common.c b/src/shearwater_common.c index c184bb3..7a47f58 100644 --- a/src/shearwater_common.c +++ b/src/shearwater_common.c @@ -39,6 +39,9 @@ #define RDBI_REQUEST 0x22 #define RDBI_RESPONSE 0x62 +#define WDBI_REQUEST 0x2E +#define WDBI_RESPONSE 0x6E + dc_status_t shearwater_common_setup (shearwater_common_device_t *device, dc_context_t *context, dc_iostream_t *iostream) { @@ -552,6 +555,40 @@ shearwater_common_rdbi (shearwater_common_device_t *device, unsigned int id, uns return status; } +dc_status_t +shearwater_common_wdbi (shearwater_common_device_t *device, unsigned int id, const unsigned char data[], unsigned int size) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + + if (size + 3 > SZ_PACKET) { + return DC_STATUS_INVALIDARGS; + } + + // Transfer the request. + unsigned int n = 0; + unsigned char request[SZ_PACKET] = { + WDBI_REQUEST, + (id >> 8) & 0xFF, + (id ) & 0xFF}; + if (size) { + memcpy (request + 3, data, size); + } + unsigned char response[SZ_PACKET]; + status = shearwater_common_transfer (device, request, size + 3, response, sizeof (response), &n); + if (status != DC_STATUS_SUCCESS) { + return status; + } + + // Verify the response. + if (n < 3 || response[0] != WDBI_RESPONSE || response[1] != request[1] || response[2] != request[2]) { + ERROR (abstract->context, "Unexpected response packet."); + return DC_STATUS_PROTOCOL; + } + + return status; +} + unsigned int shearwater_common_get_model (shearwater_common_device_t *device, unsigned int hardware) { diff --git a/src/shearwater_common.h b/src/shearwater_common.h index 8f8e356..1c25b75 100644 --- a/src/shearwater_common.h +++ b/src/shearwater_common.h @@ -67,6 +67,9 @@ shearwater_common_download (shearwater_common_device_t *device, dc_buffer_t *buf dc_status_t shearwater_common_rdbi (shearwater_common_device_t *device, unsigned int id, unsigned char data[], unsigned int size); +dc_status_t +shearwater_common_wdbi (shearwater_common_device_t *device, unsigned int id, const unsigned char data[], unsigned int size); + unsigned int shearwater_common_get_model (shearwater_common_device_t *device, unsigned int hardware);