2023-11-23 15:11:47 +00:00
|
|
|
from modem_frametypes import FRAME_TYPE as FR_TYPE
|
|
|
|
import helpers
|
2023-11-23 15:59:53 +00:00
|
|
|
import codec2
|
2023-11-23 15:11:47 +00:00
|
|
|
|
|
|
|
class DataFrameFactory:
|
|
|
|
|
2023-11-26 11:45:51 +00:00
|
|
|
LENGTH_SIG0_FRAME = 14
|
2023-11-28 19:43:41 +00:00
|
|
|
LENGTH_SIG1_FRAME = 14
|
2024-03-29 13:39:55 +00:00
|
|
|
LENGTH_ACK_FRAME = 3
|
2023-11-26 11:45:51 +00:00
|
|
|
|
2023-12-20 15:43:08 +00:00
|
|
|
"""
|
|
|
|
helpers.set_flag(byte, 'DATA-ACK-NACK', True, FLAG_POSITIONS)
|
|
|
|
helpers.get_flag(byte, 'DATA-ACK-NACK', FLAG_POSITIONS)
|
|
|
|
"""
|
|
|
|
ARQ_FLAGS = {
|
2023-12-25 11:27:48 +00:00
|
|
|
'FINAL': 0, # Bit-position for indicating the FINAL state
|
2023-12-28 21:27:49 +00:00
|
|
|
'ABORT': 1, # Bit-position for indicating the ABORT request
|
|
|
|
'CHECKSUM': 2, # Bit-position for indicating the CHECKSUM is correct or not
|
2023-12-20 15:43:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 11:45:51 +00:00
|
|
|
def __init__(self, config):
|
2024-03-16 12:44:58 +00:00
|
|
|
|
2023-11-26 11:45:51 +00:00
|
|
|
self.myfullcall = f"{config['STATION']['mycall']}-{config['STATION']['myssid']}"
|
|
|
|
self.mygrid = config['STATION']['mygrid']
|
2023-11-26 09:33:06 +00:00
|
|
|
|
|
|
|
# table for holding our frame templates
|
|
|
|
self.template_list = {}
|
|
|
|
|
2023-11-26 09:25:13 +00:00
|
|
|
self._load_broadcast_templates()
|
|
|
|
self._load_ping_templates()
|
2023-11-27 22:08:00 +00:00
|
|
|
self._load_arq_templates()
|
2024-03-09 09:47:27 +00:00
|
|
|
self._load_p2p_connection_templates()
|
2023-11-26 09:25:13 +00:00
|
|
|
|
|
|
|
def _load_broadcast_templates(self):
|
|
|
|
# cq frame
|
|
|
|
self.template_list[FR_TYPE.CQ.value] = {
|
2023-11-26 11:45:51 +00:00
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": 6,
|
2023-11-26 09:25:13 +00:00
|
|
|
"gridsquare": 4
|
|
|
|
}
|
|
|
|
|
|
|
|
# qrv frame
|
|
|
|
self.template_list[FR_TYPE.QRV.value] = {
|
2023-11-26 11:45:51 +00:00
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": 6,
|
2023-11-26 09:25:13 +00:00
|
|
|
"gridsquare": 4,
|
|
|
|
"snr": 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# beacon frame
|
|
|
|
self.template_list[FR_TYPE.BEACON.value] = {
|
2023-11-26 11:45:51 +00:00
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": 6,
|
2023-11-26 09:25:13 +00:00
|
|
|
"gridsquare": 4
|
|
|
|
}
|
|
|
|
|
|
|
|
def _load_ping_templates(self):
|
|
|
|
# ping frame
|
|
|
|
self.template_list[FR_TYPE.PING.value] = {
|
2023-11-26 11:45:51 +00:00
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
2023-11-28 19:34:33 +00:00
|
|
|
"destination_crc": 3,
|
|
|
|
"origin_crc": 3,
|
|
|
|
"origin": 6
|
2023-11-26 09:25:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 23:15:16 +00:00
|
|
|
# ping ack
|
|
|
|
self.template_list[FR_TYPE.PING_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"destination_crc": 3,
|
|
|
|
"origin_crc": 3,
|
|
|
|
"gridsquare": 4,
|
|
|
|
"snr": 1,
|
|
|
|
}
|
|
|
|
|
2023-11-26 09:25:13 +00:00
|
|
|
|
2023-11-27 22:08:00 +00:00
|
|
|
def _load_arq_templates(self):
|
2023-11-30 19:35:07 +00:00
|
|
|
|
2023-12-12 20:20:03 +00:00
|
|
|
self.template_list[FR_TYPE.ARQ_SESSION_OPEN.value] = {
|
2023-11-27 22:08:00 +00:00
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
2023-11-28 19:34:33 +00:00
|
|
|
"destination_crc": 3,
|
|
|
|
"origin": 6,
|
2023-11-27 22:08:00 +00:00
|
|
|
"session_id": 1,
|
2024-03-24 20:39:44 +00:00
|
|
|
"maximum_bandwidth": 2,
|
2023-11-27 22:08:00 +00:00
|
|
|
}
|
2023-11-30 19:35:07 +00:00
|
|
|
|
2023-12-12 20:20:03 +00:00
|
|
|
self.template_list[FR_TYPE.ARQ_SESSION_OPEN_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
"origin": 6,
|
|
|
|
"destination_crc": 3,
|
|
|
|
"version": 1,
|
|
|
|
"snr": 1,
|
2024-01-04 09:01:29 +00:00
|
|
|
"flag": 1,
|
2023-12-12 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.template_list[FR_TYPE.ARQ_SESSION_INFO.value] = {
|
2023-11-30 19:35:07 +00:00
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"session_id": 1,
|
2023-12-12 20:20:03 +00:00
|
|
|
"total_length": 4,
|
|
|
|
"total_crc": 4,
|
|
|
|
"snr": 1,
|
2023-12-25 11:27:48 +00:00
|
|
|
"flag": 1,
|
2024-01-21 19:34:01 +00:00
|
|
|
"type": 1,
|
2023-12-12 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.template_list[FR_TYPE.ARQ_SESSION_INFO_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
"total_crc": 4,
|
|
|
|
"snr": 1,
|
2023-11-30 19:35:07 +00:00
|
|
|
"speed_level": 1,
|
2023-12-12 20:20:03 +00:00
|
|
|
"frames_per_burst": 1,
|
2023-12-20 18:30:38 +00:00
|
|
|
"flag": 1,
|
2023-11-30 19:35:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 14:05:22 +00:00
|
|
|
self.template_list[FR_TYPE.ARQ_STOP.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.template_list[FR_TYPE.ARQ_STOP_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
2023-12-12 20:33:57 +00:00
|
|
|
# arq burst frame
|
2023-12-14 08:07:46 +00:00
|
|
|
self.template_list[FR_TYPE.ARQ_BURST_FRAME.value] = {
|
2023-12-13 08:24:43 +00:00
|
|
|
"frame_length": None,
|
2023-12-12 19:46:22 +00:00
|
|
|
"session_id": 1,
|
2024-02-22 20:15:43 +00:00
|
|
|
"speed_level": 1,
|
2023-12-12 20:33:57 +00:00
|
|
|
"offset": 4,
|
2023-12-12 19:46:22 +00:00
|
|
|
"data": "dynamic",
|
|
|
|
}
|
2023-12-08 10:42:38 +00:00
|
|
|
|
2023-12-08 09:25:26 +00:00
|
|
|
# arq burst ack
|
2023-12-14 08:07:46 +00:00
|
|
|
self.template_list[FR_TYPE.ARQ_BURST_ACK.value] = {
|
2024-03-29 13:39:55 +00:00
|
|
|
"frame_length": self.LENGTH_ACK_FRAME,
|
2023-11-28 19:34:33 +00:00
|
|
|
"session_id": 1,
|
2024-03-29 13:28:02 +00:00
|
|
|
#"offset":4,
|
2023-11-28 19:34:33 +00:00
|
|
|
"speed_level": 1,
|
2024-03-29 13:28:02 +00:00
|
|
|
#"frames_per_burst": 1,
|
|
|
|
#"snr": 1,
|
2023-12-20 15:43:08 +00:00
|
|
|
"flag": 1,
|
2023-11-28 19:34:33 +00:00
|
|
|
}
|
2024-03-09 09:47:27 +00:00
|
|
|
|
|
|
|
def _load_p2p_connection_templates(self):
|
|
|
|
# p2p connect request
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_CONNECT.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"destination_crc": 3,
|
|
|
|
"origin": 6,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# connect ACK
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_CONNECT_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"destination_crc": 3,
|
|
|
|
"origin": 6,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# heartbeat for "is alive"
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_HEARTBEAT.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# ack heartbeat
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_HEARTBEAT_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# p2p payload frames
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_PAYLOAD.value] = {
|
|
|
|
"frame_length": None,
|
|
|
|
"session_id": 1,
|
|
|
|
"sequence_id": 1,
|
|
|
|
"data": "dynamic",
|
|
|
|
}
|
|
|
|
|
|
|
|
# p2p payload frame ack
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_PAYLOAD_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
"sequence_id": 1,
|
|
|
|
}
|
|
|
|
|
2024-03-15 13:25:46 +00:00
|
|
|
# heartbeat for "is alive"
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_DISCONNECT.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# ack heartbeat
|
|
|
|
self.template_list[FR_TYPE.P2P_CONNECTION_DISCONNECT_ACK.value] = {
|
|
|
|
"frame_length": self.LENGTH_SIG1_FRAME,
|
|
|
|
"session_id": 1,
|
|
|
|
}
|
|
|
|
|
2023-11-28 19:34:33 +00:00
|
|
|
|
2023-12-14 08:07:46 +00:00
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
def construct(self, frametype, content, frame_length = LENGTH_SIG1_FRAME):
|
|
|
|
frame_template = self.template_list[frametype.value]
|
2023-12-08 10:42:38 +00:00
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
if isinstance(frame_template["frame_length"], int):
|
2023-12-16 16:21:07 +00:00
|
|
|
frame_length = frame_template["frame_length"]
|
2023-12-08 10:42:38 +00:00
|
|
|
else:
|
2024-03-31 16:53:46 +00:00
|
|
|
frame_length -= 2
|
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
frame = bytearray(frame_length)
|
2024-03-29 13:28:02 +00:00
|
|
|
if frametype in [FR_TYPE.ARQ_BURST_ACK]:
|
|
|
|
buffer_position = 0
|
|
|
|
else:
|
|
|
|
frame[:1] = bytes([frametype.value])
|
|
|
|
buffer_position = 1
|
2023-11-26 09:25:13 +00:00
|
|
|
for key, item_length in frame_template.items():
|
2023-12-13 08:56:58 +00:00
|
|
|
if key == "frame_length":
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not isinstance(item_length, int):
|
|
|
|
item_length = len(content[key])
|
|
|
|
if buffer_position + item_length > frame_length:
|
2023-12-13 09:05:14 +00:00
|
|
|
raise OverflowError("Frame data overflow!")
|
2023-12-13 08:56:58 +00:00
|
|
|
frame[buffer_position: buffer_position + item_length] = content[key]
|
|
|
|
buffer_position += item_length
|
2023-11-26 09:25:13 +00:00
|
|
|
return frame
|
|
|
|
|
2024-04-01 17:44:41 +00:00
|
|
|
def deconstruct(self, frame, mode_name=None):
|
2023-11-26 09:25:13 +00:00
|
|
|
buffer_position = 1
|
2024-04-01 17:44:41 +00:00
|
|
|
|
|
|
|
# Handle the case where the frame type is not recognized
|
|
|
|
#raise ValueError(f"Unknown frame type: {frametype}")
|
|
|
|
if mode_name in ["SIGNALLING_ACK"]:
|
2024-03-29 13:28:02 +00:00
|
|
|
frametype = FR_TYPE.ARQ_BURST_ACK.value
|
|
|
|
frame_template = self.template_list.get(frametype)
|
|
|
|
frame = bytes([frametype]) + frame
|
2024-04-01 17:44:41 +00:00
|
|
|
else:
|
|
|
|
# Extract frametype and get the corresponding template
|
|
|
|
frametype = int.from_bytes(frame[:1], "big")
|
|
|
|
frame_template = self.template_list.get(frametype)
|
2023-11-26 09:25:13 +00:00
|
|
|
|
2023-11-28 19:00:39 +00:00
|
|
|
extracted_data = {"frame_type": FR_TYPE(frametype).name, "frame_type_int": frametype}
|
2024-04-01 17:44:41 +00:00
|
|
|
|
2023-11-26 09:25:13 +00:00
|
|
|
for key, item_length in frame_template.items():
|
2023-12-13 08:24:43 +00:00
|
|
|
if key == "frame_length":
|
|
|
|
continue
|
2023-11-26 09:25:13 +00:00
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
# data is always on the last payload slots
|
|
|
|
if item_length in ["dynamic"] and key in["data"]:
|
2023-12-16 16:21:07 +00:00
|
|
|
data = frame[buffer_position:-2]
|
2023-12-13 08:24:43 +00:00
|
|
|
item_length = len(data)
|
|
|
|
else:
|
|
|
|
data = frame[buffer_position: buffer_position + item_length]
|
2023-11-26 10:41:37 +00:00
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
# Process the data based on the key
|
|
|
|
if key in ["origin", "destination"]:
|
|
|
|
extracted_data[key] = helpers.bytes_to_callsign(data).decode()
|
2023-12-09 12:31:19 +00:00
|
|
|
|
2023-12-13 17:27:55 +00:00
|
|
|
elif key in ["origin_crc", "destination_crc", "total_crc"]:
|
2023-12-13 08:24:43 +00:00
|
|
|
extracted_data[key] = data.hex()
|
2023-12-05 17:50:39 +00:00
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
elif key == "gridsquare":
|
|
|
|
extracted_data[key] = helpers.decode_grid(data)
|
2023-12-05 17:50:39 +00:00
|
|
|
|
2023-12-13 08:24:43 +00:00
|
|
|
elif key in ["session_id", "speed_level",
|
|
|
|
"frames_per_burst", "version",
|
2024-03-24 20:39:44 +00:00
|
|
|
"offset", "total_length", "state", "type", "maximum_bandwidth"]:
|
2023-12-13 08:24:43 +00:00
|
|
|
extracted_data[key] = int.from_bytes(data, 'big')
|
2023-11-26 09:25:13 +00:00
|
|
|
|
2023-12-21 14:05:22 +00:00
|
|
|
elif key in ["snr"]:
|
|
|
|
extracted_data[key] = helpers.snr_from_bytes(data)
|
|
|
|
|
2023-12-20 15:43:08 +00:00
|
|
|
elif key == "flag":
|
|
|
|
|
|
|
|
data = int.from_bytes(data, "big")
|
|
|
|
extracted_data[key] = {}
|
2024-01-04 09:01:29 +00:00
|
|
|
|
|
|
|
# check for frametype for selecting the correspinding flag dictionary
|
|
|
|
if frametype in [FR_TYPE.ARQ_SESSION_OPEN_ACK.value, FR_TYPE.ARQ_SESSION_INFO_ACK.value, FR_TYPE.ARQ_BURST_ACK.value]:
|
2023-12-20 15:43:08 +00:00
|
|
|
flag_dict = self.ARQ_FLAGS
|
2023-12-20 18:30:38 +00:00
|
|
|
for flag in flag_dict:
|
|
|
|
# Update extracted_data with the status of each flag
|
|
|
|
# get_flag returns True or False based on the bit value at the flag's position
|
|
|
|
extracted_data[key][flag] = helpers.get_flag(data, flag, flag_dict)
|
2023-12-13 08:24:43 +00:00
|
|
|
else:
|
|
|
|
extracted_data[key] = data
|
|
|
|
|
|
|
|
buffer_position += item_length
|
2023-11-26 09:25:13 +00:00
|
|
|
|
|
|
|
return extracted_data
|
|
|
|
|
2023-12-13 08:56:58 +00:00
|
|
|
def get_bytes_per_frame(self, mode: codec2.FREEDV_MODE) -> int:
|
|
|
|
freedv = codec2.open_instance(mode.value)
|
2023-12-08 10:42:38 +00:00
|
|
|
bytes_per_frame = int(codec2.api.freedv_get_bits_per_modem_frame(freedv) / 8)
|
|
|
|
return bytes_per_frame
|
2023-12-13 15:56:11 +00:00
|
|
|
|
|
|
|
def get_available_data_payload_for_mode(self, type: FR_TYPE, mode:codec2.FREEDV_MODE):
|
|
|
|
whole_frame_length = self.get_bytes_per_frame(mode)
|
2023-12-16 16:21:07 +00:00
|
|
|
available = whole_frame_length - 2 # 2Bytes CRC16
|
|
|
|
available -= 1 # Frame Type
|
2023-12-13 15:56:11 +00:00
|
|
|
for field, length in self.template_list[type.value].items():
|
|
|
|
if field != 'frame_length' and isinstance(length, int):
|
|
|
|
available -= length
|
|
|
|
return available
|
2023-12-08 10:42:38 +00:00
|
|
|
|
2023-11-28 19:34:33 +00:00
|
|
|
def build_ping(self, destination):
|
2023-11-26 09:25:13 +00:00
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"destination_crc": helpers.get_crc_24(destination),
|
|
|
|
"origin_crc": helpers.get_crc_24(self.myfullcall),
|
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-26 09:25:13 +00:00
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.PING, payload)
|
2023-11-29 23:15:16 +00:00
|
|
|
|
|
|
|
def build_ping_ack(self, destination, snr):
|
|
|
|
payload = {
|
|
|
|
"destination_crc": helpers.get_crc_24(destination),
|
|
|
|
"origin_crc": helpers.get_crc_24(self.myfullcall),
|
|
|
|
"gridsquare": helpers.encode_grid(self.mygrid),
|
|
|
|
"snr": helpers.snr_to_bytes(snr)
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.PING_ACK, payload)
|
|
|
|
|
2023-11-23 15:11:47 +00:00
|
|
|
def build_cq(self):
|
2023-11-26 09:25:13 +00:00
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-26 09:25:13 +00:00
|
|
|
"gridsquare": helpers.encode_grid(self.mygrid)
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.CQ, payload)
|
2023-11-23 15:11:47 +00:00
|
|
|
|
2023-11-24 09:46:51 +00:00
|
|
|
def build_qrv(self, snr):
|
2023-11-26 09:25:13 +00:00
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-26 09:25:13 +00:00
|
|
|
"gridsquare": helpers.encode_grid(self.mygrid),
|
|
|
|
"snr": helpers.snr_to_bytes(snr)
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.QRV, payload)
|
|
|
|
|
2023-11-23 15:11:47 +00:00
|
|
|
def build_beacon(self):
|
2023-11-26 09:25:13 +00:00
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-26 09:25:13 +00:00
|
|
|
"gridsquare": helpers.encode_grid(self.mygrid)
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.BEACON, payload)
|
|
|
|
|
|
|
|
def build_fec_is_writing(self):
|
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-26 09:25:13 +00:00
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.IS_WRITING, payload)
|
|
|
|
|
2023-11-25 22:14:49 +00:00
|
|
|
def build_fec_wakeup(self, mode):
|
2023-11-25 22:22:31 +00:00
|
|
|
mode_int = codec2.freedv_get_mode_value_by_name(mode)
|
2023-11-23 15:11:47 +00:00
|
|
|
|
2023-11-26 09:25:13 +00:00
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-26 09:25:13 +00:00
|
|
|
"mode": bytes([mode_int]),
|
|
|
|
"n_bursts": bytes([1]) # n payload bursts,
|
|
|
|
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.FEC_WAKEUP, payload)
|
2023-12-08 09:25:26 +00:00
|
|
|
|
2023-11-25 22:22:31 +00:00
|
|
|
def build_fec(self, mode, payload):
|
|
|
|
mode_int = codec2.freedv_get_mode_value_by_name(mode)
|
|
|
|
payload_per_frame = codec2.get_bytes_per_frame(mode_int) - 2
|
|
|
|
fec_payload_length = payload_per_frame - 1
|
2023-11-23 15:11:47 +00:00
|
|
|
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
|
2023-11-25 23:11:39 +00:00
|
|
|
|
|
|
|
def build_test(self):
|
|
|
|
test_frame = bytearray(126)
|
|
|
|
test_frame[:1] = bytes([FR_TYPE.TEST_FRAME.value])
|
2023-11-26 13:25:14 +00:00
|
|
|
return test_frame
|
2023-11-27 22:08:00 +00:00
|
|
|
|
2024-03-24 20:39:44 +00:00
|
|
|
def build_arq_session_open(self, destination, session_id, maximum_bandwidth):
|
2023-11-27 22:08:00 +00:00
|
|
|
payload = {
|
2023-11-28 19:34:33 +00:00
|
|
|
"destination_crc": helpers.get_crc_24(destination),
|
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
2023-11-27 22:08:00 +00:00
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
2024-03-24 20:39:44 +00:00
|
|
|
"maximum_bandwidth": maximum_bandwidth.to_bytes(2, 'big'),
|
2023-11-27 22:08:00 +00:00
|
|
|
}
|
2023-12-12 20:20:03 +00:00
|
|
|
return self.construct(FR_TYPE.ARQ_SESSION_OPEN, payload)
|
2023-11-30 19:35:07 +00:00
|
|
|
|
2024-01-04 09:01:29 +00:00
|
|
|
def build_arq_session_open_ack(self, session_id, destination, version, snr, flag_abort=False):
|
|
|
|
flag = 0b00000000
|
|
|
|
if flag_abort:
|
|
|
|
flag = helpers.set_flag(flag, 'ABORT', True, self.ARQ_FLAGS)
|
2023-12-20 15:43:08 +00:00
|
|
|
|
2023-11-30 19:35:07 +00:00
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
2023-12-12 20:20:03 +00:00
|
|
|
"origin": helpers.callsign_to_bytes(self.myfullcall),
|
|
|
|
"destination_crc": helpers.get_crc_24(destination),
|
|
|
|
"version": bytes([version]),
|
2023-12-21 14:05:22 +00:00
|
|
|
"snr": helpers.snr_to_bytes(1),
|
2024-01-04 09:01:29 +00:00
|
|
|
"flag": flag.to_bytes(1, 'big'),
|
2023-12-21 14:05:22 +00:00
|
|
|
}
|
2023-12-12 20:20:03 +00:00
|
|
|
return self.construct(FR_TYPE.ARQ_SESSION_OPEN_ACK, payload)
|
|
|
|
|
2024-01-21 19:34:01 +00:00
|
|
|
def build_arq_session_info(self, session_id: int, total_length: int, total_crc: bytes, snr, type):
|
2023-12-25 11:27:48 +00:00
|
|
|
flag = 0b00000000
|
|
|
|
|
2023-12-12 20:20:03 +00:00
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
"total_length": total_length.to_bytes(4, 'big'),
|
|
|
|
"total_crc": total_crc,
|
2023-12-21 14:05:22 +00:00
|
|
|
"snr": helpers.snr_to_bytes(1),
|
2023-12-25 11:27:48 +00:00
|
|
|
"flag": flag.to_bytes(1, 'big'),
|
2024-01-21 19:34:01 +00:00
|
|
|
"type": type.to_bytes(1, 'big'),
|
2023-12-25 11:27:48 +00:00
|
|
|
|
2023-12-12 20:20:03 +00:00
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.ARQ_SESSION_INFO, payload)
|
2023-11-30 19:35:07 +00:00
|
|
|
|
2023-12-21 14:05:22 +00:00
|
|
|
def build_arq_stop(self, session_id: int):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.ARQ_STOP, payload)
|
|
|
|
|
|
|
|
def build_arq_stop_ack(self, session_id: int):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.ARQ_STOP_ACK, payload)
|
|
|
|
|
2023-12-28 21:27:49 +00:00
|
|
|
def build_arq_session_info_ack(self, session_id, total_crc, snr, speed_level, frames_per_burst, flag_final=False, flag_abort=False):
|
2023-12-20 18:30:38 +00:00
|
|
|
flag = 0b00000000
|
|
|
|
if flag_final:
|
|
|
|
flag = helpers.set_flag(flag, 'FINAL', True, self.ARQ_FLAGS)
|
2023-12-28 21:27:49 +00:00
|
|
|
if flag_abort:
|
|
|
|
flag = helpers.set_flag(flag, 'ABORT', True, self.ARQ_FLAGS)
|
2023-12-20 18:30:38 +00:00
|
|
|
|
2023-12-12 20:20:03 +00:00
|
|
|
payload = {
|
|
|
|
"frame_length": self.LENGTH_SIG0_FRAME,
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
2023-12-13 17:27:55 +00:00
|
|
|
"total_crc": bytes.fromhex(total_crc),
|
2023-12-21 14:05:22 +00:00
|
|
|
"snr": helpers.snr_to_bytes(1),
|
2023-12-12 20:20:03 +00:00
|
|
|
"speed_level": speed_level.to_bytes(1, 'big'),
|
|
|
|
"frames_per_burst": frames_per_burst.to_bytes(1, 'big'),
|
2023-12-20 18:30:38 +00:00
|
|
|
"flag": flag.to_bytes(1, 'big'),
|
2023-12-12 20:20:03 +00:00
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.ARQ_SESSION_INFO_ACK, payload)
|
|
|
|
|
2024-02-22 20:15:43 +00:00
|
|
|
def build_arq_burst_frame(self, freedv_mode: codec2.FREEDV_MODE, session_id: int, offset: int, data: bytes, speed_level: int):
|
2023-12-08 10:42:38 +00:00
|
|
|
payload = {
|
2023-12-09 11:31:08 +00:00
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
2024-02-23 07:59:03 +00:00
|
|
|
"speed_level": speed_level.to_bytes(1, 'big'),
|
2023-12-12 20:33:57 +00:00
|
|
|
"offset": offset.to_bytes(4, 'big'),
|
|
|
|
"data": data,
|
2023-12-08 10:42:38 +00:00
|
|
|
}
|
2024-03-09 09:47:27 +00:00
|
|
|
return self.construct(
|
|
|
|
FR_TYPE.ARQ_BURST_FRAME, payload, self.get_bytes_per_frame(freedv_mode)
|
|
|
|
)
|
2023-12-08 10:42:38 +00:00
|
|
|
|
2024-03-29 13:39:55 +00:00
|
|
|
def build_arq_burst_ack(self, session_id: bytes, speed_level: int, flag_final=False, flag_checksum=False, flag_abort=False):
|
2023-12-20 15:43:08 +00:00
|
|
|
flag = 0b00000000
|
|
|
|
if flag_final:
|
|
|
|
flag = helpers.set_flag(flag, 'FINAL', True, self.ARQ_FLAGS)
|
|
|
|
|
|
|
|
if flag_checksum:
|
|
|
|
flag = helpers.set_flag(flag, 'CHECKSUM', True, self.ARQ_FLAGS)
|
|
|
|
|
2023-12-28 21:47:17 +00:00
|
|
|
if flag_abort:
|
|
|
|
flag = helpers.set_flag(flag, 'ABORT', True, self.ARQ_FLAGS)
|
|
|
|
|
2023-11-28 19:34:33 +00:00
|
|
|
payload = {
|
2023-12-13 13:59:22 +00:00
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
2023-12-12 20:33:57 +00:00
|
|
|
"speed_level": speed_level.to_bytes(1, 'big'),
|
2023-12-20 15:43:08 +00:00
|
|
|
"flag": flag.to_bytes(1, 'big'),
|
2023-11-28 19:34:33 +00:00
|
|
|
}
|
2023-12-14 08:07:46 +00:00
|
|
|
return self.construct(FR_TYPE.ARQ_BURST_ACK, payload)
|
2024-03-09 09:47:27 +00:00
|
|
|
|
|
|
|
def build_p2p_connection_connect(self, destination, origin, session_id):
|
|
|
|
payload = {
|
|
|
|
"destination_crc": helpers.get_crc_24(destination),
|
|
|
|
"origin": helpers.callsign_to_bytes(origin),
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_CONNECT, payload)
|
|
|
|
|
|
|
|
def build_p2p_connection_connect_ack(self, destination, origin, session_id):
|
|
|
|
payload = {
|
|
|
|
"destination_crc": helpers.get_crc_24(destination),
|
|
|
|
"origin": helpers.callsign_to_bytes(origin),
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_CONNECT_ACK, payload)
|
|
|
|
|
|
|
|
def build_p2p_connection_heartbeat(self, session_id):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_HEARTBEAT, payload)
|
|
|
|
|
|
|
|
def build_p2p_connection_heartbeat_ack(self, session_id):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_HEARTBEAT_ACK, payload)
|
|
|
|
|
|
|
|
def build_p2p_connection_payload(self, freedv_mode: codec2.FREEDV_MODE, session_id: int, sequence_id: int, data: bytes):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
"sequence_id": sequence_id.to_bytes(1, 'big'),
|
|
|
|
"data": data,
|
|
|
|
}
|
|
|
|
return self.construct(
|
|
|
|
FR_TYPE.P2P_CONNECTION_PAYLOAD,
|
|
|
|
payload,
|
|
|
|
self.get_bytes_per_frame(freedv_mode),
|
|
|
|
)
|
|
|
|
|
|
|
|
def build_p2p_connection_payload_ack(self, session_id, sequence_id):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
"sequence_id": sequence_id.to_bytes(1, 'big'),
|
|
|
|
}
|
2024-03-15 13:25:46 +00:00
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_PAYLOAD_ACK, payload)
|
|
|
|
|
|
|
|
def build_p2p_connection_disconnect(self, session_id):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_DISCONNECT, payload)
|
|
|
|
|
|
|
|
def build_p2p_connection_disconnect_ack(self, session_id):
|
|
|
|
payload = {
|
|
|
|
"session_id": session_id.to_bytes(1, 'big'),
|
|
|
|
}
|
|
|
|
return self.construct(FR_TYPE.P2P_CONNECTION_DISCONNECT_ACK, payload)
|