FreeDATA/modem/arq_session_iss.py

128 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
import arq_session
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
STATE_DISCONNECTED = 0
STATE_CONNECTING = 1
STATE_CONNECTED = 2
STATE_SENDING = 3
STATE_ENDED = 10
RETRIES_CONNECT = 3
RETRIES_TRANSFER = 3
TIMEOUT_CONNECT_ACK = 5
TIMEOUT_TRANSFER = 2
def __init__(self, config: dict, tx_frame_queue: queue.Queue, dxcall: str, data: bytearray):
2023-12-05 17:50:39 +00:00
super().__init__(config, tx_frame_queue, dxcall)
2023-12-05 14:40:04 +00:00
self.data = data
self.state = self.STATE_DISCONNECTED
self.id = self.generate_id()
self.event_connection_ack_received = threading.Event()
self.event_transfer_ack_received = threading.Event()
self.frame_factory = data_frame_factory.DataFrameFactory(self.config)
def generate_id(self):
return random.randint(1,255)
def set_state(self, state):
2023-12-06 10:47:47 +00:00
self.logger.info(f"ARQ Session {self.id} state {self.state}")
2023-12-05 14:40:04 +00:00
self.state = state
def runner(self):
if not self.connect():
return False
return self.send_data()
def run(self):
self.thread = threading.Thread(target=self.runner, name=f"ARQ ISS Session {self.id}", daemon=True)
self.thread.run()
def connect(self):
2023-12-05 17:50:39 +00:00
self.state = self.STATE_CONNECTING
2023-12-05 14:40:04 +00:00
connect_frame = self.frame_factory.build_arq_session_connect(True, self.dxcall, self.id)
retries = self.RETRIES_CONNECT
while retries > 0:
self.transmit_frame(connect_frame)
2023-12-06 10:47:47 +00:00
self.logger.info("Waiting for CONN ACK...")
2023-12-05 14:40:04 +00:00
if self.event_connection_ack_received.wait(self.TIMEOUT_CONNECT_ACK):
self.setState(self.STATE_CONNECTED)
return True
retries = retries - 1
self.setState(self.STATE_DISCONNECTED)
return False
def on_connection_ack_received(self, ack):
if self.state != self.STATE_CONNECTING:
raise RuntimeError(f"ARQ Session: Received connection ACK while in state {self.state}")
self.speed_level = ack['speed_level']
self.event_connection_ack_received.set()
# Sends the full payload in multiple frames
def send_data(self):
2023-12-08 10:42:38 +00:00
# Todo make this n frames per burst stuff part of the protocol again
# hard coding n frames per burst to 1 for now.
n_frames_per_burst = 1
n_frame = 1
2023-12-05 14:40:04 +00:00
offset = 0
2023-12-05 18:12:21 +00:00
while offset < len(self.data):
2023-12-06 10:59:35 +00:00
max_size = self.get_payload_size(self.speed_level)
2023-12-05 18:12:21 +00:00
end_offset = min(len(self.data), max_size)
frame_payload = self.data[offset:end_offset]
2023-12-08 10:42:38 +00:00
data_frame = self.frame_factory.build_arq_data_frame(self.id, n_frames_per_burst, max_size, n_frame, frame_payload)
self.set_state(self.STATE_SENDING)
if not self.send_arq(data_frame):
return False
offset = end_offset + 1
2023-12-05 14:40:04 +00:00
# Send part of the payload using ARQ
def send_arq(self, frame):
retries = self.RETRIES_TRANSFER
while retries > 0:
# to know later if it has changed
speed_level = self.speed_level
self.transmit_frame(frame)
# wait for ack
if self.event_transfer_ack_received.wait(self.TIMEOUT_TRANSFER):
speed_level = self.speed_level
return True
# don't decrement retries if speed level is changing
if self.speed_level == speed_level:
retries = retries - 1
self.set_state(self.STATE_DISCONNECTED)
return False
def on_transfer_ack_received(self, ack):
self.event_transfer_ack_received.set()
self.speed_level = ack['speed_level']
def on_transfer_nack_received(self, nack):
self.speed_level = nack['speed_level']
def on_disconnect_received(self):
self.abort()
def abort(self):
self.state = self.STATE_DISCONNECTED
self.event_connection_ack_received.set()
self.event_connection_ack_received.clear()
self.event_transfer_feedback.set()
self.event_transfer_feedback.clear()