From 56bb29ebdd3f9bdc03b801ff5c20733d3279efc2 Mon Sep 17 00:00:00 2001 From: Jono Targett Date: Mon, 15 May 2023 12:02:23 +0930 Subject: [PATCH] Better error handling for stream failures, actually give an error message --- microservice.py | 5 +---- radio.py | 22 +++++++++++++--------- soapyhelpers.py | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 soapyhelpers.py diff --git a/microservice.py b/microservice.py index a6e12fa..38b98c3 100755 --- a/microservice.py +++ b/microservice.py @@ -107,10 +107,7 @@ def configure(radio, frequency): description: The specified radio is not connected. """ if radio in radios: - if radios[radio].is_streaming(): - return 'no', 400 - else: - return jsonify(radios[radio].configure(frequency)) + return jsonify(radios[radio].configure(frequency)) else: return "Radio not connected", 400 diff --git a/radio.py b/radio.py index 151a55d..a0395f2 100644 --- a/radio.py +++ b/radio.py @@ -5,6 +5,7 @@ from formats import * import sys import subprocess import struct +from soapyhelpers import * class Radio: @@ -72,20 +73,23 @@ class Radio: while self.run: # Check that the child processes are still running if (not is_alive(self.demod)) or (not is_alive(self.playback)): - print('DSP chain error, aborting stream.', file=sys.stderr) + print('DSP chain failed, aborting stream.', file=sys.stderr) break result = self.device.readStream(self.stream, [self.buffer], Radio.SAMPLES) - if result.ret < 1: - print('Stream read failed, aborting stream.', file=sys.stderr) + if result == 0: + continue + elif result.ret < 0: + error = SoapyError(result.ret) + print("Stream read failed, aborting stream:", error, file=sys.stderr) break - - read_size = int(result.ret * 2) - self.demod.stdin.write( - struct.pack(PACKINGS[Radio.FORMAT] % read_size, - *self.buffer[:read_size]) - ) + else: + read_size = int(result.ret * 2) + self.demod.stdin.write( + struct.pack(PACKINGS[Radio.FORMAT] % read_size, + *self.buffer[:read_size]) + ) self._cleanup_stream() diff --git a/soapyhelpers.py b/soapyhelpers.py new file mode 100644 index 0000000..4721de6 --- /dev/null +++ b/soapyhelpers.py @@ -0,0 +1,19 @@ +import SoapySDR as soapy +from enum import IntEnum + +class SoapyError(IntEnum): + Timeout = soapy.SOAPY_SDR_TIMEOUT + StreamError = soapy.SOAPY_SDR_STREAM_ERROR + Corruption = soapy.SOAPY_SDR_CORRUPTION + Overflow = soapy.SOAPY_SDR_OVERFLOW + NotSupported = soapy.SOAPY_SDR_NOT_SUPPORTED + TimeError = soapy.SOAPY_SDR_TIME_ERROR + Underflow = soapy.SOAPY_SDR_UNDERFLOW + +class SoapyFlag(IntEnum): + EndBurst = soapy.SOAPY_SDR_END_BURST + HasTime = soapy.SOAPY_SDR_HAS_TIME + EndAbrupt = soapy.SOAPY_SDR_END_ABRUPT + OnePacket = soapy.SOAPY_SDR_ONE_PACKET + MoreFragments = soapy.SOAPY_SDR_MORE_FRAGMENTS + WaitTrigger = soapy.SOAPY_SDR_WAIT_TRIGGER