2022-05-23 00:54:12 +00:00
|
|
|
# -*- coding: UTF-8 -*-
|
2021-02-24 13:22:28 +00:00
|
|
|
"""
|
|
|
|
Created on Sun Dec 27 20:43:40 2020
|
|
|
|
|
|
|
|
@author: DJ2LS
|
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
|
2022-05-23 00:54:12 +00:00
|
|
|
# pylint: disable=import-outside-toplevel, attribute-defined-outside-init
|
2022-05-11 22:10:59 +00:00
|
|
|
|
|
|
|
import base64
|
2021-12-19 18:45:08 +00:00
|
|
|
import sys
|
2021-02-24 13:22:28 +00:00
|
|
|
import threading
|
|
|
|
import time
|
2022-05-11 22:10:59 +00:00
|
|
|
import uuid
|
2021-12-30 11:41:41 +00:00
|
|
|
import zlib
|
2022-05-11 22:10:59 +00:00
|
|
|
from random import randrange
|
|
|
|
|
2022-01-04 22:02:29 +00:00
|
|
|
import codec2
|
2022-05-11 22:10:59 +00:00
|
|
|
import helpers
|
|
|
|
import modem
|
2022-05-23 00:54:12 +00:00
|
|
|
import numpy as np
|
2022-01-28 19:07:39 +00:00
|
|
|
import sock
|
2022-05-11 22:10:59 +00:00
|
|
|
import static
|
2022-05-23 00:54:12 +00:00
|
|
|
import structlog
|
|
|
|
import ujson as json
|
2022-06-24 13:22:46 +00:00
|
|
|
from codec2 import FREEDV_MODE
|
2022-06-17 23:48:47 +00:00
|
|
|
from exceptions import NoCallsign
|
2022-09-05 08:37:50 +00:00
|
|
|
from queues import DATA_QUEUE_RECEIVED, DATA_QUEUE_TRANSMIT, RX_BUFFER
|
2022-06-21 22:33:55 +00:00
|
|
|
from static import FRAME_TYPE as FR_TYPE
|
2021-09-26 15:51:11 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
TESTMODE = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-09 01:27:24 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
class DATA:
|
2022-05-23 00:54:12 +00:00
|
|
|
"""Terminal Node Controller for FreeDATA"""
|
|
|
|
|
2022-05-28 02:17:15 +00:00
|
|
|
log = structlog.get_logger("DATA")
|
2022-05-19 20:15:24 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
# Initial call sign. Will be overwritten later
|
|
|
|
self.mycallsign = static.MYCALLSIGN
|
2022-01-04 15:34:20 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
self.data_queue_transmit = DATA_QUEUE_TRANSMIT
|
|
|
|
self.data_queue_received = DATA_QUEUE_RECEIVED
|
2022-01-04 15:34:20 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
# ------- ARQ SESSION
|
|
|
|
self.arq_file_transfer = False
|
|
|
|
self.IS_ARQ_SESSION_MASTER = False
|
|
|
|
self.arq_session_last_received = 0
|
|
|
|
self.arq_session_timeout = 30
|
|
|
|
self.session_connect_max_retries = 3
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.transmission_uuid = ""
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
self.data_channel_last_received = 0.0 # time of last "live sign" of a frame
|
2022-05-23 00:54:12 +00:00
|
|
|
self.burst_ack_snr = 0 # SNR from received burst ack frames
|
|
|
|
|
|
|
|
# Flag to indicate if we received an acknowledge frame for a burst
|
|
|
|
self.burst_ack = False
|
|
|
|
# Flag to indicate if we received an acknowledge frame for a data frame
|
|
|
|
self.data_frame_ack_received = False
|
|
|
|
# Flag to indicate if we received an request for repeater frames
|
|
|
|
self.rpt_request_received = False
|
2022-05-19 20:15:24 +00:00
|
|
|
self.rpt_request_buffer = [] # requested frames, saved in a list
|
|
|
|
self.rx_start_of_transmission = 0 # time of transmission start
|
2022-05-23 00:54:12 +00:00
|
|
|
|
|
|
|
# 3 bytes for the BOF Beginning of File indicator in a data frame
|
|
|
|
self.data_frame_bof = b"BOF"
|
|
|
|
# 3 bytes for the EOF End of File indicator in a data frame
|
|
|
|
self.data_frame_eof = b"EOF"
|
2021-09-26 15:51:11 +00:00
|
|
|
|
2022-04-02 12:54:43 +00:00
|
|
|
self.rx_n_max_retries_per_burst = 50
|
2022-02-08 14:27:34 +00:00
|
|
|
self.n_retries_per_burst = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 01:11:40 +00:00
|
|
|
# Flag to indicate if we recevied a low bandwidth mode channel opener
|
2022-05-28 15:52:05 +00:00
|
|
|
self.received_LOW_BANDWIDTH_MODE = False
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-03-10 19:46:34 +00:00
|
|
|
self.data_channel_max_retries = 5
|
2022-05-11 22:10:59 +00:00
|
|
|
self.datachannel_timeout = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
# List of codec2 modes to use in "low bandwidth" mode.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.mode_list_low_bw = [
|
2022-06-24 13:22:46 +00:00
|
|
|
FREEDV_MODE.datac3.value,
|
2022-05-23 00:54:12 +00:00
|
|
|
]
|
|
|
|
# List for time to wait for corresponding mode in seconds
|
2022-05-11 22:10:59 +00:00
|
|
|
self.time_list_low_bw = [3, 7]
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
# List of codec2 modes to use in "high bandwidth" mode.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.mode_list_high_bw = [
|
2022-06-24 13:22:46 +00:00
|
|
|
FREEDV_MODE.datac3.value,
|
|
|
|
FREEDV_MODE.datac1.value,
|
2022-05-23 00:54:12 +00:00
|
|
|
]
|
|
|
|
# List for time to wait for corresponding mode in seconds
|
|
|
|
self.time_list_high_bw = [3, 7, 8, 30]
|
|
|
|
|
|
|
|
# Mode list for selecting between low bandwidth ( 500Hz ) and modes with higher bandwidth
|
|
|
|
# but ability to fall back to low bandwidth modes if needed.
|
2022-05-28 12:08:33 +00:00
|
|
|
if static.LOW_BANDWIDTH_MODE:
|
2022-05-26 01:23:30 +00:00
|
|
|
# List of codec2 modes to use in "low bandwidth" mode.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.mode_list = self.mode_list_low_bw
|
|
|
|
# list of times to wait for corresponding mode in seconds
|
|
|
|
self.time_list = self.time_list_low_bw
|
2022-02-08 14:27:34 +00:00
|
|
|
|
|
|
|
else:
|
2022-05-26 01:23:30 +00:00
|
|
|
# List of codec2 modes to use in "high bandwidth" mode.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.mode_list = self.mode_list_high_bw
|
|
|
|
# list of times to wait for corresponding mode in seconds
|
|
|
|
self.time_list = self.time_list_high_bw
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
self.speed_level = len(self.mode_list) - 1 # speed level for selecting mode
|
2022-02-22 20:05:48 +00:00
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-02 20:12:16 +00:00
|
|
|
self.is_IRS = False
|
|
|
|
self.burst_nack = False
|
|
|
|
self.burst_nack_counter = 0
|
|
|
|
self.frame_received_counter = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:44:35 +00:00
|
|
|
self.rx_frame_bof_received = False
|
|
|
|
self.rx_frame_eof_received = False
|
2021-12-26 16:52:05 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
self.transmission_timeout = 360 # transmission timeout in seconds
|
2021-02-24 13:22:28 +00:00
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
# Dictionary of functions and log messages used in process_data
|
|
|
|
# instead of a long series of if-elif-else statements.
|
|
|
|
self.rx_dispatcher = {
|
|
|
|
FR_TYPE.ARQ_DC_OPEN_ACK_N.value: (
|
|
|
|
self.arq_received_channel_is_open,
|
|
|
|
"ARQ OPEN ACK (Narrow)",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_DC_OPEN_ACK_W.value: (
|
|
|
|
self.arq_received_channel_is_open,
|
|
|
|
"ARQ OPEN ACK (Wide)",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_DC_OPEN_N.value: (
|
|
|
|
self.arq_received_data_channel_opener,
|
|
|
|
"ARQ Data Channel Open (Narrow)",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_DC_OPEN_W.value: (
|
|
|
|
self.arq_received_data_channel_opener,
|
|
|
|
"ARQ Data Channel Open (Wide)",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_SESSION_CLOSE.value: (
|
|
|
|
self.received_session_close,
|
|
|
|
"ARQ CLOSE SESSION",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_SESSION_HB.value: (
|
|
|
|
self.received_session_heartbeat,
|
|
|
|
"ARQ HEARTBEAT",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_SESSION_OPEN.value: (
|
|
|
|
self.received_session_opener,
|
|
|
|
"ARQ OPEN SESSION",
|
|
|
|
),
|
|
|
|
FR_TYPE.ARQ_STOP.value: (self.received_stop_transmission, "ARQ STOP TX"),
|
|
|
|
FR_TYPE.BEACON.value: (self.received_beacon, "BEACON"),
|
|
|
|
FR_TYPE.BURST_ACK.value: (self.burst_ack_nack_received, "BURST ACK"),
|
|
|
|
FR_TYPE.BURST_NACK.value: (self.burst_ack_nack_received, "BURST NACK"),
|
|
|
|
FR_TYPE.CQ.value: (self.received_cq, "CQ"),
|
|
|
|
FR_TYPE.FR_ACK.value: (self.frame_ack_received, "FRAME ACK"),
|
|
|
|
FR_TYPE.FR_NACK.value: (self.frame_nack_received, "FRAME NACK"),
|
|
|
|
FR_TYPE.FR_REPEAT.value: (self.burst_rpt_received, "REPEAT REQUEST"),
|
|
|
|
FR_TYPE.PING_ACK.value: (self.received_ping_ack, "PING ACK"),
|
|
|
|
FR_TYPE.PING.value: (self.received_ping, "PING"),
|
|
|
|
FR_TYPE.QRV.value: (self.received_qrv, "QRV"),
|
|
|
|
}
|
|
|
|
self.command_dispatcher = {
|
|
|
|
"CONNECT": (self.arq_session_handler, "CONNECT"),
|
|
|
|
"CQ": (self.transmit_cq, "CQ"),
|
|
|
|
"DISCONNECT": (self.close_session, "DISCONNECT"),
|
|
|
|
"SEND_TEST_FRAME": (self.send_test_frame, "TEST"),
|
|
|
|
"STOP": (self.stop_transmission, "STOP"),
|
|
|
|
}
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Start worker and watchdog threads
|
|
|
|
worker_thread_transmit = threading.Thread(
|
|
|
|
target=self.worker_transmit, name="worker thread transmit", daemon=True
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
worker_thread_transmit.start()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
worker_thread_receive = threading.Thread(
|
|
|
|
target=self.worker_receive, name="worker thread receive", daemon=True
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
worker_thread_receive.start()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# START THE THREAD FOR THE TIMEOUT WATCHDOG
|
2022-05-23 00:54:12 +00:00
|
|
|
watchdog_thread = threading.Thread(
|
|
|
|
target=self.watchdog, name="watchdog", daemon=True
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
watchdog_thread.start()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
arq_session_thread = threading.Thread(
|
|
|
|
target=self.heartbeat, name="watchdog", daemon=True
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
arq_session_thread.start()
|
|
|
|
|
2022-03-10 19:46:34 +00:00
|
|
|
self.beacon_interval = 0
|
2022-05-23 00:54:12 +00:00
|
|
|
self.beacon_thread = threading.Thread(
|
|
|
|
target=self.run_beacon, name="watchdog", daemon=True
|
|
|
|
)
|
2022-03-10 19:46:34 +00:00
|
|
|
self.beacon_thread.start()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def worker_transmit(self) -> None:
|
|
|
|
"""Dispatch incoming UI instructions for transmitting operations"""
|
2022-03-04 15:50:32 +00:00
|
|
|
while True:
|
2022-01-07 10:25:28 +00:00
|
|
|
data = self.data_queue_transmit.get()
|
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
# Dispatch commands known to command_dispatcher
|
|
|
|
if data[0] in self.command_dispatcher:
|
|
|
|
self.log.debug(f"[TNC] TX {self.command_dispatcher[data[0]][1]}...")
|
|
|
|
self.command_dispatcher[data[0]][0]()
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
# Dispatch commands that need more arguments.
|
2022-05-23 00:54:12 +00:00
|
|
|
elif data[0] == "PING":
|
2022-01-07 10:25:28 +00:00
|
|
|
# [1] dxcallsign
|
|
|
|
self.transmit_ping(data[1])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
elif data[0] == "BEACON":
|
2022-01-07 10:25:28 +00:00
|
|
|
# [1] INTERVAL int
|
|
|
|
# [2] STATE bool
|
2022-03-10 19:46:34 +00:00
|
|
|
if data[2]:
|
|
|
|
self.beacon_interval = data[1]
|
|
|
|
static.BEACON_STATE = True
|
|
|
|
else:
|
|
|
|
static.BEACON_STATE = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
elif data[0] == "ARQ_RAW":
|
2022-01-28 19:07:39 +00:00
|
|
|
# [1] DATA_OUT bytes
|
|
|
|
# [2] MODE int
|
|
|
|
# [3] N_FRAMES_PER_BURST int
|
2022-03-14 19:21:15 +00:00
|
|
|
# [4] self.transmission_uuid str
|
2022-03-19 11:42:10 +00:00
|
|
|
# [5] mycallsign with ssid
|
|
|
|
self.open_dc_and_transmit(data[1], data[2], data[3], data[4], data[5])
|
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.error(
|
|
|
|
"[TNC] worker_transmit: received invalid command:", data=data
|
|
|
|
)
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def worker_receive(self) -> None:
|
|
|
|
"""Queue received data for processing"""
|
2022-03-04 15:50:32 +00:00
|
|
|
while True:
|
2022-01-07 10:25:28 +00:00
|
|
|
data = self.data_queue_received.get()
|
|
|
|
# [0] bytes
|
|
|
|
# [1] freedv instance
|
|
|
|
# [2] bytes_per_frame
|
2022-05-23 00:54:12 +00:00
|
|
|
self.process_data(
|
|
|
|
bytes_out=data[0], freedv=data[1], bytes_per_frame=data[2]
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def process_data(self, bytes_out, freedv, bytes_per_frame: int) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Process incoming data and decide what to do with the frame.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
bytes_out:
|
|
|
|
freedv:
|
|
|
|
bytes_per_frame:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] process_data:", n_retries_per_burst=self.n_retries_per_burst
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Process data only if broadcast or we are the receiver
|
2022-05-09 00:41:49 +00:00
|
|
|
# bytes_out[1:4] == callsign check for signalling frames,
|
2022-04-19 09:09:11 +00:00
|
|
|
# bytes_out[2:5] == transmission
|
2022-01-24 18:42:59 +00:00
|
|
|
# we could also create an own function, which returns True.
|
2022-04-11 09:03:54 +00:00
|
|
|
frametype = int.from_bytes(bytes(bytes_out[:1]), "big")
|
2022-04-19 09:09:11 +00:00
|
|
|
_valid1, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[1:4]))
|
|
|
|
_valid2, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[2:5]))
|
2022-06-21 22:33:55 +00:00
|
|
|
if (
|
|
|
|
_valid1
|
|
|
|
or _valid2
|
|
|
|
or frametype
|
|
|
|
in [
|
|
|
|
FR_TYPE.CQ.value,
|
|
|
|
FR_TYPE.QRV.value,
|
|
|
|
FR_TYPE.PING.value,
|
2022-06-24 18:55:59 +00:00
|
|
|
FR_TYPE.BEACON.value,
|
2022-06-21 22:33:55 +00:00
|
|
|
]
|
|
|
|
):
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# CHECK IF FRAMETYPE IS BETWEEN 10 and 50 ------------------------
|
|
|
|
frame = frametype - 10
|
|
|
|
n_frames_per_burst = int.from_bytes(bytes(bytes_out[1:2]), "big")
|
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
# Dispatch activity based on received frametype
|
|
|
|
if frametype in self.rx_dispatcher:
|
|
|
|
# Process frames "known" by rx_dispatcher
|
|
|
|
self.log.debug(f"[TNC] {self.rx_dispatcher[frametype][1]} RECEIVED....")
|
|
|
|
self.rx_dispatcher[frametype][0](bytes_out[:-2])
|
|
|
|
|
|
|
|
# Process frametypes requiring a different set of arguments.
|
|
|
|
elif FR_TYPE.BURST_51.value >= frametype >= FR_TYPE.BURST_01.value:
|
2022-01-07 10:25:28 +00:00
|
|
|
# get snr of received data
|
2022-05-23 00:54:12 +00:00
|
|
|
# FIXME: find a fix for this - after moving to classes, this no longer works
|
2022-05-19 20:15:24 +00:00
|
|
|
# snr = self.calculate_snr(freedv)
|
2022-01-07 10:44:35 +00:00
|
|
|
snr = static.SNR
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] RX SNR", snr=snr)
|
2022-01-07 10:25:28 +00:00
|
|
|
# send payload data to arq checker without CRC16
|
2022-05-23 00:54:12 +00:00
|
|
|
self.arq_data_received(
|
|
|
|
bytes(bytes_out[:-2]), bytes_per_frame, snr, freedv
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
|
|
|
|
# if we received the last frame of a burst or the last remaining rpt frame, do a modem unsync
|
2022-05-19 20:15:24 +00:00
|
|
|
# if static.RX_BURST_BUFFER.count(None) <= 1 or (frame+1) == n_frames_per_burst:
|
2022-05-23 00:54:12 +00:00
|
|
|
# self.log.debug(f"[TNC] LAST FRAME OF BURST --> UNSYNC {frame+1}/{n_frames_per_burst}")
|
2022-01-07 10:44:35 +00:00
|
|
|
# self.c_lib.freedv_set_sync(freedv, 0)
|
2022-01-07 10:25:28 +00:00
|
|
|
|
|
|
|
# TESTFRAMES
|
2022-06-21 22:33:55 +00:00
|
|
|
elif frametype == FR_TYPE.TEST_FRAME.value:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] TESTFRAME RECEIVED", frame=bytes_out[:])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Unknown frame type
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
2022-07-02 20:20:51 +00:00
|
|
|
self.log.warning(
|
|
|
|
"[TNC] ARQ - other frame type", frametype=FR_TYPE(frametype).name
|
|
|
|
)
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
|
|
|
# for debugging purposes to receive all data
|
2022-06-24 18:55:59 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] Foreign frame received",
|
|
|
|
frame=bytes_out[:-2].hex(),
|
|
|
|
frame_type=FR_TYPE(int.from_bytes(bytes_out[:1], byteorder="big")).name,
|
|
|
|
)
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def enqueue_frame_for_tx(
|
|
|
|
self,
|
|
|
|
frame_to_tx: bytearray,
|
2022-06-24 13:22:46 +00:00
|
|
|
c2_mode=FREEDV_MODE.datac0.value,
|
2022-05-23 00:54:12 +00:00
|
|
|
copies=1,
|
|
|
|
repeat_delay=0,
|
|
|
|
) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Send (transmit) supplied frame to TNC
|
|
|
|
|
|
|
|
:param frame_to_tx: Frame data to send
|
|
|
|
:type frame_to_tx: bytearray
|
2022-05-23 00:54:12 +00:00
|
|
|
:param c2_mode: Codec2 mode to use, defaults to 14 (datac0)
|
|
|
|
:type c2_mode: int, optional
|
2022-05-11 22:10:59 +00:00
|
|
|
:param copies: Number of frame copies to send, defaults to 1
|
|
|
|
:type copies: int, optional
|
|
|
|
:param repeat_delay: Delay time before sending repeat frame, defaults to 0
|
|
|
|
:type repeat_delay: int, optional
|
|
|
|
"""
|
2022-06-24 13:22:46 +00:00
|
|
|
self.log.debug("[TNC] enqueue_frame_for_tx", c2_mode=FREEDV_MODE(c2_mode).name)
|
2022-05-23 00:54:12 +00:00
|
|
|
|
|
|
|
# Set the TRANSMITTING flag before adding an object to the transmit queue
|
|
|
|
# TODO: This is not that nice, we could improve this somehow
|
2022-05-11 22:10:59 +00:00
|
|
|
static.TRANSMITTING = True
|
2022-05-23 00:54:12 +00:00
|
|
|
modem.MODEM_TRANSMIT_QUEUE.put([c2_mode, copies, repeat_delay, [frame_to_tx]])
|
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Wait while transmitting
|
|
|
|
while static.TRANSMITTING:
|
|
|
|
time.sleep(0.01)
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-07-04 21:09:08 +00:00
|
|
|
def send_data_to_socket_queue(self, **jsondata):
|
2022-05-30 15:41:24 +00:00
|
|
|
"""
|
|
|
|
Send information to the UI via JSON and the sock.SOCKET_QUEUE.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Dictionary containing the data to be sent, in the format:
|
|
|
|
key=value, for each item. E.g.:
|
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="received",
|
2022-06-05 17:11:09 +00:00
|
|
|
status="success",
|
2022-06-06 19:07:41 +00:00
|
|
|
uuid=self.transmission_uuid,
|
2022-05-30 15:41:24 +00:00
|
|
|
timestamp=timestamp,
|
2022-06-06 19:07:41 +00:00
|
|
|
mycallsign=str(self.mycallsign, "UTF-8"),
|
2022-05-30 15:41:24 +00:00
|
|
|
dxcallsign=str(static.DXCALLSIGN, "UTF-8"),
|
|
|
|
dxgrid=str(static.DXGRID, "UTF-8"),
|
|
|
|
data=base64_data,
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
self.log.debug("[TNC] send_data_to_socket_queue:", jsondata=jsondata)
|
|
|
|
json_data_out = json.dumps(jsondata)
|
|
|
|
sock.SOCKET_QUEUE.put(json_data_out)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def send_burst_ack_frame(self, snr) -> None:
|
|
|
|
"""Build and send ACK frame for burst DATA frame"""
|
2022-05-19 20:15:24 +00:00
|
|
|
ack_frame = bytearray(14)
|
2022-06-24 13:48:50 +00:00
|
|
|
ack_frame[:1] = bytes([FR_TYPE.BURST_ACK.value])
|
2022-05-11 22:10:59 +00:00
|
|
|
ack_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
ack_frame[4:7] = static.MYCALLSIGN_CRC
|
|
|
|
ack_frame[7:8] = bytes([int(snr)])
|
|
|
|
ack_frame[8:9] = bytes([int(self.speed_level)])
|
|
|
|
|
|
|
|
# Transmit frame
|
|
|
|
self.enqueue_frame_for_tx(ack_frame)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def send_data_ack_frame(self, snr) -> None:
|
|
|
|
"""Build and send ACK frame for received DATA frame"""
|
2022-05-19 20:15:24 +00:00
|
|
|
ack_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
ack_frame[:1] = bytes([FR_TYPE.FR_ACK.value])
|
2022-05-11 22:10:59 +00:00
|
|
|
ack_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
ack_frame[4:7] = static.MYCALLSIGN_CRC
|
|
|
|
ack_frame[7:8] = bytes([int(snr)])
|
|
|
|
ack_frame[8:9] = bytes([int(self.speed_level)])
|
|
|
|
|
|
|
|
# Transmit frame
|
|
|
|
self.enqueue_frame_for_tx(ack_frame, copies=3, repeat_delay=100)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def send_retransmit_request_frame(self, freedv) -> None:
|
2022-05-11 22:10:59 +00:00
|
|
|
# check where a None is in our burst buffer and do frame+1, beacuse lists start at 0
|
2022-05-28 15:52:05 +00:00
|
|
|
# FIXME: Check to see if there's a `frame - 1` in the receive portion. Remove both if there is.
|
2022-05-23 00:54:12 +00:00
|
|
|
missing_frames = [
|
|
|
|
frame + 1
|
|
|
|
for frame, element in enumerate(static.RX_BURST_BUFFER)
|
|
|
|
if element is None
|
|
|
|
]
|
2022-05-11 22:10:59 +00:00
|
|
|
|
|
|
|
# set n frames per burst to modem
|
2022-05-23 07:37:24 +00:00
|
|
|
# this is an idea, so it's not getting lost....
|
2022-05-11 22:10:59 +00:00
|
|
|
# we need to work on this
|
2022-05-19 20:15:24 +00:00
|
|
|
codec2.api.freedv_set_frames_per_burst(freedv, len(missing_frames))
|
2022-05-11 22:10:59 +00:00
|
|
|
|
|
|
|
# TODO: Trim `missing_frames` bytesarray to [7:13] (6) frames, if it's larger.
|
|
|
|
|
|
|
|
# then create a repeat frame
|
2022-05-19 20:15:24 +00:00
|
|
|
rpt_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
rpt_frame[:1] = bytes([FR_TYPE.FR_REPEAT.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
rpt_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
rpt_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-05-11 22:10:59 +00:00
|
|
|
rpt_frame[7:13] = missing_frames
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ARQ | RX | Requesting", frames=missing_frames)
|
2022-05-11 22:10:59 +00:00
|
|
|
# Transmit frame
|
|
|
|
self.enqueue_frame_for_tx(rpt_frame)
|
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
def send_burst_nack_frame(self, snr: float = 0) -> None:
|
2022-05-23 00:54:12 +00:00
|
|
|
"""Build and send NACK frame for received DATA frame"""
|
2022-05-19 20:15:24 +00:00
|
|
|
nack_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
nack_frame[:1] = bytes([FR_TYPE.FR_NACK.value])
|
2022-05-11 22:10:59 +00:00
|
|
|
nack_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
nack_frame[4:7] = static.MYCALLSIGN_CRC
|
|
|
|
nack_frame[7:8] = bytes([int(snr)])
|
|
|
|
nack_frame[8:9] = bytes([int(self.speed_level)])
|
|
|
|
|
|
|
|
# TRANSMIT NACK FRAME FOR BURST
|
|
|
|
self.enqueue_frame_for_tx(nack_frame)
|
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
def send_burst_nack_frame_watchdog(self, snr: float = 0) -> None:
|
2022-05-23 00:54:12 +00:00
|
|
|
"""Build and send NACK frame for watchdog timeout"""
|
2022-05-19 20:15:24 +00:00
|
|
|
nack_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
nack_frame[:1] = bytes([FR_TYPE.BURST_NACK.value])
|
2022-05-11 22:10:59 +00:00
|
|
|
nack_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
nack_frame[4:7] = static.MYCALLSIGN_CRC
|
|
|
|
nack_frame[7:8] = bytes([int(snr)])
|
|
|
|
nack_frame[8:9] = bytes([int(self.speed_level)])
|
|
|
|
|
|
|
|
# TRANSMIT NACK FRAME FOR BURST
|
|
|
|
self.enqueue_frame_for_tx(nack_frame)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def send_disconnect_frame(self) -> None:
|
|
|
|
"""Build and send a disconnect frame"""
|
2022-05-19 20:15:24 +00:00
|
|
|
disconnection_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
disconnection_frame[:1] = bytes([FR_TYPE.ARQ_SESSION_CLOSE.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
disconnection_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
disconnection_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-05-11 22:10:59 +00:00
|
|
|
disconnection_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
|
|
|
|
|
|
|
self.enqueue_frame_for_tx(disconnection_frame, copies=5, repeat_delay=250)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def arq_data_received(
|
2022-05-28 15:52:05 +00:00
|
|
|
self, data_in: bytes, bytes_per_frame: int, snr: float, freedv
|
2022-05-23 00:54:12 +00:00
|
|
|
) -> None:
|
2022-05-11 22:10:59 +00:00
|
|
|
"""
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
|
|
|
bytes_per_frame:int:
|
2022-05-28 15:52:05 +00:00
|
|
|
snr:float:
|
2022-05-09 00:41:49 +00:00
|
|
|
freedv:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
"""
|
2022-07-02 20:19:33 +00:00
|
|
|
# We've arrived here from process_data which already checked that the frame
|
|
|
|
# is intended for this station.
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in = bytes(data_in)
|
|
|
|
|
2022-03-19 11:42:10 +00:00
|
|
|
# get received crc for different mycall ssids
|
|
|
|
# check if callsign ssid override
|
2022-07-02 20:19:33 +00:00
|
|
|
_, mycallsign = helpers.check_callsign(
|
2022-07-04 21:27:35 +00:00
|
|
|
self.mycallsign, data_in[2:5]
|
2022-05-23 00:54:12 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# only process data if we are in ARQ and BUSY state else return to quit
|
2022-05-23 00:54:12 +00:00
|
|
|
if not static.ARQ_STATE and static.TNC_STATE != "BUSY":
|
2022-01-07 10:25:28 +00:00
|
|
|
return
|
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
self.arq_file_transfer = True
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "BUSY"
|
2022-01-07 10:25:28 +00:00
|
|
|
static.ARQ_STATE = True
|
2022-05-23 00:54:12 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-01-07 10:25:28 +00:00
|
|
|
self.data_channel_last_received = int(time.time())
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Extract some important data from the frame
|
|
|
|
# Get sequence number of burst frame
|
|
|
|
RX_N_FRAME_OF_BURST = int.from_bytes(bytes(data_in[:1]), "big") - 10
|
|
|
|
# Get number of bursts from received frame
|
|
|
|
RX_N_FRAMES_PER_BURST = int.from_bytes(bytes(data_in[1:2]), "big")
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# The RX burst buffer needs to have a fixed length filled with "None".
|
|
|
|
# We need this later for counting the "Nones" to detect missing data.
|
|
|
|
# Check if burst buffer has expected length else create it
|
2022-01-07 10:25:28 +00:00
|
|
|
if len(static.RX_BURST_BUFFER) != RX_N_FRAMES_PER_BURST:
|
2022-05-09 00:41:49 +00:00
|
|
|
static.RX_BURST_BUFFER = [None] * RX_N_FRAMES_PER_BURST
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Append data to rx burst buffer
|
2022-05-23 00:54:12 +00:00
|
|
|
# [frame_type][n_frames_per_burst][CRC24][CRC24]
|
|
|
|
static.RX_BURST_BUFFER[RX_N_FRAME_OF_BURST] = data_in[8:] # type: ignore
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] static.RX_BURST_BUFFER", buffer=static.RX_BURST_BUFFER)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
snr,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Check if we received all frames in the burst by checking if burst buffer has no more "Nones"
|
|
|
|
# This is the ideal case because we received all data
|
|
|
|
if None not in static.RX_BURST_BUFFER:
|
2022-01-10 16:01:33 +00:00
|
|
|
# then iterate through burst buffer and stick the burst together
|
|
|
|
# the temp burst buffer is needed for checking, if we already recevied data
|
2022-05-23 00:54:12 +00:00
|
|
|
temp_burst_buffer = b""
|
2022-05-11 22:10:59 +00:00
|
|
|
for value in static.RX_BURST_BUFFER:
|
2022-05-19 20:15:24 +00:00
|
|
|
# static.RX_FRAME_BUFFER += static.RX_BURST_BUFFER[i]
|
2022-05-23 00:54:12 +00:00
|
|
|
temp_burst_buffer += bytes(value) # type: ignore
|
2022-01-10 16:01:33 +00:00
|
|
|
|
|
|
|
# if frame buffer ends not with the current frame, we are going to append new data
|
2022-05-23 00:54:12 +00:00
|
|
|
# if data already exists, we received the frame correctly,
|
|
|
|
# but the ACK frame didn't receive its destination (ISS)
|
2022-02-18 08:58:49 +00:00
|
|
|
if static.RX_FRAME_BUFFER.endswith(temp_burst_buffer):
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | RX | Frame already received - sending ACK again"
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
static.RX_BURST_BUFFER = []
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-18 08:58:49 +00:00
|
|
|
else:
|
2022-05-11 22:10:59 +00:00
|
|
|
# Here we are going to search for our data in the last received bytes.
|
|
|
|
# This reduces the chance we will lose the entire frame in the case of signalling frame loss
|
|
|
|
|
2022-05-23 07:37:24 +00:00
|
|
|
# static.RX_FRAME_BUFFER --> existing data
|
2022-02-18 08:58:49 +00:00
|
|
|
# temp_burst_buffer --> new data
|
|
|
|
# search_area --> area where we want to search
|
|
|
|
search_area = 510
|
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
search_position = len(static.RX_FRAME_BUFFER) - search_area
|
2022-02-18 09:04:00 +00:00
|
|
|
# find position of data. returns -1 if nothing found in area else >= 0
|
2022-05-23 00:54:12 +00:00
|
|
|
# we are beginning from the end, so if data exists twice or more,
|
|
|
|
# only the last one should be replaced
|
|
|
|
get_position = static.RX_FRAME_BUFFER[search_position:].rfind(
|
|
|
|
temp_burst_buffer
|
|
|
|
)
|
2022-02-18 08:58:49 +00:00
|
|
|
# if we find data, replace it at this position with the new data and strip it
|
|
|
|
if get_position >= 0:
|
2022-05-23 00:54:12 +00:00
|
|
|
static.RX_FRAME_BUFFER = static.RX_FRAME_BUFFER[
|
|
|
|
: search_position + get_position
|
|
|
|
]
|
2022-02-18 08:58:49 +00:00
|
|
|
static.RX_FRAME_BUFFER += temp_burst_buffer
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.warning(
|
|
|
|
"[TNC] ARQ | RX | replacing existing buffer data",
|
|
|
|
area=search_area,
|
|
|
|
pos=get_position,
|
|
|
|
)
|
2022-05-28 15:52:05 +00:00
|
|
|
# If we don't find data in this range, we really have new data and going to replace it
|
2022-02-18 08:58:49 +00:00
|
|
|
else:
|
|
|
|
static.RX_FRAME_BUFFER += temp_burst_buffer
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] ARQ | RX | appending data to buffer")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
# Check if we didn't receive a BOF and EOF yet to avoid sending
|
2022-05-23 00:54:12 +00:00
|
|
|
# ack frames if we already received all data
|
|
|
|
if (
|
|
|
|
not self.rx_frame_bof_received
|
|
|
|
and not self.rx_frame_eof_received
|
|
|
|
and data_in.find(self.data_frame_eof) < 0
|
|
|
|
):
|
2022-05-09 00:41:49 +00:00
|
|
|
|
|
|
|
self.frame_received_counter += 1
|
2022-02-02 20:12:16 +00:00
|
|
|
if self.frame_received_counter >= 2:
|
|
|
|
self.frame_received_counter = 0
|
2022-05-23 02:13:43 +00:00
|
|
|
self.speed_level = min(
|
|
|
|
self.speed_level + 1, len(self.mode_list) - 1
|
|
|
|
)
|
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
2022-02-02 20:12:16 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Update modes we are listening to
|
2022-02-08 14:27:34 +00:00
|
|
|
self.set_listening_modes(self.mode_list[self.speed_level])
|
2022-02-02 20:12:16 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Create and send ACK frame
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ARQ | RX | SENDING ACK")
|
2022-05-11 22:10:59 +00:00
|
|
|
self.send_burst_ack_frame(snr)
|
|
|
|
|
|
|
|
# Reset n retries per burst counter
|
2022-04-05 09:29:20 +00:00
|
|
|
self.n_retries_per_burst = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-04-05 09:29:20 +00:00
|
|
|
# calculate statistics
|
2022-05-23 00:54:12 +00:00
|
|
|
self.calculate_transfer_rate_rx(
|
|
|
|
self.rx_start_of_transmission, len(static.RX_FRAME_BUFFER)
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
elif RX_N_FRAME_OF_BURST == RX_N_FRAMES_PER_BURST - 1:
|
2022-05-11 22:10:59 +00:00
|
|
|
# We have "Nones" in our rx buffer,
|
|
|
|
# Check if we received last frame of burst - this is an indicator for missed frames.
|
2022-05-23 00:54:12 +00:00
|
|
|
# With this way of doing this, we always MUST receive the last
|
|
|
|
# frame of a burst otherwise the entire burst is lost
|
2022-05-28 15:52:05 +00:00
|
|
|
# TODO: See if a timeout on the send side with re-transmit last burst would help.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] all frames in burst received:",
|
|
|
|
frame=RX_N_FRAME_OF_BURST,
|
|
|
|
frames=RX_N_FRAMES_PER_BURST,
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
self.send_retransmit_request_frame(freedv)
|
2022-05-23 00:54:12 +00:00
|
|
|
self.calculate_transfer_rate_rx(
|
|
|
|
self.rx_start_of_transmission, len(static.RX_FRAME_BUFFER)
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Should never reach this point
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.error(
|
|
|
|
"[TNC] data_handler: Should not reach this point...",
|
|
|
|
frame=RX_N_FRAME_OF_BURST,
|
|
|
|
frames=RX_N_FRAMES_PER_BURST,
|
|
|
|
)
|
2022-01-24 18:42:59 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# We have a BOF and EOF flag in our data. If we received both we received our frame.
|
2022-05-19 20:37:27 +00:00
|
|
|
# In case of loosing data, but we received already a BOF and EOF we need to make sure, we
|
2022-05-09 00:41:49 +00:00
|
|
|
# received the complete last burst by checking it for Nones
|
2022-01-07 10:25:28 +00:00
|
|
|
bof_position = static.RX_FRAME_BUFFER.find(self.data_frame_bof)
|
|
|
|
eof_position = static.RX_FRAME_BUFFER.find(self.data_frame_eof)
|
2022-01-24 18:42:59 +00:00
|
|
|
|
|
|
|
# get total bytes per transmission information as soon we recevied a frame with a BOF
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
if bof_position >= 0:
|
2022-05-23 00:54:12 +00:00
|
|
|
payload = static.RX_FRAME_BUFFER[
|
2022-06-21 22:33:55 +00:00
|
|
|
bof_position + len(self.data_frame_bof) : eof_position
|
2022-05-23 00:54:12 +00:00
|
|
|
]
|
2022-05-19 20:15:24 +00:00
|
|
|
frame_length = int.from_bytes(payload[4:8], "big") # 4:8 4bytes
|
2022-02-08 14:27:34 +00:00
|
|
|
static.TOTAL_BYTES = frame_length
|
2022-05-19 20:15:24 +00:00
|
|
|
compression_factor = int.from_bytes(payload[8:9], "big") # 4:8 4bytes
|
2022-05-23 02:13:43 +00:00
|
|
|
# limit to max value of 255
|
|
|
|
compression_factor = np.clip(compression_factor, 0, 255)
|
2022-02-08 14:27:34 +00:00
|
|
|
static.ARQ_COMPRESSION_FACTOR = compression_factor / 10
|
2022-05-23 00:54:12 +00:00
|
|
|
self.calculate_transfer_rate_rx(
|
|
|
|
self.rx_start_of_transmission, len(static.RX_FRAME_BUFFER)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
bof_position >= 0
|
|
|
|
and eof_position > 0
|
|
|
|
and None not in static.RX_BURST_BUFFER
|
|
|
|
):
|
|
|
|
self.log.debug(
|
|
|
|
"[TNC] arq_data_received:",
|
|
|
|
bof_position=bof_position,
|
|
|
|
eof_position=eof_position,
|
|
|
|
)
|
2022-01-07 10:44:35 +00:00
|
|
|
self.rx_frame_bof_received = True
|
|
|
|
self.rx_frame_eof_received = True
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Extract raw data from buffer
|
2022-05-23 00:54:12 +00:00
|
|
|
payload = static.RX_FRAME_BUFFER[
|
2022-06-21 22:33:55 +00:00
|
|
|
bof_position + len(self.data_frame_bof) : eof_position
|
2022-05-23 00:54:12 +00:00
|
|
|
]
|
2022-05-11 22:10:59 +00:00
|
|
|
# Get the data frame crc
|
2022-05-23 00:54:12 +00:00
|
|
|
data_frame_crc = payload[:4] # 0:4 = 4 bytes
|
2022-05-11 22:10:59 +00:00
|
|
|
# Get the data frame length
|
2022-05-23 00:54:12 +00:00
|
|
|
frame_length = int.from_bytes(payload[4:8], "big") # 4:8 = 4 bytes
|
2022-02-08 14:27:34 +00:00
|
|
|
static.TOTAL_BYTES = frame_length
|
|
|
|
# 8:9 = compression factor
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-02-08 14:27:34 +00:00
|
|
|
data_frame = payload[9:]
|
2022-01-24 18:42:59 +00:00
|
|
|
data_frame_crc_received = helpers.get_crc_32(data_frame)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Check if data_frame_crc is equal with received crc
|
2022-01-07 10:25:28 +00:00
|
|
|
if data_frame_crc == data_frame_crc_received:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ARQ | RX | DATA FRAME SUCESSFULLY RECEIVED")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Decompress the data frame
|
2022-01-07 10:25:28 +00:00
|
|
|
data_frame_decompressed = zlib.decompress(data_frame)
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_COMPRESSION_FACTOR = len(data_frame_decompressed) / len(
|
|
|
|
data_frame
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
data_frame = data_frame_decompressed
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-06 19:07:41 +00:00
|
|
|
self.transmission_uuid = str(uuid.uuid4())
|
2022-03-12 14:06:49 +00:00
|
|
|
timestamp = int(time.time())
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Re-code data_frame in base64, UTF-8 for JSON UI communication.
|
2022-05-23 00:54:12 +00:00
|
|
|
base64_data = base64.b64encode(data_frame).decode("UTF-8")
|
2022-09-05 08:37:50 +00:00
|
|
|
|
|
|
|
if not RX_BUFFER.full():
|
|
|
|
# check if RX_BUFFER isn't full
|
|
|
|
pass
|
|
|
|
else:
|
2022-09-05 08:47:03 +00:00
|
|
|
# if full, free space by getting an item
|
2022-09-05 08:37:50 +00:00
|
|
|
# if full, free space by getting an item
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | RX | RX_BUFFER FULL - dropping old data",
|
|
|
|
buffer_size=RX_BUFFER.qsize(),
|
2022-09-05 08:47:03 +00:00
|
|
|
maxsize=static.RX_BUFFER_SIZE
|
2022-09-05 08:37:50 +00:00
|
|
|
)
|
|
|
|
RX_BUFFER.get()
|
|
|
|
|
|
|
|
# add item to RX_BUFFER
|
|
|
|
RX_BUFFER.put(
|
2022-06-21 22:33:55 +00:00
|
|
|
[
|
|
|
|
self.transmission_uuid,
|
|
|
|
timestamp,
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
base64_data,
|
|
|
|
]
|
2022-05-23 00:54:12 +00:00
|
|
|
)
|
2022-09-05 08:37:50 +00:00
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-06 18:31:15 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="received",
|
2022-06-06 19:07:41 +00:00
|
|
|
uuid=self.transmission_uuid,
|
2022-05-30 15:41:24 +00:00
|
|
|
timestamp=timestamp,
|
|
|
|
mycallsign=str(mycallsign, "UTF-8"),
|
|
|
|
dxcallsign=str(static.DXCALLSIGN, "UTF-8"),
|
|
|
|
dxgrid=str(static.DXGRID, "UTF-8"),
|
|
|
|
data=base64_data,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | RX | SENDING DATA FRAME ACK",
|
|
|
|
snr=snr,
|
|
|
|
crc=data_frame_crc.hex(),
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
self.send_data_ack_frame(snr)
|
2022-05-23 00:54:12 +00:00
|
|
|
# Update statistics AFTER the frame ACK is sent
|
|
|
|
self.calculate_transfer_rate_rx(
|
|
|
|
self.rx_start_of_transmission, len(static.RX_FRAME_BUFFER)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] | RX | DATACHANNEL ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]<< >>["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
snr=snr,
|
|
|
|
)
|
2021-12-29 19:53:11 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="failed",
|
2022-06-06 19:07:41 +00:00
|
|
|
uuid=self.transmission_uuid,
|
2022-06-05 17:11:09 +00:00
|
|
|
)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.warning(
|
2022-06-06 18:31:15 +00:00
|
|
|
"[TNC] ARQ | RX | DATA FRAME NOT SUCCESSFULLY RECEIVED!",
|
2022-05-23 00:54:12 +00:00
|
|
|
e="wrong crc",
|
2022-06-24 13:22:46 +00:00
|
|
|
expected=data_frame_crc.hex(),
|
|
|
|
received=data_frame_crc_received.hex(),
|
2022-05-23 00:54:12 +00:00
|
|
|
overflows=static.BUFFER_OVERFLOW_COUNTER,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.log.info("[TNC] ARQ | RX | Sending NACK")
|
2022-05-11 22:10:59 +00:00
|
|
|
self.send_burst_nack_frame(snr)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update arq_session timestamp
|
2022-05-23 00:54:12 +00:00
|
|
|
self.arq_session_last_received = int(time.time())
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Finally cleanup our buffers and states,
|
|
|
|
self.arq_cleanup()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
def arq_transmit(self, data_out: bytes, mode: int, n_frames_per_burst: int):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Transmit ARQ frame
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_out:bytes:
|
|
|
|
mode:int:
|
|
|
|
n_frames_per_burst:int:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
self.arq_file_transfer = True
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Start at the highest speed level for selected speed mode
|
|
|
|
self.speed_level = len(self.mode_list) - 1
|
2022-02-22 20:05:48 +00:00
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
2022-01-05 09:04:54 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
self.tx_n_retry_of_burst = 0 # retries we already sent data
|
2022-05-23 00:54:12 +00:00
|
|
|
# Maximum number of retries to send before declaring a frame is lost
|
|
|
|
TX_N_MAX_RETRIES_PER_BURST = 50
|
2022-05-19 20:15:24 +00:00
|
|
|
TX_N_FRAMES_PER_BURST = n_frames_per_burst # amount of n frames per burst
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-04-11 09:03:54 +00:00
|
|
|
# TIMEOUTS
|
2022-05-19 20:15:24 +00:00
|
|
|
BURST_ACK_TIMEOUT_SECONDS = 3.0 # timeout for burst acknowledges
|
|
|
|
DATA_FRAME_ACK_TIMEOUT_SECONDS = 3.0 # timeout for data frame acknowledges
|
|
|
|
RPT_ACK_TIMEOUT_SECONDS = 3.0 # timeout for rpt frame acknowledges
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# save len of data_out to TOTAL_BYTES for our statistics --> kBytes
|
2022-05-19 20:15:24 +00:00
|
|
|
# static.TOTAL_BYTES = round(len(data_out) / 1024, 2)
|
2022-01-24 18:42:59 +00:00
|
|
|
static.TOTAL_BYTES = len(data_out)
|
2022-05-23 00:54:12 +00:00
|
|
|
frame_total_size = len(data_out).to_bytes(4, byteorder="big")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="transmitting",
|
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
percent=static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
bytesperminute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-24 13:22:46 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] | TX | DATACHANNEL",
|
|
|
|
Bytes=static.TOTAL_BYTES,
|
|
|
|
)
|
2022-01-05 10:38:39 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Compress data frame
|
2022-01-07 10:25:28 +00:00
|
|
|
data_frame_compressed = zlib.compress(data_out)
|
2022-02-08 14:27:34 +00:00
|
|
|
compression_factor = len(data_out) / len(data_frame_compressed)
|
2022-03-11 19:38:28 +00:00
|
|
|
static.ARQ_COMPRESSION_FACTOR = np.clip(compression_factor, 0, 255)
|
2022-02-08 14:27:34 +00:00
|
|
|
compression_factor = bytes([int(static.ARQ_COMPRESSION_FACTOR * 10)])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
|
|
|
data_out = data_frame_compressed
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
# Reset data transfer statistics
|
2022-01-07 10:25:28 +00:00
|
|
|
tx_start_of_transmission = time.time()
|
|
|
|
self.calculate_transfer_rate_tx(tx_start_of_transmission, 0, len(data_out))
|
|
|
|
|
2022-05-28 12:31:58 +00:00
|
|
|
# Append a crc at the beginning and end of file indicators
|
2022-01-24 18:42:59 +00:00
|
|
|
frame_payload_crc = helpers.get_crc_32(data_out)
|
2022-06-24 13:22:46 +00:00
|
|
|
self.log.debug("[TNC] frame payload CRC:", crc=frame_payload_crc.hex())
|
2022-05-23 00:54:12 +00:00
|
|
|
|
|
|
|
# Assemble the data frame
|
|
|
|
data_out = (
|
|
|
|
self.data_frame_bof
|
|
|
|
+ frame_payload_crc
|
|
|
|
+ frame_total_size
|
|
|
|
+ compression_factor
|
|
|
|
+ data_out
|
|
|
|
+ self.data_frame_eof
|
|
|
|
)
|
|
|
|
|
|
|
|
# Initial bufferposition is 0
|
2022-05-11 22:10:59 +00:00
|
|
|
bufferposition = bufferposition_end = 0
|
2022-01-05 09:59:09 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Iterate through data_out buffer
|
2022-06-05 18:09:38 +00:00
|
|
|
while not self.data_frame_ack_received and static.ARQ_STATE:
|
2022-01-07 10:25:28 +00:00
|
|
|
# we have TX_N_MAX_RETRIES_PER_BURST attempts for sending a burst
|
2022-05-11 22:10:59 +00:00
|
|
|
for self.tx_n_retry_of_burst in range(TX_N_MAX_RETRIES_PER_BURST):
|
2022-06-21 22:47:18 +00:00
|
|
|
data_mode = mode
|
2022-09-05 07:04:35 +00:00
|
|
|
# self.log.debug("[TNC] FIXED MODE:", mode=FREEDV_MODE(data_mode).name)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-23 22:16:32 +00:00
|
|
|
# we are doing a modulo check of transmission retries of the actual burst
|
|
|
|
# every 2nd retry which fails, decreases speedlevel by 1.
|
|
|
|
# as soon as we received an ACK for the current burst, speed_level will increase
|
|
|
|
# by 1.
|
|
|
|
# The intent is to optimize speed by adapting to the current RF conditions.
|
|
|
|
# if not self.tx_n_retry_of_burst % 2 and self.tx_n_retry_of_burst > 0:
|
|
|
|
# self.speed_level = max(self.speed_level - 1, 0)
|
|
|
|
|
|
|
|
# if self.tx_n_retry_of_burst <= 1:
|
|
|
|
# self.speed_level += 1
|
|
|
|
# self.speed_level = max(self.speed_level + 1, len(self.mode_list) - 1)
|
|
|
|
|
|
|
|
# Bound speed level to:
|
|
|
|
# - minimum of either the speed or the length of mode list - 1
|
|
|
|
# - maximum of either the speed or zero
|
|
|
|
self.speed_level = min(self.speed_level, len(self.mode_list) - 1)
|
|
|
|
self.speed_level = max(self.speed_level, 0)
|
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
|
|
|
data_mode = self.mode_list[self.speed_level]
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-23 22:16:32 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] Speed-level:",
|
|
|
|
level=self.speed_level,
|
|
|
|
retry=self.tx_n_retry_of_burst,
|
2022-06-24 13:22:46 +00:00
|
|
|
mode=FREEDV_MODE(data_mode).name,
|
2022-06-23 22:16:32 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Payload information
|
2022-05-19 20:15:24 +00:00
|
|
|
payload_per_frame = modem.get_bytes_per_frame(data_mode) - 2
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Tempbuffer list for storing our data frames
|
2022-01-07 10:25:28 +00:00
|
|
|
tempbuffer = []
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Append data frames with TX_N_FRAMES_PER_BURST to tempbuffer
|
2022-05-11 22:10:59 +00:00
|
|
|
# TODO: this part needs a complete rewrite!
|
|
|
|
# TX_N_FRAMES_PER_BURST = 1 is working
|
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
arqheader = bytearray()
|
2022-07-02 20:20:51 +00:00
|
|
|
# arqheader[:1] = bytes([FR_TYPE.BURST_01.value + i])
|
|
|
|
arqheader[:1] = bytes([FR_TYPE.BURST_01.value])
|
2022-05-09 00:41:49 +00:00
|
|
|
arqheader[1:2] = bytes([TX_N_FRAMES_PER_BURST])
|
2022-04-19 09:09:11 +00:00
|
|
|
arqheader[2:5] = static.DXCALLSIGN_CRC
|
|
|
|
arqheader[5:8] = static.MYCALLSIGN_CRC
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
bufferposition_end = bufferposition + payload_per_frame - len(arqheader)
|
2022-01-04 13:16:50 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Normal condition
|
2022-01-07 10:25:28 +00:00
|
|
|
if bufferposition_end <= len(data_out):
|
2022-05-19 20:15:24 +00:00
|
|
|
frame = data_out[bufferposition:bufferposition_end]
|
|
|
|
frame = arqheader + frame
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Pad the last bytes of a frame
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
|
|
|
extended_data_out = data_out[bufferposition:]
|
2022-05-23 00:54:12 +00:00
|
|
|
extended_data_out += bytes([0]) * (
|
|
|
|
payload_per_frame - len(extended_data_out) - len(arqheader)
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
frame = arqheader + extended_data_out
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Append frame to tempbuffer for transmission
|
2022-01-07 10:25:28 +00:00
|
|
|
tempbuffer.append(frame)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] tempbuffer:", tempbuffer=tempbuffer)
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | TX | FRAMES",
|
2022-06-24 13:22:46 +00:00
|
|
|
mode=FREEDV_MODE(data_mode).name,
|
2022-05-23 00:54:12 +00:00
|
|
|
fpb=TX_N_FRAMES_PER_BURST,
|
|
|
|
retry=self.tx_n_retry_of_burst,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-29 12:42:38 +00:00
|
|
|
for t_buf_item in tempbuffer:
|
|
|
|
self.enqueue_frame_for_tx(t_buf_item, c2_mode=data_mode)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# After transmission finished, wait for an ACK or RPT frame
|
2022-05-19 20:15:24 +00:00
|
|
|
# burstacktimeout = time.time() + BURST_ACK_TIMEOUT_SECONDS + 100
|
2022-05-23 00:54:12 +00:00
|
|
|
# while (not self.burst_ack and not self.burst_nack and
|
|
|
|
# not self.rpt_request_received and not self.data_frame_ack_received and
|
|
|
|
# time.time() < burstacktimeout and static.ARQ_STATE):
|
|
|
|
# time.sleep(0.01)
|
|
|
|
|
|
|
|
# burstacktimeout = time.time() + BURST_ACK_TIMEOUT_SECONDS + 100
|
|
|
|
while static.ARQ_STATE and not (
|
|
|
|
self.burst_ack
|
|
|
|
or self.burst_nack
|
|
|
|
or self.rpt_request_received
|
|
|
|
or self.data_frame_ack_received
|
|
|
|
):
|
2022-02-08 14:27:34 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-24 18:55:59 +00:00
|
|
|
# Once we receive a burst ack, reset its state and break the RETRIES loop
|
2022-01-07 11:55:03 +00:00
|
|
|
if self.burst_ack:
|
2022-05-19 20:15:24 +00:00
|
|
|
self.burst_ack = False # reset ack state
|
|
|
|
self.tx_n_retry_of_burst = 0 # reset retries
|
2022-06-05 18:09:38 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] arq_transmit: Received BURST ACK. Sending next chunk."
|
|
|
|
)
|
2022-05-19 20:15:24 +00:00
|
|
|
break # break retry loop
|
2022-01-05 09:59:09 +00:00
|
|
|
|
2022-02-02 20:12:16 +00:00
|
|
|
if self.burst_nack:
|
2022-05-19 20:15:24 +00:00
|
|
|
self.burst_nack = False # reset nack state
|
2022-02-02 20:12:16 +00:00
|
|
|
|
|
|
|
# not yet implemented
|
2022-01-07 10:25:28 +00:00
|
|
|
if self.rpt_request_received:
|
|
|
|
pass
|
2022-01-05 10:38:39 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
if self.data_frame_ack_received:
|
2022-06-05 18:09:38 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] arq_transmit: Received FRAME ACK. Sending next chunk."
|
|
|
|
)
|
2022-05-19 20:15:24 +00:00
|
|
|
break # break retry loop
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# We need this part for leaving the repeat loop
|
2022-05-26 01:23:30 +00:00
|
|
|
# static.ARQ_STATE == "DATA" --> when stopping transmission manually
|
2022-01-07 10:25:28 +00:00
|
|
|
if not static.ARQ_STATE:
|
2022-05-19 20:15:24 +00:00
|
|
|
# print("not ready for data...leaving loop....")
|
2022-06-05 18:09:38 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] arq_transmit: ARQ State changed to FALSE. Breaking retry loop."
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
break
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.calculate_transfer_rate_tx(
|
|
|
|
tx_start_of_transmission, bufferposition_end, len(data_out)
|
|
|
|
)
|
2022-01-07 10:25:28 +00:00
|
|
|
# NEXT ATTEMPT
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] ATTEMPT:",
|
|
|
|
retry=self.tx_n_retry_of_burst,
|
|
|
|
maxretries=TX_N_MAX_RETRIES_PER_BURST,
|
|
|
|
overflows=static.BUFFER_OVERFLOW_COUNTER,
|
|
|
|
)
|
|
|
|
# End of FOR loop
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# update buffer position
|
|
|
|
bufferposition = bufferposition_end
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-04-07 09:19:29 +00:00
|
|
|
# update stats
|
2022-05-23 00:54:12 +00:00
|
|
|
self.calculate_transfer_rate_tx(
|
|
|
|
tx_start_of_transmission, bufferposition_end, len(data_out)
|
|
|
|
)
|
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="transmitting",
|
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
percent=static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
bytesperminute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-05 18:09:38 +00:00
|
|
|
# Stay in the while loop until we receive a data_frame_ack. Otherwise
|
|
|
|
# the loop exits after sending the last frame only once and doesn't
|
|
|
|
# wait for an acknowledgement.
|
|
|
|
if self.data_frame_ack_received and bufferposition > len(data_out):
|
|
|
|
self.log.debug("[TNC] arq_tx: Last fragment sent and acknowledged.")
|
|
|
|
break
|
2022-05-19 20:15:24 +00:00
|
|
|
# GOING TO NEXT ITERATION
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
if self.data_frame_ack_received:
|
2022-06-09 12:11:39 +00:00
|
|
|
# we need to wait until sending "transmitted" state
|
|
|
|
# gui database is too slow for handling this within 0.001 seconds
|
|
|
|
# so let's sleep a little
|
2022-06-09 19:31:12 +00:00
|
|
|
time.sleep(0.2)
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="transmission",
|
2022-06-05 17:11:09 +00:00
|
|
|
status="transmitted",
|
2022-05-30 15:41:24 +00:00
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
percent=static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
bytesperminute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | TX | DATA TRANSMITTED!",
|
|
|
|
BytesPerMinute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
BitsPerSecond=static.ARQ_BITS_PER_SECOND,
|
|
|
|
overflows=static.BUFFER_OVERFLOW_COUNTER,
|
|
|
|
)
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="failed",
|
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
percent=static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
bytesperminute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | TX | TRANSMISSION FAILED OR TIME OUT!",
|
|
|
|
overflows=static.BUFFER_OVERFLOW_COUNTER,
|
|
|
|
)
|
2022-03-10 19:46:34 +00:00
|
|
|
self.stop_transmission()
|
2021-03-17 10:22:06 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Last but not least do a state cleanup
|
|
|
|
self.arq_cleanup()
|
|
|
|
if TESTMODE:
|
|
|
|
# Quit after transmission
|
2022-06-05 18:09:38 +00:00
|
|
|
self.log.debug("[TNC] TESTMODE: arq_transmit exiting.")
|
2022-05-11 22:10:59 +00:00
|
|
|
sys.exit(0)
|
2021-10-02 09:29:08 +00:00
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
def burst_ack_nack_received(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-07-02 20:14:05 +00:00
|
|
|
Received a ACK/NACK for a transmitted frame, keep track and
|
2022-05-23 00:54:12 +00:00
|
|
|
make adjustments to speed level if needed.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
# Process data only if we are in ARQ and BUSY state
|
2022-01-07 10:25:28 +00:00
|
|
|
if static.ARQ_STATE:
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-07-02 20:14:05 +00:00
|
|
|
|
|
|
|
frametype = int.from_bytes(bytes(data_in[:1]), "big")
|
|
|
|
desc = "ack"
|
|
|
|
if frametype == FR_TYPE.BURST_ACK.value:
|
|
|
|
# Increase speed level if we received a burst ack
|
|
|
|
# self.speed_level = min(self.speed_level + 1, len(self.mode_list) - 1)
|
2022-07-02 20:20:51 +00:00
|
|
|
# Force data retry loops of TX TNC to stop and continue with next frame
|
|
|
|
self.burst_ack = True
|
|
|
|
# Reset burst nack counter
|
|
|
|
self.burst_nack_counter = 0
|
|
|
|
# Reset n retries per burst counter
|
|
|
|
self.n_retries_per_burst = 0
|
2022-07-02 20:14:05 +00:00
|
|
|
else:
|
2022-07-02 20:20:51 +00:00
|
|
|
# Decrease speed level if we received a burst nack
|
|
|
|
# self.speed_level = max(self.speed_level - 1, 0)
|
2022-07-02 20:14:05 +00:00
|
|
|
# Set flag to retry frame again.
|
2022-07-02 20:20:51 +00:00
|
|
|
self.burst_nack = True
|
2022-07-02 20:14:05 +00:00
|
|
|
# Increment burst nack counter
|
|
|
|
self.burst_nack_counter += 1
|
|
|
|
desc = "nack"
|
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
|
|
|
self.data_channel_last_received = int(time.time())
|
2022-05-19 20:15:24 +00:00
|
|
|
self.burst_ack_snr = int.from_bytes(bytes(data_in[7:8]), "big")
|
|
|
|
self.speed_level = int.from_bytes(bytes(data_in[8:9]), "big")
|
2022-02-22 20:05:48 +00:00
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
2022-07-02 20:14:05 +00:00
|
|
|
|
2022-06-24 13:22:46 +00:00
|
|
|
self.log.debug(
|
2022-07-02 20:14:05 +00:00
|
|
|
f"[TNC] burst_{desc}_received:",
|
2022-06-24 13:22:46 +00:00
|
|
|
speed_level=self.speed_level,
|
|
|
|
c2_mode=FREEDV_MODE(self.mode_list[self.speed_level]).name,
|
|
|
|
)
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
def frame_ack_received(
|
|
|
|
self, data_in: bytes # pylint: disable=unused-argument
|
|
|
|
) -> None:
|
2022-05-23 00:54:12 +00:00
|
|
|
"""Received an ACK for a transmitted frame"""
|
|
|
|
# Process data only if we are in ARQ and BUSY state
|
2022-05-09 00:41:49 +00:00
|
|
|
if static.ARQ_STATE:
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-23 02:13:43 +00:00
|
|
|
# Force data loops of TNC to stop and continue with next frame
|
|
|
|
self.data_frame_ack_received = True
|
|
|
|
# Update arq_session and data_channel timestamp
|
|
|
|
self.data_channel_last_received = int(time.time())
|
|
|
|
self.arq_session_last_received = int(time.time())
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
def frame_nack_received(
|
|
|
|
self, data_in: bytes # pylint: disable=unused-argument
|
|
|
|
) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-07-02 20:14:05 +00:00
|
|
|
Received a NACK for a transmitted frame
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="failed",
|
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
percent=static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
bytesperminute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
)
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-05-23 00:54:12 +00:00
|
|
|
self.arq_session_last_received = int(time.time())
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.arq_cleanup()
|
2022-01-04 11:11:21 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
def burst_rpt_received(self, data_in: bytes):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Repeat request frame received for transmitted frame
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
# Only process data if we are in ARQ and BUSY state
|
|
|
|
if static.ARQ_STATE and static.TNC_STATE == "BUSY":
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
self.rpt_request_received = True
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
|
|
|
self.data_channel_last_received = int(time.time())
|
2022-01-07 10:25:28 +00:00
|
|
|
self.rpt_request_buffer = []
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
missing_area = bytes(data_in[3:12]) # 1:9
|
2021-02-28 14:24:14 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
for i in range(0, 6, 2):
|
2022-05-23 00:54:12 +00:00
|
|
|
if not missing_area[i : i + 2].endswith(b"\x00\x00"):
|
|
|
|
missing = missing_area[i : i + 2]
|
2022-01-07 10:25:28 +00:00
|
|
|
self.rpt_request_buffer.insert(0, missing)
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
############################################################################################################
|
2022-03-04 15:50:32 +00:00
|
|
|
# ARQ SESSION HANDLER
|
2022-05-28 15:52:05 +00:00
|
|
|
############################################################################################################
|
2022-05-23 00:54:12 +00:00
|
|
|
def arq_session_handler(self) -> bool:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-26 01:23:30 +00:00
|
|
|
Create a session with `static.DXCALLSIGN` and wait until the session is open.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-05-23 00:54:12 +00:00
|
|
|
True if the session was opened successfully
|
|
|
|
False if the session open request failed
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 07:37:24 +00:00
|
|
|
# TODO: we need to check this, maybe placing it to class init
|
2022-03-04 15:50:32 +00:00
|
|
|
self.datachannel_timeout = False
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] SESSION ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]>> <<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
state=static.ARQ_SESSION_STATE,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.open_session()
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
# wait until data channel is open
|
|
|
|
while not static.ARQ_SESSION and not self.arq_session_timeout:
|
|
|
|
time.sleep(0.01)
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_SESSION_STATE = "connecting"
|
2022-06-15 08:40:54 +00:00
|
|
|
self.send_data_to_socket_queue(
|
|
|
|
freedata="tnc-message",
|
|
|
|
arq="session",
|
|
|
|
status="connecting",
|
|
|
|
)
|
2022-05-23 00:54:12 +00:00
|
|
|
if static.ARQ_SESSION and static.ARQ_SESSION_STATE == "connected":
|
2022-05-26 01:23:30 +00:00
|
|
|
# static.ARQ_SESSION_STATE = "connected"
|
2022-06-15 08:40:54 +00:00
|
|
|
self.send_data_to_socket_queue(
|
|
|
|
freedata="tnc-message",
|
|
|
|
arq="session",
|
|
|
|
status="connected",
|
|
|
|
)
|
2022-03-04 15:50:32 +00:00
|
|
|
return True
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_SESSION_STATE = "failed"
|
2022-06-15 08:40:54 +00:00
|
|
|
self.send_data_to_socket_queue(
|
|
|
|
freedata="tnc-message",
|
|
|
|
arq="session",
|
|
|
|
status="failed",
|
|
|
|
reason="timeout",
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
return False
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def open_session(self) -> bool:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Create and send the frame to request a connection.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-05-23 00:54:12 +00:00
|
|
|
True if the session was opened successfully
|
|
|
|
False if the session open request failed
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
self.IS_ARQ_SESSION_MASTER = True
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_SESSION_STATE = "connecting"
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
connection_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
connection_frame[:1] = bytes([FR_TYPE.ARQ_SESSION_OPEN.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
connection_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-05-11 22:10:59 +00:00
|
|
|
connection_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
while not static.ARQ_SESSION:
|
|
|
|
time.sleep(0.01)
|
2022-05-23 02:13:43 +00:00
|
|
|
for attempt in range(self.session_connect_max_retries):
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] SESSION ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]>>?<<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
2022-05-28 15:52:05 +00:00
|
|
|
a=attempt + 1, # Adjust for 0-based for user display
|
2022-05-23 00:54:12 +00:00
|
|
|
state=static.ARQ_SESSION_STATE,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(connection_frame)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Wait for a time, looking to see if `static.ARQ_SESSION`
|
|
|
|
# indicates we've received a positive response from the far station.
|
2022-05-09 00:41:49 +00:00
|
|
|
timeout = time.time() + 3
|
|
|
|
while time.time() < timeout:
|
2022-03-04 15:50:32 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-23 00:54:12 +00:00
|
|
|
# Stop waiting if data channel is opened
|
2022-03-04 15:50:32 +00:00
|
|
|
if static.ARQ_SESSION:
|
2022-04-24 15:05:11 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
# Session connect timeout, send close_session frame to
|
2022-05-28 12:31:58 +00:00
|
|
|
# attempt to clean up the far-side, if it received the
|
2022-04-24 15:05:11 +00:00
|
|
|
# open_session frame and can still hear us.
|
|
|
|
if not static.ARQ_SESSION:
|
|
|
|
self.close_session()
|
|
|
|
return False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Given the while condition, it will only exit when `static.ARQ_SESSION` is True
|
|
|
|
return True
|
|
|
|
|
|
|
|
def received_session_opener(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Received a session open request packet.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
self.IS_ARQ_SESSION_MASTER = False
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_SESSION_STATE = "connecting"
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update arq_session timestamp
|
2022-03-04 15:50:32 +00:00
|
|
|
self.arq_session_last_received = int(time.time())
|
|
|
|
|
2022-04-19 09:09:11 +00:00
|
|
|
static.DXCALLSIGN_CRC = bytes(data_in[4:7])
|
|
|
|
static.DXCALLSIGN = helpers.bytes_to_callsign(bytes(data_in[7:13]))
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] SESSION ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]>>|<<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
state=static.ARQ_SESSION_STATE,
|
|
|
|
)
|
2022-03-04 15:50:32 +00:00
|
|
|
static.ARQ_SESSION = True
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "BUSY"
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
self.transmit_session_heartbeat()
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def close_session(self) -> None:
|
|
|
|
"""Close the ARQ session"""
|
|
|
|
static.ARQ_SESSION_STATE = "disconnecting"
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] SESSION ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]<<X>>["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
state=static.ARQ_SESSION_STATE,
|
|
|
|
)
|
2022-06-05 17:11:09 +00:00
|
|
|
|
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="session",
|
|
|
|
status="close",
|
|
|
|
)
|
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
self.IS_ARQ_SESSION_MASTER = False
|
|
|
|
static.ARQ_SESSION = False
|
2022-05-23 00:54:12 +00:00
|
|
|
self.arq_cleanup()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
self.send_disconnect_frame()
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
def received_session_close(self, data_in: bytes):
|
2022-05-01 15:41:40 +00:00
|
|
|
"""
|
|
|
|
Closes the session when a close session frame is received and
|
|
|
|
the DXCALLSIGN_CRC matches the remote station participating in the session.
|
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-05-01 15:41:40 +00:00
|
|
|
|
|
|
|
"""
|
2022-07-02 20:19:33 +00:00
|
|
|
# We've arrived here from process_data which already checked that the frame
|
|
|
|
# is intended for this station.
|
|
|
|
# Close the session if the CRC matches the remote station in static.
|
2022-05-04 00:08:26 +00:00
|
|
|
_valid_crc, _ = helpers.check_callsign(static.DXCALLSIGN, bytes(data_in[4:7]))
|
|
|
|
if _valid_crc:
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_SESSION_STATE = "disconnected"
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] SESSION ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]<<X>>["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
state=static.ARQ_SESSION_STATE,
|
|
|
|
)
|
2022-06-05 17:11:09 +00:00
|
|
|
|
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="session",
|
|
|
|
status="close",
|
|
|
|
)
|
2022-03-11 19:38:28 +00:00
|
|
|
|
2022-04-30 11:03:24 +00:00
|
|
|
self.IS_ARQ_SESSION_MASTER = False
|
|
|
|
static.ARQ_SESSION = False
|
|
|
|
self.arq_cleanup()
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def transmit_session_heartbeat(self) -> None:
|
|
|
|
"""Send ARQ sesion heartbeat while connected"""
|
2022-04-24 15:05:11 +00:00
|
|
|
# static.ARQ_SESSION = True
|
2022-05-26 01:23:30 +00:00
|
|
|
# static.TNC_STATE = "BUSY"
|
|
|
|
# static.ARQ_SESSION_STATE = "connected"
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
connection_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
connection_frame[:1] = bytes([FR_TYPE.ARQ_SESSION_HB.value])
|
2022-04-19 09:09:11 +00:00
|
|
|
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
connection_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-03-19 11:42:10 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(connection_frame)
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def received_session_heartbeat(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Received an ARQ session heartbeat, record and update state accordingly.
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-05-01 15:34:31 +00:00
|
|
|
# Accept session data if the DXCALLSIGN_CRC matches the station in static.
|
2022-05-04 00:08:26 +00:00
|
|
|
_valid_crc, _ = helpers.check_callsign(static.DXCALLSIGN, bytes(data_in[4:7]))
|
|
|
|
if _valid_crc:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Received session heartbeat")
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"SESSION-HB",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-04-30 11:03:24 +00:00
|
|
|
static.ARQ_SESSION = True
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_SESSION_STATE = "connected"
|
|
|
|
static.TNC_STATE = "BUSY"
|
|
|
|
|
|
|
|
# Update the timeout timestamps
|
|
|
|
self.arq_session_last_received = int(time.time())
|
2022-04-30 11:03:24 +00:00
|
|
|
self.data_channel_last_received = int(time.time())
|
2022-05-23 00:54:12 +00:00
|
|
|
|
2022-04-30 11:03:24 +00:00
|
|
|
if not self.IS_ARQ_SESSION_MASTER and not self.arq_file_transfer:
|
|
|
|
self.transmit_session_heartbeat()
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
##########################################################################################################
|
2022-03-04 15:50:32 +00:00
|
|
|
# ARQ DATA CHANNEL HANDLER
|
2022-05-23 00:54:12 +00:00
|
|
|
##########################################################################################################
|
|
|
|
def open_dc_and_transmit(
|
|
|
|
self,
|
|
|
|
data_out: bytes,
|
|
|
|
mode: int,
|
|
|
|
n_frames_per_burst: int,
|
|
|
|
transmission_uuid: str,
|
|
|
|
mycallsign,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Open data channel and transmit data
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_out:bytes:
|
|
|
|
mode:int:
|
|
|
|
n_frames_per_burst:int:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-05-23 00:54:12 +00:00
|
|
|
True if the data session was opened and the data was sent
|
|
|
|
False if the data session was not opened
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-24 19:49:13 +00:00
|
|
|
# overwrite mycallsign in case of different SSID
|
|
|
|
self.mycallsign = mycallsign
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "BUSY"
|
2022-03-04 15:50:32 +00:00
|
|
|
self.arq_file_transfer = True
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-14 19:21:15 +00:00
|
|
|
self.transmission_uuid = transmission_uuid
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-28 12:31:58 +00:00
|
|
|
# wait a moment for the case, a heartbeat is already on the way back to us
|
2022-03-04 15:50:32 +00:00
|
|
|
if static.ARQ_SESSION:
|
|
|
|
time.sleep(0.5)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-24 22:29:34 +00:00
|
|
|
self.datachannel_timeout = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# we need to compress data for gettin a compression factor.
|
2022-05-28 12:31:58 +00:00
|
|
|
# so we are compressing twice. This is not that nice and maybe there is another way
|
2022-01-07 10:25:28 +00:00
|
|
|
# for calculating transmission statistics
|
2022-07-03 18:06:01 +00:00
|
|
|
# static.ARQ_COMPRESSION_FACTOR = len(data_out) / len(zlib.compress(data_out))
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-19 11:42:10 +00:00
|
|
|
self.arq_open_data_channel(mode, n_frames_per_burst, mycallsign)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# wait until data channel is open
|
2022-01-24 22:29:34 +00:00
|
|
|
while not static.ARQ_STATE and not self.datachannel_timeout:
|
2022-01-07 10:25:28 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-24 22:29:34 +00:00
|
|
|
if static.ARQ_STATE:
|
|
|
|
self.arq_transmit(data_out, mode, n_frames_per_burst)
|
2022-05-23 00:54:12 +00:00
|
|
|
return True
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def arq_open_data_channel(
|
|
|
|
self, mode: int, n_frames_per_burst: int, mycallsign
|
|
|
|
) -> bool:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Open an ARQ data channel.
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
mode:int:
|
|
|
|
n_frames_per_burst:int:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-05-23 00:54:12 +00:00
|
|
|
True if the data channel was opened successfully
|
|
|
|
False if the data channel failed to open
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-09 00:41:49 +00:00
|
|
|
self.is_IRS = False
|
2022-05-23 00:54:12 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-01-07 10:25:28 +00:00
|
|
|
self.data_channel_last_received = int(time.time())
|
|
|
|
|
2022-06-19 20:45:06 +00:00
|
|
|
if static.LOW_BANDWIDTH_MODE:
|
2022-06-21 22:33:55 +00:00
|
|
|
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_N.value])
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Requesting low bandwidth mode")
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-02-08 14:27:34 +00:00
|
|
|
else:
|
2022-06-21 22:33:55 +00:00
|
|
|
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_W.value])
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Requesting high bandwidth mode")
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
connection_frame = bytearray(14)
|
|
|
|
connection_frame[:1] = frametype
|
|
|
|
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
connection_frame[4:7] = static.MYCALLSIGN_CRC
|
|
|
|
connection_frame[7:13] = helpers.callsign_to_bytes(mycallsign)
|
2022-05-09 00:41:49 +00:00
|
|
|
connection_frame[13:14] = bytes([n_frames_per_burst])
|
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
while not static.ARQ_STATE:
|
|
|
|
time.sleep(0.01)
|
2022-05-23 00:54:12 +00:00
|
|
|
for attempt in range(self.data_channel_max_retries):
|
2022-06-05 17:11:09 +00:00
|
|
|
|
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="opening",
|
|
|
|
)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | DATA | TX | ["
|
|
|
|
+ str(mycallsign, "UTF-8")
|
|
|
|
+ "]>> <<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
attempt=f"{str(attempt+1)}/{str(self.data_channel_max_retries)}",
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
|
|
|
|
self.enqueue_frame_for_tx(connection_frame)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
|
|
|
timeout = time.time() + 3
|
|
|
|
while time.time() < timeout:
|
2022-01-07 10:25:28 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-23 00:54:12 +00:00
|
|
|
# Stop waiting if data channel is opened
|
2022-01-07 10:25:28 +00:00
|
|
|
if static.ARQ_STATE:
|
2022-05-23 00:54:12 +00:00
|
|
|
return True
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# `data_channel_max_retries` attempts have been sent. Aborting attempt & cleaning up
|
2022-06-05 17:11:09 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] arq_open_data_channel:", transmission_uuid=self.transmission_uuid
|
|
|
|
)
|
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-05-30 15:41:24 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="failed",
|
2022-06-06 18:44:35 +00:00
|
|
|
reason="unknown",
|
2022-05-30 15:41:24 +00:00
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
percent=static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
bytesperminute=static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
)
|
2022-05-23 00:54:12 +00:00
|
|
|
|
|
|
|
self.log.warning(
|
|
|
|
"[TNC] ARQ | TX | DATA ["
|
|
|
|
+ str(mycallsign, "UTF-8")
|
|
|
|
+ "]>>X<<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]"
|
|
|
|
)
|
|
|
|
self.datachannel_timeout = True
|
|
|
|
self.arq_cleanup()
|
2021-11-19 16:47:25 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Attempt to cleanup the far-side, if it received the
|
2022-05-23 00:54:12 +00:00
|
|
|
# open_session frame and can still hear us.
|
|
|
|
self.close_session()
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Shouldn't get here..
|
|
|
|
return True
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
def arq_received_data_channel_opener(self, data_in: bytes):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Received request to open data channel framt
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-07-02 20:19:33 +00:00
|
|
|
# We've arrived here from process_data which already checked that the frame
|
|
|
|
# is intended for this station.
|
2022-03-04 15:50:32 +00:00
|
|
|
self.arq_file_transfer = True
|
2022-05-09 00:41:49 +00:00
|
|
|
self.is_IRS = True
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="opening",
|
|
|
|
)
|
2022-07-02 20:19:33 +00:00
|
|
|
|
2022-04-19 09:09:11 +00:00
|
|
|
static.DXCALLSIGN_CRC = bytes(data_in[4:7])
|
|
|
|
static.DXCALLSIGN = helpers.bytes_to_callsign(bytes(data_in[7:13]))
|
2022-03-19 11:42:10 +00:00
|
|
|
|
2022-07-02 20:19:33 +00:00
|
|
|
# n_frames_per_burst is currently unused
|
|
|
|
# n_frames_per_burst = int.from_bytes(bytes(data_in[13:14]), "big")
|
|
|
|
|
2022-02-08 14:27:34 +00:00
|
|
|
frametype = int.from_bytes(bytes(data_in[:1]), "big")
|
2022-05-23 01:11:40 +00:00
|
|
|
# check if we received low bandwidth mode
|
2022-06-21 22:33:55 +00:00
|
|
|
if frametype == FR_TYPE.ARQ_DC_OPEN_W.value:
|
2022-05-28 12:08:33 +00:00
|
|
|
self.received_LOW_BANDWIDTH_MODE = False
|
2022-05-09 00:41:49 +00:00
|
|
|
self.mode_list = self.mode_list_high_bw
|
2022-02-08 14:27:34 +00:00
|
|
|
self.time_list = self.time_list_high_bw
|
|
|
|
else:
|
2022-05-28 12:08:33 +00:00
|
|
|
self.received_LOW_BANDWIDTH_MODE = True
|
2022-05-09 00:41:49 +00:00
|
|
|
self.mode_list = self.mode_list_low_bw
|
2022-02-08 14:27:34 +00:00
|
|
|
self.time_list = self.time_list_low_bw
|
2022-05-11 22:10:59 +00:00
|
|
|
self.speed_level = len(self.mode_list) - 1
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Update modes we are listening to
|
2022-02-08 14:27:34 +00:00
|
|
|
self.set_listening_modes(self.mode_list[self.speed_level])
|
2021-05-09 15:55:15 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-19 11:42:10 +00:00
|
|
|
# check if callsign ssid override
|
2022-07-02 20:19:33 +00:00
|
|
|
_, mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | DATA | RX | ["
|
|
|
|
+ str(mycallsign, "UTF-8")
|
|
|
|
+ "]>> <<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
2022-05-23 01:11:40 +00:00
|
|
|
bandwidth="wide",
|
2022-05-23 00:54:12 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-04-11 09:03:54 +00:00
|
|
|
static.ARQ_STATE = True
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "BUSY"
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-02-08 14:27:34 +00:00
|
|
|
self.reset_statistics()
|
2021-03-16 14:21:58 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-02-08 14:27:34 +00:00
|
|
|
self.data_channel_last_received = int(time.time())
|
2022-05-23 00:54:12 +00:00
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
# Select the frame type based on the current TNC mode
|
2022-05-28 12:08:33 +00:00
|
|
|
if static.LOW_BANDWIDTH_MODE or self.received_LOW_BANDWIDTH_MODE:
|
2022-06-21 22:33:55 +00:00
|
|
|
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_ACK_N.value])
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Responding with low bandwidth mode")
|
2022-02-08 14:27:34 +00:00
|
|
|
else:
|
2022-06-21 22:33:55 +00:00
|
|
|
frametype = bytes([FR_TYPE.ARQ_DC_OPEN_ACK_W.value])
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Responding with high bandwidth mode")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
connection_frame = bytearray(14)
|
|
|
|
connection_frame[:1] = frametype
|
|
|
|
connection_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
connection_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-05-23 02:13:43 +00:00
|
|
|
# For checking protocol version on the receiving side
|
|
|
|
connection_frame[13:14] = bytes([static.ARQ_PROTOCOL_VERSION])
|
2022-01-07 11:55:03 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(connection_frame)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | DATA | RX | ["
|
|
|
|
+ str(mycallsign, "UTF-8")
|
|
|
|
+ "]>>|<<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
2022-05-23 01:11:40 +00:00
|
|
|
bandwidth="wide",
|
2022-05-23 00:54:12 +00:00
|
|
|
snr=static.SNR,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# set start of transmission for our statistics
|
|
|
|
self.rx_start_of_transmission = time.time()
|
2021-02-28 14:24:14 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-05-19 20:15:24 +00:00
|
|
|
self.data_channel_last_received = int(time.time())
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def arq_received_channel_is_open(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Called if we received a data channel opener
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-02-08 14:27:34 +00:00
|
|
|
protocol_version = int.from_bytes(bytes(data_in[13:14]), "big")
|
|
|
|
if protocol_version == static.ARQ_PROTOCOL_VERSION:
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
2022-06-06 18:31:15 +00:00
|
|
|
status="opened",
|
2022-06-05 17:11:09 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
frametype = int.from_bytes(bytes(data_in[:1]), "big")
|
|
|
|
|
2022-06-21 22:33:55 +00:00
|
|
|
if frametype == FR_TYPE.ARQ_DC_OPEN_ACK_N.value:
|
2022-05-28 12:08:33 +00:00
|
|
|
self.received_LOW_BANDWIDTH_MODE = True
|
2022-05-09 00:41:49 +00:00
|
|
|
self.mode_list = self.mode_list_low_bw
|
2022-02-08 14:27:34 +00:00
|
|
|
self.time_list = self.time_list_low_bw
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] low bandwidth mode", modes=self.mode_list)
|
2022-02-08 14:27:34 +00:00
|
|
|
else:
|
2022-05-28 12:08:33 +00:00
|
|
|
self.received_LOW_BANDWIDTH_MODE = False
|
2022-05-09 00:41:49 +00:00
|
|
|
self.mode_list = self.mode_list_high_bw
|
2022-02-08 14:27:34 +00:00
|
|
|
self.time_list = self.time_list_high_bw
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] high bandwidth mode", modes=self.mode_list)
|
2022-05-23 02:13:43 +00:00
|
|
|
self.speed_level = len(self.mode_list) - 1
|
2022-05-23 00:54:12 +00:00
|
|
|
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"DATA-CHANNEL",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.log.info(
|
|
|
|
"[TNC] ARQ | DATA | TX | ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]>>|<<["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
snr=static.SNR,
|
|
|
|
)
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
# as soon as we set ARQ_STATE to DATA, transmission starts
|
2022-02-08 14:27:34 +00:00
|
|
|
static.ARQ_STATE = True
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-02-08 14:27:34 +00:00
|
|
|
self.data_channel_last_received = int(time.time())
|
|
|
|
else:
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "IDLE"
|
2022-02-08 14:27:34 +00:00
|
|
|
static.ARQ_STATE = False
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="failed",
|
|
|
|
reason="protocol version missmatch",
|
|
|
|
)
|
2022-05-28 15:52:05 +00:00
|
|
|
# TODO: We should display a message to this effect on the UI.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.warning(
|
|
|
|
"[TNC] protocol version mismatch:",
|
|
|
|
received=protocol_version,
|
|
|
|
own=static.ARQ_PROTOCOL_VERSION,
|
|
|
|
)
|
2022-02-08 14:27:34 +00:00
|
|
|
self.arq_cleanup()
|
2021-02-28 14:24:14 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# ---------- PING
|
2022-05-23 00:54:12 +00:00
|
|
|
def transmit_ping(self, dxcallsign: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Funktion for controlling pings
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
dxcallsign:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-06-17 23:48:47 +00:00
|
|
|
if not str(dxcallsign).strip():
|
2022-07-03 18:06:01 +00:00
|
|
|
# TODO: We should display a message to this effect on the UI.
|
2022-06-17 23:48:47 +00:00
|
|
|
self.log.warning("[TNC] Missing required callsign", dxcallsign=dxcallsign)
|
|
|
|
return
|
2022-02-21 11:20:36 +00:00
|
|
|
static.DXCALLSIGN = dxcallsign
|
2022-04-19 09:09:11 +00:00
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-17 23:48:47 +00:00
|
|
|
self.send_data_to_socket_queue(freedata="tnc-message", ping="transmitting")
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] PING REQ ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "] >>> ["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]"
|
|
|
|
)
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
ping_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
ping_frame[:1] = bytes([FR_TYPE.PING.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
ping_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
ping_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-04-19 09:09:11 +00:00
|
|
|
ping_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
2021-10-17 14:59:15 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ENABLE FSK", state=static.ENABLE_FSK)
|
2022-03-31 10:45:44 +00:00
|
|
|
if static.ENABLE_FSK:
|
2022-06-24 13:22:46 +00:00
|
|
|
self.enqueue_frame_for_tx(ping_frame, c2_mode=FREEDV_MODE.fsk_ldpc_0.value)
|
2022-03-31 10:45:44 +00:00
|
|
|
else:
|
2022-06-24 13:22:46 +00:00
|
|
|
self.enqueue_frame_for_tx(ping_frame, c2_mode=FREEDV_MODE.datac0.value)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def received_ping(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Called if we received a ping
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-07-02 20:20:14 +00:00
|
|
|
dxcallsign_CRC = bytes(data_in[4:7])
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[7:13]))
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
2022-07-02 20:20:14 +00:00
|
|
|
dxcallsign,
|
2022-05-23 00:54:12 +00:00
|
|
|
static.DXGRID,
|
|
|
|
"PING",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-07 09:31:59 +00:00
|
|
|
ping="received",
|
|
|
|
uuid=str(uuid.uuid4()),
|
|
|
|
timestamp=int(time.time()),
|
|
|
|
mycallsign=str(self.mycallsign, "UTF-8"),
|
2022-07-02 20:20:14 +00:00
|
|
|
dxcallsign=str(dxcallsign, "UTF-8"),
|
2022-06-05 17:11:09 +00:00
|
|
|
dxgrid=str(static.DXGRID, "UTF-8"),
|
2022-06-21 22:33:55 +00:00
|
|
|
snr=str(static.SNR),
|
2022-06-05 17:11:09 +00:00
|
|
|
)
|
2022-03-19 11:42:10 +00:00
|
|
|
# check if callsign ssid override
|
2022-04-19 09:09:11 +00:00
|
|
|
valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4])
|
2022-04-10 20:45:05 +00:00
|
|
|
if not valid:
|
|
|
|
# PING packet not for me.
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] received_ping: ping not for this station.")
|
2022-04-10 20:45:05 +00:00
|
|
|
return
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-07-02 20:20:14 +00:00
|
|
|
static.DXCALLSIGN_CRC = dxcallsign_CRC
|
|
|
|
static.DXCALLSIGN = dxcallsign
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] PING REQ ["
|
|
|
|
+ str(mycallsign, "UTF-8")
|
|
|
|
+ "] <<< ["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
snr=static.SNR,
|
|
|
|
)
|
2021-02-24 15:47:52 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
ping_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
ping_frame[:1] = bytes([FR_TYPE.PING_ACK.value])
|
2022-04-19 09:09:11 +00:00
|
|
|
ping_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
ping_frame[4:7] = static.MYCALLSIGN_CRC
|
|
|
|
ping_frame[7:13] = static.MYGRID
|
2021-03-12 13:14:36 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ENABLE FSK", state=static.ENABLE_FSK)
|
2022-03-31 10:45:44 +00:00
|
|
|
if static.ENABLE_FSK:
|
2022-06-24 13:22:46 +00:00
|
|
|
self.enqueue_frame_for_tx(ping_frame, c2_mode=FREEDV_MODE.fsk_ldpc_0.value)
|
2022-03-31 10:45:44 +00:00
|
|
|
else:
|
2022-06-24 13:22:46 +00:00
|
|
|
self.enqueue_frame_for_tx(ping_frame, c2_mode=FREEDV_MODE.datac0.value)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def received_ping_ack(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Called if a PING ack has been received
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-04-19 09:09:11 +00:00
|
|
|
static.DXCALLSIGN_CRC = bytes(data_in[4:7])
|
2022-05-23 00:54:12 +00:00
|
|
|
static.DXGRID = bytes(data_in[7:13]).rstrip(b"\x00")
|
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-06 18:44:35 +00:00
|
|
|
ping="acknowledge",
|
2022-05-30 15:41:24 +00:00
|
|
|
uuid=str(uuid.uuid4()),
|
|
|
|
timestamp=int(time.time()),
|
|
|
|
mycallsign=str(self.mycallsign, "UTF-8"),
|
|
|
|
dxcallsign=str(static.DXCALLSIGN, "UTF-8"),
|
|
|
|
dxgrid=str(static.DXGRID, "UTF-8"),
|
|
|
|
snr=str(static.SNR),
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
static.DXCALLSIGN,
|
|
|
|
static.DXGRID,
|
|
|
|
"PING-ACK",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] PING ACK ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "] >|< ["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]",
|
|
|
|
snr=static.SNR,
|
|
|
|
)
|
|
|
|
static.TNC_STATE = "IDLE"
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def stop_transmission(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
|
|
|
Force a stop of the running transmission
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.warning("[TNC] Stopping transmission!")
|
2022-05-19 20:15:24 +00:00
|
|
|
stop_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
stop_frame[:1] = bytes([FR_TYPE.ARQ_STOP.value])
|
2022-04-19 09:09:11 +00:00
|
|
|
stop_frame[1:4] = static.DXCALLSIGN_CRC
|
|
|
|
stop_frame[4:7] = static.MYCALLSIGN_CRC
|
2022-05-19 20:15:24 +00:00
|
|
|
stop_frame[7:13] = helpers.callsign_to_bytes(self.mycallsign)
|
2021-12-06 19:16:14 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(stop_frame, copies=2, repeat_delay=250)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "IDLE"
|
2022-01-10 17:09:38 +00:00
|
|
|
static.ARQ_STATE = False
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="stopped",
|
2022-06-06 18:31:15 +00:00
|
|
|
)
|
2022-01-10 17:09:38 +00:00
|
|
|
self.arq_cleanup()
|
2021-12-06 19:16:14 +00:00
|
|
|
|
2022-07-02 20:14:05 +00:00
|
|
|
def received_stop_transmission(
|
|
|
|
self, data_in: bytes
|
|
|
|
) -> None: # pylint: disable=unused-argument
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
|
|
|
Received a transmission stop
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.warning("[TNC] Stopping transmission!")
|
|
|
|
static.TNC_STATE = "IDLE"
|
2022-01-10 17:09:38 +00:00
|
|
|
static.ARQ_STATE = False
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="stopped",
|
2022-06-06 19:07:41 +00:00
|
|
|
uuid=self.transmission_uuid,
|
|
|
|
mycallsign=str(self.mycallsign, "UTF-8"),
|
2022-06-05 17:11:09 +00:00
|
|
|
dxcallsign=str(static.DXCALLSIGN, "UTF-8"),
|
|
|
|
)
|
2022-01-10 17:09:38 +00:00
|
|
|
self.arq_cleanup()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# ----------- BROADCASTS
|
2022-05-23 00:54:12 +00:00
|
|
|
def run_beacon(self) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-19 20:15:24 +00:00
|
|
|
Controlling function for running a beacon
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-23 07:46:42 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
self: arq class
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-01-07 10:25:28 +00:00
|
|
|
try:
|
2022-05-28 02:17:15 +00:00
|
|
|
while True:
|
2022-03-10 19:46:34 +00:00
|
|
|
time.sleep(0.5)
|
|
|
|
while static.BEACON_STATE:
|
2022-05-23 00:54:12 +00:00
|
|
|
if (
|
|
|
|
not static.ARQ_SESSION
|
|
|
|
and not self.arq_file_transfer
|
|
|
|
and not static.BEACON_PAUSE
|
|
|
|
):
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
beacon="transmitting",
|
2022-06-06 18:44:35 +00:00
|
|
|
interval=self.beacon_interval,
|
2022-06-05 17:11:09 +00:00
|
|
|
)
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] Sending beacon!", interval=self.beacon_interval
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-10 19:46:34 +00:00
|
|
|
beacon_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
beacon_frame[:1] = bytes([FR_TYPE.BEACON.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
beacon_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign)
|
2022-03-10 19:46:34 +00:00
|
|
|
beacon_frame[9:13] = static.MYGRID[:4]
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ENABLE FSK", state=static.ENABLE_FSK)
|
2022-03-10 19:46:34 +00:00
|
|
|
|
2022-03-31 10:45:44 +00:00
|
|
|
if static.ENABLE_FSK:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.enqueue_frame_for_tx(
|
|
|
|
beacon_frame,
|
2022-06-24 13:22:46 +00:00
|
|
|
c2_mode=FREEDV_MODE.fsk_ldpc_0.value,
|
2022-05-23 00:54:12 +00:00
|
|
|
)
|
2022-03-31 10:45:44 +00:00
|
|
|
else:
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(beacon_frame)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-10 19:46:34 +00:00
|
|
|
interval_timer = time.time() + self.beacon_interval
|
2022-05-23 00:54:12 +00:00
|
|
|
while (
|
|
|
|
time.time() < interval_timer
|
|
|
|
and static.BEACON_STATE
|
|
|
|
and not static.BEACON_PAUSE
|
|
|
|
):
|
2022-03-10 19:46:34 +00:00
|
|
|
time.sleep(0.01)
|
2022-02-21 11:20:36 +00:00
|
|
|
|
2022-05-25 22:27:33 +00:00
|
|
|
except Exception as err:
|
2022-05-28 02:17:15 +00:00
|
|
|
self.log.debug("[TNC] run_beacon: ", exception=err)
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def received_beacon(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Called if we received a beacon
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-04-15 14:49:40 +00:00
|
|
|
# here we add the received station to the heard stations buffer
|
2022-04-17 20:16:13 +00:00
|
|
|
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[1:7]))
|
2022-05-23 00:54:12 +00:00
|
|
|
dxgrid = bytes(data_in[9:13]).rstrip(b"\x00")
|
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-07 08:08:15 +00:00
|
|
|
beacon="received",
|
2022-05-30 15:41:24 +00:00
|
|
|
uuid=str(uuid.uuid4()),
|
|
|
|
timestamp=int(time.time()),
|
|
|
|
mycallsign=str(self.mycallsign, "UTF-8"),
|
|
|
|
dxcallsign=str(dxcallsign, "UTF-8"),
|
|
|
|
dxgrid=str(dxgrid, "UTF-8"),
|
|
|
|
snr=str(static.SNR),
|
|
|
|
)
|
2022-06-07 08:08:15 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] BEACON RCVD ["
|
|
|
|
+ str(dxcallsign, "UTF-8")
|
|
|
|
+ "]["
|
|
|
|
+ str(dxgrid, "UTF-8")
|
|
|
|
+ "] ",
|
|
|
|
snr=static.SNR,
|
|
|
|
)
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
dxcallsign,
|
|
|
|
dxgrid,
|
|
|
|
"BEACON",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
|
|
|
|
|
|
|
def transmit_cq(self) -> None:
|
2022-04-15 19:13:03 +00:00
|
|
|
"""
|
|
|
|
Transmit a CQ
|
2022-05-11 22:10:59 +00:00
|
|
|
Args:
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing
|
2022-04-15 19:13:03 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] CQ CQ CQ")
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
cq="transmitting",
|
|
|
|
)
|
2022-05-19 20:15:24 +00:00
|
|
|
cq_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
cq_frame[:1] = bytes([FR_TYPE.CQ.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
cq_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign)
|
2022-05-23 00:54:12 +00:00
|
|
|
cq_frame[7:11] = helpers.encode_grid(static.MYGRID.decode("UTF-8"))
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ENABLE FSK", state=static.ENABLE_FSK)
|
|
|
|
self.log.debug("[TNC] CQ Frame:", data=[cq_frame])
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-04-15 14:49:40 +00:00
|
|
|
if static.ENABLE_FSK:
|
2022-06-24 13:22:46 +00:00
|
|
|
self.enqueue_frame_for_tx(cq_frame, c2_mode=FREEDV_MODE.fsk_ldpc_0.value)
|
2022-04-15 14:49:40 +00:00
|
|
|
else:
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(cq_frame)
|
2022-04-15 14:49:40 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def received_cq(self, data_in: bytes) -> None:
|
2022-04-15 14:49:40 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Called when we receive a CQ frame
|
2022-04-15 14:49:40 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data_in:bytes:
|
2022-04-15 14:49:40 +00:00
|
|
|
|
|
|
|
Returns:
|
2022-05-11 22:10:59 +00:00
|
|
|
Nothing
|
2022-04-15 14:49:40 +00:00
|
|
|
"""
|
|
|
|
# here we add the received station to the heard stations buffer
|
2022-04-17 20:16:13 +00:00
|
|
|
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[1:7]))
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] received_cq:", dxcallsign=dxcallsign)
|
|
|
|
dxgrid = bytes(helpers.decode_grid(data_in[7:11]), "UTF-8")
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-07 08:08:15 +00:00
|
|
|
cq="received",
|
2022-06-06 19:07:41 +00:00
|
|
|
mycallsign=str(self.mycallsign, "UTF-8"),
|
2022-06-05 17:11:09 +00:00
|
|
|
dxcallsign=str(static.DXCALLSIGN, "UTF-8"),
|
|
|
|
dxgrid=str(static.DXGRID, "UTF-8"),
|
|
|
|
)
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] CQ RCVD ["
|
|
|
|
+ str(dxcallsign, "UTF-8")
|
|
|
|
+ "]["
|
|
|
|
+ str(dxgrid, "UTF-8")
|
|
|
|
+ "] ",
|
|
|
|
snr=static.SNR,
|
|
|
|
)
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
dxcallsign,
|
|
|
|
dxgrid,
|
|
|
|
"CQ CQ CQ",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-04-15 14:49:40 +00:00
|
|
|
|
2022-04-18 15:17:53 +00:00
|
|
|
if static.RESPOND_TO_CQ:
|
|
|
|
self.transmit_qrv()
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def transmit_qrv(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Called when we send a QRV frame
|
2022-04-15 19:13:03 +00:00
|
|
|
Args:
|
2022-05-19 20:15:24 +00:00
|
|
|
self
|
2022-04-15 19:13:03 +00:00
|
|
|
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-04-15 19:13:03 +00:00
|
|
|
# Sleep a random amount of time before responding to make it more likely to be
|
|
|
|
# heard when many stations respond. Each DATAC0 frame is 0.44 sec (440ms) in
|
|
|
|
# duration, plus overhead. Set the wait interval to be random between 0 and 2s
|
2022-04-15 23:59:12 +00:00
|
|
|
# in 0.5s increments.
|
|
|
|
helpers.wait(randrange(0, 20, 5) / 10.0)
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
qrv="transmitting",
|
|
|
|
)
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] Sending QRV!")
|
2022-04-15 19:13:03 +00:00
|
|
|
|
2022-05-19 20:15:24 +00:00
|
|
|
qrv_frame = bytearray(14)
|
2022-06-21 22:33:55 +00:00
|
|
|
qrv_frame[:1] = bytes([FR_TYPE.QRV.value])
|
2022-05-19 20:15:24 +00:00
|
|
|
qrv_frame[1:7] = helpers.callsign_to_bytes(self.mycallsign)
|
2022-05-23 00:54:12 +00:00
|
|
|
qrv_frame[7:11] = helpers.encode_grid(static.MYGRID.decode("UTF-8"))
|
2022-04-15 19:13:03 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info("[TNC] ENABLE FSK", state=static.ENABLE_FSK)
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-03-31 10:45:44 +00:00
|
|
|
if static.ENABLE_FSK:
|
2022-06-24 13:22:46 +00:00
|
|
|
self.enqueue_frame_for_tx(qrv_frame, c2_mode=FREEDV_MODE.fsk_ldpc_0.value)
|
2022-03-31 10:45:44 +00:00
|
|
|
else:
|
2022-05-11 22:10:59 +00:00
|
|
|
self.enqueue_frame_for_tx(qrv_frame)
|
2021-09-26 15:51:11 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def received_qrv(self, data_in: bytes) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Called when we receive a QRV frame
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-04-15 19:13:03 +00:00
|
|
|
data_in:bytes:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-01-07 10:25:28 +00:00
|
|
|
# here we add the received station to the heard stations buffer
|
2022-04-17 20:16:13 +00:00
|
|
|
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[1:7]))
|
2022-05-23 00:54:12 +00:00
|
|
|
dxgrid = bytes(helpers.decode_grid(data_in[7:11]), "UTF-8")
|
|
|
|
|
2022-05-30 15:41:24 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-06 18:44:35 +00:00
|
|
|
qrv="received",
|
2022-05-30 15:41:24 +00:00
|
|
|
)
|
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] QRV RCVD ["
|
|
|
|
+ str(dxcallsign, "UTF-8")
|
|
|
|
+ "]["
|
|
|
|
+ str(dxgrid, "UTF-8")
|
|
|
|
+ "] ",
|
|
|
|
snr=static.SNR,
|
|
|
|
)
|
|
|
|
helpers.add_to_heard_stations(
|
|
|
|
dxcallsign,
|
|
|
|
dxgrid,
|
|
|
|
"QRV",
|
|
|
|
static.SNR,
|
|
|
|
static.FREQ_OFFSET,
|
|
|
|
static.HAMLIB_FREQUENCY,
|
|
|
|
)
|
2022-04-15 19:13:03 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# ------------ CALUCLATE TRANSFER RATES
|
2022-05-23 00:54:12 +00:00
|
|
|
def calculate_transfer_rate_rx(
|
|
|
|
self, rx_start_of_transmission: float, receivedbytes: int
|
|
|
|
) -> list:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
Calculate transfer rate for received data
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
rx_start_of_transmission:float:
|
|
|
|
receivedbytes:int:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
Returns: List of:
|
|
|
|
bits_per_second: float,
|
|
|
|
bytes_per_minute: float,
|
|
|
|
transmission_percent: float
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-09 00:41:49 +00:00
|
|
|
try:
|
2022-03-14 19:21:15 +00:00
|
|
|
if static.TOTAL_BYTES == 0:
|
|
|
|
static.TOTAL_BYTES = 1
|
2022-05-19 20:15:24 +00:00
|
|
|
static.ARQ_TRANSMISSION_PERCENT = min(
|
2022-05-23 00:54:12 +00:00
|
|
|
int(
|
|
|
|
(
|
|
|
|
receivedbytes
|
|
|
|
* static.ARQ_COMPRESSION_FACTOR
|
|
|
|
/ (static.TOTAL_BYTES)
|
|
|
|
)
|
|
|
|
* 100
|
|
|
|
),
|
|
|
|
100,
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
transmissiontime = time.time() - self.rx_start_of_transmission
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
if receivedbytes > 0:
|
2022-05-11 22:10:59 +00:00
|
|
|
static.ARQ_BITS_PER_SECOND = int((receivedbytes * 8) / transmissiontime)
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_BYTES_PER_MINUTE = int(
|
|
|
|
(receivedbytes) / (transmissiontime / 60)
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
|
|
|
static.ARQ_BITS_PER_SECOND = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
static.ARQ_BYTES_PER_MINUTE = 0
|
2022-05-25 22:27:33 +00:00
|
|
|
except Exception as err:
|
2022-05-28 02:17:15 +00:00
|
|
|
self.log.error(f"[TNC] calculate_transfer_rate_rx: Exception: {err}")
|
2022-01-07 10:25:28 +00:00
|
|
|
static.ARQ_TRANSMISSION_PERCENT = 0.0
|
2021-09-26 15:51:11 +00:00
|
|
|
static.ARQ_BITS_PER_SECOND = 0
|
2022-01-07 10:25:28 +00:00
|
|
|
static.ARQ_BYTES_PER_MINUTE = 0
|
2021-10-05 17:59:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
return [
|
|
|
|
static.ARQ_BITS_PER_SECOND,
|
|
|
|
static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
]
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def reset_statistics(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
|
|
|
Reset statistics
|
|
|
|
"""
|
2022-02-08 14:27:34 +00:00
|
|
|
# reset ARQ statistics
|
|
|
|
static.ARQ_BYTES_PER_MINUTE_BURST = 0
|
2022-06-19 12:47:56 +00:00
|
|
|
static.ARQ_BYTES_PER_MINUTE = 0
|
|
|
|
static.ARQ_BITS_PER_SECOND_BURST = 0
|
|
|
|
static.ARQ_BITS_PER_SECOND = 0
|
|
|
|
static.ARQ_TRANSMISSION_PERCENT = 0
|
|
|
|
static.TOTAL_BYTES = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def calculate_transfer_rate_tx(
|
|
|
|
self, tx_start_of_transmission: float, sentbytes: int, tx_buffer_length: int
|
|
|
|
) -> list:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-05-23 07:37:24 +00:00
|
|
|
Calculate transfer rate for transmission
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
tx_start_of_transmission:float:
|
|
|
|
sentbytes:int:
|
|
|
|
tx_buffer_length:int:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
Returns: List of:
|
|
|
|
bits_per_second: float,
|
|
|
|
bytes_per_minute: float,
|
|
|
|
transmission_percent: float
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-01-07 10:25:28 +00:00
|
|
|
try:
|
2022-05-23 00:54:12 +00:00
|
|
|
static.ARQ_TRANSMISSION_PERCENT = min(
|
|
|
|
int((sentbytes / tx_buffer_length) * 100), 100
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
transmissiontime = time.time() - tx_start_of_transmission
|
2021-09-27 15:33:59 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
if sentbytes > 0:
|
2022-05-23 02:13:43 +00:00
|
|
|
static.ARQ_BITS_PER_SECOND = int((sentbytes * 8) / transmissiontime)
|
|
|
|
static.ARQ_BYTES_PER_MINUTE = int((sentbytes) / (transmissiontime / 60))
|
2021-09-27 15:33:59 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
|
|
|
static.ARQ_BITS_PER_SECOND = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
static.ARQ_BYTES_PER_MINUTE = 0
|
|
|
|
|
2022-05-25 22:27:33 +00:00
|
|
|
except Exception as err:
|
2022-05-28 02:17:15 +00:00
|
|
|
self.log.error(f"[TNC] calculate_transfer_rate_tx: Exception: {err}")
|
2022-01-07 10:25:28 +00:00
|
|
|
static.ARQ_TRANSMISSION_PERCENT = 0.0
|
|
|
|
static.ARQ_BITS_PER_SECOND = 0
|
|
|
|
static.ARQ_BYTES_PER_MINUTE = 0
|
2021-09-26 15:51:11 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
return [
|
|
|
|
static.ARQ_BITS_PER_SECOND,
|
|
|
|
static.ARQ_BYTES_PER_MINUTE,
|
|
|
|
static.ARQ_TRANSMISSION_PERCENT,
|
|
|
|
]
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# ----------------------CLEANUP AND RESET FUNCTIONS
|
2022-05-23 00:54:12 +00:00
|
|
|
def arq_cleanup(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Cleanup function which clears all ARQ states
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
if TESTMODE:
|
2022-06-05 18:09:38 +00:00
|
|
|
self.log.debug("[TNC] TESTMODE: arq_cleanup: Not performing cleanup.")
|
2022-05-23 00:54:12 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.log.debug("[TNC] arq_cleanup")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:44:35 +00:00
|
|
|
self.rx_frame_bof_received = False
|
|
|
|
self.rx_frame_eof_received = False
|
2022-01-07 11:55:03 +00:00
|
|
|
self.burst_ack = False
|
2022-01-07 10:25:28 +00:00
|
|
|
self.rpt_request_received = False
|
|
|
|
self.data_frame_ack_received = False
|
|
|
|
static.RX_BURST_BUFFER = []
|
2022-05-23 00:54:12 +00:00
|
|
|
static.RX_FRAME_BUFFER = b""
|
2022-05-19 20:15:24 +00:00
|
|
|
self.burst_ack_snr = 255
|
2022-01-07 10:25:28 +00:00
|
|
|
|
2022-01-15 18:23:28 +00:00
|
|
|
# reset modem receiving state to reduce cpu load
|
|
|
|
modem.RECEIVE_DATAC1 = False
|
|
|
|
modem.RECEIVE_DATAC3 = False
|
2022-05-19 20:15:24 +00:00
|
|
|
# modem.RECEIVE_FSK_LDPC_0 = False
|
2022-03-24 19:49:13 +00:00
|
|
|
modem.RECEIVE_FSK_LDPC_1 = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-15 19:17:19 +00:00
|
|
|
# reset buffer overflow counter
|
2022-05-11 22:10:59 +00:00
|
|
|
static.BUFFER_OVERFLOW_COUNTER = [0, 0, 0, 0, 0]
|
2022-02-02 20:12:16 +00:00
|
|
|
|
|
|
|
self.is_IRS = False
|
|
|
|
self.burst_nack = False
|
|
|
|
self.burst_nack_counter = 0
|
|
|
|
self.frame_received_counter = 0
|
|
|
|
self.speed_level = len(self.mode_list) - 1
|
2022-02-22 20:05:48 +00:00
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 01:11:40 +00:00
|
|
|
# low bandwidth mode indicator
|
2022-05-28 12:08:33 +00:00
|
|
|
self.received_LOW_BANDWIDTH_MODE = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-08 14:27:34 +00:00
|
|
|
# reset retry counter for rx channel / burst
|
|
|
|
self.n_retries_per_burst = 0
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
if not static.ARQ_SESSION:
|
2022-05-23 00:54:12 +00:00
|
|
|
static.TNC_STATE = "IDLE"
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
static.ARQ_STATE = False
|
|
|
|
self.arq_file_transfer = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-10 19:46:34 +00:00
|
|
|
static.BEACON_PAUSE = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def arq_reset_ack(self, state: bool) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Funktion for resetting acknowledge states
|
2022-03-04 15:50:32 +00:00
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
state:bool:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-01-07 11:55:03 +00:00
|
|
|
self.burst_ack = state
|
2022-01-07 10:25:28 +00:00
|
|
|
self.rpt_request_received = state
|
|
|
|
self.data_frame_ack_received = state
|
2021-09-26 15:51:11 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def set_listening_modes(self, mode: int) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
2022-03-06 16:23:04 +00:00
|
|
|
Function for setting the data modes we are listening to for saving cpu power
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-23 00:54:12 +00:00
|
|
|
mode:int: Codec2 mode to listen for
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
# set modes we want to listen to
|
2022-06-24 13:22:46 +00:00
|
|
|
if mode == FREEDV_MODE.datac1.value:
|
2022-02-02 20:12:16 +00:00
|
|
|
modem.RECEIVE_DATAC1 = True
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Changing listening data mode", mode="datac1")
|
2022-06-24 13:22:46 +00:00
|
|
|
elif mode == FREEDV_MODE.datac3.value:
|
2022-02-02 20:12:16 +00:00
|
|
|
modem.RECEIVE_DATAC3 = True
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Changing listening data mode", mode="datac3")
|
2022-06-24 13:22:46 +00:00
|
|
|
elif mode == FREEDV_MODE.fsk_ldpc_1.value:
|
2022-03-24 19:49:13 +00:00
|
|
|
modem.RECEIVE_FSK_LDPC_1 = True
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug("[TNC] Changing listening data mode", mode="fsk_ldpc_1")
|
2022-06-21 22:47:18 +00:00
|
|
|
else:
|
2022-02-02 20:12:16 +00:00
|
|
|
modem.RECEIVE_DATAC1 = True
|
|
|
|
modem.RECEIVE_DATAC3 = True
|
2022-03-24 19:49:13 +00:00
|
|
|
modem.RECEIVE_FSK_LDPC_1 = True
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[TNC] Changing listening data mode", mode="datac1/datac3/fsk_ldpc"
|
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
# ------------------------- WATCHDOG FUNCTIONS FOR TIMER
|
2022-05-23 00:54:12 +00:00
|
|
|
def watchdog(self) -> None:
|
2022-03-04 15:50:32 +00:00
|
|
|
"""Author: DJ2LS
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
Watchdog master function. From here, "pet" the watchdogs
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-01-07 10:25:28 +00:00
|
|
|
"""
|
|
|
|
while True:
|
2022-02-02 20:12:16 +00:00
|
|
|
time.sleep(0.1)
|
2022-01-07 10:25:28 +00:00
|
|
|
self.data_channel_keep_alive_watchdog()
|
2022-02-02 20:12:16 +00:00
|
|
|
self.burst_watchdog()
|
2022-03-04 15:50:32 +00:00
|
|
|
self.arq_session_keep_alive_watchdog()
|
2021-09-26 15:51:11 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def burst_watchdog(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
Watchdog which checks if we are running into a connection timeout
|
2022-03-06 16:23:04 +00:00
|
|
|
DATA BURST
|
|
|
|
"""
|
2022-05-09 00:41:49 +00:00
|
|
|
# IRS SIDE
|
2022-05-19 20:15:24 +00:00
|
|
|
# TODO: We need to redesign this part for cleaner state handling
|
2022-05-23 00:54:12 +00:00
|
|
|
# Return if not ARQ STATE and not ARQ SESSION STATE as they are different use cases
|
|
|
|
if (
|
|
|
|
not static.ARQ_STATE
|
|
|
|
and static.ARQ_SESSION_STATE != "connected"
|
|
|
|
or not self.is_IRS
|
|
|
|
):
|
2022-05-20 12:38:43 +00:00
|
|
|
return
|
2022-05-23 00:54:12 +00:00
|
|
|
|
|
|
|
# We want to reach this state only if connected ( == return above not called )
|
|
|
|
if (
|
|
|
|
self.data_channel_last_received + self.time_list[self.speed_level]
|
|
|
|
<= time.time()
|
|
|
|
):
|
|
|
|
self.log.warning(
|
|
|
|
"[TNC] Frame timeout",
|
|
|
|
attempt=self.n_retries_per_burst,
|
|
|
|
max_attempts=self.rx_n_max_retries_per_burst,
|
|
|
|
speed_level=self.speed_level,
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
self.frame_received_counter = 0
|
|
|
|
self.burst_nack_counter += 1
|
|
|
|
if self.burst_nack_counter >= 2:
|
|
|
|
self.burst_nack_counter = 0
|
2022-05-23 02:13:43 +00:00
|
|
|
self.speed_level = max(self.speed_level - 1, 0)
|
2022-05-11 22:10:59 +00:00
|
|
|
static.ARQ_SPEED_LEVEL = self.speed_level
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
# Update modes we are listening to
|
2022-05-11 22:10:59 +00:00
|
|
|
self.set_listening_modes(self.mode_list[self.speed_level])
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Why not pass `snr` or `static.SNR`?
|
|
|
|
self.send_burst_nack_frame_watchdog(0)
|
2022-05-11 22:10:59 +00:00
|
|
|
|
2022-05-23 02:13:43 +00:00
|
|
|
# Update data_channel timestamp
|
2022-05-11 22:10:59 +00:00
|
|
|
self.data_channel_last_received = time.time()
|
|
|
|
self.n_retries_per_burst += 1
|
2022-05-23 00:54:12 +00:00
|
|
|
else:
|
|
|
|
# print((self.data_channel_last_received + self.time_list[self.speed_level])-time.time())
|
|
|
|
pass
|
2022-05-11 22:10:59 +00:00
|
|
|
|
|
|
|
if self.n_retries_per_burst >= self.rx_n_max_retries_per_burst:
|
|
|
|
self.stop_transmission()
|
|
|
|
self.arq_cleanup()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def data_channel_keep_alive_watchdog(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
|
|
|
watchdog which checks if we are running into a connection timeout
|
2022-05-09 00:41:49 +00:00
|
|
|
DATA CHANNEL
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-01-07 10:25:28 +00:00
|
|
|
# and not static.ARQ_SEND_KEEP_ALIVE:
|
2022-05-23 00:54:12 +00:00
|
|
|
if static.ARQ_STATE and static.TNC_STATE == "BUSY":
|
2022-01-07 10:25:28 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-23 00:54:12 +00:00
|
|
|
if (
|
|
|
|
self.data_channel_last_received + self.transmission_timeout
|
|
|
|
> time.time()
|
|
|
|
):
|
2022-01-07 10:25:28 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-19 20:15:24 +00:00
|
|
|
# print(self.data_channel_last_received + self.transmission_timeout - time.time())
|
|
|
|
# pass
|
2022-01-07 10:25:28 +00:00
|
|
|
else:
|
2022-05-23 02:13:43 +00:00
|
|
|
# Clear the timeout timestamp
|
2022-01-07 10:25:28 +00:00
|
|
|
self.data_channel_last_received = 0
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] DATA ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]<<T>>["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]"
|
|
|
|
)
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="transmission",
|
|
|
|
status="failed",
|
2022-06-06 19:07:41 +00:00
|
|
|
uuid=self.transmission_uuid,
|
2022-06-05 17:11:09 +00:00
|
|
|
)
|
2022-05-23 00:54:12 +00:00
|
|
|
self.arq_cleanup()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def arq_session_keep_alive_watchdog(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
|
|
|
watchdog which checks if we are running into a connection timeout
|
|
|
|
ARQ SESSION
|
|
|
|
"""
|
2022-05-23 00:54:12 +00:00
|
|
|
if (
|
|
|
|
static.ARQ_SESSION
|
|
|
|
and static.TNC_STATE == "BUSY"
|
|
|
|
and not self.arq_file_transfer
|
|
|
|
):
|
2022-03-04 15:50:32 +00:00
|
|
|
if self.arq_session_last_received + self.arq_session_timeout > time.time():
|
|
|
|
time.sleep(0.01)
|
|
|
|
else:
|
2022-05-23 00:54:12 +00:00
|
|
|
self.log.info(
|
|
|
|
"[TNC] SESSION ["
|
|
|
|
+ str(self.mycallsign, "UTF-8")
|
|
|
|
+ "]<<T>>["
|
|
|
|
+ str(static.DXCALLSIGN, "UTF-8")
|
|
|
|
+ "]"
|
|
|
|
)
|
2022-06-05 17:11:09 +00:00
|
|
|
self.send_data_to_socket_queue(
|
2022-06-06 18:50:36 +00:00
|
|
|
freedata="tnc-message",
|
2022-06-05 17:11:09 +00:00
|
|
|
arq="session",
|
|
|
|
status="failed",
|
|
|
|
reason="timeout",
|
|
|
|
)
|
2022-03-04 15:50:32 +00:00
|
|
|
self.close_session()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def heartbeat(self) -> None:
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-05-28 15:52:05 +00:00
|
|
|
Heartbeat thread which auto pauses and resumes the heartbeat signal when in an arq session
|
2022-03-06 16:23:04 +00:00
|
|
|
"""
|
2022-05-28 02:17:15 +00:00
|
|
|
while True:
|
2022-03-04 15:50:32 +00:00
|
|
|
time.sleep(0.01)
|
2022-05-23 00:54:12 +00:00
|
|
|
if (
|
|
|
|
static.ARQ_SESSION
|
|
|
|
and self.IS_ARQ_SESSION_MASTER
|
|
|
|
and static.ARQ_SESSION_STATE == "connected"
|
|
|
|
and not self.arq_file_transfer
|
|
|
|
):
|
2022-03-04 15:50:32 +00:00
|
|
|
time.sleep(1)
|
|
|
|
self.transmit_session_heartbeat()
|
|
|
|
time.sleep(2)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 00:54:12 +00:00
|
|
|
def send_test_frame(self) -> None:
|
2022-06-24 13:39:56 +00:00
|
|
|
"""Send an empty test frame"""
|
|
|
|
self.enqueue_frame_for_tx(
|
|
|
|
frame_to_tx=bytearray(126), c2_mode=FREEDV_MODE.datac3.value
|
|
|
|
)
|