|
| 1 | +package direwolf |
| 2 | + |
| 3 | +/*------------------------------------------------------------- |
| 4 | + * |
| 5 | + * Purpose: IL2P Trailing CRC-16-CCITT protected by (7,4) Hamming encoding. |
| 6 | + * |
| 7 | + * The CRC provides a final validity check after RS FEC decoding, |
| 8 | + * catching rare cases where RS decoding silently produces |
| 9 | + * incorrect data under extreme error conditions. |
| 10 | + * |
| 11 | + * Reference: IL2P specification v0.6 |
| 12 | + * |
| 13 | + *--------------------------------------------------------------*/ |
| 14 | + |
| 15 | +// Hamming (7,4) encode table from the IL2P spec. |
| 16 | +// Maps 4-bit data nibble to 7-bit Hamming codeword. |
| 17 | +var il2p_hamming_encode = [16]byte{ |
| 18 | + 0x00, 0x71, 0x62, 0x13, 0x54, 0x25, 0x36, 0x47, |
| 19 | + 0x38, 0x49, 0x5a, 0x2b, 0x6c, 0x1d, 0x0e, 0x7f, |
| 20 | +} |
| 21 | + |
| 22 | +// Hamming (7,4) decode table from the IL2P spec. |
| 23 | +// Maps 7-bit received codeword to 4-bit data nibble. |
| 24 | +// Provides single-bit error correction. |
| 25 | +var il2p_hamming_decode = [128]byte{ |
| 26 | + 0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x0e, 0x07, |
| 27 | + 0x00, 0x09, 0x0e, 0x0b, 0x0e, 0x0d, 0x0e, 0x0e, |
| 28 | + 0x00, 0x03, 0x03, 0x03, 0x04, 0x0d, 0x06, 0x03, |
| 29 | + 0x08, 0x0d, 0x0a, 0x03, 0x0d, 0x0d, 0x0e, 0x0d, |
| 30 | + 0x00, 0x05, 0x02, 0x0b, 0x05, 0x05, 0x06, 0x05, |
| 31 | + 0x08, 0x0b, 0x0b, 0x0b, 0x0c, 0x05, 0x0e, 0x0b, |
| 32 | + 0x08, 0x01, 0x06, 0x03, 0x06, 0x05, 0x06, 0x06, |
| 33 | + 0x08, 0x08, 0x08, 0x0b, 0x08, 0x0d, 0x06, 0x0f, |
| 34 | + 0x00, 0x09, 0x02, 0x07, 0x04, 0x07, 0x07, 0x07, |
| 35 | + 0x09, 0x09, 0x0a, 0x09, 0x0c, 0x09, 0x0e, 0x07, |
| 36 | + 0x04, 0x01, 0x0a, 0x03, 0x04, 0x04, 0x04, 0x07, |
| 37 | + 0x0a, 0x09, 0x0a, 0x0a, 0x04, 0x0d, 0x0a, 0x0f, |
| 38 | + 0x02, 0x01, 0x02, 0x02, 0x0c, 0x05, 0x02, 0x07, |
| 39 | + 0x0c, 0x09, 0x02, 0x0b, 0x0c, 0x0c, 0x0c, 0x0f, |
| 40 | + 0x01, 0x01, 0x02, 0x01, 0x04, 0x01, 0x06, 0x0f, |
| 41 | + 0x08, 0x01, 0x0a, 0x0f, 0x0c, 0x0f, 0x0f, 0x0f, |
| 42 | +} |
| 43 | + |
| 44 | +/*------------------------------------------------------------- |
| 45 | + * |
| 46 | + * Name: il2p_crc_calc |
| 47 | + * |
| 48 | + * Purpose: Compute CRC-16-CCITT over AX.25 frame data. |
| 49 | + * |
| 50 | + * Inputs: data - AX.25 frame bytes (without AX.25 FCS). |
| 51 | + * |
| 52 | + * Returns: 16-bit CRC value. |
| 53 | + * |
| 54 | + *--------------------------------------------------------------*/ |
| 55 | + |
| 56 | +func il2p_crc_calc(data []byte) uint16 { |
| 57 | + return fcs_calc(data) |
| 58 | +} |
| 59 | + |
| 60 | +/*------------------------------------------------------------- |
| 61 | + * |
| 62 | + * Name: il2p_crc_encode |
| 63 | + * |
| 64 | + * Purpose: Hamming-encode a 16-bit CRC into 4 bytes. |
| 65 | + * |
| 66 | + * Inputs: crc - 16-bit CRC value. |
| 67 | + * |
| 68 | + * Returns: 4 bytes, each containing a Hamming (7,4) encoded nibble. |
| 69 | + * High nibble of CRC is encoded first. |
| 70 | + * |
| 71 | + *--------------------------------------------------------------*/ |
| 72 | + |
| 73 | +func il2p_crc_encode(crc uint16) [IL2P_CRC_ENCODED_SIZE]byte { |
| 74 | + var encoded [IL2P_CRC_ENCODED_SIZE]byte |
| 75 | + encoded[0] = il2p_hamming_encode[(crc>>12)&0x0f] |
| 76 | + encoded[1] = il2p_hamming_encode[(crc>>8)&0x0f] |
| 77 | + encoded[2] = il2p_hamming_encode[(crc>>4)&0x0f] |
| 78 | + encoded[3] = il2p_hamming_encode[crc&0x0f] |
| 79 | + return encoded |
| 80 | +} |
| 81 | + |
| 82 | +/*------------------------------------------------------------- |
| 83 | + * |
| 84 | + * Name: il2p_crc_decode |
| 85 | + * |
| 86 | + * Purpose: Decode 4 Hamming-encoded bytes back to a 16-bit CRC. |
| 87 | + * |
| 88 | + * Inputs: encoded - 4 bytes of Hamming (7,4) encoded CRC. |
| 89 | + * |
| 90 | + * Returns: 16-bit CRC value. |
| 91 | + * |
| 92 | + *--------------------------------------------------------------*/ |
| 93 | + |
| 94 | +func il2p_crc_decode(encoded []byte) uint16 { |
| 95 | + var n0 = uint16(il2p_hamming_decode[encoded[0]&0x7f]) |
| 96 | + var n1 = uint16(il2p_hamming_decode[encoded[1]&0x7f]) |
| 97 | + var n2 = uint16(il2p_hamming_decode[encoded[2]&0x7f]) |
| 98 | + var n3 = uint16(il2p_hamming_decode[encoded[3]&0x7f]) |
| 99 | + return (n0 << 12) | (n1 << 8) | (n2 << 4) | n3 |
| 100 | +} |
| 101 | + |
| 102 | +/*------------------------------------------------------------- |
| 103 | + * |
| 104 | + * Name: il2p_crc_check |
| 105 | + * |
| 106 | + * Purpose: Validate received Hamming-encoded CRC against AX.25 frame data. |
| 107 | + * |
| 108 | + * Inputs: frame_data - Decoded AX.25 frame bytes. |
| 109 | + * encoded_crc - 4 bytes of received Hamming-encoded CRC. |
| 110 | + * |
| 111 | + * Returns: true if CRC matches, false otherwise. |
| 112 | + * |
| 113 | + *--------------------------------------------------------------*/ |
| 114 | + |
| 115 | +func il2p_crc_check(frame_data []byte, encoded_crc []byte) bool { |
| 116 | + var expected = il2p_crc_calc(frame_data) |
| 117 | + var received = il2p_crc_decode(encoded_crc) |
| 118 | + return expected == received |
| 119 | +} |
| 120 | + |
| 121 | +/*------------------------------------------------------------- |
| 122 | + * |
| 123 | + * Name: il2p_crc_enabled |
| 124 | + * |
| 125 | + * Purpose: Check if IL2P trailing CRC is enabled for a channel. |
| 126 | + * |
| 127 | + * Inputs: channel - Radio channel number. |
| 128 | + * |
| 129 | + * Returns: true if CRC is enabled, false otherwise. |
| 130 | + * |
| 131 | + *--------------------------------------------------------------*/ |
| 132 | + |
| 133 | +func il2p_crc_enabled(channel int) bool { |
| 134 | + if save_audio_config_p == nil { |
| 135 | + return true // Default to enabled if no config available. |
| 136 | + } |
| 137 | + return save_audio_config_p.achan[channel].il2p_crc != 0 |
| 138 | +} |
0 commit comments