Skip to content

Commit d900962

Browse files
committed
Device cleanup: fix SoapySDR/Serial/UDP review findings and strip redundant log-line terminators
1 parent 4084ef6 commit d900962

12 files changed

Lines changed: 72 additions & 37 deletions

File tree

Source/Aviation/ADSB.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ namespace Plane
436436
{
437437
if (!warning_given)
438438
{
439-
Error() << "ADSB: Reference position is not available. Cannot determine location of onground planes. Provide receiver location with -Z lat lon." << std::endl;
439+
Error() << "ADSB: Reference position is not available. Cannot determine location of onground planes. Provide receiver location with -Z lat lon.";
440440
warning_given = true;
441441
}
442442
return false;

Source/DBMS/PostgreSQL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ namespace IO
379379
void PostgreSQL::setup()
380380
{
381381
db_keys.resize(AIS::KEY_COUNT, -1);
382-
Debug() << "Connecting to ProgreSQL database: \"" + conn_string + "\"\n";
382+
Debug() << "Connecting to ProgreSQL database: \"" + conn_string + "\"";
383383
con = PQconnectdb(conn_string.c_str());
384384

385385
if (con == nullptr || PQstatus(con) != CONNECTION_OK)

Source/DSP/DSP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ namespace DSP {
349349
if (error) {
350350
soxr_delete(m_soxr);
351351
m_soxr = nullptr;
352-
Error() << "SOX processing returns error." << std::endl;
352+
Error() << "SOX processing returns error.";
353353
return;
354354
}
355355

Source/Device/SDRPLAY.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace Device {
5858

5959
err = sdrplay_api_SelectDevice(&device);
6060
if (err != sdrplay_api_Success) {
61-
Error() << sdrplay_api_GetErrorString(err) << std::endl;
61+
Error() << sdrplay_api_GetErrorString(err);
6262
sdrplay_api_UnlockDeviceApi();
6363
throw std::runtime_error("SDRPLAY: cannot open device");
6464
}
@@ -147,7 +147,7 @@ namespace Device {
147147

148148
void SDRPLAY::callback_event(sdrplay_api_EventT eventId, sdrplay_api_TunerSelectT tuner, sdrplay_api_EventParamsT* params) {
149149
if (eventId == sdrplay_api_DeviceRemoved) {
150-
Error() << "SDRPLAY: device disconnected" << std::endl;
150+
Error() << "SDRPLAY: device disconnected";
151151
Stop();
152152
}
153153
}
@@ -162,7 +162,7 @@ namespace Device {
162162
}
163163

164164
if (!fifo.Push((char*)output.data(), len * sizeof(CFLOAT32)))
165-
Error() << "SDRPLAY: buffer overrun." << std::endl;
165+
Error() << "SDRPLAY: buffer overrun.";
166166
}
167167
}
168168

@@ -174,7 +174,7 @@ namespace Device {
174174
fifo.Pop();
175175
}
176176
else if (isStreaming())
177-
Error() << "SDRPLAY: timeout." << std::endl;
177+
Error() << "SDRPLAY: timeout.";
178178
}
179179
}
180180

Source/Device/Serial.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace Device
6868
RAW r = {getFormat(), buffer, 0};
6969

7070
#ifdef _WIN32
71-
Debug() << "Serial: starting thread" << std::endl;
71+
Debug() << "Serial: starting thread";
7272
DWORD bytesRead;
7373

7474
while (isStreaming())
@@ -89,7 +89,7 @@ namespace Device
8989
else
9090
{
9191
DWORD error = GetLastError();
92-
Error() << "Serial read error: " << error << std::endl;
92+
Error() << "Serial read error: " << error;
9393
lost = true;
9494
break;
9595
}
@@ -119,18 +119,18 @@ namespace Device
119119
{
120120
if (nread == 0)
121121
{
122-
Error() << "Serial read encountered an error: unexpected end." << std::endl;
122+
Error() << "Serial read encountered an error: unexpected end.";
123123
}
124124
else
125125
{
126-
Error() << "Serial read encountered an error: " << strerror(errno) << std::endl;
126+
Error() << "Serial read encountered an error: " << strerror(errno);
127127
}
128128
lost = true;
129129
}
130130
}
131131
else if (rslt < 0)
132132
{
133-
Error() << "Serial read encountered an error: " << strerror(errno) << std::endl;
133+
Error() << "Serial read encountered an error: " << strerror(errno);
134134
lost = true;
135135
}
136136
}
@@ -211,6 +211,18 @@ namespace Device
211211
}
212212

