Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbluetoothserviceinfo_macos.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
10
11#include <QtCore/qloggingcategory.h>
12#include <QtCore/qvariant.h>
13#include <QtCore/qglobal.h>
14#include <QtCore/qmutex.h>
15#include <QtCore/qmap.h>
16#include <QtCore/qurl.h>
17
18#include <Foundation/Foundation.h>
19
20#include <IOBluetooth/IOBluetooth.h>
21
23
24namespace {
25
28
29// Alas, since there is no d_ptr<->q_ptr link (which is not that bad in itself),
30// I need these getters duplicated here:
32{
33 ServiceInfo::Sequence parameters = privateInfo.protocolDescriptor(QBluetoothUuid::ProtocolUuid::Rfcomm);
34 if (!parameters.isEmpty())
35 return ServiceInfo::RfcommProtocol;
36
37 parameters = privateInfo.protocolDescriptor(QBluetoothUuid::ProtocolUuid::L2cap);
38 if (!parameters.isEmpty())
39 return ServiceInfo::L2capProtocol;
40
41 return ServiceInfo::UnknownProtocol;
42}
43
45{
46 const auto parameters = privateInfo.protocolDescriptor(uuid);
47 if (parameters.isEmpty())
48 return -1;
49 else if (parameters.size() == 1)
50 return 0;
51
52 return parameters.at(1).toInt();
53}
54
55} // unnamed namespace
56
58{
59}
60
62{
63}
64
66{
67 Q_UNUSED(localAddress);
68 return false;
69}
70
72{
73 using namespace DarwinBluetooth;
74
75 if (isRegistered())
76 return false;
77
78 using namespace DarwinBluetooth;
79
80 ObjCStrongReference<NSMutableDictionary> serviceDict(iobluetooth_service_dictionary(info));
81 if (!serviceDict) {
82 qCWarning(QT_BT_DARWIN) << "failed to create a service dictionary";
83 return false;
84 }
85
86 Q_ASSERT(!registered);
87 Q_ASSERT_X(!serviceRecord, Q_FUNC_INFO, "not registered, but serviceRecord is not nil");
88
89 SDPRecord newRecord;
90 newRecord.reset([IOBluetoothSDPServiceRecord
91 publishedServiceRecordWithDictionary:serviceDict], RetainPolicy::doInitialRetain);
92 if (!newRecord) {
93 qCWarning(QT_BT_DARWIN) << "failed to register a service record";
94 return false;
95 }
96
97 BluetoothSDPServiceRecordHandle newRecordHandle = 0;
98 auto *ioSDPRecord = newRecord.getAs<IOBluetoothSDPServiceRecord>();
99 if ([ioSDPRecord getServiceRecordHandle:&newRecordHandle] != kIOReturnSuccess) {
100 qCWarning(QT_BT_DARWIN) << "failed to register a service record";
101 [ioSDPRecord removeServiceRecord];
102 return false;
103 }
104
105 const ServiceInfo::Protocol type = info.socketProtocol();
106 quint16 realPort = 0;
108 bool configured = false;
109
111 BluetoothL2CAPPSM psm = 0;
112 server = QBluetoothServerPrivate::registeredServer(info.protocolServiceMultiplexer(), type);
113 if ([ioSDPRecord getL2CAPPSM:&psm] == kIOReturnSuccess) {
114 configured = true;
115 realPort = psm;
116 }
118 BluetoothRFCOMMChannelID channelID = 0;
119 server = QBluetoothServerPrivate::registeredServer(info.serverChannel(), type);
120 if ([ioSDPRecord getRFCOMMChannelID:&channelID] == kIOReturnSuccess) {
121 configured = true;
122 realPort = channelID;
123 }
124 }
125
126 if (!configured) {
127 [ioSDPRecord removeServiceRecord];
128 qCWarning(QT_BT_DARWIN) << "failed to register a service record";
129 return false;
130 }
131
132 registered = true;
133 serviceRecord.swap(newRecord);
134 serviceRecordHandle = newRecordHandle;
135
136 if (server)
137 server->startListener(realPort);
138
139 return true;
140}
141
143{
144 return registered;
145}
146
148{
149 if (!registered)
150 return false;
151
152 Q_ASSERT_X(serviceRecord, Q_FUNC_INFO, "service registered, but serviceRecord is nil");
153
154 auto *nativeRecord = serviceRecord.getAs<IOBluetoothSDPServiceRecord>();
155 [nativeRecord removeServiceRecord];
156 serviceRecord.reset();
157
158 const ServiceInfo::Protocol type = socket_protocol(*this);
160
161 const QMutexLocker lock(&QBluetoothServerPrivate::channelMapMutex());
162 if (type == ServiceInfo::RfcommProtocol)
163 server = QBluetoothServerPrivate::registeredServer(channel_or_psm(*this, QBluetoothUuid::ProtocolUuid::Rfcomm), type);
164 else if (type == ServiceInfo::L2capProtocol)
165 server = QBluetoothServerPrivate::registeredServer(channel_or_psm(*this, QBluetoothUuid::ProtocolUuid::L2cap), type);
166
167 if (server)
168 server->stopListener();
169
170 registered = false;
171 serviceRecordHandle = 0;
172
173 return true;
174}
175
\inmodule QtBluetooth
bool registerService(const QBluetoothAddress &localAdapter=QBluetoothAddress())
\inmodule QtBluetooth
Protocol
This enum describes the socket protocol used by the service.
ProtocolUuid
This enum is a convienience type for Bluetooth protocol UUIDs.
bool isEmpty() const noexcept
Definition qlist.h:401
\inmodule QtCore
Definition qmutex.h:313
Dictionary iobluetooth_service_dictionary(const QBluetoothServiceInfo &serviceInfo)
Combined button and popup list for selecting options.
ServiceInfo::Protocol socket_protocol(const QBluetoothServiceInfoPrivate &privateInfo)
QBluetoothServiceInfo ServiceInfo
int channel_or_psm(const QBluetoothServiceInfoPrivate &privateInfo, QBluetoothUuid::ProtocolUuid uuid)
#define Q_FUNC_INFO
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
#define qCWarning(category,...)
GLenum type
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
#define Q_UNUSED(x)
unsigned short quint16
Definition qtypes.h:48
QReadWriteLock lock
[0]
QHostInfo info
[0]