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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
/****************************************************************************
**
** Copyright (C) 2013 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 "qbluetoothserviceinfo.h"
#include "qbluetoothserviceinfo_p.h"
#include <QUrl>
QT_BEGIN_NAMESPACE_BLUETOOTH
/*!
\class QBluetoothServiceInfo::Sequence
\inmodule QtBluetooth
\brief The Sequence class stores attributes of a Bluetooth Data Element
Sequence.
*/
/*!
\fn QBluetoothServiceInfo::Sequence::Sequence()
Constructs a new empty sequence.
*/
/*!
\fn QBluetoothServiceInfo::Sequence::Sequence(const QList<QVariant> &list)
Constructs a new sequence that is a copy of \a list.
*/
/*!
\class QBluetoothServiceInfo::Alternative
\inmodule QtBluetooth
\brief The Alternative class stores attributes of a Bluetooth Data Element
Alternative.
*/
/*!
\fn QBluetoothServiceInfo::Alternative::Alternative()
Constructs a new empty alternative.
*/
/*!
\fn QBluetoothServiceInfo::Alternative::Alternative(const QList<QVariant> &list)
Constructs a new alternative that is a copy of \a list.
*/
/*!
\class QBluetoothServiceInfo
\inmodule QtBluetooth
\brief The QBluetoothServiceInfo class enables access to the attributes of a
Bluetooth service.
QBluetoothServiceInfo provides information about a service offered by a Bluetooth device.
In addition it can be used to register new services on the local device. Note that such
a registration only affects the Bluetooth SDP entries. Any server listening
for incoming connections (e.g an RFCOMM server) must be started before registerService()
is called. Deregistration must happen in the reverse order.
QBluetoothServiceInfo is not a value type in the traditional sense. All copies of the same
service info object share the same data as they do not detach upon changing them. This
ensures that two copies can (de)register the same Bluetooth service.
*/
/*!
\enum QBluetoothServiceInfo::AttributeId
Bluetooth service attributes. Please check the Bluetooth Core Specification for a more detailed description of these attributes.
\value ServiceRecordHandle Specifies a service record from which attributes can be retrieved.
\value ServiceClassIds UUIDs of service classes that the service conforms to.
\value ServiceRecordState Attibute changes when any other service attribute is added, deleted or modified.
\value ServiceId UUID that uniquely identifies the service.
\value ProtocolDescriptorList List of protocols used by the service.
\value BrowseGroupList List of browse groups the service is in.
\value LanguageBaseAttributeIdList List of language base attribute IDs to support human-readable attributes.
\value ServiceInfoTimeToLive Number of seconds for which the service record is expected to remain valid and unchanged.
\value ServiceAvailability Value indicating the availability of the service.
\value BluetoothProfileDescriptorList List of profiles to which the service conforms.
\value DocumentationUrl URL that points to the documentation on the service..
\value ClientExecutableUrl URL that refers to the location of an application that can be used to utilize the service.
\value IconUrl URL to the location of the icon representing the service.
\value AdditionalProtocolDescriptorList Additional protocols used by the service. This attribute extends the \l QBluetoothServiceInfo::ProtocolDescriptorList.
\value PrimaryLanguageBase Base index for primary language text descriptors.
\value ServiceName Name of the Bluetooth service in the primary language.
\value ServiceDescription Description of the Bluetooth service in the primary language.
\value ServiceProvider Name of the company / entity that provides the Bluetooth service primary language.
*/
/*!
\enum QBluetoothServiceInfo::Protocol
This enum describes the socket protocol used by the service.
\value UnknownProtocol The service uses an unknown socket protocol.
\value L2capProtocol The service uses the L2CAP socket protocol.
\value RfcommProtocol The service uses the RFCOMM socket protocol.
*/
/*!
\fn bool QBluetoothServiceInfo::isRegistered() const
Returns true if the service information is registered with the platform's Service Discovery Protocol
(SDP) implementation, otherwise returns false.
*/
bool QBluetoothServiceInfo::isRegistered() const
{
return d_ptr->isRegistered();
}
/*!
\fn bool QBluetoothServiceInfo::registerService() const
Registers this service with the platform's Service Discovery Protocol (SDP) implementation,
making it findable by other devices when they perform service discovery. Returns true if the
service is successfully registered, otherwise returns false. Once registered changes to the record
cannot be made. The service must be unregistered and registered again with the changes.
*/
bool QBluetoothServiceInfo::registerService() const
{
return d_ptr->registerService();
}
/*!
\fn bool QBluetoothServiceInfo::unregisterService() const
Unregisters this service with the platform's Service Discovery Protocol (SDP) implementation.
After this, the service will no longer be findable by other devices through service discovery.
Returns true if the service is successfully unregistered, otherwise returns false.
*/
bool QBluetoothServiceInfo::unregisterService() const
{
return d_ptr->unregisterService();
}
/*!
\fn void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QBluetoothUuid &value)
This is a convenience function.
Sets the attribute identified by \a attributeId to \a value.
*/
/*!
\fn void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QBluetoothServiceInfo::Sequence &value)
This is a convenience function.
Sets the attribute identified by \a attributeId to \a value.
*/
/*!
\fn void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QBluetoothServiceInfo::Alternative &value)
This is a convenience function.
Sets the attribute identified by \a attributeId to \a value.
*/
/*!
\fn void QBluetoothServiceInfo::setServiceName(const QString &name)
This is a convenience function. It is equivalent to calling
setAttribute(QBluetoothServiceInfo::ServiceName, name).
Sets the service name in the primary language to \a name.
\sa serviceName(), setAttribute()
*/
/*!
\fn QString QBluetoothServiceInfo::serviceName() const
This is a convenience function. It is equivalent to calling
attribute(QBluetoothServiceInfo::ServiceName).toString().
Returns the service name in the primary language.
\sa setServiceName(), attribute()
*/
/*!
\fn void QBluetoothServiceInfo::setServiceDescription(const QString &description)
This is a convenience function. It is equivalent to calling
setAttribute(QBluetoothServiceInfo::ServiceDescription, description).
Sets the service description in the primary language to \a description.
\sa serviceDescription(), setAttribute()
*/
/*!
\fn QString QBluetoothServiceInfo::serviceDescription() const
This is a convenience function. It is equivalent to calling
attribute(QBluetoothServiceInfo::ServiceDescription).toString().
Returns the service description in the primary language.
\sa setServiceDescription(), attribute()
*/
/*!
\fn void QBluetoothServiceInfo::setServiceProvider(const QString &provider)
This is a convenience function. It is equivalent to calling
setAttribute(QBluetoothServiceInfo::ServiceProvider, provider).
Sets the service provider in the primary language to \a provider.
\sa serviceProvider(), setAttribute()
*/
/*!
\fn QString QBluetoothServiceInfo::serviceProvider() const
This is a convenience function. It is equivalent to calling
attribute(QBluetoothServiceInfo::ServiceProvider).toString().
Returns the service provider in the primary language.
\sa setServiceProvider(), attribute()
*/
/*!
\fn void QBluetoothServiceInfo::setServiceAvailability(quint8 availability)
This is a convenience function. It is equivalent to calling
setAttribute(QBluetoothServiceInfo::ServiceAvailability, availability).
Sets the availabiltiy of the service to \a availability.
\sa serviceAvailability(), setAttribute()
*/
/*!
\fn quint8 QBluetoothServiceInfo::serviceAvailability() const
This is a convenience function. It is equivalent to calling
attribute(QBluetoothServiceInfo::ServiceAvailability).toUInt().
Returns the availability of the service.
\sa setServiceAvailability(), attribute()
*/
/*!
\fn void QBluetoothServiceInfo::setServiceUuid(const QBluetoothUuid &uuid)
This is a convenience function. It is equivalent to calling
setAttribute(QBluetoothServiceInfo::ServiceId, uuid).
Sets the service UUID to \a uuid.
\sa serviceUuid(), setAttribute()
*/
/*!
\fn QBluetoothUuid QBluetoothServiceInfo::serviceUuid() const
This is a convenience function. It is equivalent to calling
attribute(QBluetoothServiceInfo::ServiceId).value<QBluetoothUuid>().
Returns the UUID of the service.
\sa setServiceUuid(), attribute()
*/
/*!
\fn QList<QBluetoothUuid> QBluetoothServiceInfo::serviceClassUuids() const
This is a convenience function. It is equivalent to calling
attribute(QBluetoothServiceInfo::ServiceClassIds).value<QList<QBluetoothUuid> >().
Returns a list of UUIDs describing the service classes that this service conforms to.
\sa attribute()
*/
/*!
Construct a new invalid QBluetoothServiceInfo;
*/
QBluetoothServiceInfo::QBluetoothServiceInfo()
: d_ptr(QSharedPointer<QBluetoothServiceInfoPrivate>(new QBluetoothServiceInfoPrivate))
{
}
/*!
Construct a new QBluetoothServiceInfo that is a copy of \a other.
The two copies continue to share the same underlying data which does not detach
upon write.
*/
QBluetoothServiceInfo::QBluetoothServiceInfo(const QBluetoothServiceInfo &other)
: d_ptr(other.d_ptr)
{
}
/*!
Destroys the QBluetoothServiceInfo object.
*/
QBluetoothServiceInfo::~QBluetoothServiceInfo()
{
}
/*!
Returns true if the QBluetoothServiceInfo object is valid, otherwise returns false.
An invalid QBluetoothServiceInfo object will have no attributes.
*/
bool QBluetoothServiceInfo::isValid() const
{
return !d_ptr->attributes.isEmpty();
}
/*!
Returns true if the QBluetoothServiceInfo object is considered complete, otherwise returns false.
A complete QBluetoothServiceInfo object contains a ProtocolDescriptorList attribute.
*/
bool QBluetoothServiceInfo::isComplete() const
{
return d_ptr->attributes.keys().contains(ProtocolDescriptorList);
}
/*!
Returns the address of the Bluetooth device that provides this service.
*/
QBluetoothDeviceInfo QBluetoothServiceInfo::device() const
{
return d_ptr->deviceInfo;
}
/*!
Sets the Bluetooth device that provides this service to \a device.
*/
void QBluetoothServiceInfo::setDevice(const QBluetoothDeviceInfo &device)
{
d_ptr->deviceInfo = device;
}
/*!
Sets the attribute identified by \a attributeId to \a value.
If the service information is registered with the platforms SDP database, the database entry is also
updated.
\sa isRegistered(), registerService()
*/
void QBluetoothServiceInfo::setAttribute(quint16 attributeId, const QVariant &value)
{
if (value.type() == QVariant::List)
qDebug() << "tried attribute with type QVariantList" << value;
d_ptr->attributes[attributeId] = value;
if (isRegistered())
d_ptr->setRegisteredAttribute(attributeId, value);
}
/*!
Returns the value of the attribute \a attributeId.
*/
QVariant QBluetoothServiceInfo::attribute(quint16 attributeId) const
{
return d_ptr->attributes.value(attributeId);
}
/*!
Returns a list of all attribute ids that the QBluetoothServiceInfo object has.
*/
QList<quint16> QBluetoothServiceInfo::attributes() const
{
return d_ptr->attributes.keys();
}
/*!
Returns true if the QBluetoothServiceInfo object contains the attribute \a attributeId, otherwise returns
false.
*/
bool QBluetoothServiceInfo::contains(quint16 attributeId) const
{
return d_ptr->attributes.contains(attributeId);
}
/*!
Removes the attribute \a attributeId from the QBluetoothServiceInfo object.
*/
void QBluetoothServiceInfo::removeAttribute(quint16 attributeId)
{
d_ptr->attributes.remove(attributeId);
if (isRegistered())
d_ptr->removeRegisteredAttribute(attributeId);
}
/*!
Returns the protocol that the QBluetoothServiceInfo object uses.
*/
QBluetoothServiceInfo::Protocol QBluetoothServiceInfo::socketProtocol() const
{
QBluetoothServiceInfo::Sequence parameters = protocolDescriptor(QBluetoothUuid::Rfcomm);
if (!parameters.isEmpty())
return RfcommProtocol;
parameters = protocolDescriptor(QBluetoothUuid::L2cap);
if (!parameters.isEmpty())
return L2capProtocol;
return UnknownProtocol;
}
/*!
This is a convenience function. Returns the protocol/service multiplexer for services which
support the L2CAP protocol, otherwise returns -1.
This function is equivalent to extracting the information from
QBluetoothServiceInfo::Sequence returned by
QBluetoothServiceInfo::attribute(QBluetoothServiceInfo::ProtocolDescriptorList).
*/
int QBluetoothServiceInfo::protocolServiceMultiplexer() const
{
QBluetoothServiceInfo::Sequence parameters = protocolDescriptor(QBluetoothUuid::L2cap);
if (parameters.isEmpty())
return -1;
else if (parameters.count() == 1)
return 0;
else
return parameters.at(1).toUInt();
}
/*!
This is a convenience function. Returns the server channel for services which support the
RFCOMM protocol, otherwise returns -1.
This function is equivalent to extracting the information from
QBluetoothServiceInfo::Sequence returned by
QBluetoothServiceInfo::attribute(QBluetootherServiceInfo::ProtocolDescriptorList).
*/
int QBluetoothServiceInfo::serverChannel() const
{
return d_ptr->serverChannel();
}
/*!
Returns the protocol parameters as a QBluetoothServiceInfo::Sequence for protocol \a protocol.
An empty QBluetoothServiceInfo::Sequence is returned if \a protocol is not supported.
*/
QBluetoothServiceInfo::Sequence QBluetoothServiceInfo::protocolDescriptor(QBluetoothUuid::ProtocolUuid protocol) const
{
return d_ptr->protocolDescriptor(protocol);
}
/*!
Makes a copy of the \a other and assigns it to this QBluetoothServiceInfo object.
The two copies continue to share the same service and registration details.
*/
QBluetoothServiceInfo &QBluetoothServiceInfo::operator=(const QBluetoothServiceInfo &other)
{
d_ptr = other.d_ptr;
return *this;
}
static void dumpAttributeVariant(const QVariant &var, const QString indent)
{
switch (int(var.type())) {
case QMetaType::Void:
qDebug("%sEmpty", indent.toLocal8Bit().constData());
break;
case QMetaType::UChar:
qDebug("%suchar %u", indent.toLocal8Bit().constData(), var.toUInt());
break;
case QMetaType::UShort:
qDebug("%sushort %u", indent.toLocal8Bit().constData(), var.toUInt());
case QMetaType::UInt:
qDebug("%suint %u", indent.toLocal8Bit().constData(), var.toUInt());
break;
case QMetaType::Char:
qDebug("%schar %d", indent.toLocal8Bit().constData(), var.toInt());
break;
case QMetaType::Short:
qDebug("%sshort %d", indent.toLocal8Bit().constData(), var.toInt());
break;
case QMetaType::Int:
qDebug("%sint %d", indent.toLocal8Bit().constData(), var.toInt());
break;
case QMetaType::QString:
qDebug("%sstring %s", indent.toLocal8Bit().constData(), var.toString().toLocal8Bit().constData());
break;
case QMetaType::Bool:
qDebug("%sbool %d", indent.toLocal8Bit().constData(), var.toBool());
break;
case QMetaType::QUrl:
qDebug("%surl %s", indent.toLocal8Bit().constData(), var.toUrl().toString().toLocal8Bit().constData());
break;
case QVariant::UserType:
if (var.userType() == qMetaTypeId<QBluetoothUuid>()) {
QBluetoothUuid uuid = var.value<QBluetoothUuid>();
switch (uuid.minimumSize()) {
case 0:
qDebug("%suuid NULL", indent.toLocal8Bit().constData());
break;
case 2:
qDebug("%suuid %04x", indent.toLocal8Bit().constData(), uuid.toUInt16());
break;
case 4:
qDebug("%suuid %08x", indent.toLocal8Bit().constData(), uuid.toUInt32());
break;
case 16:
qDebug("%suuid %s", indent.toLocal8Bit().constData(), QByteArray(reinterpret_cast<const char *>(uuid.toUInt128().data), 16).toHex().constData());
break;
default:
qDebug("%suuid ???", indent.toLocal8Bit().constData());
;
}
} else if (var.userType() == qMetaTypeId<QBluetoothServiceInfo::Sequence>()) {
qDebug("%sSequence", indent.toLocal8Bit().constData());
const QBluetoothServiceInfo::Sequence *sequence = static_cast<const QBluetoothServiceInfo::Sequence *>(var.data());
foreach (const QVariant &v, *sequence)
dumpAttributeVariant(v, indent + QLatin1Char('\t'));
} else if (var.userType() == qMetaTypeId<QBluetoothServiceInfo::Alternative>()) {
qDebug("%sAlternative", indent.toLocal8Bit().constData());
const QBluetoothServiceInfo::Alternative *alternative = static_cast<const QBluetoothServiceInfo::Alternative *>(var.data());
foreach (const QVariant &v, *alternative)
dumpAttributeVariant(v, indent + QLatin1Char('\t'));
}
break;
default:
qDebug("%sunknown variant type %d", indent.toLocal8Bit().constData(), var.userType());
}
}
QDebug operator<<(QDebug dbg, const QBluetoothServiceInfo &info)
{
foreach (quint16 id, info.attributes()) {
dumpAttributeVariant(info.attribute(id), QString::fromLatin1("(%1)\t").arg(id));
}
return dbg;
}
QBluetoothServiceInfo::Sequence QBluetoothServiceInfoPrivate::protocolDescriptor(QBluetoothUuid::ProtocolUuid protocol) const
{
if (!attributes.contains(QBluetoothServiceInfo::ProtocolDescriptorList))
return QBluetoothServiceInfo::Sequence();
foreach (const QVariant &v, attributes.value(QBluetoothServiceInfo::ProtocolDescriptorList).value<QBluetoothServiceInfo::Sequence>()) {
QBluetoothServiceInfo::Sequence parameters = v.value<QBluetoothServiceInfo::Sequence>();
if (parameters.empty())
continue;
if (parameters.at(0).userType() == qMetaTypeId<QBluetoothUuid>()) {
if (parameters.at(0).value<QBluetoothUuid>() == protocol)
return parameters;
}
}
return QBluetoothServiceInfo::Sequence();
}
int QBluetoothServiceInfoPrivate::serverChannel() const
{
QBluetoothServiceInfo::Sequence parameters = protocolDescriptor(QBluetoothUuid::Rfcomm);
if (parameters.isEmpty())
return -1;
else if (parameters.count() == 1)
return 0;
else
return parameters.at(1).toUInt();
}
QT_END_NAMESPACE_BLUETOOTH
|