Skip to content

Commit 10ab01d

Browse files
jefdriesenmikeller
authored andcommitted
Read the payload and trailer byte separately
Reading the payload and the trailer byte separately simplifies the code in the caller because the destination buffer doesn't need space to store the extra trailer byte.
1 parent e2ad016 commit 10ab01d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/cressi_edy.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,19 @@ cressi_edy_packet (cressi_edy_device_t *device, const unsigned char command[], u
141141
ERROR (abstract->context, "Failed to receive the answer.");
142142
return status;
143143
}
144+
}
145+
146+
if (trailer) {
147+
// Receive the trailer byte.
148+
unsigned char end = 0;
149+
status = dc_iostream_read (device->iostream, &end, 1, NULL);
150+
if (status != DC_STATUS_SUCCESS) {
151+
ERROR (abstract->context, "Failed to receive the answer.");
152+
return status;
153+
}
144154

145155
// Verify the trailer of the packet.
146-
if (trailer && answer[asize - 1] != 0x45) {
156+
if (end != 0x45) {
147157
ERROR (abstract->context, "Unexpected answer trailer byte.");
148158
return DC_STATUS_PROTOCOL;
149159
}
@@ -203,9 +213,8 @@ static dc_status_t
203213
cressi_edy_init3 (cressi_edy_device_t *device)
204214
{
205215
unsigned char command[1] = {0x0C};
206-
unsigned char answer[1] = {0};
207216

208-
return cressi_edy_transfer (device, command, sizeof (command), answer, sizeof (answer), 1);
217+
return cressi_edy_transfer (device, command, sizeof (command), NULL, 0, 1);
209218
}
210219

211220

@@ -324,6 +333,7 @@ cressi_edy_device_close (dc_device_t *abstract)
324333
static dc_status_t
325334
cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
326335
{
336+
dc_status_t status = DC_STATUS_SUCCESS;
327337
cressi_edy_device_t *device = (cressi_edy_device_t*) abstract;
328338

329339
if ((address % SZ_PAGE != 0) ||
@@ -334,22 +344,18 @@ cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned ch
334344
while (nbytes < size) {
335345
// Read the package.
336346
unsigned int number = address / SZ_PAGE;
337-
unsigned char answer[SZ_PACKET + 1] = {0};
338347
unsigned char command[3] = {0x52,
339348
(number >> 8) & 0xFF, // high
340349
(number ) & 0xFF}; // low
341-
dc_status_t rc = cressi_edy_transfer (device, command, sizeof (command), answer, sizeof (answer), 1);
342-
if (rc != DC_STATUS_SUCCESS)
343-
return rc;
344-
345-
memcpy (data, answer, SZ_PACKET);
350+
status = cressi_edy_transfer (device, command, sizeof (command), data + nbytes, SZ_PACKET, 1);
351+
if (status != DC_STATUS_SUCCESS)
352+
return status;
346353

347354
nbytes += SZ_PACKET;
348355
address += SZ_PACKET;
349-
data += SZ_PACKET;
350356
}
351357

352-
return DC_STATUS_SUCCESS;
358+
return status;
353359
}
354360

355361

0 commit comments

Comments
 (0)