Dokit
Internal development documentation
Loading...
Searching...
No Matches
dataloggerservice.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2025 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4/*!
5 * \file
6 * Defines the DataLoggerService and DataLoggerServicePrivate classes.
7 */
8
10#include "dataloggerservice_p.h"
11#include "pokitproducts_p.h"
12
14
15#include <QDataStream>
16#include <QIODevice>
17#include <QLowEnergyController>
18#include <QtEndian>
19
20/*!
21 * \class DataLoggerService
22 *
23 * The DataLoggerService class accesses the `Data Logger` service of Pokit devices.
24 */
25
26/// Returns \a mode as a user-friendly string.
28{
29 switch (mode) {
30 case Mode::Idle: return tr("Idle");
31 case Mode::DcVoltage: return tr("DC voltage");
32 case Mode::AcVoltage: return tr("AC voltage");
33 case Mode::DcCurrent: return tr("DC current");
34 case Mode::AcCurrent: return tr("AC current");
35 case Mode::Temperature: return tr("Temperature");
36 default: return QString();
37 }
38}
39
40/// Returns \a range as a user-friendly string, or a null QString if \a mode has no ranges.
41QString DataLoggerService::toString(const PokitProduct product, const quint8 range, const Mode mode)
42{
43 switch (mode) {
44 case Mode::Idle:
45 break;
46 case Mode::DcVoltage:
47 case Mode::AcVoltage:
48 return VoltageRange::toString(product, range);
49 case Mode::DcCurrent:
50 case Mode::AcCurrent:
51 return CurrentRange::toString(product, range);
53 break;
54 }
55 return QString();
56}
57
58/// Returns \a range as a user-friendly string, or a null QString if \a mode has no ranges.
59QString DataLoggerService::toString(const quint8 range, const Mode mode) const
60{
61 return toString(*pokitProduct(), range, mode);
62}
63
64/*!
65 * Returns the maximum value for \a range, or 0 if \a range is not a known value for \a product's \a mode.
66 */
67quint32 DataLoggerService::maxValue(const PokitProduct product, const quint8 range, const Mode mode)
68{
69 switch (mode) {
70 case Mode::Idle:
71 break;
72 case Mode::DcVoltage:
73 case Mode::AcVoltage:
74 return VoltageRange::maxValue(product, range);
75 case Mode::DcCurrent:
76 case Mode::AcCurrent:
77 return CurrentRange::maxValue(product, range);
79 break;
80 }
81 return 0;
82}
83
84/*!
85 * Returns the maximum value for \a range, or 0 \a range is not a known value for the current \a product's \a mode.
86 */
87quint32 DataLoggerService::maxValue(const quint8 range, const Mode mode) const
88{
89 return maxValue(*pokitProduct(), range, mode);
90}
91
92/*!
93 * \typedef DataLoggerService::Samples
94 *
95 * Raw samples from the `Reading` characteristic. These raw samples are (supposedly) within the
96 * range -2048 to +2047, and need to be multiplied by the Metadata::scale value from the `Metadata`
97 * characteristc to get the true values.
98 *
99 * Also supposedly, there should be no more than 10 samples at a time, according to Pokit's current
100 * API docs. There is not artificial limitation imposed by QtPokit, so devices may begin batching
101 * more samples in future. Specifically, the Pokit Pro seems to send 88 samples (in 176 bytes) at a
102 * time.
103 */
104
105/*!
106 * Constructs a new Pokit service with \a parent.
107 */
109 : AbstractPokitService(new DataLoggerServicePrivate(controller, this), parent)
110{
111
112}
113
114/*!
115 * \cond internal
116 * Constructs a new Pokit service with \a parent, and private implementation \a d.
117 */
119 DataLoggerServicePrivate * const d, QObject * const parent)
120 : AbstractPokitService(d, parent)
121{
122
123}
124/// \endcond
125
130
131/*!
132 * Reads the `DataLogger` service's `Metadata` characteristic.
133 *
134 * Returns `true` is the read request is succesfully queued, `false` otherwise (ie if the
135 * underlying controller it not yet connected to the Pokit device, or the device's services have
136 * not yet been discovered).
137 *
138 * Emits metadataRead() if/when the characteristic has been read successfully.
139 */
141{
143 return d->readCharacteristic(CharacteristicUuids::metadata);
144}
145
146/*!
147 * Configures the Pokit device's data logger mode.
148 *
149 * Returns `true` if the write request was successfully queued, `false` otherwise.
150 *
151 * Emits settingsWritten() if/when the \a settings have been writtem successfully.
152 */
154{
155 Q_D(const DataLoggerService);
156 const QLowEnergyCharacteristic characteristic =
157 d->getCharacteristic(CharacteristicUuids::settings);
158 if (!characteristic.isValid()) {
159 return false;
160 }
161
162 const bool updateIntervalIs32bit =
163 (d->getCharacteristic(CharacteristicUuids::metadata).value().size() >= 23);
164 const QByteArray value = DataLoggerServicePrivate::encodeSettings(settings, updateIntervalIs32bit);
165 if (value.isNull()) {
166 return false;
167 }
168
169 d->service->writeCharacteristic(characteristic, value);
170 return (d->service->error() != QLowEnergyService::ServiceError::CharacteristicWriteError);
171}
172
173/*!
174 * Start the data logger with \a settings.
175 *
176 * This is just a synonym for setSettings() except makes the caller's intention more explicit, and
177 * sanity-checks that the settings's command is DataLoggerService::Command::Start.
178 */
180{
181 Q_D(const DataLoggerService);
182 Q_ASSERT(settings.command == DataLoggerService::Command::Start);
184 qCWarning(d->lc).noquote() << tr("Settings command must be 'Start'.");
185 return false;
186 }
187 return setSettings(settings);
188}
189
190/*!
191 * Stop the data logger.
192 *
193 * This is just a convenience function equivalent to calling setSettings() with the command set to
194 * DataLoggerService::Command::Stop.
195 */
197{
198 // Note, only the Settings::command member need be set, since the others are all ignored by the
199 // Pokit device when the command is Stop. However, we still explicitly initialise all other
200 // members just to ensure we're never exposing uninitialised RAM to an external device.
202}
203
204/*!
205 * Start the data logger.
206 *
207 * This is just a convenience function equivalent to calling setSettings() with the command set to
208 * DataLoggerService::Command::Refresh.
209 *
210 * Once the Pokit device has processed this request succesffully, the device will begin notifying
211 * the `Metadata` and `Reading` characteristic, resulting in emits of metadataRead and samplesRead
212 * respectively.
213 */
215{
216 // Note, only the Settings::command member need be set, since the others are all ignored by the
217 // Pokit device when the command is Refresh. However, we still explicitly initialise all other
218 // members just to ensure we're never exposing uninitialised RAM to an external device.
220}
221
222/*!
223 * Returns the most recent value of the `DataLogger` service's `Metadata` characteristic.
224 *
225 * The returned value, if any, is from the underlying Bluetooth stack's cache. If no such value is
226 * currently available (ie the serviceDetailsDiscovered signal has not been emitted yet), then the
227 * returned DataLoggerService::Metadata::scale member will be a quiet NaN, which can be checked like:
228 *
229 * ```
230 * const DataLoggerService::Metadata metadata = multimeterService->metadata();
231 * if (qIsNaN(metadata.scale)) {
232 * // Handle failure.
233 * }
234 * ```
235 */
237{
238 Q_D(const DataLoggerService);
239 const QLowEnergyCharacteristic characteristic =
240 d->getCharacteristic(CharacteristicUuids::metadata);
241 return (characteristic.isValid()) ? DataLoggerServicePrivate::parseMetadata(characteristic.value())
242 : Metadata{ LoggerStatus::Error, std::numeric_limits<float>::quiet_NaN(), Mode::Idle, 0, 0, 0, 0 };
243}
244
245/*!
246 * Enables client-side notifications of Data Logger metadata changes.
247 *
248 * This is an alternative to manually requesting individual reads via readMetadataCharacteristic().
249 *
250 * Returns `true` is the request was successfully submited to the device queue, `false` otherwise.
251 *
252 * Successfully read values (if any) will be emitted via the metadataRead() signal.
253 */
255{
257 return d->enableCharacteristicNotificatons(CharacteristicUuids::metadata);
258}
259
260/*!
261 * Disables client-side notifications of Data Logger metadata changes.
262 *
263 * Instantaneous reads can still be fetched by readMetadataCharacteristic().
264 *
265 * Returns `true` is the request was successfully submited to the device queue, `false` otherwise.
266 */
268{
270 return d->disableCharacteristicNotificatons(CharacteristicUuids::metadata);
271}
272
273/*!
274 * Enables client-side notifications of Data Logger readings.
275 *
276 * Returns `true` is the request was successfully submited to the device queue, `false` otherwise.
277 *
278 * Successfully read samples (if any) will be emitted via the samplesRead() signal.
279 */
281{
283 return d->enableCharacteristicNotificatons(CharacteristicUuids::reading);
284}
285
286/*!
287 * Disables client-side notifications of Data Logger readings.
288 *
289 * Returns `true` is the request was successfully submited to the device queue, `false` otherwise.
290 */
292{
294 return d->disableCharacteristicNotificatons(CharacteristicUuids::reading);
295}
296
297/*!
298 * \fn DataLoggerService::settingsWritten
299 *
300 * This signal is emitted when the `Settings` characteristic has been written successfully.
301 *
302 * \see setSettings
303 */
304
305/*!
306 * \fn DataLoggerService::metadataRead
307 *
308 * This signal is emitted when the `Metadata` characteristic has been read successfully.
309 *
310 * \see readMetadataCharacteristic
311 */
312
313/*!
314 * \fn DataLoggerService::samplesRead
315 *
316 * This signal is emitted when the `Reading` characteristic has been notified.
317 *
318 * \see beginSampling
319 * \see stopSampling
320 */
321
322
323/*!
324 * \cond internal
325 * \class DataLoggerServicePrivate
326 *
327 * The DataLoggerServicePrivate class provides private implementation for DataLoggerService.
328 */
329
330/*!
331 * \internal
332 * Constructs a new DataLoggerServicePrivate object with public implementation \a q.
333 */
340
341/*!
342 * Returns \a settings in the format Pokit devices expect. If \a updateIntervalIs32bit is \c true
343 * then the `Update Interval` field will be encoded in 32-bit instead of 16.
344 */
346 const bool updateIntervalIs32bit)
347{
348 static_assert(sizeof(settings.command) == 1, "Expected to be 1 byte.");
349 static_assert(sizeof(settings.arguments) == 2, "Expected to be 2 bytes.");
350 static_assert(sizeof(settings.mode) == 1, "Expected to be 1 byte.");
351 static_assert(sizeof(settings.range) == 1, "Expected to be 1 byte.");
352 static_assert(sizeof(settings.updateInterval) == 4, "Expected to be 4 bytes.");
353 static_assert(sizeof(settings.timestamp) == 4, "Expected to be 4 bytes.");
354
355 QByteArray value;
356 QDataStream stream(&value, QIODevice::WriteOnly);
358 stream.setFloatingPointPrecision(QDataStream::SinglePrecision); // 32-bit floats, not 64-bit.
359 stream << (quint8)settings.command << settings.arguments << (quint8)settings.mode << settings.range;
360
361 /*!
362 * \pokitApi For Pokit Meter, `updateInterval` is `uint16` seconds (as per the Pokit API 1.00),
363 * however for Pokit Pro it's `uint32` milliseconds, even though that's not officially
364 * documented anywhere.
365 */
366
367 if (!updateIntervalIs32bit) {
368 stream << (quint16)((settings.updateInterval+500)/1000) << settings.timestamp;
369 Q_ASSERT(value.size() == 11); // According to Pokit API 1.00.
370 } else {
371 stream << settings.updateInterval << settings.timestamp;
372 Q_ASSERT(value.size() == 13); // According to testing / experimentation.
373 }
374 return value;
375}
376
377/*!
378 * Parses the `Metadata` \a value into a DataLoggerService::Metatdata struct.
379 */
381{
383 DataLoggerService::LoggerStatus::Error, std::numeric_limits<float>::quiet_NaN(),
385 };
386
387 // Pokit Meter: 15 bytes, Pokit Pro: 23 bytes.
388 if (!checkSize(QLatin1String("Metadata"), value, 15, 23)) {
389 return metadata;
390 }
391
392 qCDebug(lc) << value.mid(7,12).toHex(',');
393 metadata.status = static_cast<DataLoggerService::LoggerStatus>(value.at(0));
394 metadata.scale = qFromLittleEndian<float>(value.mid(1,4).constData());
395 metadata.mode = static_cast<DataLoggerService::Mode>(value.at(5));
396 metadata.range = static_cast<quint8>(value.at(6));
397
398 /*!
399 * \pokitApi For Pokit Meter, `updateInterval` is `uint16` (as per the Pokit API 1.00), however
400 * for Pokit Pro it's `uint32`, even though that's not officially documented anywhere.
401 * Also note, the doc claims 'microseconds' (ie 10^-6), but clearly the value is 'milliseconds'
402 * (ie 10^-3) for Pokit Pro, and whole seconds for Pokit Meter.
403 */
404
405 if (value.size() == 15) {
406 metadata.updateInterval = qFromLittleEndian<quint16>(value.mid(7,2).constData())*1000;
407 metadata.numberOfSamples = qFromLittleEndian<quint16>(value.mid(9,2).constData());
408 metadata.timestamp = qFromLittleEndian<quint32>(value.mid(11,4).constData());
409 } else if (value.size() == 23) {
410 metadata.updateInterval = qFromLittleEndian<quint32>(value.mid(7,4).constData());
411 metadata.numberOfSamples = qFromLittleEndian<quint16>(value.mid(11,2).constData());
412 metadata.timestamp = qFromLittleEndian<quint32>(value.mid(19,4).constData());
413 } else {
414 qCWarning(lc).noquote() << tr("Cannot decode metadata of %n byte/s: %1", nullptr, value.size())
415 .arg(toHexString(value));
416 }
417 return metadata;
418}
419
420/*!
421 * Parses the `Reading` \a value into a DataLoggerService::Samples vector.
422 */
424{
426 if ((value.size()%2) != 0) {
427 qCWarning(lc).noquote() << tr("Samples value has odd size %1 (should be even): %2")
428 .arg(value.size()).arg(toHexString(value));
429 return samples;
430 }
431 while ((samples.size()*2) < value.size()) {
432 samples.append(qFromLittleEndian<qint16>(value.mid(samples.size()*2,2).constData()));
433 }
434 qCDebug(lc).noquote() << tr("Read %n sample/s from %1-bytes.", nullptr, samples.size()).arg(value.size());
435 return samples;
436}
437
438/*!
439 * Implements AbstractPokitServicePrivate::characteristicRead to parse \a value, then emit a
440 * specialised signal, for each supported \a characteristic.
441 */
443 const QByteArray &value)
444{
446
448 qCWarning(lc).noquote() << tr("Settings characteristic is write-only, but somehow read")
449 << serviceUuid << characteristic.name() << characteristic.uuid();
450 return;
451 }
452
455 Q_EMIT q->metadataRead(parseMetadata(value));
456 return;
457 }
458
460 qCWarning(lc).noquote() << tr("Reading characteristic is notify-only")
461 << serviceUuid << characteristic.name() << characteristic.uuid();
462 return;
463 }
464
465 qCWarning(lc).noquote() << tr("Unknown characteristic read for Data Logger service")
466 << serviceUuid << characteristic.name() << characteristic.uuid();
467}
468
469/*!
470 * Implements AbstractPokitServicePrivate::characteristicWritten to parse \a newValue, then emit a
471 * specialised signal, for each supported \a characteristic.
472 */
474 const QByteArray &newValue)
475{
477
480 Q_EMIT q->settingsWritten();
481 return;
482 }
483
485 qCWarning(lc).noquote() << tr("Metadata characteristic is read/notify, but somehow written")
486 << serviceUuid << characteristic.name() << characteristic.uuid();
487 return;
488 }
489
491 qCWarning(lc).noquote() << tr("Reading characteristic is notify-only, but somehow written")
492 << serviceUuid << characteristic.name() << characteristic.uuid();
493 return;
494 }
495
496 qCWarning(lc).noquote() << tr("Unknown characteristic written for Data Logger service")
497 << serviceUuid << characteristic.name() << characteristic.uuid();
498}
499
500/*!
501 * Implements AbstractPokitServicePrivate::characteristicChanged to parse \a newValue, then emit a
502 * specialised signal, for each supported \a characteristic.
503 */
505 const QByteArray &newValue)
506{
508
511 qCWarning(lc).noquote() << tr("Settings characteristic is write-only, but somehow updated")
512 << serviceUuid << characteristic.name() << characteristic.uuid();
513 return;
514 }
515
517 Q_EMIT q->metadataRead(parseMetadata(newValue));
518 return;
519 }
520
522 Q_EMIT q->samplesRead(parseSamples(newValue));
523 return;
524 }
525
526 qCWarning(lc).noquote() << tr("Unknown characteristic notified for Data Logger service")
527 << serviceUuid << characteristic.name() << characteristic.uuid();
528}
529
530/// \endcond
QBluetoothUuid serviceUuid
UUIDs for service.
virtual void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
Handles QLowEnergyService::characteristicChanged events.
AbstractPokitServicePrivate(const QBluetoothUuid &serviceUuid, QLowEnergyController *controller, AbstractPokitService *const q)
virtual void characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
Handles QLowEnergyService::characteristicRead events.
virtual void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
Handles QLowEnergyService::characteristicWritten events.
QLowEnergyController * controller
BLE controller to fetch the service from.
static QString toHexString(const QByteArray &data, const int maxSize=20)
Returns up to maxSize bytes of data as a human readable hexadecimal string.
static bool checkSize(const QString &label, const QByteArray &data, const int minSize, const int maxSize=-1, const bool failOnMax=false)
Returns false if data is smaller than minSize, otherwise returns failOnMax if data is bigger than max...
std::optional< PokitProduct > pokitProduct() const
Returns the Pokit product this service is attached to.
The DataLoggerServicePrivate class provides private implementation for DataLoggerService.
void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue) override
Implements AbstractPokitServicePrivate::characteristicChanged to parse newValue, then emit a speciali...
void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue) override
Implements AbstractPokitServicePrivate::characteristicWritten to parse newValue, then emit a speciali...
static QByteArray encodeSettings(const DataLoggerService::Settings &settings, const bool updateIntervalIs32bit)
Returns settings in the format Pokit devices expect.
void characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) override
Implements AbstractPokitServicePrivate::characteristicRead to parse value, then emit a specialised si...
static DataLoggerService::Samples parseSamples(const QByteArray &value)
Parses the Reading value into a DataLoggerService::Samples vector.
static DataLoggerService::Metadata parseMetadata(const QByteArray &value)
Parses the Metadata value into a DataLoggerService::Metatdata struct.
DataLoggerServicePrivate(QLowEnergyController *controller, DataLoggerService *const q)
The DataLoggerService class accesses the Data Logger service of Pokit devices.
bool readMetadataCharacteristic()
Reads the DataLogger service's Metadata characteristic.
bool enableMetadataNotifications()
Enables client-side notifications of Data Logger metadata changes.
LoggerStatus
Values supported by the Status attribute of the Metadata characteristic.
@ Error
An error has occurred.
DataLoggerService(QLowEnergyController *const pokitDevice, QObject *parent=nullptr)
Constructs a new Pokit service with parent.
bool disableMetadataNotifications()
Disables client-side notifications of Data Logger metadata changes.
bool enableReadingNotifications()
Enables client-side notifications of Data Logger readings.
bool setSettings(const Settings &settings)
Configures the Pokit device's data logger mode.
bool startLogger(const Settings &settings)
Start the data logger with settings.
QVector< qint16 > Samples
Raw samples from the Reading characteristic.
static QString toString(const Mode &mode)
Returns mode as a user-friendly string.
bool fetchSamples()
Start the data logger.
static quint32 maxValue(const PokitProduct product, const quint8 range, const Mode mode)
Returns the maximum value for range, or 0 if range is not a known value for product's mode.
bool readCharacteristics() override
Read all characteristics.
QString toString(const quint8 range, const Mode mode) const
Returns range as a user-friendly string, or a null QString if mode has no ranges.
@ Stop
Stop the Data Logger.
@ Refresh
Refresh the Data Logger.
@ Start
Start the Data Logger.
bool disableReadingNotifications()
Disables client-side notifications of Data Logger readings.
bool stopLogger()
Stop the data logger.
Metadata metadata() const
Returns the most recent value of the DataLogger service's Metadata characteristic.
Mode
Values supported by the Mode attribute of the Settings and Metadata characteristics.
@ DcVoltage
Measure DC voltage.
@ AcCurrent
Measure AC current.
@ AcVoltage
Measure AC voltage.
@ Idle
Make device idle.
@ Temperature
Measure temperature.
@ DcCurrent
Measure DC current.
Declares the DataLoggerService class.
Declares the DataLoggerServicePrivate class.
QString toString(const PokitProduct product, const quint8 range)
Returns product's current range as a human-friendly string.
quint32 maxValue(const PokitProduct product, const quint8 range)
Returns the maximum value for range in microamps, or 0 if range is not a known value for product.
quint32 maxValue(const PokitProduct product, const quint8 range)
Returns the maximum value for range in millivolts, or 0 if range is not a known value for product.
QString toString(const PokitProduct product, const quint8 range)
Returns product's current range as a human-friendly string.
PokitProduct
Pokit products known to, and supported by, the QtPokit library.
char at(int i) const const
const char * constData() const const
bool isNull() const const
QByteArray mid(int pos, int len) const const
int size() const const
QByteArray toHex() const const
void setByteOrder(QDataStream::ByteOrder bo)
void setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision)
bool isValid() const const
QString name() const const
QBluetoothUuid uuid() const const
QByteArray value() const const
QObject(QObject *parent)
Q_EMITQ_EMIT
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void append(const T &value)
int size() const const
Declares the StatusService class.
static const QBluetoothUuid metadata
UUID of the DataLogger service's Metadata characterstic.
static const QBluetoothUuid settings
UUID of the DataLogger service's Settings characterstic.
static const QBluetoothUuid reading
UUID of the DataLogger service's Reading characterstic.
Attributes included in the Metadata characterstic.
Attributes included in the Settings characterstic.
quint32 timestamp
Custom timestamp for start time in retrieved metadata.
quint16 arguments
Reserved to used along with command in future.
Command command
Custom operation request.
Mode mode
Desired operation mode.
quint32 updateInterval
Desired update interval in milliseconds.