From a6a300e7c5961c0259e13c6602c20c4863fbd023 Mon Sep 17 00:00:00 2001 From: lwvmobile Date: Mon, 9 Oct 2023 17:35:11 -0400 Subject: [PATCH] P25p1 -- LSD Parity Check; #166 --- include/dsd.h | 2 ++ src/p25_crc.c | 33 ++++++++++++++++++++++++++++ src/p25p1_ldu1.c | 29 ++++++++++++++++--------- src/p25p1_ldu2.c | 56 +++++++++++++++++++++++++++++------------------- 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/include/dsd.h b/include/dsd.h index b3e0b33..d2f2223 100644 --- a/include/dsd.h +++ b/include/dsd.h @@ -983,6 +983,8 @@ void soft_tonef (float samp[160], int n, int ID, int AD); void p25_lcw (dsd_opts * opts, dsd_state * state, uint8_t LCW_bits[], uint8_t irrecoverable_errors); //new p25 1/2 rate decoder int p25_12(uint8_t * input, uint8_t treturn[12]); +//new p25 lsd fec function +int p25p1_lsd_fec(uint8_t * input); void processP25lcw (dsd_opts * opts, dsd_state * state, char *lcformat, char *mfid, char *lcinfo); void processHDU (dsd_opts * opts, dsd_state * state); diff --git a/src/p25_crc.c b/src/p25_crc.c index 9bcad26..adc206c 100644 --- a/src/p25_crc.c +++ b/src/p25_crc.c @@ -133,3 +133,36 @@ int crc12_xb_bridge (int payload[190], int len) err = crc12_ok(buf, len); return (err); } + +uint8_t lsd_parity[256] = { +0x00, 0x39, 0x72, 0x4B, 0xE4, 0xDD, 0x96, 0xAF, 0xF1, 0xC8, 0x83, 0xBA, 0x15, 0x2C, 0x67, 0x5E, +0xDB, 0xE2, 0xA9, 0x90, 0x3F, 0x06, 0x4D, 0x74, 0x2A, 0x13, 0x58, 0x61, 0xCE, 0xF7, 0xBC, 0x85, +0x8F, 0xB6, 0xFD, 0xC4, 0x6B, 0x52, 0x19, 0x20, 0x7E, 0x47, 0x0C, 0x35, 0x9A, 0xA3, 0xE8, 0xD1, +0x54, 0x6D, 0x26, 0x1F, 0xB0, 0x89, 0xC2, 0xFB, 0xA5, 0x9C, 0xD7, 0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0x4F, 0x76, 0xD9, 0xE0, 0xAB, 0x92, 0xCC, 0xF5, 0xBE, 0x87, 0x28, 0x11, 0x5A, 0x63}; + +int p25p1_lsd_fec(uint8_t * input) +{ + uint8_t lsd = (uint8_t)ConvertBitIntoBytes(&input[0], 8); + uint8_t par = (uint8_t)ConvertBitIntoBytes(&input[8], 8); + uint8_t chk = lsd_parity[lsd]; + //NOTE: Simply checking the parity doesn't account for the parity bits being bad, but the + //low speed data bits being correct, but is better than nothing + if (chk == par) return 1; + else + { + //TODO: some FEC functions + return 0; + } +} diff --git a/src/p25p1_ldu1.c b/src/p25p1_ldu1.c index 452912a..e35b2d6 100644 --- a/src/p25p1_ldu1.c +++ b/src/p25p1_ldu1.c @@ -30,8 +30,8 @@ processLDU1 (dsd_opts* opts, dsd_state* state) int i; uint8_t lcformat[9], mfid[9], lcinfo[57]; char lsd1[9], lsd2[9]; - uint8_t lsd_hex1, lsd_hex2; - int status_count; + uint8_t lsd_hex1, lsd_hex2; uint8_t lowspeeddata[32]; memset (lowspeeddata, 0, sizeof(lowspeeddata)); + int status_count; int lsd1_okay, lsd2_okay = 0; char hex_data[12][6]; // Data in hex-words (6 bit words). A total of 12 hex words. char hex_parity[12][6]; // Parity of the data, again in hex-word format. A total of 12 parity hex words. @@ -227,6 +227,8 @@ processLDU1 (dsd_opts* opts, dsd_state* state) lsd_hex1 = lsd_hex1 << 1; lsd1[i] = lsd[i] + '0'; lsd_hex1 |= (uint8_t)lsd[i]; + lowspeeddata[i+0] = (uint8_t)lsd1[i]; + lowspeeddata[i+8] = (uint8_t)cyclic_parity[i]; } for (i=0; i<=6; i+=2) @@ -243,6 +245,8 @@ processLDU1 (dsd_opts* opts, dsd_state* state) lsd_hex2 = lsd_hex2 << 1; lsd2[i] = lsd[i] + '0'; lsd_hex2 |= (uint8_t)lsd[i]; + lowspeeddata[i+16] = (uint8_t)lsd2[i]; + lowspeeddata[i+24] = (uint8_t)cyclic_parity[i]; } // TODO: error correction of the LSD bytes... <--THIS! @@ -449,6 +453,10 @@ processLDU1 (dsd_opts* opts, dsd_state* state) fprintf (stderr, "%s\n", KNRM); } + //WIP: LSD FEC + lsd1_okay = p25p1_lsd_fec (lowspeeddata+0); + lsd2_okay = p25p1_lsd_fec (lowspeeddata+16); + if (opts->payload == 1) { fprintf (stderr, "%s",KCYN); @@ -461,11 +469,12 @@ processLDU1 (dsd_opts* opts, dsd_state* state) //view Low Speed Data fprintf (stderr, "%s",KCYN); fprintf (stderr, " LSD: %02X %02X ", lsd_hex1, lsd_hex2); - if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) ) fprintf (stderr, "(%c", lsd_hex1); + if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) && (lsd1_okay == 1) ) fprintf (stderr, "(%c", lsd_hex1); else fprintf (stderr, "( "); - if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) )fprintf (stderr, "%c)", lsd_hex2); + if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) && (lsd2_okay == 1) ) fprintf (stderr, "%c)", lsd_hex2); else fprintf (stderr, " )"); - + if (lsd1_okay == 0) fprintf (stderr, " L1 ERR"); + if (lsd2_okay == 0) fprintf (stderr, " L2 ERR"); fprintf (stderr, "%s\n", KNRM); } @@ -474,19 +483,19 @@ processLDU1 (dsd_opts* opts, dsd_state* state) if (state->dmr_alias_format[0] == 0x02) { k = state->data_block_counter[0]; - if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) ) + if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) && (lsd1_okay == 1) ) state->dmr_alias_block_segment[0][0][k/4][k%4] = (char)lsd_hex1; - else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; + // else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; k++; - if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) ) + if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) && (lsd2_okay == 1) ) state->dmr_alias_block_segment[0][0][k/4][k%4] = (char)lsd_hex2; - else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; + // else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; k++; state->data_block_counter[0] = k; } //reset format, len, counter. - if (lsd_hex1 == 0x02) + if (lsd_hex1 == 0x02 && lsd1_okay == 1 && lsd2_okay == 1) { state->dmr_alias_format[0] = 0x02; if (lsd_hex2 > 8) lsd_hex2 = 8; //sanity check diff --git a/src/p25p1_ldu2.c b/src/p25p1_ldu2.c index 7d3aaa4..08238f4 100644 --- a/src/p25p1_ldu2.c +++ b/src/p25p1_ldu2.c @@ -32,9 +32,9 @@ processLDU2 (dsd_opts * opts, dsd_state * state) char algid[9], kid[17]; char lsd1[9], lsd2[9]; int algidhex, kidhex; - uint8_t lsd_hex1, lsd_hex2; + uint8_t lsd_hex1, lsd_hex2; uint8_t lowspeeddata[32]; memset (lowspeeddata, 0, sizeof(lowspeeddata)); unsigned long long int mihex1, mihex2, mihex3; - int status_count; + int status_count; int lsd1_okay, lsd2_okay = 0; UNUSED(mihex3); char hex_data[16][6]; // Data in hex-words (6 bit words). A total of 16 hex words. @@ -230,6 +230,8 @@ processLDU2 (dsd_opts * opts, dsd_state * state) lsd_hex1 = lsd_hex1 << 1; lsd1[i] = lsd[i] + '0'; lsd_hex1 |= (uint8_t)lsd[i]; + lowspeeddata[i+0] = (uint8_t)lsd1[i]; + lowspeeddata[i+8] = (uint8_t)cyclic_parity[i];; } for (i=0; i<=6; i+=2) @@ -246,6 +248,8 @@ processLDU2 (dsd_opts * opts, dsd_state * state) lsd_hex2 = lsd_hex2 << 1; lsd2[i] = lsd[i] + '0'; lsd_hex2 |= (uint8_t)lsd[i]; + lowspeeddata[i+16] = (uint8_t)lsd2[i]; + lowspeeddata[i+24] = (uint8_t)cyclic_parity[i]; } // TODO: error correction of the LSD bytes... <--THIS! @@ -455,6 +459,10 @@ processLDU2 (dsd_opts * opts, dsd_state * state) mihex2 = (unsigned long long int)ConvertBitIntoBytes(&mi[32], 32); mihex3 = (unsigned long long int)ConvertBitIntoBytes(&mi[64], 8); + //WIP: LSD FEC + lsd1_okay = p25p1_lsd_fec (lowspeeddata+0); + lsd2_okay = p25p1_lsd_fec (lowspeeddata+16); + if (irrecoverable_errors == 0) { fprintf (stderr, "%s", KYEL); @@ -481,46 +489,50 @@ processLDU2 (dsd_opts * opts, dsd_state * state) fprintf (stderr, " ENC"); fprintf (stderr, "%s", KNRM); } - - if (opts->payload == 1) - { - //view Low Speed Data - fprintf (stderr, "%s",KCYN); - fprintf (stderr, " LSD: %02X %02X ", lsd_hex1, lsd_hex2); - if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) ) fprintf (stderr, "(%c", lsd_hex1); - else fprintf (stderr, "( "); - if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) )fprintf (stderr, "%c)", lsd_hex2); - else fprintf (stderr, " )"); - fprintf (stderr, "%s",KNRM); - } - - fprintf (stderr, "\n"); } else { fprintf (stderr, "%s", KRED); - fprintf (stderr, " LDU2 FEC ERR \n"); + fprintf (stderr, " LDU2 FEC ERR "); fprintf (stderr, "%s", KNRM); } + + + if (opts->payload == 1) + { + //view Low Speed Data + fprintf (stderr, "%s",KCYN); + fprintf (stderr, " LSD: %02X %02X ", lsd_hex1, lsd_hex2); + if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) && (lsd1_okay == 1) ) fprintf (stderr, "(%c", lsd_hex1); + else fprintf (stderr, "( "); + if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) && (lsd2_okay == 1) ) fprintf (stderr, "%c)", lsd_hex2); + else fprintf (stderr, " )"); + if (lsd1_okay == 0) fprintf (stderr, " L1 ERR"); + if (lsd2_okay == 0) fprintf (stderr, " L2 ERR"); + fprintf (stderr, "%s", KNRM); + } + + fprintf (stderr, "\n", KNRM); + //TEST: Store LSD into array if 0x02 0x08 (opcode and len?) int k = 0; if (state->dmr_alias_format[0] == 0x02) { k = state->data_block_counter[0]; - if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) ) + if ( (lsd_hex1 > 0x19) && (lsd_hex1 < 0x7F) && (lsd1_okay == 1) ) state->dmr_alias_block_segment[0][0][k/4][k%4] = (char)lsd_hex1; - else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; + // else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; k++; - if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) ) + if ( (lsd_hex2 > 0x19) && (lsd_hex2 < 0x7F) && (lsd2_okay == 1) ) state->dmr_alias_block_segment[0][0][k/4][k%4] = (char)lsd_hex2; - else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; + // else state->dmr_alias_block_segment[0][0][k/4][k%4] = 0x20; k++; state->data_block_counter[0] = k; } //reset format, len, counter. - if (lsd_hex1 == 0x02) + if (lsd_hex1 == 0x02 && lsd1_okay == 1 && lsd2_okay == 1) { state->dmr_alias_format[0] = 0x02; if (lsd_hex2 > 8) lsd_hex2 = 8; //sanity check