FreeDATA/modem/arq_session.py

57 lines
1.7 KiB
Python
Raw Normal View History

2023-12-05 17:50:39 +00:00
import queue, threading
2023-12-06 10:59:35 +00:00
import codec2
2023-12-05 17:50:39 +00:00
import data_frame_factory
2023-12-05 18:12:21 +00:00
import structlog
2023-12-05 17:50:39 +00:00
class ARQSession():
MODE_BY_SPEED = [
2023-12-06 10:59:35 +00:00
codec2.FREEDV_MODE.datac4.value,
codec2.FREEDV_MODE.datac3.value,
codec2.FREEDV_MODE.datac1.value,
]
2023-12-05 17:50:39 +00:00
def __init__(self, config: dict, tx_frame_queue: queue.Queue, dxcall: str):
2023-12-05 18:12:21 +00:00
self.logger = structlog.get_logger(type(self).__name__)
2023-12-05 17:50:39 +00:00
self.config = config
self.dxcall = dxcall
self.tx_frame_queue = tx_frame_queue
self.speed_level = 0
self.frame_factory = data_frame_factory.DataFrameFactory(self.config)
self.id = None
2023-12-10 08:59:02 +00:00
# 3 bytes for the BOF Beginning of File indicator in a data frame
self.data_frame_bof = b"BOF"
# 3 bytes for the EOF End of File indicator in a data frame
self.data_frame_eof = b"EOF"
2023-12-11 18:02:50 +00:00
def log(self, message, isWarning = False):
msg = f"[{type(self).__name__}]: {message}"
logger = self.logger.warn if isWarning else self.logger.info
logger(msg)
2023-12-10 08:59:02 +00:00
2023-12-05 17:50:39 +00:00
def get_mode_by_speed_level(self, speed_level):
return self.MODE_BY_SPEED[speed_level]
2023-12-05 18:01:48 +00:00
def transmit_frame(self, frame: bytearray):
2023-12-11 18:02:50 +00:00
self.log("Transmitting frame")
2023-12-05 18:01:48 +00:00
modem_queue_item = {
'mode': self.get_mode_by_speed_level(self.speed_level),
'repeat': 1,
'repeat_delay': 1,
'frame': frame,
}
self.tx_frame_queue.put(modem_queue_item)
2023-12-05 18:12:21 +00:00
def setState(self, state):
self.state = state
2023-12-11 18:02:50 +00:00
self.log(f"state changed to {state}")
2023-12-06 10:59:35 +00:00
def get_payload_size(self, speed_level):
mode = self.MODE_BY_SPEED[speed_level]
return codec2.get_bytes_per_frame(mode)