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 : /*!
5 : * \file
6 : * Declares the DataLoggerService class.
7 : */
8 :
9 : #ifndef QTPOKIT_DATALOGGERSERVICE_H
10 : #define QTPOKIT_DATALOGGERSERVICE_H
11 :
12 : #include "abstractpokitservice.h"
13 :
14 : #include <QBluetoothAddress>
15 : #include <QBluetoothUuid>
16 : #include <QVersionNumber>
17 :
18 : QTPOKIT_BEGIN_NAMESPACE
19 :
20 : class DataLoggerServicePrivate;
21 :
22 : class QTPOKIT_EXPORT DataLoggerService : public AbstractPokitService
23 : {
24 5852 : Q_OBJECT
25 :
26 : public:
27 : /// UUID of the "DataLogger" service.
28 : static inline const QBluetoothUuid serviceUuid { QStringLiteral("a5ff3566-1fd8-4e10-8362-590a578a4121") };
29 :
30 : /// Characteristics available via the `DataLogger` service.
31 : struct QTPOKIT_EXPORT CharacteristicUuids {
32 : /// UUID of the `DataLogger` service's `Settings` characterstic.
33 : static inline const QBluetoothUuid settings { QStringLiteral("5f97c62b-a83b-46c6-b9cd-cac59e130a78") };
34 :
35 : /// UUID of the `DataLogger` service's `Metadata` characterstic.
36 : static inline const QBluetoothUuid metadata { QStringLiteral("9acada2e-3936-430b-a8f7-da407d97ca6e") };
37 :
38 : /// UUID of the `DataLogger` service's `Reading` characterstic.
39 : static inline const QBluetoothUuid reading { QStringLiteral("3c669dab-fc86-411c-9498-4f9415049cc0") };
40 : };
41 :
42 : /// Values supported by the `Command` attribute of the `Settings` characteristic.
43 : enum class Command : quint8 {
44 : Start = 0, ///< Start the Data Logger.
45 : Stop = 1, ///< Stop the Data Logger.
46 : Refresh = 2, ///< Refresh the Data Logger.
47 : };
48 :
49 : /// Values supported by the `Mode` attribute of the `Settings` and `Metadata` characteristics.
50 : enum class Mode : quint8 {
51 : Idle = 0, ///< Make device idle.
52 : DcVoltage = 1, ///< Measure DC voltage.
53 : AcVoltage = 2, ///< Measure AC voltage.
54 : DcCurrent = 3, ///< Measure DC current.
55 : AcCurrent = 4, ///< Measure AC current.
56 : Temperature = 5, ///< Measure temperature.
57 : };
58 : static QString toString(const Mode &mode);
59 :
60 : static QString toString(const PokitProduct product, const quint8 range, const Mode mode);
61 : QString toString(const quint8 range, const Mode mode) const;
62 : static QVariant maxValue(const PokitProduct product, const quint8 range, const Mode mode);
63 : QVariant maxValue(const quint8 range, const Mode mode) const;
64 :
65 : /// Attributes included in the `Settings` characterstic.
66 : struct Settings {
67 : Command command; ///< Custom operation request.
68 : quint16 arguments; ///< Reserved to used along with #command in future.
69 : Mode mode; ///< Desired operation mode.
70 : quint8 range; ///< Desired range.
71 : quint32 updateInterval; ///< Desired update interval in milliseconds.
72 : quint32 timestamp; ///< Custom timestamp for start time in retrieved metadata.
73 : };
74 :
75 : /// Values supported by the `Status` attribute of the `Metadata` characteristic.
76 : enum class LoggerStatus : quint8 {
77 : Done = 0, ///< Sampling has completed.
78 : Sampling = 1, ///< Actively sampling.
79 : BufferFull = 2, ///< Buffer is full.
80 : Error = 255 ///< An error has occurred.
81 : };
82 :
83 : /// Attributes included in the `Metadata` characterstic.
84 : struct Metadata {
85 : LoggerStatus status; ///< Current data logger status.
86 : float scale; ///< Scale to apply to read samples.
87 : Mode mode; ///< Current operation mode.
88 : quint8 range; ///< Current range.
89 : quint32 updateInterval; ///< Current logging interval in milliseconds.
90 : quint16 numberOfSamples; ///< Number of samples acquired (1 to 6192).
91 : quint32 timestamp; ///< Timestamp stored at the beginning of the logging session.
92 : };
93 :
94 : typedef QVector<qint16> Samples;
95 :
96 : DataLoggerService(QLowEnergyController * const pokitDevice, QObject * parent = nullptr);
97 1782 : ~DataLoggerService() = default;
98 :
99 : bool readCharacteristics() override;
100 : bool readMetadataCharacteristic();
101 :
102 : // Settings characteristic (BLE write only).
103 : bool setSettings(const Settings &settings);
104 : bool startLogger(const Settings &settings);
105 : bool stopLogger();
106 : bool fetchSamples();
107 :
108 : // Metadata characteristic (BLE read/notify).
109 : Metadata metadata() const;
110 : bool enableMetadataNotifications();
111 : bool disableMetadataNotifications();
112 :
113 : // Reading characteristic (BLE notify only).
114 : bool enableReadingNotifications();
115 : bool disableReadingNotifications();
116 :
117 : Q_SIGNALS:
118 : void settingsWritten();
119 : void metadataRead(const DataLoggerService::Metadata &meta);
120 : void samplesRead(const DataLoggerService::Samples &samples);
121 :
122 : protected:
123 : /// \cond internal
124 : DataLoggerService(DataLoggerServicePrivate * const d, QObject * const parent);
125 : /// \endcond
126 :
127 : private:
128 532 : Q_DECLARE_PRIVATE(DataLoggerService)
129 : Q_DISABLE_COPY(DataLoggerService)
130 : friend class TestDataLoggerService;
131 : };
132 :
133 : QTPOKIT_END_NAMESPACE
134 :
135 : #endif // QTPOKIT_DATALOGGERSERVICE_H
|