summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergyserviceinfo.cpp
blob: 178ca7049509bd0c2fc279de43065635213f5704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************
**
** Copyright (C) 2013 BlackBerry Limited all rights reserved
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qlowenergyserviceinfo.h"
#include "qlowenergyserviceinfo_p.h"

QT_BEGIN_NAMESPACE

/*!
    \enum QLowEnergyServiceInfo::ServiceType

    This enum describes the type of the service. One LE device can have one or more primary services.

    \value PrimaryService           The primary service. The primary service can have one or
                                    more included services.
    \value IncludedService          The included service by primary services.
*/

/*!
    \enum QLowEnergyServiceInfo::Error

    This enum describes the type of an error that can appear.

    \value ConnectionRefused        The connection to the service was refused. This can be caused if the device
                                    got turned off or it has a random device address. To solve
                                    the case if device has a random address on linux platform
                                    setRandomAddress() method must be called before connecting.
    \value DeviceBusy               The device is already connected to another Bluetooth device.
    \value InitializationFailed     Could not initialize GATT callbacks.
    \value MemoryAllocation         Could not initialize memory needed for reading GATT characteristics.
    \value DisconnectFail           Could not disconnect from the service.
    \value UnknownError             Unknown error.
*/

/*!
    Method for parsing the service name with given \a uuid.
 * \brief parseUuid
 * \param uuid
 * \return
 */
QString parseUuid(const QBluetoothUuid &uuid)
{
    static QHash<int, QString> uuidnames;
    if ( uuidnames.isEmpty() ) {
        uuidnames[0x1800] = QStringLiteral("Generic Access");
        uuidnames[0x1801] = QStringLiteral("Generic Attribute");
        uuidnames[0x1802] = QStringLiteral("ImmediateAlert");
        uuidnames[0x1803] = QStringLiteral("Link Loss");
        uuidnames[0x1804] = QStringLiteral("Tx Power");
        uuidnames[0x1805] = QStringLiteral("Current Time Service");
        uuidnames[0x1806] = QStringLiteral("Reference Time Update Service");
        uuidnames[0x1807] = QStringLiteral("Next DST Change Service");
        uuidnames[0x1808] = QStringLiteral("Glucose");
        uuidnames[0x1809] = QStringLiteral("Health Thermometer");
        uuidnames[0x180A] = QStringLiteral("Device Information");
        uuidnames[0x180D] = QStringLiteral("Heart Rate");
        uuidnames[0x180E] = QStringLiteral("Phone Alert Status Service");
        uuidnames[0x180F] = QStringLiteral("Battery Service");
        uuidnames[0x1810] = QStringLiteral("Blood Pressure");
        uuidnames[0x1811] = QStringLiteral("Alert Notification Service");
        uuidnames[0x1812] = QStringLiteral("Human Interface Device");
        uuidnames[0x1813] = QStringLiteral("Scan Parameters");
        uuidnames[0x1814] = QStringLiteral("Running Speed and Cadance");
        uuidnames[0x1816] = QStringLiteral("Cycling Speed and Cadance");
        uuidnames[0x1818] = QStringLiteral("Cycling Power");
        uuidnames[0x1819] = QStringLiteral("Location and Navigation");
    }
    QString name = uuidnames.value(uuid.toUInt16(), QStringLiteral("Unknow Service"));
    return name;
}

/*!
    Construct a new QLowEnergyServiceInfo.
*/
QLowEnergyServiceInfo::QLowEnergyServiceInfo():
    d_ptr(QSharedPointer<QLowEnergyServiceInfoPrivate>(new QLowEnergyServiceInfoPrivate))
{

}

/*!
    Construct a new QLowEnergyServiceInfo object with the given \a uuid.

    Based on uuid, corresponsing service name is given.
*/
QLowEnergyServiceInfo::QLowEnergyServiceInfo(const QBluetoothUuid &uuid):
    d_ptr(QSharedPointer<QLowEnergyServiceInfoPrivate>(new QLowEnergyServiceInfoPrivate))
{
    d_ptr->uuid = QBluetoothUuid(uuid);
    d_ptr->serviceName = parseUuid(d_ptr->uuid);
}

/*!
    Construct a new QLowEnergyServiceInfo that is a copy of \a other.

    The two copies continue to share the same underlying data which does not detach
    upon write.
*/
QLowEnergyServiceInfo::QLowEnergyServiceInfo(const QLowEnergyServiceInfo &other):
    d_ptr(other.d_ptr)
{

}

/*!
    Destroys the QLowEnergyServiceInfo object.
*/
QLowEnergyServiceInfo::~QLowEnergyServiceInfo()
{

}

/*!
    Returns the gatt service uuid.
*/
QBluetoothUuid QLowEnergyServiceInfo::uuid() const
{
    return d_ptr->uuid;
}

/*!
    Returns the list of service characteristics. If service was not connected, an empty
    list will be returned.
*/
QList<QLowEnergyCharacteristicInfo> QLowEnergyServiceInfo::characteristics() const
{
    return d_ptr->characteristicList;
}

/*!
    Returns the service name.
*/
QString QLowEnergyServiceInfo::name() const
{
    return d_ptr->serviceName;
}

/*!
    Sets service type with \a type.
*/
void QLowEnergyServiceInfo::setServiceType(QLowEnergyServiceInfo::ServiceType type)
{
    d_ptr->serviceType = type;
}

/*!
    Returns the service type. If setServiceType is not called default service type
    (PrimaryService) is returned.
*/
QLowEnergyServiceInfo::ServiceType QLowEnergyServiceInfo::serviceType() const
{
    return d_ptr->serviceType;
}

/*!
    Makes a copy of the \a other and assigns it to this QLowEnergyServiceInfo object.
    The two copies continue to share the same service and registration details.
*/
QLowEnergyServiceInfo &QLowEnergyServiceInfo::operator=(const QLowEnergyServiceInfo &other)
{
    d_ptr = other.d_ptr;
    return *this;
}

/*!
    Checks whether service is connected.
 */
bool QLowEnergyServiceInfo::isConnected() const
{
    return d_ptr->connected;
}

/*!
    This method is called if a device has a random device address that is used for connecting.
    It should be called before connecting to a service; otherwise connecting will not be
    successful and ConnectionRefused error will be emitted.
 */
void QLowEnergyServiceInfo::setRandomAddress()
{
    d_ptr->randomAddress = true;
}

/*!
    Returns an error string if error occurred.
 */
QString QLowEnergyServiceInfo::errorString() const
{
    return d_ptr->errorString;
}

/*!
    Returns the address of the Bluetooth device that provides this service.
*/
QBluetoothDeviceInfo QLowEnergyServiceInfo::device() const
{
    return d_ptr->deviceInfo;
}

/*!
    Sets the Bluetooth device that provides this service to \a device.
*/
void QLowEnergyServiceInfo::setDevice(const QBluetoothDeviceInfo &device)
{
    d_ptr->deviceInfo = device;
}

/*!
    Returns true if the QLowEnergyServiceInfo object is valid, otherwise returns false.
*/
bool QLowEnergyServiceInfo::isValid() const
{
    if (d_ptr->uuid == QBluetoothUuid())
        return false;
    if (!d_ptr->deviceInfo.isValid())
        return false;
    if (!d_ptr->valid())
        return false;
    return true;
}

QT_END_NAMESPACE