summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergycontrollernew.cpp
blob: 3b499d3bc93a8f7c08f0a3157c16c180e0740433 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** 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 "qlowenergycontrollernew.h"
#include "qlowenergycontrollernew_p.h"

#include <QtBluetooth/QBluetoothLocalDevice>

#include <algorithm>

QT_BEGIN_NAMESPACE

void QLowEnergyControllerNewPrivate::setError(
        QLowEnergyControllerNew::Error newError)
{
    Q_Q(QLowEnergyControllerNew);
    error = newError;

    switch (newError) {
    case QLowEnergyControllerNew::UnknownRemoteDeviceError:
        errorString = QLowEnergyControllerNew::tr("Remote device cannot be found");
        break;
    case QLowEnergyControllerNew::InvalidBluetoothAdapterError:
        errorString = QLowEnergyControllerNew::tr("Cannot find local adapter");
        break;
    case QLowEnergyControllerNew::NetworkError:
        errorString = QLowEnergyControllerNew::tr("Error occurred during connection I/O");
        break;
    case QLowEnergyControllerNew::UnknownError:
    default:
        errorString = QLowEnergyControllerNew::tr("Unknown Error");
        break;
    }

    emit q->error(newError);
}

bool QLowEnergyControllerNewPrivate::isValidLocalAdapter()
{
    if (localAdapter.isNull())
        return false;

    const QList<QBluetoothHostInfo> foundAdapters = QBluetoothLocalDevice::allDevices();
    bool adapterFound = false;

    foreach (const QBluetoothHostInfo &info, foundAdapters) {
        if (info.address() == localAdapter) {
            adapterFound = true;
            break;
        }
    }

    return adapterFound;
}

void QLowEnergyControllerNewPrivate::setState(
        QLowEnergyControllerNew::ControllerState newState)
{
    Q_Q(QLowEnergyControllerNew);
    if (state == newState)
        return;

    state = newState;
    emit q->stateChanged(state);
}

void QLowEnergyControllerNewPrivate::invalidateServices()
{
    foreach (const QSharedPointer<QLowEnergyServicePrivate> service, serviceList.values()) {
        service->setController(0);
        service->setState(QLowEnergyService::InvalidService);
    }

    serviceList.clear();
}

QSharedPointer<QLowEnergyServicePrivate> QLowEnergyControllerNewPrivate::serviceForHandle(
        QLowEnergyHandle handle)
{
    foreach (QSharedPointer<QLowEnergyServicePrivate> service, serviceList.values())
        if (service->startHandle <= handle && handle <= service->endHandle)
            return service;

    return QSharedPointer<QLowEnergyServicePrivate>();
}

/*!
    Returns a valid characteristic if the given handle is the
    handle of the characteristic itself or one of its descriptors
 */
QLowEnergyCharacteristic QLowEnergyControllerNewPrivate::characteristicForHandle(
        QLowEnergyHandle handle)
{
    QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(handle);
    if (service.isNull())
        return QLowEnergyCharacteristic();

    if (service->characteristicList.isEmpty())
        return QLowEnergyCharacteristic();

    // check whether it is the handle of a characteristic header
    if (service->characteristicList.contains(handle))
        return QLowEnergyCharacteristic(service, handle);

    // check whether it is the handle of the characteristic value or its descriptors
    QList<QLowEnergyHandle> charHandles = service->characteristicList.keys();
    std::sort(charHandles.begin(), charHandles.end());
    for (int i = charHandles.size() - 1; i >= 0; i--) {
        if (charHandles.at(i) > handle)
            continue;

        return QLowEnergyCharacteristic(service, charHandles.at(i));
    }

    return QLowEnergyCharacteristic();
}

/*!
    Returns a valid descriptor if \a handle blongs to a descriptor;
    otherwise an invalid one.
 */
QLowEnergyDescriptor QLowEnergyControllerNewPrivate::descriptorForHandle(
        QLowEnergyHandle handle)
{
    const QLowEnergyCharacteristic matchingChar = characteristicForHandle(handle);
    if (!matchingChar.isValid())
        return QLowEnergyDescriptor();

    const QLowEnergyServicePrivate::CharData charData = matchingChar.
            d_ptr->characteristicList[matchingChar.attributeHandle()];

    if (charData.descriptorList.contains(handle))
        return QLowEnergyDescriptor(matchingChar.d_ptr, matchingChar.attributeHandle(),
                                    handle);

    return QLowEnergyDescriptor();
}

