Skip to content

Commit

Permalink
Handle exceptions from SoapySDR during stream setup more cleanly.
Browse files Browse the repository at this point in the history
  • Loading branch information
mutability committed Apr 16, 2019
1 parent b00e549 commit c09beb1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
10 changes: 6 additions & 4 deletions dump978_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "convert.h"
#include "demodulator.h"
#include "exception.h"
#include "message_dispatch.h"
#include "sample_source.h"
#include "soapy_source.h"
Expand Down Expand Up @@ -246,14 +247,15 @@ static int realmain(int argc, char **argv) {
}

int main(int argc, char **argv) {
#ifndef DEBUG_EXCEPTIONS
try {
return realmain(argc, argv);
} catch (const config_error &e) {
std::cerr << "Configuration error: " << e.what() << std::endl;
return 2;
#ifndef DEBUG_EXCEPTIONS
} catch (...) {
std::cerr << "Uncaught exception: " << boost::current_exception_diagnostic_information() << std::endl;
return 2;
}
#else
return realmain(argc, argv);
#endif
}
}
20 changes: 20 additions & 0 deletions exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// -*- c++ -*-

// Copyright (c) 2019, FlightAware LLC.
// All rights reserved.
// Licensed under the 2-clause BSD license; see the LICENSE file

#ifndef DUMP978_EXCEPTION_H
#define DUMP978_EXCEPTION_H

#include <stdexcept>

namespace flightaware::uat {
class config_error : public std::runtime_error {
public:
explicit config_error(const std::string &what_arg) : runtime_error(what_arg) {}
explicit config_error(const char *what_arg) : runtime_error(what_arg) {}
};
}; // namespace flightaware::uat

#endif
25 changes: 18 additions & 7 deletions soapy_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the 2-clause BSD license; see the LICENSE file

#include "soapy_source.h"
#include "exception.h"

#include <iomanip>
#include <iostream>
Expand Down Expand Up @@ -100,9 +101,14 @@ SoapySampleSource::SoapySampleSource(boost::asio::io_service &service, const std
SoapySampleSource::~SoapySampleSource() { Stop(); }

void SoapySampleSource::Init() {
device_ = {SoapySDR::Device::make(device_name_), &SoapySDR::Device::unmake};
try {
device_ = {SoapySDR::Device::make(device_name_), &SoapySDR::Device::unmake};
} catch (const std::runtime_error &err) {
throw config_error(std::string("No matching SoapySDR device found (cause: ") + err.what() + ")");
}

if (!device_) {
throw std::runtime_error("no suitable device found");
throw config_error("No matching SoapySDR device found");
}

// hacky mchackerson
Expand All @@ -112,7 +118,7 @@ void SoapySampleSource::Init() {

if (options_.count("sdr-auto-gain")) {
if (!device_->hasGainMode(SOAPY_SDR_RX, 0)) {
throw std::runtime_error("device does not support automatic gain mode");
throw config_error("Device does not support automatic gain mode");
}
std::cerr << "SoapySDR: using automatic gain" << std::endl;
device_->setGainMode(SOAPY_SDR_RX, 0, true);
Expand Down Expand Up @@ -171,12 +177,12 @@ void SoapySampleSource::Init() {
soapy_format = device_->getNativeStreamFormat(SOAPY_SDR_RX, 0, fullScale);
format_ = SoapyToFormat(soapy_format);
if (format_ == SampleFormat::UNKNOWN) {
throw std::runtime_error("Unsupported native SDR format: " + soapy_format + "; try specifying --format");
throw config_error("Unsupported native SDR format: " + soapy_format + "; try specifying --format");
}
} else {
soapy_format = FormatToSoapy(format_);
if (soapy_format.empty()) {
throw std::runtime_error("unsupported sample format");
throw config_error("Unsupported sample format");
}
}

Expand All @@ -199,9 +205,14 @@ void SoapySampleSource::Init() {
#endif
}

stream_ = {device_->setupStream(SOAPY_SDR_RX, soapy_format, channels, stream_settings), std::bind(&SoapySDR::Device::closeStream, device_, std::placeholders::_1)};
try {
stream_ = {device_->setupStream(SOAPY_SDR_RX, soapy_format, channels, stream_settings), std::bind(&SoapySDR::Device::closeStream, device_, std::placeholders::_1)};
} catch (const std::runtime_error &err) {
throw config_error(std::string("Failed to construct soapysdr stream (cause: ") + err.what() + ")");
}

if (!stream_) {
throw std::runtime_error("failed to construct stream");
throw config_error("Failed to construct soapysdr stream");
}
}

Expand Down

0 comments on commit c09beb1

Please sign in to comment.