2022-05-21 23:04:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2022-06-19 13:55:50 +00:00
|
|
|
Send-side station emulator for connect frame tests over a high quality simulated audio channel.
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2022-06-19 13:55:50 +00:00
|
|
|
Near end-to-end test for sending / receiving connection control frames through the
|
|
|
|
TNC and modem and back through on the other station. Data injection initiates from the
|
|
|
|
queue used by the daemon process into and out of the TNC.
|
|
|
|
|
|
|
|
Invoked from test_tnc.py.
|
2022-06-19 14:04:46 +00:00
|
|
|
|
|
|
|
@author: DJ2LS, N2KIQ
|
2022-05-21 23:04:17 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-12 23:47:53 +00:00
|
|
|
import json
|
2022-06-10 20:54:20 +00:00
|
|
|
import signal
|
2022-05-21 23:04:17 +00:00
|
|
|
import sys
|
|
|
|
import time
|
2022-06-10 20:54:20 +00:00
|
|
|
from typing import Callable
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2022-06-07 00:26:41 +00:00
|
|
|
import structlog
|
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
sys.path.insert(0, "../tnc")
|
|
|
|
import data_handler
|
|
|
|
import helpers
|
|
|
|
import modem
|
2022-06-07 00:26:41 +00:00
|
|
|
import sock
|
2022-05-21 23:04:17 +00:00
|
|
|
import static
|
|
|
|
|
2022-06-10 20:54:20 +00:00
|
|
|
ISS_original_arq_cleanup: Callable
|
2022-05-21 23:04:17 +00:00
|
|
|
MESSAGE: str
|
|
|
|
|
2022-06-07 00:26:41 +00:00
|
|
|
log = structlog.get_logger("util_tnc_ISS")
|
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
def iss_arq_cleanup():
|
|
|
|
"""Replacement for modem.arq_cleanup to detect when to exit process."""
|
2022-06-07 00:26:41 +00:00
|
|
|
log.info(
|
2022-06-12 23:47:53 +00:00
|
|
|
"iss_arq_cleanup", socket_queue=sock.SOCKET_QUEUE.queue, message=MESSAGE.lower()
|
2022-06-07 00:26:41 +00:00
|
|
|
)
|
|
|
|
if '"arq":"transmission","status":"stopped"' in str(sock.SOCKET_QUEUE.queue):
|
|
|
|
# log.info("iss_arq_cleanup", socket_queue=sock.SOCKET_QUEUE.queue)
|
2022-05-21 23:04:17 +00:00
|
|
|
time.sleep(1)
|
2022-06-07 00:26:41 +00:00
|
|
|
if f'"{MESSAGE.lower()}":"transmitting"' not in str(
|
|
|
|
sock.SOCKET_QUEUE.queue
|
|
|
|
) and f'"{MESSAGE.lower()}":"sending"' not in str(sock.SOCKET_QUEUE.queue):
|
2022-05-21 23:04:17 +00:00
|
|
|
print(f"{MESSAGE} was not sent.")
|
2022-06-07 00:26:41 +00:00
|
|
|
log.info("iss_arq_cleanup", socket_queue=sock.SOCKET_QUEUE.queue)
|
2022-06-10 20:54:20 +00:00
|
|
|
# sys.exit does not terminate threads, and os_exit doesn't allow coverage collection.
|
|
|
|
signal.raise_signal(signal.SIGKILL)
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2022-06-10 20:54:20 +00:00
|
|
|
signal.raise_signal(signal.SIGTERM)
|
2022-05-21 23:04:17 +00:00
|
|
|
ISS_original_arq_cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
def t_arq_iss(*args):
|
|
|
|
# pylint: disable=global-statement
|
|
|
|
global ISS_original_arq_cleanup, MESSAGE
|
|
|
|
|
|
|
|
MESSAGE = args[0]
|
2022-06-12 18:20:49 +00:00
|
|
|
tmp_path = args[1]
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2022-06-15 23:24:47 +00:00
|
|
|
sock.log = structlog.get_logger("util_tnc_ISS_sock")
|
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
# enable testmode
|
|
|
|
data_handler.TESTMODE = True
|
2022-06-12 18:20:49 +00:00
|
|
|
modem.RXCHANNEL = tmp_path / "hfchannel1"
|
2022-05-21 23:04:17 +00:00
|
|
|
modem.TESTMODE = True
|
2022-06-12 18:20:49 +00:00
|
|
|
modem.TXCHANNEL = tmp_path / "hfchannel2"
|
2022-05-21 23:04:17 +00:00
|
|
|
static.HAMLIB_RADIOCONTROL = "disabled"
|
2022-06-12 18:20:49 +00:00
|
|
|
log.info("t_arq_iss:", RXCHANNEL=modem.RXCHANNEL)
|
|
|
|
log.info("t_arq_iss:", TXCHANNEL=modem.TXCHANNEL)
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
mycallsign = bytes("DJ2LS-2", "utf-8")
|
|
|
|
mycallsign = helpers.callsign_to_bytes(mycallsign)
|
|
|
|
static.MYCALLSIGN = helpers.bytes_to_callsign(mycallsign)
|
|
|
|
static.MYCALLSIGN_CRC = helpers.get_crc_24(static.MYCALLSIGN)
|
|
|
|
static.MYGRID = bytes("AA12aa", "utf-8")
|
|
|
|
static.SSID_LIST = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
|
|
|
dxcallsign = b"DN2LS-0"
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
|
|
|
static.DXCALLSIGN = dxcallsign
|
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN)
|
|
|
|
|
|
|
|
bytes_out = b'{"dt":"f","fn":"zeit.txt","ft":"text\\/plain","d":"data:text\\/plain;base64,MyBtb2Rlcywgb2huZSBjbGFzcwowLjAwMDk2OTQ4MTE4MDk5MTg0MTcKCjIgbW9kZXMsIG9obmUgY2xhc3MKMC4wMDA5NjY1NDUxODkxMjI1Mzk0CgoxIG1vZGUsIG9obmUgY2xhc3MKMC4wMDA5NjY5NzY1NTU4Nzc4MjA5CgMyBtb2Rlcywgb2huZSBjbGFzcwowLjAwMDk2OTQ4MTE4MDk5MTg0MTcKCjIgbW9kZXMsIG9obmUgY2xhc3MKMC4wMDA5NjY1NDUxODkxMjI1Mzk0CgoxIG1vZGUsIG9obmUgY2xhc3MKMC4wMDA5NjY5NzY1NTU4Nzc4MjA5Cg=MyBtb2Rlcywgb2huZSBjbGFzcwowLjAwMDk2OTQ4MTE4MDk5MTg0MTcKCjIgbW9kZXMsIG9obmUgY2xhc3MKMC4wMDA5NjY1NDUxODkxMjI1Mzk0CgoxIG1vZGUsIG9obmUgY2xhc3MKMC4wMDA5NjY5NzY1NTU4Nzc4MjA5CgMyBtb2Rlcywgb2huZSBjbGFzcwowLjAwMDk2OTQ4MTE4MDk5MTg0MTcKCjIgbW9kZXMsIG9obmUgY2xhc3MKMC4wMDA5NjY1NDUxODkxMjI1Mzk0CgoxIG1vZGUsIG9obmUgY2xhc3MKMC4wMDA5NjY5NzY1NTU4Nzc4MjA5CgMyBtb2Rlcywgb2huZSBjbGFzcwowLjAwMDk2OTQ4MTE4MDk5MTg0MTcKCjIgbW9kZXMsIG9obmUgY2xhc3MKMC4wMDA5NjY1NDUxODkxMjI1Mzk0CgoxIG1vZGUsIG9obmUgY2xhc3MKMC4wMDA5NjY5NzY1NTU4Nzc4MjA5Cg=","crc":"123123123"}'
|
|
|
|
|
|
|
|
# start data handler
|
|
|
|
tnc = data_handler.DATA()
|
2022-06-15 23:24:47 +00:00
|
|
|
tnc.log = structlog.get_logger("util_tnc_ISS_DATA")
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
# Inject a way to exit the TNC infinite loop
|
|
|
|
ISS_original_arq_cleanup = tnc.arq_cleanup
|
|
|
|
tnc.arq_cleanup = iss_arq_cleanup
|
|
|
|
|
|
|
|
# start modem
|
|
|
|
t_modem = modem.RF()
|
2022-06-15 23:24:47 +00:00
|
|
|
t_modem.log = structlog.get_logger("util_tnc_ISS_RF")
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
# mode = codec2.freedv_get_mode_value_by_name(FREEDV_MODE)
|
|
|
|
# n_frames_per_burst = N_FRAMES_PER_BURST
|
|
|
|
|
|
|
|
# add command to data qeue
|
|
|
|
"""
|
|
|
|
elif data[0] == 'ARQ_RAW':
|
|
|
|
# [0] ARQ_RAW
|
|
|
|
# [1] DATA_OUT bytes
|
|
|
|
# [2] MODE int
|
|
|
|
# [3] N_FRAMES_PER_BURST int
|
|
|
|
# [4] self.transmission_uuid str
|
|
|
|
# [5] mycallsign with ssid
|
|
|
|
"""
|
|
|
|
# data_handler.DATA_QUEUE_TRANSMIT.put(['ARQ_RAW', bytes_out, 255, n_frames_per_burst, '123', b'DJ2LS-0'])
|
|
|
|
|
2022-06-12 23:47:53 +00:00
|
|
|
data = {}
|
2022-06-15 23:24:47 +00:00
|
|
|
if MESSAGE in ["CONNECT"]:
|
2022-06-12 23:47:53 +00:00
|
|
|
data = {
|
|
|
|
"type": "arq",
|
|
|
|
"command": "connect",
|
|
|
|
"dxcallsign": str(dxcallsign, encoding="UTF-8"),
|
|
|
|
}
|
2022-05-21 23:04:17 +00:00
|
|
|
else:
|
2022-06-12 23:47:53 +00:00
|
|
|
assert not MESSAGE, f"{MESSAGE} not known to test."
|
|
|
|
|
2023-04-21 13:56:52 +00:00
|
|
|
time.sleep(2.5)
|
2022-06-15 23:24:47 +00:00
|
|
|
|
2023-02-09 22:49:00 +00:00
|
|
|
sock.ThreadedTCPRequestHandler.process_tnc_commands(None,json.dumps(data, indent=None))
|
|
|
|
sock.ThreadedTCPRequestHandler.process_tnc_commands(None,json.dumps(data, indent=None))
|
|
|
|
sock.ThreadedTCPRequestHandler.process_tnc_commands(None,json.dumps(data, indent=None))
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2023-04-21 13:56:52 +00:00
|
|
|
time.sleep(7.5)
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2022-06-12 23:47:53 +00:00
|
|
|
data = {"type": "arq", "command": "stop_transmission"}
|
2023-02-09 22:49:00 +00:00
|
|
|
sock.ThreadedTCPRequestHandler.process_tnc_commands(None,json.dumps(data, indent=None))
|
2022-05-21 23:04:17 +00:00
|
|
|
|
2023-04-21 13:56:52 +00:00
|
|
|
time.sleep(2.5)
|
2023-02-09 22:49:00 +00:00
|
|
|
sock.ThreadedTCPRequestHandler.process_tnc_commands(None,json.dumps(data, indent=None))
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
# Set timeout
|
2022-06-12 23:47:53 +00:00
|
|
|
timeout = time.time() + 15
|
2022-05-21 23:04:17 +00:00
|
|
|
|
|
|
|
while time.time() < timeout:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2022-06-12 23:47:53 +00:00
|
|
|
log.warning("queue:", queue=sock.SOCKET_QUEUE.queue)
|
|
|
|
|
2022-05-21 23:04:17 +00:00
|
|
|
assert not "TIMEOUT!"
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("This cannot be run as an application.")
|
|
|
|
sys.exit(-1)
|