Merge pull request #187 from DJ2LS/refactor_N2KIQ_202205

Refactor TNC modules
This commit is contained in:
DJ2LS 2022-05-15 20:45:33 +02:00 committed by GitHub
commit 2a109844e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1741 additions and 1995 deletions

View file

@ -1,19 +1,17 @@
import json
import sys
import multiprocessing
import sounddevice as sd
import atexit
import json
import multiprocessing
import sys
import sounddevice as sd
atexit.register(sd._terminate)
def get_audio_devices():
"""
return list of input and output audio devices in own process to avoid crashes of portaudio on raspberry pi
also uses a process data manager
"""
# we need to run this on windows for multiprocessing support
@ -24,22 +22,21 @@ def get_audio_devices():
# If we are not doing this at this early point, not all devices will be displayed
sd._terminate()
sd._initialize()
with multiprocessing.Manager() as manager:
proxy_input_devices = manager.list()
proxy_output_devices = manager.list()
#print(multiprocessing.get_start_method())
p = multiprocessing.Process(target=fetch_audio_devices, args=(proxy_input_devices, proxy_output_devices))
p.start()
p.join()
return list(proxy_input_devices), list(proxy_output_devices)
return list(proxy_input_devices), list(proxy_output_devices)
def fetch_audio_devices(input_devices, output_devices):
"""
get audio devices from portaudio
Args:
input_devices: proxy variable for input devices
output_devices: proxy variable for outout devices
@ -47,15 +44,13 @@ def fetch_audio_devices(input_devices, output_devices):
Returns:
"""
devices = sd.query_devices(device=None, kind=None)
index = 0
for device in devices:
for index, device in enumerate(devices):
#for i in range(0, p.get_device_count()):
# we need to do a try exception, beacuse for windows theres no audio device range
try:
name = device["name"]
maxOutputChannels = device["max_output_channels"]
maxInputChannels = device["max_input_channels"]
@ -66,8 +61,6 @@ def fetch_audio_devices(input_devices, output_devices):
name = ''
if maxInputChannels > 0:
input_devices.append({"id": index, "name": str(name)})
input_devices.append({"id": index, "name": name})
if maxOutputChannels > 0:
output_devices.append({"id": index, "name": str(name)})
index += 1
output_devices.append({"id": index, "name": name})

View file

