summaryrefslogtreecommitdiffstats
path: root/plugins/declarative/connectivity/qdeclarativebluetoothservice.cpp
blob: 7529857c47db7cb91a5fb0deef882e7a2f91f5cc (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativebluetoothservice_p.h"

#include <bluetooth/qbluetoothdeviceinfo.h>
#include <bluetooth/qbluetoothsocket.h>
#include <bluetooth/qbluetoothaddress.h>
#include <bluetooth/ql2capserver.h>
#include <bluetooth/qrfcommserver.h>

#include <QObject>

/* ==================== QDeclarativeBluetoothService ======================= */

/*!
   \qmlclass BluetoothService QDeclarativeBluetoothService
   \brief The BluetoothService element contains all the information available for a single bluetooth service.\.

   \ingroup connectivity-qml
   \inmodule QtConnectivity
   \since Mobility 1.2

   \sa QBluetoothAddress
   \sa QBluetoothSocket

    The BluetoothService element is part of the \bold{QtMobility.connectivity 1.2} module.

    It allows a QML project to get information about a remote service, or describe a service
    for a BluetoothSocket to connect to.
 */


class QDeclarativeBluetoothServicePrivate
{
public:
    QDeclarativeBluetoothServicePrivate()
        : m_componentComplete(false),
          m_service(0), m_port(0),
          m_needsRegistration(false),
          m_listen(0)
    {

    }

    ~QDeclarativeBluetoothServicePrivate()
    {
        delete m_service;
    }

    int listen();

    bool m_componentComplete;
    QBluetoothServiceInfo *m_service;
    QString m_protocol;
    qint32 m_port;
    QString m_description;
    QString m_name;
    QString m_uuid;
    bool m_needsRegistration;
    QObject *m_listen;

};

QDeclarativeBluetoothService::QDeclarativeBluetoothService(QObject *parent) :
    QObject(parent)
{
    d = new QDeclarativeBluetoothServicePrivate;
}

QDeclarativeBluetoothService::QDeclarativeBluetoothService(const QBluetoothServiceInfo &service, QObject *parent)
    : QObject(parent)
{
    d = new QDeclarativeBluetoothServicePrivate;
    d->m_service = new QBluetoothServiceInfo(service);
}

QDeclarativeBluetoothService::~QDeclarativeBluetoothService()
{
    delete d;
}

void QDeclarativeBluetoothService::componentComplete()
{
    d->m_componentComplete = true;

    if(d->m_needsRegistration)
        setRegistered(true);
}


/*!
  \qmlproperty string BluetoothService::deviceName
  \since Mobility 1.2

  This property holds the name of the remote device.
  */

QString QDeclarativeBluetoothService::deviceName() const
{
    if(!d->m_service)
        return QString();

    return d->m_service->device().name();
}

/*!
  \qmlproperty string BluetoothService::deviceAddress
  \since Mobility 1.2

  This property holds the remote device MAc address. Must be valid if you to
  connect to a remote device with a BluetoothSocket.
  */

QString QDeclarativeBluetoothService::deviceAddress() const
{
    if(!d->m_service)
        return QString();

    return d->m_service->device().address().toString();
}

void QDeclarativeBluetoothService::setDeviceAddress(QString address)
{
    if(!d->m_service)
        d->m_service = new QBluetoothServiceInfo();

    QBluetoothAddress a(address);
    QBluetoothDeviceInfo device(a, QString(), QBluetoothDeviceInfo::ComputerDevice);
    d->m_service->setDevice(device);
}

/*!
  \qmlproperty string BluetoothService::serviceName
  \since Mobility 1.2

  This property holds the name of the remote service if available.
  */

QString QDeclarativeBluetoothService::serviceName() const
{
    if(!d->m_service)
        return QString();

    if(!d->m_name.isEmpty())
        return d->m_name;

    return d->m_service->serviceName();
}

void QDeclarativeBluetoothService::setServiceName(QString name)
{
    d->m_name = name;
}


/*!
  \qmlproperty string BluetoothService::serviceDescription
  \since Mobility 1.2

  This property holds the description provided by the remote service.
  */
QString QDeclarativeBluetoothService::serviceDescription() const
{
    if(!d->m_service)
        return QString();

    if(!d->m_description.isEmpty())
        return d->m_name;

    return d->m_service->serviceDescription();
}

void QDeclarativeBluetoothService::setServiceDescription(QString description)
{
    d->m_description = description;
    emit detailsChanged();
}

/*!
  \qmlproperty string BluetoothService::serviceProtocol
  \since Mobility 1.2

  This property holds the protocol used for the service. Can be the string
  "l2cap" or "rfcomm"
  */

QString QDeclarativeBluetoothService::serviceProtocol() const
{
    if(!d->m_protocol.isEmpty())
        return d->m_protocol;

    if(!d->m_service)
        return QString();

    if(d->m_service->socketProtocol() == QBluetoothServiceInfo::L2capProtocol)
        return QLatin1String("l2cap");
    if(d->m_service->socketProtocol() == QBluetoothServiceInfo::RfcommProtocol)
        return QLatin1String("rfcomm");

    return QLatin1String("unknown");
}

void QDeclarativeBluetoothService::setServiceProtocol(QString protocol)
{
    if(protocol != "rfcomm" && protocol != "l2cap")
        qWarning() << "Invalid protocol" << protocol;

    d->m_protocol = protocol;
    emit detailsChanged();
}

/*!
  \qmlproperty string BluetoothService::serviceUuid
  \since Mobility 1.2

  This property holds the UUID of the remote service. Service UUID or port, as
  well as the address must be set to connect to a remote service. If UUID and
  port are set, the port is used. The UUID takes longer to connect since
  service discovery must be initiated to discover the port value.
  */

QString QDeclarativeBluetoothService::serviceUuid() const
{
    if(!d->m_uuid.isEmpty())
        return d->m_uuid;

    if(!d->m_service)
        return QString();

    return d->m_service->attribute(QBluetoothServiceInfo::ServiceId).toString();
}

void QDeclarativeBluetoothService::setServiceUuid(QString uuid)
{
    d->m_uuid = uuid;
    if(!d->m_service)
        d->m_service = new QBluetoothServiceInfo();
    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceId, QBluetoothUuid(uuid));

    emit detailsChanged();
}

