Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2026 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 46820 : DeviceCommand::DeviceCommand(QObject * const parent) : AbstractCommand(parent)
22 22335 : {
23 :
24 49285 : }
25 :
26 : /*!
27 : * Begins scanning for the Pokit device.
28 : */
29 140 : bool DeviceCommand::start()
30 52 : {
31 400 : qCInfo(lc).noquote() << ((deviceToScanFor.isNull())
32 262 : ? tr("Looking for first available Pokit device...")
33 378 : : tr(R"(Looking for device "%1"...)").arg(deviceToScanFor));
34 192 : discoveryAgent->start();
35 192 : return true;
36 52 : }
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 2310 : void DeviceCommand::disconnect(int exitCode)
43 858 : {
44 4290 : qCDebug(lc).noquote() << tr("Disconnecting Pokit device...");
45 858 : Q_ASSERT(device);
46 858 : Q_ASSERT(device->controller());
47 3168 : exitCodeOnDisconnect = exitCode;
48 3168 : device->controller()->disconnectFromDevice();
49 3168 : }
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 55500 : #define DOKIT_CLI_IF_LESS_THAN_RETURN(value, ns, label) \
61 55500 : if (value <= ns::maxValue(label)) { \
62 11433 : return label; \
63 11433 : }
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 910 : template<> PokitMeter::CurrentRange DeviceCommand::minRange<>(const quint32 maxValue)
90 1443 : {
91 2353 : if (maxValue == 0) return PokitMeter::CurrentRange::AutoRange;
92 2172 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_10mA)
93 1810 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_30mA)
94 1448 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_150mA)
95 905 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_300mA)
96 543 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::CurrentRange::_2A)
97 111 : return PokitMeter::CurrentRange::AutoRange;
98 333 : }
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 1330 : template<> PokitMeter::ResistanceRange DeviceCommand::minRange(const quint32 maxValue)
105 2109 : {
106 3439 : if (maxValue == 0) return PokitMeter::ResistanceRange::AutoRange;
107 3258 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_160)
108 2896 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_330)
109 2534 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_890)
110 2172 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_1K5)
111 1810 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_10K)
112 1448 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_100K)
113 1086 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_470K)
114 543 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::ResistanceRange::_1M)
115 111 : return PokitMeter::ResistanceRange::AutoRange;
116 333 : }
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 1050 : template<> PokitMeter::VoltageRange DeviceCommand::minRange(const quint32 maxValue)
123 1665 : {
124 2715 : if (maxValue == 0) return PokitMeter::VoltageRange::AutoRange;
125 2534 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_300mV)
126 2172 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_2V)
127 1810 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_6V)
128 1448 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_12V)
129 1086 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_30V)
130 543 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitMeter, PokitMeter::VoltageRange::_60V)
131 111 : return PokitMeter::VoltageRange::AutoRange;
132 333 : }
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 630 : template<> PokitPro::CapacitanceRange DeviceCommand::minRange(const quint32 maxValue)
139 999 : {
140 1629 : if (maxValue == 0) return PokitPro::CapacitanceRange::AutoRange;
141 1448 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CapacitanceRange::_100nF)
142 1086 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CapacitanceRange::_10uF)
143 724 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CapacitanceRange::_1mF)
144 111 : return PokitPro::CapacitanceRange::AutoRange;
145 444 : }
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 1190 : template<> PokitPro::CurrentRange DeviceCommand::minRange(const quint32 maxValue)
152 1887 : {
153 3077 : if (maxValue == 0) return PokitPro::CurrentRange::AutoRange;
154 2896 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_500uA)
155 2534 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_2mA)
156 2172 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_10mA)
157 1810 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_125mA)
158 1267 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_300mA)
159 905 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_3A)
160 543 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::CurrentRange::_10A)
161 111 : return PokitPro::CurrentRange::AutoRange;
162 333 : }
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 1750 : template<> PokitPro::ResistanceRange DeviceCommand::minRange(const quint32 maxValue)
169 2775 : {
170 4525 : if (maxValue == 0) return PokitPro::ResistanceRange::AutoRange;
171 4344 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_30)
172 3982 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_75)
173 3620 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_400)
174 3258 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_5K)
175 2896 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_10K)
176 2534 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_15K)
177 2172 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_40K)
178 1810 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_500K)
179 1267 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_700K)
180 905 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_1M)
181 543 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::ResistanceRange::_3M)
182 111 : return PokitPro::ResistanceRange::AutoRange;
183 333 : }
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 1330 : template<> PokitPro::VoltageRange DeviceCommand::minRange(const quint32 maxValue)
190 2109 : {
191 3439 : if (maxValue == 0) return PokitPro::VoltageRange::AutoRange;
192 3258 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_250mV)
193 2896 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_2V)
194 2534 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_10V)
195 2172 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_30V)
196 1810 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_60V)
197 1448 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_125V)
198 905 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_400V)
199 543 : DOKIT_CLI_IF_LESS_THAN_RETURN(maxValue, PokitPro, PokitPro::VoltageRange::_600V)
200 111 : return PokitPro::VoltageRange::AutoRange;
201 333 : }
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 70 : quint8 DeviceCommand::minCapacitanceRange(const PokitProduct product, const quint32 maxValue)
214 111 : {
215 181 : 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 181 : case PokitProduct::PokitPro:
220 181 : return +minRange<PokitPro::CapacitanceRange>(maxValue);
221 111 : }
222 51 : Q_ASSERT_X(false, "DeviceCommand::minCapacitanceRange", "Unknown PokitProduct enum value");
223 0 : return 255;
224 111 : }
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 140 : quint8 DeviceCommand::minCurrentRange(const PokitProduct product, const quint32 maxValue)
234 222 : {
235 362 : switch (product) {
236 181 : case PokitProduct::PokitMeter:
237 181 : return +minRange<PokitMeter::CurrentRange>(maxValue);
238 181 : case PokitProduct::PokitPro:
239 181 : return +minRange<PokitPro::CurrentRange>(maxValue);
240 222 : }
241 102 : Q_ASSERT_X(false, "DeviceCommand::minCurrentRange", "Unknown PokitProduct enum value");
242 0 : return 255;
243 222 : }
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 140 : quint8 DeviceCommand::minResistanceRange(const PokitProduct product, const quint32 maxValue)
253 222 : {
254 362 : switch (product) {
255 181 : case PokitProduct::PokitMeter:
256 181 : return +minRange<PokitMeter::ResistanceRange>(maxValue);
257 181 : case PokitProduct::PokitPro:
258 181 : return +minRange<PokitPro::ResistanceRange>(maxValue);
259 222 : }
260 102 : Q_ASSERT_X(false, "DeviceCommand::minResistanceRange", "Unknown PokitProduct enum value");
261 0 : return 255;
262 222 : }
263 :
264 : /*!
265 : * Returns the \a product's lowest voltage range that can measure at least up to \a maxValue (mV), or AutoRange if no
266 : * such range is available.
267 : *
268 : * \see DeviceCommand::minRange<PokitMeter::VoltageRange>(const quint32 maxValue)
269 : * \see minRange<PokitPro::VoltageRange>(const quint32 maxValue)
270 : */
271 140 : quint8 DeviceCommand::minVoltageRange(const PokitProduct product, const quint32 maxValue)
272 222 : {
273 362 : switch (product) {
274 181 : case PokitProduct::PokitMeter:
275 181 : return +minRange<PokitMeter::VoltageRange>(maxValue);
276 181 : case PokitProduct::PokitPro:
277 181 : return +minRange<PokitPro::VoltageRange>(maxValue);
278 222 : }
279 102 : Q_ASSERT_X(false, "DeviceCommand::minVoltageRange", "Unknown PokitProduct enum value");
280 0 : return 255;
281 222 : }
282 :
283 : #undef DOKIT_CLI_IF_LESS_THAN_RETURN
284 :
285 :
286 : /*!
287 : * Handles controller error events. This base implementation simply logs \a error and then exits
288 : * with `EXIT_FAILURE`. Derived classes may override this slot to implement their own error
289 : * handing if desired.
290 : */
291 140 : void DeviceCommand::controllerError(QLowEnergyController::Error error)
292 222 : {
293 776 : qCWarning(lc).noquote() << tr("Bluetooth controller error:") << error;
294 362 : QCoreApplication::exit(EXIT_FAILURE);
295 362 : }
296 :
297 : /*!
298 : * Handles device disconnection events. This base implementation simply logs and exits the
299 : * application (via QCoreApplication::exit) with the current exitCodeOnDisconnect value, which is
300 : * initialise to `EXIT_FAILURE` in the constructor, but should be set to `EXIT_SUCESS` if/when
301 : * the derived command class has completed its actions and requested the disconnection (as opposed
302 : * to a spontaneous disconnection on error).
303 : */
304 70 : void DeviceCommand::deviceDisconnected()
305 111 : {
306 215 : qCDebug(lc).noquote() << tr("Pokit device disconnected. Exiting with code %1.")
307 0 : .arg(exitCodeOnDisconnect);
308 181 : QCoreApplication::exit(exitCodeOnDisconnect);
309 181 : }
310 :
311 : /*!
312 : * Handles service error events. This base implementation simply logs \a error and then exits
313 : * with `EXIT_FAILURE`. Derived classes may override this slot to implement their own error
314 : * handing if desired.
315 : *
316 : * \note As this base class does not construct services (derived classed do), its up to the derived
317 : * classed to connect this slot to the relevant service's error signal if desired.
318 : */
319 140 : void DeviceCommand::serviceError(const QLowEnergyService::ServiceError error)
320 222 : {
321 776 : qCWarning(lc).noquote() << tr("Bluetooth service error:") << error;
322 362 : QCoreApplication::exit(EXIT_FAILURE);
323 362 : }
324 :
325 : /*!
326 : * Handles service detail discovery events. This base implementation simply logs the event, and
327 : * nothing more. Derived classes may (usually do) override this slot to provide their own processing
328 : * when a services details have been discovered.
329 : */
330 1820 : void DeviceCommand::serviceDetailsDiscovered()
331 761 : {
332 3465 : qCDebug(lc).noquote() << tr("Service details discovered.");
333 2581 : }
334 :
335 : /*!
336 : * Checks if \a info is the device (if any) we're looking for, and if so, create a controller and
337 : * service, and begins connecting to the device.
338 : */
339 70 : void DeviceCommand::deviceDiscovered(const QBluetoothDeviceInfo &info)
340 111 : {
341 111 : Q_ASSERT(isPokitProduct(info));
342 :
343 181 : if (device) {
344 0 : qCDebug(lc).noquote() << tr(R"(Ignoring additional Pokit device "%1" (%2) at (%3).)")
345 0 : .arg(info.name(), info.deviceUuid().toString(), info.address().toString());
346 0 : return;
347 0 : }
348 :
349 323 : if ((deviceToScanFor.isEmpty()) || (deviceToScanFor == info.name()) ||
350 392 : ((!info.address().isNull()) && (info.address() == QBluetoothAddress(deviceToScanFor))) ||
351 251 : ((!info.deviceUuid().isNull()) && (info.deviceUuid() == QBluetoothUuid(deviceToScanFor))))
352 0 : {
353 0 : qCDebug(lc).noquote() << tr(R"(Found Pokit device "%1" (%2) at (%3).)")
354 0 : .arg(info.name(), info.deviceUuid().toString(), info.address().toString());
355 0 : discoveryAgent->stop();
356 :
357 0 : device = new PokitDevice(info, this);
358 0 : connect(device->controller(), &QLowEnergyController::disconnected,
359 0 : this, &DeviceCommand::deviceDisconnected);
360 0 : connect(device->controller(),
361 0 : #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
362 0 : QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
363 : #else
364 0 : &QLowEnergyController::errorOccurred,
365 0 : #endif
366 0 : this, &DeviceCommand::controllerError, Qt::QueuedConnection);
367 :
368 0 : AbstractPokitService * const service = getService();
369 0 : service->setPokitProduct(pokitProduct(info));
370 :
371 0 : Q_ASSERT(service);
372 0 : connect(service, &AbstractPokitService::serviceDetailsDiscovered,
373 0 : this, &DeviceCommand::serviceDetailsDiscovered);
374 0 : connect(service, &AbstractPokitService::serviceErrorOccurred,
375 0 : this, &DeviceCommand::serviceError);
376 :
377 0 : qCDebug(lc).noquote() << tr(R"(Connecting to %1 device "%2" (%3) at (%4).)").arg(
378 0 : toString(*service->pokitProduct()), info.name(), info.deviceUuid().toString(), info.address().toString());
379 0 : device->controller()->connectToDevice();
380 0 : return;
381 0 : }
382 :
383 215 : qCDebug(lc).noquote() << tr(R"(Ignoring non-matching Pokit device "%1" (%2) at (%3).)")
384 0 : .arg(info.name(), info.deviceUuid().toString(), info.address().toString());
385 119 : return;
386 111 : }
387 :
388 : /*!
389 : * Checks that the requested device was discovered, and if not, reports and error and exits.
390 : */
391 140 : void DeviceCommand::deviceDiscoveryFinished()
392 222 : {
393 362 : if (!device) {
394 570 : qCWarning(lc).noquote() << ((deviceToScanFor.isNull())
395 432 : ? tr("Failed to find any Pokit device.")
396 548 : : tr(R"(Failed to find device "%1".)").arg(deviceToScanFor));
397 260 : QCoreApplication::exit(EXIT_FAILURE);
398 222 : }
399 362 : }
|