"""ODTP gateway: embedded data, text messages and locations, end to end.""" import os import struct from bitarray import bitarray from adn_server.application.odtp.messages import Messages from adn_server.application.odtp.ports import OdtpSettings from adn_server.application.odtp.session import _Uplink from adn_server.domain import odtp from adn_server.domain.dmr import data_header as hd from adn_server.domain.dmr import hdap, ip_sms from adn_server.domain.dmr.crc import crc32_data from adn_server.domain.dmr.data_blocks import ( DT_CSBK, DT_DATA_HEADER, build_blocks, data_frames, decode_block, decode_header_burst, ) from adn_server.domain.dmr.embedded_lc import collect_embedded_lc from adn_server.domain.dmr.frames import DmrdStream, superframe_embedding from adn_server.domain.dmrd import parse_dmrd_header from tests.support.odtp_stack import PEER, TERMINAL_ID, FakeClock, Stack RADIO = 2140001 TA_HEADER = bytes([0x04, 0x00, 0x42]) + b"C31AG " # Talker Alias header: 7-bit, 6 characters NMEA = b"$GPRMC,120000,A,4230.378,N,00131.310,E,0.0,0.0,251026,,*00" def _up(**kw): stack = Stack(**kw) term = stack.terminal() term.login() assert term.got(odtp.KEEP_ALIVE) return stack, term def _packets(frames): """(header, user data) of each packet in a list of DMRD frames (CSBK preambles skipped).""" out, cur = [], None for f in frames: h = parse_dmrd_header(f) if h.dtype_vseq == DT_CSBK: continue if h.dtype_vseq == DT_DATA_HEADER: cur = [hd.DataHeader.from_bytes(decode_header_burst(f[20:53])), []] out.append(cur) else: cur[1].append((h.dtype_vseq, f[20:53])) result = [] for header, data in out: confirmed = header.confirmed or (header.dpf == hd.DPF_SHORT_DATA_DEFINED and header.sarq) blocks = [decode_block(b, dt, confirmed, i == len(data) - 1) for i, (dt, b) in enumerate(data)] user = b"".join(b.data for b in blocks) assert not blocks or (all(b.crc_ok for b in blocks) and crc32_data(user) == blocks[-1].crc32) result.append((header, user)) return result def _radio(fmt, text, dst=TERMINAL_ID): """A private text from RADIO asking for every ACK it can, as the MASTER delivers it to the terminal's peer.""" m = Messages(RADIO, RADIO * 100 + 1, OdtpSettings(sms_preambles=0), FakeClock(), lambda f: None, lambda *a: None) if fmt == "tms": tms = bytearray(ip_sms.tms_simple_text(text, 3)) tms[2] |= 0x40 # application ACK wanted frames = m._ip_frames(dst, ip_sms.PORT_TMS, bytes(tms), 2) elif fmt == "hytera": tmp = hdap.text(9, dst, RADIO, text) frames = m._short_frames(dst, hdap.build(hdap.SERVICE_TMP, hdap.TMP_PRIVATE_MESSAGE, hdap.find(tmp)[0].payload), 2) else: frames = m._ip_frames(dst, ip_sms.PORT_DMR_STANDARD_SMS, ip_sms.standard_sms(text), 2) return [f[:11] + PEER + f[15:] for f in frames] def _lcs(frames): acc, out = {}, [] for f in frames: h = parse_dmrd_header(f) if h.frame_type in (0, 1): lc = collect_embedded_lc(acc, h.dtype_vseq if h.frame_type == 0 else 0, f[20:53]) if lc is not None: out.append(lc) return out def test_text_status_location_codecs(): msg = odtp.text_message(TERMINAL_ID, RADIO, "hola ñandú") assert odtp.parse_text_message(msg) == (TERMINAL_ID, RADIO, False, "hola ñandú") assert odtp.parse_text_message(odtp.text_message(1, 213, "x", group=True))[2] is True assert odtp.parse_text_status(odtp.text_status(RADIO, TERMINAL_ID, 8)) == (RADIO, TERMINAL_ID, 8) report = struct.pack("