From ba4a119a4f2bb45d97d0c31adbbd1fa6e1b635f4 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 7 Oct 2021 19:56:52 +0200 Subject: [PATCH] Detect NAK response packets When the dive computer receives a command it doesn't support, it sends back a single byte NAK (0xA5) packet instead of the expected ACK byte (0x5A) at the start of the packet. Retrying is pointless in this case, because the next attempt will also fail. Instead, return immediately with an appropriate error code, and let the upper layers handle the unsupported command. Note that the detection is currently only enabled for BLE communication. --- src/oceanic_atom2.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index 060a1cc..c327ceb 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -682,6 +682,22 @@ oceanic_atom2_packet (oceanic_atom2_device_t *device, const unsigned char comman return status; } + // Verify the number of bytes. + if (nbytes < 1) { + ERROR (abstract->context, "Invalid packet size (%u).", nbytes); + return DC_STATUS_PROTOCOL; + } + + // Verify the ACK byte of the answer. + if (packet[0] != ack) { + ERROR (abstract->context, "Unexpected answer start byte(s)."); + if (packet[0] == (unsigned char) ~ack) { + return DC_STATUS_UNSUPPORTED; + } else { + return DC_STATUS_PROTOCOL; + } + } + // Verify the number of bytes. if (nbytes < 1 + asize + crc_size) { ERROR (abstract->context, "Unexpected number of bytes received (%u %u).", nbytes, 1 + asize + crc_size); @@ -690,12 +706,6 @@ oceanic_atom2_packet (oceanic_atom2_device_t *device, const unsigned char comman nbytes -= 1 + crc_size; - // Verify the ACK byte of the answer. - if (packet[0] != ack) { - ERROR (abstract->context, "Unexpected answer start byte(s)."); - return DC_STATUS_PROTOCOL; - } - if (asize) { // Verify the checksum of the answer. unsigned short crc, ccrc;