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.
This commit is contained in:
Jef Driesen 2021-10-07 19:56:52 +02:00
parent 9ff6e5caad
commit ba4a119a4f

View File

@ -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;