From 9c30a7268336f04cc61ecca09c61bdd3fd3670f4 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 16 May 2008 08:30:49 +0000 Subject: [PATCH] Added a public function to control the maximum number of retries. --- reefnet_sensusultra.c | 23 +++++++++++++++++++++++ reefnet_sensusultra.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/reefnet_sensusultra.c b/reefnet_sensusultra.c index 9a69a97..8e70f06 100644 --- a/reefnet_sensusultra.c +++ b/reefnet_sensusultra.c @@ -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) diff --git a/reefnet_sensusultra.h b/reefnet_sensusultra.h index 644e2bb..1784238 100644 --- a/reefnet_sensusultra.h +++ b/reefnet_sensusultra.h @@ -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);