| 1 | /*************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtBluetooth module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qlowenergyservicedata.h" |
| 41 | |
| 42 | #include "qlowenergycharacteristicdata.h" |
| 43 | |
| 44 | #include <QtCore/qloggingcategory.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | Q_DECLARE_LOGGING_CATEGORY(QT_BT) |
| 49 | |
| 50 | struct QLowEnergyServiceDataPrivate : public QSharedData |
| 51 | { |
| 52 | QLowEnergyServiceDataPrivate() : type(QLowEnergyServiceData::ServiceTypePrimary) {} |
| 53 | |
| 54 | QLowEnergyServiceData::ServiceType type; |
| 55 | QBluetoothUuid uuid; |
| 56 | QList<QLowEnergyService *> includedServices; |
| 57 | QList<QLowEnergyCharacteristicData> characteristics; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | /*! |
| 62 | \since 5.7 |
| 63 | \class QLowEnergyServiceData |
| 64 | \brief The QLowEnergyServiceData class is used to set up GATT service data. |
| 65 | \inmodule QtBluetooth |
| 66 | \ingroup shared |
| 67 | |
| 68 | An Object of this class provides a service to be added to a GATT server via |
| 69 | \l QLowEnergyController::addService(). |
| 70 | */ |
| 71 | |
| 72 | /*! |
| 73 | \enum QLowEnergyServiceData::ServiceType |
| 74 | The type of GATT service. |
| 75 | |
| 76 | \value ServiceTypePrimary |
| 77 | The service is a primary service. |
| 78 | \value ServiceTypeSecondary |
| 79 | The service is a secondary service. Secondary services are included by other services |
| 80 | to implement some higher-level functionality. |
| 81 | */ |
| 82 | |
| 83 | /*! Creates a new invalid object of this class. */ |
| 84 | QLowEnergyServiceData::QLowEnergyServiceData() : d(new QLowEnergyServiceDataPrivate) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | /*! Constructs a new object of this class that is a copy of \a other. */ |
| 89 | QLowEnergyServiceData::QLowEnergyServiceData(const QLowEnergyServiceData &other) : d(other.d) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | /*! Destroys this object. */ |
| 94 | QLowEnergyServiceData::~QLowEnergyServiceData() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | /*! Makes this object a copy of \a other and returns the new value of this object. */ |
| 99 | QLowEnergyServiceData &QLowEnergyServiceData::operator=(const QLowEnergyServiceData &other) |
| 100 | { |
| 101 | d = other.d; |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | /*! Returns the type of this service. */ |
| 106 | QLowEnergyServiceData::ServiceType QLowEnergyServiceData::type() const |
| 107 | { |
| 108 | return d->type; |
| 109 | } |
| 110 | |
| 111 | /*! Sets the type of this service to \a type. */ |
| 112 | void QLowEnergyServiceData::setType(ServiceType type) |
| 113 | { |
| 114 | d->type = type; |
| 115 | } |
| 116 | |
| 117 | /*! Returns the UUID of this service. */ |
| 118 | QBluetoothUuid QLowEnergyServiceData::uuid() const |
| 119 | { |
| 120 | return d->uuid; |
| 121 | } |
| 122 | |
| 123 | /*! Sets the UUID of this service to \a uuid. */ |
| 124 | void QLowEnergyServiceData::setUuid(const QBluetoothUuid &uuid) |
| 125 | { |
| 126 | d->uuid = uuid; |
| 127 | } |
| 128 | |
| 129 | /*! Returns the list of included services. */ |
| 130 | QList<QLowEnergyService *> QLowEnergyServiceData::includedServices() const |
| 131 | { |
| 132 | return d->includedServices; |
| 133 | } |
| 134 | |
| 135 | /*! |
| 136 | Sets the list of included services to \a services. |
| 137 | All objects in this list must have been returned from a call to |
| 138 | \l QLowEnergyController::addService. |
| 139 | \sa addIncludedService() |
| 140 | */ |
| 141 | void QLowEnergyServiceData::setIncludedServices(const QList<QLowEnergyService *> &services) |
| 142 | { |
| 143 | d->includedServices = services; |
| 144 | } |
| 145 | |
| 146 | /*! |
| 147 | Adds \a service to the list of included services. |
| 148 | The \a service object must have been returned from a call to |
| 149 | \l QLowEnergyController::addService. This requirement prevents circular includes |
| 150 | (which are forbidden by the Bluetooth specification), and also helps to support the use case of |
| 151 | including more than one service of the same type. |
| 152 | \sa setIncludedServices() |
| 153 | */ |
| 154 | void QLowEnergyServiceData::addIncludedService(QLowEnergyService *service) |
| 155 | { |
| 156 | d->includedServices << service; |
| 157 | } |
| 158 | |
| 159 | /*! Returns the list of characteristics. */ |
| 160 | QList<QLowEnergyCharacteristicData> QLowEnergyServiceData::characteristics() const |
| 161 | { |
| 162 | return d->characteristics; |
| 163 | } |
| 164 | |
| 165 | /*! |
| 166 | Sets the list of characteristics to \a characteristics. |
| 167 | Only valid characteristics are considered. |
| 168 | \sa addCharacteristic() |
| 169 | */ |
| 170 | void QLowEnergyServiceData::setCharacteristics(const QList<QLowEnergyCharacteristicData> &characteristics) |
| 171 | { |
| 172 | d->characteristics.clear(); |
| 173 | for (const QLowEnergyCharacteristicData &cd : characteristics) |
| 174 | addCharacteristic(characteristic: cd); |
| 175 | } |
| 176 | |
| 177 | /*! |
| 178 | Adds \a characteristic to the list of characteristics, if it is valid. |
| 179 | \sa setCharacteristics() |
| 180 | */ |
| 181 | void QLowEnergyServiceData::addCharacteristic(const QLowEnergyCharacteristicData &characteristic) |
| 182 | { |
| 183 | if (characteristic.isValid()) |
| 184 | d->characteristics << characteristic; |
| 185 | else |
| 186 | qCWarning(QT_BT) << "not adding invalid characteristic to service" ; |
| 187 | } |
| 188 | |
| 189 | /*! Returns \c true if this service is has a non-null UUID. */ |
| 190 | bool QLowEnergyServiceData::isValid() const |
| 191 | { |
| 192 | return !uuid().isNull(); |
| 193 | } |
| 194 | |
| 195 | /*! |
| 196 | \fn void QLowEnergyServiceData::swap(QLowEnergyServiceData &other) |
| 197 | Swaps this object with \a other. |
| 198 | */ |
| 199 | |
| 200 | /*! |
| 201 | Returns \c true if \a sd1 and \a sd2 are equal with respect to their public state, |
| 202 | otherwise returns \c false. |
| 203 | */ |
| 204 | bool operator==(const QLowEnergyServiceData &sd1, const QLowEnergyServiceData &sd2) |
| 205 | { |
| 206 | return sd1.d == sd2.d || (sd1.type() == sd2.type() && sd1.uuid() == sd2.uuid() |
| 207 | && sd1.includedServices() == sd2.includedServices() |
| 208 | && sd1.characteristics() == sd2.characteristics()); |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | \fn bool operator!=(const QLowEnergyServiceData &sd1, |
| 213 | const QLowEnergyServiceData &sd2) |
| 214 | Returns \c true if \a sd1 and \a sd2 are not equal with respect to their public state, |
| 215 | otherwise returns \c false. |
| 216 | */ |
| 217 | |
| 218 | QT_END_NAMESPACE |
| 219 | |