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