and another global has been eliminated..

This commit is contained in:
DJ2LS 2023-11-17 23:05:41 +01:00
parent 145ac1533a
commit 5121cb5a14
13 changed files with 87 additions and 34 deletions

View file

@ -3,7 +3,6 @@ Gather information about audio devices.
"""
import atexit
import multiprocessing
import crcengine
import sounddevice as sd
import structlog

View file

@ -27,7 +27,7 @@ import stats
import ujson as json
from codec2 import FREEDV_MODE, FREEDV_MODE_USED_SLOTS
from queues import DATA_QUEUE_RECEIVED, DATA_QUEUE_TRANSMIT, RX_BUFFER
from static import FRAME_TYPE as FR_TYPE
from modem_frametypes import FRAME_TYPE as FR_TYPE
import broadcast
TESTMODE = False
@ -45,6 +45,7 @@ class DATA:
self.event_queue = event_queue
self.mycallsign = config['STATION']['mycall']
self.ssid_list = config['STATION']['ssid_list']
self.mycallsign_crc = b''
self.mygrid = config['STATION']['mygrid']
@ -423,8 +424,8 @@ class DATA:
frametype = int.from_bytes(bytes(bytes_out[:1]), "big")
# check for callsign CRC
_valid1, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[1:4]))
_valid2, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[2:5]))
_valid1, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[1:4]), self.ssid_list)
_valid2, _ = helpers.check_callsign(self.mycallsign, bytes(bytes_out[2:5]), self.ssid_list)
# check for session ID
# signalling frames
@ -739,6 +740,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
# Check if we received all frames in the burst by checking if burst buffer has no more "Nones"
@ -1534,6 +1536,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
frametype = int.from_bytes(bytes(data_in[:1]), "big")
@ -1589,6 +1592,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
# Force data loops of Modem to stop and continue with next frame
self.data_frame_ack_received = True
@ -1627,6 +1631,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.send_data_to_socket_queue(
freedata="modem-message",
@ -1666,6 +1671,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.log.info("[Modem] ARQ REPEAT RECEIVED")
@ -1881,7 +1887,7 @@ class DATA:
self.states.set("dxcallsign", self.dxcallsign)
# check if callsign ssid override
valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[2:5])
valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[2:5], self.ssid_list)
self.mycallsign = mycallsign
self.dxgrid = b'------'
helpers.add_to_heard_stations(
@ -1891,6 +1897,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.log.info(
"[Modem] SESSION ["
@ -1957,7 +1964,7 @@ class DATA:
# We've arrived here from process_data which already checked that the frame
# is intended for this station.
# Close the session if the CRC matches the remote station in static.
_valid_crc, mycallsign = helpers.check_callsign(self.mycallsign, bytes(data_in[2:5]))
_valid_crc, mycallsign = helpers.check_callsign(self.mycallsign, bytes(data_in[2:5]), self.ssid_list)
_valid_session = helpers.check_session_id(self.session_id, bytes(data_in[1:2]))
if (_valid_crc or _valid_session) and self.states.arq_session_state not in ["disconnected"]:
self.states.set("arq_session_state", "disconnected")
@ -1969,6 +1976,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.log.info(
"[Modem] SESSION ["
@ -2020,7 +2028,7 @@ class DATA:
"""
# Accept session data if the DXCALLSIGN_CRC matches the station in static or session id.
_valid_crc, _ = helpers.check_callsign(self.dxcallsign, bytes(data_in[4:7]))
_valid_crc, _ = helpers.check_callsign(self.dxcallsign, bytes(data_in[4:7]), self.ssid_list)
_valid_session = helpers.check_session_id(self.session_id, bytes(data_in[1:2]))
if _valid_crc or _valid_session and self.states.arq_session_state in ["connected", "connecting"]:
self.log.debug("[Modem] Received session heartbeat")
@ -2032,6 +2040,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.send_data_to_socket_queue(
@ -2259,7 +2268,7 @@ class DATA:
self.arq_file_transfer = True
# check if callsign ssid override
_, self.mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4])
_, self.mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4], self.ssid_list)
# ignore channel opener if already in ARQ STATE
# use case: Station A is connecting to Station B while
@ -2350,12 +2359,13 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.session_id = data_in[13:14]
# check again if callsign ssid override
_, self.mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4])
_, self.mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4], self.ssid_list)
self.log.info(
"[Modem] ARQ | DATA | RX | ["
@ -2464,6 +2474,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.log.info(
@ -2566,7 +2577,7 @@ class DATA:
dxcallsign = helpers.bytes_to_callsign(bytes(data_in[7:13]))
# check if callsign ssid override
valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4])
valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4], self.ssid_list)
if not valid:
# PING packet not for me.
self.log.debug("[Modem] received_ping: ping not for this station.")
@ -2591,6 +2602,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.send_data_to_socket_queue(
@ -2634,7 +2646,7 @@ class DATA:
# check if we received correct ping
# check if callsign ssid override
_valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4])
_valid, mycallsign = helpers.check_callsign(self.mycallsign, data_in[1:4], self.ssid_list)
if _valid:
self.dxgrid = bytes(helpers.decode_grid(data_in[7:11]), "UTF-8")
@ -2659,6 +2671,7 @@ class DATA:
combined_snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
self.log.info(
@ -2821,6 +2834,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
def transmit_cq(self) -> None:
@ -2888,6 +2902,7 @@ class DATA:
snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)
if self.respond_to_cq and self.respond_to_call:
@ -2969,6 +2984,7 @@ class DATA:
combined_snr,
self.modem_frequency_offset,
self.states.radio_frequency,
self.states.heard_stations
)

