Skip to content

Commit 1254cb7

Browse files
jefdriesenmikeller
authored andcommitted
Detect NAK response packets
Errors are reported by the dive computer with a NAK packet containing an error code. Detect and handle such packets and log the error code.
1 parent dfb8fae commit 1254cb7

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/seac_screen.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434

3535
#define MAXRETRIES 4
3636

37+
#define START 0x55
38+
#define ACK 0x09
39+
#define NAK 0x30
40+
41+
#define ERR_INVALID_CMD 0x02
42+
#define ERR_INVALID_LENGTH 0x03
43+
#define ERR_INVALID_DATA 0x04
44+
#define ERR_BATTERY_LOW 0x05
45+
#define ERR_BUSY 0x06
46+
3747
#define SZ_MAXCMD 8
3848
#define SZ_MAXRSP SZ_READ
3949

@@ -106,7 +116,7 @@ seac_screen_send (seac_screen_device_t *device, unsigned short cmd, const unsign
106116
// Setup the data packet
107117
unsigned len = size + 6;
108118
unsigned char packet[SZ_MAXCMD + 7] = {
109-
0x55,
119+
START,
110120
(len >> 8) & 0xFF,
111121
(len ) & 0xFF,
112122
(cmd >> 8) & 0xFF,
@@ -144,7 +154,7 @@ seac_screen_receive (seac_screen_device_t *device, unsigned short cmd, unsigned
144154
}
145155

146156
// Verify the start byte.
147-
if (packet[0] != 0x55) {
157+
if (packet[0] != START) {
148158
ERROR (abstract->context, "Unexpected start byte (%02x).", packet[0]);
149159
return DC_STATUS_PROTOCOL;
150160
}
@@ -173,17 +183,32 @@ seac_screen_receive (seac_screen_device_t *device, unsigned short cmd, unsigned
173183

174184
// Verify the command response.
175185
unsigned int rsp = array_uint16_be (packet + 3);
176-
unsigned int misc = packet[1 + length - 3];
177-
if (rsp != cmd || misc != 0x09) {
178-
ERROR (abstract->context, "Unexpected command response (%04x %02x).", rsp, misc);
186+
if (rsp != cmd) {
187+
ERROR (abstract->context, "Unexpected command response (%04x).", rsp);
188+
return DC_STATUS_PROTOCOL;
189+
}
190+
191+
// Verify the ACK/NAK byte.
192+
unsigned int type = packet[1 + length - 3];
193+
if (type != ACK && type != NAK) {
194+
ERROR (abstract->context, "Unexpected ACK/NAK byte (%02x).", type);
179195
return DC_STATUS_PROTOCOL;
180196
}
181197

182-
if (length - 7 != size) {
198+
// Verify the length of the packet.
199+
unsigned int expected = (type == ACK ? size : 1) + 7;
200+
if (length != expected) {
183201
ERROR (abstract->context, "Unexpected packet length (%u).", length);
184202
return DC_STATUS_PROTOCOL;
185203
}
186204

205+
// Get the error code from a NAK packet.
206+
if (type == NAK) {
207+
unsigned int errcode = packet[5];
208+
ERROR (abstract->context, "Received NAK packet with error code %02x.", errcode);
209+
return DC_STATUS_PROTOCOL;
210+
}
211+
187212
memcpy (data, packet + 5, length - 7);
188213

189214
return DC_STATUS_SUCCESS;

0 commit comments

Comments
 (0)