@ -1,75 +1,77 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
# pylint: disable=import-outside-toplevel
import ctypes
from ctypes import *
import sys
import os
from enum import Enum
import numpy as np
from threading import Lock
import glob
import os
import sys
from enum import Enum
from threading import Lock
import numpy as np
import structlog
# Enum for codec2 modes
class FREEDV_MODE(Enum):
"""
enum for codec2 modes and names
Enumeration for codec2 modes and names
"""
fsk_ldpc_0 = 200
fsk_ldpc_1 = 201
fsk_ldpc = 9
datac0 = 14
datac1 = 10
datac3 = 12
allmodes = 255
fsk_ldpc = 9
datac0 = 14
datac1 = 10
datac3 = 12
allmodes = 255
# function for returning the mode value
def freedv_get_mode_value_by_name(mode):
# Function for returning the mode value
def freedv_get_mode_value_by_name(mode: str) -> int:
"""
get the codec2 mode by entering its string
Get the codec2 mode by entering its string
Args:
mode:
Returns: int
mode:
Returns:
int
"""
return FREEDV_MODE[mode].value
# function for returning the mode name
def freedv_get_mode_name_by_value(mode):
# Function for returning the mode name
def freedv_get_mode_name_by_value(mode: int) -> str:
"""
get the codec2 mode name as string
Args:
mode:
Returns: string
mode:
Returns:
string
"""
return FREEDV_MODE(mode).name
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
# Check if we are running in a pyinstaller environment
if hasattr(sys, "_MEIPASS"):
sys.path.append(getattr(sys, "_MEIPASS"))
else:
sys.path.append(os.path.abspath("."))
structlog.get_logger("structlog").info("[C2 ] Searching for libcodec2...")
if sys.platform == 'linux':
files = glob.glob('**/*libcodec2*',recursive=True)
files = glob.glob(r'**/*libcodec2*',recursive=True)
files.append('libcodec2.so')
elif sys.platform == 'darwin':
files = glob.glob('**/*libcodec2*.dylib',recursive=True)
elif sys.platform == 'win32' or sys.platform == 'win64':
files = glob.glob('**\*libcodec2*.dll',recursive=True)
files = glob.glob(r'**/*libcodec2*.dylib',recursive=True)
elif sys.platform in ['win32', 'win64']:
files = glob.glob(r'**\*libcodec2*.dll',recursive=True)
else:
files = []
api = None
for file in files:
try:
api = ctypes.CDLL(file)
@ -78,87 +80,83 @@ for file in files:
except Exception as e:
structlog.get_logger("structlog").warning("[C2 ] Libcodec2 found but not loaded", path=file, e=e)
# Quit module if codec2 cant be loaded
if api is None or 'api' not in locals():
structlog.get_logger("structlog").critical("[C2 ] Libcodec2 not loaded")
sys.exit(1)
# quit module if codec2 cant be loaded
if not 'api' in locals():
structlog.get_logger("structlog").critical("[C2 ] Libcodec2 not loaded", path=file)
os._exit(1)
# ctypes function init
#api.freedv_set_tuning_range.restype = ctypes.c_int
#api.freedv_set_tuning_range.argype = [ctypes.c_void_p, ctypes.c_float, ctypes.c_float]
api.freedv_open.argype = [ctypes.c_int]
api.freedv_open.restype = ctypes.c_void_p
# ctypes function init
api.freedv_open_advanced.argtype = [ctypes.c_int, ctypes.c_void_p]
api.freedv_open_advanced.restype = ctypes.c_void_p
#api.freedv_set_tuning_range.restype = c_int
#api.freedv_set_tuning_range.argype = [c_void_p, c_float, c_float]
api.freedv_get_bits_per_modem_frame.argtype = [ctypes.c_void_p]
api.freedv_get_bits_per_modem_frame.restype = ctypes.c_int
api.freedv_open.argype = [c_int]
api.freedv_open.restype = c_void_p
api.freedv_nin.argtype = [ctypes.c_void_p]
api.freedv_nin.restype = ctypes.c_int
api.freedv_open_advanced.argtype = [c_int, c_void_p]
api.freedv_open_advanced.restype = c_void_p
api.freedv_rawdatarx.argtype = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
api.freedv_rawdatarx.restype = ctypes.c_int
api.freedv_get_bits_per_modem_frame.argtype = [c_void_p]
api.freedv_get_bits_per_modem_frame.restype = c_int
api.freedv_rawdatatx.argtype = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
api.freedv_rawdatatx.restype = ctypes.c_int
api.freedv_nin.argtype = [c_void_p]
api.freedv_nin.restype = c_int
api.freedv_rawdatapostambletx.argtype = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
api.freedv_rawdatapostambletx.restype = ctypes.c_int
api.freedv_rawdatarx.argtype = [c_void_p, c_char_p, c_char_p]
api.freedv_rawdatarx.restype = c_int
api.freedv_rawdatapreambletx.argtype = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
api.freedv_rawdatapreambletx.restype = ctypes.c_int
api.freedv_rawdatatx.argtype = [c_void_p, c_char_p, c_char_p]
api.freedv_rawdatatx.restype = c_int
api.freedv_get_n_max_modem_samples.argtype = [ctypes.c_void_p]
api.freedv_get_n_max_modem_samples.restype = ctypes.c_int
api.freedv_rawdatapostambletx.argtype = [c_void_p, c_char_p, c_char_p]
api.freedv_rawdatapostambletx.restype = c_int
api.freedv_set_frames_per_burst.argtype = [ctypes.c_void_p, ctypes.c_int]
api.freedv_set_frames_per_burst.restype = ctypes.c_void_p
api.freedv_rawdatapreambletx.argtype = [c_void_p, c_char_p, c_char_p]
api.freedv_rawdatapreambletx.restype = c_int
api.freedv_get_rx_status.argtype = [ctypes.c_void_p]
api.freedv_get_rx_status.restype = ctypes.c_int
api.freedv_get_n_max_modem_samples.argtype = [c_void_p]
api.freedv_get_n_max_modem_samples.restype = c_int
api.freedv_get_modem_stats.argtype = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
api.freedv_get_modem_stats.restype = ctypes.c_int
api.freedv_set_frames_per_burst.argtype = [c_void_p, c_int]
api.freedv_set_frames_per_burst.restype = c_void_p
api.freedv_get_rx_status.argtype = [c_void_p]
api.freedv_get_rx_status.restype = c_int
api.freedv_get_n_tx_postamble_modem_samples.argtype = [ctypes.c_void_p]
api.freedv_get_n_tx_postamble_modem_samples.restype = ctypes.c_int
api.freedv_get_modem_stats.argtype = [c_void_p, c_void_p, c_void_p]
api.freedv_get_modem_stats.restype = c_int
api.freedv_get_n_tx_preamble_modem_samples.argtype = [ctypes.c_void_p]
api.freedv_get_n_tx_preamble_modem_samples.restype = ctypes.c_int
api.freedv_get_n_tx_postamble_modem_samples.argtype = [c_void_p]
api.freedv_get_n_tx_postamble_modem_samples.restype = c_int
api.freedv_get_n_tx_modem_samples.argtype = [ctypes.c_void_p]
api.freedv_get_n_tx_modem_samples.restype = ctypes.c_int
api.freedv_get_n_tx_preamble_modem_samples.argtype = [c_void_p]
api.freedv_get_n_tx_preamble_modem_samples.restype = c_int
api.freedv_get_n_max_modem_samples.argtype = [ctypes.c_void_p]
api.freedv_get_n_max_modem_samples.restype = ctypes.c_int
api.freedv_get_n_tx_modem_samples.argtype = [c_void_p]
api.freedv_get_n_tx_modem_samples.restype = c_int
api.freedv_get_n_max_modem_samples.argtype = [c_void_p]
api.freedv_get_n_max_modem_samples.restype = c_int
api.FREEDV_FS_8000 = 8000
api.FREEDV_MODE_DATAC1 = 10
api.FREEDV_MODE_DATAC3 = 12
api.FREEDV_MODE_DATAC0 = 14
api.FREEDV_FS_8000 = 8000
api.FREEDV_MODE_DATAC1 = 10
api.FREEDV_MODE_DATAC3 = 12
api.FREEDV_MODE_DATAC0 = 14
api.FREEDV_MODE_FSK_LDPC = 9
# -------------------------------- FSK LDPC MODE SETTINGS
# advanced structure for fsk modes
# Advanced structure for fsk modes
class ADVANCED(ctypes.Structure):
""" """
_fields_ = [
("interleave_frames", ctypes.c_int),
("interleave_frames", ctypes.c_int),
("M", ctypes.c_int),
("Rs", ctypes.c_int),
("Fs", ctypes.c_int),
("first_tone", ctypes.c_int),
("tone_spacing", ctypes.c_int),
("codename", ctypes.c_char_p),
("first_tone", ctypes.c_int),
("tone_spacing", ctypes.c_int),
("codename", ctypes.c_char_p),
]
'''
@ -203,15 +201,15 @@ api.FREEDV_MODE_FSK_LDPC_1_ADV.tone_spacing = 200
api.FREEDV_MODE_FSK_LDPC_1_ADV.codename = 'H_256_512_4'.encode('utf-8') # code word
# ------- MODEM STATS STRUCTURES
MODEM_STATS_NC_MAX = 50+1
MODEM_STATS_NR_MAX = 160
MODEM_STATS_ET_MAX = 8
MODEM_STATS_NC_MAX = 50 + 1
MODEM_STATS_NR_MAX = 160
MODEM_STATS_ET_MAX = 8
MODEM_STATS_EYE_IND_MAX = 160
MODEM_STATS_NSPEC = 512
MODEM_STATS_MAX_F_HZ = 4000
MODEM_STATS_MAX_F_EST = 4
# modem stats structure
MODEM_STATS_NSPEC = 512
MODEM_STATS_MAX_F_HZ = 4000
MODEM_STATS_MAX_F_EST = 4
# Modem stats structure
class MODEMSTATS(ctypes.Structure):
""" """
_fields_ = [
@ -232,9 +230,7 @@ class MODEMSTATS(ctypes.Structure):
("f_est", (ctypes.c_float * MODEM_STATS_MAX_F_EST)), # How many samples in the eye diagram
("fft_buf", (ctypes.c_float * MODEM_STATS_NSPEC * 2)),
]
# Return code flags for freedv_get_rx_status() function
api.FREEDV_RX_TRIAL_SYNC = 0x1 # demodulator has trial sync
api.FREEDV_RX_SYNC = 0x2 # demodulator has sync
@ -259,129 +255,129 @@ api.rx_sync_flags_to_text = [
"EBS-",
"EBST"]
# audio buffer ---------------------------------------------------------
# Audio buffer ---------------------------------------------------------
class audio_buffer:
"""
thread safe audio buffer, which fits to needs of codec2
Thread safe audio buffer, which fits to needs of codec2
made by David Rowe, VK5DGR
"""
# a buffer of int16 samples, using a fixed length numpy array self.buffer for storage
# A buffer of int16 samples, using a fixed length numpy array self.buffer for storage
# self.nbuffer is the current number of samples in the buffer
def __init__(self, size):
structlog.get_logger("structlog").debug("[C2 ] creating audio buffer", size=size)
structlog.get_logger("structlog").debug("[C2 ] Creating audio buffer", size=size)
self.size = size
self.buffer = np.zeros(size, dtype=np.int16)
self.nbuffer = 0
self.mutex = Lock()
def push(self,samples):
"""
Push new data to buffer
Args:
samples:
samples:
Returns:
Nothing
"""
self.mutex.acquire()
# add samples at the end of the buffer
# Add samples at the end of the buffer
assert self.nbuffer+len(samples) <= self.size
self.buffer[self.nbuffer:self.nbuffer+len(samples)] = samples
self.nbuffer += len(samples)
self.mutex.release()
def pop(self,size):
"""
get data from buffer in size of NIN
Args:
size:
size:
Returns:
Nothing
"""
self.mutex.acquire()
# remove samples from the start of the buffer
self.nbuffer -= size;
# Remove samples from the start of the buffer
self.nbuffer -= size
self.buffer[:self.nbuffer] = self.buffer[size:size+self.nbuffer]
assert self.nbuffer >= 0
self.mutex.release()
# resampler ---------------------------------------------------------
api.FDMDV_OS_48 = int(6) # oversampling rate
api.FDMDV_OS_TAPS_48K = int(48) # number of OS filter taps at 48kHz
api.FDMDV_OS_TAPS_48_8K = int(api.FDMDV_OS_TAPS_48K/api.FDMDV_OS_48) # number of OS filter taps at 8kHz
api.fdmdv_8_to_48_short.argtype = [c_void_p, c_void_p, c_int]
api.fdmdv_48_to_8_short.argtype = [c_void_p, c_void_p, c_int]
# Resampler ---------------------------------------------------------
api.FDMDV_OS_48 = int(6) # oversampling rate
api.FDMDV_OS_TAPS_48K = int(48) # number of OS filter taps at 48kHz
api.FDMDV_OS_TAPS_48_8K = int(api.FDMDV_OS_TAPS_48K/api.FDMDV_OS_48) # number of OS filter taps at 8kHz
api.fdmdv_8_to_48_short.argtype = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int]
api.fdmdv_48_to_8_short.argtype = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int]
class resampler:
"""
resampler class
Re-sampler class
"""
# resample an array of variable length, we just store the filter memories here
# Re-sample an array of variable length, we just store the filter memories here
MEM8 = api.FDMDV_OS_TAPS_48_8K
MEM48 = api.FDMDV_OS_TAPS_48K
def __init__(self):
structlog.get_logger("structlog").debug("[C2 ] create 48<->8 kHz resampler")
structlog.get_logger("structlog").debug("[C2 ] Create 48<->8 kHz resampler")
self.filter_mem8 = np.zeros(self.MEM8, dtype=np.int16)
self.filter_mem48 = np.zeros(self.MEM48)
def resample48_to_8(self,in48):
def resample48_to_8(self, in48):
"""
audio resampler integration from codec2
downsample audio from 48000Hz to 8000Hz
Audio resampler integration from codec2
Downsample audio from 48000Hz to 8000Hz
Args:
in48: input data as np.int16
Returns: downsampled 8000Hz data as np.int16
in48: input data as np.int16
Returns:
Downsampled 8000Hz data as np.int16
"""
assert in48.dtype == np.int16
# length of input vector must be an integer multiple of api.FDMDV_OS_48
assert(len(in48) % api.FDMDV_OS_48 == 0)
# Length of input vector must be an integer multiple of api.FDMDV_OS_48
assert len(in48) % api.FDMDV_OS_48 == 0
# concat filter memory and input samples
# Concatenate filter memory and input samples
in48_mem = np.zeros(self.MEM48+len(in48), dtype=np.int16)
in48_mem[:self.MEM48] = self.filter_mem48
in48_mem[self.MEM48:] = in48
# In C: pin48=&in48_mem[MEM48]
pin48 = byref(np.ctypeslib.as_ctypes(in48_mem), 2*self.MEM48)
pin48 = ctypes.byref(np.ctypeslib.as_ctypes(in48_mem), 2 * self.MEM48)
n8 = int(len(in48) / api.FDMDV_OS_48)
out8 = np.zeros(n8, dtype=np.int16)
api.fdmdv_48_to_8_short(out8.ctypes, pin48, n8);
api.fdmdv_48_to_8_short(out8.ctypes, pin48, n8)
# store memory for next time
# Store memory for next time
self.filter_mem48 = in48_mem[:self.MEM48]
return out8
def resample8_to_48(self,in8):
def resample8_to_48(self, in8):
"""
audio resampler integration from codec2
resample audio from 8000Hz to 48000Hz
Audio resampler integration from codec2
Re-sample audio from 8000Hz to 48000Hz
Args:
in8: input data as np.int16
Returns: 48000Hz audio as np.int16
in8: input data as np.int16
Returns:
48000Hz audio as np.int16
"""
assert in8.dtype == np.int16
# concat filter memory and input samples
# Concatenate filter memory and input samples
in8_mem = np.zeros(self.MEM8+len(in8), dtype=np.int16)
in8_mem[:self.MEM8] = self.filter_mem8
in8_mem[self.MEM8:] = in8
# In C: pin8=&in8_mem[MEM8]
pin8 = byref(np.ctypeslib.as_ctypes(in8_mem), 2*self.MEM8)
pin8 = ctypes.byref(np.ctypeslib.as_ctypes(in8_mem), 2 * self.MEM8)
out48 = np.zeros(api.FDMDV_OS_48*len(in8), dtype=np.int16)
api.fdmdv_8_to_48_short(out48.ctypes, pin8, len(in8));
# store memory for next time
# Store memory for next time
self.filter_mem8 = in8_mem[:self.MEM8]
return out48

View file

@ -8,58 +8,60 @@ Author: DJ2LS, January 2022
daemon for providing basic information for the tnc like audio or serial devices
"""
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
# pylint: disable=import-outside-toplevel
import argparse
import threading
import socketserver
import time
import sys
import subprocess
import ujson as json
import psutil
import serial.tools.list_ports
import static
import crcengine
import re
import structlog
import log_handler
import helpers
import atexit
import multiprocessing
import os
import queue
import audio
import sock
import atexit
import re
import signal
import multiprocessing
import socketserver
import subprocess
import sys
import threading
import time
import crcengine
import psutil
import serial.tools.list_ports
import structlog
import ujson as json
import audio
import helpers
import log_handler
import sock
import static
# signal handler for closing aplication
def signal_handler(sig, frame):
"""
signal handler for closing the network socket on app exit
Signal handler for closing the network socket on app exit
Args:
sig:
frame:
sig:
frame:
Returns: system exit
"""
print('Closing daemon...')
sock.CLOSE_SIGNAL = True
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
class DAEMON():
"""
daemon class
"""
Daemon class
"""
def __init__(self):
# load crc engine
# load crc engine
self.crc_algorithm = crcengine.new('crc16-ccitt-false') # load crc8 library
self.daemon_queue = sock.DAEMON_QUEUE
update_audio_devices = threading.Thread(target=self.update_audio_devices, name="UPDATE_AUDIO_DEVICES", daemon=True)
update_audio_devices.start()
@ -69,26 +71,23 @@ class DAEMON():
worker = threading.Thread(target=self.worker, name="WORKER", daemon=True)
worker.start()
def update_audio_devices(self):
"""
update audio devices and set to static
"""
Update audio devices and set to static
"""
while 1:
try:
if not static.TNCSTARTED:
static.AUDIO_INPUT_DEVICES, static.AUDIO_OUTPUT_DEVICES = audio.get_audio_devices()
except Exception as e:
print(e)
structlog.get_logger("structlog").error("[DMN] update_audio_devices: Exception gathering audio devices:", e=e)
# print(e)
time.sleep(1)
def update_serial_devices(self):
"""
update serial devices and set to static
Update serial devices and set to static
"""
while 1:
try:
@ -96,26 +95,25 @@ class DAEMON():
serial_devices = []
ports = serial.tools.list_ports.comports()
for port, desc, hwid in ports:
# calculate hex of hwid if we have unique names
crc_hwid = self.crc_algorithm(bytes(hwid, encoding='utf-8'))
crc_hwid = crc_hwid.to_bytes(2, byteorder='big')
crc_hwid = crc_hwid.hex()
description = desc + ' [' + crc_hwid + ']'
description = f"{desc} [{crc_hwid}]"
serial_devices.append({"port": str(port), "description": str(description) })
static.SERIAL_DEVICES = serial_devices
time.sleep(1)
except Exception as e:
print(e)
structlog.get_logger("structlog").error("[DMN] update_serial_devices: Exception gathering serial devices:", e=e)
# print(e)
def worker(self):
"""
a worker for the received commands
Worker to handle the received commands
"""
while 1:
try:
data = self.daemon_queue.get()
# data[1] mycall
@ -141,81 +139,79 @@ class DAEMON():
# data[21] enable FSK
# data[22] tx-audio-level
# data[23] respond_to_cq
if data[0] == 'STARTTNC':
structlog.get_logger("structlog").warning("[DMN] Starting TNC", rig=data[5], port=data[6])
# list of parameters, necessary for running subprocess command as a list
options = []
options.append('--port')
options.append(str(static.DAEMONPORT - 1))
options.append('--mycall')
options.append(data[1])
options.append('--mygrid')
options.append(data[2])
options.append('--rx')
options.append(data[3])
options.append('--tx')
options.append(data[4])
# if radiocontrol != disabled
# this should hopefully avoid a ton of problems if we are just running in
# this should hopefully avoid a ton of problems if we are just running in
# disabled mode
if data[13] != 'disabled':
options.append('--devicename')
options.append(data[5])
options.append('--deviceport')
options.append(data[6])
options.append('--serialspeed')
options.append(data[7])
options.append('--pttprotocol')
options.append(data[8])
options.append('--pttport')
options.append(data[9])
options.append('--data_bits')
options.append(data[10])
options.append('--stop_bits')
options.append(data[11])
options.append('--handshake')
options.append(data[12])
options.append('--radiocontrol')
options.append(data[13])
if data[13] != 'rigctld':
options.append('--rigctld_ip')
options.append(data[14])
options.append('--rigctld_port')
options.append(data[15])
if data[16] == 'True':
options.append('--scatter')
if data[17] == 'True':
options.append('--fft')
if data[18] == 'True':
options.append('--500hz')
options.append('--tuning_range_fmin')
options.append(data[19])
options.append('--tuning_range_fmax')
options.append(data[20])
@ -224,35 +220,34 @@ class DAEMON():
# options.append('--fsk')
options.append('--tx-audio-level')
options.append(data[22])
if data[23] == 'True':
options.append(data[22])
if data[23] == 'True':
options.append('--qrv')
# try running tnc from binary, else run from source
# this helps running the tnc in a developer environment
# Try running tnc from binary, else run from source
# This helps running the tnc in a developer environment
try:
command = []
if sys.platform == 'linux' or sys.platform == 'darwin':
if sys.platform in ['linux', 'darwin']:
command.append('./freedata-tnc')
elif sys.platform == 'win32' or sys.platform == 'win64':
elif sys.platform in ['win32', 'win64']:
command.append('freedata-tnc.exe')
command += options
p = subprocess.Popen(command)
atexit.register(p.kill)
structlog.get_logger("structlog").info("[DMN] TNC started", path="binary")
except:
except FileNotFoundError as e:
structlog.get_logger("structlog").error("[DMN] worker: Exception:", e=e)
command = []
if sys.platform == 'linux' or sys.platform == 'darwin':
if sys.platform in ['linux', 'darwin']:
command.append('python3')
elif sys.platform == 'win32' or sys.platform == 'win64':
elif sys.platform in ['win32', 'win64']:
command.append('python')
command.append('main.py')
command += options
p = subprocess.Popen(command)
@ -269,7 +264,7 @@ class DAEMON():
structlog.get_logger("structlog").warning("[DMN] Stopping TNC")
#os.kill(static.TNCPROCESS, signal.SIGKILL)
static.TNCSTARTED = False
'''
'''
# data[1] devicename
# data[2] deviceport
# data[3] serialspeed
@ -282,7 +277,6 @@ class DAEMON():
# data[10] rigctld_ip
# data[11] rigctld_port
if data[0] == 'TEST_HAMLIB':
devicename = data[1]
deviceport = data[2]
serialspeed = data[3]
@ -295,8 +289,6 @@ class DAEMON():
rigctld_ip = data[10]
rigctld_port = data[11]
# check how we want to control the radio
if radiocontrol == 'direct':
import rig
@ -306,64 +298,63 @@ class DAEMON():
import rigctld as rig
else:
import rigdummy as rig
hamlib = rig.radio()
hamlib.open_rig(devicename=devicename, deviceport=deviceport, hamlib_ptt_type=pttprotocol, serialspeed=serialspeed, pttport=pttport, data_bits=data_bits, stop_bits=stop_bits, handshake=handshake, rigctld_ip=rigctld_ip, rigctld_port = rigctld_port)
hamlib.open_rig(devicename=devicename, deviceport=deviceport, hamlib_ptt_type=pttprotocol,
serialspeed=serialspeed, pttport=pttport, data_bits=data_bits, stop_bits=stop_bits,
handshake=handshake, rigctld_ip=rigctld_ip, rigctld_port = rigctld_port)
hamlib_version = rig.hamlib_version
hamlib.set_ptt(True)
hamlib.set_ptt(True)
pttstate = hamlib.get_ptt()
if pttstate:
structlog.get_logger("structlog").info("[DMN] Hamlib PTT", status = 'SUCCESS')
structlog.get_logger("structlog").info("[DMN] Hamlib PTT", status='SUCCESS')
response = {'command': 'test_hamlib', 'result': 'SUCCESS'}
elif not pttstate:
structlog.get_logger("structlog").warning("[DMN] Hamlib PTT", status = 'NO SUCCESS')
structlog.get_logger("structlog").warning("[DMN] Hamlib PTT", status='NO SUCCESS')
response = {'command': 'test_hamlib', 'result': 'NOSUCCESS'}
else:
structlog.get_logger("structlog").error("[DMN] Hamlib PTT", status = 'FAILED')
structlog.get_logger("structlog").error("[DMN] Hamlib PTT", status='FAILED')
response = {'command': 'test_hamlib', 'result': 'FAILED'}
hamlib.set_ptt(False)
hamlib.set_ptt(False)
hamlib.close_rig()
jsondata = json.dumps(response)
sock.SOCKET_QUEUE.put(jsondata)
except Exception as e:
print(e)
structlog.get_logger("structlog").error("[DMN] worker: Exception: ", e=e)
# print(e)
if __name__ == '__main__':
# we need to run this on windows for multiprocessing support
multiprocessing.freeze_support()
# --------------------------------------------GET PARAMETER INPUTS
PARSER = argparse.ArgumentParser(description='FreeDATA Daemon')
PARSER.add_argument('--port', dest="socket_port",default=3001, help="Socket port in the range of 1024-65536", type=int)
PARSER.add_argument('--port', dest="socket_port", default=3001, help="Socket port in the range of 1024-65536", type=int)
ARGS = PARSER.parse_args()
static.DAEMONPORT = ARGS.socket_port
try:
if sys.platform == 'linux':
logging_path = os.getenv("HOME") + '/.config/' + 'FreeDATA/' + 'daemon'
if sys.platform == 'darwin':
logging_path = os.getenv("HOME") + '/Library/' + 'Application Support/' + 'FreeDATA/' + 'daemon'
if sys.platform == 'win32' or sys.platform == 'win64':
logging_path = os.getenv('APPDATA') + '/' + 'FreeDATA/' + 'daemon'
logging_path = os.getenv("HOME") + '/Library/' + 'Application Support/' + 'FreeDATA/' + 'daemon'
if sys.platform in ['win32', 'win64']:
logging_path = os.getenv('APPDATA') + '/' + 'FreeDATA/' + 'daemon'
if not os.path.exists(logging_path):
os.makedirs(logging_path)
log_handler.setup_logging(logging_path)
except:
structlog.get_logger("structlog").error("[DMN] logger init error")
except Exception as e:
structlog.get_logger("structlog").error("[DMN] logger init error", exception=e)
try:
structlog.get_logger("structlog").info("[DMN] Starting TCP/IP socket", port=static.DAEMONPORT)
@ -376,10 +367,9 @@ if __name__ == '__main__':
except Exception as e:
structlog.get_logger("structlog").error("[DMN] Starting TCP/IP socket failed", port=static.DAEMONPORT, e=e)
os._exit(1)
sys.exit(1)
daemon = DAEMON()
structlog.get_logger("structlog").info("[DMN] Starting FreeDATA Daemon", author="DJ2LS", year="2022", version=static.VERSION)
while True:
time.sleep(1)

File diff suppressed because it is too large Load diff

View file

@ -5,151 +5,142 @@ Created on Fri Dec 25 21:25:14 2020
@author: DJ2LS
"""
import time
import crcengine
import structlog
import static
def wait(seconds):
def wait(seconds: float) -> bool:
"""
Args:
seconds:
seconds:
Returns:
"""
timeout = time.time() + seconds
while time.time() < timeout:
time.sleep(0.01)
return True
def get_crc_8(data):
def get_crc_8(data) -> bytes:
"""Author: DJ2LS
Get the CRC8 of a byte string
param: data = bytes()
Args:
data:
data:
Returns:
CRC-8 (CCITT) of the provided data as bytes
"""
crc_algorithm = crcengine.new('crc8-ccitt') # load crc8 library
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(1, byteorder='big')
return crc_data
def get_crc_16(data):
def get_crc_16(data) -> bytes:
"""Author: DJ2LS
Get the CRC16 of a byte string
param: data = bytes()
Args:
data:
data:
Returns:
CRC-16 (CCITT) of the provided data as bytes
"""
crc_algorithm = crcengine.new('crc16-ccitt-false') # load crc16 library
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(2, byteorder='big')
return crc_data
def get_crc_24(data):
def get_crc_24(data) -> bytes:
"""Author: DJ2LS
Get the CRC24-OPENPGP of a byte string
https://github.com/GardenTools/CrcEngine#examples
param: data = bytes()
Args:
data:
data:
Returns:
CRC-24 (OpenPGP) of the provided data as bytes
"""
crc_algorithm = crcengine.create(0x864cfb, 24, 0xb704ce, ref_in=False,
ref_out=False, xor_out=0,
name='crc-24-openpgp')
name='crc-24-openpgp')
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(3, byteorder='big')
return crc_data
def get_crc_32(data):
def get_crc_32(data: bytes) -> bytes:
"""Author: DJ2LS
Get the CRC32 of a byte string
param: data = bytes()
Args:
data:
data:
Returns:
CRC-32 of the provided data as bytes
"""
crc_algorithm = crcengine.new('crc32') # load crc16 library
crc_algorithm = crcengine.new('crc32') # load crc32 library
crc_data = crc_algorithm(data)
crc_data = crc_data.to_bytes(4, byteorder='big')
return crc_data
def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency):
"""
Args:
dxcallsign:
dxgrid:
datatype:
snr:
offset:
frequency:
dxcallsign:
dxgrid:
datatype:
snr:
offset:
frequency:
Returns:
Nothing
"""
# check if buffer empty
if len(static.HEARD_STATIONS) == 0:
static.HEARD_STATIONS.append([dxcallsign, dxgrid, int(time.time()), datatype, snr, offset, frequency])
# if not, we search and update
else:
for i in range(0, len(static.HEARD_STATIONS)):
# update callsign with new timestamp
for i in range(len(static.HEARD_STATIONS)):
# Update callsign with new timestamp
if static.HEARD_STATIONS[i].count(dxcallsign) > 0:
static.HEARD_STATIONS[i] = [dxcallsign, dxgrid, int(time.time()), datatype, snr, offset, frequency]
break
# insert if nothing found
# Insert if nothing found
if i == len(static.HEARD_STATIONS) - 1:
static.HEARD_STATIONS.append([dxcallsign, dxgrid, int(time.time()), datatype, snr, offset, frequency])
break
# for idx, item in enumerate(static.HEARD_STATIONS):
# if dxcallsign in item:
# item = [dxcallsign, int(time.time())]
# static.HEARD_STATIONS[idx] = item
def callsign_to_bytes(callsign):
def callsign_to_bytes(callsign) -> bytes:
"""
Args:
callsign:
callsign:
Returns:
@ -171,21 +162,23 @@ def callsign_to_bytes(callsign):
#-13 Weather stations
#-14 Truckers or generally full time drivers
#-15 generic additional station, digi, mobile, wx, etc
# try converting to bytestring if possible type string
try:
callsign = bytes(callsign, 'utf-8')
except:
# Try converting to bytestring if possible type string
try:
callsign = bytes(callsign, 'utf-8')
except TypeError as e:
structlog.get_logger("structlog").debug("[HLP] callsign_to_bytes: Exception converting callsign to bytes:", e=e)
pass
# we need to do this step to reduce the needed paypload by the callsign ( stripping "-" out of the callsign )
# Need this step to reduce the needed payload by the callsign (stripping "-" out of the callsign)
callsign = callsign.split(b'-')
ssid = 0
try:
ssid = int(callsign[1])
except:
ssid = 0
#callsign = callsign[0]
except IndexError as e:
structlog.get_logger("structlog").debug("[HLP] callsign_to_bytes: Error callsign SSID to integer:", e=e)
#callsign = callsign[0]
#bytestring = bytearray(8)
#bytestring[:len(callsign)] = callsign
#bytestring[7:8] = bytes([ssid])
@ -194,22 +187,18 @@ def callsign_to_bytes(callsign):
callsign = callsign[0].decode("utf-8")
ssid = bytes([ssid]).decode("utf-8")
return encode_call(callsign + ssid)
#return bytes(bytestring)
#return bytes(bytestring)
def bytes_to_callsign(bytestring):
def bytes_to_callsign(bytestring: bytes) -> bytes:
"""
Convert our callsign, received by a frame to a callsign in a human readable format
Args:
bytestring:
bytestring:
Returns:
bytes
"""
# http://www.aprs.org/aprs11/SSIDs.txt
#-0 Your primary station usually fixed and message capable
#-1 generic additional station, digi, mobile, wx, etc
@ -227,8 +216,8 @@ def bytes_to_callsign(bytestring):
#-13 Weather stations
#-14 Truckers or generally full time drivers
#-15 generic additional station, digi, mobile, wx, etc
# we need to do this step to reduce the needed paypload by the callsign ( stripping "-" out of the callsign )
# we need to do this step to reduce the needed paypload by the callsign ( stripping "-" out of the callsign )
'''
callsign = bytes(bytestring[:7])
callsign = callsign.rstrip(b'\x00')
@ -238,39 +227,38 @@ def bytes_to_callsign(bytestring):
callsign = callsign.decode('utf-8')
callsign = callsign + str(ssid)
callsign = callsign.encode('utf-8')
return bytes(callsign)
return bytes(callsign)
'''
decoded = decode_call(bytestring)
callsign = decoded[:-1]
ssid = ord(bytes(decoded[-1], "utf-8"))
return bytes(callsign + "-" + str(ssid), "utf-8")
return bytes(f"{callsign}-{ssid}", "utf-8")
def check_callsign(callsign:bytes, crc_to_check:bytes):
"""
Funktion to check a crc against a callsign to calculate the ssid by generating crc until we got it
Args:
callsign: Callsign which we want to check
crc_to_check: The CRC which we want the callsign to check against
callsign: Callsign which we want to check
crc_to_check: The CRC which we want the callsign to check against
Returns:
[True, Callsign + SSID]
False
"""
print(callsign)
# print(callsign)
structlog.get_logger("structlog").debug("[HLP] check_callsign: Checking:", callsign=callsign)
try:
callsign = callsign.split(b'-')
callsign = callsign[0] # we want the callsign without SSID
except:
callsign = callsign
# We want the callsign without SSID
callsign = callsign.split(b'-')[0]
except Exception as e:
structlog.get_logger("structlog").debug("[HLP] check_callsign: Error callsign SSIG to integer:", e=e)
for ssid in static.SSID_LIST:
call_with_ssid = bytearray(callsign)
call_with_ssid = bytearray(callsign)
call_with_ssid.extend('-'.encode('utf-8'))
call_with_ssid.extend(str(ssid).encode('utf-8'))
@ -279,41 +267,39 @@ def check_callsign(callsign:bytes, crc_to_check:bytes):
if callsign_crc == crc_to_check:
print(call_with_ssid)
return [True, bytes(call_with_ssid)]
return [False, ""]
def encode_grid(grid):
"""
@auther: DB1UJ
Args:
grid:string: maidenhead QTH locater [a-r][a-r][0-9][0-9][a-x][a-x]
grid:string: maidenhead QTH locater [a-r][a-r][0-9][0-9][a-x][a-x]
Returns:
4 bytes contains 26 bit valid data with encoded grid locator
4 bytes contains 26 bit valid data with encoded grid locator
"""
out_code_word = int(0)
out_code_word = 0
grid = grid.upper() # upper case to be save
int_first = ord(grid[0])-65 # -65 offset for 'A' become zero, utf8 table
int_sec = ord(grid[1])-65 # -65 offset for 'A' become zero, utf8 table
int_first = ord(grid[0]) - 65 # -65 offset for 'A' become zero, utf8 table
int_sec = ord(grid[1]) - 65 # -65 offset for 'A' become zero, utf8 table
int_val = (int_first * 18) + int_sec # encode for modulo devision, 2 numbers in 1
out_code_word = (int_val & 0b111111111) # only 9 bit LSB A - R * A - R is needed
out_code_word = out_code_word << 9 # shift 9 bit left having space next bits, letter A-R * A-R
out_code_word <<= 9 # shift 9 bit left having space next bits, letter A-R * A-R
int_val = int(grid[2:4]) # number string to number int, highest value 99
out_code_word = out_code_word | (int_val & 0b1111111) # using bit OR to add new value
out_code_word = out_code_word << 7 # shift 7 bit left having space next bits, letter A-X
out_code_word |= (int_val & 0b1111111) # using bit OR to add new value
out_code_word <<= 7 # shift 7 bit left having space next bits, letter A-X
int_val = ord(grid[4])-65 # -65 offset for 'A' become zero, utf8 table
out_code_word = out_code_word | (int_val & 0b11111) # using bit OR to add new value
out_code_word = out_code_word << 5 # shift 5 bit left having space next bits, letter A-X
int_val = ord(grid[4]) - 65 # -65 offset for 'A' become zero, utf8 table
out_code_word |= (int_val & 0b11111) # using bit OR to add new value
out_code_word <<= 5 # shift 5 bit left having space next bits, letter A-X
int_val = ord(grid[5])-65 # -65 offset for 'A' become zero, utf8 table
out_code_word = out_code_word | (int_val & 0b11111) # using bit OR to add new value
int_val = ord(grid[5]) - 65 # -65 offset for 'A' become zero, utf8 table
out_code_word |= (int_val & 0b11111) # using bit OR to add new value
return out_code_word.to_bytes(length=4, byteorder='big')
@ -321,32 +307,31 @@ def decode_grid(b_code_word:bytes):
"""
@auther: DB1UJ
Args:
b_code_word:bytes: 4 bytes with 26 bit valid data LSB
b_code_word:bytes: 4 bytes with 26 bit valid data LSB
Returns:
grid:str: upper case maidenhead QTH locater [A-R][A-R][0-9][0-9][A-X][A-X]
grid:str: upper case maidenhead QTH locater [A-R][A-R][0-9][0-9][A-X][A-X]
"""
code_word = int.from_bytes(b_code_word, byteorder='big', signed=False)
grid = chr((code_word & 0b11111) + 65)
code_word = code_word >> 5
grid = chr((code_word & 0b11111) + 65)
code_word >>= 5
grid = chr((code_word & 0b11111) + 65) + grid
code_word = code_word >> 7
code_word >>= 7
grid = str(int(code_word & 0b1111111)) + grid
if (code_word & 0b1111111) < 10:
grid = '0' + grid
code_word = code_word >> 9
grid = f'0{grid}'
code_word >>= 9
int_val = int(code_word & 0b111111111)
int_first = int_val // 18
int_sec = int_val % 18
grid = chr(int(int_first)+65) + chr(int(int_sec)+65) + grid
int_first, int_sec = divmod(int_val, 18)
# int_first = int_val // 18
# int_sec = int_val % 18
grid = chr(int(int_first) + 65) + chr(int(int_sec) + 65) + grid
return grid
def encode_call(call):
"""
@auther: DB1UJ
@ -356,17 +341,17 @@ def encode_call(call):
Returns:
6 bytes contains 6 bits/sign encoded 8 char call sign with binary SSID (only upper letters + numbers, SSID)
"""
out_code_word = int(0)
out_code_word = 0
call = call.upper() # upper case to be save
for x in call:
int_val = ord(x)-48 # -48 reduce bits, begin with first number utf8 table
out_code_word = out_code_word << 6 # shift left 6 bit, making space for a new char
out_code_word = out_code_word | (int_val & 0b111111) # bit OR adds the new char, masked with AND 0b111111
out_code_word = out_code_word >> 6 # clean last char
out_code_word = out_code_word << 6 # make clean space
out_code_word = out_code_word | (ord(call[-1]) & 0b111111) # add the SSID uncoded only 0 - 63
int_val = ord(x) - 48 # -48 reduce bits, begin with first number utf8 table
out_code_word <<= 6 # shift left 6 bit, making space for a new char
out_code_word |= (int_val & 0b111111) # bit OR adds the new char, masked with AND 0b111111
out_code_word >>= 6 # clean last char
out_code_word <<= 6 # make clean space
out_code_word |= (ord(call[-1]) & 0b111111) # add the SSID uncoded only 0 - 63
return out_code_word.to_bytes(length=6, byteorder='big')
@ -384,10 +369,9 @@ def decode_call(b_code_word:bytes):
call = str()
while code_word != 0:
call = chr((code_word & 0b111111)+48) + call
code_word = code_word >> 6
call = chr((code_word & 0b111111)+48) + call
code_word >>= 6
call = call[0:-1] + ssid # remove the last char from call and replace with SSID
call = call[:-1] + ssid # remove the last char from call and replace with SSID
return call

View file

@ -3,12 +3,11 @@ def setup_logging(filename):
"""
Args:
filename:
filename:
Returns:
"""
import logging.config
import structlog

View file

@ -6,26 +6,23 @@ Created on Tue Dec 22 16:58:45 2020
@author: DJ2LS
main module for running the tnc
"""
import argparse
import threading
import static
import socketserver
import helpers
import data_handler
import structlog
import log_handler
import modem
import sys
import multiprocessing
import os
import signal
import socketserver
import sys
import threading
import time
import multiprocessing
import structlog
import data_handler
import helpers
import log_handler
import modem
import static
# signal handler for closing aplication
def signal_handler(sig, frame):
@ -33,7 +30,7 @@ def signal_handler(sig, frame):
a signal handler, which closes the network/socket when closing the application
Args:
sig: signal
frame:
frame:
Returns: system exit
@ -41,7 +38,7 @@ def signal_handler(sig, frame):
print('Closing TNC...')
sock.CLOSE_SIGNAL = True
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == '__main__':
@ -51,31 +48,30 @@ if __name__ == '__main__':
PARSER = argparse.ArgumentParser(description='FreeDATA TNC')
PARSER.add_argument('--mycall', dest="mycall", default="AA0AA", help="My callsign", type=str)
PARSER.add_argument('--ssid', dest="ssid_list", nargs='*', default=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], help="SSID list we are responding to", type=str)
PARSER.add_argument('--mygrid', dest="mygrid", default="JN12AA", help="My gridsquare", type=str)
PARSER.add_argument('--mygrid', dest="mygrid", default="JN12AA", help="My gridsquare", type=str)
PARSER.add_argument('--rx', dest="audio_input_device", default=0, help="listening sound card", type=int)
PARSER.add_argument('--tx', dest="audio_output_device", default=0, help="transmitting sound card", type=int)
PARSER.add_argument('--port', dest="socket_port", default=3000, help="Socket port in the range of 1024-65536", type=int)
PARSER.add_argument('--deviceport', dest="hamlib_device_port", default="/dev/ttyUSB0", help="Hamlib device port", type=str)
PARSER.add_argument('--devicename', dest="hamlib_device_name", default="2028", help="Hamlib device name", type=str)
PARSER.add_argument('--serialspeed', dest="hamlib_serialspeed", choices=[1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200], default=9600, help="Serialspeed", type=int)
PARSER.add_argument('--pttprotocol', dest="hamlib_ptt_type", choices=['USB', 'RIG', 'RTS', 'DTR', 'CM108', 'MICDATA', 'PARALLEL', 'DTR-H', 'DTR-L', 'NONE'], default='USB', help="PTT Type", type=str)
PARSER.add_argument('--pttport', dest="hamlib_ptt_port", default="/dev/ttyUSB0", help="PTT Port", type=str)
PARSER.add_argument('--data_bits', dest="hamlib_data_bits", choices=[7, 8], default=8, help="Hamlib data bits", type=int)
PARSER.add_argument('--stop_bits', dest="hamlib_stop_bits", choices=[1, 2], default=1, help="Hamlib stop bits", type=int)
PARSER.add_argument('--handshake', dest="hamlib_handshake", default="None", help="Hamlib handshake", type=str)
PARSER.add_argument('--radiocontrol', dest="hamlib_radiocontrol", choices=['disabled', 'direct', 'rigctl', 'rigctld'], default="disabled", help="Set how you want to control your radio")
PARSER.add_argument('--rigctld_port', dest="rigctld_port", default=4532, type=int, help="Set rigctld port")
PARSER.add_argument('--rigctld_ip', dest="rigctld_ip", default="localhost", help="Set rigctld ip")
PARSER.add_argument('--scatter', dest="send_scatter", action="store_true", help="Send scatter information via network")
PARSER.add_argument('--fft', dest="send_fft", action="store_true", help="Send fft information via network")
PARSER.add_argument('--500hz', dest="low_bandwith_mode", action="store_true", help="Enable low bandwith mode ( 500 Hz only )")
PARSER.add_argument('--fsk', dest="enable_fsk", action="store_true", help="Enable FSK mode for ping, beacon and CQ")
PARSER.add_argument('--qrv', dest="enable_respond_to_cq", action="store_true", help="Enable sending a QRV frame if CQ received")
PARSER.add_argument('--tuning_range_fmin', dest="tuning_range_fmin", choices=[-50.0, -100.0, -150.0, -200.0, -250.0], default=-50.0, help="Tuning range fmin", type=float)
PARSER.add_argument('--devicename', dest="hamlib_device_name", default="2028", help="Hamlib device name", type=str)
PARSER.add_argument('--serialspeed', dest="hamlib_serialspeed", choices=[1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200], default=9600, help="Serialspeed", type=int)
PARSER.add_argument('--pttprotocol', dest="hamlib_ptt_type", choices=['USB', 'RIG', 'RTS', 'DTR', 'CM108', 'MICDATA', 'PARALLEL', 'DTR-H', 'DTR-L', 'NONE'], default='USB', help="PTT Type", type=str)
PARSER.add_argument('--pttport', dest="hamlib_ptt_port", default="/dev/ttyUSB0", help="PTT Port", type=str)
PARSER.add_argument('--data_bits', dest="hamlib_data_bits", choices=[7, 8], default=8, help="Hamlib data bits", type=int)
PARSER.add_argument('--stop_bits', dest="hamlib_stop_bits", choices=[1, 2], default=1, help="Hamlib stop bits", type=int)
PARSER.add_argument('--handshake', dest="hamlib_handshake", default="None", help="Hamlib handshake", type=str)
PARSER.add_argument('--radiocontrol', dest="hamlib_radiocontrol", choices=['disabled', 'direct', 'rigctl', 'rigctld'], default="disabled", help="Set how you want to control your radio")
PARSER.add_argument('--rigctld_port', dest="rigctld_port", default=4532, type=int, help="Set rigctld port")
PARSER.add_argument('--rigctld_ip', dest="rigctld_ip", default="localhost", help="Set rigctld ip")
PARSER.add_argument('--scatter', dest="send_scatter", action="store_true", help="Send scatter information via network")
PARSER.add_argument('--fft', dest="send_fft", action="store_true", help="Send fft information via network")
PARSER.add_argument('--500hz', dest="low_bandwith_mode", action="store_true", help="Enable low bandwith mode ( 500 Hz only )")
PARSER.add_argument('--fsk', dest="enable_fsk", action="store_true", help="Enable FSK mode for ping, beacon and CQ")
PARSER.add_argument('--qrv', dest="enable_respond_to_cq", action="store_true", help="Enable sending a QRV frame if CQ received")
PARSER.add_argument('--tuning_range_fmin', dest="tuning_range_fmin", choices=[-50.0, -100.0, -150.0, -200.0, -250.0], default=-50.0, help="Tuning range fmin", type=float)
PARSER.add_argument('--tuning_range_fmax', dest="tuning_range_fmax", choices=[50.0, 100.0, 150.0, 200.0, 250.0], default=50.0, help="Tuning range fmax", type=float)
PARSER.add_argument('--tx-audio-level', dest="tx_audio_level", default=50, help="Set the tx audio level at an early stage", type=int)
PARSER.add_argument('--tx-audio-level', dest="tx_audio_level", default=50, help="Set the tx audio level at an early stage", type=int)
ARGS = PARSER.parse_args()
# additional step for beeing sure our callsign is correctly
@ -84,10 +80,10 @@ if __name__ == '__main__':
mycallsign = bytes(ARGS.mycall.upper(), '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.MYCALLSIGN_CRC = helpers.get_crc_24(static.MYCALLSIGN)
static.SSID_LIST = ARGS.ssid_list
static.MYGRID = bytes(ARGS.mygrid, 'utf-8')
static.AUDIO_INPUT_DEVICE = ARGS.audio_input_device
static.AUDIO_OUTPUT_DEVICE = ARGS.audio_output_device
@ -101,50 +97,46 @@ if __name__ == '__main__':
static.HAMLIB_STOP_BITS = str(ARGS.hamlib_stop_bits)
static.HAMLIB_HANDSHAKE = ARGS.hamlib_handshake
static.HAMLIB_RADIOCONTROL = ARGS.hamlib_radiocontrol
static.HAMLIB_RGICTLD_IP = ARGS.rigctld_ip
static.HAMLIB_RGICTLD_PORT = str(ARGS.rigctld_port)
static.HAMLIB_RIGCTLD_IP = ARGS.rigctld_ip
static.HAMLIB_RIGCTLD_PORT = str(ARGS.rigctld_port)
static.ENABLE_SCATTER = ARGS.send_scatter
static.ENABLE_FFT = ARGS.send_fft
static.ENABLE_FSK = ARGS.enable_fsk
static.LOW_BANDWITH_MODE = ARGS.low_bandwith_mode
static.TUNING_RANGE_FMIN = ARGS.tuning_range_fmin
static.TUNING_RANGE_FMAX = ARGS.tuning_range_fmax
static.TX_AUDIO_LEVEL = ARGS.tx_audio_level
static.RESPOND_TO_CQ = ARGS.enable_respond_to_cq
static.ENABLE_FSK = ARGS.enable_fsk
static.LOW_BANDWITH_MODE = ARGS.low_bandwith_mode
static.TUNING_RANGE_FMIN = ARGS.tuning_range_fmin
static.TUNING_RANGE_FMAX = ARGS.tuning_range_fmax
static.TX_AUDIO_LEVEL = ARGS.tx_audio_level
static.RESPOND_TO_CQ = ARGS.enable_respond_to_cq
# we need to wait until we got all parameters from argparse first before we can load the other modules
import sock
# config logging
try:
import sock
# config logging
try:
if sys.platform == 'linux':
logging_path = os.getenv("HOME") + '/.config/' + 'FreeDATA/' + 'tnc'
if sys.platform == 'darwin':
logging_path = os.getenv("HOME") + '/Library/' + 'Application Support/' + 'FreeDATA/' + 'tnc'
if sys.platform == 'win32' or sys.platform == 'win64':
logging_path = os.getenv('APPDATA') + '/' + 'FreeDATA/' + 'tnc'
logging_path = os.getenv("HOME") + '/Library/' + 'Application Support/' + 'FreeDATA/' + 'tnc'
if sys.platform in ['win32', 'win64']:
logging_path = os.getenv('APPDATA') + '/' + 'FreeDATA/' + 'tnc'
if not os.path.exists(logging_path):
os.makedirs(logging_path)
log_handler.setup_logging(logging_path)
except:
structlog.get_logger("structlog").error("[DMN] logger init error")
except Exception as e:
structlog.get_logger("structlog").error("[DMN] logger init error", exception=e)
structlog.get_logger("structlog").info("[TNC] Starting FreeDATA", author="DJ2LS", year="2022", version=static.VERSION)
# start data handler
data_handler.DATA()
# start modem
modem = modem.RF()
# --------------------------------------------START CMD SERVER
try:
structlog.get_logger("structlog").info("[TNC] Starting TCP/IP socket", port=static.PORT)
# https://stackoverflow.com/a/16641793
@ -157,6 +149,6 @@ if __name__ == '__main__':
except Exception as e:
structlog.get_logger("structlog").error("[TNC] Starting TCP/IP socket failed", port=static.PORT, e=e)
os._exit(1)
sys.exit(1)
while 1:
time.sleep(1)

File diff suppressed because it is too large Load diff

View file

@ -7,30 +7,27 @@ import atexit
import subprocess
import os
# set global hamlib version
hamlib_version = 0
# append local search path
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
if hasattr(sys, "_MEIPASS"):
sys.path.append(getattr(sys, "_MEIPASS"))
else:
sys.path.append(os.path.abspath("."))
# try importing hamlib
# try importing hamlib
try:
# get python version
python_version = str(sys.version_info[0]) + "." + str(sys.version_info[1])
# installation path for Ubuntu 20.04 LTS python modules
#sys.path.append('/usr/local/lib/python'+ python_version +'/site-packages')
# installation path for Ubuntu 20.10 +
sys.path.append('/usr/local/lib/')
# installation path for Suse
sys.path.append('/usr/local/lib64/python'+ python_version +'/site-packages')
@ -41,14 +38,14 @@ try:
sys.path.append('/usr/local/lib/python3.8/site-packages')
sys.path.append('/usr/local/lib/python3.9/site-packages')
sys.path.append('/usr/local/lib/python3.10/site-packages')
sys.path.append('lib/hamlib/linux/python3.8/site-packages')
import Hamlib
# https://stackoverflow.com/a/4703409
hamlib_version = re.findall(r"[-+]?\d*\.?\d+|\d+", Hamlib.cvar.hamlib_version)
hamlib_version = re.findall(r"[-+]?\d*\.?\d+|\d+", Hamlib.cvar.hamlib_version)
hamlib_version = float(hamlib_version[0])
min_hamlib_version = 4.1
if hamlib_version > min_hamlib_version:
structlog.get_logger("structlog").info("[RIG] Hamlib found", version=hamlib_version)
@ -68,15 +65,14 @@ except Exception as e:
else:
raise Exception
except Exception as e:
structlog.get_logger("structlog").critical("[RIG] HAMLIB NOT INSTALLED", error=e)
structlog.get_logger("structlog").critical("[RIG] HAMLIB NOT INSTALLED", error=e)
hamlib_version = 0
sys.exit()
class radio:
""" """
def __init__(self):
self.devicename = ''
self.devicenumber = ''
self.deviceport = ''
@ -87,26 +83,25 @@ class radio:
self.data_bits = ''
self.stop_bits = ''
self.handshake = ''
def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake, rigctld_port, rigctld_ip):
def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake, rigctld_port, rigctld_ip):
"""
Args:
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_port:
rigctld_ip:
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_port:
rigctld_ip:
Returns:
"""
self.devicename = devicename
self.deviceport = str(deviceport)
self.serialspeed = str(serialspeed) # we need to ensure this is a str, otherwise set_conf functions are crashing
@ -115,8 +110,7 @@ class radio:
self.data_bits = str(data_bits)
self.stop_bits = str(stop_bits)
self.handshake = str(handshake)
# try to init hamlib
try:
Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE)
@ -127,8 +121,7 @@ class radio:
except:
structlog.get_logger("structlog").error("[RIG] Hamlib: rig not supported...")
self.devicenumber = 0
self.my_rig = Hamlib.Rig(self.devicenumber)
self.my_rig.set_conf("rig_pathname", self.deviceport)
self.my_rig.set_conf("retry", "5")
@ -137,10 +130,7 @@ class radio:
self.my_rig.set_conf("stop_bits", self.stop_bits)
self.my_rig.set_conf("data_bits", self.data_bits)
self.my_rig.set_conf("ptt_pathname", self.pttport)
if self.hamlib_ptt_type == 'RIG':
self.hamlib_ptt_type = Hamlib.RIG_PTT_RIG
self.my_rig.set_conf("ptt_type", 'RIG')
@ -149,7 +139,6 @@ class radio:
self.hamlib_ptt_type = Hamlib.RIG_PORT_USB
self.my_rig.set_conf("ptt_type", 'USB')
elif self.hamlib_ptt_type == 'DTR-H':
self.hamlib_ptt_type = Hamlib.RIG_PTT_SERIAL_DTR
self.my_rig.set_conf("dtr_state", "HIGH")
@ -176,22 +165,21 @@ class radio:
elif self.hamlib_ptt_type == 'RIG_PTT_NONE':
self.hamlib_ptt_type = Hamlib.RIG_PTT_NONE
else: #self.hamlib_ptt_type == 'RIG_PTT_NONE':
self.hamlib_ptt_type = Hamlib.RIG_PTT_NONE
structlog.get_logger("structlog").info("[RIG] Opening...", device=self.devicenumber, path=self.my_rig.get_conf("rig_pathname"), serial_speed=self.my_rig.get_conf("serial_speed"), serial_handshake=self.my_rig.get_conf("serial_handshake"), stop_bits=self.my_rig.get_conf("stop_bits"), data_bits=self.my_rig.get_conf("data_bits"), ptt_pathname=self.my_rig.get_conf("ptt_pathname"))
self.my_rig.open()
atexit.register(self.my_rig.close)
try:
# lets determine the error message when opening rig
error = str(Hamlib.rigerror(my_rig.error_status)).splitlines()
error = error[1].split('err=')
error = error[1]
if error == 'Permission denied':
structlog.get_logger("structlog").error("[RIG] Hamlib has no permissions", e = error)
help_url = 'https://github.com/DJ2LS/FreeDATA/wiki/UBUNTU-Manual-installation#1-permissions'
@ -199,7 +187,6 @@ class radio:
except:
structlog.get_logger("structlog").info("[RIG] Hamlib device opened", status='SUCCESS')
# set ptt to false if ptt is stuck for some reason
self.set_ptt(False)
@ -212,16 +199,16 @@ class radio:
except Exception as e:
structlog.get_logger("structlog").error("[RIG] Hamlib - can't open rig", error=e, e=sys.exc_info()[0])
return False
def get_frequency(self):
""" """
return int(self.my_rig.get_freq())
def get_mode(self):
""" """
(hamlib_mode, bandwith) = self.my_rig.get_mode()
return Hamlib.rig_strrmode(hamlib_mode)
def get_bandwith(self):
""" """
(hamlib_mode, bandwith) = self.my_rig.get_mode()
@ -230,16 +217,16 @@ class radio:
# not needed yet beacuse of some possible problems
#def set_mode(self, mode):
# return 0
def get_ptt(self):
""" """
return self.my_rig.get_ptt()
def set_ptt(self, state):
"""
Args:
state:
state:
Returns:
@ -249,7 +236,7 @@ class radio:
else:
self.my_rig.set_ptt(Hamlib.RIG_VFO_CURR, 0)
return state
def close_rig(self):
""" """
self.my_rig.close()

View file

@ -2,26 +2,27 @@
# Intially created by Franco Spinelli, IW2DHW, 01/2022
# Updated by DJ2LS
#
#
# versione mia di rig.py per gestire Ft897D tramite rigctl e senza
# fare alcun riferimento alla configurazione
#
# e' una pezza clamorosa ma serve per poter provare on-air il modem
#
import subprocess
import structlog
import time
import sys
import os
import subprocess
import sys
import time
import structlog
# for rig_model -> rig_number only
# set global hamlib version
hamlib_version = 0
class radio:
""" """
def __init__(self):
self.devicename = ''
self.devicenumber = ''
self.deviceport = ''
@ -32,26 +33,25 @@ class radio:
self.data_bits = ''
self.stop_bits = ''
self.handshake = ''
def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake, rigctld_ip, rigctld_port):
def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake, rigctld_ip, rigctld_port):
"""
Args:
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_ip:
rigctld_port:
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_ip:
rigctld_port:
Returns:
"""
self.devicename = devicename
self.deviceport = deviceport
self.serialspeed = str(serialspeed) # we need to ensure this is a str, otherwise set_conf functions are crashing
@ -60,50 +60,45 @@ class radio:
self.data_bits = data_bits
self.stop_bits = stop_bits
self.handshake = handshake
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
if hasattr(sys, "_MEIPASS"):
sys.path.append(getattr(sys, "_MEIPASS"))
else:
sys.path.append(os.path.abspath("."))
# get devicenumber by looking for deviceobject in Hamlib module
try:
import Hamlib
self.devicenumber = int(getattr(Hamlib, self.devicename))
except:
except Exception as e:
if int(self.devicename):
self.devicenumber = int(self.devicename)
else:
self.devicenumber = 6 #dummy
structlog.get_logger("structlog").warning("[RIGCTL] RADIO NOT FOUND USING DUMMY!", error=e)
structlog.get_logger("structlog").warning("[RIGCTL] Radio not found. Using DUMMY!", error=e)
# set deviceport to dummy port, if we selected dummy model
if self.devicenumber == 1 or self.devicenumber == 6:
self.deviceport = '/dev/ttyUSB0'
print(self.devicenumber, self.deviceport, self.serialspeed)
# select precompiled executable for win32/win64 rigctl
# this is really a hack...somewhen we need a native hamlib integration for windows
if sys.platform == 'win32' or sys.platform == 'win64':
if sys.platform in ['win32', 'win64']:
self.cmd = app_path + 'lib\\hamlib\\'+sys.platform+'\\rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))
else:
self.cmd = 'rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))
# eseguo semplicemente rigctl con il solo comando T 1 o T 0 per
# il set e t per il get
# il set e t per il get
# set ptt to false if ptt is stuck for some reason
self.set_ptt(False)
return True
def get_frequency(self):
""" """
cmd = self.cmd + ' f'
@ -116,7 +111,6 @@ class radio:
except:
return False
def get_mode(self):
""" """
#(hamlib_mode, bandwith) = self.my_rig.get_mode()
@ -126,7 +120,6 @@ class radio:
except:
return False
def get_bandwith(self):
""" """
#(hamlib_mode, bandwith) = self.my_rig.get_mode()
@ -141,18 +134,18 @@ class radio:
"""
Args:
mode:
mode:
Returns:
"""
# non usata
return 0
def get_ptt(self):
""" """
cmd = self.cmd + ' t'
sw_proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
sw_proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
time.sleep(0.5)
status = sw_proc.communicate()[0]
@ -160,12 +153,12 @@ class radio:
return status
except:
return False
def set_ptt(self, state):
"""
Args:
state:
state:
Returns:
@ -178,12 +171,12 @@ class radio:
cmd = cmd + '0'
print('set_ptt', cmd)
sw_proc = subprocess.Popen(cmd, shell=True, text=True)
sw_proc = subprocess.Popen(cmd, shell=True, text=True)
try:
return state
except:
return False
def close_rig(self):
""" """
#self.my_rig.close()

View file

@ -1,21 +1,25 @@
#!/usr/bin/env python3
import socket
import structlog
import log_handler
import logging
import time
import static
# class taken from darsidelemm
# rigctl - https://github.com/darksidelemm/rotctld-web-gui/blob/master/rotatorgui.py#L35
#
# modified and adjusted to FreeDATA needs by DJ2LS
import logging
import socket
import time
import structlog
import log_handler
import static
# set global hamlib version
hamlib_version = 0
class radio():
"""rotctld (hamlib) communication class"""
# Note: This is a massive hack.
"""rigctld (hamlib) communication class"""
# Note: This is a massive hack.
def __init__(self, hostname="localhost", port=4532, poll_rate=5, timeout=5):
""" Open a connection to rotctld, and test it for validity """
@ -31,31 +35,30 @@ class radio():
"""
Args:
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_ip:
rigctld_port:
devicename:
deviceport:
hamlib_ptt_type:
serialspeed:
pttport:
data_bits:
stop_bits:
handshake:
rigctld_ip:
rigctld_port:
Returns:
"""
self.hostname = rigctld_ip
self.port = int(rigctld_port)
if self.connect():
logging.debug(f"Rigctl intialized")
logging.debug("Rigctl intialized")
return True
else:
structlog.get_logger("structlog").error("[RIGCTLD] Can't connect to rigctld!", ip=self.hostname, port=self.port)
return False
structlog.get_logger("structlog").error("[RIGCTLD] Can't connect to rigctld!", ip=self.hostname, port=self.port)
return False
def connect(self):
"""Connect to rigctld instance"""
if not self.connected:
@ -69,19 +72,18 @@ class radio():
self.close_rig()
structlog.get_logger("structlog").warning("[RIGCTLD] Connection to rigctld refused! Reconnect...", ip=self.hostname, port=self.port, e=e)
return False
def close_rig(self):
""" """
self.sock.close()
self.connected = False
def send_command(self, command):
"""Send a command to the connected rotctld instance,
and return the return value.
Args:
command:
command:
Returns:
@ -99,21 +101,21 @@ class radio():
structlog.get_logger("structlog").warning("[RIGCTLD] No command response!", command=command, ip=self.hostname, port=self.port)
self.connected = False
else:
# reconnecting....
time.sleep(0.5)
self.connect()
def get_mode(self):
""" """
try:
data = self.send_command(b"m")
data = data.split(b'\n')
mode = data[0]
return mode.decode("utf-8")
return mode.decode("utf-8")
except:
0
return 0
def get_bandwith(self):
""" """
try:
@ -123,7 +125,7 @@ class radio():
return bandwith.decode("utf-8")
except:
return 0
def get_frequency(self):
""" """
try:
@ -131,19 +133,19 @@ class radio():
return frequency.decode("utf-8")
except:
return 0
def get_ptt(self):
""" """
try:
return self.send_command(b"t")
except:
return False
def set_ptt(self, state):
"""
Args:
state:
state:
Returns:
@ -153,6 +155,6 @@ class radio():
self.send_command(b"T 1")
else:
self.send_command(b"T 0")
return state
return state
except:
return False

View file

@ -3,32 +3,32 @@
import structlog
hamlib_version = 0
class radio:
""" """
def __init__(self):
pass
def open_rig(self, **kwargs):
def open_rig(self, **kwargs):
"""
Args:
**kwargs:
**kwargs:
Returns:
"""
return True
def get_frequency(self):
""" """
return None
def get_mode(self):
""" """
return None
def get_bandwith(self):
""" """
return None
@ -37,29 +37,28 @@ class radio:
"""
Args:
mode:
mode:
Returns:
"""
return None
def get_ptt(self):
""" """
return None
def set_ptt(self, state):
def set_ptt(self, state):
"""
Args:
state:
state:
Returns:
"""
return state
def close_rig(self):
""" """
return

View file

@ -7,11 +7,11 @@ Created on Fri Dec 25 21:25:14 2020
# GET COMMANDS
# "command" : "..."
# SET COMMANDS
# "command" : "..."
# "parameter" : " ..."
# DATA COMMANDS
# "command" : "..."
# "type" : "..."
@ -19,21 +19,25 @@ Created on Fri Dec 25 21:25:14 2020
# "data" : "..."
"""
import atexit
import base64
import logging
import os
import queue
import socketserver
import sys
import threading
import ujson as json
import time
import static
import psutil
import structlog
import ujson as json
import audio
import data_handler
import helpers
import sys
import os
import logging, structlog, log_handler
import queue
import psutil
import audio
import base64
import atexit
import log_handler
import static
SOCKET_QUEUE = queue.Queue()
DAEMON_QUEUE = queue.Queue()
@ -42,34 +46,26 @@ CONNECTED_CLIENTS = set()
CLOSE_SIGNAL = False
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
"""
the socket handler base class
"""
pass
class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
""" """
connection_alive = False
def send_to_client(self):
"""
function called by socket handler
send data to a network client if available
"""
tempdata = b''
while self.connection_alive and not CLOSE_SIGNAL:
# send tnc state as network stream
# 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()
if data != tempdata:
@ -81,27 +77,26 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
tempdata = data
SOCKET_QUEUE.put(data)
time.sleep(0.5)
while not SOCKET_QUEUE.empty():
data = SOCKET_QUEUE.get()
sock_data = bytes(data, 'utf-8')
sock_data += b'\n' # append line limiter
# send data to all clients
#try:
for client in CONNECTED_CLIENTS:
try:
client.send(sock_data)
except Exception as e:
print("connection lost...")
print(e)
# print("connection lost...")
structlog.get_logger("structlog").info("[SCK] Connection lost", e=e)
self.connection_alive = False
# we want to transmit scatter data only once to reduce network traffic
static.SCATTER = []
# we want to display INFO messages only once
static.INFO = []
static.INFO = []
#self.request.sendall(sock_data)
time.sleep(0.15)
@ -115,18 +110,17 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
try:
chunk = self.request.recv(1024)
data += chunk
if chunk == b'':
#print("connection broken. Closing...")
self.connection_alive = False
if data.startswith(b'{') and data.endswith(b'}\n'):
if data.startswith(b'{') and data.endswith(b'}\n'):
# split data by \n if we have multiple commands in socket buffer
data = data.split(b'\n')
# remove empty data
data.remove(b'')
# iterate thorugh data list
for commands in data:
if self.server.server_address[1] == static.PORT:
@ -136,60 +130,53 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
# wait some time between processing multiple commands
# this is only a first test to avoid doubled transmission
# we might improve this by only processing one command or
# we might improve this by only processing one command or
# doing some kind of selection to determin which commands need to be dropped
# and which one can be processed during a running transmission
time.sleep(3)
# finally delete our rx buffer to be ready for new commands
data = bytes()
except Exception as e:
structlog.get_logger("structlog").info("[SCK] Connection closed", ip=self.client_address[0], port=self.client_address[1], e=e)
self.connection_alive = False
def handle(self):
"""
socket handler
"""
CONNECTED_CLIENTS.add(self.request)
structlog.get_logger("structlog").debug("[SCK] Client connected", ip=self.client_address[0], port=self.client_address[1])
self.connection_alive = True
self.sendThread = threading.Thread(target=self.send_to_client, args=[],daemon=True).start()
self.receiveThread = threading.Thread(target=self.receive_from_client, args=[],daemon=True).start()
# keep connection alive until we close it
while self.connection_alive and not CLOSE_SIGNAL:
time.sleep(1)
def finish(self):
""" """
structlog.get_logger("structlog").warning("[SCK] Closing client socket", ip=self.client_address[0], port=self.client_address[1])
structlog.get_logger("structlog").warning("[SCK] Closing client socket", ip=self.client_address[0], port=self.client_address[1])
try:
CONNECTED_CLIENTS.remove(self.request)
except:
structlog.get_logger("structlog").warning("[SCK] client connection already removed from client list", client=self.request)
def process_tnc_commands(data):
"""
process tnc commands
Args:
data:
data:
Returns:
"""
# 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)
structlog.get_logger("structlog").debug("[SCK] CMD", command=received_json)
@ -198,19 +185,18 @@ def process_tnc_commands(data):
try:
static.TX_AUDIO_LEVEL = int(received_json["value"])
command_response("tx_audio_level", True)
except Exception as e:
command_response("tx_audio_level", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
except Exception as e:
command_response("tx_audio_level", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# TRANSMIT SINE WAVE -----------------------------------------------------
if received_json["type"] == "set" and received_json["command"] == "send_test_frame":
try:
data_handler.DATA_QUEUE_TRANSMIT.put(['SEND_TEST_FRAME'])
command_response("send_test_frame", True)
except Exception as e:
command_response("send_test_frame", False)
except Exception as e:
command_response("send_test_frame", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# CQ CQ CQ -----------------------------------------------------
@ -218,10 +204,11 @@ def process_tnc_commands(data):
try:
data_handler.DATA_QUEUE_TRANSMIT.put(['CQ'])
command_response("cqcqcq", True)
except Exception as e:
command_response("cqcqcq", False)
except Exception as e:
command_response("cqcqcq", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# START_BEACON -----------------------------------------------------
if received_json["command"] == "start_beacon":
try:
@ -229,21 +216,21 @@ def process_tnc_commands(data):
interval = int(received_json["parameter"])
data_handler.DATA_QUEUE_TRANSMIT.put(['BEACON', interval, True])
command_response("start_beacon", True)
except Exception as e:
command_response("start_beacon", False)
except Exception as e:
command_response("start_beacon", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# STOP_BEACON -----------------------------------------------------
if received_json["command"] == "stop_beacon":
try:
try:
structlog.get_logger("structlog").warning("[TNC] Stopping beacon!")
static.BEACON_STATE = False
data_handler.DATA_QUEUE_TRANSMIT.put(['BEACON', None, False])
command_response("stop_beacon", True)
except Exception as e:
command_response("stop_beacon", False)
except Exception as e:
command_response("stop_beacon", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# PING ----------------------------------------------------------
if received_json["type"] == 'ping' and received_json["command"] == "ping":
# send ping frame and wait for ACK
@ -255,14 +242,13 @@ def process_tnc_commands(data):
# then we are forcing a station ssid = 0
dxcallsign = helpers.callsign_to_bytes(dxcallsign)
dxcallsign = helpers.bytes_to_callsign(dxcallsign)
data_handler.DATA_QUEUE_TRANSMIT.put(['PING', dxcallsign])
command_response("ping", True)
except Exception as e:
except Exception as e:
command_response("ping", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# CONNECT ----------------------------------------------------------
if received_json["type"] == 'arq' and received_json["command"] == "connect":
static.BEACON_PAUSE = True
@ -275,14 +261,14 @@ def process_tnc_commands(data):
# then we are forcing a station ssid = 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)
data_handler.DATA_QUEUE_TRANSMIT.put(['CONNECT', dxcallsign])
command_response("connect", True)
except Exception as e:
command_response("connect", False)
except Exception as e:
command_response("connect", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# DISCONNECT ----------------------------------------------------------
@ -291,15 +277,14 @@ def process_tnc_commands(data):
try:
data_handler.DATA_QUEUE_TRANSMIT.put(['DISCONNECT'])
command_response("disconnect", True)
except Exception as e:
except Exception as e:
command_response("disconnect", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# TRANSMIT RAW DATA -------------------------------------------
if received_json["type"] == 'arq' and received_json["command"] == "send_raw":
static.BEACON_PAUSE = True
try:
try:
if not static.ARQ_SESSION:
dxcallsign = received_json["parameter"][0]["dxcallsign"]
# additional step for beeing sure our callsign is correctly
@ -314,37 +299,33 @@ def process_tnc_commands(data):
dxcallsign = static.DXCALLSIGN
static.DXCALLSIGN_CRC = helpers.get_crc_24(static.DXCALLSIGN)
mode = int(received_json["parameter"][0]["mode"])
n_frames = int(received_json["parameter"][0]["n_frames"])
base64data = received_json["parameter"][0]["data"]
# check if specific callsign is set with different SSID than the TNC is initialized
try:
mycallsign = received_json["parameter"][0]["mycallsign"]
except:
mycallsign = static.MYCALLSIGN
# check if transmission uuid provided else set no-uuid
try:
arq_uuid = received_json["uuid"]
except:
arq_uuid = 'no-uuid'
if not len(base64data) % 4:
if not len(base64data) % 4:
binarydata = base64.b64decode(base64data)
data_handler.DATA_QUEUE_TRANSMIT.put(['ARQ_RAW', binarydata, mode, n_frames, arq_uuid, mycallsign])
else:
raise TypeError
except Exception as e:
command_response("send_raw", False)
except Exception as e:
command_response("send_raw", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# STOP TRANSMISSION ----------------------------------------------------------
# STOP TRANSMISSION ----------------------------------------------------------
if received_json["type"] == 'arq' and received_json["command"] == "stop_transmission":
try:
if static.TNC_STATE == 'BUSY' or static.ARQ_STATE:
@ -353,10 +334,9 @@ def process_tnc_commands(data):
static.TNC_STATE = 'IDLE'
static.ARQ_STATE = False
command_response("stop_transmission", True)
except Exception as e:
command_response("stop_transmission", False)
except Exception as e:
command_response("stop_transmission", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
if received_json["type"] == 'get' and received_json["command"] == 'rx_buffer':
try:
@ -364,8 +344,8 @@ def process_tnc_commands(data):
"command": "rx_buffer",
"data-array": [],
}
for i in range(0, len(static.RX_BUFFER)):
for i in range(len(static.RX_BUFFER)):
#print(static.RX_BUFFER[i][4])
#rawdata = json.loads(static.RX_BUFFER[i][4])
base64_data = static.RX_BUFFER[i][4]
@ -375,18 +355,17 @@ def process_tnc_commands(data):
#self.request.sendall(bytes(jsondata, encoding))
SOCKET_QUEUE.put(jsondata)
command_response("rx_buffer", True)
except Exception as e:
command_response("rx_buffer", False)
except Exception as e:
command_response("rx_buffer", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
if received_json["type"] == 'set' and received_json["command"] == 'del_rx_buffer':
try:
static.RX_BUFFER = []
command_response("del_rx_buffer", True)
except Exception as e:
command_response("del_rx_buffer", False)
except Exception as e:
command_response("del_rx_buffer", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
# exception, if JSON cant be decoded
@ -397,7 +376,6 @@ def send_tnc_state():
"""
send the tnc state to network
"""
encoding = 'utf-8'
output = {
@ -410,7 +388,7 @@ def send_tnc_state():
"audio_rms": str(static.AUDIO_RMS),
"snr": str(static.SNR),
"frequency": str(static.HAMLIB_FREQUENCY),
"speed_level": str(static.ARQ_SPEED_LEVEL),
"speed_level": str(static.ARQ_SPEED_LEVEL),
"mode": str(static.HAMLIB_MODE),
"bandwith": str(static.HAMLIB_BANDWITH),
"fft": str(static.FFT),
@ -432,19 +410,24 @@ def send_tnc_state():
}
# add heard stations to heard stations object
for i in range(0, len(static.HEARD_STATIONS)):
output["stations"].append({"dxcallsign": str(static.HEARD_STATIONS[i][0], 'utf-8'), "dxgrid": str(static.HEARD_STATIONS[i][1], 'utf-8'),"timestamp": static.HEARD_STATIONS[i][2], "datatype": static.HEARD_STATIONS[i][3], "snr": static.HEARD_STATIONS[i][4], "offset": static.HEARD_STATIONS[i][5], "frequency": static.HEARD_STATIONS[i][6]})
jsondata = json.dumps(output)
return jsondata
for heard in static.HEARD_STATIONS:
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]})
return json.dumps(output)
def process_daemon_commands(data):
"""
process daemon commands
Args:
data:
data:
Returns:
@ -465,11 +448,10 @@ def process_daemon_commands(data):
command_response("mycallsign", True)
structlog.get_logger("structlog").info("[DMN] SET MYCALL", call=static.MYCALLSIGN, crc=static.MYCALLSIGN_CRC)
except Exception as e:
except Exception as e:
command_response("mycallsign", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
if received_json["type"] == 'set' and received_json["command"] == 'mygrid':
try:
mygrid = received_json["parameter"]
@ -480,13 +462,11 @@ def process_daemon_commands(data):
static.MYGRID = bytes(mygrid, 'utf-8')
structlog.get_logger("structlog").info("[SCK] SET MYGRID", grid=static.MYGRID)
command_response("mygrid", True)
except Exception as e:
except Exception as e:
command_response("mygrid", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
if received_json["type"] == 'set' and received_json["command"] == 'start_tnc' and not static.TNCSTARTED:
try:
mycall = str(received_json["parameter"][0]["mycall"])
mygrid = str(received_json["parameter"][0]["mygrid"])
@ -512,44 +492,42 @@ def process_daemon_commands(data):
tx_audio_level = str(received_json["parameter"][0]["tx_audio_level"])
respond_to_cq = str(received_json["parameter"][0]["respond_to_cq"])
# print some debugging parameters
for item in received_json["parameter"][0]:
structlog.get_logger("structlog").debug("[DMN] TNC Startup Config : " + item, value=received_json["parameter"][0][item])
DAEMON_QUEUE.put(['STARTTNC', \
mycall, \
mygrid, \
rx_audio, \
tx_audio, \
devicename, \
deviceport, \
serialspeed, \
pttprotocol, \
pttport, \
data_bits, \
stop_bits, \
handshake, \
radiocontrol, \
rigctld_ip, \
rigctld_port, \
enable_scatter, \
enable_fft, \
low_bandwith_mode, \
tuning_range_fmin, \
tuning_range_fmax, \
enable_fsk, \
tx_audio_level, \
respond_to_cq \
DAEMON_QUEUE.put(['STARTTNC',
mycall,
mygrid,
rx_audio,
tx_audio,
devicename,
deviceport,
serialspeed,
pttprotocol,
pttport,
data_bits,
stop_bits,
handshake,
radiocontrol,
rigctld_ip,
rigctld_port,
enable_scatter,
enable_fft,
low_bandwith_mode,
tuning_range_fmin,
tuning_range_fmax,
enable_fsk,
tx_audio_level,
respond_to_cq,
])
command_response("start_tnc", True)
except Exception as e:
command_response("start_tnc", False)
except Exception as e:
command_response("start_tnc", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
if received_json["type"] == 'get' and received_json["command"] == 'test_hamlib':
try:
devicename = str(received_json["parameter"][0]["devicename"])
deviceport = str(received_json["parameter"][0]["deviceport"])
@ -563,43 +541,43 @@ def process_daemon_commands(data):
rigctld_ip = str(received_json["parameter"][0]["rigctld_ip"])
rigctld_port = str(received_json["parameter"][0]["rigctld_port"])
DAEMON_QUEUE.put(['TEST_HAMLIB', \
devicename, \
deviceport, \
serialspeed, \
pttprotocol, \
pttport, \
data_bits, \
stop_bits, \
handshake, \
radiocontrol, \
rigctld_ip, \
rigctld_port \
DAEMON_QUEUE.put(['TEST_HAMLIB',
devicename,
deviceport,
serialspeed,
pttprotocol,
pttport,
data_bits,
stop_bits,
handshake,
radiocontrol,
rigctld_ip,
rigctld_port,
])
command_response("test_hamlib", True)
except Exception as e:
command_response("test_hamlib", False)
except Exception as e:
command_response("test_hamlib", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
if received_json["type"] == 'set' and received_json["command"] == 'stop_tnc':
try:
static.TNCPROCESS.kill()
# unregister process from atexit to avoid process zombies
atexit.unregister(static.TNCPROCESS.kill)
structlog.get_logger("structlog").warning("[DMN] Stopping TNC")
static.TNCSTARTED = False
command_response("stop_tnc", True)
except Exception as e:
command_response("stop_tnc", False)
except Exception as e:
command_response("stop_tnc", False)
structlog.get_logger("structlog").warning("[SCK] command execution error", e=e, command=received_json)
def send_daemon_state():
"""
send the daemon state to network
"""
try:
python_version = str(sys.version_info[0]) + "." + str(sys.version_info[1])
python_version = f"{str(sys.version_info[0])}.{str(sys.version_info[1])}"
output = {
'command': 'daemon_state',
@ -613,26 +591,21 @@ def send_daemon_state():
#'ram': str(psutil.virtual_memory().percent),
'version': '0.1'
}
if static.TNCSTARTED:
output["daemon_state"].append({"status": "running"})
else:
output["daemon_state"].append({"status": "stopped"})
jsondata = json.dumps(output)
return jsondata
except Exception as e:
structlog.get_logger("structlog").warning("[SCK] error", e=e)
return None
def command_response(command, status):
if status:
status = "OK"
else:
status = "Failed"
jsondata = {"command_response": command, "status" : status}
s_status = "OK" if status else "Failed"
jsondata = {"command_response": command, "status" : s_status}
data_out = json.dumps(jsondata)
SOCKET_QUEUE.put(data_out)

View file

@ -5,7 +5,7 @@ Created on Wed Dec 23 11:13:57 2020
@author: DJ2LS
Here we are saving application wide variables and stats, which have to be accessed everywhere.
Not nice, suggestions are appreciated :-)
Not nice, suggestions are appreciated :-)
"""
VERSION = '0.4.0-alpha'
@ -15,7 +15,6 @@ DAEMONPORT = 3001
TNCSTARTED = False
TNCPROCESS = 0
# Operator Defaults
MYCALLSIGN = b'AA0AA'
MYCALLSIGN_CRC = b'A'
@ -39,7 +38,6 @@ SOCKET_TIMEOUT = 1 # seconds
SERIAL_DEVICES = []
# ---------------------------------
PTT_STATE = False
TRANSMITTING = False
@ -55,7 +53,7 @@ HAMLIB_HANDSHAKE = 'None'
HAMLIB_RADIOCONTROL = 'direct'
HAMLIB_RIGCTLD_IP = '127.0.0.1'
HAMLIB_RIGCTLD_PORT = '4532'
HAMLIB_FREQUENCY = 0
HAMLIB_MODE = ''
HAMLIB_BANDWITH = 0
@ -96,7 +94,6 @@ ARQ_TRANSMISSION_PERCENT = 0
ARQ_SPEED_LEVEL = 0
TOTAL_BYTES = 0
#CHANNEL_STATE = 'RECEIVING_SIGNALLING'
TNC_STATE = 'IDLE'
ARQ_STATE = False
@ -122,4 +119,4 @@ INFO = []
# ------- CODEC2 SETTINGS
TUNING_RANGE_FMIN = -50.0
TUNING_RANGE_FMAX = 50.0
TUNING_RANGE_FMAX = 50.0