2019-05-04 18:26:11 +00:00
|
|
|
import subprocess
|
2019-05-12 13:56:18 +00:00
|
|
|
from owrx.config import PropertyManager
|
|
|
|
from owrx.feature import FeatureDetector, UnknownFeatureException
|
2019-05-30 14:12:13 +00:00
|
|
|
from owrx.meta import MetaParser
|
2019-07-07 12:10:03 +00:00
|
|
|
from owrx.wsjt import WsjtParser
|
2019-08-15 13:45:15 +00:00
|
|
|
from owrx.aprs import AprsParser
|
2019-09-18 13:40:23 +00:00
|
|
|
from owrx.metrics import Metrics, DirectMetric
|
2019-05-04 18:26:11 +00:00
|
|
|
import threading
|
|
|
|
import csdr
|
2019-05-05 15:34:40 +00:00
|
|
|
import time
|
2019-05-07 15:30:30 +00:00
|
|
|
import os
|
|
|
|
import signal
|
2019-05-10 12:23:54 +00:00
|
|
|
import sys
|
2019-05-10 16:30:53 +00:00
|
|
|
import socket
|
2019-05-10 19:50:58 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-05-04 18:26:11 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
class SdrService(object):
|
|
|
|
sdrProps = None
|
|
|
|
sources = {}
|
|
|
|
lastPort = None
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
@staticmethod
|
|
|
|
def getNextPort():
|
|
|
|
pm = PropertyManager.getSharedInstance()
|
|
|
|
(start, end) = pm["iq_port_range"]
|
|
|
|
if SdrService.lastPort is None:
|
|
|
|
SdrService.lastPort = start
|
|
|
|
else:
|
|
|
|
SdrService.lastPort += 1
|
|
|
|
if SdrService.lastPort > end:
|
|
|
|
raise IndexError("no more available ports to start more sdrs")
|
|
|
|
return SdrService.lastPort
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
@staticmethod
|
2019-05-10 12:23:54 +00:00
|
|
|
def loadProps():
|
2019-05-09 20:44:29 +00:00
|
|
|
if SdrService.sdrProps is None:
|
|
|
|
pm = PropertyManager.getSharedInstance()
|
2019-05-10 12:23:54 +00:00
|
|
|
featureDetector = FeatureDetector()
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
def loadIntoPropertyManager(dict: dict):
|
|
|
|
propertyManager = PropertyManager()
|
|
|
|
for (name, value) in dict.items():
|
|
|
|
propertyManager[name] = value
|
|
|
|
return propertyManager
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 12:23:54 +00:00
|
|
|
def sdrTypeAvailable(value):
|
|
|
|
try:
|
|
|
|
if not featureDetector.is_available(value["type"]):
|
2019-07-21 17:40:28 +00:00
|
|
|
logger.error(
|
|
|
|
'The RTL source type "{0}" is not available. please check requirements.'.format(
|
|
|
|
value["type"]
|
|
|
|
)
|
|
|
|
)
|
2019-05-10 12:23:54 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
except UnknownFeatureException:
|
2019-07-21 17:40:28 +00:00
|
|
|
logger.error(
|
|
|
|
'The RTL source type "{0}" is invalid. Please check your configuration'.format(value["type"])
|
|
|
|
)
|
2019-05-10 12:23:54 +00:00
|
|
|
return False
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 12:23:54 +00:00
|
|
|
# transform all dictionary items into PropertyManager object, filtering out unavailable ones
|
|
|
|
SdrService.sdrProps = {
|
|
|
|
name: loadIntoPropertyManager(value) for (name, value) in pm["sdrs"].items() if sdrTypeAvailable(value)
|
|
|
|
}
|
2019-07-21 17:40:28 +00:00
|
|
|
logger.info(
|
|
|
|
"SDR sources loaded. Availables SDRs: {0}".format(
|
|
|
|
", ".join(map(lambda x: x["name"], SdrService.sdrProps.values()))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-05-10 12:23:54 +00:00
|
|
|
@staticmethod
|
2019-07-21 17:40:28 +00:00
|
|
|
def getSource(id=None):
|
2019-05-10 12:23:54 +00:00
|
|
|
SdrService.loadProps()
|
2019-05-09 20:44:29 +00:00
|
|
|
if id is None:
|
|
|
|
# TODO: configure default sdr in config? right now it will pick the first one off the list.
|
|
|
|
id = list(SdrService.sdrProps.keys())[0]
|
2019-05-10 14:14:16 +00:00
|
|
|
sources = SdrService.getSources()
|
|
|
|
return sources[id]
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 14:14:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def getSources():
|
|
|
|
SdrService.loadProps()
|
|
|
|
for id in SdrService.sdrProps.keys():
|
|
|
|
if not id in SdrService.sources:
|
|
|
|
props = SdrService.sdrProps[id]
|
2019-07-21 17:40:28 +00:00
|
|
|
className = "".join(x for x in props["type"].title() if x.isalnum()) + "Source"
|
2019-05-10 14:14:16 +00:00
|
|
|
cls = getattr(sys.modules[__name__], className)
|
|
|
|
SdrService.sources[id] = cls(props, SdrService.getNextPort())
|
|
|
|
return SdrService.sources
|
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
|
2019-07-21 20:13:20 +00:00
|
|
|
class SdrSourceException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
class SdrSource(object):
|
|
|
|
def __init__(self, props, port):
|
|
|
|
self.props = props
|
2019-05-10 14:14:16 +00:00
|
|
|
self.activateProfile()
|
2019-05-09 20:44:29 +00:00
|
|
|
self.rtlProps = self.props.collect(
|
2019-05-19 20:10:11 +00:00
|
|
|
"samp_rate", "nmux_memory", "center_freq", "ppm", "rf_gain", "lna_gain", "rf_amp", "antenna", "if_gain"
|
2019-05-09 20:44:29 +00:00
|
|
|
).defaults(PropertyManager.getSharedInstance())
|
2019-05-07 15:09:29 +00:00
|
|
|
|
|
|
|
def restart(name, value):
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.debug("restarting sdr source due to property change: {0} changed to {1}".format(name, value))
|
2019-05-07 15:30:30 +00:00
|
|
|
self.stop()
|
|
|
|
self.start()
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
self.rtlProps.wire(restart)
|
|
|
|
self.port = port
|
|
|
|
self.monitor = None
|
2019-05-10 12:58:25 +00:00
|
|
|
self.clients = []
|
2019-05-10 18:59:06 +00:00
|
|
|
self.spectrumClients = []
|
2019-05-10 12:58:25 +00:00
|
|
|
self.spectrumThread = None
|
2019-05-10 20:08:18 +00:00
|
|
|
self.process = None
|
2019-05-10 12:58:25 +00:00
|
|
|
self.modificationLock = threading.Lock()
|
2019-05-07 15:09:29 +00:00
|
|
|
|
2019-05-19 11:36:05 +00:00
|
|
|
# override this in subclasses
|
|
|
|
def getCommand(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# override this in subclasses, if necessary
|
|
|
|
def getFormatConversion(self):
|
|
|
|
return None
|
2019-05-10 12:23:54 +00:00
|
|
|
|
2019-07-21 20:12:41 +00:00
|
|
|
def activateProfile(self, profile_id=None):
|
2019-05-10 14:14:16 +00:00
|
|
|
profiles = self.props["profiles"]
|
2019-07-21 20:12:41 +00:00
|
|
|
if profile_id is None:
|
|
|
|
profile_id = list(profiles.keys())[0]
|
|
|
|
logger.debug("activating profile {0}".format(profile_id))
|
|
|
|
profile = profiles[profile_id]
|
2019-05-10 14:14:16 +00:00
|
|
|
for (key, value) in profile.items():
|
|
|
|
# skip the name, that would overwrite the source name.
|
2019-07-21 17:40:28 +00:00
|
|
|
if key == "name":
|
|
|
|
continue
|
2019-05-10 14:14:16 +00:00
|
|
|
self.props[key] = value
|
|
|
|
|
|
|
|
def getProfiles(self):
|
|
|
|
return self.props["profiles"]
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return self.props["name"]
|
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
def getProps(self):
|
|
|
|
return self.props
|
|
|
|
|
|
|
|
def getPort(self):
|
|
|
|
return self.port
|
2019-05-07 15:09:29 +00:00
|
|
|
|
2019-05-07 15:30:30 +00:00
|
|
|
def start(self):
|
2019-05-10 12:58:25 +00:00
|
|
|
self.modificationLock.acquire()
|
|
|
|
if self.monitor:
|
|
|
|
self.modificationLock.release()
|
|
|
|
return
|
2019-05-07 15:30:30 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
props = self.rtlProps
|
2019-05-07 15:30:30 +00:00
|
|
|
|
2019-05-19 11:36:05 +00:00
|
|
|
start_sdr_command = self.getCommand().format(
|
2019-07-21 17:40:28 +00:00
|
|
|
**props.collect(
|
|
|
|
"samp_rate", "center_freq", "ppm", "rf_gain", "lna_gain", "rf_amp", "antenna", "if_gain"
|
|
|
|
).__dict__()
|
2019-05-07 15:09:29 +00:00
|
|
|
)
|
|
|
|
|
2019-05-19 11:36:05 +00:00
|
|
|
format_conversion = self.getFormatConversion()
|
|
|
|
if format_conversion is not None:
|
|
|
|
start_sdr_command += " | " + format_conversion
|
2019-05-04 18:26:11 +00:00
|
|
|
|
|
|
|
nmux_bufcnt = nmux_bufsize = 0
|
2019-07-21 17:40:28 +00:00
|
|
|
while nmux_bufsize < props["samp_rate"] / 4:
|
|
|
|
nmux_bufsize += 4096
|
|
|
|
while nmux_bufsize * nmux_bufcnt < props["nmux_memory"] * 1e6:
|
|
|
|
nmux_bufcnt += 1
|
2019-05-04 18:26:11 +00:00
|
|
|
if nmux_bufcnt == 0 or nmux_bufsize == 0:
|
2019-07-21 17:40:28 +00:00
|
|
|
logger.error(
|
|
|
|
"Error: nmux_bufsize or nmux_bufcnt is zero. These depend on nmux_memory and samp_rate options in config_webrx.py"
|
|
|
|
)
|
2019-05-10 12:58:25 +00:00
|
|
|
self.modificationLock.release()
|
2019-05-04 18:26:11 +00:00
|
|
|
return
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.debug("nmux_bufsize = %d, nmux_bufcnt = %d" % (nmux_bufsize, nmux_bufcnt))
|
2019-07-21 17:40:28 +00:00
|
|
|
cmd = start_sdr_command + " | nmux --bufsize %d --bufcnt %d --port %d --address 127.0.0.1" % (
|
|
|
|
nmux_bufsize,
|
|
|
|
nmux_bufcnt,
|
|
|
|
self.port,
|
|
|
|
)
|
2019-05-07 15:30:30 +00:00
|
|
|
self.process = subprocess.Popen(cmd, shell=True, preexec_fn=os.setpgrp)
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.info("Started rtl source: " + cmd)
|
2019-05-07 15:30:30 +00:00
|
|
|
|
2019-07-21 20:13:20 +00:00
|
|
|
available = False
|
|
|
|
|
2019-05-15 09:44:03 +00:00
|
|
|
def wait_for_process_to_end():
|
|
|
|
rc = self.process.wait()
|
|
|
|
logger.debug("shut down with RC={0}".format(rc))
|
|
|
|
self.monitor = None
|
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
self.monitor = threading.Thread(target=wait_for_process_to_end)
|
2019-05-15 09:44:03 +00:00
|
|
|
self.monitor.start()
|
|
|
|
|
2019-07-23 19:28:51 +00:00
|
|
|
retries = 1000
|
2019-07-21 20:13:20 +00:00
|
|
|
while retries > 0:
|
|
|
|
retries -= 1
|
|
|
|
if self.monitor is None:
|
|
|
|
break
|
2019-05-10 16:30:53 +00:00
|
|
|
testsock = socket.socket()
|
|
|
|
try:
|
|
|
|
testsock.connect(("127.0.0.1", self.getPort()))
|
|
|
|
testsock.close()
|
2019-07-21 20:13:20 +00:00
|
|
|
available = True
|
2019-05-10 16:30:53 +00:00
|
|
|
break
|
|
|
|
except:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2019-05-10 12:58:25 +00:00
|
|
|
self.modificationLock.release()
|
|
|
|
|
2019-07-21 20:13:20 +00:00
|
|
|
if not available:
|
|
|
|
raise SdrSourceException("rtl source failed to start up")
|
|
|
|
|
2019-05-10 16:30:53 +00:00
|
|
|
for c in self.clients:
|
|
|
|
c.onSdrAvailable()
|
|
|
|
|
|
|
|
def isAvailable(self):
|
|
|
|
return self.monitor is not None
|
|
|
|
|
2019-05-07 15:30:30 +00:00
|
|
|
def stop(self):
|
2019-05-10 16:30:53 +00:00
|
|
|
for c in self.clients:
|
|
|
|
c.onSdrUnavailable()
|
|
|
|
|
2019-05-10 12:58:25 +00:00
|
|
|
self.modificationLock.acquire()
|
2019-05-10 18:59:06 +00:00
|
|
|
|
2019-05-10 20:08:18 +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
|
2019-05-10 18:59:06 +00:00
|
|
|
if self.monitor:
|
|
|
|
self.monitor.join()
|
2019-05-10 12:23:54 +00:00
|
|
|
self.sleepOnRestart()
|
2019-05-10 12:58:25 +00:00
|
|
|
self.modificationLock.release()
|
2019-05-10 12:23:54 +00:00
|
|
|
|
|
|
|
def sleepOnRestart(self):
|
|
|
|
pass
|
|
|
|
|
2019-09-15 22:31:35 +00:00
|
|
|
def hasActiveClients(self):
|
|
|
|
activeClients = [c for c in self.clients if c.isActive()]
|
|
|
|
return len(activeClients) > 0
|
|
|
|
|
2019-05-10 12:58:25 +00:00
|
|
|
def addClient(self, c):
|
|
|
|
self.clients.append(c)
|
2019-09-15 22:31:35 +00:00
|
|
|
if self.hasActiveClients():
|
|
|
|
self.start()
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 12:58:25 +00:00
|
|
|
def removeClient(self, c):
|
|
|
|
try:
|
|
|
|
self.clients.remove(c)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-09-15 22:31:35 +00:00
|
|
|
if not self.hasActiveClients():
|
2019-05-10 12:58:25 +00:00
|
|
|
self.stop()
|
|
|
|
|
2019-05-10 18:59:06 +00:00
|
|
|
def addSpectrumClient(self, c):
|
|
|
|
self.spectrumClients.append(c)
|
2019-05-14 21:30:03 +00:00
|
|
|
if self.spectrumThread is None:
|
|
|
|
self.spectrumThread = SpectrumThread(self)
|
|
|
|
self.spectrumThread.start()
|
2019-05-10 18:59:06 +00:00
|
|
|
|
|
|
|
def removeSpectrumClient(self, c):
|
|
|
|
try:
|
|
|
|
self.spectrumClients.remove(c)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-05-14 21:30:03 +00:00
|
|
|
if not self.spectrumClients and self.spectrumThread is not None:
|
|
|
|
self.spectrumThread.stop()
|
|
|
|
self.spectrumThread = None
|
2019-05-10 18:59:06 +00:00
|
|
|
|
|
|
|
def writeSpectrumData(self, data):
|
|
|
|
for c in self.spectrumClients:
|
|
|
|
c.write_spectrum_data(data)
|
|
|
|
|
|
|
|
|
2019-09-10 22:30:14 +00:00
|
|
|
class Resampler(SdrSource):
|
|
|
|
def __init__(self, props, port, sdr):
|
|
|
|
sdrProps = sdr.getProps()
|
|
|
|
self.shift = (sdrProps["center_freq"] - props["center_freq"]) / sdrProps["samp_rate"]
|
|
|
|
self.decimation = int(float(sdrProps["samp_rate"]) / props["samp_rate"])
|
|
|
|
if_samp_rate = sdrProps["samp_rate"] / self.decimation
|
|
|
|
self.transition_bw = 0.15 * (if_samp_rate / float(sdrProps["samp_rate"]))
|
|
|
|
props["samp_rate"] = if_samp_rate
|
|
|
|
|
|
|
|
self.sdr = sdr
|
|
|
|
super().__init__(props, port)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.modificationLock.acquire()
|
|
|
|
if self.monitor:
|
|
|
|
self.modificationLock.release()
|
|
|
|
return
|
|
|
|
|
|
|
|
props = self.rtlProps
|
|
|
|
|
|
|
|
resampler_command = [
|
|
|
|
"nc -v 127.0.0.1 {nc_port}".format(nc_port=self.sdr.getPort()),
|
|
|
|
"csdr shift_addition_cc {shift}".format(shift=self.shift),
|
|
|
|
"csdr fir_decimate_cc {decimation} {ddc_transition_bw} HAMMING".format(
|
2019-09-13 21:03:05 +00:00
|
|
|
decimation=self.decimation, ddc_transition_bw=self.transition_bw
|
2019-09-10 22:30:14 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
nmux_bufcnt = nmux_bufsize = 0
|
|
|
|
while nmux_bufsize < props["samp_rate"] / 4:
|
|
|
|
nmux_bufsize += 4096
|
|
|
|
while nmux_bufsize * nmux_bufcnt < props["nmux_memory"] * 1e6:
|
|
|
|
nmux_bufcnt += 1
|
|
|
|
if nmux_bufcnt == 0 or nmux_bufsize == 0:
|
|
|
|
logger.error(
|
|
|
|
"Error: nmux_bufsize or nmux_bufcnt is zero. These depend on nmux_memory and samp_rate options in config_webrx.py"
|
|
|
|
)
|
|
|
|
self.modificationLock.release()
|
|
|
|
return
|
|
|
|
logger.debug("nmux_bufsize = %d, nmux_bufcnt = %d" % (nmux_bufsize, nmux_bufcnt))
|
2019-09-13 21:03:05 +00:00
|
|
|
resampler_command += [
|
|
|
|
"nmux --bufsize %d --bufcnt %d --port %d --address 127.0.0.1" % (nmux_bufsize, nmux_bufcnt, self.port)
|
|
|
|
]
|
2019-09-10 22:30:14 +00:00
|
|
|
cmd = " | ".join(resampler_command)
|
|
|
|
logger.debug("resampler command: %s", cmd)
|
|
|
|
self.process = subprocess.Popen(cmd, shell=True, preexec_fn=os.setpgrp)
|
|
|
|
logger.info("Started resampler source: " + cmd)
|
|
|
|
|
|
|
|
available = False
|
|
|
|
|
|
|
|
def wait_for_process_to_end():
|
|
|
|
rc = self.process.wait()
|
|
|
|
logger.debug("shut down with RC={0}".format(rc))
|
|
|
|
self.monitor = None
|
|
|
|
|
|
|
|
self.monitor = threading.Thread(target=wait_for_process_to_end)
|
|
|
|
self.monitor.start()
|
|
|
|
|
|
|
|
retries = 1000
|
|
|
|
while retries > 0:
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.modificationLock.release()
|
|
|
|
|
|
|
|
if not available:
|
|
|
|
raise SdrSourceException("resampler source failed to start up")
|
|
|
|
|
|
|
|
for c in self.clients:
|
|
|
|
c.onSdrAvailable()
|
|
|
|
|
|
|
|
def activateProfile(self, profile_id=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-05-10 12:23:54 +00:00
|
|
|
class RtlSdrSource(SdrSource):
|
2019-05-19 11:36:05 +00:00
|
|
|
def getCommand(self):
|
|
|
|
return "rtl_sdr -s {samp_rate} -f {center_freq} -p {ppm} -g {rf_gain} -"
|
|
|
|
|
|
|
|
def getFormatConversion(self):
|
|
|
|
return "csdr convert_u8_f"
|
2019-05-10 12:23:54 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 12:23:54 +00:00
|
|
|
class HackrfSource(SdrSource):
|
2019-05-19 11:36:05 +00:00
|
|
|
def getCommand(self):
|
|
|
|
return "hackrf_transfer -s {samp_rate} -f {center_freq} -g {rf_gain} -l{lna_gain} -a{rf_amp} -r-"
|
|
|
|
|
|
|
|
def getFormatConversion(self):
|
|
|
|
return "csdr convert_s8_f"
|
2019-05-10 12:23:54 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 12:23:54 +00:00
|
|
|
class SdrplaySource(SdrSource):
|
2019-05-19 11:36:05 +00:00
|
|
|
def getCommand(self):
|
2019-05-19 20:10:11 +00:00
|
|
|
command = "rx_sdr -F CF32 -s {samp_rate} -f {center_freq} -p {ppm}"
|
2019-07-21 17:40:28 +00:00
|
|
|
gainMap = {"rf_gain": "RFGR", "if_gain": "IFGR"}
|
|
|
|
gains = [
|
|
|
|
"{0}={{{1}}}".format(gainMap[name], name)
|
|
|
|
for (name, value) in self.rtlProps.collect("rf_gain", "if_gain").__dict__().items()
|
|
|
|
if value is not None
|
|
|
|
]
|
2019-05-19 20:10:11 +00:00
|
|
|
if gains:
|
2019-07-21 17:40:28 +00:00
|
|
|
command += " -g {gains}".format(gains=",".join(gains))
|
2019-05-19 11:36:05 +00:00
|
|
|
if self.rtlProps["antenna"] is not None:
|
2019-07-21 17:40:28 +00:00
|
|
|
command += ' -a "{antenna}"'
|
2019-05-19 11:36:05 +00:00
|
|
|
command += " -"
|
|
|
|
return command
|
2019-05-10 12:23:54 +00:00
|
|
|
|
|
|
|
def sleepOnRestart(self):
|
|
|
|
time.sleep(1)
|
2019-05-04 18:26:11 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-06-07 13:44:11 +00:00
|
|
|
class AirspySource(SdrSource):
|
2019-06-07 13:49:43 +00:00
|
|
|
def getCommand(self):
|
2019-07-21 17:40:28 +00:00
|
|
|
frequency = self.props["center_freq"] / 1e6
|
2019-06-07 13:49:43 +00:00
|
|
|
command = "airspy_rx"
|
|
|
|
command += " -f{0}".format(frequency)
|
|
|
|
command += " -r /dev/stdout -a{samp_rate} -g {rf_gain}"
|
|
|
|
return command
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-06-07 13:49:43 +00:00
|
|
|
def getFormatConversion(self):
|
|
|
|
return "csdr convert_s16_f"
|
2019-06-07 13:44:11 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-14 21:30:03 +00:00
|
|
|
class SpectrumThread(csdr.output):
|
2019-05-09 20:44:29 +00:00
|
|
|
def __init__(self, sdrSource):
|
|
|
|
self.sdrSource = sdrSource
|
2019-05-10 18:59:06 +00:00
|
|
|
super().__init__()
|
2019-05-04 18:26:11 +00:00
|
|
|
|
2019-05-16 20:39:50 +00:00
|
|
|
self.props = props = self.sdrSource.props.collect(
|
2019-07-21 17:40:28 +00:00
|
|
|
"samp_rate",
|
|
|
|
"fft_size",
|
|
|
|
"fft_fps",
|
|
|
|
"fft_voverlap_factor",
|
|
|
|
"fft_compression",
|
|
|
|
"csdr_dynamic_bufsize",
|
|
|
|
"csdr_print_bufsizes",
|
|
|
|
"csdr_through",
|
|
|
|
"temporary_directory",
|
2019-05-09 20:44:29 +00:00
|
|
|
).defaults(PropertyManager.getSharedInstance())
|
2019-05-07 14:32:53 +00:00
|
|
|
|
2019-05-14 21:30:03 +00:00
|
|
|
self.dsp = dsp = csdr.dsp(self)
|
2019-05-09 20:44:29 +00:00
|
|
|
dsp.nc_port = self.sdrSource.getPort()
|
2019-05-04 18:26:11 +00:00
|
|
|
dsp.set_demodulator("fft")
|
2019-05-09 14:52:42 +00:00
|
|
|
|
|
|
|
def set_fft_averages(key, value):
|
|
|
|
samp_rate = props["samp_rate"]
|
|
|
|
fft_size = props["fft_size"]
|
|
|
|
fft_fps = props["fft_fps"]
|
|
|
|
fft_voverlap_factor = props["fft_voverlap_factor"]
|
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
dsp.set_fft_averages(
|
|
|
|
int(round(1.0 * samp_rate / fft_size / fft_fps / (1.0 - fft_voverlap_factor)))
|
|
|
|
if fft_voverlap_factor > 0
|
|
|
|
else 0
|
|
|
|
)
|
2019-05-18 19:38:15 +00:00
|
|
|
|
|
|
|
self.subscriptions = [
|
|
|
|
props.getProperty("samp_rate").wire(dsp.set_samp_rate),
|
|
|
|
props.getProperty("fft_size").wire(dsp.set_fft_size),
|
|
|
|
props.getProperty("fft_fps").wire(dsp.set_fft_fps),
|
|
|
|
props.getProperty("fft_compression").wire(dsp.set_fft_compression),
|
2019-07-13 15:16:38 +00:00
|
|
|
props.getProperty("temporary_directory").wire(dsp.set_temporary_directory),
|
2019-07-21 17:40:28 +00:00
|
|
|
props.collect("samp_rate", "fft_size", "fft_fps", "fft_voverlap_factor").wire(set_fft_averages),
|
2019-05-18 19:38:15 +00:00
|
|
|
]
|
|
|
|
|
2019-05-09 14:52:42 +00:00
|
|
|
set_fft_averages(None, None)
|
|
|
|
|
2019-05-07 14:32:53 +00:00
|
|
|
dsp.csdr_dynamic_bufsize = props["csdr_dynamic_bufsize"]
|
|
|
|
dsp.csdr_print_bufsizes = props["csdr_print_bufsizes"]
|
|
|
|
dsp.csdr_through = props["csdr_through"]
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.debug("Spectrum thread initialized successfully.")
|
2019-05-15 09:44:03 +00:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.sdrSource.addClient(self)
|
|
|
|
if self.sdrSource.isAvailable():
|
|
|
|
self.dsp.start()
|
2019-05-14 21:30:03 +00:00
|
|
|
|
2019-08-04 15:31:50 +00:00
|
|
|
def supports_type(self, t):
|
2019-08-11 09:37:45 +00:00
|
|
|
return t == "audio"
|
2019-05-14 21:30:03 +00:00
|
|
|
|
2019-08-04 15:31:50 +00:00
|
|
|
def receive_output(self, type, read_fn):
|
2019-05-16 20:39:50 +00:00
|
|
|
if self.props["csdr_dynamic_bufsize"]:
|
2019-07-21 17:40:28 +00:00
|
|
|
read_fn(8) # dummy read to skip bufsize & preamble
|
2019-05-16 20:39:50 +00:00
|
|
|
logger.debug("Note: CSDR_DYNAMIC_BUFSIZE_ON = 1")
|
|
|
|
|
2019-08-04 15:31:50 +00:00
|
|
|
threading.Thread(target=self.pump(read_fn, self.sdrSource.writeSpectrumData)).start()
|
2019-05-04 18:26:11 +00:00
|
|
|
|
2019-05-10 16:30:53 +00:00
|
|
|
def stop(self):
|
2019-05-14 21:30:03 +00:00
|
|
|
self.dsp.stop()
|
2019-05-15 09:44:03 +00:00
|
|
|
self.sdrSource.removeClient(self)
|
2019-05-18 19:38:15 +00:00
|
|
|
for c in self.subscriptions:
|
|
|
|
c.cancel()
|
|
|
|
self.subscriptions = []
|
2019-05-15 09:44:03 +00:00
|
|
|
|
2019-09-15 22:31:35 +00:00
|
|
|
def isActive(self):
|
|
|
|
return True
|
|
|
|
|
2019-05-15 09:44:03 +00:00
|
|
|
def onSdrAvailable(self):
|
|
|
|
self.dsp.start()
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-15 09:44:03 +00:00
|
|
|
def onSdrUnavailable(self):
|
|
|
|
self.dsp.stop()
|
2019-05-04 21:11:13 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-14 21:30:03 +00:00
|
|
|
class DspManager(csdr.output):
|
2019-05-09 20:44:29 +00:00
|
|
|
def __init__(self, handler, sdrSource):
|
2019-05-04 21:11:13 +00:00
|
|
|
self.handler = handler
|
2019-05-09 20:44:29 +00:00
|
|
|
self.sdrSource = sdrSource
|
2019-05-30 14:12:13 +00:00
|
|
|
self.metaParser = MetaParser(self.handler)
|
2019-07-07 12:10:03 +00:00
|
|
|
self.wsjtParser = WsjtParser(self.handler)
|
2019-08-15 13:45:15 +00:00
|
|
|
self.aprsParser = AprsParser(self.handler)
|
2019-05-04 21:11:13 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
self.localProps = (
|
|
|
|
self.sdrSource.getProps()
|
|
|
|
.collect(
|
|
|
|
"audio_compression",
|
|
|
|
"fft_compression",
|
|
|
|
"digimodes_fft_size",
|
|
|
|
"csdr_dynamic_bufsize",
|
|
|
|
"csdr_print_bufsizes",
|
|
|
|
"csdr_through",
|
|
|
|
"digimodes_enable",
|
|
|
|
"samp_rate",
|
|
|
|
"digital_voice_unvoiced_quality",
|
|
|
|
"dmr_filter",
|
|
|
|
"temporary_directory",
|
|
|
|
"center_freq",
|
|
|
|
)
|
|
|
|
.defaults(PropertyManager.getSharedInstance())
|
|
|
|
)
|
2019-05-04 21:11:13 +00:00
|
|
|
|
2019-05-14 21:30:03 +00:00
|
|
|
self.dsp = csdr.dsp(self)
|
2019-05-09 20:44:29 +00:00
|
|
|
self.dsp.nc_port = self.sdrSource.getPort()
|
2019-05-05 13:51:33 +00:00
|
|
|
|
|
|
|
def set_low_cut(cut):
|
|
|
|
bpf = self.dsp.get_bpf()
|
|
|
|
bpf[0] = cut
|
|
|
|
self.dsp.set_bpf(*bpf)
|
|
|
|
|
|
|
|
def set_high_cut(cut):
|
|
|
|
bpf = self.dsp.get_bpf()
|
|
|
|
bpf[1] = cut
|
|
|
|
self.dsp.set_bpf(*bpf)
|
|
|
|
|
2019-07-14 17:32:48 +00:00
|
|
|
def set_dial_freq(key, value):
|
2019-08-15 13:45:15 +00:00
|
|
|
freq = self.localProps["center_freq"] + self.localProps["offset_freq"]
|
|
|
|
self.wsjtParser.setDialFrequency(freq)
|
|
|
|
self.aprsParser.setDialFrequency(freq)
|
2019-09-17 16:44:37 +00:00
|
|
|
self.metaParser.setDialFrequency(freq)
|
2019-07-14 17:32:48 +00:00
|
|
|
|
2019-05-18 19:38:15 +00:00
|
|
|
self.subscriptions = [
|
|
|
|
self.localProps.getProperty("audio_compression").wire(self.dsp.set_audio_compression),
|
|
|
|
self.localProps.getProperty("fft_compression").wire(self.dsp.set_fft_compression),
|
|
|
|
self.localProps.getProperty("digimodes_fft_size").wire(self.dsp.set_secondary_fft_size),
|
|
|
|
self.localProps.getProperty("samp_rate").wire(self.dsp.set_samp_rate),
|
|
|
|
self.localProps.getProperty("output_rate").wire(self.dsp.set_output_rate),
|
|
|
|
self.localProps.getProperty("offset_freq").wire(self.dsp.set_offset_freq),
|
|
|
|
self.localProps.getProperty("squelch_level").wire(self.dsp.set_squelch_level),
|
|
|
|
self.localProps.getProperty("low_cut").wire(set_low_cut),
|
|
|
|
self.localProps.getProperty("high_cut").wire(set_high_cut),
|
2019-05-18 20:10:43 +00:00
|
|
|
self.localProps.getProperty("mod").wire(self.dsp.set_demodulator),
|
2019-06-15 19:47:28 +00:00
|
|
|
self.localProps.getProperty("digital_voice_unvoiced_quality").wire(self.dsp.set_unvoiced_quality),
|
2019-07-13 15:16:38 +00:00
|
|
|
self.localProps.getProperty("dmr_filter").wire(self.dsp.set_dmr_filter),
|
2019-07-14 17:32:48 +00:00
|
|
|
self.localProps.getProperty("temporary_directory").wire(self.dsp.set_temporary_directory),
|
2019-07-21 17:40:28 +00:00
|
|
|
self.localProps.collect("center_freq", "offset_freq").wire(set_dial_freq),
|
2019-05-18 19:38:15 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
self.dsp.set_offset_freq(0)
|
2019-07-21 17:40:28 +00:00
|
|
|
self.dsp.set_bpf(-4000, 4000)
|
2019-05-18 19:38:15 +00:00
|
|
|
self.dsp.csdr_dynamic_bufsize = self.localProps["csdr_dynamic_bufsize"]
|
|
|
|
self.dsp.csdr_print_bufsizes = self.localProps["csdr_print_bufsizes"]
|
|
|
|
self.dsp.csdr_through = self.localProps["csdr_through"]
|
2019-05-05 13:51:33 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
if self.localProps["digimodes_enable"]:
|
|
|
|
|
2019-05-05 18:36:50 +00:00
|
|
|
def set_secondary_mod(mod):
|
2019-07-21 17:40:28 +00:00
|
|
|
if mod == False:
|
|
|
|
mod = None
|
2019-05-08 14:31:52 +00:00
|
|
|
self.dsp.set_secondary_demodulator(mod)
|
|
|
|
if mod is not None:
|
2019-07-21 17:40:28 +00:00
|
|
|
self.handler.write_secondary_dsp_config(
|
|
|
|
{
|
|
|
|
"secondary_fft_size": self.localProps["digimodes_fft_size"],
|
|
|
|
"if_samp_rate": self.dsp.if_samp_rate(),
|
|
|
|
"secondary_bw": self.dsp.secondary_bw(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-05-18 19:38:15 +00:00
|
|
|
self.subscriptions += [
|
|
|
|
self.localProps.getProperty("secondary_mod").wire(set_secondary_mod),
|
2019-07-21 17:40:28 +00:00
|
|
|
self.localProps.getProperty("secondary_offset_freq").wire(self.dsp.set_secondary_offset_freq),
|
2019-05-18 19:38:15 +00:00
|
|
|
]
|
2019-05-05 18:36:50 +00:00
|
|
|
|
2019-05-15 09:44:03 +00:00
|
|
|
self.sdrSource.addClient(self)
|
|
|
|
|
2019-05-04 21:11:13 +00:00
|
|
|
super().__init__()
|
|
|
|
|
2019-05-05 14:17:55 +00:00
|
|
|
def start(self):
|
2019-05-15 09:44:03 +00:00
|
|
|
if self.sdrSource.isAvailable():
|
2019-05-10 16:30:53 +00:00
|
|
|
self.dsp.start()
|
2019-05-14 21:30:03 +00:00
|
|
|
|
2019-08-04 12:55:56 +00:00
|
|
|
def receive_output(self, t, read_fn):
|
2019-05-14 21:30:03 +00:00
|
|
|
logger.debug("adding new output of type %s", t)
|
|
|
|
writers = {
|
|
|
|
"audio": self.handler.write_dsp_data,
|
|
|
|
"smeter": self.handler.write_s_meter_level,
|
|
|
|
"secondary_fft": self.handler.write_secondary_fft,
|
|
|
|
"secondary_demod": self.handler.write_secondary_demod,
|
2019-07-07 12:10:03 +00:00
|
|
|
"meta": self.metaParser.parse,
|
2019-07-21 17:40:28 +00:00
|
|
|
"wsjt_demod": self.wsjtParser.parse,
|
2019-08-15 13:45:15 +00:00
|
|
|
"packet_demod": self.aprsParser.parse,
|
2019-05-14 21:30:03 +00:00
|
|
|
}
|
|
|
|
write = writers[t]
|
|
|
|
|
2019-07-28 09:40:58 +00:00
|
|
|
threading.Thread(target=self.pump(read_fn, write)).start()
|
2019-05-05 20:09:48 +00:00
|
|
|
|
2019-05-04 21:11:13 +00:00
|
|
|
def stop(self):
|
2019-05-05 17:46:13 +00:00
|
|
|
self.dsp.stop()
|
2019-05-10 12:58:25 +00:00
|
|
|
self.sdrSource.removeClient(self)
|
2019-05-18 19:38:15 +00:00
|
|
|
for sub in self.subscriptions:
|
|
|
|
sub.cancel()
|
|
|
|
self.subscriptions = []
|
2019-05-04 21:11:13 +00:00
|
|
|
|
2019-05-05 13:51:33 +00:00
|
|
|
def setProperty(self, prop, value):
|
|
|
|
self.localProps.getProperty(prop).setValue(value)
|
2019-05-05 15:34:40 +00:00
|
|
|
|
2019-09-15 22:31:35 +00:00
|
|
|
def isActive(self):
|
|
|
|
return True
|
|
|
|
|
2019-05-10 16:30:53 +00:00
|
|
|
def onSdrAvailable(self):
|
2019-05-10 22:38:46 +00:00
|
|
|
logger.debug("received onSdrAvailable, attempting DspSource restart")
|
2019-05-15 09:44:03 +00:00
|
|
|
self.dsp.start()
|
2019-05-10 12:58:25 +00:00
|
|
|
|
2019-05-10 16:30:53 +00:00
|
|
|
def onSdrUnavailable(self):
|
2019-05-10 22:38:46 +00:00
|
|
|
logger.debug("received onSdrUnavailable, shutting down DspSource")
|
2019-05-15 09:44:03 +00:00
|
|
|
self.dsp.stop()
|
2019-05-10 12:58:25 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-05 15:34:40 +00:00
|
|
|
class CpuUsageThread(threading.Thread):
|
|
|
|
sharedInstance = None
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-05 15:34:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def getSharedInstance():
|
|
|
|
if CpuUsageThread.sharedInstance is None:
|
|
|
|
CpuUsageThread.sharedInstance = CpuUsageThread()
|
|
|
|
return CpuUsageThread.sharedInstance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.clients = []
|
|
|
|
self.doRun = True
|
|
|
|
self.last_worktime = 0
|
|
|
|
self.last_idletime = 0
|
2019-09-23 01:06:51 +00:00
|
|
|
self.endEvent = threading.Event()
|
2019-05-05 15:34:40 +00:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while self.doRun:
|
|
|
|
try:
|
|
|
|
cpu_usage = self.get_cpu_usage()
|
|
|
|
except:
|
|
|
|
cpu_usage = 0
|
|
|
|
for c in self.clients:
|
|
|
|
c.write_cpu_usage(cpu_usage)
|
2019-09-23 01:06:51 +00:00
|
|
|
self.endEvent.wait(timeout=3)
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.debug("cpu usage thread shut down")
|
2019-05-05 15:34:40 +00:00
|
|
|
|
|
|
|
def get_cpu_usage(self):
|
|
|
|
try:
|
2019-07-21 17:40:28 +00:00
|
|
|
f = open("/proc/stat", "r")
|
2019-05-05 15:34:40 +00:00
|
|
|
except:
|
2019-07-21 17:40:28 +00:00
|
|
|
return 0 # Workaround, possibly we're on a Mac
|
2019-05-05 15:34:40 +00:00
|
|
|
line = ""
|
2019-07-21 17:40:28 +00:00
|
|
|
while not "cpu " in line:
|
|
|
|
line = f.readline()
|
2019-05-05 15:34:40 +00:00
|
|
|
f.close()
|
|
|
|
spl = line.split(" ")
|
|
|
|
worktime = int(spl[2]) + int(spl[3]) + int(spl[4])
|
|
|
|
idletime = int(spl[5])
|
2019-07-21 17:40:28 +00:00
|
|
|
dworktime = worktime - self.last_worktime
|
|
|
|
didletime = idletime - self.last_idletime
|
|
|
|
rate = float(dworktime) / (didletime + dworktime)
|
2019-05-05 15:34:40 +00:00
|
|
|
self.last_worktime = worktime
|
|
|
|
self.last_idletime = idletime
|
2019-07-21 17:40:28 +00:00
|
|
|
if self.last_worktime == 0:
|
|
|
|
return 0
|
2019-05-05 15:34:40 +00:00
|
|
|
return rate
|
|
|
|
|
|
|
|
def add_client(self, c):
|
|
|
|
self.clients.append(c)
|
2019-09-21 13:24:06 +00:00
|
|
|
if not self.is_alive():
|
|
|
|
self.start()
|
2019-05-05 15:34:40 +00:00
|
|
|
|
|
|
|
def remove_client(self, c):
|
2019-05-09 20:44:29 +00:00
|
|
|
try:
|
|
|
|
self.clients.remove(c)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-05-05 15:34:40 +00:00
|
|
|
if not self.clients:
|
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def shutdown(self):
|
2019-05-15 17:43:52 +00:00
|
|
|
CpuUsageThread.sharedInstance = None
|
2019-05-12 16:10:24 +00:00
|
|
|
self.doRun = False
|
2019-09-23 01:06:51 +00:00
|
|
|
self.endEvent.set()
|
2019-05-12 16:10:24 +00:00
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 20:47:40 +00:00
|
|
|
class TooManyClientsException(Exception):
|
|
|
|
pass
|
|
|
|
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-12 16:10:24 +00:00
|
|
|
class ClientRegistry(object):
|
2019-05-10 20:47:40 +00:00
|
|
|
sharedInstance = None
|
2019-09-18 13:40:23 +00:00
|
|
|
creationLock = threading.Lock()
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 20:47:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def getSharedInstance():
|
2019-09-18 13:40:23 +00:00
|
|
|
with ClientRegistry.creationLock:
|
|
|
|
if ClientRegistry.sharedInstance is None:
|
|
|
|
ClientRegistry.sharedInstance = ClientRegistry()
|
2019-05-12 16:10:24 +00:00
|
|
|
return ClientRegistry.sharedInstance
|
2019-05-10 20:47:40 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.clients = []
|
2019-09-18 13:40:23 +00:00
|
|
|
Metrics.getSharedInstance().addMetric("openwebrx.users", DirectMetric(self.clientCount))
|
2019-05-10 20:47:40 +00:00
|
|
|
super().__init__()
|
|
|
|
|
2019-05-12 16:10:24 +00:00
|
|
|
def broadcast(self):
|
|
|
|
n = self.clientCount()
|
|
|
|
for c in self.clients:
|
|
|
|
c.write_clients(n)
|
2019-05-10 20:47:40 +00:00
|
|
|
|
|
|
|
def addClient(self, client):
|
|
|
|
pm = PropertyManager.getSharedInstance()
|
|
|
|
if len(self.clients) >= pm["max_clients"]:
|
|
|
|
raise TooManyClientsException()
|
|
|
|
self.clients.append(client)
|
2019-05-15 17:51:50 +00:00
|
|
|
self.broadcast()
|
2019-05-10 20:47:40 +00:00
|
|
|
|
2019-05-10 21:00:18 +00:00
|
|
|
def clientCount(self):
|
|
|
|
return len(self.clients)
|
|
|
|
|
2019-05-10 20:47:40 +00:00
|
|
|
def removeClient(self, client):
|
|
|
|
try:
|
|
|
|
self.clients.remove(client)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-07-21 17:40:28 +00:00
|
|
|
self.broadcast()
|