1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qlowenergyservicedata.h" |
5 | |
6 | #include "qlowenergycharacteristicdata.h" |
7 | |
8 | #include <QtCore/qloggingcategory.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | Q_DECLARE_LOGGING_CATEGORY(QT_BT) |
13 | |
14 | struct QLowEnergyServiceDataPrivate : public QSharedData |
15 | { |
16 | QLowEnergyServiceDataPrivate() : type(QLowEnergyServiceData::ServiceTypePrimary) {} |
17 | |
18 | QLowEnergyServiceData::ServiceType type; |
19 | QBluetoothUuid uuid; |
20 | QList<QLowEnergyService *> includedServices; |
21 | QList<QLowEnergyCharacteristicData> characteristics; |
22 | }; |
23 | |
24 | |
25 | /*! |
26 | \since 5.7 |
27 | \class QLowEnergyServiceData |
28 | \brief The QLowEnergyServiceData class is used to set up GATT service data. |
29 | \inmodule QtBluetooth |
30 | \ingroup shared |
31 | |
32 | An Object of this class provides a service to be added to a GATT server via |
33 | \l QLowEnergyController::addService(). |
34 | */ |
35 | |
36 | /*! |
37 | \enum QLowEnergyServiceData::ServiceType |
38 | The type of GATT service. |
39 | |
40 | \value ServiceTypePrimary |
41 | The service is a primary service. |
42 | \value ServiceTypeSecondary |
43 | The service is a secondary service. Secondary services are included by other services |
44 | to implement some higher-level functionality. |
45 | */ |
46 | |
47 | /*! Creates a new invalid object of this class. */ |
48 | QLowEnergyServiceData::QLowEnergyServiceData() : d(new QLowEnergyServiceDataPrivate) |
49 | { |
50 | } |
51 | |
52 | /*! Constructs a new object of this class that is a copy of \a other. */ |
53 | QLowEnergyServiceData::QLowEnergyServiceData(const QLowEnergyServiceData &other) : d(other.d) |
54 | { |
55 | } |
56 | |
57 | /*! Destroys this object. */ |
58 | QLowEnergyServiceData::~QLowEnergyServiceData() |
59 | { |
60 | } |
61 | |
62 | /*! Makes this object a copy of \a other and returns the new value of this object. */ |
63 | QLowEnergyServiceData &QLowEnergyServiceData::operator=(const QLowEnergyServiceData &other) |
64 | { |
65 | d = other.d; |
66 | return *this; |
67 | } |
68 | |
69 | /*! Returns the type of this service. */ |
70 | QLowEnergyServiceData::ServiceType QLowEnergyServiceData::type() const |
71 | { |
72 | return d->type; |
73 | } |
74 | |
75 | /*! Sets the type of this service to \a type. */ |
76 | void QLowEnergyServiceData::setType(ServiceType type) |
77 | { |
78 | d->type = type; |
79 | } |
80 | |
81 | /*! Returns the UUID of this service. */ |
82 | QBluetoothUuid QLowEnergyServiceData::uuid() const |
83 | { |
84 | return d->uuid; |
85 | } |
86 | |
87 | /*! Sets the UUID of this service to \a uuid. */ |
88 | void QLowEnergyServiceData::setUuid(const QBluetoothUuid &uuid) |
89 | { |
90 | d->uuid = uuid; |
91 | } |
92 | |
93 | /*! Returns the list of included services. */ |
94 | QList<QLowEnergyService *> QLowEnergyServiceData::includedServices() const |
95 | { |
96 | return d->includedServices; |
97 | } |
98 | |
99 | /*! |
100 | Sets the list of included services to \a services. |
101 | All objects in this list must have been returned from a call to |
102 | \l QLowEnergyController::addService. |
103 | \sa addIncludedService() |
104 | */ |
105 | void QLowEnergyServiceData::setIncludedServices(const QList<QLowEnergyService *> &services) |
106 | { |
107 | d->includedServices = services; |
108 | } |
109 | |
110 | /*! |
111 | Adds \a service to the list of included services. |
112 | The \a service object must have been returned from a call to |
113 | \l QLowEnergyController::addService. This requirement prevents circular includes |
114 | (which are forbidden by the Bluetooth specification), and also helps to support the use case of |
115 | including more than one service of the same type. |
116 | \sa setIncludedServices() |
117 | */ |
118 | void QLowEnergyServiceData::addIncludedService(QLowEnergyService *service) |
119 | { |
120 | d->includedServices << service; |
121 | } |
122 | |
123 | /*! Returns the list of characteristics. */ |
124 | QList<QLowEnergyCharacteristicData> QLowEnergyServiceData::characteristics() const |
125 | { |
126 | return d->characteristics; |
127 | } |
128 | |
129 | /*! |
130 | Sets the list of characteristics to \a characteristics. |
131 | Only valid characteristics are considered. |
132 | \sa addCharacteristic() |
133 | */ |
134 | void QLowEnergyServiceData::setCharacteristics(const QList<QLowEnergyCharacteristicData> &characteristics) |
135 | { |
136 | d->characteristics.clear(); |
137 | for (const QLowEnergyCharacteristicData &cd : characteristics) |
138 | addCharacteristic(characteristic: cd); |
139 | } |
140 | |
141 | /*! |
142 | Adds \a characteristic to the list of characteristics, if it is valid. |
143 | \sa setCharacteristics() |
144 | */ |
145 | void QLowEnergyServiceData::addCharacteristic(const QLowEnergyCharacteristicData &characteristic) |
146 | { |
147 | if (characteristic.isValid()) |
148 | d->characteristics << characteristic; |
149 | else |
150 | qCWarning(QT_BT) << "not adding invalid characteristic to service"; |
151 | } |
152 | |
153 | /*! Returns \c true if this service is has a non-null UUID. */ |
154 | bool QLowEnergyServiceData::isValid() const |
155 | { |
156 | return !uuid().isNull(); |
157 | } |
158 | |
159 | /*! |
160 | \fn void QLowEnergyServiceData::swap(QLowEnergyServiceData &other) |
161 | Swaps this object with \a other. |
162 | */ |
163 | |
164 | /*! |
165 | \fn bool QLowEnergyServiceData::operator==(const QLowEnergyServiceData &a, |
166 | const QLowEnergyServiceData &b) |
167 | |
168 | \brief Returns \c true if \a a and \a b are equal with respect to their public state, |
169 | otherwise returns \c false. |
170 | */ |
171 | |
172 | /*! |
173 | \fn bool QLowEnergyServiceData::operator!=(const QLowEnergyServiceData &a, |
174 | const QLowEnergyServiceData &b) |
175 | |
176 | \brief Returns \c true if \a a and \a b are unequal with respect to their public state, |
177 | otherwise returns \c false. |
178 | */ |
179 | |
180 | /*! |
181 | \brief Returns \c true if \a a and \a b are equal with respect to their public state, |
182 | otherwise returns \c false. |
183 | \internal |
184 | */ |
185 | bool QLowEnergyServiceData::equals(const QLowEnergyServiceData &a, const QLowEnergyServiceData &b) |
186 | { |
187 | return a.d == b.d || ( |
188 | a.type() == b.type() |
189 | && a.uuid() == b.uuid() |
190 | && a.includedServices() == b.includedServices() |
191 | && a.characteristics() == b.characteristics()); |
192 | } |
193 | |
194 | QT_END_NAMESPACE |
195 |