2023-12-05 14:40:04 +00:00
|
|
|
import threading
|
2023-12-05 18:01:48 +00:00
|
|
|
import arq_session
|
2023-12-13 22:07:06 +00:00
|
|
|
import helpers
|
2023-12-14 16:29:04 +00:00
|
|
|
from modem_frametypes import FRAME_TYPE
|
2023-12-15 13:41:11 +00:00
|
|
|
from codec2 import FREEDV_MODE
|
2023-12-19 14:01:08 +00:00
|
|
|
from enum import Enum
|
2023-12-15 13:41:11 +00:00
|
|
|
|
2023-12-19 14:01:08 +00:00
|
|
|
class IRS_State(Enum):
|
|
|
|
NEW = 0
|
|
|
|
OPEN_ACK_SENT = 1
|
|
|
|
INFO_ACK_SENT = 2
|
|
|
|
BURST_REPLY_SENT = 3
|
|
|
|
ENDED = 4
|
|
|
|
FAILED = 5
|
2023-12-05 14:40:04 +00:00
|
|
|
|
2023-12-05 18:01:48 +00:00
|
|
|
class ARQSessionIRS(arq_session.ARQSession):
|
2023-12-05 14:40:04 +00:00
|
|
|
|
|
|
|
RETRIES_CONNECT = 3
|
2023-12-13 22:37:40 +00:00
|
|
|
RETRIES_TRANSFER = 3 # we need to increase this
|
2023-12-05 14:40:04 +00:00
|
|
|
|
2023-12-18 12:56:49 +00:00
|
|
|
TIMEOUT_CONNECT = 3
|
|
|
|
TIMEOUT_DATA = 12
|
2023-12-05 14:40:04 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
STATE_TRANSITION = {
|
2023-12-19 14:01:08 +00:00
|
|
|
IRS_State.NEW: {
|
2023-12-14 16:29:04 +00:00
|
|
|
FRAME_TYPE.ARQ_SESSION_OPEN.value : 'send_open_ack',
|
|
|
|
},
|
2023-12-19 14:01:08 +00:00
|
|
|
IRS_State.OPEN_ACK_SENT: {
|
2023-12-14 16:29:04 +00:00
|
|
|
FRAME_TYPE.ARQ_SESSION_OPEN.value: 'send_open_ack',
|
|
|
|
FRAME_TYPE.ARQ_SESSION_INFO.value: 'send_info_ack',
|
|
|
|
},
|
2023-12-19 14:01:08 +00:00
|
|
|
IRS_State.INFO_ACK_SENT: {
|
2023-12-14 16:29:04 +00:00
|
|
|
FRAME_TYPE.ARQ_SESSION_INFO.value: 'send_info_ack',
|
|
|
|
FRAME_TYPE.ARQ_BURST_FRAME.value: 'receive_data',
|
|
|
|
},
|
2023-12-19 14:01:08 +00:00
|
|
|
IRS_State.BURST_REPLY_SENT: {
|
2023-12-14 16:29:04 +00:00
|
|
|
FRAME_TYPE.ARQ_BURST_FRAME.value: 'receive_data',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-15 23:51:57 +00:00
|
|
|
def __init__(self, config: dict, modem, dxcall: str, session_id: int):
|
|
|
|
super().__init__(config, modem, dxcall)
|
2023-12-05 18:01:48 +00:00
|
|
|
|
2023-12-05 17:50:39 +00:00
|
|
|
self.id = session_id
|
2023-12-14 16:29:04 +00:00
|
|
|
self.dxcall = dxcall
|
2023-12-11 18:02:50 +00:00
|
|
|
self.version = 1
|
2023-12-05 14:40:04 +00:00
|
|
|
|
2023-12-19 14:01:08 +00:00
|
|
|
self.state = IRS_State.NEW
|
2023-12-05 14:40:04 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
self.total_length = 0
|
|
|
|
self.total_crc = ''
|
2023-12-13 13:33:09 +00:00
|
|
|
self.received_data = None
|
|
|
|
self.received_bytes = 0
|
|
|
|
self.received_crc = None
|
|
|
|
|
2023-12-14 21:53:32 +00:00
|
|
|
self.transmitted_acks = 0
|
|
|
|
|
2023-12-17 00:43:23 +00:00
|
|
|
def set_decode_mode(self):
|
|
|
|
self.modem.demodulator.set_decode_mode(self.get_mode_by_speed_level(self.speed_level))
|
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
def all_data_received(self):
|
2023-12-16 16:42:51 +00:00
|
|
|
return self.total_length == self.received_bytes
|
2023-12-13 13:33:09 +00:00
|
|
|
|
2023-12-18 12:56:49 +00:00
|
|
|
def final_crc_matches(self) -> bool:
|
|
|
|
match = self.total_crc == helpers.get_crc_32(bytes(self.received_data)).hex()
|
|
|
|
return match
|
2023-12-13 22:07:06 +00:00
|
|
|
|
2023-12-15 13:56:35 +00:00
|
|
|
def transmit_and_wait(self, frame, timeout, mode):
|
|
|
|
self.transmit_frame(frame, mode)
|
2023-12-14 16:57:58 +00:00
|
|
|
self.log(f"Waiting {timeout} seconds...")
|
2023-12-17 11:54:25 +00:00
|
|
|
if not self.event_frame_received.wait(timeout):
|
|
|
|
# use case: data burst got lost, we want to send a NACK with updated speed level
|
2023-12-19 14:01:08 +00:00
|
|
|
if self.state in [IRS_State.BURST_REPLY_SENT, IRS_State.INFO_ACK_SENT]:
|
2023-12-14 21:53:32 +00:00
|
|
|
self.transmitted_acks = 0
|
|
|
|
self.calibrate_speed_settings()
|
|
|
|
self.send_burst_nack()
|
|
|
|
return
|
2023-12-17 11:54:25 +00:00
|
|
|
|
2023-12-14 16:57:58 +00:00
|
|
|
self.log("Timeout waiting for ISS. Session failed.")
|
2023-12-19 14:01:08 +00:00
|
|
|
self.set_state(IRS_State.FAILED)
|
2023-12-19 14:35:07 +00:00
|
|
|
self.event_manager.send_arq_finished(False, self.id, self.dxcall, self.total_length, False)
|
2023-12-13 13:33:09 +00:00
|
|
|
|
2023-12-15 13:41:11 +00:00
|
|
|
def launch_transmit_and_wait(self, frame, timeout, mode):
|
2023-12-14 16:29:04 +00:00
|
|
|
thread_wait = threading.Thread(target = self.transmit_and_wait,
|
2023-12-15 13:41:11 +00:00
|
|
|
args = [frame, timeout, mode])
|
2023-12-14 16:29:04 +00:00
|
|
|
thread_wait.start()
|
|
|
|
|
|
|
|
def send_open_ack(self, open_frame):
|
|
|
|
ack_frame = self.frame_factory.build_arq_session_open_ack(
|
|
|
|
self.id,
|
|
|
|
self.dxcall,
|
|
|
|
self.version,
|
|
|
|
self.snr[0])
|
2023-12-16 09:28:30 +00:00
|
|
|
self.launch_transmit_and_wait(ack_frame, self.TIMEOUT_CONNECT, mode=FREEDV_MODE.signalling)
|
2023-12-19 14:01:08 +00:00
|
|
|
self.set_state(IRS_State.OPEN_ACK_SENT)
|
2023-12-14 16:29:04 +00:00
|
|
|
|
|
|
|
def send_info_ack(self, info_frame):
|
|
|
|
# Get session info from ISS
|
|
|
|
self.received_data = bytearray(info_frame['total_length'])
|
2023-12-16 16:42:51 +00:00
|
|
|
self.total_length = info_frame['total_length']
|
2023-12-14 16:29:04 +00:00
|
|
|
self.total_crc = info_frame['total_crc']
|
|
|
|
self.dx_snr.append(info_frame['snr'])
|
|
|
|
|
2023-12-18 12:56:49 +00:00
|
|
|
self.log(f"New transfer of {self.total_length} bytes")
|
2023-12-19 14:35:07 +00:00
|
|
|
self.event_manager.send_arq_session_new(False, self.id, self.dxcall, self.total_length)
|
2023-12-18 12:56:49 +00:00
|
|
|
|
2023-12-15 13:41:11 +00:00
|
|
|
self.calibrate_speed_settings()
|
2023-12-17 00:43:23 +00:00
|
|
|
self.set_decode_mode()
|
2023-12-14 16:29:04 +00:00
|
|
|
info_ack = self.frame_factory.build_arq_session_info_ack(
|
|
|
|
self.id, self.total_crc, self.snr[0],
|
|
|
|
self.speed_level, self.frames_per_burst)
|
2023-12-16 09:28:30 +00:00
|
|
|
self.launch_transmit_and_wait(info_ack, self.TIMEOUT_CONNECT, mode=FREEDV_MODE.signalling)
|
2023-12-19 14:01:08 +00:00
|
|
|
self.set_state(IRS_State.INFO_ACK_SENT)
|
2023-12-14 16:29:04 +00:00
|
|
|
|
2023-12-14 21:53:32 +00:00
|
|
|
def send_burst_nack(self):
|
|
|
|
self.calibrate_speed_settings()
|
2023-12-17 00:43:23 +00:00
|
|
|
self.set_decode_mode()
|
2023-12-14 21:53:32 +00:00
|
|
|
nack = self.frame_factory.build_arq_burst_ack(self.id, self.received_bytes, self.speed_level, self.frames_per_burst, self.snr[0])
|
2023-12-17 11:54:25 +00:00
|
|
|
self.transmit_frame(nack, mode=FREEDV_MODE.signalling)
|
2023-12-15 15:22:38 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
def process_incoming_data(self, frame):
|
|
|
|
if frame['offset'] != self.received_bytes:
|
2023-12-18 12:56:49 +00:00
|
|
|
self.log(f"Discarding data offset {frame['offset']}")
|
2023-12-13 13:33:09 +00:00
|
|
|
return False
|
|
|
|
|
2023-12-16 17:04:04 +00:00
|
|
|
remaining_data_length = self.total_length - self.received_bytes
|
2023-12-13 22:07:06 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
# Is this the last data part?
|
|
|
|
if remaining_data_length <= len(frame['data']):
|
|
|
|
# we only want the remaining length, not the entire frame data
|
|
|
|
data_part = frame['data'][:remaining_data_length]
|
2023-12-13 22:07:06 +00:00
|
|
|
else:
|
2023-12-14 16:29:04 +00:00
|
|
|
# we want the entire frame data
|
|
|
|
data_part = frame['data']
|
2023-12-13 21:25:22 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
self.received_data[frame['offset']:] = data_part
|
|
|
|
self.received_bytes += len(data_part)
|
2023-12-18 12:56:49 +00:00
|
|
|
self.log(f"Received {self.received_bytes}/{self.total_length} bytes")
|
2023-12-19 14:35:07 +00:00
|
|
|
self.event_manager.send_arq_session_progress(
|
|
|
|
False, self.id, self.dxcall, self.received_bytes, self.total_length)
|
2023-12-13 21:25:22 +00:00
|
|
|
|
2023-12-13 15:56:11 +00:00
|
|
|
return True
|
2023-12-09 13:16:53 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
def receive_data(self, burst_frame):
|
|
|
|
self.process_incoming_data(burst_frame)
|
2023-12-14 21:53:32 +00:00
|
|
|
self.calibrate_speed_settings()
|
2023-12-14 16:29:04 +00:00
|
|
|
ack = self.frame_factory.build_arq_burst_ack(
|
2023-12-13 13:59:22 +00:00
|
|
|
self.id, self.received_bytes,
|
2023-12-14 16:29:04 +00:00
|
|
|
self.speed_level, self.frames_per_burst, self.snr[0])
|
2023-12-11 18:02:50 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
if not self.all_data_received():
|
2023-12-14 21:53:32 +00:00
|
|
|
# increase ack counter
|
|
|
|
self.transmitted_acks += 1
|
2023-12-17 11:54:25 +00:00
|
|
|
self.transmit_frame(ack, mode=FREEDV_MODE.signalling)
|
2023-12-19 14:01:08 +00:00
|
|
|
self.set_state(IRS_State.BURST_REPLY_SENT)
|
2023-12-14 16:29:04 +00:00
|
|
|
return
|
2023-12-14 08:07:46 +00:00
|
|
|
|
2023-12-18 12:56:49 +00:00
|
|
|
if self.final_crc_matches():
|
2023-12-14 16:29:04 +00:00
|
|
|
self.log("All data received successfully!")
|
2023-12-17 11:54:25 +00:00
|
|
|
self.transmit_frame(ack, mode=FREEDV_MODE.signalling)
|
2023-12-19 14:01:08 +00:00
|
|
|
self.set_state(IRS_State.ENDED)
|
2023-12-19 14:35:07 +00:00
|
|
|
self.event_manager.send_arq_session_finished(
|
|
|
|
False, self.id, self.dxcall, self.total_length, True)
|
2023-12-14 08:07:46 +00:00
|
|
|
|
2023-12-14 16:29:04 +00:00
|
|
|
else:
|
|
|
|
self.log("CRC fail at the end of transmission!")
|
2023-12-19 14:01:08 +00:00
|
|
|
self.set_state(IRS_State.FAILED)
|
2023-12-19 14:35:07 +00:00
|
|
|
self.event_manager.send_arq_session_finished(
|
|
|
|
False, self.id, self.dxcall, self.total_length, False)
|
|
|
|
|
2023-12-14 08:07:46 +00:00
|
|
|
|
2023-12-12 21:33:17 +00:00
|
|
|
def calibrate_speed_settings(self):
|
2023-12-17 11:16:51 +00:00
|
|
|
self.speed_level = 0 # for now stay at lowest speed level
|
|
|
|
return
|
2023-12-14 21:53:32 +00:00
|
|
|
# if we have two ACKS, then consider increasing speed level
|
|
|
|
if self.transmitted_acks >= 2:
|
|
|
|
self.transmitted_acks = 0
|
|
|
|
new_speed_level = min(self.speed_level + 1, len(self.SPEED_LEVEL_DICT) - 1)
|
2023-12-13 22:37:40 +00:00
|
|
|
|
2023-12-14 21:53:32 +00:00
|
|
|
# check first if the next mode supports the actual snr
|
2023-12-15 16:01:03 +00:00
|
|
|
if self.snr[0] >= self.SPEED_LEVEL_DICT[new_speed_level]["min_snr"]:
|
2023-12-14 21:53:32 +00:00
|
|
|
self.speed_level = new_speed_level
|
2023-12-13 22:37:40 +00:00
|
|
|
|