213213
#ifdef _WIN32
214+
switch (baudrate)
215+
{
216+
case 9600:
217+
case 19200:
218+
case 38400:
219+
case 57600:
220+
case 115200:
221+
break;
222+
default:
223+
throw std::runtime_error("Serial: unsupported baudrate.");
224+
}
225+
214226
serial_handle = CreateFileA(port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
215227
if (serial_handle == INVALID_HANDLE_VALUE)
216228
{
@@ -339,7 +351,7 @@ namespace Device
339351
if (flowcontrol == FlowControl::HARDWARE)
340352
{
341353
tty.c_cflag |= CRTSCTS;
342-
Info() << "Serial: hardware flow control enabled" << std::endl;
354+
Info() << "Serial: hardware flow control enabled";
343355
}
344356
else
345357
{
@@ -385,7 +397,7 @@ namespace Device
385397
if (!cmd.empty())
386398
{
387399
SleepSystem(250);
388-
Info() << "Serial: init command sent \"" << cmd << "\"" << std::endl;
400+
Info() << "Serial: init command sent \"" << cmd << "\"";
389401
WriteSerialCommand(serial_fd, cmd);
390402
}
391403
}
@@ -494,7 +506,21 @@ namespace Device
494506
{
495507
device_list.clear();
496508

497-
#ifndef _WIN32
509+
#if defined(__APPLE__)
510+
// macOS exposes serial ports as /dev/cu.*
511+
std::vector<std::string> dev_files = Util::Helper::getFilesInDirectory("/dev");
512+
for (const auto &name : dev_files)
513+
{
514+
if (name.compare(0, 3, "cu.") != 0 || name.find("Bluetooth") != std::string::npos)
515+
continue;
516+
517+
std::string device_path = "/dev/" + name;
518+
519+
uint64_t handle = device_list.size();
520+
device_list.push_back(device_path);
521+
DeviceList.push_back(Description("Serial", "USB Serial", name, handle, Type::SERIALPORT));
522+
}
523+
#elif !defined(_WIN32)
498524
// Scan /dev/serial/by-id for persistent device names (symlinks)
499525
std::vector<std::string> by_id_files = Util::Helper::getFilesInDirectory("/dev/serial/by-id");
500526
for (const auto &name : by_id_files)

Source/Device/Serial.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <fstream>
2323
#include <vector>
2424
#include <thread>
25+
#include <atomic>
2526
#include <string>
2627

2728
#ifdef _WIN32
@@ -78,7 +79,7 @@ namespace Device
7879

7980
std::thread read_thread;
8081

81-
bool lost = false;
82+
std::atomic<bool> lost{false};
8283
bool dump = false;
8384
std::string dump_file;
8485
std::ofstream dump_stream;

Source/Device/SoapySDR.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ namespace Device {
4343
void SOAPYSDR::Close() {
4444
Device::Close();
4545

46-
if (dev != NULL && !skip_unmake) {
46+
if (dev != nullptr && !skip_unmake) {
4747
SoapySDR::Device::unmake(dev);
4848
}
49-
dev = NULL;
49+
dev = nullptr;
5050
}
5151

5252
void SOAPYSDR::Play() {
@@ -81,9 +81,9 @@ namespace Device {
8181
if (run_thread.joinable()) run_thread.join();
8282
}
8383

84-
if (dev != NULL && !skip_unmake) {
84+
if (dev != nullptr && !skip_unmake) {
8585
SoapySDR::Device::unmake(dev);
86-
dev = NULL;
86+
dev = nullptr;
8787
}
8888
}
8989

@@ -96,7 +96,7 @@ namespace Device {
9696
stream = dev->setupStream(SOAPY_SDR_RX, "CF32", channels, stream_args);
9797
}
9898
catch (std::exception& e) {
99-
Error() << "SOAPYSDR: " << e.what() << std::endl;
99+
Error() << "SOAPYSDR: " << e.what();
100100
lost = true;
101101
return;
102102
}
@@ -118,17 +118,17 @@ namespace Device {
118118
int ret = dev->readStream(stream, buffers, mtu, flags, timeNs, timeout_us);
119119

120120
if (ret < 0) {
121-
Error() << "SOAPYSDR: error reading stream: " << SoapySDR_errToStr(ret) << std::endl;
121+
Error() << "SOAPYSDR: error reading stream: " << SoapySDR_errToStr(ret);
122122
lost = true;
123123
skip_unmake = true;
124124
break;
125125
}
126126
if (ret > 0 && isStreaming() && !fifo.Push((char*)input.data(), ret * sizeof(CFLOAT32)))
127-
Error() << "SOAPYSDR: buffer overrun." << std::endl;
127+
Error() << "SOAPYSDR: buffer overrun.";
128128
}
129129
}
130130
catch (std::exception& e) {
131-
Error() << "SOAPYSDR: exception " << e.what() << std::endl;
131+
Error() << "SOAPYSDR: exception " << e.what();
132132
lost = true;
133133
}
134134
if (activated) {
@@ -150,7 +150,7 @@ namespace Device {
150150
fifo.Pop();
151151
}
152152
else {
153-
if (isStreaming()) Error() << "SOAPYSDR: timeout." << std::endl;
153+
if (isStreaming()) Error() << "SOAPYSDR: timeout.";
154154
}
155155
}
156156
}
@@ -197,8 +197,9 @@ namespace Device {
197197

198198
std::string dev_str = "driver=" + d["driver"] + ",serial=" + d["serial"];
199199

200+
SoapySDR::Device* device = nullptr;
200201
try {
201-
auto device = SoapySDR::Device::make(dev_str);
202+
device = SoapySDR::Device::make(dev_str);
202203
int nChannels = device->getNumChannels(SOAPY_SDR_RX);
203204
for (int c = 0; c < nChannels; c++) {
204205
std::string serial_str = "SCH" + std::to_string(c) + "-" + d["driver"];
@@ -207,11 +208,13 @@ namespace Device {
207208
DeviceList.push_back(Description("SOAPYSDR", dev_str, serial_str, (uint64_t)cnt, Type::SOAPYSDR));
208209
cnt++;
209210
}
210-
SoapySDR::Device::unmake(device);
211211
}
212212
catch (const std::exception& ex) {
213213
Warning() << "SOAPYSDR: skipping device: " << ex.what();
214214
}
215+
216+
if (device != nullptr)
217+
SoapySDR::Device::unmake(device);
215218
}
216219
}
217220

Source/Device/SoapySDR.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace Device
5252
bool print = false;
5353

5454
std::vector<SoapyDevice> dev_list;
55-
SoapySDR::Device *dev = NULL;
55+
SoapySDR::Device *dev = nullptr;
5656

5757
SoapySDR::Kwargs stream_args;
5858
SoapySDR::Kwargs setting_args;
@@ -75,7 +75,6 @@ namespace Device
7575
static const uint32_t BUFFER_SIZE = 16 * 16384;
7676

7777
void applySettings();
78-
int findRate(const std::vector<double> &);
7978

8079
public:
8180
SOAPYSDR() : Device(Format::CF32, 0, Type::SOAPYSDR, "SoapySDR") {}

Source/Device/UDP.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ namespace Device
8989
void UDP::Close()
9090
{
9191
Device::Close();
92+
Stop();
9293
StopServer();
9394
}
9495

@@ -119,7 +120,7 @@ namespace Device
119120

120121
void UDP::Run()
121122
{
122-
Debug() << "UDP: starting thread.\n";
123+
Debug() << "UDP: starting thread.";
123124
char buffer[16384];
124125
RAW r = {getFormat(), buffer, 0};
125126
int nread;
@@ -134,7 +135,12 @@ namespace Device
134135
r.size = nread;
135136
Send(&r, 1, tag);
136137
}
137-
} while (nread > 0);
138+
else if (nread < 0 && !Net::wouldBlock(Net::lastError()))
139+
{
140+
Error() << "UDP: receive error: " << Net::errorString(Net::lastError());
141+
lost = true;
142+
}
143+
} while (nread > 0 && isStreaming());
138144

139145
struct timeval tv;
140146
fd_set fds;
@@ -145,7 +151,7 @@ namespace Device
145151
tv = {1, 0};
146152
select(sock + 1, &fds, nullptr, nullptr, &tv);
147153
}
148-
Debug() << "UDP: ending thread.\n";
154+
Debug() << "UDP: ending thread.";
149155
}
150156

151157
void UDP::applySettings()

Source/Device/ZMQ.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ namespace Device {
110110
fifo.Pop();
111111
}
112112
else {
113-
Error() << "ZMQ: no signal." << std::endl;
113+
Error() << "ZMQ: no signal.";
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)