2021-02-16 13:23:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Fri Dec 25 21:25:14 2020
|
|
|
|
|
|
|
|
@author: DJ2LS
|
2021-08-08 09:08:34 +00:00
|
|
|
|
|
|
|
# GET COMMANDS
|
2022-05-25 22:27:33 +00:00
|
|
|
# "command" : "..."
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-25 22:27:33 +00:00
|
|
|
# SET COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
# "parameter" : " ..."
|
2021-08-08 09:08:34 +00:00
|
|
|
|
2022-05-25 22:27:33 +00:00
|
|
|
# DATA COMMANDS
|
|
|
|
# "command" : "..."
|
|
|
|
# "type" : "..."
|
|
|
|
# "dxcallsign" : "..."
|
|
|
|
# "data" : "..."
|
2021-02-16 13:23:57 +00:00
|
|
|
"""
|
2022-05-11 22:10:59 +00:00
|
|
|
import atexit
|
|
|
|
import base64
|
|
|
|
import queue
|
2021-02-16 13:23:57 +00:00
|
|
|
import socketserver
|
2022-05-11 22:10:59 +00:00
|
|
|
import sys
|
2021-02-16 13:23:57 +00:00
|
|
|
import threading
|
2021-03-17 10:22:06 +00:00
|
|
|
import time
|
2022-12-27 20:13:08 +00:00
|
|
|
import wave
|
2022-05-11 22:10:59 +00:00
|
|
|
import helpers
|
|
|
|
import static
|
2022-05-26 01:23:30 +00:00
|
|
|
import structlog
|
2023-01-29 16:50:57 +00:00
|
|
|
from random import randrange
|
2022-05-26 01:23:30 +00:00
|
|
|
import ujson as json
|
2022-06-17 23:48:47 +00:00
|
|
|
from exceptions import NoCallsign
|
2023-01-04 18:26:11 +00:00
|
|
|
from queues import DATA_QUEUE_TRANSMIT, RX_BUFFER, RIGCTLD_COMMAND_QUEUE
|
2021-11-18 18:40:22 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
SOCKET_QUEUE = queue.Queue()
|
2022-01-22 19:39:37 +00:00
|
|
|
DAEMON_QUEUE = queue.Queue()
|
2021-09-23 15:49:45 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
CONNECTED_CLIENTS = set()
|
2022-02-16 08:11:32 +00:00
|
|
|
CLOSE_SIGNAL = False
|
|
|
|
|
2022-05-28 02:17:15 +00:00
|
|
|
log = structlog.get_logger("sock")
|
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
the socket handler base class
|
|
|
|
"""
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
pass
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-24 18:55:59 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-06-24 18:55:59 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
connection_alive = False
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-05-11 22:10:59 +00:00
|
|
|
connection_alive = False
|
2022-06-01 00:35:35 +00:00
|
|
|
log = structlog.get_logger("ThreadedTCPRequestHandler")
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
def send_to_client(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
function called by socket handler
|
|
|
|
send data to a network client if available
|
|
|
|
"""
|
2022-05-26 01:23:30 +00:00
|
|
|
tempdata = b""
|
2022-02-16 08:11:32 +00:00
|
|
|
while self.connection_alive and not CLOSE_SIGNAL:
|
2022-01-20 19:38:56 +00:00
|
|
|
# send tnc state as network stream
|
2022-01-22 19:39:37 +00:00
|
|
|
# check server port against daemon port and send corresponding data
|
|
|
|
if self.server.server_address[1] == static.PORT and not static.TNCSTARTED:
|
|
|
|
data = send_tnc_state()
|
2022-02-08 14:27:34 +00:00
|
|
|
if data != tempdata:
|
|
|
|
tempdata = data
|
|
|
|
SOCKET_QUEUE.put(data)
|
2022-01-22 19:39:37 +00:00
|
|
|
else:
|
|
|
|
data = send_daemon_state()
|
2022-02-08 14:27:34 +00:00
|
|
|
if data != tempdata:
|
|
|
|
tempdata = data
|
|
|
|
SOCKET_QUEUE.put(data)
|
2022-12-12 11:28:52 +00:00
|
|
|
threading.Event().wait(0.5)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
while not SOCKET_QUEUE.empty():
|
|
|
|
data = SOCKET_QUEUE.get()
|
2022-05-26 01:23:30 +00:00
|
|
|
sock_data = bytes(data, "utf-8")
|
|
|
|
sock_data += b"\n" # append line limiter
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
# send data to all clients
|
2022-12-04 14:35:41 +00:00
|
|
|
try:
|
|
|
|
for client in CONNECTED_CLIENTS:
|
|
|
|
try:
|
|
|
|
client.send(sock_data)
|
|
|
|
except Exception as err:
|
|
|
|
self.log.info("[SCK] Connection lost", e=err)
|
2022-12-16 16:09:48 +00:00
|
|
|
# TODO: Check if we really should set connection alive to false. This might disconnect all other clients as well...
|
2022-12-04 14:35:41 +00:00
|
|
|
self.connection_alive = False
|
|
|
|
except Exception as err:
|
|
|
|
self.log.debug("[SCK] catch harmless RuntimeError: Set changed size during iteration", e=err)
|
2022-02-17 09:11:12 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# we want to transmit scatter data only once to reduce network traffic
|
|
|
|
static.SCATTER = []
|
|
|
|
# we want to display INFO messages only once
|
2022-05-09 00:41:49 +00:00
|
|
|
static.INFO = []
|
2022-05-26 01:23:30 +00:00
|
|
|
# self.request.sendall(sock_data)
|
2022-12-12 11:28:52 +00:00
|
|
|
threading.Event().wait(0.15)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
def receive_from_client(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
function which is called by the socket handler
|
|
|
|
it processes the data which is returned by a client
|
|
|
|
"""
|
2022-01-20 19:38:56 +00:00
|
|
|
data = bytes()
|
2022-02-16 08:11:32 +00:00
|
|
|
while self.connection_alive and not CLOSE_SIGNAL:
|
|
|
|
try:
|
|
|
|
chunk = self.request.recv(1024)
|
|
|
|
data += chunk
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if chunk == b"":
|
|
|
|
# print("connection broken. Closing...")
|
2022-02-16 08:11:32 +00:00
|
|
|
self.connection_alive = False
|
2022-03-06 16:23:04 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if data.startswith(b"{") and data.endswith(b"}\n"):
|
2022-02-16 08:11:32 +00:00
|
|
|
# split data by \n if we have multiple commands in socket buffer
|
2022-05-26 01:23:30 +00:00
|
|
|
data = data.split(b"\n")
|
2022-02-16 08:11:32 +00:00
|
|
|
# remove empty data
|
2022-05-26 01:23:30 +00:00
|
|
|
data.remove(b"")
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
# iterate thorugh data list
|
|
|
|
for commands in data:
|
|
|
|
if self.server.server_address[1] == static.PORT:
|
|
|
|
process_tnc_commands(commands)
|
|
|
|
else:
|
|
|
|
process_daemon_commands(commands)
|
2022-02-17 09:11:12 +00:00
|
|
|
|
|
|
|
# wait some time between processing multiple commands
|
|
|
|
# this is only a first test to avoid doubled transmission
|
2022-05-09 00:41:49 +00:00
|
|
|
# we might improve this by only processing one command or
|
2022-02-17 09:11:12 +00:00
|
|
|
# doing some kind of selection to determin which commands need to be dropped
|
|
|
|
# and which one can be processed during a running transmission
|
2023-01-11 15:18:29 +00:00
|
|
|
threading.Event().wait(0.5)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-16 08:11:32 +00:00
|
|
|
# finally delete our rx buffer to be ready for new commands
|
|
|
|
data = bytes()
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
|
|
|
self.log.info(
|
|
|
|
"[SCK] Connection closed",
|
|
|
|
ip=self.client_address[0],
|
|
|
|
port=self.client_address[1],
|
|
|
|
e=err,
|
|
|
|
)
|
2022-02-16 08:11:32 +00:00
|
|
|
self.connection_alive = False
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
def handle(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
socket handler
|
|
|
|
"""
|
2022-01-22 19:39:37 +00:00
|
|
|
CONNECTED_CLIENTS.add(self.request)
|
2022-01-06 21:15:14 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.debug(
|
|
|
|
"[SCK] Client connected",
|
|
|
|
ip=self.client_address[0],
|
|
|
|
port=self.client_address[1],
|
|
|
|
)
|
2022-01-20 19:38:56 +00:00
|
|
|
self.connection_alive = True
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
self.sendThread = threading.Thread(
|
|
|
|
target=self.send_to_client, args=[], daemon=True
|
2022-05-28 15:52:05 +00:00
|
|
|
)
|
|
|
|
self.sendThread.start()
|
2022-05-26 01:23:30 +00:00
|
|
|
self.receiveThread = threading.Thread(
|
|
|
|
target=self.receive_from_client, args=[], daemon=True
|
2022-05-28 15:52:05 +00:00
|
|
|
)
|
|
|
|
self.receiveThread.start()
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# keep connection alive until we close it
|
2022-02-16 08:11:32 +00:00
|
|
|
while self.connection_alive and not CLOSE_SIGNAL:
|
2022-12-12 11:28:52 +00:00
|
|
|
threading.Event().wait(1)
|
2022-01-06 21:15:14 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def finish(self):
|
2022-03-04 15:50:32 +00:00
|
|
|
""" """
|
2022-05-26 01:23:30 +00:00
|
|
|
self.log.warning(
|
|
|
|
"[SCK] Closing client socket",
|
|
|
|
ip=self.client_address[0],
|
|
|
|
port=self.client_address[1],
|
|
|
|
)
|
2022-01-28 19:07:39 +00:00
|
|
|
try:
|
|
|
|
CONNECTED_CLIENTS.remove(self.request)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
|
|
|
self.log.warning(
|
|
|
|
"[SCK] client connection already removed from client list",
|
|
|
|
client=self.request,
|
|
|
|
)
|
|
|
|
|
2022-06-24 18:55:59 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
def process_tnc_commands(data):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
process tnc commands
|
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-05-28 15:52:05 +00:00
|
|
|
log = structlog.get_logger("process_tnc_commands")
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# we need to do some error handling in case of socket timeout or decoding issue
|
|
|
|
try:
|
|
|
|
# convert data to json object
|
|
|
|
received_json = json.loads(data)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.debug("[SCK] CMD", command=received_json)
|
2022-12-01 09:05:24 +00:00
|
|
|
|
|
|
|
# ENABLE TNC LISTENING STATE -----------------------------------------------------
|
|
|
|
if received_json["type"] == "set" and received_json["command"] == "listen":
|
2022-03-31 19:13:30 +00:00
|
|
|
try:
|
2022-12-01 09:05:24 +00:00
|
|
|
static.LISTEN = received_json["state"] in ['true', 'True', True, "ON", "on"]
|
|
|
|
command_response("listen", True)
|
|
|
|
|
|
|
|
# if tnc is connected, force disconnect when static.LISTEN == False
|
|
|
|
if not static.LISTEN and static.ARQ_SESSION_STATE not in ["disconnecting", "disconnected", "failed"]:
|
|
|
|
DATA_QUEUE_TRANSMIT.put(["DISCONNECT"])
|
|
|
|
# set early disconnecting state so we can interrupt connection attempts
|
|
|
|
static.ARQ_SESSION_STATE = "disconnecting"
|
|
|
|
command_response("disconnect", True)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-12-01 09:05:24 +00:00
|
|
|
command_response("listen", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-12-01 09:05:24 +00:00
|
|
|
"[SCK] CQ command execution error", e=err, command=received_json
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-03-31 19:13:30 +00:00
|
|
|
|
2022-12-26 11:11:59 +00:00
|
|
|
# START STOP AUDIO RECORDING -----------------------------------------------------
|
|
|
|
if received_json["type"] == "set" and received_json["command"] == "record_audio":
|
|
|
|
try:
|
2022-12-26 11:27:13 +00:00
|
|
|
if not static.AUDIO_RECORD:
|
2022-12-27 20:13:08 +00:00
|
|
|
static.AUDIO_RECORD_FILE = wave.open(f"{int(time.time())}_audio_recording.wav", 'w')
|
|
|
|
static.AUDIO_RECORD_FILE.setnchannels(1)
|
|
|
|
static.AUDIO_RECORD_FILE.setsampwidth(2)
|
|
|
|
static.AUDIO_RECORD_FILE.setframerate(8000)
|
2022-12-26 11:11:59 +00:00
|
|
|
static.AUDIO_RECORD = True
|
|
|
|
else:
|
|
|
|
static.AUDIO_RECORD = False
|
|
|
|
static.AUDIO_RECORD_FILE.close()
|
|
|
|
|
|
|
|
command_response("respond_to_call", True)
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
command_response("respond_to_call", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] CQ command execution error", e=err, command=received_json
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-04 15:56:12 +00:00
|
|
|
# SET ENABLE/DISABLE RESPOND TO CALL -----------------------------------------------------
|
|
|
|
if received_json["type"] == "set" and received_json["command"] == "respond_to_call":
|
|
|
|
try:
|
|
|
|
static.RESPOND_TO_CALL = received_json["state"] in ['true', 'True', True]
|
|
|
|
command_response("respond_to_call", True)
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
command_response("respond_to_call", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] CQ command execution error", e=err, command=received_json
|
|
|
|
)
|
|
|
|
|
2022-11-29 06:28:28 +00:00
|
|
|
# SET ENABLE RESPOND TO CQ -----------------------------------------------------
|
|
|
|
if received_json["type"] == "set" and received_json["command"] == "respond_to_cq":
|
|
|
|
try:
|
|
|
|
static.RESPOND_TO_CQ = received_json["state"] in ['true', 'True', True]
|
|
|
|
command_response("respond_to_cq", True)
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
command_response("respond_to_cq", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] CQ command execution error", e=err, command=received_json
|
|
|
|
)
|
|
|
|
|
2022-12-01 09:05:24 +00:00
|
|
|
# SET TX AUDIO LEVEL -----------------------------------------------------
|
|
|
|
if (
|
|
|
|
received_json["type"] == "set"
|
|
|
|
and received_json["command"] == "tx_audio_level"
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
static.TX_AUDIO_LEVEL = int(received_json["value"])
|
|
|
|
command_response("tx_audio_level", True)
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
command_response("tx_audio_level", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] TX audio command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
|
|
|
)
|
|
|
|
|
2022-05-31 23:45:37 +00:00
|
|
|
# TRANSMIT TEST FRAME ----------------------------------------------------
|
2022-05-26 01:23:30 +00:00
|
|
|
if (
|
|
|
|
received_json["type"] == "set"
|
|
|
|
and received_json["command"] == "send_test_frame"
|
|
|
|
):
|
2022-03-31 19:13:30 +00:00
|
|
|
try:
|
2022-07-03 17:41:06 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["SEND_TEST_FRAME"])
|
2022-03-31 19:13:30 +00:00
|
|
|
command_response("send_test_frame", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("send_test_frame", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Send test frame command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-03-31 19:13:30 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# CQ CQ CQ -----------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["command"] == "cqcqcq":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
2022-07-03 17:41:06 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["CQ"])
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("cqcqcq", True)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("cqcqcq", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] CQ command execution error", e=err, command=received_json
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-05-09 01:27:24 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# START_BEACON -----------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["command"] == "start_beacon":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
static.BEACON_STATE = True
|
|
|
|
interval = int(received_json["parameter"])
|
2022-07-03 17:41:06 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["BEACON", interval, True])
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("start_beacon", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("start_beacon", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Start beacon command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# STOP_BEACON -----------------------------------------------------
|
2022-01-28 19:07:39 +00:00
|
|
|
if received_json["command"] == "stop_beacon":
|
2022-05-09 00:41:49 +00:00
|
|
|
try:
|
2022-06-24 18:55:59 +00:00
|
|
|
log.warning("[SCK] Stopping beacon!")
|
2022-03-10 19:46:34 +00:00
|
|
|
static.BEACON_STATE = False
|
2022-07-03 17:41:06 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["BEACON", None, False])
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("stop_beacon", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("stop_beacon", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Stop beacon command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# PING ----------------------------------------------------------
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "ping" and received_json["command"] == "ping":
|
2022-01-20 19:38:56 +00:00
|
|
|
# send ping frame and wait for ACK
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
dxcallsign = received_json["dxcallsign"]
|
2022-06-17 23:48:47 +00:00
|
|
|
if not str(dxcallsign).strip():
|
|
|
|
raise NoCallsign
|
2022-02-21 11:20:36 +00:00
|
|
|
|
2022-12-05 15:57:45 +00:00
|
|
|
# additional step for being sure our callsign is correctly
|
2022-02-21 11:20:36 +00:00
|
|
|
# in case we are not getting a station ssid
|
|
|
|
# then we are forcing a station ssid = 0
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-12-05 15:57:45 +00:00
|
|
|
# check if specific callsign is set with different SSID than the TNC is initialized
|
|
|
|
try:
|
|
|
|
mycallsign = received_json["mycallsign"]
|
|
|
|
mycallsign = helpers.callsign_to_bytes(mycallsign)
|
|
|
|
mycallsign = helpers.bytes_to_callsign(mycallsign)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
mycallsign = static.MYCALLSIGN
|
|
|
|
|
|
|
|
DATA_QUEUE_TRANSMIT.put(["PING", mycallsign, dxcallsign])
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("ping", True)
|
2022-06-17 23:48:47 +00:00
|
|
|
except NoCallsign:
|
|
|
|
command_response("ping", False)
|
|
|
|
log.warning("[SCK] callsign required for ping", command=received_json)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("ping", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] PING command execution error", e=err, command=received_json
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
# CONNECT ----------------------------------------------------------
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "arq" and received_json["command"] == "connect":
|
2022-11-18 09:09:16 +00:00
|
|
|
|
|
|
|
# pause our beacon first
|
2022-03-10 19:46:34 +00:00
|
|
|
static.BEACON_PAUSE = True
|
2022-03-04 15:50:32 +00:00
|
|
|
|
2022-11-20 10:44:29 +00:00
|
|
|
# check for connection attempts key
|
|
|
|
try:
|
|
|
|
attempts = int(received_json["attempts"])
|
|
|
|
except Exception:
|
|
|
|
# 15 == self.session_connect_max_retries
|
|
|
|
attempts = 15
|
|
|
|
|
2022-11-18 09:09:16 +00:00
|
|
|
dxcallsign = received_json["dxcallsign"]
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-12-04 12:01:09 +00:00
|
|
|
# check if specific callsign is set with different SSID than the TNC is initialized
|
|
|
|
try:
|
2022-12-05 15:57:45 +00:00
|
|
|
mycallsign = received_json["mycallsign"]
|
2022-12-04 12:01:09 +00:00
|
|
|
mycallsign = helpers.callsign_to_bytes(mycallsign)
|
|
|
|
mycallsign = helpers.bytes_to_callsign(mycallsign)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
mycallsign = static.MYCALLSIGN
|
|
|
|
|
2022-11-18 09:09:16 +00:00
|
|
|
# additional step for being sure our callsign is correctly
|
|
|
|
# in case we are not getting a station ssid
|
|
|
|
# then we are forcing a station ssid = 0
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-11-20 11:54:59 +00:00
|
|
|
if static.ARQ_SESSION_STATE not in ["disconnected", "failed"]:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("connect", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Connect command execution error",
|
2022-11-19 23:20:32 +00:00
|
|
|
e=f"already connected to station:{static.DXCALLSIGN}",
|
2022-06-24 18:55:59 +00:00
|
|
|
command=received_json,
|
2022-11-18 09:09:16 +00:00
|
|
|
)
|
|
|
|
else:
|
2022-11-18 09:29:20 +00:00
|
|
|
|
|
|
|
# finally check again if we are disconnected or failed
|
|
|
|
|
2022-11-20 11:54:59 +00:00
|
|
|
# try connecting
|
|
|
|
try:
|
2022-11-18 09:29:20 +00:00
|
|
|
|
2022-12-04 12:01:09 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["CONNECT", mycallsign, dxcallsign, attempts])
|
2022-11-20 11:54:59 +00:00
|
|
|
command_response("connect", True)
|
|
|
|
except Exception as err:
|
2022-11-18 09:29:20 +00:00
|
|
|
command_response("connect", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] Connect command execution error",
|
2022-11-20 11:54:59 +00:00
|
|
|
e=err,
|
2022-11-18 09:29:20 +00:00
|
|
|
command=received_json,
|
|
|
|
)
|
|
|
|
# allow beacon transmission again
|
|
|
|
static.BEACON_PAUSE = False
|
2022-11-18 09:09:16 +00:00
|
|
|
|
2022-11-20 11:54:59 +00:00
|
|
|
# allow beacon transmission again
|
|
|
|
static.BEACON_PAUSE = False
|
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
# DISCONNECT ----------------------------------------------------------
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "arq" and received_json["command"] == "disconnect":
|
2022-03-04 15:50:32 +00:00
|
|
|
try:
|
2022-12-01 09:05:24 +00:00
|
|
|
if static.ARQ_SESSION_STATE not in ["disconnecting", "disconnected", "failed"]:
|
2022-11-20 17:19:56 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["DISCONNECT"])
|
|
|
|
|
|
|
|
# set early disconnecting state so we can interrupt connection attempts
|
|
|
|
static.ARQ_SESSION_STATE = "disconnecting"
|
|
|
|
command_response("disconnect", True)
|
|
|
|
else:
|
|
|
|
command_response("disconnect", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] Disconnect command not possible",
|
|
|
|
state=static.ARQ_SESSION_STATE,
|
|
|
|
command=received_json,
|
|
|
|
)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("disconnect", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Disconnect command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-04 15:50:32 +00:00
|
|
|
# TRANSMIT RAW DATA -------------------------------------------
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "arq" and received_json["command"] == "send_raw":
|
2022-03-10 19:46:34 +00:00
|
|
|
static.BEACON_PAUSE = True
|
2022-11-20 10:44:29 +00:00
|
|
|
|
2023-01-29 16:50:57 +00:00
|
|
|
# wait some random time
|
2023-01-29 17:50:09 +00:00
|
|
|
helpers.wait(randrange(5, 25, 5) / 10.0)
|
2023-01-29 16:50:57 +00:00
|
|
|
|
2023-01-25 12:20:57 +00:00
|
|
|
# we need to warn if already in arq state
|
2023-01-22 14:19:42 +00:00
|
|
|
if static.ARQ_STATE:
|
|
|
|
command_response("send_raw", False)
|
|
|
|
log.warning(
|
2023-01-25 12:20:57 +00:00
|
|
|
"[SCK] Send raw command execution warning",
|
2023-01-22 14:19:42 +00:00
|
|
|
e="already in arq state",
|
2023-01-25 12:20:57 +00:00
|
|
|
i="command queued",
|
2023-01-22 14:19:42 +00:00
|
|
|
command=received_json,
|
|
|
|
)
|
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
try:
|
2022-03-04 15:50:32 +00:00
|
|
|
if not static.ARQ_SESSION:
|
|
|
|
dxcallsign = received_json["parameter"][0]["dxcallsign"]
|
2023-01-22 14:19:42 +00:00
|
|
|
# additional step for being sure our callsign is correctly
|
2022-03-04 15:50:32 +00:00
|
|
|
# in case we are not getting a station ssid
|
|
|
|
# then we are forcing a station ssid = 0
|
|
|
|
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
|
|
|
|
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
|
|
|
|
static.DXCALLSIGN = dxcallsign
|
2022-04-19 09:09:11 +00:00
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN)
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("send_raw", True)
|
2022-03-04 15:50:32 +00:00
|
|
|
else:
|
2022-03-06 16:23:04 +00:00
|
|
|
dxcallsign = static.DXCALLSIGN
|
2022-04-19 09:09:11 +00:00
|
|
|
static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN)
|
2022-03-08 07:44:19 +00:00
|
|
|
|
2022-01-30 13:16:08 +00:00
|
|
|
mode = int(received_json["parameter"][0]["mode"])
|
|
|
|
n_frames = int(received_json["parameter"][0]["n_frames"])
|
2022-02-02 20:12:16 +00:00
|
|
|
base64data = received_json["parameter"][0]["data"]
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-03-19 11:42:10 +00:00
|
|
|
# check if specific callsign is set with different SSID than the TNC is initialized
|
|
|
|
try:
|
|
|
|
mycallsign = received_json["parameter"][0]["mycallsign"]
|
2022-12-04 11:22:35 +00:00
|
|
|
mycallsign = helpers.callsign_to_bytes(mycallsign)
|
|
|
|
mycallsign = helpers.bytes_to_callsign(mycallsign)
|
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
2022-03-19 11:42:10 +00:00
|
|
|
mycallsign = static.MYCALLSIGN
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-11-20 10:44:29 +00:00
|
|
|
# check for connection attempts key
|
|
|
|
try:
|
2022-11-20 11:02:53 +00:00
|
|
|
attempts = int(received_json["parameter"][0]["attempts"])
|
|
|
|
|
2022-11-20 10:44:29 +00:00
|
|
|
except Exception:
|
2023-01-22 14:19:42 +00:00
|
|
|
attempts = 10
|
2022-11-20 10:44:29 +00:00
|
|
|
|
2022-03-14 19:21:15 +00:00
|
|
|
# check if transmission uuid provided else set no-uuid
|
|
|
|
try:
|
|
|
|
arq_uuid = received_json["uuid"]
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception:
|
|
|
|
arq_uuid = "no-uuid"
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-24 18:55:59 +00:00
|
|
|
if len(base64data) % 4:
|
2022-02-02 20:12:16 +00:00
|
|
|
raise TypeError
|
2022-06-24 18:55:59 +00:00
|
|
|
|
|
|
|
binarydata = base64.b64decode(base64data)
|
|
|
|
|
2022-07-03 17:41:06 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(
|
2022-12-04 11:22:35 +00:00
|
|
|
["ARQ_RAW", binarydata, mode, n_frames, arq_uuid, mycallsign, dxcallsign, attempts]
|
2022-06-24 18:55:59 +00:00
|
|
|
)
|
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("send_raw", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Send raw command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-04-11 09:10:32 +00:00
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
# STOP TRANSMISSION ----------------------------------------------------------
|
2022-05-26 01:23:30 +00:00
|
|
|
if (
|
|
|
|
received_json["type"] == "arq"
|
|
|
|
and received_json["command"] == "stop_transmission"
|
|
|
|
):
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
2022-05-26 01:23:30 +00:00
|
|
|
if static.TNC_STATE == "BUSY" or static.ARQ_STATE:
|
2022-07-03 17:41:06 +00:00
|
|
|
DATA_QUEUE_TRANSMIT.put(["STOP"])
|
2022-06-24 18:55:59 +00:00
|
|
|
log.warning("[SCK] Stopping transmission!")
|
2022-05-26 01:23:30 +00:00
|
|
|
static.TNC_STATE = "IDLE"
|
2022-01-30 13:16:08 +00:00
|
|
|
static.ARQ_STATE = False
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("stop_transmission", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("stop_transmission", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] STOP command execution error", e=err, command=received_json
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "get" and received_json["command"] == "rx_buffer":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
output = {
|
|
|
|
"command": "rx_buffer",
|
|
|
|
"data-array": [],
|
|
|
|
}
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-09-05 08:37:50 +00:00
|
|
|
if not RX_BUFFER.empty():
|
|
|
|
for _buffer_length in range(RX_BUFFER.qsize()):
|
|
|
|
base64_data = RX_BUFFER.queue[_buffer_length][4]
|
|
|
|
output["data-array"].append(
|
|
|
|
{
|
|
|
|
"uuid": RX_BUFFER.queue[_buffer_length][0],
|
|
|
|
"timestamp": RX_BUFFER.queue[_buffer_length][1],
|
|
|
|
"dxcallsign": str(RX_BUFFER.queue[_buffer_length][2], "utf-8"),
|
|
|
|
"dxgrid": str(RX_BUFFER.queue[_buffer_length][3], "utf-8"),
|
|
|
|
"data": base64_data,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
jsondata = json.dumps(output)
|
|
|
|
# self.request.sendall(bytes(jsondata, encoding))
|
|
|
|
SOCKET_QUEUE.put(jsondata)
|
|
|
|
command_response("rx_buffer", True)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("rx_buffer", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Send RX buffer command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
received_json["type"] == "set"
|
|
|
|
and received_json["command"] == "del_rx_buffer"
|
|
|
|
):
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
2022-09-05 08:37:50 +00:00
|
|
|
RX_BUFFER.queue.clear()
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("del_rx_buffer", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("del_rx_buffer", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] Delete RX buffer command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-01-30 13:16:08 +00:00
|
|
|
|
2023-01-04 18:26:11 +00:00
|
|
|
# SET FREQUENCY -----------------------------------------------------
|
|
|
|
if received_json["command"] == "frequency" and received_json["type"] == "set":
|
|
|
|
try:
|
|
|
|
RIGCTLD_COMMAND_QUEUE.put(["set_frequency", received_json["frequency"]])
|
|
|
|
command_response("set_frequency", True)
|
|
|
|
except Exception as err:
|
|
|
|
command_response("set_frequency", False)
|
|
|
|
log.warning(
|
2023-01-04 19:12:03 +00:00
|
|
|
"[SCK] Set frequency command execution error",
|
|
|
|
e=err,
|
|
|
|
command=received_json,
|
|
|
|
)
|
|
|
|
|
|
|
|
# SET MODE -----------------------------------------------------
|
|
|
|
if received_json["command"] == "mode" and received_json["type"] == "set":
|
|
|
|
try:
|
|
|
|
RIGCTLD_COMMAND_QUEUE.put(["set_mode", received_json["mode"]])
|
|
|
|
command_response("set_mode", True)
|
|
|
|
except Exception as err:
|
|
|
|
command_response("set_mode", False)
|
|
|
|
log.warning(
|
|
|
|
"[SCK] Set mode command execution error",
|
2023-01-04 18:26:11 +00:00
|
|
|
e=err,
|
|
|
|
command=received_json,
|
|
|
|
)
|
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
# exception, if JSON cant be decoded
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-06-24 18:55:59 +00:00
|
|
|
log.error("[SCK] JSON decoding error", e=err)
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
def send_tnc_state():
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
send the tnc state to network
|
|
|
|
"""
|
2022-05-26 01:23:30 +00:00
|
|
|
encoding = "utf-8"
|
2022-01-20 19:38:56 +00:00
|
|
|
|
|
|
|
output = {
|
2022-01-28 19:07:39 +00:00
|
|
|
"command": "tnc_state",
|
|
|
|
"ptt_state": str(static.PTT_STATE),
|
|
|
|
"tnc_state": str(static.TNC_STATE),
|
|
|
|
"arq_state": str(static.ARQ_STATE),
|
2022-03-04 15:50:32 +00:00
|
|
|
"arq_session": str(static.ARQ_SESSION),
|
2022-03-11 19:38:28 +00:00
|
|
|
"arq_session_state": str(static.ARQ_SESSION_STATE),
|
2022-11-18 14:19:41 +00:00
|
|
|
"audio_dbfs": str(static.AUDIO_DBFS),
|
2022-01-28 19:07:39 +00:00
|
|
|
"snr": str(static.SNR),
|
|
|
|
"frequency": str(static.HAMLIB_FREQUENCY),
|
2023-01-30 11:08:45 +00:00
|
|
|
"rf_level": str(static.HAMLIB_RF),
|
2023-01-30 11:31:34 +00:00
|
|
|
"strength": str(static.HAMLIB_STRENGTH),
|
2023-01-30 11:08:45 +00:00
|
|
|
"alc": str(static.HAMLIB_ALC),
|
2023-02-01 15:11:07 +00:00
|
|
|
"audio_level": str(static.TX_AUDIO_LEVEL),
|
|
|
|
"audio_auto_tune": str(static.AUDIO_AUTO_TUNE),
|
2022-05-09 00:41:49 +00:00
|
|
|
"speed_level": str(static.ARQ_SPEED_LEVEL),
|
2022-01-28 19:07:39 +00:00
|
|
|
"mode": str(static.HAMLIB_MODE),
|
2022-05-28 12:08:33 +00:00
|
|
|
"bandwidth": str(static.HAMLIB_BANDWIDTH),
|
2022-01-28 19:07:39 +00:00
|
|
|
"fft": str(static.FFT),
|
2022-02-15 17:10:14 +00:00
|
|
|
"channel_busy": str(static.CHANNEL_BUSY),
|
2022-01-28 19:07:39 +00:00
|
|
|
"scatter": static.SCATTER,
|
2022-09-05 08:37:50 +00:00
|
|
|
"rx_buffer_length": str(RX_BUFFER.qsize()),
|
2022-01-28 19:07:39 +00:00
|
|
|
"rx_msg_buffer_length": str(len(static.RX_MSG_BUFFER)),
|
|
|
|
"arq_bytes_per_minute": str(static.ARQ_BYTES_PER_MINUTE),
|
|
|
|
"arq_bytes_per_minute_burst": str(static.ARQ_BYTES_PER_MINUTE_BURST),
|
2022-12-31 11:38:46 +00:00
|
|
|
"arq_seconds_until_finish": str(static.ARQ_SECONDS_UNTIL_FINISH),
|
2022-01-28 19:07:39 +00:00
|
|
|
"arq_compression_factor": str(static.ARQ_COMPRESSION_FACTOR),
|
|
|
|
"arq_transmission_percent": str(static.ARQ_TRANSMISSION_PERCENT),
|
2023-01-12 23:14:42 +00:00
|
|
|
"speed_list": static.SPEED_LIST,
|
2022-01-28 19:07:39 +00:00
|
|
|
"total_bytes": str(static.TOTAL_BYTES),
|
2022-05-26 01:23:30 +00:00
|
|
|
"beacon_state": str(static.BEACON_STATE),
|
2022-01-28 19:07:39 +00:00
|
|
|
"stations": [],
|
|
|
|
"mycallsign": str(static.MYCALLSIGN, encoding),
|
2023-01-06 16:33:20 +00:00
|
|
|
"mygrid": str(static.MYGRID, encoding),
|
2022-01-28 19:07:39 +00:00
|
|
|
"dxcallsign": str(static.DXCALLSIGN, encoding),
|
|
|
|
"dxgrid": str(static.DXGRID, encoding),
|
2022-11-18 12:10:26 +00:00
|
|
|
"hamlib_status": static.HAMLIB_STATUS,
|
2022-12-01 09:05:24 +00:00
|
|
|
"listen": str(static.LISTEN),
|
2022-12-26 11:11:59 +00:00
|
|
|
"audio_recording": str(static.AUDIO_RECORD),
|
2022-01-20 19:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# add heard stations to heard stations object
|
2022-05-11 22:10:59 +00:00
|
|
|
for heard in static.HEARD_STATIONS:
|
2022-05-26 01:23:30 +00:00
|
|
|
output["stations"].append(
|
|
|
|
{
|
|
|
|
"dxcallsign": str(heard[0], "utf-8"),
|
|
|
|
"dxgrid": str(heard[1], "utf-8"),
|
|
|
|
"timestamp": heard[2],
|
|
|
|
"datatype": heard[3],
|
|
|
|
"snr": heard[4],
|
|
|
|
"offset": heard[5],
|
|
|
|
"frequency": heard[6],
|
|
|
|
}
|
|
|
|
)
|
2022-05-11 22:10:59 +00:00
|
|
|
return json.dumps(output)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-05-28 15:52:05 +00:00
|
|
|
# This appears to have been taken out of a class, but is never called because
|
|
|
|
# the `self.request.sendall` call is a syntax error as `self` is undefined and
|
|
|
|
# we don't see errors in use.
|
2022-01-22 19:39:37 +00:00
|
|
|
def process_daemon_commands(data):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
process daemon commands
|
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
data:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2022-06-01 00:35:35 +00:00
|
|
|
log = structlog.get_logger("process_daemon_commands")
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
# convert data to json object
|
|
|
|
received_json = json.loads(data)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.debug("[SCK] CMD", command=received_json)
|
|
|
|
if received_json["type"] == "set" and received_json["command"] == "mycallsign":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
callsign = received_json["parameter"]
|
2022-03-06 16:23:04 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if bytes(callsign, "utf-8") == b"":
|
|
|
|
self.request.sendall(b"INVALID CALLSIGN")
|
|
|
|
log.warning(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] SET MYCALL FAILED",
|
2022-05-26 01:23:30 +00:00
|
|
|
call=static.MYCALLSIGN,
|
2022-06-24 18:55:59 +00:00
|
|
|
crc=static.MYCALLSIGN_CRC.hex(),
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
2022-01-30 13:16:08 +00:00
|
|
|
else:
|
2022-05-26 01:23:30 +00:00
|
|
|
static.MYCALLSIGN = bytes(callsign, "utf-8")
|
2022-04-19 09:09:11 +00:00
|
|
|
static.MYCALLSIGN_CRC = helpers.get_crc_24(static.MYCALLSIGN)
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("mycallsign", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.info(
|
2022-06-24 18:55:59 +00:00
|
|
|
"[SCK] SET MYCALL",
|
2022-05-26 01:23:30 +00:00
|
|
|
call=static.MYCALLSIGN,
|
2022-06-24 18:55:59 +00:00
|
|
|
crc=static.MYCALLSIGN_CRC.hex(),
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
|
|
|
except Exception as err:
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("mycallsign", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning("[SCK] command execution error", e=err, command=received_json)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "set" and received_json["command"] == "mygrid":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
mygrid = received_json["parameter"]
|
2022-01-20 19:38:56 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if bytes(mygrid, "utf-8") == b"":
|
|
|
|
self.request.sendall(b"INVALID GRID")
|
2022-06-24 18:55:59 +00:00
|
|
|
command_response("mygrid", False)
|
2022-01-30 13:16:08 +00:00
|
|
|
else:
|
2022-05-26 01:23:30 +00:00
|
|
|
static.MYGRID = bytes(mygrid, "utf-8")
|
|
|
|
log.info("[SCK] SET MYGRID", grid=static.MYGRID)
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("mygrid", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("mygrid", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning("[SCK] command execution error", e=err, command=received_json)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if (
|
|
|
|
received_json["type"] == "set"
|
|
|
|
and received_json["command"] == "start_tnc"
|
|
|
|
and not static.TNCSTARTED
|
|
|
|
):
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
2023-02-02 21:41:35 +00:00
|
|
|
startparam = received_json["parameter"][0]
|
|
|
|
|
|
|
|
mycall = str(helpers.return_key_from_object("AA0AA", startparam,"mycall"))
|
|
|
|
mygrid = str(helpers.return_key_from_object("JN12ab", startparam,"mygrid"))
|
|
|
|
rx_audio = str(helpers.return_key_from_object("0", startparam,"rx_audio"))
|
|
|
|
tx_audio = str(helpers.return_key_from_object("0", startparam,"tx_audio"))
|
2023-02-02 21:54:25 +00:00
|
|
|
radiocontrol = str(helpers.return_key_from_object("disabled", startparam,"radiocontrol"))
|
|
|
|
rigctld_ip = str(helpers.return_key_from_object("127.0.0.1", startparam,"rigctld_ip"))
|
|
|
|
rigctld_port = str(helpers.return_key_from_object("4532", startparam,"rigctld_port"))
|
2023-02-02 21:41:35 +00:00
|
|
|
enable_scatter = str(helpers.return_key_from_object("True", startparam,"enable_scatter"))
|
|
|
|
enable_fft = str(helpers.return_key_from_object("True", startparam,"enable_fft"))
|
|
|
|
enable_fsk = str(helpers.return_key_from_object("False", startparam,"enable_fsk"))
|
|
|
|
low_bandwidth_mode = str(helpers.return_key_from_object("False", startparam,"low_bandwidth_mode"))
|
|
|
|
tuning_range_fmin = str(helpers.return_key_from_object("-50", startparam,"tuning_range_fmin"))
|
|
|
|
tuning_range_fmax = str(helpers.return_key_from_object("50", startparam,"tuning_range_fmax"))
|
|
|
|
tx_audio_level = str(helpers.return_key_from_object("100", startparam,"tx_audio_level"))
|
|
|
|
respond_to_cq = str(helpers.return_key_from_object("False", startparam,"respond_to_cq"))
|
|
|
|
rx_buffer_size = str(helpers.return_key_from_object("16", startparam,"rx_buffer_size"))
|
|
|
|
enable_explorer = str(helpers.return_key_from_object("False", startparam,"enable_explorer"))
|
|
|
|
enable_auto_tune = str(helpers.return_key_from_object("False", startparam,"enable_auto_tune"))
|
|
|
|
enable_stats = str(helpers.return_key_from_object("False", startparam,"enable_stats"))
|
2022-12-08 17:09:13 +00:00
|
|
|
try:
|
|
|
|
# convert ssid list to python list
|
2023-02-02 22:49:52 +00:00
|
|
|
ssid_list = str(helpers.return_key_from_object("0, 1, 2, 3, 4, 5, 6, 7, 8, 9", startparam, "ssid_list"))
|
2022-12-08 17:09:13 +00:00
|
|
|
ssid_list = ssid_list.replace(" ", "")
|
|
|
|
ssid_list = ssid_list.split(",")
|
|
|
|
# convert str to int
|
|
|
|
ssid_list = list(map(int, ssid_list))
|
|
|
|
except KeyError:
|
2023-02-02 22:49:52 +00:00
|
|
|
ssid_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2022-12-08 15:26:33 +00:00
|
|
|
|
2022-04-02 16:40:12 +00:00
|
|
|
# print some debugging parameters
|
2023-02-02 22:49:52 +00:00
|
|
|
for item in startparam:
|
2022-05-26 01:23:30 +00:00
|
|
|
log.debug(
|
2022-06-24 18:55:59 +00:00
|
|
|
f"[SCK] TNC Startup Config : {item}",
|
2023-02-02 22:49:52 +00:00
|
|
|
value=startparam[item],
|
2022-05-26 01:23:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
DAEMON_QUEUE.put(
|
|
|
|
[
|
|
|
|
"STARTTNC",
|
|
|
|
mycall,
|
|
|
|
mygrid,
|
|
|
|
rx_audio,
|
|
|
|
tx_audio,
|
|
|
|
radiocontrol,
|
|
|
|
rigctld_ip,
|
|
|
|
rigctld_port,
|
|
|
|
enable_scatter,
|
|
|
|
enable_fft,
|
2022-05-28 15:52:05 +00:00
|
|
|
low_bandwidth_mode,
|
2022-05-26 01:23:30 +00:00
|
|
|
tuning_range_fmin,
|
|
|
|
tuning_range_fmax,
|
|
|
|
enable_fsk,
|
|
|
|
tx_audio_level,
|
|
|
|
respond_to_cq,
|
2022-09-05 09:54:50 +00:00
|
|
|
rx_buffer_size,
|
2022-11-05 21:27:33 +00:00
|
|
|
enable_explorer,
|
2022-12-08 15:26:33 +00:00
|
|
|
ssid_list,
|
2023-02-01 15:11:07 +00:00
|
|
|
enable_auto_tune,
|
2023-02-02 21:41:35 +00:00
|
|
|
enable_stats
|
2022-05-26 01:23:30 +00:00
|
|
|
]
|
|
|
|
)
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("start_tnc", True)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("start_tnc", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning("[SCK] command execution error", e=err, command=received_json)
|
2022-01-22 19:39:37 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "get" and received_json["command"] == "test_hamlib":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
radiocontrol = str(received_json["parameter"][0]["radiocontrol"])
|
|
|
|
rigctld_ip = str(received_json["parameter"][0]["rigctld_ip"])
|
|
|
|
rigctld_port = str(received_json["parameter"][0]["rigctld_port"])
|
2022-02-08 14:27:34 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
DAEMON_QUEUE.put(
|
|
|
|
[
|
|
|
|
"TEST_HAMLIB",
|
|
|
|
radiocontrol,
|
|
|
|
rigctld_ip,
|
|
|
|
rigctld_port,
|
|
|
|
]
|
|
|
|
)
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("test_hamlib", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("test_hamlib", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning("[SCK] command execution error", e=err, command=received_json)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
if received_json["type"] == "set" and received_json["command"] == "stop_tnc":
|
2022-01-30 13:16:08 +00:00
|
|
|
try:
|
|
|
|
static.TNCPROCESS.kill()
|
2022-03-04 15:50:32 +00:00
|
|
|
# unregister process from atexit to avoid process zombies
|
|
|
|
atexit.unregister(static.TNCPROCESS.kill)
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-06-24 18:55:59 +00:00
|
|
|
log.warning("[SCK] Stopping TNC")
|
2022-01-30 13:16:08 +00:00
|
|
|
static.TNCSTARTED = False
|
2022-03-08 07:44:19 +00:00
|
|
|
command_response("stop_tnc", True)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
2022-05-09 00:41:49 +00:00
|
|
|
command_response("stop_tnc", False)
|
2022-05-26 01:23:30 +00:00
|
|
|
log.warning("[SCK] command execution error", e=err, command=received_json)
|
|
|
|
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-01-22 19:39:37 +00:00
|
|
|
def send_daemon_state():
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
send the daemon state to network
|
|
|
|
"""
|
2022-06-01 00:35:35 +00:00
|
|
|
log = structlog.get_logger("send_daemon_state")
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
try:
|
2022-05-11 22:10:59 +00:00
|
|
|
python_version = f"{str(sys.version_info[0])}.{str(sys.version_info[1])}"
|
2022-02-15 17:10:14 +00:00
|
|
|
|
|
|
|
output = {
|
2022-05-26 01:23:30 +00:00
|
|
|
"command": "daemon_state",
|
|
|
|
"daemon_state": [],
|
|
|
|
"python_version": str(python_version),
|
|
|
|
"input_devices": static.AUDIO_INPUT_DEVICES,
|
|
|
|
"output_devices": static.AUDIO_OUTPUT_DEVICES,
|
|
|
|
"serial_devices": static.SERIAL_DEVICES,
|
2022-02-15 17:10:14 +00:00
|
|
|
#'cpu': str(psutil.cpu_percent()),
|
|
|
|
#'ram': str(psutil.virtual_memory().percent),
|
2022-05-26 01:23:30 +00:00
|
|
|
"version": "0.1",
|
|
|
|
}
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-02-15 17:10:14 +00:00
|
|
|
if static.TNCSTARTED:
|
|
|
|
output["daemon_state"].append({"status": "running"})
|
|
|
|
else:
|
|
|
|
output["daemon_state"].append({"status": "stopped"})
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-23 12:02:22 +00:00
|
|
|
return json.dumps(output)
|
2022-05-26 01:23:30 +00:00
|
|
|
except Exception as err:
|
|
|
|
log.warning("[SCK] error", e=err)
|
2022-02-17 09:11:12 +00:00
|
|
|
return None
|
2022-05-09 00:41:49 +00:00
|
|
|
|
2022-05-26 01:23:30 +00:00
|
|
|
|
2022-03-08 07:44:19 +00:00
|
|
|
def command_response(command, status):
|
2022-05-11 22:10:59 +00:00
|
|
|
s_status = "OK" if status else "Failed"
|
2022-05-26 01:23:30 +00:00
|
|
|
jsondata = {"command_response": command, "status": s_status}
|
2022-06-24 19:22:16 +00:00
|
|
|
data_out = json.dumps(jsondata)
|
2022-03-08 07:44:19 +00:00
|
|
|
SOCKET_QUEUE.put(data_out)
|
2022-12-27 21:57:54 +00:00
|
|
|
|
2023-02-02 21:41:35 +00:00
|
|
|
|
|
|
|
def try_except(string):
|
|
|
|
try:
|
|
|
|
return string
|
|
|
|
except Exception:
|
|
|
|
return False
|