Line data Source code
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 PokitDiscoveryAgent and PokitDiscoveryAgentPrivate classes.
7 : */
8 :
9 : #include <qtpokit/pokitdiscoveryagent.h>
10 : #include <qtpokit/pokitproducts.h>
11 : #include "pokitdiscoveryagent_p.h"
12 :
13 : #include <qtpokit/statusservice.h>
14 :
15 : #include <QBluetoothUuid>
16 :
17 : QTPOKIT_BEGIN_NAMESPACE
18 :
19 : /*!
20 : * \class PokitDiscoveryAgent
21 : *
22 : * The PokitDiscoveryAgent class discovers nearby Pokit devices.
23 : *
24 : * After constructing a PokitDiscoveryAgent object, and subscribing to the relevant signals,
25 : * invoke start() to begin discovery.
26 : */
27 :
28 : /*!
29 : * Constructs a new Pokit device discovery agent with \a parent, using \a deviceAdapter for the
30 : * search device.
31 : */
32 0 : PokitDiscoveryAgent::PokitDiscoveryAgent(
33 0 : const QBluetoothAddress &deviceAdapter, QObject *parent)
34 0 : : QBluetoothDeviceDiscoveryAgent(deviceAdapter, parent),
35 0 : d_ptr(new PokitDiscoveryAgentPrivate(this))
36 0 : {
37 :
38 0 : }
39 :
40 : /*!
41 : * Constructs a new Pokit device discovery agent with \a parent.
42 : */
43 39223 : PokitDiscoveryAgent::PokitDiscoveryAgent(QObject * parent)
44 15706 : : QBluetoothDeviceDiscoveryAgent(parent),
45 54929 : d_ptr(new PokitDiscoveryAgentPrivate(this))
46 19655 : {
47 :
48 58878 : }
49 :
50 : /*!
51 : * \cond internal
52 : * Constructs a new Pokit device discovery agent with \a parent, using \a deviceAdapter for the
53 : * search device, and private implementation \a d.
54 : */
55 0 : PokitDiscoveryAgent::PokitDiscoveryAgent(
56 : PokitDiscoveryAgentPrivate * const d, const QBluetoothAddress &deviceAdapter,
57 0 : QObject * const parent)
58 0 : : QBluetoothDeviceDiscoveryAgent(deviceAdapter, parent), d_ptr(d)
59 0 : {
60 :
61 0 : }
62 :
63 : /*!
64 : * Constructs a new Pokit device discovery agent with \a parent, and private implementation \a d.
65 : */
66 0 : PokitDiscoveryAgent::PokitDiscoveryAgent(
67 0 : PokitDiscoveryAgentPrivate * const d, QObject * const parent)
68 0 : : QBluetoothDeviceDiscoveryAgent(parent), d_ptr(d)
69 0 : {
70 :
71 0 : }
72 : /// \endcond
73 :
74 : /*!
75 : * Destroys this PokitDiscoveryAgent object.
76 : */
77 77220 : PokitDiscoveryAgent::~PokitDiscoveryAgent()
78 19655 : {
79 58878 : delete d_ptr;
80 96875 : }
81 :
82 : /*!
83 : * Starts Pokit device discovery.
84 : *
85 : * This override simply enforces that \a method must be \c LowEnergyMethod, as all Pokit devices
86 : * used Bluetooth Low Energy (BLE).
87 : */
88 0 : void PokitDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)
89 0 : {
90 0 : Q_D(PokitDiscoveryAgent);
91 0 : Q_ASSERT(methods == QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
92 0 : qCDebug(d->lc).noquote() << tr("Scanning for Bluetooth Low Energy devices.");
93 0 : QBluetoothDeviceDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
94 0 : }
95 :
96 : /*!
97 : * Starts Pokit device discovery.
98 : */
99 240 : void PokitDiscoveryAgent::start()
100 72 : {
101 72 : Q_D(PokitDiscoveryAgent);
102 399 : qCDebug(d->lc).noquote() << tr("Scanning for Bluetooth Low Energy devices.");
103 312 : QBluetoothDeviceDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
104 312 : }
105 :
106 : /*!
107 : * \fn void PokitDiscoveryAgent::pokitDeviceDiscovered(const QBluetoothDeviceInfo &info)
108 : *
109 : * This signal is emitted when the Pokit device described by \a info is discovered.
110 : */
111 :
112 : /*!
113 : * \fn void PokitDiscoveryAgent::pokitDeviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
114 : *
115 : * This signal is emitted when the Pokit device described by \a info is updated. The
116 : * \a updatedFields flags tell which information has been updated.
117 : */
118 :
119 : /*!
120 : * \cond internal
121 : * \class PokitDiscoveryAgentPrivate
122 : *
123 : * The PokitDiscoveryAgentPrivate class provides private implementation for
124 : * PokitDiscoveryAgent.
125 : */
126 :
127 : /*!
128 : * \internal
129 : * Constructs a new PokitDiscoveryAgentPrivate object with public implementation \a q.
130 : */
131 39223 : PokitDiscoveryAgentPrivate::PokitDiscoveryAgentPrivate(PokitDiscoveryAgent * const q)
132 54929 : : q_ptr(q)
133 19655 : {
134 58878 : connect(q, &QBluetoothDeviceDiscoveryAgent::canceled,
135 32005 : this, &PokitDiscoveryAgentPrivate::canceled);
136 :
137 58878 : connect(q, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
138 32005 : this, &PokitDiscoveryAgentPrivate::deviceDiscovered);
139 :
140 16952 : #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) // Signal added in Qt 5.12.
141 52026 : connect(q, &QBluetoothDeviceDiscoveryAgent::deviceUpdated,
142 29302 : this, &PokitDiscoveryAgentPrivate::deviceUpdated);
143 16952 : #endif
144 :
145 58878 : connect(q,
146 7691 : #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
147 7691 : QOverload<PokitDiscoveryAgent::Error>::of(&PokitDiscoveryAgent::error),
148 : #else
149 11964 : &QBluetoothDeviceDiscoveryAgent::errorOccurred,
150 11964 : #endif
151 32005 : this, &PokitDiscoveryAgentPrivate::error);
152 :
153 58878 : connect(q, &QBluetoothDeviceDiscoveryAgent::finished,
154 32005 : this, &PokitDiscoveryAgentPrivate::finished);
155 58878 : }
156 :
157 : /*!
158 : * Handle scan canceled signals, by simply logging the event for diagnostic purposes.
159 : */
160 80 : void PokitDiscoveryAgentPrivate::canceled() const
161 56 : {
162 165 : qCDebug(lc).noquote() << tr("Pokit device scan cancelled.");
163 136 : }
164 :
165 : /*!
166 : * Handle deviceDiscovered signals.
167 : *
168 : * Here we simply check if \a info describes a Pokit device, and if so, emit pokitDeviceDiscovered().
169 : */
170 480 : void PokitDiscoveryAgentPrivate::deviceDiscovered(const QBluetoothDeviceInfo &info)
171 336 : {
172 336 : Q_Q(PokitDiscoveryAgent);
173 816 : if (!isPokitProduct(info)) return;
174 660 : qCDebug(lc).noquote() << tr(R"(Discovered Pokit device "%1" at %2.)")
175 0 : .arg(info.name(), info.address().toString());
176 544 : Q_EMIT q->pokitDeviceDiscovered(info);
177 224 : }
178 :
179 : #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) // Required signal, and Fields, added in Qt 5.12.
180 : /*!
181 : * Handle deviceUpdated signals.
182 : *
183 : * Here we simply check if \a info describes a Pokit device, and if so, emit pokitDeviceUpdated().
184 : *
185 : * \since Qt 5.12.0
186 : */
187 426 : void PokitDiscoveryAgentPrivate::deviceUpdated(
188 : const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
189 282 : {
190 282 : Q_Q(PokitDiscoveryAgent);
191 708 : if (!isPokitProduct(info)) return;
192 588 : qCDebug(lc).noquote() << tr(R"(Pokit device "%1" at %2 updated with RSSI %3.)")
193 0 : .arg(info.name(), info.address().toString()).arg(info.rssi());
194 472 : Q_EMIT q->pokitDeviceUpdated(info, updatedFields);
195 188 : }
196 : #endif
197 :
198 : /*!
199 : * Handle scan errors, by simply logging \a error for diagnostic purposes.
200 : */
201 316 : void PokitDiscoveryAgentPrivate::error(const QBluetoothDeviceDiscoveryAgent::Error error) const
202 128 : {
203 1316 : qCWarning(lc).noquote() << tr("Pokit device scan error:") << error;
204 444 : }
205 :
206 : /*!
207 : * Handle scan finished signals, by simply logging the event for diagnostic purposes.
208 : */
209 80 : void PokitDiscoveryAgentPrivate::finished() const
210 56 : {
211 165 : qCDebug(lc).noquote() << tr("Pokit device scan finished.");
212 136 : }
213 :
214 : /// \endcond
215 :
216 : QTPOKIT_END_NAMESPACE
|