diff --git a/tnc/broadcast.py b/tnc/broadcast.py new file mode 100644 index 00000000..6866075a --- /dev/null +++ b/tnc/broadcast.py @@ -0,0 +1,101 @@ +import structlog +import threading +import helpers +import time +import modem +import base64 +from static import ARQ, AudioParam, Beacon, Channel, Daemon, HamlibParam, ModemParam, Station, Statistics, TCIParam, TNC +import sock +import ujson as json + + +class broadcastHandler: + """Terminal Node Controller for FreeDATA""" + + log = structlog.get_logger("BROADCAST") + + def __init__(self) -> None: + self.fec_wakeup_callsign = bytes() + self.longest_duration = 6 + + def received_fec_wakeup(self, data_in: bytes): + self.fec_wakeup_callsign = helpers.bytes_to_callsign(bytes(data_in[1:7])) + mode = int.from_bytes(bytes(data_in[7:8]), "big") + bursts = int.from_bytes(bytes(data_in[8:9]), "big") + + modem.RECEIVE_DATAC4 = True + + self.send_data_to_socket_queue( + freedata="tnc-message", + fec="wakeup", + mode=mode, + bursts=bursts, + dxcallsign=str(self.fec_wakeup_callsign, "UTF-8") + ) + + timeout = time.time() + (self.longest_duration * bursts) + 2 + self.log.info( + "[TNC] FRAME WAKEUP RCVD [" + + str(self.fec_wakeup_callsign, "UTF-8") + + "] ", mode=mode, bursts=bursts, timeout=timeout, + ) + + while time.time() < timeout: + threading.Event().wait(0.01) + + self.log.info( + "[TNC] closing broadcast slot [" + + str(self.fec_wakeup_callsign, "UTF-8") + + "] ", mode=mode, bursts=bursts, + ) + # TODO: We need a dynamic way of modifying this + modem.RECEIVE_DATAC4 = False + self.fec_wakeup_callsign = bytes() + + + def received_fec(self, data_in: bytes): + self.send_data_to_socket_queue( + freedata="tnc-message", + fec="broadcast", + dxcallsign=str(self.fec_wakeup_callsign, "UTF-8"), + data=base64.b64encode(data_in[1:]).decode("UTF-8") + ) + + self.log.info("[TNC] FEC DATA RCVD") + + def send_data_to_socket_queue(self, **jsondata): + """ + Send information to the UI via JSON and the sock.SOCKET_QUEUE. + + Args: + Dictionary containing the data to be sent, in the format: + key=value, for each item. E.g.: + self.send_data_to_socket_queue( + freedata="tnc-message", + arq="received", + status="success", + uuid=self.transmission_uuid, + timestamp=timestamp, + mycallsign=str(self.mycallsign, "UTF-8"), + dxcallsign=str(Station.dxcallsign, "UTF-8"), + dxgrid=str(Station.dxgrid, "UTF-8"), + data=base64_data, + ) + """ + + # add mycallsign and dxcallsign to network message if they not exist + # and make sure we are not overwrite them if they exist + try: + if "mycallsign" not in jsondata: + jsondata["mycallsign"] = str(self.mycallsign, "UTF-8") + if "dxcallsign" not in jsondata: + jsondata["dxcallsign"] = str(Station.dxcallsign, "UTF-8") + except Exception as e: + self.log.debug("[TNC] error adding callsigns to network message", e=e) + + # run json dumps + json_data_out = json.dumps(jsondata) + + self.log.debug("[TNC] send_data_to_socket_queue:", jsondata=json_data_out) + # finally push data to our network queue + sock.SOCKET_QUEUE.put(json_data_out) \ No newline at end of file diff --git a/tnc/data_handler.py b/tnc/data_handler.py index e1c3b74f..dc91631d 100644 --- a/tnc/data_handler.py +++ b/tnc/data_handler.py @@ -28,7 +28,7 @@ import ujson as json from codec2 import FREEDV_MODE, FREEDV_MODE_USED_SLOTS from queues import DATA_QUEUE_RECEIVED, DATA_QUEUE_TRANSMIT, RX_BUFFER from static import FRAME_TYPE as FR_TYPE - +import broadcast TESTMODE = False @@ -101,7 +101,7 @@ class DATA: self.rx_n_frames_per_burst = 0 self.max_n_frames_per_burst = 1 - self.fec_wakeup_callsign = bytes() + self.broadcast = broadcast.broadcastHandler() # Flag to indicate if we received a low bandwidth mode channel opener self.received_LOW_BANDWIDTH_MODE = False @@ -223,8 +223,8 @@ class DATA: FR_TYPE.PING.value: (self.received_ping, "PING"), FR_TYPE.QRV.value: (self.received_qrv, "QRV"), FR_TYPE.IS_WRITING.value: (self.received_is_writing, "IS_WRITING"), - FR_TYPE.FEC.value: (self.received_fec, "FEC"), - FR_TYPE.FEC_WAKEUP.value: (self.received_fec_wakeup, "FEC WAKEUP"), + FR_TYPE.FEC.value: (self.broadcast.received_fec, "FEC"), + FR_TYPE.FEC_WAKEUP.value: (self.broadcast.received_fec_wakeup, "FEC WAKEUP"), } self.command_dispatcher = { @@ -2977,51 +2977,6 @@ class DATA: ) - def received_fec_wakeup(self, data_in: bytes): - self.fec_wakeup_callsign = helpers.bytes_to_callsign(bytes(data_in[1:7])) - mode = int.from_bytes(bytes(data_in[7:8]), "big") - bursts = int.from_bytes(bytes(data_in[8:9]), "big") - - self.set_listening_modes(True, False, mode) - - self.send_data_to_socket_queue( - freedata="tnc-message", - fec="wakeup", - mode=mode, - bursts=bursts, - dxcallsign=str(self.fec_wakeup_callsign, "UTF-8") - ) - - timeout = time.time() + (self.longest_duration * bursts) + 2 - self.log.info( - "[TNC] FRAME WAKEUP RCVD [" - + str(self.fec_wakeup_callsign, "UTF-8") - + "] ", mode=mode, bursts=bursts, timeout=timeout, - ) - - while time.time() < timeout: - threading.Event().wait(0.01) - - self.log.info( - "[TNC] closing broadcast slot [" - + str(self.fec_wakeup_callsign, "UTF-8") - + "] ", mode=mode, bursts=bursts, - ) - # TODO: We need a dynamic way of modifying this - modem.RECEIVE_DATAC4 = False - self.fec_wakeup_callsign = bytes() - - - def received_fec(self, data_in: bytes): - self.send_data_to_socket_queue( - freedata="tnc-message", - fec="broadcast", - dxcallsign=str(self.fec_wakeup_callsign, "UTF-8"), - data=base64.b64encode(data_in[1:]).decode("UTF-8") - ) - - self.log.info("[TNC] FEC DATA RCVD") - def received_is_writing(self, data_in: bytes) -> None: """