From cc66ffd6f303eaa1781e42560281d45db3ae16dd Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Fri, 16 Aug 2019 01:27:03 +0200 Subject: [PATCH] use generated port numbers for direwolf, allowing multiple instances --- csdr.py | 20 ++++++++++++++++++-- owrx/kiss.py | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/csdr.py b/csdr.py index a896e9b..d55bf0f 100644 --- a/csdr.py +++ b/csdr.py @@ -235,7 +235,7 @@ class dsp(object): chain = secondary_chain_base + "csdr fmdemod_quadri_cf | " if self.last_decimation != 1.0: chain += "csdr fractional_decimator_ff {last_decimation} | " - chain += "csdr convert_f_s16 | direwolf -r {audio_rate} -t 0 - 1>&2" + chain += "csdr convert_f_s16 | direwolf -c {direwolf_config} -r {audio_rate} -t 0 - 1>&2" return chain def set_secondary_demodulator(self, what): @@ -278,6 +278,7 @@ class dsp(object): logger.debug("starting secondary demodulator from IF input sampled at %d" % self.if_samp_rate()) secondary_command_demod = self.secondary_chain(self.secondary_demodulator) self.try_create_pipes(self.secondary_pipe_names, secondary_command_demod) + self.try_create_configs(secondary_command_demod) secondary_command_demod = secondary_command_demod.format( input_pipe=self.iqtee2_pipe, @@ -289,6 +290,7 @@ class dsp(object): if_samp_rate=self.if_samp_rate(), last_decimation=self.last_decimation, audio_rate=self.get_audio_rate(), + direwolf_config=self.direwolf_config, ) logger.debug("secondary command (demod) = %s", secondary_command_demod) @@ -337,7 +339,7 @@ class dsp(object): self.output.send_output("secondary_demod", partial(self.secondary_process_demod.stdout.read, 1)) if self.isPacket(): - kiss = KissClient(8001) + kiss = KissClient(self.direwolf_port) self.output.send_output("packet_demod", kiss.read) # open control pipes for csdr and send initialization data @@ -548,6 +550,20 @@ class dsp(object): except Exception: logger.exception("try_delete_pipes()") + def try_create_configs(self, command): + if "{direwolf_config}" in command: + self.direwolf_config = "{tmp_dir}/openwebrx_direwolf_{myid}.conf".format(tmp_dir=self.temporary_directory, myid=id(self)) + self.direwolf_port = KissClient.getFreePort() + file = open(self.direwolf_config, "w") + file.write(""" +MODEM 1200 +KISSPORT {port} +AGWPORT off + """.format(port=self.direwolf_port)) + file.close() + else: + self.direwolf_config = None + def start(self): self.modification_lock.acquire() if self.running: diff --git a/owrx/kiss.py b/owrx/kiss.py index 7a401f2..c036715 100644 --- a/owrx/kiss.py +++ b/owrx/kiss.py @@ -1,6 +1,7 @@ import socket import time import logging +import random logger = logging.getLogger(__name__) @@ -11,6 +12,21 @@ TFESC = 0xDD class KissClient(object): + @staticmethod + def getFreePort(): + # direwolf has some strange hardcoded port ranges + while True: + try: + port = random.randrange(1024, 49151) + # test if port is available for use + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(('localhost', port)) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.close() + return port + except OSError: + pass + def __init__(self, port): time.sleep(1) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)