2020-03-21 21:40:39 +00:00
|
|
|
from owrx.config import Config
|
2019-12-21 19:58:28 +00:00
|
|
|
import threading
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import shlex
|
|
|
|
import time
|
|
|
|
import signal
|
2021-03-02 23:16:28 +00:00
|
|
|
import pkgutil
|
2019-12-27 23:26:45 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from owrx.command import CommandMapper
|
2019-12-31 14:24:11 +00:00
|
|
|
from owrx.socket import getAvailablePort
|
2021-03-05 17:57:09 +00:00
|
|
|
from owrx.property import PropertyStack, PropertyLayer, PropertyFilter, PropertyCarousel, PropertyDeleted
|
2021-02-23 23:09:57 +00:00
|
|
|
from owrx.property.filter import ByLambda
|
2021-04-29 13:17:21 +00:00
|
|
|
from owrx.form.input import Input, TextInput, NumberInput, CheckboxInput, ModesInput, ExponentialInput
|
|
|
|
from owrx.form.input.converter import OptionalConverter
|
|
|
|
from owrx.form.input.device import GainInput, SchedulerInput, WaterfallLevelsInput
|
|
|
|
from owrx.form.input.validator import RequiredValidator
|
2021-04-29 13:28:18 +00:00
|
|
|
from owrx.form.section import OptionalSection
|
2021-04-17 15:42:08 +00:00
|
|
|
from owrx.feature import FeatureDetector
|
2021-02-19 14:29:17 +00:00
|
|
|
from typing import List
|
2021-03-20 16:23:35 +00:00
|
|
|
from enum import Enum
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-07-16 14:12:16 +00:00
|
|
|
from pycsdr.modules import TcpSource, Buffer
|
|
|
|
from pycsdr.types import Format
|
2020-12-19 15:28:18 +00:00
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
class SdrSourceState(Enum):
|
|
|
|
STOPPED = "Stopped"
|
|
|
|
STARTING = "Starting"
|
|
|
|
RUNNING = "Running"
|
|
|
|
STOPPING = "Stopping"
|
|
|
|
TUNING = "Tuning"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
|
|
|
|
class SdrBusyState(Enum):
|
2021-03-20 16:23:35 +00:00
|
|
|
IDLE = 1
|
|
|
|
BUSY = 2
|
2021-02-20 21:54:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SdrClientClass(Enum):
|
2021-03-20 16:23:35 +00:00
|
|
|
INACTIVE = 1
|
|
|
|
BACKGROUND = 2
|
|
|
|
USER = 3
|
2021-02-20 21:54:07 +00:00
|
|
|
|
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
class SdrSourceEventClient(object):
|
2021-02-20 21:54:07 +00:00
|
|
|
def onStateChange(self, state: SdrSourceState):
|
2020-08-30 21:47:04 +00:00
|
|
|
pass
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def onBusyStateChange(self, state: SdrBusyState):
|
2020-08-30 21:47:04 +00:00
|
|
|
pass
|
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
def onFail(self):
|
2020-08-30 21:47:04 +00:00
|
|
|
pass
|
|
|
|
|
2021-03-18 21:59:46 +00:00
|
|
|
def onShutdown(self):
|
|
|
|
pass
|
2020-08-30 21:47:04 +00:00
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
def onDisable(self):
|
|
|
|
pass
|
2020-08-30 21:47:04 +00:00
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
def onEnable(self):
|
|
|
|
pass
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def getClientClass(self) -> SdrClientClass:
|
|
|
|
return SdrClientClass.INACTIVE
|
2020-08-30 21:47:04 +00:00
|
|
|
|
|
|
|
|
2021-03-05 17:57:09 +00:00
|
|
|
class SdrProfileCarousel(PropertyCarousel):
|
|
|
|
def __init__(self, props):
|
|
|
|
super().__init__()
|
2021-03-05 18:44:45 +00:00
|
|
|
if "profiles" not in props:
|
|
|
|
return
|
|
|
|
|
2021-03-05 17:57:09 +00:00
|
|
|
for profile_id, profile in props["profiles"].items():
|
|
|
|
self.addLayer(profile_id, profile)
|
2021-03-05 18:28:54 +00:00
|
|
|
# activate first available profile
|
|
|
|
self.switch()
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-03-05 17:57:09 +00:00
|
|
|
props["profiles"].wire(self.handleProfileUpdate)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-03-05 17:57:09 +00:00
|
|
|
def addLayer(self, profile_id, profile):
|
|
|
|
profile_stack = PropertyStack()
|
|
|
|
profile_stack.addLayer(0, PropertyLayer(profile_id=profile_id).readonly())
|
|
|
|
profile_stack.addLayer(1, profile)
|
|
|
|
super().addLayer(profile_id, profile_stack)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-03-05 17:57:09 +00:00
|
|
|
def handleProfileUpdate(self, changes):
|
|
|
|
for profile_id, profile in changes.items():
|
|
|
|
if profile is PropertyDeleted:
|
|
|
|
self.removeLayer(profile_id)
|
2021-03-06 21:20:47 +00:00
|
|
|
else:
|
2021-03-05 17:57:09 +00:00
|
|
|
self.addLayer(profile_id, profile)
|
|
|
|
|
2021-03-05 18:09:51 +00:00
|
|
|
def _getDefaultLayer(self):
|
|
|
|
# return the first available profile, or the default empty layer if we don't have any
|
|
|
|
if self.layers:
|
|
|
|
return next(iter(self.layers.values()))
|
|
|
|
return super()._getDefaultLayer()
|
|
|
|
|
2021-03-05 17:57:09 +00:00
|
|
|
|
2019-12-27 23:26:45 +00:00
|
|
|
class SdrSource(ABC):
|
2019-12-31 14:24:11 +00:00
|
|
|
def __init__(self, id, props):
|
2019-12-21 19:58:28 +00:00
|
|
|
self.id = id
|
2020-03-24 21:13:42 +00:00
|
|
|
|
2020-04-01 22:10:28 +00:00
|
|
|
self.commandMapper = None
|
2021-07-16 14:12:16 +00:00
|
|
|
self.tcpSource = None
|
2020-12-25 19:27:30 +00:00
|
|
|
self.buffer = None
|
2020-04-01 22:10:28 +00:00
|
|
|
|
2020-03-24 21:13:42 +00:00
|
|
|
self.props = PropertyStack()
|
2021-02-28 23:26:56 +00:00
|
|
|
|
2020-03-24 21:13:42 +00:00
|
|
|
# layer 0 reserved for profile properties
|
2021-03-05 17:57:09 +00:00
|
|
|
self.profileCarousel = SdrProfileCarousel(props)
|
|
|
|
# prevent profile names from overriding the device name
|
2021-02-28 23:26:56 +00:00
|
|
|
self.props.addLayer(0, PropertyFilter(self.profileCarousel, ByLambda(lambda x: x != "name")))
|
2021-02-24 21:30:28 +00:00
|
|
|
|
|
|
|
# props from our device config
|
2020-03-24 21:13:42 +00:00
|
|
|
self.props.addLayer(1, props)
|
2021-02-24 21:30:28 +00:00
|
|
|
|
2021-01-30 15:04:13 +00:00
|
|
|
# the sdr_id is constant, so we put it in a separate layer
|
|
|
|
# this is used to detect device changes, that are then sent to the client
|
2021-03-05 17:57:09 +00:00
|
|
|
self.props.addLayer(2, PropertyLayer(sdr_id=id).readonly())
|
2021-02-24 21:30:28 +00:00
|
|
|
|
|
|
|
# finally, accept global config properties from the top-level config
|
2021-03-05 17:57:09 +00:00
|
|
|
self.props.addLayer(3, Config.get())
|
2021-02-24 21:30:28 +00:00
|
|
|
|
2020-03-24 21:52:17 +00:00
|
|
|
self.sdrProps = self.props.filter(*self.getEventNames())
|
2020-03-24 21:13:42 +00:00
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
self.wireEvents()
|
|
|
|
|
2020-11-23 14:26:01 +00:00
|
|
|
self.port = getAvailablePort()
|
2019-12-21 19:58:28 +00:00
|
|
|
self.monitor = None
|
|
|
|
self.clients = []
|
|
|
|
self.spectrumClients = []
|
|
|
|
self.spectrumThread = None
|
2020-08-30 15:35:53 +00:00
|
|
|
self.spectrumLock = threading.Lock()
|
2019-12-21 19:58:28 +00:00
|
|
|
self.process = None
|
|
|
|
self.modificationLock = threading.Lock()
|
2021-03-18 18:34:53 +00:00
|
|
|
self.state = SdrSourceState.STOPPED
|
|
|
|
self.enabled = "enabled" not in props or props["enabled"]
|
|
|
|
props.filter("enabled").wire(self._handleEnableChanged)
|
2019-12-21 19:58:28 +00:00
|
|
|
self.failed = False
|
2021-02-20 21:54:07 +00:00
|
|
|
self.busyState = SdrBusyState.IDLE
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-01-13 22:44:00 +00:00
|
|
|
self.validateProfiles()
|
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
if self.isAlwaysOn() and self.isEnabled():
|
2019-12-31 18:14:05 +00:00
|
|
|
self.start()
|
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
def isEnabled(self):
|
|
|
|
return self.enabled
|
|
|
|
|
|
|
|
def _handleEnableChanged(self, changes):
|
|
|
|
if "enabled" in changes and changes["enabled"] is not PropertyDeleted:
|
|
|
|
self.enabled = changes["enabled"]
|
|
|
|
else:
|
|
|
|
self.enabled = True
|
2021-03-18 18:59:10 +00:00
|
|
|
if not self.enabled:
|
|
|
|
self.stop()
|
2021-03-18 18:47:11 +00:00
|
|
|
for c in self.clients.copy():
|
2021-03-18 18:34:53 +00:00
|
|
|
if self.isEnabled():
|
|
|
|
c.onEnable()
|
|
|
|
else:
|
|
|
|
c.onDisable()
|
|
|
|
|
|
|
|
def isFailed(self):
|
|
|
|
return self.failed
|
|
|
|
|
|
|
|
def fail(self):
|
|
|
|
self.failed = True
|
2021-03-18 18:47:11 +00:00
|
|
|
for c in self.clients.copy():
|
2021-03-18 18:34:53 +00:00
|
|
|
c.onFail()
|
|
|
|
|
2021-01-13 22:44:00 +00:00
|
|
|
def validateProfiles(self):
|
|
|
|
props = PropertyStack()
|
|
|
|
props.addLayer(1, self.props)
|
|
|
|
for id, p in self.props["profiles"].items():
|
2021-02-23 22:23:37 +00:00
|
|
|
props.replaceLayer(0, p)
|
2021-01-13 22:44:00 +00:00
|
|
|
if "center_freq" not in props:
|
2021-01-20 16:01:46 +00:00
|
|
|
logger.warning('Profile "%s" does not specify a center_freq', id)
|
2021-01-13 22:44:00 +00:00
|
|
|
continue
|
|
|
|
if "samp_rate" not in props:
|
2021-01-20 16:01:46 +00:00
|
|
|
logger.warning('Profile "%s" does not specify a samp_rate', id)
|
2021-01-13 22:44:00 +00:00
|
|
|
continue
|
|
|
|
if "start_freq" in props:
|
|
|
|
start_freq = props["start_freq"]
|
|
|
|
srh = props["samp_rate"] / 2
|
|
|
|
center_freq = props["center_freq"]
|
|
|
|
if start_freq < center_freq - srh or start_freq > center_freq + srh:
|
2021-01-20 16:01:46 +00:00
|
|
|
logger.warning('start_freq for profile "%s" is out of range', id)
|
2021-01-13 22:44:00 +00:00
|
|
|
|
2019-12-31 18:14:05 +00:00
|
|
|
def isAlwaysOn(self):
|
|
|
|
return "always-on" in self.props and self.props["always-on"]
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
def getEventNames(self):
|
|
|
|
return [
|
|
|
|
"samp_rate",
|
|
|
|
"center_freq",
|
|
|
|
"ppm",
|
|
|
|
"rf_gain",
|
|
|
|
"lfo_offset",
|
2020-04-01 22:10:28 +00:00
|
|
|
] + list(self.getCommandMapper().keys())
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2019-12-27 23:26:45 +00:00
|
|
|
def getCommandMapper(self):
|
2019-12-31 18:14:05 +00:00
|
|
|
if self.commandMapper is None:
|
|
|
|
self.commandMapper = CommandMapper()
|
2019-12-27 23:26:45 +00:00
|
|
|
return self.commandMapper
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2019-12-27 23:26:45 +00:00
|
|
|
@abstractmethod
|
2020-12-30 16:18:46 +00:00
|
|
|
def onPropertyChange(self, changes):
|
2019-12-21 19:58:28 +00:00
|
|
|
pass
|
|
|
|
|
2019-12-27 23:26:45 +00:00
|
|
|
def wireEvents(self):
|
2020-03-24 21:52:17 +00:00
|
|
|
self.sdrProps.wire(self.onPropertyChange)
|
2019-12-27 23:26:45 +00:00
|
|
|
|
|
|
|
def getCommand(self):
|
|
|
|
return [self.getCommandMapper().map(self.getCommandValues())]
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-03-05 18:28:54 +00:00
|
|
|
def activateProfile(self, profile_id):
|
|
|
|
logger.debug("activating profile {0} for {1}".format(profile_id, self.getId()))
|
2021-02-28 23:26:56 +00:00
|
|
|
try:
|
|
|
|
self.profileCarousel.switch(profile_id)
|
|
|
|
except KeyError:
|
2021-03-05 18:28:54 +00:00
|
|
|
logger.warning("invalid profile %s for sdr %s. ignoring", profile_id, self.getId())
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
def getId(self):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
def getProfileId(self):
|
2021-03-05 18:28:54 +00:00
|
|
|
return self.props["profile_id"]
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
def getProfiles(self):
|
|
|
|
return self.props["profiles"]
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return self.props["name"]
|
|
|
|
|
|
|
|
def getProps(self):
|
|
|
|
return self.props
|
|
|
|
|
|
|
|
def getPort(self):
|
|
|
|
return self.port
|
|
|
|
|
2021-07-16 14:12:16 +00:00
|
|
|
def _getTcpSource(self):
|
2020-12-19 15:28:18 +00:00
|
|
|
with self.modificationLock:
|
2021-07-16 14:12:16 +00:00
|
|
|
if self.tcpSource is None:
|
2021-07-24 16:50:30 +00:00
|
|
|
self.tcpSource = TcpSource(self.port, Format.COMPLEX_FLOAT)
|
2021-07-16 14:12:16 +00:00
|
|
|
return self.tcpSource
|
2020-12-19 15:28:18 +00:00
|
|
|
|
2020-12-25 19:27:30 +00:00
|
|
|
def getBuffer(self):
|
|
|
|
if self.buffer is None:
|
2021-07-16 14:12:16 +00:00
|
|
|
self.buffer = Buffer(Format.COMPLEX_FLOAT)
|
2021-07-24 16:50:30 +00:00
|
|
|
self._getTcpSource().setWriter(self.buffer)
|
2020-12-25 19:27:30 +00:00
|
|
|
return self.buffer
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
def getCommandValues(self):
|
2020-03-24 21:52:17 +00:00
|
|
|
dict = self.sdrProps.__dict__()
|
2019-12-21 19:58:28 +00:00
|
|
|
if "lfo_offset" in dict and dict["lfo_offset"] is not None:
|
|
|
|
dict["tuner_freq"] = dict["center_freq"] + dict["lfo_offset"]
|
|
|
|
else:
|
|
|
|
dict["tuner_freq"] = dict["center_freq"]
|
|
|
|
return dict
|
|
|
|
|
|
|
|
def start(self):
|
2019-12-27 23:26:45 +00:00
|
|
|
with self.modificationLock:
|
|
|
|
if self.monitor:
|
2019-12-21 19:58:28 +00:00
|
|
|
return
|
|
|
|
|
2020-09-19 18:45:23 +00:00
|
|
|
if self.isFailed():
|
|
|
|
return
|
|
|
|
|
2019-12-31 15:20:36 +00:00
|
|
|
try:
|
|
|
|
self.preStart()
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Exception during preStart()")
|
|
|
|
|
2019-12-27 23:26:45 +00:00
|
|
|
cmd = self.getCommand()
|
|
|
|
cmd = [c for c in cmd if c is not None]
|
|
|
|
|
|
|
|
# don't use shell mode for commands without piping
|
|
|
|
if len(cmd) > 1:
|
|
|
|
# multiple commands with pipes
|
|
|
|
cmd = "|".join(cmd)
|
2020-01-17 20:18:02 +00:00
|
|
|
self.process = subprocess.Popen(cmd, shell=True, start_new_session=True)
|
2019-12-27 23:26:45 +00:00
|
|
|
else:
|
|
|
|
# single command
|
|
|
|
cmd = cmd[0]
|
2020-01-17 20:18:02 +00:00
|
|
|
# start_new_session can go as soon as there's no piped commands left
|
2019-12-27 23:26:45 +00:00
|
|
|
# the os.killpg call must be replaced with something more reasonable at the same time
|
2020-01-17 20:18:02 +00:00
|
|
|
self.process = subprocess.Popen(shlex.split(cmd), start_new_session=True)
|
2019-12-28 15:44:45 +00:00
|
|
|
logger.info("Started sdr source: " + cmd)
|
2019-12-27 23:26:45 +00:00
|
|
|
|
|
|
|
available = False
|
2021-02-21 17:11:08 +00:00
|
|
|
failed = False
|
2019-12-27 23:26:45 +00:00
|
|
|
|
|
|
|
def wait_for_process_to_end():
|
2021-02-21 17:11:08 +00:00
|
|
|
nonlocal failed
|
2019-12-27 23:26:45 +00:00
|
|
|
rc = self.process.wait()
|
|
|
|
logger.debug("shut down with RC={0}".format(rc))
|
2021-04-26 23:44:30 +00:00
|
|
|
self.process = None
|
2020-09-19 18:45:23 +00:00
|
|
|
self.monitor = None
|
2021-02-20 21:54:07 +00:00
|
|
|
if self.getState() is SdrSourceState.RUNNING:
|
2021-03-18 18:34:53 +00:00
|
|
|
self.fail()
|
2020-09-19 18:45:23 +00:00
|
|
|
else:
|
2021-04-26 23:44:30 +00:00
|
|
|
failed = True
|
2021-03-18 18:34:53 +00:00
|
|
|
self.setState(SdrSourceState.STOPPED)
|
2019-12-27 23:26:45 +00:00
|
|
|
|
2020-08-14 18:22:25 +00:00
|
|
|
self.monitor = threading.Thread(target=wait_for_process_to_end, name="source_monitor")
|
2019-12-27 23:26:45 +00:00
|
|
|
self.monitor.start()
|
|
|
|
|
|
|
|
retries = 1000
|
2021-02-21 17:11:08 +00:00
|
|
|
while retries > 0 and not failed:
|
2019-12-27 23:26:45 +00:00
|
|
|
retries -= 1
|
|
|
|
if self.monitor is None:
|
|
|
|
break
|
|
|
|
testsock = socket.socket()
|
2021-09-15 13:03:11 +00:00
|
|
|
testsock.settimeout(1)
|
2019-12-27 23:26:45 +00:00
|
|
|
try:
|
|
|
|
testsock.connect(("127.0.0.1", self.getPort()))
|
|
|
|
testsock.close()
|
|
|
|
available = True
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
if not available:
|
2021-02-21 17:11:08 +00:00
|
|
|
failed = True
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2019-12-27 23:26:45 +00:00
|
|
|
try:
|
|
|
|
self.postStart()
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Exception during postStart()")
|
2021-02-21 17:11:08 +00:00
|
|
|
failed = True
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
if failed:
|
|
|
|
self.fail()
|
|
|
|
else:
|
|
|
|
self.setState(SdrSourceState.RUNNING)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2019-12-31 15:20:36 +00:00
|
|
|
def preStart(self):
|
|
|
|
"""
|
|
|
|
override this method in subclasses if there's anything to be done before starting up the actual SDR
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
def postStart(self):
|
2019-12-31 15:20:36 +00:00
|
|
|
"""
|
|
|
|
override this method in subclasses if there's things to do after the actual SDR has started up
|
|
|
|
"""
|
2019-12-21 19:58:28 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def isAvailable(self):
|
|
|
|
return self.monitor is not None
|
|
|
|
|
|
|
|
def stop(self):
|
2019-12-27 23:26:45 +00:00
|
|
|
with self.modificationLock:
|
|
|
|
if self.process is not None:
|
2021-04-26 23:44:30 +00:00
|
|
|
self.setState(SdrSourceState.STOPPING)
|
2019-12-27 23:26:45 +00:00
|
|
|
try:
|
|
|
|
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
|
2021-09-15 13:03:11 +00:00
|
|
|
if self.monitor:
|
|
|
|
# wait 10 seconds for a regular shutdown
|
|
|
|
self.monitor.join(10)
|
|
|
|
# if the monitor is still running, the process still hasn't ended, so kill it
|
|
|
|
if self.monitor:
|
|
|
|
logger.warning("source has not shut down normally within 10 seconds, sending SIGKILL")
|
|
|
|
os.killpg(os.getpgid(self.process.pid), signal.SIGKILL)
|
2019-12-27 23:26:45 +00:00
|
|
|
except ProcessLookupError:
|
|
|
|
# been killed by something else, ignore
|
|
|
|
pass
|
|
|
|
if self.monitor:
|
|
|
|
self.monitor.join()
|
2021-07-16 14:12:16 +00:00
|
|
|
if self.tcpSource is not None:
|
|
|
|
self.tcpSource.stop()
|
|
|
|
self.tcpSource = None
|
2021-01-03 23:23:29 +00:00
|
|
|
self.buffer = None
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-03-18 21:59:46 +00:00
|
|
|
def shutdown(self):
|
|
|
|
self.stop()
|
|
|
|
for c in self.clients.copy():
|
|
|
|
c.onShutdown()
|
|
|
|
|
2021-02-27 19:48:37 +00:00
|
|
|
def getClients(self, *args):
|
|
|
|
if not args:
|
|
|
|
return self.clients
|
|
|
|
return [c for c in self.clients if c.getClientClass() in args]
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
def hasClients(self, *args):
|
2021-02-27 19:48:37 +00:00
|
|
|
return len(self.getClients(*args)) > 0
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2020-08-30 21:47:04 +00:00
|
|
|
def addClient(self, c: SdrSourceEventClient):
|
2021-02-25 21:19:05 +00:00
|
|
|
if c in self.clients:
|
|
|
|
return
|
2019-12-21 19:58:28 +00:00
|
|
|
self.clients.append(c)
|
2020-09-19 18:45:23 +00:00
|
|
|
c.onStateChange(self.getState())
|
2021-02-20 21:54:07 +00:00
|
|
|
hasUsers = self.hasClients(SdrClientClass.USER)
|
|
|
|
hasBackgroundTasks = self.hasClients(SdrClientClass.BACKGROUND)
|
2019-12-21 19:58:28 +00:00
|
|
|
if hasUsers or hasBackgroundTasks:
|
|
|
|
self.start()
|
2021-02-20 21:54:07 +00:00
|
|
|
self.setBusyState(SdrBusyState.BUSY if hasUsers else SdrBusyState.IDLE)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2020-08-30 21:47:04 +00:00
|
|
|
def removeClient(self, c: SdrSourceEventClient):
|
2021-02-26 00:12:03 +00:00
|
|
|
if c not in self.clients:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.clients.remove(c)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-02-26 21:36:15 +00:00
|
|
|
self.checkStatus()
|
|
|
|
|
|
|
|
def checkStatus(self):
|
2021-02-20 21:54:07 +00:00
|
|
|
hasUsers = self.hasClients(SdrClientClass.USER)
|
|
|
|
self.setBusyState(SdrBusyState.BUSY if hasUsers else SdrBusyState.IDLE)
|
2020-07-21 17:57:23 +00:00
|
|
|
|
2019-12-31 18:14:05 +00:00
|
|
|
# no need to check for users if we are always-on
|
|
|
|
if self.isAlwaysOn():
|
|
|
|
return
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
hasBackgroundTasks = self.hasClients(SdrClientClass.BACKGROUND)
|
2019-12-21 19:58:28 +00:00
|
|
|
if not hasUsers and not hasBackgroundTasks:
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
def addSpectrumClient(self, c):
|
2020-09-19 18:45:23 +00:00
|
|
|
if c in self.spectrumClients:
|
|
|
|
return
|
|
|
|
|
2020-08-30 15:35:53 +00:00
|
|
|
# local import due to circular depencency
|
|
|
|
from owrx.fft import SpectrumThread
|
2019-12-28 00:24:07 +00:00
|
|
|
|
2020-08-30 15:35:53 +00:00
|
|
|
self.spectrumClients.append(c)
|
|
|
|
with self.spectrumLock:
|
|
|
|
if self.spectrumThread is None:
|
|
|
|
self.spectrumThread = SpectrumThread(self)
|
|
|
|
self.spectrumThread.start()
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
def removeSpectrumClient(self, c):
|
|
|
|
try:
|
|
|
|
self.spectrumClients.remove(c)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-08-30 15:35:53 +00:00
|
|
|
with self.spectrumLock:
|
|
|
|
if not self.spectrumClients and self.spectrumThread is not None:
|
|
|
|
self.spectrumThread.stop()
|
|
|
|
self.spectrumThread = None
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
def writeSpectrumData(self, data):
|
|
|
|
for c in self.spectrumClients:
|
|
|
|
c.write_spectrum_data(data)
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def getState(self) -> SdrSourceState:
|
2019-12-23 20:12:28 +00:00
|
|
|
return self.state
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def setState(self, state: SdrSourceState):
|
2019-12-21 19:58:28 +00:00
|
|
|
if state == self.state:
|
|
|
|
return
|
|
|
|
self.state = state
|
2021-03-18 18:47:11 +00:00
|
|
|
for c in self.clients.copy():
|
2019-12-21 19:58:28 +00:00
|
|
|
c.onStateChange(state)
|
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def setBusyState(self, state: SdrBusyState):
|
2019-12-21 19:58:28 +00:00
|
|
|
if state == self.busyState:
|
|
|
|
return
|
|
|
|
self.busyState = state
|
2021-03-18 18:47:11 +00:00
|
|
|
for c in self.clients.copy():
|
2019-12-21 19:58:28 +00:00
|
|
|
c.onBusyStateChange(state)
|
2021-02-19 14:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SdrDeviceDescriptionMissing(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-02-22 22:49:28 +00:00
|
|
|
class SdrDeviceDescription(object):
|
2021-02-19 14:29:17 +00:00
|
|
|
@staticmethod
|
|
|
|
def getByType(sdr_type: str) -> "SdrDeviceDescription":
|
|
|
|
try:
|
|
|
|
className = "".join(x for x in sdr_type.title() if x.isalnum()) + "DeviceDescription"
|
|
|
|
module = __import__("owrx.source.{0}".format(sdr_type), fromlist=[className])
|
|
|
|
cls = getattr(module, className)
|
|
|
|
return cls()
|
|
|
|
except (ModuleNotFoundError, AttributeError):
|
|
|
|
raise SdrDeviceDescriptionMissing("Device description for type {} not available".format(sdr_type))
|
|
|
|
|
2021-03-02 23:16:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def getTypes():
|
2021-04-17 15:42:08 +00:00
|
|
|
def get_description(module_name):
|
2021-03-02 23:16:28 +00:00
|
|
|
try:
|
2021-04-17 15:42:08 +00:00
|
|
|
description = SdrDeviceDescription.getByType(module_name)
|
|
|
|
return description.getName()
|
2021-03-02 23:16:28 +00:00
|
|
|
except SdrDeviceDescriptionMissing:
|
2021-04-17 15:42:08 +00:00
|
|
|
return None
|
2021-03-24 21:46:51 +00:00
|
|
|
|
2021-04-17 15:42:08 +00:00
|
|
|
descriptions = {
|
|
|
|
module_name: get_description(module_name) for _, module_name, _ in pkgutil.walk_packages(__path__)
|
|
|
|
}
|
|
|
|
# filter out empty names and unavailable types
|
|
|
|
fd = FeatureDetector()
|
|
|
|
return {k: v for k, v in descriptions.items() if v is not None and fd.is_available(k)}
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
"""
|
|
|
|
must be overridden with a textual representation of the device, to be used for device type selection
|
|
|
|
|
|
|
|
:return: str
|
|
|
|
"""
|
|
|
|
return None
|
2021-03-02 23:16:28 +00:00
|
|
|
|
2021-02-28 20:26:55 +00:00
|
|
|
def getDeviceInputs(self) -> List[Input]:
|
2021-03-24 22:17:50 +00:00
|
|
|
keys = self.getDeviceMandatoryKeys() + self.getDeviceOptionalKeys()
|
|
|
|
return [TextInput("name", "Device name", validator=RequiredValidator())] + [
|
|
|
|
i for i in self.getInputs() if i.id in keys
|
|
|
|
]
|
2021-02-28 20:26:55 +00:00
|
|
|
|
|
|
|
def getProfileInputs(self) -> List[Input]:
|
2021-03-24 22:17:50 +00:00
|
|
|
keys = self.getProfileMandatoryKeys() + self.getProfileOptionalKeys()
|
|
|
|
return [TextInput("name", "Profile name", validator=RequiredValidator())] + [
|
|
|
|
i for i in self.getInputs() if i.id in keys
|
|
|
|
]
|
2021-02-28 20:26:55 +00:00
|
|
|
|
2021-02-19 14:29:17 +00:00
|
|
|
def getInputs(self) -> List[Input]:
|
|
|
|
return [
|
2021-02-21 23:57:02 +00:00
|
|
|
CheckboxInput("enabled", "Enable this device", converter=OptionalConverter(defaultFormValue=True)),
|
2021-02-23 19:02:38 +00:00
|
|
|
GainInput("rf_gain", "Device gain", self.hasAgc()),
|
2021-02-19 17:18:25 +00:00
|
|
|
NumberInput(
|
|
|
|
"ppm",
|
|
|
|
"Frequency correction",
|
|
|
|
append="ppm",
|
|
|
|
),
|
2021-02-19 15:29:30 +00:00
|
|
|
CheckboxInput(
|
|
|
|
"always-on",
|
2021-02-21 23:57:02 +00:00
|
|
|
"Keep device running at all times",
|
2021-02-19 17:18:25 +00:00
|
|
|
infotext="Prevents shutdown of the device when idle. Useful for devices with unreliable startup.",
|
|
|
|
),
|
|
|
|
CheckboxInput(
|
|
|
|
"services",
|
|
|
|
"Run background services on this device",
|
2021-02-19 15:29:30 +00:00
|
|
|
),
|
2021-02-27 22:14:41 +00:00
|
|
|
ExponentialInput(
|
2021-02-20 16:54:19 +00:00
|
|
|
"lfo_offset",
|
2021-02-28 20:04:43 +00:00
|
|
|
"Oscillator offset",
|
2021-02-27 22:14:41 +00:00
|
|
|
"Hz",
|
2021-02-20 16:54:19 +00:00
|
|
|
infotext="Use this when the actual receiving frequency differs from the frequency to be tuned on the"
|
|
|
|
+ " device. <br/> Formula: Center frequency + oscillator offset = sdr tune frequency",
|
|
|
|
),
|
2021-02-25 14:13:39 +00:00
|
|
|
WaterfallLevelsInput("waterfall_levels", "Waterfall levels"),
|
2021-02-24 16:12:23 +00:00
|
|
|
SchedulerInput("scheduler", "Scheduler"),
|
2021-02-27 22:14:41 +00:00
|
|
|
ExponentialInput("center_freq", "Center frequency", "Hz"),
|
|
|
|
ExponentialInput("samp_rate", "Sample rate", "S/s"),
|
|
|
|
ExponentialInput("start_freq", "Initial frequency", "Hz"),
|
2021-02-23 17:32:23 +00:00
|
|
|
ModesInput("start_mod", "Initial modulation"),
|
|
|
|
NumberInput("initial_squelch_level", "Initial squelch level", append="dBFS"),
|
2021-02-19 14:29:17 +00:00
|
|
|
]
|
|
|
|
|
2021-02-23 19:02:38 +00:00
|
|
|
def hasAgc(self):
|
|
|
|
# default is True since most devices have agc. override in subclasses if agc is not available
|
|
|
|
return True
|
|
|
|
|
2021-03-24 22:17:50 +00:00
|
|
|
def getDeviceMandatoryKeys(self):
|
2021-02-21 23:35:47 +00:00
|
|
|
return ["name", "enabled"]
|
|
|
|
|
2021-03-24 22:17:50 +00:00
|
|
|
def getDeviceOptionalKeys(self):
|
2021-02-24 16:12:23 +00:00
|
|
|
return [
|
|
|
|
"ppm",
|
|
|
|
"always-on",
|
|
|
|
"services",
|
|
|
|
"rf_gain",
|
|
|
|
"lfo_offset",
|
2021-02-25 14:13:39 +00:00
|
|
|
"waterfall_levels",
|
2021-02-24 16:12:23 +00:00
|
|
|
"scheduler",
|
|
|
|
]
|
2021-02-19 14:29:17 +00:00
|
|
|
|
2021-02-23 17:32:23 +00:00
|
|
|
def getProfileMandatoryKeys(self):
|
2021-02-28 20:26:55 +00:00
|
|
|
return ["name", "center_freq", "samp_rate", "start_freq", "start_mod"]
|
2021-02-23 17:32:23 +00:00
|
|
|
|
|
|
|
def getProfileOptionalKeys(self):
|
2021-02-25 14:13:39 +00:00
|
|
|
return ["initial_squelch_level", "rf_gain", "lfo_offset", "waterfall_levels"]
|
2021-02-23 17:32:23 +00:00
|
|
|
|
|
|
|
def getDeviceSection(self):
|
2021-02-28 20:26:55 +00:00
|
|
|
return OptionalSection(
|
2021-03-24 22:17:50 +00:00
|
|
|
"Device settings", self.getDeviceInputs(), self.getDeviceMandatoryKeys(), self.getDeviceOptionalKeys()
|
2021-02-28 20:26:55 +00:00
|
|
|
)
|
2021-02-23 17:32:23 +00:00
|
|
|
|
|
|
|
def getProfileSection(self):
|
|
|
|
return OptionalSection(
|
|
|
|
"Profile settings",
|
2021-02-28 20:26:55 +00:00
|
|
|
self.getProfileInputs(),
|
2021-02-23 17:32:23 +00:00
|
|
|
self.getProfileMandatoryKeys(),
|
|
|
|
self.getProfileOptionalKeys(),
|
|
|
|
)
|