Dokit 0.5.3-pre
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
41/*!
42 * Destroys this DeviceInfoService object.
43 */
48
50{
51 const bool r1 = readFirmwareRevisionCharacteristic();
52 const bool r2 = readHardwareRevisionCharacteristic();
53 const bool r3 = readSoftwareRevisionCharacteristic();
54 const bool r4 = readManufacturerCharacteristics();
55 const bool r5 = readModelNumberCharacteristic();
56 const bool r6 = ((service() != nullptr) && (service()->characteristic(CharacteristicUuids::serialNumber).isValid()))
58 return (r1 && r2 && r3 && r4 && r5 && r6);
59}
60
61/*!
62 * Read the `Device Info` service's `Firmware Revision` characteristic.
63 *
64 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
65 * underlying controller it not yet connected to the Pokit device, or the device's services have
66 * not yet been discovered).
67 *
68 * Emits firmwareRevisionRead() if/when the characteristic has been read successfully.
69 */
75
76/*!
77 * Read the `Device Info` service's `Hardware Revision` characteristic.
78 *
79 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
80 * underlying controller it not yet connected to the Pokit device, or the device's services have
81 * not yet been discovered).
82 *
83 * Emits hardwareRevisionRead() if/when the characteristic has been read successfully.
84 */
90
91/*!
92 * Read the `Device Info` service's `Manufacturer Name` characteristic.
93 *
94 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
95 * underlying controller it not yet connected to the Pokit device, or the device's services have
96 * not yet been discovered).
97 *
98 * Emits manufacturerNameRead() if/when the characteristic has been read successfully.
99 */
105
106/*!
107 * Read the `Device Info` service's `Model Number` characteristic.
108 *
109 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
110 * underlying controller it not yet connected to the Pokit device, or the device's services have
111 * not yet been discovered).
112 *
113 * Emits modelNumberRead() if/when the characteristic has been read successfully.
114 */
120
121/*!
122 * Read the `Device Info` service's `Software Revision` characteristic.
123 *
124 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
125 * underlying controller it not yet connected to the Pokit device, or the device's services have
126 * not yet been discovered).
127 *
128 * Emits softwareRevisionRead() if/when the characteristic has been read successfully.
129 */
135
136/*!
137 * Read the `Device Info` service's (undocumented) `Serial Number` characteristic.
138 *
139 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
140 * underlying controller it not yet connected to the Pokit device, or the device's services have
141 * not yet been discovered).
142 *
143 * Emits serialNumberRead() if/when the characteristic has been read successfully.
144 */
150
151/*!
152 * Returns the most recent value of the `Device Info` service's `Manufacturer Name` characteristic.
153 *
154 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
155 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
156 * null QString is returned.
157 */
159{
160 Q_D(const DeviceInfoService);
161 const QLowEnergyCharacteristic characteristic =
162 d->getCharacteristic(CharacteristicUuids::manufacturerName);
163 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
164}
165
166/*!
167 * Returns the most recent value of the `Device Info` service's `Model Number` characteristic.
168 *
169 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
170 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
171 * null QString is returned.
172 */
174{
175 Q_D(const DeviceInfoService);
176 const QLowEnergyCharacteristic characteristic =
177 d->getCharacteristic(CharacteristicUuids::modelNumber);
178 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
179}
180
181/*!
182 * Returns the most recent value of the `Device Info` service's `Hardware Revision` characteristic.
183 *
184 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
185 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
186 * null QString is returned.
187 */
189{
190 Q_D(const DeviceInfoService);
191 const QLowEnergyCharacteristic characteristic =
192 d->getCharacteristic(CharacteristicUuids::hardwareRevision);
193 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
194}
195
196/*!
197 * Returns the most recent value of the `Device Info` service's `Firmware Revision` characteristic.
198 *
199 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
200 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
201 * null QString is returned.
202 */
204{
205 Q_D(const DeviceInfoService);
206 const QLowEnergyCharacteristic characteristic =
207 d->getCharacteristic(CharacteristicUuids::firmwareRevision);
208 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
209}
210
211/*!
212 * Returns the most recent value of the `Device Info` service's `Software Revision` characteristic.
213 *
214 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
215 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
216 * null QString is returned.
217 */
219{
220 Q_D(const DeviceInfoService);
221 const QLowEnergyCharacteristic characteristic =
222 d->getCharacteristic(CharacteristicUuids::softwareRevision);
223 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()) : QString();
224}
225
226/*!
227 * Returns the most recent value of the `Device Info` service's (undocumented) `Serial Number`
228 * characteristic.
229 *
230 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
231 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then a
232 * null QString is returned.
233 */
235{
236 Q_D(const DeviceInfoService);
237 const QLowEnergyCharacteristic characteristic =
238 d->getCharacteristic(CharacteristicUuids::serialNumber);
239 /*!
240 * \cond internal
241 * \pokitApi Unlike other string characteristics, Pokit (Pro) devices always appear to add a trailing
242 * `null` byte to serial number strings. So here we strip any that are present.
243 * \endcond
244 */
245 return (characteristic.isValid()) ? QString::fromUtf8(characteristic.value()).remove(QLatin1Char('\0')) : QString();
246}
247
248/*!
249 * \fn DeviceInfoService::manufacturerRead
250 *
251 * This signal is emitted when the `Manufacturer Name` characteristic has been read successfully.
252 *
253 * \see readManufacturerCharacteristic
254 * \see manufacturer
255 */
256
257/*!
258 * \fn DeviceInfoService::modelNumberRead
259 *
260 * This signal is emitted when the `Model Number` characteristic has been read successfully.
261 *
262 * \see readModelNumberCharacteristic
263 * \see modelNumber
264 */
265
266/*!
267 * \fn DeviceInfoService::hardwareRevisionRead
268 *
269 * This signal is emitted when the `Hardware Revision` characteristic has been read successfully.
270 *
271 * \see readHardwareRevisionCharacteristic
272 * \see hardwareRevision
273 */
274
275/*!
276 * \fn DeviceInfoService::firmwareRevisionRead
277 *
278 * This signal is emitted when the `Firmware Revision` characteristic has been read successfully.
279 *
280 * \see readFirmwareRevisionCharacteristic
281 * \see firmwareRevision
282 */
283
284/*!
285 * \fn DeviceInfoService::softwareRevisionRead
286 *
287 * This signal is emitted when the `Software Revision` characteristic has been read successfully.
288 *
289 * \see readSoftwareRevisionCharacteristic
290 * \see softwareRevision
291 */
292
293/*!
294 * \fn DeviceInfoService::serialNumberRead
295 *
296 * This signal is emitted when the `Serial Number` characteristic has been read successfully.
297 *
298 * \see readSerialNumberCharacteristic
299 * \see serialNumber
300 */
301
302/*!
303 * \cond internal
304 * \class DeviceInfoServicePrivate
305 *
306 * The DeviceInfoServicePrivate class provides private implementation for DeviceInfoService.
307 */
308
309/*!
310 * \internal
311 * Constructs a new DeviceInfoServicePrivate object with public implementation \a q.
312 */
319
320/*!
321 * Implements AbstractPokitServicePrivate::characteristicRead to parse \a value, then emit a
322 * specialised signal, for each supported \a characteristic.
323 */
325 const QByteArray &value)
326{
328
331 const QString name = QString::fromUtf8(value);
332 qCDebug(lc).noquote() << tr(R"(Manufacturer name: "%1")").arg(name);
333 Q_EMIT q->manufacturerRead(name);
334 return;
335 }
336
338 const QString model = QString::fromUtf8(value);
339 qCDebug(lc).noquote() << tr(R"(Model number: "%1")").arg(model);
340 Q_EMIT q->modelNumberRead(model);
341 return;
342 }
343
345 const QString revision = QString::fromUtf8(value);
346 qCDebug(lc).noquote() << tr(R"(Hardware revision: "%1")").arg(revision);
347 Q_EMIT q->hardwareRevisionRead(revision);
348 return;
349 }
350
352 const QString revision = QString::fromUtf8(value);
353 qCDebug(lc).noquote() << tr(R"(Firmware revision: "%1")").arg(revision);
354 Q_EMIT q->firmwareRevisionRead(revision);
355 return;
356 }
357
359 const QString revision = QString::fromUtf8(value);
360 qCDebug(lc).noquote() << tr(R"(Software revision: "%1")").arg(revision);
361 Q_EMIT q->softwareRevisionRead(revision);
362 return;
363 }
364
366 const QString serialNumber = QString::fromUtf8(value);
367 qCDebug(lc).noquote() << tr(R"(Serial number: "%1")").arg(serialNumber);
368 Q_EMIT q->serialNumberRead(serialNumber);
369 return;
370 }
371
372 qCWarning(lc).noquote() << tr("Unknown characteristic read for Device Info service")
373 << serviceUuid << characteristic.name() << characteristic.uuid();
374}
375
376/// \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() override
Destroys this DeviceInfoService object.
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.