Added a public function to control the maximum number of retries.
This commit is contained in:
parent
21ecc37946
commit
9c30a72683
@ -22,6 +22,7 @@
|
||||
|
||||
struct sensusultra {
|
||||
struct serial *port;
|
||||
unsigned int maxretries;
|
||||
};
|
||||
|
||||
|
||||
@ -40,6 +41,7 @@ reefnet_sensusultra_open (sensusultra **out, const char* name)
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->maxretries = 2;
|
||||
|
||||
// Open the device.
|
||||
int rc = serial_open (&device->port, name);
|
||||
@ -94,6 +96,17 @@ reefnet_sensusultra_close (sensusultra *device)
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
reefnet_sensusultra_set_maxretries (sensusultra *device, unsigned int maxretries)
|
||||
{
|
||||
if (device == NULL)
|
||||
return REEFNET_ERROR;
|
||||
|
||||
device->maxretries = maxretries;
|
||||
|
||||
return REEFNET_SUCCESS;
|
||||
}
|
||||
|
||||
static unsigned short
|
||||
reefnet_sensusultra_checksum (const unsigned char *data, unsigned int size)
|
||||
{
|
||||
@ -221,6 +234,7 @@ reefnet_sensusultra_handshake (sensusultra *device, unsigned char *data, unsigne
|
||||
serial_flush (device->port, SERIAL_QUEUE_BOTH);
|
||||
|
||||
int rc = 0;
|
||||
unsigned int nretries = 0;
|
||||
unsigned char handshake[REEFNET_SENSUSULTRA_HANDSHAKE_SIZE + 2] = {0};
|
||||
while ((rc = reefnet_sensusultra_packet (device, handshake, sizeof (handshake), 0)) != REEFNET_SUCCESS) {
|
||||
// Automatically discard a corrupted handshake packet,
|
||||
@ -228,6 +242,10 @@ reefnet_sensusultra_handshake (sensusultra *device, unsigned char *data, unsigne
|
||||
if (rc != REEFNET_ERROR_PROTOCOL)
|
||||
return rc;
|
||||
|
||||
// Abort if the maximum number of retries is reached.
|
||||
if (nretries++ >= device->maxretries)
|
||||
return rc;
|
||||
|
||||
// According to the developers guide, a 250 ms delay is suggested to
|
||||
// guarantee that the prompt byte sent after the handshake packet is
|
||||
// not accidentally buffered by the host and (mis)interpreted as part
|
||||
@ -277,6 +295,7 @@ reefnet_sensusultra_page (sensusultra *device, unsigned char *data, unsigned int
|
||||
return REEFNET_ERROR;
|
||||
|
||||
int rc = 0;
|
||||
unsigned int nretries = 0;
|
||||
unsigned char package[REEFNET_SENSUSULTRA_PACKET_SIZE + 4] = {0};
|
||||
while ((rc = reefnet_sensusultra_packet (device, package, sizeof (package), 2)) != REEFNET_SUCCESS) {
|
||||
// Automatically discard a corrupted packet,
|
||||
@ -284,6 +303,10 @@ reefnet_sensusultra_page (sensusultra *device, unsigned char *data, unsigned int
|
||||
if (rc != REEFNET_ERROR_PROTOCOL)
|
||||
return rc;
|
||||
|
||||
// Abort if the maximum number of retries is reached.
|
||||
if (nretries++ >= device->maxretries)
|
||||
return rc;
|
||||
|
||||
// Reject the packet.
|
||||
rc = reefnet_sensusultra_send_uchar (device, REJECT);
|
||||
if (rc != REEFNET_SUCCESS)
|
||||
|
||||
@ -18,6 +18,8 @@ int reefnet_sensusultra_open (sensusultra **device, const char* name);
|
||||
|
||||
int reefnet_sensusultra_close (sensusultra *device);
|
||||
|
||||
int reefnet_sensusultra_set_maxretries (sensusultra *device, unsigned int maxretries);
|
||||
|
||||
int reefnet_sensusultra_handshake (sensusultra *device, unsigned char *data, unsigned int size);
|
||||
|
||||
int reefnet_sensusultra_read_data (sensusultra *device, unsigned char *data, unsigned int size);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user