-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBladeRfDevicesManager.cpp
More file actions
executable file
·179 lines (146 loc) · 5.63 KB
/
BladeRfDevicesManager.cpp
File metadata and controls
executable file
·179 lines (146 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <QThread>
#include "BladeRfDeviceController.hpp"
#include "BladeRfDevicesManager.hpp"
#define DetermineDeviceFromCall const auto device = qobject_cast<BladeRfDeviceController*>(sender());
#define DeviceCall(device, command) QMetaObject::invokeMethod(device, &BladeRfDeviceController::command, Qt::QueuedConnection);
#define DeviceCallArgs(device, command, argsType, args) QMetaObject::invokeMethod(device, command, Qt::QueuedConnection, Q_ARG(argsType, args));
BladeRfDevicesManager::BladeRfDevicesManager(QObject* parent)
: QObject(parent)
{
}
BladeRfDevicesManager::~BladeRfDevicesManager()
{
for (auto device : qAsConst(mDevices))
{
device.handle->sessionStop();
device.handle->thread()->quit();
device.handle->thread()->wait();
}
}
QList<BladeRfDeviceController*> BladeRfDevicesManager::devices() const
{
QList<BladeRfDeviceController*> result;
for (const auto& holder : mDevices)
result.append(holder.handle);
return result;
}
BladeRfDeviceController* BladeRfDevicesManager::device(SdrDeviceType type) const
{
for (const auto& holder : mDevices)
if (holder.handle->sdrDeviceType() == type) return holder.handle;
return nullptr;
}
void BladeRfDevicesManager::init()
{
const auto buffer = Application()->buffer();
bladerf_devinfo* deviceList = nullptr;
mDevicesCount = bladerf_get_device_list(&deviceList);
if (mDevicesCount < 0)
{
qWarning("No bladeRF devices found!");
return;
}
else qDebug("%i devices found", mDevicesCount);
for (int i = 0; i < mDevicesCount; ++i)
{
auto device = new BladeRfDeviceController;
auto thread = new QThread(this);
connect(thread, &QThread::finished,
device, &BladeRfDeviceController::deleteLater);
connect(device, &BladeRfDeviceController::opened,
this, &BladeRfDevicesManager::onDeviceOpened,
Qt::QueuedConnection);
connect(device, &BladeRfDeviceController::closed,
this, &BladeRfDevicesManager::onDeviceClosed,
Qt::QueuedConnection);
connect(device, &BladeRfDeviceController::errorOccured,
this, &BladeRfDevicesManager::onDeviceError,
Qt::QueuedConnection);
connect(device, &BladeRfDeviceController::sessionStarted,
this, &BladeRfDevicesManager::onCaptureStarted,
Qt::QueuedConnection);
connect(device, &BladeRfDeviceController::sessionStopped,
this, &BladeRfDevicesManager::onCaptureStopped,
Qt::QueuedConnection);
connect(device, &BladeRfDeviceController::rxDataAvailable,
buffer.get(), &RxSignalDataBuffer::onCaptureAvailable,
Qt::QueuedConnection);
connect(device, &BladeRfDeviceController::sessionStarted,
buffer.get(), &RxSignalDataBuffer::onSessionStateChanged,
Qt::QueuedConnection);
device->moveToThread(thread);
thread->setObjectName(deviceList[i].serial);
thread->start();
DeviceCallArgs(device, "deviceOpen", bladerf_devinfo, deviceList[i]);
}
bladerf_free_device_list(deviceList);
}
void BladeRfDevicesManager::startCapture(const SessionRadioSettings& config)
{
for (const auto& device : qAsConst(mDevices))
DeviceCallArgs(device.handle, "sessionStart", SessionRadioSettings, config);
}
void BladeRfDevicesManager::stopCapture()
{
for (const auto& device : qAsConst(mDevices))
DeviceCall(device.handle, sessionStop);
}
void BladeRfDevicesManager::onDeviceOpened()
{
DetermineDeviceFromCall;
const auto type = device->sdrDeviceType();
device->printAboutDevice();
mDevices.insert(type, Device(device));
device->thread()->setObjectName(sdrDeviceTypeToUiTag(type));
if (device->sdrDeviceType() == SdrDeviceType::Unknown)
{
qWarning("Unknown device: %s", qPrintable(device->sdrDeviceSerial()));
emit errorOccured();
}
else if (mDevices.count() == mDevicesCount)
{
if (auto handle = mDevices[SdrDeviceType::FirstDirection].handle; handle)
{
handle->enableClockBroadcast();
if (handle = mDevices[SdrDeviceType::SecondDirection].handle; handle)
{
handle->enableClockListening();
handle->enableClockBroadcast();
if (handle = mDevices[SdrDeviceType::ThirdDirection].handle; handle)
{
handle->enableClockListening();
emit initialized();
}
}
}
}
}
void BladeRfDevicesManager::onDeviceClosed()
{
DetermineDeviceFromCall;
mDevices.insert(device->sdrDeviceType(), Device(device));
if (mDevices.count() == 0) emit deinitialized();
}
void BladeRfDevicesManager::onDeviceError()
{
emit errorOccured();
}
void BladeRfDevicesManager::onCaptureStarted()
{
DetermineDeviceFromCall;
mDevices[device->sdrDeviceType()].captureState = true;
for (const auto& device : qAsConst(mDevices))
if (not device.captureState) return;
std::this_thread::sleep_for(std::chrono::seconds(1)); // needs to fire after all streams starts and must be in this thread to block
qDebug("Trigger fire");
mDevices[SdrDeviceType::FirstDirection].handle->triggerFire();
emit started();
}
void BladeRfDevicesManager::onCaptureStopped()
{
DetermineDeviceFromCall;
mDevices[device->sdrDeviceType()].captureState = false;
for (const auto& device : qAsConst(mDevices))
if (device.captureState) return;
emit stopped();
}