/*!
  \qmlproperty int BluetoothService::servicePort
  \since Mobility 1.2

  This property holds the port value for the remote service. Bluetooth does not
  use well defined port values, so port values should not be stored and used
  later without care. Connecting via UUID is much more consistent.
  */
qint32 QDeclarativeBluetoothService::servicePort() const
{
    if(d->m_port > 0)
        return d->m_port;

    if(!d->m_service)
        return -1;

    if(d->m_service->serverChannel() > 0)
        return d->m_service->serverChannel();
    if(d->m_service->protocolServiceMultiplexer() > 0)
        return d->m_service->protocolServiceMultiplexer();

    return -1;
}

void QDeclarativeBluetoothService::setServicePort(qint32 port)
{
    if(d->m_port != port){
        d->m_port = port;
        if(isRegistered())
            setRegistered(true);
        emit detailsChanged();
    }
}

/*!
  \qmlproperty string BluetoothService::registered
  \since Mobility 1.2

  This property holds the registration/publication status of the service.  If true the service
  is published via service discovery.  Not implemented in 1.2.
  */

bool QDeclarativeBluetoothService::isRegistered() const
{
    if(!d->m_service)
        return false;

    return d->m_service->isRegistered();
}


int QDeclarativeBluetoothServicePrivate::listen() {

    if (m_protocol == "l2cap") {
        QL2capServer *server = new QL2capServer();

        server->setMaxPendingConnections(1);
        server->listen(QBluetoothAddress(), m_port);
        m_port = server->serverPort();
        m_listen = server;
    }
    else if (m_protocol == "rfcomm") {
        QRfcommServer *server = new QRfcommServer();

        server->setMaxPendingConnections(1);
        server->listen(QBluetoothAddress(), m_port);
        m_port = server->serverPort();
        m_listen = server;
    }
    else {
        qDebug() << "Unknown protocol, can't make service" << m_protocol;
    }

    return m_port;

}

