Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
2 : // SPDX-License-Identifier: LGPL-3.0-or-later
3 :
4 : #include "devicecommand.h"
5 :
6 : #include <qtpokit/abstractpokitservice.h>
7 : #include <qtpokit/pokitdevice.h>
8 : #include <qtpokit/pokitdiscoveryagent.h>
9 : #include "qtpokit/pokitmeter.h"
10 : #include "qtpokit/pokitpro.h"
11 :
12 : /*!
13 : * \class DeviceCommand
14 : *
15 : * The AbstractCommand class extends AbstractCommand to add a PokitDevice instance.
16 : */
17 :
18 : /*!
19 : * Construct a new DeviceCommand object with \a parent.
20 : */
21 20556 : DeviceCommand::DeviceCommand(QObject * const parent) : AbstractCommand(parent)
22 10160 : {
23 :
24 24616 : }
25 :
26 : /*!
27 : * Begins scanning for the Pokit device.
28 : */
29 76 : bool DeviceCommand::start()
30 32 : {
31 200 : qCInfo(lc).noquote() << ((deviceToScanFor.isNull())
32 154 : ? tr("Looking for first available Pokit device...")
33 199 : : tr(R"(Looking for device "%1"...)").arg(deviceToScanFor));
34 108 : discoveryAgent->start();
35 108 : return true;
36 32 : }
37 :
38 : /*!
39 : * Disconnects the underlying Pokit device, and sets \a exitCode to be return to the OS once the
40 : * disconnection has taken place.
41 : */
42 1080 : void DeviceCommand::disconnect(int exitCode)
43 468 : {
44 1812 : qCDebug(lc).noquote() << tr("Disconnecting Pokit device...");
45 468 : Q_ASSERT(device);
46 468 : Q_ASSERT(device->controller());
47 1548 : exitCodeOnDisconnect = exitCode;
48 1548 : device->controller()->disconnectFromDevice();
49 1548 : }
50 :
51 : /*!
52 : * \fn virtual AbstractPokitService * DeviceCommand::getService() = 0
53 : *
54 : * Returns a Pokit service object for the derived command class. This will be called by
55 : * deviceDiscovered() when the requested Pokit device has been found, after which
56 : * deviceDiscovered() will connect the returned service's common signals, and kick off the
57 : * device's connection process.
58 : */
59 :
60 22000 : #define DOKIT_CLI_IF_LESS_THAN_RETURN(value, ns, label) \
61 22000 : if (value <= ns::maxValue(label).toUInt()) { \
62 4532 : return label; \
63 4532 : }
64 :
65 : /**
66 : * \fn template<typename T> static T DeviceCommand::minRange(const quint32 maxValue)
67 : *
68 : * Returns the lowest \a T range that can measure at least up to \a maxValue, or AutoRange if no such range is
69 : * available.
70 : *
71 : * \tparam T Range enumerator to evaluate ranges for. Must be one of:
72 : * * PokitMeter::CurrentRange
73 : * * PokitMeter::ResistanceRange
74 : * * PokitMeter::VoltageRange
75 : * * PokitPro::CapacitanceRange
76 : * * PokitPro::CurrentRange
77 : * * PokitPro::ResistanceRange
78 : * * PokitPro::VoltageRange
79 : *
80 : * \cond Doxygen has "only very limited support for member specialization at the moment", so hide these from Doxygen.
81 : * Specifically, if we don't hide them, then Doxygen (at least the current version: v1.9.6) sees the following
82 : * specialisations as new, public, non-static members.
83 : */
84 :
85 : /*!
86 : * Returns the lowest PokitMeter::CurrentRange value that can measure at least up to \a maxValue, or AutoRange if
87 : * the Pokit Meter cannot measure as high as \a maxValue.
88 : */
89 494 : template<> PokitMeter::CurrentRange DeviceCommand::minRange<>(const quint32 maxValue)
90 572 : {
91 1066 : if (maxValue == 0) return PokitMeter::CurrentRange::AutoRange;
92 984 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_10mA)
93 820 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_30mA)
94 656 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_150mA)
95 410 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_300mA)
96 246 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_2A)
97 44 : return PokitMeter::CurrentRange::AutoRange;
98 132 : }
99 :
100 : /*!
101 : * Returns the lowest PokitMeter::ResistanceRange value that can measure at least up to \a maxValue, or AutoRange if
102 : * the Pokit Meter cannot measure as high as \a maxValue.
103 : */
104 722 : template<> PokitMeter::ResistanceRange DeviceCommand::minRange(const quint32 maxValue)
105 836 : {
106 1558 : if (maxValue == 0) return PokitMeter::ResistanceRange::AutoRange;
107 1476 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_160)
108 1312 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_330)
109 1148 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_890)
110 984 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_1K5)
111 820 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_10K)
112 656 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_100K)
113 492 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_470K)
114 246 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_1M)
115 44 : return PokitMeter::ResistanceRange::AutoRange;
116 132 : }
117 :
118 : /*!
119 : * Returns the lowest PokitMeter::VoltageRange value that can measure at least up to \a maxValue, or AutoRange if
120 : * the Pokit Meter cannot measure as high as \a maxValue.
121 : */
122 570 : template<> PokitMeter::VoltageRange DeviceCommand::minRange(const quint32 maxValue)
123 660 : {
124 1230 : if (maxValue == 0) return PokitMeter::VoltageRange::AutoRange;
125 1148 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_300mV)
126 984 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_2V)
127 820 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_6V)
128 656 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_12V)
129 492 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_30V)
130 246 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_60V)
131 44 : return PokitMeter::VoltageRange::AutoRange;
132 132 : }
133 :
134 : /*!
135 : * Returns the lowest PokitPro::CapacitanceRange value that can measure at least up to \a maxValue, or AutoRange if
136 : * the Pokit Pro cannot measure as high as \a maxValue.
137 : */
138 342 : template<> PokitPro::CapacitanceRange DeviceCommand::minRange(const quint32 maxValue)
139 396 : {
140 738 : if (maxValue == 0) return PokitPro::CapacitanceRange::AutoRange;
141 656 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CapacitanceRange::_100nF)
142 492 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CapacitanceRange::_10uF)
143 328 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CapacitanceRange::_1mF)
144 44 : return PokitPro::CapacitanceRange::AutoRange;
145 176 : }
146 :
147 : /*!
148 : * Returns the lowest PokitPro::CurrentRange value that can measure at least up to \a maxValue, or AutoRange if
149 : * the Pokit Pro cannot measure as high as \a maxValue.
150 : */
151 646 : template<> PokitPro::CurrentRange DeviceCommand::minRange(const quint32 maxValue)
152 748 : {
153 1394 : if (maxValue == 0) return PokitPro::CurrentRange::AutoRange;
154 1312 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_500uA)
155 1148 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_2mA)
156 984 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_10mA)
157 820 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_125mA)
158 574 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_300mA)
159 410 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_3A)
160 246 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_10A)
161 44 : return PokitPro::CurrentRange::AutoRange;
162 132 : }
163 :
164 : /*!
165 : * Returns the lowest PokitPro::ResistanceRange value that can measure at least up to \a maxValue, or AutoRange if
166 : * the Pokit Pro cannot measure as high as \a maxValue.
167 : */
168 950 : template<> PokitPro::ResistanceRange DeviceCommand::minRange(const quint32 maxValue)
169 1100 : {
170 2050 : if (maxValue == 0) return PokitPro::ResistanceRange::AutoRange;
171 1968 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_30)
172 1804 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_75)
173 1640 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_400)
174 1476 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_5K)
175 1312 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_10K)
176 1148 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_15K)
177 984 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_40K)
178 820 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_500K)
179 574 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_700K)
180 410 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_1M)
181 246 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_3M)
182 44 : return PokitPro::ResistanceRange::AutoRange;
183 132 : }
184 :
185 : /*!
186 : * Returns the lowest PokitPro::VoltageRange value that can measure at least up to \a maxValue, or AutoRange if
187 : * the Pokit Pro cannot measure as high as \a maxValue.
188 : */
189 722 : template<> PokitPro::VoltageRange DeviceCommand::minRange(const quint32 maxValue)
190 836 : {
191 1558 : if (maxValue == 0) return PokitPro::VoltageRange::AutoRange;
192 1476 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_250mV)
193 1312 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_2V)
194 1148 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_10V)
195 984 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_30V)
196 820 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_60V)
197 656 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_125V)
198 410 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_400V)
199 246 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_600V)
200 44 : return PokitPro::VoltageRange::AutoRange;
201 132 : }
202 :
203 : /// \endcond
204 :
205 : /*!
206 : * Returns the \a product's lowest capacitance range that can measure at least up to \a maxValue (nF), or AutoRange if
207 : * no such range is available.
208 : *
209 : * \note Since Pokit Meters do not support capacitance measurement, \a product should not be PokitProduct::PokitMeter.
210 : *
211 : * \see minRange<PokitPro::CapacitanceRange>
212 : */
213 38 : quint8 DeviceCommand::minCapacitanceRange(const PokitProduct product, const quint32 maxValue)
214 44 : {
215 82 : switch (product) {
216 0 : case PokitProduct::PokitMeter:
217 0 : Q_ASSERT_X(false, "DeviceCommand::minCapacitanceRange", "Pokit Meter has no capacitance support.");
218 0 : return 255;
219 82 : case PokitProduct::PokitPro:
220 82 : return +minRange<PokitPro::CapacitanceRange>(maxValue);
221 44 : }
222 0 : Q_ASSERT_X(false, "DeviceCommand::minCapacitanceRange", "Unknown PokitProduct enum value");
223 0 : return 255;
224 44 : }
225 :
226 : /*!
227 : * Returns the \a product's lowest current range that can measure at least up to \a maxValue (µA), or AutoRange if no
228 : * such range is available.
229 : *
230 : * \see DeviceCommand::minRange<PokitMeter::CurrentRange>(const quint32 maxValue)
231 : * \see minRange<PokitPro::CurrentRange>(const quint32 maxValue)
232 : */
233 76 : quint8 DeviceCommand::minCurrentRange(const PokitProduct product, const quint32 maxValue)
234 88 : {
235 164 : switch (product) {
236 82 : case PokitProduct::PokitMeter:
237 82 : return +minRange<PokitMeter::CurrentRange>(maxValue);
238 82 : case PokitProduct::PokitPro:
239 82 : return +minRange<PokitPro::CurrentRange>(maxValue);
240 88 : }
241 0 : Q_ASSERT_X(false, "DeviceCommand::minCurrentRange", "Unknown PokitProduct enum value");
242 0 : return 255;
243 88 : }
244 :
245 : /*!
246 : * Returns the \a product's lowest resistance range that can measure at least up to \a maxValue (Ω), or AutoRange if no
247 : * such range is available.
248 : *
249 : * \see DeviceCommand::minRange<PokitMeter::ResistanceRange>(const quint32 maxValue)
250 : * \see minRange<PokitPro::ResistanceRange>(const quint32 maxValue)
251 : */
252 76 : quint8 DeviceCommand::minResistanceRange(const PokitProduct product, const quint32 maxValue)
253 88 : {
254 164 : switch (product) {
255 82 : case PokitProduct::PokitMeter:
256 82 : return +minRange<PokitMeter::ResistanceRange>(maxValue);
257 82 : case PokitProduct::PokitPro:
258 82 : return +minRange<PokitPro::ResistanceRange>(maxValue);
259 88 : }
260 0 : Q_ASSERT_X(false, "DeviceCommand::minResistanceRange", "Unknown PokitProduct enum value");
261 0 : return 255;
262 88 : }
263 :
264 : /*!
265 : t
266 : * Returns the \a product's lowest voltage range that can measure at least up to \a maxValue (mV), or AutoRange if no
267 : * such range is available.
268 : *
269 : * \see DeviceCommand::minRange<PokitMeter::VoltageRange>(const quint32 maxValue)
270 : * \see minRange<PokitPro::VoltageRange>(const quint32 maxValue)
271 : */
272 76 : quint8 DeviceCommand::minVoltageRange(const PokitProduct product, const quint32 maxValue)
273 88 : {
274 164 : switch (product) {
275 82 : case PokitProduct::PokitMeter:
276 82 : return +minRange<PokitMeter::VoltageRange>(maxValue);
277 82 : case PokitProduct::PokitPro:
278 82 : return +minRange<PokitPro::VoltageRange>(maxValue);
279 88 : }
280 0 : Q_ASSERT_X(false, "DeviceCommand::minVoltageRange", "Unknown PokitProduct enum value");
281 0 : return 255;
282 88 : }
283 :
284 : #undef DOKIT_CLI_IF_LESS_THAN_RETURN
285 :
286 :
287 : /*!
288 : * Handles controller error events. This base implementation simply logs \a error and then exits
289 : * with `EXIT_FAILURE`. Derived classes may override this slot to implement their own error
290 : * handing if desired.
291 : */
292 76 : void DeviceCommand::controllerError(QLowEnergyController::Error error)
293 88 : {
294 362 : qCWarning(lc).noquote() << tr("Bluetooth controller error:") << error;
295 164 : QCoreApplication::exit(EXIT_FAILURE);
296 164 : }
297 :
298 : /*!
299 : * Handles devics disconnection events. This base implementation simply logs and exits the
300 : * application (via QCoreApplication::exit) with the current exitCodeOnDisconnect value, which is
301 : * initialise to `EXIT_FAILURE` in the constructor, but should be set to `EXIT_SUCESS` if/when
302 : * the derived command class has completed its actions and requested the disconnection (as opposed
303 : * to a spontaneous disconnection on error).
304 : */
305 38 : void DeviceCommand::deviceDisconnected()
306 44 : {
307 90 : qCDebug(lc).noquote() << tr("Pokit device disconnected. Exiting with code %1.")
308 0 : .arg(exitCodeOnDisconnect);
309 82 : QCoreApplication::exit(exitCodeOnDisconnect);
310 82 : }
311 :
312 : /*!
313 : * Handles service error events. This base implementation simply logs \a error and then exits
314 : * with `EXIT_FAILURE`. Derived classes may override this slot to implement their own error
315 : * handing if desired.
316 : *
317 : * \note As this base class does not construct services (derived classed do), its up to the derived
318 : * classed to connect this slot to the relevant service's error signal if desired.
319 : */
320 76 : void DeviceCommand::serviceError(const QLowEnergyService::ServiceError error)
321 88 : {
322 362 : qCWarning(lc).noquote() << tr("Bluetooth service error:") << error;
323 164 : QCoreApplication::exit(EXIT_FAILURE);
324 164 : }
325 :
326 : /*!
327 : * Handles service detail discovery events. This base implementation simply logs the event, and
328 : * nothing more. Derived classes may (usually do) override this slot to provide their own processing
329 : * when a services details have been discovered.
330 : */
331 814 : void DeviceCommand::serviceDetailsDiscovered()
332 384 : {
333 1406 : qCDebug(lc).noquote() << tr("Service details discovered.");
334 1198 : }
335 :
336 : /*!
337 : * Checks if \a info is the device (if any) we're looking for, and if so, create a contoller and
338 : * service, and begins connecting to the device.
339 : */
340 38 : void DeviceCommand::deviceDiscovered(const QBluetoothDeviceInfo &info)
341 44 : {
342 44 : Q_ASSERT(isPokitProduct(info));
343 :
344 82 : if (device) {
345 0 : qCDebug(lc).noquote() << tr(R"(Ignoring additional Pokit device "%1" (%2) at (%3).)")
346 0 : .arg(info.name(), info.deviceUuid().toString(), info.address().toString());
347 0 : return;
348 0 : }
349 :
350 158 : if ((deviceToScanFor.isEmpty()) || (deviceToScanFor == info.name()) ||
351 202 : ((!info.address().isNull()) && (info.address() == QBluetoothAddress(deviceToScanFor))) ||
352 120 : ((!info.deviceUuid().isNull()) && (info.deviceUuid() == QBluetoothUuid(deviceToScanFor))))
353 0 : {
354 0 : qCDebug(lc).noquote() << tr(R"(Found Pokit device "%1" (%2) at (%3).)")
355 0 : .arg(info.name(), info.deviceUuid().toString(), info.address().toString());
356 0 : discoveryAgent->stop();
357 :
358 0 : device = new PokitDevice(info, this);
359 0 : connect(device->controller(), &QLowEnergyController::disconnected,
360 0 : this, &DeviceCommand::deviceDisconnected);
361 0 : connect(device->controller(),
362 0 : #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
363 0 : QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
364 : #else
365 0 : &QLowEnergyController::errorOccurred,
366 0 : #endif
367 0 : this, &DeviceCommand::controllerError, Qt::QueuedConnection);
368 :
369 0 : AbstractPokitService * const service = getService();
370 0 : service->setPokitProduct(pokitProduct(info));
371 :
372 0 : Q_ASSERT(service);
373 0 : connect(service, &AbstractPokitService::serviceDetailsDiscovered,
374 0 : this, &DeviceCommand::serviceDetailsDiscovered);
375 0 : connect(service, &AbstractPokitService::serviceErrorOccurred,
376 0 : this, &DeviceCommand::serviceError);
377 :
378 0 : qCDebug(lc).noquote() << tr(R"(Connecting to %1 device "%2" (%3) at (%4).)").arg(
379 0 : toString(*service->pokitProduct()), info.name(), info.deviceUuid().toString(), info.address().toString());
380 0 : device->controller()->connectToDevice();
381 0 : return;
382 0 : }
383 :
384 90 : qCDebug(lc).noquote() << tr(R"(Ignoring non-matching Pokit device "%1" (%2) at (%3).)")
385 0 : .arg(info.name(), info.deviceUuid().toString(), info.address().toString());
386 49 : return;
387 44 : }
388 :
389 : /*!
390 : * Checks that the requested device was discovered, and if not, reports and error and exits.
391 : */
392 76 : void DeviceCommand::deviceDiscoveryFinished()
393 88 : {
394 164 : if (!device) {
395 256 : qCWarning(lc).noquote() << ((deviceToScanFor.isNull())
396 210 : ? tr("Failed to find any Pokit device.")
397 255 : : tr(R"(Failed to find device "%1".)").arg(deviceToScanFor));
398 164 : QCoreApplication::exit(EXIT_FAILURE);
399 88 : }
400 164 : }
|