diff --git a/src/dmr_dburst.c b/src/dmr_dburst.c index eeb9aa8..742b204 100644 --- a/src/dmr_dburst.c +++ b/src/dmr_dburst.c @@ -14,6 +14,24 @@ #include "dsd.h" +uint32_t dmr_flc_rs_calc(uint8_t * input) +{ + uint8_t tmp_bytes[12]; + memset(tmp_bytes, 0, sizeof(tmp_bytes)); + memcpy (tmp_bytes, input, 9); + + uint32_t rs_clc = 0; + rs_12_9_encode(tmp_bytes); + + rs_clc |= tmp_bytes[9]; + rs_clc <<= 8; + rs_clc |= tmp_bytes[10]; + rs_clc <<= 8; + rs_clc |= tmp_bytes[11]; + + return rs_clc; +} + void dmr_data_burst_handler(dsd_opts * opts, dsd_state * state, uint8_t info[196], uint8_t databurst) { @@ -304,20 +322,21 @@ void dmr_data_burst_handler(dsd_opts * opts, dsd_state * state, uint8_t info[196 /* Apply the CRC mask (see DMR standard B.3.12 Data Type CRC Mask) */ CRCExtracted = CRCExtracted ^ crcmask; - /* Check/correct the BPTC data and compute the Reed-Solomon (12,9) CRC */ + /* Check the 9 byte Full Link Control for Correct RS (or RAS MAC) Encoding*/ if (is_lc) { if (is_ras) { - uint8_t tmp_bytes[12]; - uint32_t std_crc = 0; - memcpy (tmp_bytes, BPTCDmrDataByte, sizeof(tmp_bytes)); - if (ComputeAndCorrectFullLinkControlCrc(tmp_bytes, &std_crc, crcmask) && std_crc == CRCExtracted) + uint32_t std_crc = dmr_flc_rs_calc(BPTCDmrDataByte); + if (std_crc == CRCExtracted) { - memcpy (BPTCDmrDataByte, tmp_bytes, sizeof(tmp_bytes)); - is_ras = 0; CRCComputed = std_crc; CRCCorrect = 1; + is_ras = 0; + + #ifdef DEBUG_RAS + fprintf (stderr, " EXT: %06X; CRC: %06X;", CRCExtracted, CRCComputed); + #endif } else { @@ -330,7 +349,19 @@ void dmr_data_burst_handler(dsd_opts * opts, dsd_state * state, uint8_t info[196 #endif } } - else CRCCorrect = ComputeAndCorrectFullLinkControlCrc(BPTCDmrDataByte, &CRCComputed, crcmask); + else + { + CRCComputed = dmr_flc_rs_calc(BPTCDmrDataByte); + if (CRCComputed == CRCExtracted) + CRCCorrect = 1; + + /* Check/correct the BPTC data and compute the Reed-Solomon (12,9) CRC */ + else CRCCorrect = ComputeAndCorrectFullLinkControlCrc(BPTCDmrDataByte, &CRCComputed, crcmask); + + #ifdef DEBUG_RAS + fprintf (stderr, " EXT: %06X; CRC: %06X;", CRCExtracted, CRCComputed); + #endif + } //Hytera OTA Encryption LC Context Creation for VLC Header if (state->hota_key_loaded && CRCCorrect && databurst == 0x01) diff --git a/src/dmr_flco.c b/src/dmr_flco.c index 3ed8b31..bbf14ee 100644 --- a/src/dmr_flco.c +++ b/src/dmr_flco.c @@ -629,7 +629,7 @@ void dmr_flco (dsd_opts * opts, dsd_state * state, uint8_t lc_bits[], uint32_t C state->gi[slot] = 1; } } - else if (flco == 0x3 || flco == 0x23) //UU_V_Ch_Usr + else if (flco == 0x3 || flco == 0x13 || flco == 0x23) //UU_V_Ch_Usr { sprintf (state->call_string[slot], " Private "); fprintf (stderr, "Private "); diff --git a/src/dmr_utils.c b/src/dmr_utils.c index 5fa9083..cd6965f 100644 --- a/src/dmr_utils.c +++ b/src/dmr_utils.c @@ -236,10 +236,11 @@ uint32_t ComputeAndCorrectFullLinkControlCrc(uint8_t * FullLinkControlDataBytes, rs_12_9_calc_syndrome(&VoiceLCHeaderStr, &syndrome); if(rs_12_9_check_syndrome(&syndrome) != 0) result = rs_12_9_correct_errors(&VoiceLCHeaderStr, &syndrome, &errors_found); - /* Reconstitue the CRC */ - *CRCComputed = (uint32_t)((VoiceLCHeaderStr.data[9] << 16) & 0xFF0000); - *CRCComputed |= (uint32_t)((VoiceLCHeaderStr.data[10] << 8) & 0x00FF00); - *CRCComputed |= (uint32_t)((VoiceLCHeaderStr.data[11] << 0) & 0x0000FF); + /* Compute the expected 24-bit RS(12,9) parity from the 9 LC data bytes */ + rs_12_9_checksum_t * cs = rs_12_9_calc_checksum(&VoiceLCHeaderStr); + *CRCComputed = (uint32_t)((cs->bytes[0] << 16) & 0xFF0000); + *CRCComputed |= (uint32_t)((cs->bytes[1] << 8) & 0x00FF00); + *CRCComputed |= (uint32_t)((cs->bytes[2] << 0) & 0x0000FF); if((result == RS_12_9_CORRECT_ERRORS_RESULT_NO_ERRORS_FOUND) || (result == RS_12_9_CORRECT_ERRORS_RESULT_ERRORS_CORRECTED)) diff --git a/src/rs-12-9.c b/src/rs-12-9.c index 1d8edb2..f6e82c6 100644 --- a/src/rs-12-9.c +++ b/src/rs-12-9.c @@ -254,16 +254,17 @@ rs_12_9_correct_errors_result_t rs_12_9_correct_errors(rs_12_9_codeword_t *codew rs_12_9_roots_t *roots; uint8_t num, denom; + if (rs_12_9_check_syndrome(syndrome) == 0) { + *errors_found = 0; + return RS_12_9_CORRECT_ERRORS_RESULT_NO_ERRORS_FOUND; + } + rs_12_9_calculate(syndrome, &error_locator_poly, &error_evaluator_poly); roots = rs_12_9_find_roots(&error_locator_poly); *errors_found = roots->errors_num; - if (roots->errors_num == 0) - return RS_12_9_CORRECT_ERRORS_RESULT_NO_ERRORS_FOUND; - - // Error correction is done using the error-evaluator equation on pp 207. - if (roots->errors_num > 0 && roots->errors_num <= RS_12_9_CHECKSUMSIZE) { - // First check for illegal error locations. + // With 3 parity bytes (d_min = 4), RS(12,9) can only uniquely correct t = 1 byte error. + if (roots->errors_num == 1) { for (r = 0; r < roots->errors_num; r++) { if (roots->error_locations[r] >= RS_12_9_DATASIZE+RS_12_9_CHECKSUMSIZE) return RS_12_9_CORRECT_ERRORS_RESULT_ERRORS_CANT_BE_CORRECTED; @@ -284,10 +285,20 @@ rs_12_9_correct_errors_result_t rs_12_9_correct_errors(rs_12_9_codeword_t *codew for (j = 1; j < RS_12_9_POLY_MAXDEG; j += 2) denom ^= rs_12_9_galois_multiplication(error_locator_poly.data[j], rs_12_9_galois_exp_table_get(((255-i)*(j-1)) % 255)); + if (denom == 0) + return RS_12_9_CORRECT_ERRORS_RESULT_ERRORS_CANT_BE_CORRECTED; + err = rs_12_9_galois_multiplication(num, rs_12_9_galois_inv(denom)); //console_log(LOGLEVEL_CODING LOGLEVEL_DEBUG " rs (12,9): error magnitude %#x at byte loc. %u\n", err, sizeof(rs_12_9_codeword_t)-i); codeword->data[sizeof(rs_12_9_codeword_t)-i-1] ^= err; + + rs_12_9_poly_t post_syndrome; + rs_12_9_calc_syndrome(codeword, &post_syndrome); + if (rs_12_9_check_syndrome(&post_syndrome) != 0) { + codeword->data[sizeof(rs_12_9_codeword_t)-i-1] ^= err; + return RS_12_9_CORRECT_ERRORS_RESULT_ERRORS_CANT_BE_CORRECTED; + } } return RS_12_9_CORRECT_ERRORS_RESULT_ERRORS_CORRECTED; }