From e6a3cfbe420c7cbd3b046d6929bf2773b5e054af Mon Sep 17 00:00:00 2001 From: Philip Heron Date: Mon, 4 Apr 2016 00:28:25 +0100 Subject: [PATCH] Bypass the FEC if the packet has a valid CRC --- ssdv.c | 68 +++++++++++++++++++++++++++++++++++++++++++++------------- ssdv.h | 5 +++-- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/ssdv.c b/ssdv.c index 6c33d72..726f5d3 100644 --- a/ssdv.c +++ b/ssdv.c @@ -1300,19 +1300,50 @@ char ssdv_dec_is_packet(uint8_t *packet, int *errors) memcpy(pkt, packet, SSDV_PKT_SIZE); pkt[0] = 0x55; + type = SSDV_TYPE_INVALID; + if(pkt[1] == 0x66 + SSDV_TYPE_NOFEC) { - type = SSDV_TYPE_NOFEC; - + /* Test for a valid NOFEC packet */ pkt_size_payload = SSDV_PKT_SIZE - SSDV_PKT_SIZE_HEADER - SSDV_PKT_SIZE_CRC; pkt_size_crcdata = SSDV_PKT_SIZE_HEADER + pkt_size_payload - 1; + /* No FEC scan */ if(errors) *errors = 0; - } - else - { - type = SSDV_TYPE_NORMAL; + /* Test the checksum */ + x = crc32(&pkt[1], pkt_size_crcdata); + + i = 1 + pkt_size_crcdata; + if(x == (pkt[i + 3] | (pkt[i + 2] << 8) | (pkt[i + 1] << 16) | (pkt[i] << 24))) + { + /* Valid, set the type and continue */ + type = SSDV_TYPE_NOFEC; + } + } + else if(pkt[1] == 0x66 + SSDV_TYPE_NORMAL) + { + /* Test for a valid NORMAL packet */ + pkt_size_payload = SSDV_PKT_SIZE - SSDV_PKT_SIZE_HEADER - SSDV_PKT_SIZE_CRC - SSDV_PKT_SIZE_RSCODES; + pkt_size_crcdata = SSDV_PKT_SIZE_HEADER + pkt_size_payload - 1; + + /* No FEC scan */ + if(errors) *errors = 0; + + /* Test the checksum */ + x = crc32(&pkt[1], pkt_size_crcdata); + + i = 1 + pkt_size_crcdata; + if(x == (pkt[i + 3] | (pkt[i + 2] << 8) | (pkt[i + 1] << 16) | (pkt[i] << 24))) + { + /* Valid, set the type and continue */ + type = SSDV_TYPE_NORMAL; + } + } + + if(type == SSDV_TYPE_INVALID) + { + /* Test for a valid NORMAL packet with correctable errors */ pkt_size_payload = SSDV_PKT_SIZE - SSDV_PKT_SIZE_HEADER - SSDV_PKT_SIZE_CRC - SSDV_PKT_SIZE_RSCODES; pkt_size_crcdata = SSDV_PKT_SIZE_HEADER + pkt_size_payload - 1; @@ -1322,6 +1353,22 @@ char ssdv_dec_is_packet(uint8_t *packet, int *errors) if(i < 0) return(-1); /* Reed-solomon decoder failed */ if(errors) *errors = i; + + /* Test the checksum */ + x = crc32(&pkt[1], pkt_size_crcdata); + + i = 1 + pkt_size_crcdata; + if(x == (pkt[i + 3] | (pkt[i + 2] << 8) | (pkt[i + 1] << 16) | (pkt[i] << 24))) + { + /* Valid, set the type and continue */ + type = SSDV_TYPE_NORMAL; + } + } + + if(type == SSDV_TYPE_INVALID) + { + /* All attempts to read the packet have failed */ + return(-1); } /* Sanity checks */ @@ -1335,15 +1382,6 @@ char ssdv_dec_is_packet(uint8_t *packet, int *errors) if(p.mcu_offset >= pkt_size_payload) return(-1); } - /* Test the checksum */ - x = crc32(&pkt[1], pkt_size_crcdata); - - i = 1 + pkt_size_crcdata; - if(pkt[i++] != ((x >> 24) & 0xFF)) return(-1); - if(pkt[i++] != ((x >> 16) & 0xFF)) return(-1); - if(pkt[i++] != ((x >> 8) & 0xFF)) return(-1); - if(pkt[i++] != (x & 0xFF)) return(-1); - /* Appears to be a valid packet! Copy it back */ memcpy(packet, pkt, SSDV_PKT_SIZE); diff --git a/ssdv.h b/ssdv.h index 8a1f4f8..70f42bf 100644 --- a/ssdv.h +++ b/ssdv.h @@ -42,8 +42,9 @@ extern "C" { #define SSDV_MAX_CALLSIGN (6) /* Maximum number of characters in a callsign */ -#define SSDV_TYPE_NORMAL (0) -#define SSDV_TYPE_NOFEC (1) +#define SSDV_TYPE_INVALID (0xFF) +#define SSDV_TYPE_NORMAL (0x00) +#define SSDV_TYPE_NOFEC (0x01) typedef struct {