From 9b6e1a1984c48f935c9eceb365bc2f0701d68f4e Mon Sep 17 00:00:00 2001 From: Cort Buffington Date: Sun, 23 Oct 2016 11:07:16 -0500 Subject: [PATCH] Encoding Progress --- constants.py | 10 +++--- dec_dmr.py | 3 +- enc_dmr.py | 32 ++++++++++++++++++ hb_router.py | 46 ++++++++++++++++--------- qr.py | 94 ---------------------------------------------------- 5 files changed, 71 insertions(+), 114 deletions(-) create mode 100755 enc_dmr.py delete mode 100755 qr.py diff --git a/constants.py b/constants.py index 6a5c270..db34de4 100644 --- a/constants.py +++ b/constants.py @@ -6,10 +6,12 @@ BS_DATA_SYNC = bitarray() BS_VOICE_SYNC.frombytes(b'\x75\x5F\xD7\xDF\x75\xF7') BS_DATA_SYNC.frombytes(b'\xDF\xF5\x7D\x75\xDF\x5D') -LCSS_SINGLE_FRAG = bitarray('00') -LCSS_FIRST_FRAG = bitarray('01') -LCSS_LAST_FRAG = bitarray('10') -LCSS_CONT_FRAG = bitarray('11') +# Precomputed EMB values, where CC always = 1, and PI always = 0 +BURST_B_EMB = bitarray('0001001110010001') +BURST_C_EMB = bitarray('0001011101110100') +BURST_D_EMB = bitarray('0001011101110100') +BURST_E_EMB = bitarray('0001010100000111') +BURST_F_EMB = bitarray('0001000111100010') ''' EMB: CC(4b), PI(1b), LCSS(2b), EMB Parity(9b - QR 16,7,5) diff --git a/dec_dmr.py b/dec_dmr.py index 182b6bc..d5711c4 100755 --- a/dec_dmr.py +++ b/dec_dmr.py @@ -30,10 +30,11 @@ def voice_head_term(_string): burst = to_bits(_string) info = burst[0:98] + burst[166:264] slot_type = burst[98:108] + burst[156:166] + sync = burst[108:156] lc = bptc.decode_full_lc(info).tobytes() cc = to_bytes(slot_type[0:4]) dtype = to_bytes(slot_type[4:8]) - return (lc, cc, dtype) + return (lc, cc, dtype, sync) def voice_sync(_string): diff --git a/enc_dmr.py b/enc_dmr.py new file mode 100755 index 0000000..07b4cad --- /dev/null +++ b/enc_dmr.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# +# This work is licensed under the Creative Attribution-NonCommercial-ShareAlike +# 3.0 Unported License.To view a copy of this license, visit +# http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to +# Creative Commons, 444 Castro Street, Suite 900, Mountain View, +# California, 94041, USA. + +from __future__ import print_function + +from bitarray import bitarray +import constants + +# Does anybody read this stuff? There's a PEP somewhere that says I should do this. +__author__ = 'Cortney T. Buffington, N0MJS' +__copyright__ = 'Copyright (c) 2016 Cortney T. Buffington, N0MJS and the K0USY Group' +__credits__ = 'Jonathan Naylor, G4KLX' +__license__ = 'Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported' +__maintainer__ = 'Cort Buffington, N0MJS' +__email__ = 'n0mjs@me.com' + + +#------------------------------------------------------------------------------ +# Used to execute the module directly to run built-in tests +#------------------------------------------------------------------------------ + +if __name__ == '__main__': + + from binascii import b2a_hex as h + from time import time + + print(ENC_EMB) \ No newline at end of file diff --git a/hb_router.py b/hb_router.py index c728d32..ce75af4 100755 --- a/hb_router.py +++ b/hb_router.py @@ -23,7 +23,7 @@ from twisted.internet import task # Things we import from the main hblink module from hblink import CONFIG, HBMASTER, HBCLIENT, logger, systems, hex_str_3, int_id -import dmr_decon, crc +import dec_dmr # Import Bridging rules # Note: A stanza *must* exist for any MASTER or CLIENT configured in the main @@ -72,26 +72,42 @@ class routerMASTER(HBMASTER): def __init__(self, *args, **kwargs): HBMASTER.__init__(self, *args, **kwargs) - self.embeddec_lc_rx = {'B': '', 'C': '', 'D': '', 'E': '', 'F': ''} - self.embeddec_lc_tx = {'B': '', 'C': '', 'D': '', 'E': '', 'F': ''} + self._last_stream_id + self.embedded_lc_rx = [0,0,0,0] + self.embedded_lc_tx = [0,0,0,0] + self.embedded_lc = '' + self.lc_index = 0 def dmrd_received(self, _radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data): _bits = int_id(_data[15]) if _call_type == 'group': if _frame_type == 'data_sync': - lc = dmr_decon.voice_head_term(_data[20:53])[0] - print(h(lc)) - if _dtype_vseq == 1: - lc_bits = bitarray() - lc_bits.frombytes(lc) - embedded_lc = lc_bits + crc.csum5(lc) - print('SHIT IS TEH SHIT:', lc_bits) - print('THIS IS THE SHIT:', embedded_lc) - elif _frame_type == 'voice_sync': - print(dmr_decon.voice_sync(_data[20:53])) - elif _frame_type == 'voice': - print(dmr_decon.voice(_data[20:53])) + lc = dec_dmr.voice_head_term(_data[20:53]) + if lc[2] == '\x01': + print('Voice Header: LC: {}, CC: {}, DTYPE: {}, SYNC: {}'.format(h(lc[0]), h(lc[1]), h(lc[2]), h(lc[3]))) + if lc[2] == '\x02': + print('Voice Terminator: LC: {}, CC: {}, DTYPE: {}, SYNC: {}'.format(h(lc[0]), h(lc[1]), h(lc[2]), h(lc[3]))) + + if _frame_type == 'voice_sync': + lc = dec_dmr.voice_sync(_data[20:53]) + print('Voice Burst A: SYNC: {}'.format(h(lc[1]))) + + if _frame_type == 'voice': + lc = dec_dmr.voice(_data[20:53]) + if lc[2] == '\x01': + self.lc_index = 0 + self.embedded_lc_rx[self.lc_index] = lc[3] + elif lc[2] == '\x03': + self.lc_index += 1 + self.embedded_lc_rx[self.lc_index] = lc[3] + elif lc[2] == '\x02': + self.lc_index += 1 + if self.lc_index == 3: + self.embedded_lc_rx[self.lc_index] = lc[3] + self.embedded_lc = dec_dmr.bptc.decode_emblc(self.embedded_lc_rx[0] + self.embedded_lc_rx[1] + self.embedded_lc_rx[2] + self.embedded_lc_rx[3]) + print('Emedded LC Completed: {}'.format(h(self.embedded_lc))) + print('Voice Burst B-F: CC: {}, LCSS: {}, EMBEDDED LC: {}'.format(h(lc[1]), h(lc[2]), h(lc[3]))) _routed = False for rule in RULES[self._master]['GROUP_VOICE']: diff --git a/qr.py b/qr.py deleted file mode 100755 index ca5dc0c..0000000 --- a/qr.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env python -# -# This work is licensed under the Creative Attribution-NonCommercial-ShareAlike -# 3.0 Unported License.To view a copy of this license, visit -# http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to -# Creative Commons, 444 Castro Street, Suite 900, Mountain View, -# California, 94041, USA. - -from __future__ import print_function -from bitarray import bitarray - -# Does anybody read this stuff? There's a PEP somewhere that says I should do this. -__author__ = 'Cortney T. Buffington, N0MJS' -__copyright__ = 'Copyright (c) 2016 Cortney T. Buffington, N0MJS and the K0USY Group' -__credits__ = 'Jonathan Naylor, G4KLX who many parts of this were thankfully borrowed from' -__license__ = 'Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported' -__maintainer__ = 'Cort Buffington, N0MJS' -__email__ = 'n0mjs@me.com' - - -ENCODE_1676 = ( - 0x0000, 0x0273, 0x04E5, 0x0696, 0x09C9, 0x0BBA, 0x0D2C, 0x0F5F, 0x11E2, 0x1391, 0x1507, 0x1774, - 0x182B, 0x1A58, 0x1CCE, 0x1EBD, 0x21B7, 0x23C4, 0x2552, 0x2721, 0x287E, 0x2A0D, 0x2C9B, 0x2EE8, - 0x3055, 0x3226, 0x34B0, 0x36C3, 0x399C, 0x3BEF, 0x3D79, 0x3F0A, 0x411E, 0x436D, 0x45FB, 0x4788, - 0x48D7, 0x4AA4, 0x4C32, 0x4E41, 0x50FC, 0x528F, 0x5419, 0x566A, 0x5935, 0x5B46, 0x5DD0, 0x5FA3, - 0x60A9, 0x62DA, 0x644C, 0x663F, 0x6960, 0x6B13, 0x6D85, 0x6FF6, 0x714B, 0x7338, 0x75AE, 0x77DD, - 0x7882, 0x7AF1, 0x7C67, 0x7E14, 0x804F, 0x823C, 0x84AA, 0x86D9, 0x8986, 0x8BF5, 0x8D63, 0x8F10, - 0x91AD, 0x93DE, 0x9548, 0x973B, 0x9864, 0x9A17, 0x9C81, 0x9EF2, 0xA1F8, 0xA38B, 0xA51D, 0xA76E, - 0xA831, 0xAA42, 0xACD4, 0xAEA7, 0xB01A, 0xB269, 0xB4FF, 0xB68C, 0xB9D3, 0xBBA0, 0xBD36, 0xBF45, - 0xC151, 0xC322, 0xC5B4, 0xC7C7, 0xC898, 0xCAEB, 0xCC7D, 0xCE0E, 0xD0B3, 0xD2C0, 0xD456, 0xD625, - 0xD97A, 0xDB09, 0xDD9F, 0xDFEC, 0xE0E6, 0xE295, 0xE403, 0xE670, 0xE92F, 0xEB5C, 0xEDCA, 0xEFB9, - 0xF104, 0xF377, 0xF5E1, 0xF792, 0xF8CD, 0xFABE, 0xFC28, 0xFE5B) - -DECODE_1576 = ( - 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x4020, 0x0008, 0x0009, 0x000A, 0x000B, - 0x000C, 0x000D, 0x2081, 0x2080, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0C00, 0x0016, 0x0C02, - 0x0018, 0x0120, 0x001A, 0x0122, 0x4102, 0x0124, 0x4100, 0x4101, 0x0020, 0x0021, 0x0022, 0x4004, - 0x0024, 0x4002, 0x4001, 0x4000, 0x0028, 0x0110, 0x1800, 0x1801, 0x002C, 0x400A, 0x4009, 0x4008, - 0x0030, 0x0108, 0x0240, 0x0241, 0x0034, 0x4012, 0x4011, 0x4010, 0x0101, 0x0100, 0x0103, 0x0102, - 0x0105, 0x0104, 0x1401, 0x1400, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x4060, - 0x0048, 0x0049, 0x0301, 0x0300, 0x004C, 0x1600, 0x0305, 0x0304, 0x0050, 0x0051, 0x0220, 0x0221, - 0x3000, 0x4200, 0x3002, 0x4202, 0x0058, 0x1082, 0x1081, 0x1080, 0x3008, 0x4208, 0x2820, 0x1084, - 0x0060, 0x0061, 0x0210, 0x0211, 0x0480, 0x0481, 0x4041, 0x4040, 0x0068, 0x2402, 0x2401, 0x2400, - 0x0488, 0x3100, 0x2810, 0x2404, 0x0202, 0x0880, 0x0200, 0x0201, 0x0206, 0x0884, 0x0204, 0x0205, - 0x0141, 0x0140, 0x0208, 0x0209, 0x2802, 0x0144, 0x2800, 0x2801, 0x0080, 0x0081, 0x0082, 0x0A00, - 0x0084, 0x0085, 0x2009, 0x2008, 0x0088, 0x0089, 0x2005, 0x2004, 0x2003, 0x2002, 0x2001, 0x2000, - 0x0090, 0x0091, 0x0092, 0x1048, 0x0602, 0x0C80, 0x0600, 0x0601, 0x0098, 0x1042, 0x1041, 0x1040, - 0x2013, 0x2012, 0x2011, 0x2010, 0x00A0, 0x00A1, 0x00A2, 0x4084, 0x0440, 0x0441, 0x4081, 0x4080, - 0x6000, 0x1200, 0x6002, 0x1202, 0x6004, 0x2022, 0x2021, 0x2020, 0x0841, 0x0840, 0x2104, 0x0842, - 0x2102, 0x0844, 0x2100, 0x2101, 0x0181, 0x0180, 0x0B00, 0x0182, 0x5040, 0x0184, 0x2108, 0x2030, - 0x00C0, 0x00C1, 0x4401, 0x4400, 0x0420, 0x0421, 0x0422, 0x4404, 0x0900, 0x0901, 0x1011, 0x1010, - 0x0904, 0x2042, 0x2041, 0x2040, 0x0821, 0x0820, 0x1009, 0x1008, 0x4802, 0x0824, 0x4800, 0x4801, - 0x1003, 0x1002, 0x1001, 0x1000, 0x0501, 0x0500, 0x1005, 0x1004, 0x0404, 0x0810, 0x1100, 0x1101, - 0x0400, 0x0401, 0x0402, 0x0403, 0x040C, 0x0818, 0x1108, 0x1030, 0x0408, 0x0409, 0x040A, 0x2060, - 0x0801, 0x0800, 0x0280, 0x0802, 0x0410, 0x0804, 0x0412, 0x0806, 0x0809, 0x0808, 0x1021, 0x1020, - 0x5000, 0x2200, 0x5002, 0x2202) - -X14 = 0x00004000 # vector representation of X^14 -X8 = 0x00000100 # vector representation of X^8 -MASK7 = 0xffffff00 # auxiliary vector for testing -GENPOL = 0x00000139 # generator polinomial, g(x) - -def get_synd_1576(_pattern): - aux = X14; - if _pattern >= X8: - while _pattern & MASK7: - while not (aux & _pattern): - aux = aux >> 1 - _pattern ^= (aux / X8) * GENPOL - return _pattern - -def encode(_data): - value = (_data[0] >> 1) & 0x7F - cksum = ENCODE_1676[value] - _data[0] = cksum >> 8 - _data[1] = cksum & 0xFF - return _data - -def decode(_data): - code = (_data[0] << 7) + (_data[1] >> 1) - syndrome = get_synd_1576(code) - error_pattern = DECODE_1576[syndrome] - code ^= error_pattern - return code >> 7 - - -#------------------------------------------------------------------------------ -# Used to execute the module directly to run built-in tests -#------------------------------------------------------------------------------ - -if __name__ == '__main__': - - from binascii import b2a_hex as h - from time import time \ No newline at end of file