FreeDATA/modem/frame_dispatcher.py

210 lines
8.3 KiB
Python
Raw Normal View History

"""
FRAME DISPATCHER - We are dispatching the received frames to the needed functions
2023-11-23 09:14:11 +00:00
"""
2023-11-23 09:14:11 +00:00
import threading
import helpers
2023-11-28 19:00:39 +00:00
import structlog
2023-11-23 09:14:11 +00:00
from modem_frametypes import FRAME_TYPE as FR_TYPE
import event_manager
2023-11-25 18:05:42 +00:00
from queues import DATA_QUEUE_RECEIVED, DATA_QUEUE_TRANSMIT, MODEM_TRANSMIT_QUEUE
from data_frame_factory import DataFrameFactory
2023-11-23 09:14:11 +00:00
from data_handler_broadcasts import BROADCAST
from data_handler_data_broadcasts import DATABROADCAST
from data_handler_ping import PING
from protocol_arq_iss import ISS
from protocol_arq_irs import IRS
from protocol_arq import ARQ
from protocol_arq_session import SESSION
2023-11-23 09:14:11 +00:00
class DISPATCHER():
2023-11-29 16:35:23 +00:00
def __init__(self, config, event_queue, states, data_q_rx):
2023-11-28 19:00:39 +00:00
self.log = structlog.get_logger("frame_dispatcher")
self.log.info("loading frame dispatcher.....\n")
2023-11-23 09:14:11 +00:00
self.config = config
self.event_queue = event_queue
self.states = states
self._initialize_handlers(config, event_queue, states)
self._initialize_dispatchers()
2023-11-29 16:35:23 +00:00
self.data_queue_transmit = DATA_QUEUE_TRANSMIT
self.data_queue_received = data_q_rx
2023-11-23 09:14:11 +00:00
def _initialize_handlers(self, config, event_queue, states):
"""Initializes various data handlers."""
self.frame_factory = DataFrameFactory(config)
2023-11-23 09:14:11 +00:00
self.broadcasts = BROADCAST(config, event_queue, states)
self.data_broadcasts = DATABROADCAST(config, event_queue, states)
self.ping = PING(config, event_queue, states)
self.arq = ARQ(config, event_queue, states)
self.arq_irs = IRS(config, event_queue, states)
self.arq_iss = ISS(config, event_queue, states)
self.arq_session = SESSION(config, event_queue, states)
2023-11-23 09:14:11 +00:00
self.event_manager = event_manager.EventManager([event_queue])
def _initialize_dispatchers(self):
"""Initializes dispatchers for handling different frame types."""
# Dictionary of functions and log messages used in process_data
# instead of a long series of if-elif-else statements.
self.rx_dispatcher = {
FR_TYPE.ARQ_DC_OPEN_ACK_N.value: (
self.arq_iss.arq_received_channel_is_open,
"ARQ OPEN ACK (Narrow)",
),
FR_TYPE.ARQ_DC_OPEN_ACK_W.value: (
self.arq_iss.arq_received_channel_is_open,
"ARQ OPEN ACK (Wide)",
),
FR_TYPE.ARQ_DC_OPEN_N.value: (
self.initialize_arq_transmission_irs,
2023-11-23 09:14:11 +00:00
"ARQ Data Channel Open (Narrow)",
),
FR_TYPE.ARQ_DC_OPEN_W.value: (
self.initialize_arq_transmission_irs,
2023-11-23 09:14:11 +00:00
"ARQ Data Channel Open (Wide)",
),
FR_TYPE.ARQ_SESSION_CLOSE.value: (
self.arq_session.received_session_close,
"ARQ CLOSE SESSION",
),
FR_TYPE.ARQ_SESSION_HB.value: (
self.arq_session.received_session_heartbeat,
"ARQ HEARTBEAT",
),
FR_TYPE.ARQ_SESSION_OPEN.value: (
self.arq_session.received_session_opener,
"ARQ OPEN SESSION",
),
FR_TYPE.ARQ_STOP.value: (self.arq.received_stop_transmission, "ARQ STOP TX"),
FR_TYPE.BEACON.value: (self.broadcasts.received_beacon, "BEACON"),
FR_TYPE.BURST_ACK.value: (self.arq_iss.burst_ack_nack_received, "BURST ACK"),
FR_TYPE.BURST_NACK.value: (self.arq_iss.burst_ack_nack_received, "BURST NACK"),
FR_TYPE.CQ.value: (self.broadcasts.received_cq, "CQ"),
FR_TYPE.FR_ACK.value: (self.arq_iss.frame_ack_received, "FRAME ACK"),
FR_TYPE.FR_NACK.value: (self.arq_iss.frame_nack_received, "FRAME NACK"),
FR_TYPE.FR_REPEAT.value: (self.arq_iss.burst_rpt_received, "REPEAT REQUEST"),
FR_TYPE.PING_ACK.value: (self.ping.received_ping_ack, "PING ACK"),
FR_TYPE.PING.value: (self.ping.received_ping, "PING"),
FR_TYPE.QRV.value: (self.broadcasts.received_qrv, "QRV"),
FR_TYPE.IS_WRITING.value: (self.broadcasts.received_is_writing, "IS_WRITING"),
FR_TYPE.FEC.value: (self.data_broadcasts.received_fec, "FEC"),
FR_TYPE.FEC_WAKEUP.value: (self.data_broadcasts.received_fec_wakeup, "FEC WAKEUP"),
}
2023-11-26 13:51:58 +00:00
def start(self):
2023-11-23 09:14:11 +00:00
"""Starts worker threads for transmit and receive operations."""
threading.Thread(target=self.worker_transmit, name="Transmit Worker", daemon=True).start()
threading.Thread(target=self.worker_receive, name="Receive Worker", daemon=True).start()
def worker_transmit(self) -> None:
"""Dispatch incoming UI instructions for transmitting operations"""
while True:
2023-11-24 09:46:51 +00:00
command = self.data_queue_transmit.get()
command.run(self.event_queue, MODEM_TRANSMIT_QUEUE)
2023-11-23 09:14:11 +00:00
def worker_receive(self) -> None:
"""Queue received data for processing"""
while True:
data = self.data_queue_received.get()
# [0] bytes
# [1] freedv instance
# [2] bytes_per_frame
# [3] snr
self.process_data(
bytes_out=data[0], freedv=data[1], bytes_per_frame=data[2], snr=data[3]
)
def process_data(self, bytes_out, freedv, bytes_per_frame: int, snr) -> None:
"""
Process incoming data and decide what to do with the frame.
Args:
bytes_out:
freedv:
bytes_per_frame:
snr:
Returns:
"""
if True:
# get frame as dictionary
deconstructed_frame = self.frame_factory.deconstruct(bytes_out)
2023-11-28 19:00:39 +00:00
frametype = deconstructed_frame["frame_type_int"]
print(deconstructed_frame)
print(frametype)
# Dispatch activity based on received frametype
2023-11-23 09:14:11 +00:00
if frametype in self.rx_dispatcher:
# Process frames "known" by rx_dispatcher
# self.log.debug(f"[Modem] {self.rx_dispatcher[frametype][1]} RECEIVED....")
self.rx_dispatcher[frametype][0](deconstructed_frame, snr)
2023-11-23 09:14:11 +00:00
# Process frametypes requiring a different set of arguments.
elif FR_TYPE.BURST_51.value >= frametype >= FR_TYPE.BURST_01.value:
self.arq_irs.arq_data_received(
deconstructed_frame, bytes_per_frame, snr, freedv
2023-11-23 09:14:11 +00:00
)
# if we received the last frame of a burst or the last remaining rpt frame, do a modem unsync
# if self.arq_rx_burst_buffer.count(None) <= 1 or (frame+1) == n_frames_per_burst:
# self.log.debug(f"[Modem] LAST FRAME OF BURST --> UNSYNC {frame+1}/{n_frames_per_burst}")
# self.c_lib.freedv_set_sync(freedv, 0)
# TESTFRAMES
elif frametype == FR_TYPE.TEST_FRAME.value:
self.log.debug("[Modem] TESTFRAME RECEIVED", frame=deconstructed_frame)
2023-11-23 09:14:11 +00:00
# Unknown frame type
else:
self.log.warning(
"[Modem] ARQ - other frame type", frametype=FR_TYPE(frametype).name
)
else:
# for debugging purposes to receive all data
self.log.debug(
f"[Modem] Foreign frame received ({ex})",
frame=bytes_out[:-2].hex(),
frame_type=FR_TYPE(int.from_bytes(bytes_out[:1], byteorder="big")).name,
)
def get_id_from_frame(self, data):
if data[:1] in [FR_TYPE.ARQ_DC_OPEN_N, FR_TYPE.ARQ_DC_OPEN_W]:
2023-11-28 19:00:39 +00:00
return data[13:14]
return None
def initialize_arq_instance(self):
self.arq = ARQ(self.config, self.event_queue, self.states)
self.arq_irs = IRS(self.config, self.event_queue, self.states)
self.arq_iss = ISS(self.config, self.event_queue, self.states)
self.arq_session = SESSION(self.config, self.event_queue, self.states)
return {
'arq': self.arq,
'arq_irs': self.arq_irs,
'arq_iss': self.arq_iss,
'arq_session': self.arq_session
}
def initialize_arq_transmission_irs(self, data):
if id := self.get_id_from_frame(data):
instance = self.initialize_arq_instance()
self.states.register_arq_instance_by_id(id, instance)
2023-11-28 19:00:39 +00:00
instance['arq_irs'].arq_received_data_channel_opener()