From 5c34ef40dafd7714a5948cf3e43c9f19bd2692cf Mon Sep 17 00:00:00 2001 From: Pedro Date: Thu, 23 Nov 2023 16:11:47 +0100 Subject: [PATCH] Add DataFrameFactory with the different frame types --- modem/data_frame_factory.py | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 modem/data_frame_factory.py diff --git a/modem/data_frame_factory.py b/modem/data_frame_factory.py new file mode 100644 index 00000000..fb49c772 --- /dev/null +++ b/modem/data_frame_factory.py @@ -0,0 +1,67 @@ +from modem_frametypes import FRAME_TYPE as FR_TYPE +import helpers + +class DataFrameFactory: + + def __init__(self, modem_config, modem_state, **data): + self.modem_config = modem_config + self.modem_state = modem_state + self.data = data + + self.myfullcall = f"{modem_config['STATION']['mycall']}-{modem_config['STATION']['myssid']}" + + def build(self): + build_method = getattr(self, self.type.name) + return build_method() + + def build_ping(self): + ping_frame = bytearray(self.length_sig0_frame) + ping_frame[:1] = bytes(self.type.value) + ping_frame[1:4] = helpers.get_crc_24(self.data['dxcallsign']) + ping_frame[4:7] = helpers.get_crc_24(self.myfullcall) + ping_frame[7:13] = helpers.callsign_to_bytes(self.myfullcall) + return ping_frame + + def build_cq(self): + cq_frame = bytearray(self.length_sig0_frame) + cq_frame[:1] = bytes([FR_TYPE.CQ.value]) + cq_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign) + cq_frame[7:11] = helpers.encode_grid(self.mygrid) + return cq_frame + + def build_fec_is_writing(self): + fec_frame = bytearray(14) + fec_frame[:1] = bytes([FR_TYPE.IS_WRITING.value]) + fec_frame[1:7] = helpers.callsign_to_bytes(mycallsign) + return fec_frame + + def build_qrv(self): + qrv_frame = bytearray(self.length_sig0_frame) + qrv_frame[:1] = bytes([FR_TYPE.QRV.value]) + qrv_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign) + qrv_frame[7:11] = helpers.encode_grid(self.mygrid) + qrv_frame[11:12] = helpers.snr_to_bytes(snr) + return qrv_frame + + def build_beacon(self): + beacon_frame = bytearray(self.length_sig0_frame) + beacon_frame[:1] = bytes([FR_TYPE.BEACON.value]) + beacon_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign) + beacon_frame[7:11] = helpers.encode_grid(self.mygrid) + return beacon_frame + + def build_fec_wakeup(self): + mode_int_wakeup = codec2.freedv_get_mode_value_by_name("sig0") + payload_per_wakeup_frame = modem.get_bytes_per_frame(mode_int_wakeup) - 2 + fec_wakeup_frame = bytearray(payload_per_wakeup_frame) + fec_wakeup_frame[:1] = bytes([FR_TYPE.FEC_WAKEUP.value]) + fec_wakeup_frame[1:7] = helpers.callsign_to_bytes(mycallsign) + fec_wakeup_frame[7:8] = bytes([mode_int]) + fec_wakeup_frame[8:9] = bytes([1]) # n payload bursts + return fec_wakeup_frame + + def build_fec(self): + fec_frame = bytearray(payload_per_frame) + fec_frame[:1] = bytes([FR_TYPE.FEC.value]) + fec_frame[1:payload_per_frame] = bytes(payload[:fec_payload_length]) + return fec_frame