openwebrx-clone/owrx/source/__init__.py

519 lines
17 KiB
Python
Raw Normal View History

from owrx.config import Config
import threading
import subprocess
import os
import socket
import shlex
import time
import signal
2019-12-27 23:26:45 +00:00
from abc import ABC, abstractmethod
from owrx.command import CommandMapper
from owrx.socket import getAvailablePort
2020-03-24 21:13:42 +00:00
from owrx.property import PropertyStack, PropertyLayer
2021-02-23 17:32:23 +00:00
from owrx.form import Input, TextInput, NumberInput, CheckboxInput, ModesInput
from owrx.form.converter import OptionalConverter
2021-02-19 20:07:13 +00:00
from owrx.form.device import GainInput
2021-02-19 14:29:17 +00:00
from owrx.controllers.settings import Section
from typing import List
2021-02-20 21:54:07 +00:00
from enum import Enum, auto
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"
FAILED = "Failed"
2021-02-21 17:11:08 +00:00
DISABLED = "Disabled"
2021-02-20 21:54:07 +00:00
def __str__(self):
return self.value
class SdrBusyState(Enum):
IDLE = auto()
BUSY = auto()
class SdrClientClass(Enum):
INACTIVE = auto()
BACKGROUND = auto()
USER = auto()
class SdrSourceEventClient(ABC):
@abstractmethod
2021-02-20 21:54:07 +00:00
def onStateChange(self, state: SdrSourceState):
pass
@abstractmethod
2021-02-20 21:54:07 +00:00
def onBusyStateChange(self, state: SdrBusyState):
pass
2021-02-20 21:54:07 +00:00
def getClientClass(self) -> SdrClientClass:
return SdrClientClass.INACTIVE
2019-12-27 23:26:45 +00:00
class SdrSource(ABC):
def __init__(self, id, props):
self.id = id
2020-03-24 21:13:42 +00:00
self.commandMapper = None
2020-03-24 21:13:42 +00:00
self.props = PropertyStack()
# layer 0 reserved for profile properties
self.props.addLayer(1, props)
# 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
sdrIdLayer = PropertyLayer()
sdrIdLayer["sdr_id"] = id
self.props.addLayer(2, sdrIdLayer.readonly())
self.props.addLayer(3, Config.get())
2020-03-24 21:52:17 +00:00
self.sdrProps = self.props.filter(*self.getEventNames())
2020-03-24 21:13:42 +00:00
self.profile_id = None
self.activateProfile()
self.wireEvents()
self.port = getAvailablePort()
self.monitor = None
self.clients = []
self.spectrumClients = []
self.spectrumThread = None
self.spectrumLock = threading.Lock()
self.process = None
self.modificationLock = threading.Lock()
2021-02-21 17:11:08 +00:00
self.state = SdrSourceState.STOPPED if "enabled" not in props or props["enabled"] else SdrSourceState.DISABLED
2021-02-20 21:54:07 +00:00
self.busyState = SdrBusyState.IDLE
2021-01-13 22:44:00 +00:00
self.validateProfiles()
2021-02-21 17:11:08 +00:00
if self.isAlwaysOn() and self.state is not SdrSourceState.DISABLED:
2019-12-31 18:14:05 +00:00
self.start()
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():
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"]
def getEventNames(self):
return [
"samp_rate",
"center_freq",
"ppm",
"rf_gain",
"lfo_offset",
] + list(self.getCommandMapper().keys())
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-27 23:26:45 +00:00
@abstractmethod
2020-12-30 16:18:46 +00:00
def onPropertyChange(self, changes):
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())]
def activateProfile(self, profile_id=None):
profiles = self.props["profiles"]
if profile_id is None:
profile_id = list(profiles.keys())[0]
2019-12-23 20:12:28 +00:00
if profile_id not in profiles:
logger.warning("invalid profile %s for sdr %s. ignoring", profile_id, self.id)
return
if profile_id == self.profile_id:
return
logger.debug("activating profile {0}".format(profile_id))
self.props["profile_id"] = profile_id
2020-03-24 21:13:42 +00:00
profile = profiles[profile_id]
self.profile_id = profile_id
self.props.replaceLayer(0, profile)
def getId(self):
return self.id
def getProfileId(self):
return self.profile_id
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
def getCommandValues(self):
2020-03-24 21:52:17 +00:00
dict = self.sdrProps.__dict__()
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:
return
2021-02-21 17:11:08 +00:00
if self.getState() is SdrSourceState.FAILED:
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)
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]
# 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
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))
self.monitor = None
2021-02-20 21:54:07 +00:00
if self.getState() is SdrSourceState.RUNNING:
2021-02-21 17:11:08 +00:00
failed = True
2021-02-20 21:54:07 +00:00
self.setState(SdrSourceState.FAILED)
else:
2021-02-20 21:54:07 +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()
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-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
2021-02-21 17:11:08 +00:00
self.setState(SdrSourceState.FAILED if failed else SdrSourceState.RUNNING)
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
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
"""
pass
def isAvailable(self):
return self.monitor is not None
def stop(self):
2021-02-21 17:11:08 +00:00
# don't overwrite failed flag
# TODO introduce a better solution?
if self.getState() is not SdrSourceState.FAILED:
self.setState(SdrSourceState.STOPPING)
2019-12-27 23:26:45 +00:00
with self.modificationLock:
2019-12-27 23:26:45 +00:00
if self.process is not None:
try:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
except ProcessLookupError:
# been killed by something else, ignore
pass
if self.monitor:
self.monitor.join()
def hasClients(self, *args):
clients = [c for c in self.clients if c.getClientClass() in args]
return len(clients) > 0
def addClient(self, c: SdrSourceEventClient):
self.clients.append(c)
c.onStateChange(self.getState())
2021-02-20 21:54:07 +00:00
hasUsers = self.hasClients(SdrClientClass.USER)
hasBackgroundTasks = self.hasClients(SdrClientClass.BACKGROUND)
if hasUsers or hasBackgroundTasks:
self.start()
2021-02-20 21:54:07 +00:00
self.setBusyState(SdrBusyState.BUSY if hasUsers else SdrBusyState.IDLE)
def removeClient(self, c: SdrSourceEventClient):
try:
self.clients.remove(c)
except ValueError:
pass
2021-02-20 21:54:07 +00:00
hasUsers = self.hasClients(SdrClientClass.USER)
self.setBusyState(SdrBusyState.BUSY if hasUsers else SdrBusyState.IDLE)
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)
if not hasUsers and not hasBackgroundTasks:
self.stop()
def addSpectrumClient(self, c):
if c in self.spectrumClients:
return
# local import due to circular depencency
from owrx.fft import SpectrumThread
2019-12-28 00:24:07 +00:00
self.spectrumClients.append(c)
with self.spectrumLock:
if self.spectrumThread is None:
self.spectrumThread = SpectrumThread(self)
self.spectrumThread.start()
def removeSpectrumClient(self, c):
try:
self.spectrumClients.remove(c)
except ValueError:
pass
with self.spectrumLock:
if not self.spectrumClients and self.spectrumThread is not None:
self.spectrumThread.stop()
self.spectrumThread = None
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):
if state == self.state:
return
self.state = state
for c in self.clients:
c.onStateChange(state)
2021-02-20 21:54:07 +00:00
def setBusyState(self, state: SdrBusyState):
if state == self.busyState:
return
self.busyState = state
for c in self.clients:
c.onBusyStateChange(state)
2021-02-19 14:29:17 +00:00
class SdrDeviceDescriptionMissing(Exception):
pass
2021-02-22 22:49:28 +00:00
class OptionalSection(Section):
def __init__(self, title, inputs: List[Input], mandatory, optional):
super().__init__(title, *inputs)
self.mandatory = mandatory
self.optional = optional
self.optional_inputs = []
def classes(self):
classes = super().classes()
classes.append("optional-section")
return classes
def _is_optional(self, input):
return input.id in self.optional
def render_optional_select(self):
return """
<hr class="row" />
2021-02-22 22:49:28 +00:00
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3">
Additional optional settings
</label>
<div class="input-group input-group-sm col-9 p-0">
<select class="form-control from-control-sm optional-select">
{options}
</select>
<div class="input-group-append">
<button class="btn btn-success option-add-button">Add</button>
</div>
</div>
</div>
""".format(
options="".join(
"""
<option value="{value}">{name}</option>
""".format(
value=input.id,
name=input.getLabel(),
)
for input in self.optional_inputs
)
)
def render_optional_inputs(self, data):
return """
<div class="optional-inputs" style="display: none;">
{inputs}
</div>
""".format(
inputs="".join(self.render_input(input, data) for input in self.optional_inputs)
)
def render_inputs(self, data):
return super().render_inputs(data) + self.render_optional_select() + self.render_optional_inputs(data)
def render(self, data):
indexed_inputs = {input.id: input for input in self.inputs}
visible_keys = set(self.mandatory + [k for k in self.optional if k in data])
optional_keys = set(k for k in self.optional if k not in data)
self.inputs = [input for k, input in indexed_inputs.items() if k in visible_keys]
for input in self.inputs:
if self._is_optional(input):
input.setRemovable()
self.optional_inputs = [input for k, input in indexed_inputs.items() if k in optional_keys]
for input in self.optional_inputs:
input.setRemovable()
input.setDisabled()
return super().render(data)
def parse(self, data):
data = super().parse(data)
# remove optional keys if they have been removed from the form
for k in self.optional:
if k not in data:
data[k] = None
return data
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))
def getInputs(self) -> List[Input]:
return [
TextInput("name", "Device name"),
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",
"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-20 16:54:19 +00:00
NumberInput(
"lfo_offset",
"Oscilator offset",
append="Hz",
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",
),
NumberInput("waterfall_min_level", "Lowest waterfall level", append="dBFS"),
NumberInput("waterfall_max_level", "Highest waterfall level", append="dBFS"),
2021-02-20 16:54:19 +00:00
# TODO `schedule`
2021-02-23 17:32:23 +00:00
NumberInput("center_freq", "Center frequency", append="Hz"),
NumberInput("samp_rate", "Sample rate", append="S/s"),
NumberInput("start_freq", "Initial frequency", append="Hz"),
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
def getMandatoryKeys(self):
return ["name", "enabled"]
def getOptionalKeys(self):
return ["ppm", "always-on", "services", "rf_gain", "lfo_offset", "waterfall_min_level", "waterfall_max_level"]
2021-02-19 14:29:17 +00:00
2021-02-23 17:32:23 +00:00
def getProfileMandatoryKeys(self):
return ["center_freq", "samp_rate", "start_freq", "start_mod"]
def getProfileOptionalKeys(self):
return ["initial_squelch_level", "rf_gain", "lfo_offset", "waterfall_min_level", "waterfall_max_level"]
def getDeviceSection(self):
2021-02-22 22:49:28 +00:00
return OptionalSection("Device settings", self.getInputs(), self.getMandatoryKeys(), self.getOptionalKeys())
2021-02-23 17:32:23 +00:00
def getProfileSection(self):
return OptionalSection(
"Profile settings",
self.getInputs(),
self.getProfileMandatoryKeys(),
self.getProfileOptionalKeys(),
)