void QLowEnergyControllerNewPrivate::updateValueOfCharacteristic(
        QLowEnergyHandle charHandle, const QByteArray &value)
{
    QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
    if (!service.isNull() && service->characteristicList.contains(charHandle))
        service->characteristicList[charHandle].value = value;
}

void QLowEnergyControllerNewPrivate::updateValueOfDescriptor(
        QLowEnergyHandle charHandle, QLowEnergyHandle descriptorHandle,
        const QByteArray &value)
{
    QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(charHandle);
    if (service.isNull() || !service->characteristicList.contains(charHandle))
        return;

    if (!service->characteristicList[charHandle].descriptorList.contains(descriptorHandle))
        return;

    service->characteristicList[charHandle].descriptorList[descriptorHandle].value = value;
}

QLowEnergyControllerNew::QLowEnergyControllerNew(
                            const QBluetoothAddress &remoteDevice,
                            QObject *parent)
    : QObject(parent), d_ptr(new QLowEnergyControllerNewPrivate())
{
    Q_D(QLowEnergyControllerNew);
    d->q_ptr = this;
    d->remoteDevice = remoteDevice;
    d->localAdapter = QBluetoothLocalDevice().address();
}

QLowEnergyControllerNew::QLowEnergyControllerNew(
                            const QBluetoothAddress &remoteDevice,
                            const QBluetoothAddress &localDevice,
                            QObject *parent)
    : QObject(parent), d_ptr(new QLowEnergyControllerNewPrivate())
{
    Q_D(QLowEnergyControllerNew);
    d->q_ptr = this;
    d->remoteDevice = remoteDevice;
    d->localAdapter = localDevice;
}

QLowEnergyControllerNew::~QLowEnergyControllerNew()
{
    disconnectFromDevice(); //in case we were connected
    delete d_ptr;
}

/*!
  Returns the address of the local Bluetooth adapter being used for the
  communication.

  If this class instance was requested to use the default adapter
  but there was no default adapter when creating this
  class instance, the returned \l QBluetoothAddress will be null.

  \sa QBluetoothAddress::isNull()
 */
QBluetoothAddress QLowEnergyControllerNew::localAddress() const
{
    return d_ptr->localAdapter;
}

QBluetoothAddress QLowEnergyControllerNew::remoteAddress() const
{
    return d_ptr->remoteDevice;
}

QLowEnergyControllerNew::ControllerState QLowEnergyControllerNew::state() const
{
    return d_ptr->state;
}

void QLowEnergyControllerNew::connectToDevice()
{
    Q_D(QLowEnergyControllerNew);

    if (!d->isValidLocalAdapter()) {
        d->setError(QLowEnergyControllerNew::InvalidBluetoothAdapterError);
        return;
    }

    if (state() != QLowEnergyControllerNew::UnconnectedState)
        return;

    d->connectToDevice();
}

void QLowEnergyControllerNew::disconnectFromDevice()
{
    Q_D(QLowEnergyControllerNew);

    if (state() == QLowEnergyControllerNew::UnconnectedState)
        return;

    d->invalidateServices();
    d->disconnectFromDevice();
}

void QLowEnergyControllerNew::discoverServices()
{
    Q_D(QLowEnergyControllerNew);

    if (d->state != QLowEnergyControllerNew::ConnectedState)
        return;

    d->discoverServices();
}

QList<QBluetoothUuid> QLowEnergyControllerNew::services() const
{
    return d_ptr->serviceList.keys();
}

QLowEnergyService *QLowEnergyControllerNew::createServiceObject(
        const QBluetoothUuid &serviceUuid, QObject *parent)
{
    Q_D(QLowEnergyControllerNew);
    if (!d->serviceList.contains(serviceUuid))
        return 0;

    QLowEnergyService *service = new QLowEnergyService(
                d->serviceList.value(serviceUuid), parent);

    return service;
}

QLowEnergyControllerNew::Error QLowEnergyControllerNew::error() const
{
    return d_ptr->error;
}

QString QLowEnergyControllerNew::errorString() const
{
    return d_ptr->errorString;
}

QT_END_NAMESPACE