Simplified the error recovery by moving the code to a separate function.
This commit is contained in:
parent
9c30a72683
commit
857cd39c54
@ -1,5 +1,6 @@
|
|||||||
#include <string.h> // memcmp, memcpy
|
#include <string.h> // memcmp, memcpy
|
||||||
#include <stdlib.h> // malloc, free
|
#include <stdlib.h> // malloc, free
|
||||||
|
#include <assert.h> // assert
|
||||||
|
|
||||||
#include "uwatec.h"
|
#include "uwatec.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
@ -157,123 +158,167 @@ serial_write_uwatec (serial *device, const unsigned char data[], unsigned int si
|
|||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
uwatec_memomouse_read_packet_outer (memomouse *device, unsigned char data[], unsigned int size)
|
uwatec_memomouse_confirm (memomouse *device, unsigned char value)
|
||||||
{
|
{
|
||||||
// Initial checksum value.
|
// Send the value to the device.
|
||||||
unsigned char ccrc = 0x00;
|
int rc = serial_write_uwatec (device->port, &value, 1);
|
||||||
|
if (rc != 1) {
|
||||||
|
WARNING ("Failed to send the value.");
|
||||||
|
return EXITCODE (rc, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return UWATEC_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
uwatec_memomouse_read_packet (memomouse *device, unsigned char data[], unsigned int size)
|
||||||
|
{
|
||||||
|
assert (size >= 126 + 2);
|
||||||
|
|
||||||
// Receive the header of the package.
|
// Receive the header of the package.
|
||||||
unsigned char len = 0x00;
|
int rc = serial_read (device->port, data, 1);
|
||||||
int rc = serial_read_uwatec (device->port, &len, 1);
|
if (rc != 1) {
|
||||||
if (rc != 1 || len > size) {
|
WARNING ("Failed to receive the answer.");
|
||||||
|
return EXITCODE (rc, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reverse the bits.
|
||||||
|
uwatec_memomouse_reverse (data, 1);
|
||||||
|
|
||||||
|
// Verify the header of the package.
|
||||||
|
unsigned int len = data[0];
|
||||||
|
if (len > 126) {
|
||||||
WARNING ("Unexpected answer start byte(s).");
|
WARNING ("Unexpected answer start byte(s).");
|
||||||
return EXITCODE (rc, 1);
|
return UWATEC_ERROR_PROTOCOL;
|
||||||
}
|
}
|
||||||
// Update the checksum.
|
|
||||||
ccrc = uwatec_memomouse_checksum (&len, 1, ccrc);
|
|
||||||
|
|
||||||
// Receive the contents of the package.
|
// Receive the remaining part of the package.
|
||||||
rc = serial_read_uwatec (device->port, data, len);
|
rc = serial_read (device->port, data + 1, len + 1);
|
||||||
if (rc != len) {
|
if (rc != len + 1) {
|
||||||
WARNING ("Unexpected EOF in answer.");
|
WARNING ("Failed to receive the answer.");
|
||||||
return EXITCODE (rc, len);
|
return EXITCODE (rc, len + 1);
|
||||||
}
|
}
|
||||||
// Update the checksum.
|
|
||||||
ccrc = uwatec_memomouse_checksum (data, len, ccrc);
|
|
||||||
|
|
||||||
// Receive (and verify) the checksum of the package.
|
// Reverse the bits.
|
||||||
unsigned char crc = 0x00;
|
uwatec_memomouse_reverse (data + 1, len + 1);
|
||||||
rc = serial_read_uwatec (device->port, &crc, 1);
|
|
||||||
if (rc != 1 || ccrc != crc) {
|
// Verify the checksum of the package.
|
||||||
|
unsigned char crc = data[len + 1];
|
||||||
|
unsigned char ccrc = uwatec_memomouse_checksum (data, len + 1, 0x00);
|
||||||
|
if (crc != ccrc) {
|
||||||
WARNING ("Unexpected answer CRC.");
|
WARNING ("Unexpected answer CRC.");
|
||||||
return EXITCODE (rc, 1);
|
return UWATEC_ERROR_PROTOCOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
message ("package(%i)=\"", len);
|
|
||||||
for (unsigned int i = 0; i < len; ++i) {
|
|
||||||
message ("%02x", data[i]);
|
|
||||||
}
|
|
||||||
message ("\"\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
uwatec_memomouse_read_packet_inner (memomouse *device, unsigned char data[], unsigned int msize)
|
uwatec_memomouse_read_packet_outer (memomouse *device, unsigned char data[], unsigned int size)
|
||||||
|
{
|
||||||
|
int rc = 0;
|
||||||
|
unsigned char package[126 + 2] = {0};
|
||||||
|
while ((rc = uwatec_memomouse_read_packet (device, package, sizeof (package))) < 0) {
|
||||||
|
// Automatically discard a corrupted packet,
|
||||||
|
// and request a new one.
|
||||||
|
if (rc != UWATEC_ERROR_PROTOCOL)
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
// Flush the input buffer.
|
||||||
|
serial_flush (device->port, SERIAL_QUEUE_INPUT);
|
||||||
|
|
||||||
|
// Reject the packet.
|
||||||
|
rc = uwatec_memomouse_confirm (device, nak);
|
||||||
|
if (rc != UWATEC_SUCCESS)
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
message ("package(%i)=\"", rc);
|
||||||
|
for (unsigned int i = 0; i < rc; ++i) {
|
||||||
|
message ("%02x", package[i + 1]);
|
||||||
|
}
|
||||||
|
message ("\"\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (size >= rc)
|
||||||
|
memcpy (data, package + 1, rc);
|
||||||
|
else
|
||||||
|
WARNING ("Insufficient buffer space available.");
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
uwatec_memomouse_read_packet_inner (memomouse *device, unsigned char data[], unsigned int size)
|
||||||
{
|
{
|
||||||
// Initial checksum value.
|
// Initial checksum value.
|
||||||
unsigned char ccrc = 0x00;
|
unsigned char ccrc = 0x00;
|
||||||
|
|
||||||
// Read the first package.
|
// Read the first package.
|
||||||
int rc = 0;
|
unsigned char package[126] = {0};
|
||||||
unsigned char package[128 - 2] = {0};
|
int rca = uwatec_memomouse_read_packet_outer (device, package, sizeof (package));
|
||||||
for (;;) {
|
if (rca < 0)
|
||||||
rc = uwatec_memomouse_read_packet_outer (device, package, sizeof (package));
|
return rca;
|
||||||
if (rc < 0) {
|
|
||||||
message ("Didn't got expected packet, sending NAK\n");
|
|
||||||
serial_flush (device->port, SERIAL_QUEUE_INPUT);
|
|
||||||
serial_write_uwatec (device->port, &nak, 1);
|
|
||||||
message ("Package: nbytes=%u, rc=%i\n", 0, rc);
|
|
||||||
} else {
|
|
||||||
message ("Got expected packet, sending ACK\n");
|
|
||||||
serial_write_uwatec (device->port, &ack, 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rc < 2) {
|
// Accept the package.
|
||||||
message ("Package too small.\n");
|
int rcb = uwatec_memomouse_confirm (device, ack);
|
||||||
return UWATEC_ERROR;
|
if (rcb != UWATEC_SUCCESS)
|
||||||
|
return rcb;
|
||||||
|
|
||||||
|
if (rca < 2) {
|
||||||
|
message ("First package is too small.\n");
|
||||||
|
return UWATEC_ERROR_PROTOCOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the total size of the inner package.
|
// Calculate the total size of the inner package.
|
||||||
unsigned int size = package[0] + (package[1] << 8) + 3;
|
unsigned int total = package[0] + (package[1] << 8) + 3;
|
||||||
message ("Package: size=%u\n", size);
|
message ("Package: size=%u\n", total);
|
||||||
|
|
||||||
// Calculate the size of the data in current package
|
// Calculate the size of the data in current package
|
||||||
// (excluding the checksum).
|
// (excluding the checksum).
|
||||||
unsigned int len = (rc >= size ? size - 1 : rc);
|
unsigned int len = (rca >= total ? total - 1 : rca);
|
||||||
message ("Package: nbytes=%u, rc=%i, len=%u\n", 0, rc, len);
|
message ("Package: nbytes=%u, rc=%i, len=%u\n", 0, rca, len);
|
||||||
|
|
||||||
// Update the checksum.
|
// Update the checksum.
|
||||||
ccrc = uwatec_memomouse_checksum (package, len, ccrc);
|
ccrc = uwatec_memomouse_checksum (package, len, ccrc);
|
||||||
|
|
||||||
// Append the data package to the output buffer.
|
// Append the data package to the output buffer.
|
||||||
if (len - 2 <= msize)
|
if (len - 2 <= size)
|
||||||
memcpy (data, package + 2, len - 2);
|
memcpy (data, package + 2, len - 2);
|
||||||
else
|
else
|
||||||
message ("Insufficient buffer space available.\n");
|
message ("Insufficient buffer space available.\n");
|
||||||
|
|
||||||
unsigned int nbytes = rc;
|
unsigned int nbytes = rca;
|
||||||
while (nbytes < size) {
|
while (nbytes < total) {
|
||||||
rc = uwatec_memomouse_read_packet_outer (device, package, sizeof (package));
|
// Read the package.
|
||||||
if (rc < 0) {
|
rca = uwatec_memomouse_read_packet_outer (device, package, sizeof (package));
|
||||||
message ("Didn't got expected packet, sending NAK\n");
|
if (rca < 0)
|
||||||
serial_flush (device->port, SERIAL_QUEUE_INPUT);
|
return rca;
|
||||||
serial_write_uwatec (device->port, &nak, 1);
|
|
||||||
message ("Package: nbytes=%u, rc=%i\n", nbytes, rc);
|
|
||||||
} else {
|
|
||||||
message ("Got expected packet, sending ACK\n");
|
|
||||||
serial_write_uwatec (device->port, &ack, 1);
|
|
||||||
|
|
||||||
// Calculate the size of the data in current package
|
// Accept the package.
|
||||||
// (excluding the checksum).
|
rcb = uwatec_memomouse_confirm (device, ack);
|
||||||
len = (nbytes + rc >= size ? size - nbytes - 1 : rc);
|
if (rcb != UWATEC_SUCCESS)
|
||||||
message ("Package: nbytes=%u, rc=%u, len=%u\n", nbytes, rc, len);
|
return rcb;
|
||||||
|
|
||||||
// Update the checksum.
|
// Calculate the size of the data in current package
|
||||||
ccrc = uwatec_memomouse_checksum (package, len, ccrc);
|
// (excluding the checksum).
|
||||||
|
len = (nbytes + rca >= total ? total - nbytes - 1 : rca);
|
||||||
|
message ("Package: nbytes=%u, rc=%u, len=%u\n", nbytes, rca, len);
|
||||||
|
|
||||||
// Append the data package to the output buffer.
|
// Update the checksum.
|
||||||
if (nbytes + len - 2 <= msize)
|
ccrc = uwatec_memomouse_checksum (package, len, ccrc);
|
||||||
memcpy (data + nbytes - 2, package, len);
|
|
||||||
else
|
|
||||||
message ("Insufficient buffer space available.\n");
|
|
||||||
|
|
||||||
nbytes += rc;
|
// Append the data package to the output buffer.
|
||||||
}
|
if (nbytes + len - 2 <= size)
|
||||||
|
memcpy (data + nbytes - 2, package, len);
|
||||||
|
else
|
||||||
|
message ("Insufficient buffer space available.\n");
|
||||||
|
|
||||||
|
nbytes += rca;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ("Package: nbytes=%i\n", nbytes);
|
message ("Package: nbytes=%i\n", nbytes);
|
||||||
@ -286,6 +331,7 @@ uwatec_memomouse_read_packet_inner (memomouse *device, unsigned char data[], uns
|
|||||||
return UWATEC_SUCCESS;
|
return UWATEC_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
uwatec_memomouse_read (memomouse *device, unsigned char data[], unsigned int size)
|
uwatec_memomouse_read (memomouse *device, unsigned char data[], unsigned int size)
|
||||||
{
|
{
|
||||||
@ -321,7 +367,7 @@ uwatec_memomouse_read (memomouse *device, unsigned char data[], unsigned int siz
|
|||||||
while (answer != ack) {
|
while (answer != ack) {
|
||||||
message ("Sending command\n");
|
message ("Sending command\n");
|
||||||
serial_flush (device->port, SERIAL_QUEUE_INPUT);
|
serial_flush (device->port, SERIAL_QUEUE_INPUT);
|
||||||
rc = serial_write (device->port, command, sizeof (command));
|
rc = serial_write_uwatec (device->port, command, sizeof (command));
|
||||||
if (rc != sizeof (command)) {
|
if (rc != sizeof (command)) {
|
||||||
WARNING ("Failed to send command");
|
WARNING ("Failed to send command");
|
||||||
return EXITCODE (rc, sizeof (command));
|
return EXITCODE (rc, sizeof (command));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user