void QDeclarativeBluetoothService::setRegistered(bool registered)
{

    d->m_needsRegistration = registered;

    if(!d->m_componentComplete){
        return;
    }

    if(!registered) {
        if(!d->m_service)
            return;
        d->m_service->unregisterService();
        emit registeredChanged();
    }

    if(!d->m_service){
        d->m_service = new QBluetoothServiceInfo();
    }


    delete d->m_listen;
    d->m_listen = 0;

    d->listen();
    connect(d->m_listen, SIGNAL(newConnection()), this, SLOT(new_connection()));


    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);

//    QBluetoothServiceInfo::Sequence classId;
////    classId << QVariant::fromVhttp://theunderstatement.com/alue(QBluetoothUuid(serviceUuid));
//    classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
//    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);

    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceName, d->m_name);
    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceDescription,
                             d->m_description);

    d->m_service->setServiceUuid(QBluetoothUuid(d->m_uuid));

    qDebug() << "name/uuid" << d->m_name << d->m_uuid << d->m_port;

    d->m_service->setAttribute(QBluetoothServiceInfo::BrowseGroupList,
                             QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));

    QBluetoothServiceInfo::Sequence protocolDescriptorList;
    QBluetoothServiceInfo::Sequence protocol;

    qDebug() << "Port" << d->m_port;

    if(d->m_protocol == "l2cap"){
        protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap))
                 << QVariant::fromValue(quint16(d->m_port));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
    }
    else if(d->m_protocol == "rfcomm"){
        protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
                 << QVariant::fromValue(quint8(d->m_port));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
    }
    else {
        qWarning() << "No protocol specified for bluetooth service";
    }
    d->m_service->setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                             protocolDescriptorList);

    if(d->m_service->registerService()) {
        qDebug() << "registered";
        emit registeredChanged();
    }
    else {
        qDebug() << "Failed";
    }
}

QBluetoothServiceInfo *QDeclarativeBluetoothService::serviceInfo() const
{
    return d->m_service;
}

void QDeclarativeBluetoothService::new_connection()
{
    emit newClient();
}

QDeclarativeBluetoothSocket *QDeclarativeBluetoothService::nextClient()
{
    QL2capServer *server = qobject_cast<QL2capServer *>(d->m_listen);
    if (server) {
        if(server->hasPendingConnections()){
            QBluetoothSocket *socket = server->nextPendingConnection();
            return new QDeclarativeBluetoothSocket(socket, this, 0x0);
        }
        else {
            qDebug() << "Socket has no pending connection, failing";
            return 0x0;
        }
    }
    QRfcommServer *rserver = qobject_cast<QRfcommServer *>(d->m_listen);
    if (rserver) {
        if(rserver->hasPendingConnections()){
            QBluetoothSocket *socket = rserver->nextPendingConnection();
            return new QDeclarativeBluetoothSocket(socket, this, 0x0);;
        }
        else {
            qDebug() << "Socket has no pending connection, failing";
            return 0x0;
        }
    }
    return 0x0;
}

void QDeclarativeBluetoothService::assignNextClient(QDeclarativeBluetoothSocket *dbs)
{
    QL2capServer *server = qobject_cast<QL2capServer *>(d->m_listen);
    if (server) {
        if(server->hasPendingConnections()){
            QBluetoothSocket *socket = server->nextPendingConnection();
            dbs->newSocket(socket, this);
            return;
        }
        else {
            qDebug() << "Socket has no pending connection, failing";
            return;
        }
    }
    QRfcommServer *rserver = qobject_cast<QRfcommServer *>(d->m_listen);
    if (rserver) {
        if(rserver->hasPendingConnections()){
            QBluetoothSocket *socket = rserver->nextPendingConnection();
            dbs->newSocket(socket, this);
            return;
        }
        else {
            qDebug() << "Socket has no pending connection, failing";
            return;
        }
    }
    return;
}