View file

@ -26,7 +26,7 @@ import threading
import time
import wave
import helpers
import static
import deprecated_static
from global_instances import ARQ, AudioParam, Beacon, Channel, Daemon, HamlibParam, ModemParam, Station, Statistics, TCIParam, Modem, MeshParam
import structlog
from random import randrange

View file

@ -1,6 +1,6 @@
# global_instances.py
from static import Daemon, ARQ, AudioParam, Beacon, Channel, HamlibParam, ModemParam, Station, Statistics, TCIParam, Modem, MeshParam
from deprecated_static import Daemon, ARQ, AudioParam, Beacon, Channel, HamlibParam, ModemParam, Station, Statistics, TCIParam, Modem, MeshParam
# Initialize instances with appropriate default values

View file

@ -7,7 +7,6 @@ Created on Fri Dec 25 21:25:14 2020
import time
from datetime import datetime,timezone
import crcengine
from global_instances import Station, Modem
import structlog
import numpy as np
import threading
@ -122,7 +121,7 @@ def get_crc_32(data: bytes) -> bytes:
return crc_data
def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency):
def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency, heard_stations_list):
"""
Args:
@ -137,16 +136,16 @@ def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency):
Nothing
"""
# check if buffer empty
if len(Modem.heard_stations) == 0:
Modem.heard_stations.append(
if len(heard_stations_list) == 0:
heard_stations_list.append(
[dxcallsign, dxgrid, int(datetime.now(timezone.utc).timestamp()), datatype, snr, offset, frequency]
)
# if not, we search and update
else:
for i in range(len(Modem.heard_stations)):
for i in range(len(heard_stations_list)):
# Update callsign with new timestamp
if Modem.heard_stations[i].count(dxcallsign) > 0:
Modem.heard_stations[i] = [
if heard_stations_list[i].count(dxcallsign) > 0:
heard_stations_list[i] = [
dxcallsign,
dxgrid,
int(time.time()),
@ -157,8 +156,8 @@ def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency):
]
break
# Insert if nothing found
if i == len(Modem.heard_stations) - 1:
Modem.heard_stations.append(
if i == len(heard_stations_list) - 1:
heard_stations_list.append(
[
dxcallsign,
dxgrid,
@ -172,10 +171,10 @@ def add_to_heard_stations(dxcallsign, dxgrid, datatype, snr, offset, frequency):
break
# for idx, item in enumerate(Modem.heard_stations):
# for idx, item in enumerate(heard_stations_list):
# if dxcallsign in item:
# item = [dxcallsign, int(time.time())]
# Modem.heard_stations[idx] = item
# heard_stations_list[idx] = item
def callsign_to_bytes(callsign) -> bytes:
@ -287,7 +286,7 @@ def bytes_to_callsign(bytestring: bytes) -> bytes:
return bytes(f"{callsign}-{ssid}", "utf-8")
def check_callsign(callsign: bytes, crc_to_check: bytes):
def check_callsign(callsign: bytes, crc_to_check: bytes, ssid_list):
"""
Function to check a crc against a callsign to calculate the
ssid by generating crc until we find the correct SSID
@ -312,7 +311,7 @@ def check_callsign(callsign: bytes, crc_to_check: bytes):
except Exception as err:
log.debug("[HLP] check_callsign: Error callsign SSID to integer:", e=err)
for ssid in Station.ssid_list:
for ssid in ssid_list:
call_with_ssid = bytearray(callsign)
call_with_ssid.extend("-".encode("utf-8"))
call_with_ssid.extend(str(ssid).encode("utf-8"))

View file

@ -36,8 +36,9 @@ SNR: negative --> * 2
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
# pylint: disable=import-outside-toplevel, attribute-defined-outside-init
from static import FRAME_TYPE
from global_instances import ARQ,ModemParam, MeshParam, Station, Modem
from modem_frametypes import FRAME_TYPE
#from global_instances import ARQ,ModemParam, MeshParam, Station, Modem
from codec2 import FREEDV_MODE
import numpy as np
@ -46,7 +47,6 @@ import threading
import modem
import helpers
import structlog
import ujson as json
from queues import MESH_RECEIVED_QUEUE, MESH_QUEUE_TRANSMIT, MESH_SIGNALLING_TABLE

View file

@ -20,7 +20,6 @@ import codec2
import itertools
import numpy as np
import sounddevice as sd
from static import FRAME_TYPE
import structlog
import tci
import cw

41
modem/modem_frametypes.py Normal file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from enum import Enum
class FRAME_TYPE(Enum):
"""Lookup for frame types"""
BURST_01 = 10
BURST_02 = 11
BURST_03 = 12
BURST_04 = 13
# ...
BURST_51 = 50
BURST_ACK = 60
FR_ACK = 61
FR_REPEAT = 62
FR_NACK = 63
BURST_NACK = 64
MESH_BROADCAST = 100
MESH_SIGNALLING_PING = 101
MESH_SIGNALLING_PING_ACK = 102
CQ = 200
QRV = 201
PING = 210
PING_ACK = 211
IS_WRITING = 215
ARQ_SESSION_OPEN = 221
ARQ_SESSION_HB = 222
ARQ_SESSION_CLOSE = 223
ARQ_DC_OPEN_W = 225
ARQ_DC_OPEN_ACK_W = 226
ARQ_DC_OPEN_N = 227
ARQ_DC_OPEN_ACK_N = 228
ARQ_STOP = 249
BEACON = 250
FEC = 251
FEC_WAKEUP = 252
IDENT = 254
TEST_FRAME = 255

View file

@ -9,7 +9,6 @@ import queue
import server_commands
import service_manager
import state_manager
from static import Modem as modeminfo
import threading
import ujson as json
@ -163,7 +162,7 @@ def post_modem_stop():
@app.route('/version', methods=['GET'])
def get_modem_version():
return api_response({"version": modeminfo.version})
return api_response({"version": 0})
# @app.route('/modem/arq_connect', methods=['POST'])

View file

@ -1,6 +1,5 @@
from queues import DATA_QUEUE_TRANSMIT
import base64
import helpers
import structlog
log = structlog.get_logger("COMMANDS")

View file

@ -23,6 +23,7 @@ class STATES:
self.audio_dbfs = 0
self.dxcallsign: bytes = b"ZZ9YY-0"
self.dxgrid: bytes = b"------"
self.heard_stations = []
self.arq_session_state = 'disconnected'
self.arq_speed_level = 0

View file

@ -1,3 +1,3 @@
#autopep8 --in-place --select W291,W293,W391,E231 --ignore E501 ../modem.py ../data_handler.py ../deprecated_main.py ../deprecated_sock.py ../static.py ../helpers.py
#autopep8 --in-place --select W291,W293,W391,E231 --ignore E501 ../modem.py ../data_handler.py ../deprecated_main.py ../deprecated_sock.py ../deprecated_static.py ../helpers.py
autopep8 --in-place --ignore E501 ../modem.py ../data_handler.py ../main.py ../sock.py ../static.py ../helpers.py