summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qbluetoothdevicediscoveryagent_qnx.cpp
blob: a20ab90b7d1cf589cb7c5135b26f73622a805097 (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
/***************************************************************************
**
** Copyright (C) 2012 Research In Motion
** 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 "qbluetoothdevicediscoveryagent.h"
#include "qbluetoothdevicediscoveryagent_p.h"
#include "qbluetoothaddress.h"
#include "qbluetoothuuid.h"

#include <QtCore/private/qcore_unix_p.h>


QT_BEGIN_NAMESPACE

QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(const QBluetoothAddress &deviceAdapter):
    QObject(0), lastError(QBluetoothDeviceDiscoveryAgent::NoError),
    m_rdfd(-1), m_active(false), m_nextOp(None), m_currentOp(None)
{
    Q_UNUSED(deviceAdapter);
    inquiryType = QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry;
    ppsRegisterControl();
    ppsRegisterForEvent(QStringLiteral("device_added"), this);
    ppsRegisterForEvent(QStringLiteral("device_search"), this);
    connect(&m_finishedTimer, SIGNAL(timeout()), this, SLOT(finished()));
}

QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate()
{
    if (m_active)
        stop();
    ppsUnregisterForEvent(QStringLiteral("device_added"), this);
    ppsUnregisterForEvent(QStringLiteral("device_search"), this);
    ppsUnregisterControl(this);
}

bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const
{
    return m_active;
}

void QBluetoothDeviceDiscoveryAgentPrivate::start()
{
    Q_Q(QBluetoothDeviceDiscoveryAgent);
    m_active = true;
    isFinished = false;
    discoveredDevices.clear();

    if (m_currentOp == Cancel) {
        m_nextOp = Start;
        return;
    }
    if (m_nextOp == Cancel) {
        m_nextOp = None;
    }
    m_currentOp = Start;

    if (m_rdfd != -1) {
        qCDebug(QT_BT_QNX) << "RDev FD still open";
    } else if ((m_rdfd = qt_safe_open("/pps/services/bluetooth/remote_devices/.all", O_RDONLY)) == -1) {
        qCWarning(QT_BT_QNX) << Q_FUNC_INFO << "rdfd - failed to open /pps/services/bluetooth/remote_devices/.all"
                      << m_rdfd;
        lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
        errorString = QBluetoothDeviceDiscoveryAgent::tr("Cannot open remote device socket");
        emit q->error(lastError);
        stop();
        return;
    } else {
        m_rdNotifier = new QSocketNotifier(m_rdfd, QSocketNotifier::Read, this);
        if (!m_rdNotifier) {
            qCWarning(QT_BT_QNX) << Q_FUNC_INFO << "failed to connect to m_rdNotifier";
            lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
            errorString = QBluetoothDeviceDiscoveryAgent::tr("Cannot connect to Bluetooth socket notifier");
            emit q->error(lastError);
            stop();
            return;
        }
    }

    if (ppsSendControlMessage("device_search", this)) {
        //If there is no new results after 7 seconds, the device inquire will be stopped
        m_finishedTimer.start(10000);
        connect(m_rdNotifier, SIGNAL(activated(int)), this, SLOT(remoteDevicesChanged(int)));
    } else {
        qCWarning(QT_BT_QNX) << "Could not write to control FD";
        m_active = false;
        lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
        errorString = QBluetoothDeviceDiscoveryAgent::tr("Cannot start device inquiry");
        q->error(QBluetoothDeviceDiscoveryAgent::InputOutputError);
        return;
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::stop()
{
    m_active = false;
    m_finishedTimer.stop();
    if (m_currentOp == Start) {
        m_nextOp = Cancel;
        return;
    }
    m_currentOp = Cancel;

    qCDebug(QT_BT_QNX) << "Stopping device search";
    ppsSendControlMessage("cancel_device_search",this);

    if (m_rdNotifier) {
        delete m_rdNotifier;
        m_rdNotifier = 0;
    }
    if (m_rdfd != -1) {
        qt_safe_close(m_rdfd);
        m_rdfd = -1;
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::remoteDevicesChanged(int fd)
{
    if (!m_active)
        return;
    pps_decoder_t ppsDecoder;
    pps_decoder_initialize(&ppsDecoder, NULL);

    QBluetoothAddress deviceAddr;
    QString deviceName;

    if (!ppsReadRemoteDevice(fd, &ppsDecoder, &deviceAddr, &deviceName)) {
        return;
    }

    bool paired = false;
    int cod = 0;
    int dev_type = 0;
    int rssi = 0;
    bool hasGatt = false;
    pps_decoder_get_bool(&ppsDecoder, "paired", &paired);
    pps_decoder_get_int(&ppsDecoder, "cod", &cod);
    pps_decoder_get_int(&ppsDecoder, "dev_type", &dev_type);
    pps_decoder_get_int(&ppsDecoder, "rssi", &rssi);
    pps_decoder_push(&ppsDecoder, "gatt_available_services");
    const char *next_service = 0;

    for (int service_count=0; pps_decoder_get_string(&ppsDecoder, 0, &next_service ) == PPS_DECODER_OK; service_count++) {
        hasGatt = true;
        //qBluetoothDebug() << next_service;
    }
    pps_decoder_cleanup(&ppsDecoder);

    QBluetoothDeviceInfo deviceInfo(deviceAddr, deviceName, cod);
    deviceInfo.setRssi(rssi);

    bool updated = false;
    //Prevent a device from beeing listed twice
    for (int i=0; i < discoveredDevices.size(); i++) {
        if (discoveredDevices.at(i).address() == deviceInfo.address()) {
            updated = true;
            if (discoveredDevices.at(i) == deviceInfo) {
                return;
            } else {
                discoveredDevices.removeAt(i);
                break;
            }
        }
    }
    //Starts the timer again
    m_finishedTimer.start(7000);
    if (!deviceAddr.isNull()) {
        qCDebug(QT_BT_QNX) << "Device discovered: " << deviceName << deviceAddr.toString();
        /* Looking for device type. Only Low energy devices will be added
         * BT_DEVICE_TYPE_LE_PUBLIC is 0 --->LE device
         * BT_DEVICE_TYPE_LE_PRIVATE is 1 ---> LE device
         * BT_DEVICE_TYPE_REGULAR is 32
         * BT_DEVICE_TYPE_UNKNOWN is 255
         */
        if (dev_type == 0 || dev_type == 1)
            deviceInfo.setCoreConfiguration(QBluetoothDeviceInfo::LowEnergyCoreConfiguration);
        else{
            if (hasGatt)
                deviceInfo.setCoreConfiguration(QBluetoothDeviceInfo::BaseRateAndLowEnergyCoreConfiguration);
            else
                deviceInfo.setCoreConfiguration(QBluetoothDeviceInfo::BaseRateCoreConfiguration);
        }
        discoveredDevices.append(deviceInfo);
        if (!updated)//We are not allowed to emit a signal with the updated version
            emit q_ptr->deviceDiscovered(discoveredDevices.last());
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::controlReply(ppsResult result)
{
    Q_Q(QBluetoothDeviceDiscoveryAgent);
    if (result.msg == QStringLiteral("device_search") && m_currentOp == Start) {
        if (result.dat.size() > 0 && result.dat.first() == QStringLiteral("EOK")) {
            //Do nothing. We can not be certain, that the device search is over yet
        } else if (result.error == 16) {
            qCDebug(QT_BT_QNX) << "Could not start device inquire bc resource is busy";
            if (m_nextOp == None) { //We try again
                ppsSendControlMessage("cancel_device_search",this);
                QTimer::singleShot(5000, this, SLOT(startDeviceSearch()));
                m_finishedTimer.start(20000);
            }
            return;
        } else {
            qCWarning(QT_BT_QNX) << "A PPS Bluetooth error occurred:";
            lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
            errorString = result.errorMsg;
            Q_EMIT q_ptr->error(QBluetoothDeviceDiscoveryAgent::InputOutputError);
            stop();
        }
        processNextOp();
    } else if (result.msg == QStringLiteral("cancel_device_search") && m_currentOp == Cancel && !isFinished) {
        qCDebug(QT_BT_QNX) << "Cancel device search";
//        if (!result.errorMsg.isEmpty()) {
//            lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
//            errorString = result.errorMsg;
//            q_ptr->error(QBluetoothDeviceDiscoveryAgent::InputOutputError);
//        }
        emit q->canceled();
        processNextOp();
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::controlEvent(ppsResult result)
{
    if (result.msg == QStringLiteral("device_added")) {
        qCDebug(QT_BT_QNX) << "Device was added" << result.dat.first();
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::finished()
{
    if (m_active) {
        qCDebug(QT_BT_QNX) << "Device discovery finished";
        isFinished = true;
        stop();
        q_ptr->finished();
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::startDeviceSearch()
{
    if (m_currentOp == Start) {
        ppsSendControlMessage("device_search", this); //Try again
    }
}

void QBluetoothDeviceDiscoveryAgentPrivate::processNextOp()
{
    if (m_currentOp == m_nextOp) {
        m_currentOp = None;
        m_nextOp = None;
    }
    m_currentOp = m_nextOp;
    m_nextOp = None;

    if (m_currentOp == Start) {
        start();
    } else if (m_currentOp == Cancel) {
        stop();
    }
}

QT_END_NAMESPACE