Dokit
Internal development documentation
Loading...
Searching...
No Matches
deviceinfoservice.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4/*!
5 * \file
6 * Defines the DeviceInfoService and DeviceInfoServicePrivate classes.
7 */
8
10#include "deviceinfoservice_p.h"
11
12#include <QtEndian>
13
14/*!
15 * \class DeviceInfoService
16 *
17 * The DeviceInfoService class accesses the `Device Info` service of Pokit devices.
18 */
19
20/*!
21 * Constructs a new Pokit service with \a parent.
22 */
24 : AbstractPokitService(new DeviceInfoServicePrivate(controller, this), parent)
25{
26
27}
28
29/*!
30 * \cond internal
31 * Constructs a new Pokit service with \a parent, and private implementation \a d.
32 */
34 DeviceInfoServicePrivate * const d, QObject * const parent)
35 : AbstractPokitService(d, parent)
36{
37
38}
39/// \endcond
40
42{
43 const bool r1 = readFirmwareRevisionCharacteristic();
44 const bool r2 = readHardwareRevisionCharacteristic();
45 const bool r3 = readSoftwareRevisionCharacteristic();
46 const bool r4 = readManufacturerCharacteristics();
47 const bool r5 = readModelNumberCharacteristic();
48 const bool r6 = ((service() != nullptr) && (service()->characteristic(CharacteristicUuids::serialNumber).isValid()))
50 return (r1 && r2 && r3 && r4 && r5 && r6);
51}
52
53/*!
54 * Read the `Device Info` service's `Firmware Revision` characteristic.
55 *
56 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
57 * underlying controller it not yet connected to the Pokit device, or the device's services have
58 * not yet been discovered).
59 *
60 * Emits firmwareRevisionRead() if/when the characteristic has been read successfully.
61 */
67
68/*!
69 * Read the `Device Info` service's `Hardware Revision` characteristic.
70 *
71 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
72 * underlying controller it not yet connected to the Pokit device, or the device's services have
73 * not yet been discovered).
74 *
75 * Emits hardwareRevisionRead() if/when the characteristic has been read successfully.
76 */
82
83/*!
84 * Read the `Device Info` service's `Manufacturer Name` characteristic.
85 *
86 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
87 * underlying controller it not yet connected to the Pokit device, or the device's services have
88 * not yet been discovered).
89 *
90 * Emits manufacturerNameRead() if/when the characteristic has been read successfully.
91 */
97
98/*!
99 * Read the `Device Info` service's `Model Number` characteristic.
100 *
101 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
102 * underlying controller it not yet connected to the Pokit device, or the device's services have
103 * not yet been discovered).
104 *
105 * Emits modelNumberRead() if/when the characteristic has been read successfully.
106 */
112
113/*!
114 * Read the `Device Info` service's `Software Revision` characteristic.
115 *
116 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
117 * underlying controller it not yet connected to the Pokit device, or the device's services have
118 * not yet been discovered).
119 *
120 * Emits softwareRevisionRead() if/when the characteristic has been read successfully.
121 */
127
128/*!
129 * Read the `Device Info` service's (undocumented) `Serial Number` characteristic.
130 *
131 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
132 * underlying controller it not yet connected to the Pokit device, or the device's services have
133 * not yet been discovered).
134 *
135 * Emits serialNumberRead() if/when the characteristic has been read successfully.
136 */
142
143/*!
144 * Returns the most recent value of the `Device Info` service's `Manufacturer Name` characteristic.
145 *
146 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
147 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
148 * null QString is returned.
149 */
151{
152 Q_D(const DeviceInfoService);
153 const QLowEnergyCharacteristic characteristic =
154 d->getCharacteristic(CharacteristicUuids::manufacturerName);
155 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
156}
157
158/*!
159 * Returns the most recent value of the `Device Info` service's `Model Number` characteristic.
160 *
161 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
162 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
163 * null QString is returned.
164 */
166{
167 Q_D(const DeviceInfoService);
168 const QLowEnergyCharacteristic characteristic =
169 d->getCharacteristic(CharacteristicUuids::modelNumber);
170 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
171}
172
173/*!
174 * Returns the most recent value of the `Device Info` service's `Hardware Revision` characteristic.
175 *
176 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
177 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
178 * null QString is returned.
179 */
181{
182 Q_D(const DeviceInfoService);
183 const QLowEnergyCharacteristic characteristic =
184 d->getCharacteristic(CharacteristicUuids::hardwareRevision);
185 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
186}
187
188/*!
189 * Returns the most recent value of the `Device Info` service's `Firmware Revision` characteristic.
190 *
191 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
192 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
193 * null QString is returned.
194 */
196{
197 Q_D(const DeviceInfoService);
198 const QLowEnergyCharacteristic characteristic =
199 d->getCharacteristic(CharacteristicUuids::firmwareRevision);
200 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
201}
202
203/*!
204 * Returns the most recent value of the `Device Info` service's `Software Revision` characteristic.
205 *
206 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
207 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
208 * null QString is returned.
209 */
211{
212 Q_D(const DeviceInfoService);
213 const QLowEnergyCharacteristic characteristic =
214 d->getCharacteristic(CharacteristicUuids::softwareRevision);
215 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
216}
217
218/*!
219 * Returns the most recent value of the `Device Info` service's (undocumented) `Serial Number`
220 * characteristic.
221 *
222 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
223 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
224 * null QString is returned.
225 */
227{
228 Q_D(const DeviceInfoService);
229 const QLowEnergyCharacteristic characteristic =
230 d->getCharacteristic(CharacteristicUuids::serialNumber);
231 /*!
232 * \cond internal
233 * \pokitApi Unlike other string characteristics, Pokit (Pro) devices always appear to add a trailing
234 * `null` byte to serial number strings. So here we strip any that are present.
235 * \endcond
236 */
237 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()).remove(QLatin1Char('\0')) : QString();
238}
239
240/*!
241 * \fn DeviceInfoService::manufacturerRead
242 *
243 * This signal is emitted when the `Manufacturer Name` characteristic has been read successfully.
244 *
245 * \see readManufacturerCharacteristic
246 * \see manufacturer
247 */
248
249/*!
250 * \fn DeviceInfoService::modelNumberRead
251 *
252 * This signal is emitted when the `Model Number` characteristic has been read successfully.
253 *
254 * \see readModelNumberCharacteristic
255 * \see modelNumber
256 */
257
258/*!
259 * \fn DeviceInfoService::hardwareRevisionRead
260 *
261 * This signal is emitted when the `Hardware Revision` characteristic has been read successfully.
262 *
263 * \see readHardwareRevisionCharacteristic
264 * \see hardwareRevision
265 */
266
267/*!
268 * \fn DeviceInfoService::firmwareRevisionRead
269 *
270 * This signal is emitted when the `Firmware Revision` characteristic has been read successfully.
271 *
272 * \see readFirmwareRevisionCharacteristic
273 * \see firmwareRevision
274 */
275
276/*!
277 * \fn DeviceInfoService::softwareRevisionRead
278 *
279 * This signal is emitted when the `Software Revision` characteristic has been read successfully.
280 *
281 * \see readSoftwareRevisionCharacteristic
282 * \see softwareRevision
283 */
284
285/*!
286 * \fn DeviceInfoService::serialNumberRead
287 *
288 * This signal is emitted when the `Serial Number` characteristic has been read successfully.
289 *
290 * \see readSerialNumberCharacteristic
291 * \see serialNumber
292 */
293
294/*!
295 * \cond internal
296 * \class DeviceInfoServicePrivate
297 *
298 * The DeviceInfoServicePrivate class provides private implementation for DeviceInfoService.
299 */
300
301/*!
302 * \internal
303 * Constructs a new DeviceInfoServicePrivate object with public implementation \a q.
304 */
311
312/*!
313 * Implements AbstractPokitServicePrivate::characteristicRead to parse \a value, then emit a
314 * specialised signal, for each supported \a characteristic.
315 */
317 const QByteArray &value)
318{
320
323 const QString name = QString::fromUtf8(value);
324 qCDebug(lc).noquote() << tr(R"(Manufacturer name: "%1")").arg(name);
325 Q_EMIT q->manufacturerRead(name);
326 return;
327 }
328
330 const QString model = QString::fromUtf8(value);
331 qCDebug(lc).noquote() << tr(R"(Model number: "%1")").arg(model);
332 Q_EMIT q->modelNumberRead(model);
333 return;
334 }
335
337 const QString revision = QString::fromUtf8(value);
338 qCDebug(lc).noquote() << tr(R"(Hardware revision: "%1")").arg(revision);
339 Q_EMIT q->hardwareRevisionRead(revision);
340 return;
341 }
342
344 const QString revision = QString::fromUtf8(value);
345 qCDebug(lc).noquote() << tr(R"(Firmware revision: "%1")").arg(revision);
346 Q_EMIT q->firmwareRevisionRead(revision);
347 return;
348 }
349
351 const QString revision = QString::fromUtf8(value);
352 qCDebug(lc).noquote() << tr(R"(Software revision: "%1")").arg(revision);
353 Q_EMIT q->softwareRevisionRead(revision);
354 return;
355 }
356
358 const QString serialNumber = QString::fromUtf8(value);
359 qCDebug(lc).noquote() << tr(R"(Serial number: "%1")").arg(serialNumber);
360 Q_EMIT q->serialNumberRead(serialNumber);
361 return;
362 }
363
364 qCWarning(lc).noquote() << tr("Unknown characteristic read for Device Info service")
365 << serviceUuid << characteristic.name() << characteristic.uuid();
366}
367
368/// \endcond
The AbstractPokitServicePrivate class provides private implementation for AbstractPokitService.
QBluetoothUuid serviceUuid
UUIDs for service.
virtual void characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
Handles QLowEnergyService::characteristicRead events.
The AbstractPokitService class provides a common base for Pokit services classes.
QLowEnergyService * service()
Returns a non-const pointer to the internal service object, if any.
The DeviceInfoServicePrivate class provides private implementation for DeviceInfoService.
void characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) override
Implements AbstractPokitServicePrivate::characteristicRead to parse value, then emit a specialised si...
DeviceInfoServicePrivate(QLowEnergyController *controller, DeviceInfoService *const q)
The DeviceInfoService class accesses the Device Info service of Pokit devices.
QString serialNumber() const
Returns the most recent value of the Device Info service's (undocumented) Serial Number characteristi...
bool readSerialNumberCharacteristic()
Read the Device Info service's (undocumented) Serial Number characteristic.
bool readHardwareRevisionCharacteristic()
Read the Device Info service's Hardware Revision characteristic.
bool readSoftwareRevisionCharacteristic()
Read the Device Info service's Software Revision characteristic.
QString softwareRevision() const
Returns the most recent value of the Device Info service's Software Revision characteristic.
bool readFirmwareRevisionCharacteristic()
Read the Device Info service's Firmware Revision characteristic.
QString hardwareRevision() const
Returns the most recent value of the Device Info service's Hardware Revision characteristic.
bool readManufacturerCharacteristics()
Read the Device Info service's Manufacturer Name characteristic.
QString modelNumber() const
Returns the most recent value of the Device Info service's Model Number characteristic.
bool readModelNumberCharacteristic()
Read the Device Info service's Model Number characteristic.
bool readCharacteristics() override
Read all characteristics.
QString manufacturer() const
Returns the most recent value of the Device Info service's Manufacturer Name characteristic.
QString firmwareRevision() const
Returns the most recent value of the Device Info service's Firmware Revision characteristic.
DeviceInfoService(QLowEnergyController *const pokitDevice, QObject *parent=nullptr)
Constructs a new Pokit service with parent.
Declares the DeviceInfoService class.
Declares the DeviceInfoServicePrivate class.
bool isValid() const const
QString name() const const
QBluetoothUuid uuid() const const
QByteArray value() const const
QLowEnergyCharacteristic characteristic(const QBluetoothUuid &uuid) const const
Q_EMITQ_EMIT
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromUtf8(const char *str, int size)
QString & remove(int position, int n)
static const QBluetoothUuid manufacturerName
UUID of the Device Info service's Manufacturer Name String characterstic.
static const QBluetoothUuid hardwareRevision
UUID of the Device Info service's Hardware Revision String characterstic.
static const QBluetoothUuid softwareRevision
UUID of the Device Info service's Software Revision String characterstic.
static const QBluetoothUuid serialNumber
UUID of the Device Info service's Serial Number String characterstic.
static const QBluetoothUuid firmwareRevision
UUID of the Device Info service's Firmware Revision String characterstic.
static const QBluetoothUuid modelNumber
UUID of the Device Info service's Model Number String characterstic.