FreeDATA/modem/arq_session_iss.py

113 lines
4.1 KiB
Python
Raw Normal View History

2023-12-05 14:40:04 +00:00
import threading
import data_frame_factory
import queue
import random
2023-12-05 17:50:39 +00:00
from codec2 import FREEDV_MODE
2023-12-14 16:29:04 +00:00
from modem_frametypes import FRAME_TYPE
2023-12-05 17:50:39 +00:00
import arq_session
2023-12-12 21:05:32 +00:00
import helpers
2023-12-05 14:40:04 +00:00
2023-12-05 17:50:39 +00:00
class ARQSessionISS(arq_session.ARQSession):
2023-12-05 14:40:04 +00:00
2023-12-14 16:29:04 +00:00
STATE_NEW = 0
STATE_OPEN_SENT = 1
STATE_INFO_SENT = 2
STATE_BURST_SENT = 3
STATE_ENDED = 4
STATE_FAILED = 5
2023-12-05 14:40:04 +00:00
RETRIES_CONNECT = 3
2023-12-15 15:22:38 +00:00
TIMEOUT_CONNECT_ACK = 7
2023-12-15 16:01:03 +00:00
TIMEOUT_TRANSFER = 15
2023-12-05 14:40:04 +00:00
2023-12-14 16:29:04 +00:00
STATE_TRANSITION = {
STATE_OPEN_SENT: {
FRAME_TYPE.ARQ_SESSION_OPEN_ACK.value: 'send_info',
},
STATE_INFO_SENT: {
FRAME_TYPE.ARQ_SESSION_OPEN_ACK.value: 'send_info',
FRAME_TYPE.ARQ_SESSION_INFO_ACK.value: 'send_data',
},
STATE_BURST_SENT: {
FRAME_TYPE.ARQ_SESSION_INFO_ACK.value: 'send_data',
FRAME_TYPE.ARQ_BURST_ACK.value: 'send_data',
FRAME_TYPE.ARQ_BURST_NACK.value: 'send_data',
},
}
def __init__(self, config: dict, modem, dxcall: str, data: bytearray):
super().__init__(config, modem, dxcall)
2023-12-05 14:40:04 +00:00
self.data = data
2023-12-14 16:29:04 +00:00
self.data_crc = ''
2023-12-05 14:40:04 +00:00
2023-12-14 16:29:04 +00:00
self.confirmed_bytes = 0
2023-12-05 14:40:04 +00:00
2023-12-14 16:29:04 +00:00
self.state = self.STATE_NEW
self.id = self.generate_id()
2023-12-05 14:40:04 +00:00
self.frame_factory = data_frame_factory.DataFrameFactory(self.config)
def generate_id(self):
return random.randint(1,255)
2023-12-15 13:41:11 +00:00
def transmit_wait_and_retry(self, frame_or_burst, timeout, retries, mode):
2023-12-05 14:40:04 +00:00
while retries > 0:
2023-12-15 15:40:05 +00:00
self.event_frame_received = threading.Event()
2023-12-14 16:29:04 +00:00
if isinstance(frame_or_burst, list): burst = frame_or_burst
else: burst = [frame_or_burst]
for f in burst:
2023-12-15 13:41:11 +00:00
self.transmit_frame(f, mode)
2023-12-14 16:57:58 +00:00
self.log(f"Waiting {timeout} seconds...")
2023-12-14 16:29:04 +00:00
if self.event_frame_received.wait(timeout):
2023-12-14 16:57:58 +00:00
return
self.log("Timeout!")
2023-12-05 14:40:04 +00:00
retries = retries - 1
2023-12-14 16:41:57 +00:00
self.set_state(self.STATE_FAILED)
self.log("Session failed")
2023-12-05 14:40:04 +00:00
2023-12-15 13:41:11 +00:00
def launch_twr(self, frame_or_burst, timeout, retries, mode):
twr = threading.Thread(target = self.transmit_wait_and_retry, args=[frame_or_burst, timeout, retries, mode])
2023-12-14 16:29:04 +00:00
twr.start()
2023-12-05 14:40:04 +00:00
2023-12-14 16:29:04 +00:00
def start(self):
session_open_frame = self.frame_factory.build_arq_session_open(self.dxcall, self.id)
2023-12-16 09:28:30 +00:00
self.launch_twr(session_open_frame, self.TIMEOUT_CONNECT_ACK, self.RETRIES_CONNECT, mode=FREEDV_MODE.signalling)
2023-12-14 16:29:04 +00:00
self.set_state(self.STATE_OPEN_SENT)
2023-12-12 21:05:32 +00:00
2023-12-14 16:29:04 +00:00
def set_speed_and_frames_per_burst(self, frame):
self.speed_level = frame['speed_level']
self.log(f"Speed level set to {self.speed_level}")
self.frames_per_burst = frame['frames_per_burst']
self.log(f"Frames per burst set to {self.frames_per_burst}")
2023-12-15 15:22:38 +00:00
def send_info(self, frame):
2023-12-12 21:05:32 +00:00
info_frame = self.frame_factory.build_arq_session_info(self.id, len(self.data),
helpers.get_crc_32(self.data),
2023-12-14 16:29:04 +00:00
self.snr[0])
2023-12-16 09:28:30 +00:00
self.launch_twr(info_frame, self.TIMEOUT_CONNECT_ACK, self.RETRIES_CONNECT, mode=FREEDV_MODE.signalling)
2023-12-14 16:29:04 +00:00
self.set_state(self.STATE_INFO_SENT)
2023-12-12 21:05:32 +00:00
2023-12-14 16:29:04 +00:00
def send_data(self, irs_frame):
self.set_speed_and_frames_per_burst(irs_frame)
2023-12-05 14:40:04 +00:00
2023-12-14 16:29:04 +00:00
if 'offset' in irs_frame:
self.confirmed_bytes = irs_frame['offset']
2023-12-05 14:40:04 +00:00
2023-12-14 16:33:43 +00:00
if self.confirmed_bytes == len(self.data):
self.set_state(self.STATE_ENDED)
self.log("All data transfered!")
return
2023-12-15 15:22:38 +00:00
print(self.SPEED_LEVEL_DICT[self.speed_level])
2023-12-14 16:29:04 +00:00
payload_size = self.get_data_payload_size()
burst = []
for f in range(0, self.frames_per_burst):
offset = self.confirmed_bytes
payload = self.data[offset : offset + payload_size]
2023-12-12 21:33:17 +00:00
data_frame = self.frame_factory.build_arq_burst_frame(
2023-12-14 21:53:32 +00:00
self.SPEED_LEVEL_DICT[self.speed_level]["mode"],
2023-12-14 16:29:04 +00:00
self.id, self.confirmed_bytes, payload)
burst.append(data_frame)
2023-12-14 08:19:46 +00:00
2023-12-15 15:22:38 +00:00
self.launch_twr(burst, self.TIMEOUT_TRANSFER, self.RETRIES_CONNECT, mode='auto')
2023-12-14 16:29:04 +00:00
self.set_state(self.STATE_BURST_SENT)