Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef OSXBTUTILITY_P_H
41#define OSXBTUTILITY_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include "osxbluetooth_p.h"
55
56#include <QtCore/qloggingcategory.h>
57#include <QtCore/qscopedpointer.h>
58#include <QtCore/qglobal.h>
59
60#include <Foundation/Foundation.h>
61
62QT_BEGIN_NAMESPACE
63
64class QLowEnergyCharacteristicData;
65class QBluetoothAddress;
66class QBluetoothUuid;
67
68namespace OSXBluetooth {
69
70struct NSObjectDeleter {
71 static void cleanup(NSObject *obj)
72 {
73 [obj release];
74 }
75};
76
77template<class T>
78class ObjCScopedPointer : public QScopedPointer<NSObject, NSObjectDeleter>
79{
80public:
81 // TODO: remove default argument, add 'retain' parameter,
82 // add a default ctor??? This will make the semantics more
83 // transparent + will simplify the future transition to ARC
84 // (if it will ever happen).
85 explicit ObjCScopedPointer(T *ptr = nullptr) : QScopedPointer(ptr){}
86 operator T*() const
87 {
88 return data();
89 }
90
91 T *data()const
92 {
93 return static_cast<T *>(QScopedPointer::data());
94 }
95
96 T *take()
97 {
98 return static_cast<T *>(QScopedPointer::take());
99 }
100};
101
102#define QT_BT_MAC_AUTORELEASEPOOL const QMacAutoReleasePool pool;
103
104template<class T>
105class ObjCStrongReference {
106public:
107 ObjCStrongReference()
108 : m_ptr(nil)
109 {
110 }
111 ObjCStrongReference(T *obj, bool retain)
112 {
113 if (retain)
114 m_ptr = [obj retain];
115 else
116 m_ptr = obj; // For example, created with initWithXXXX.
117 }
118 ObjCStrongReference(const ObjCStrongReference &rhs)
119 {
120 m_ptr = [rhs.m_ptr retain];
121 }
122 ObjCStrongReference &operator = (const ObjCStrongReference &rhs)
123 {
124 // "Old-style" implementation:
125 if (this != &rhs && m_ptr != rhs.m_ptr) {
126 [m_ptr release];
127 m_ptr = [rhs.m_ptr retain];
128 }
129
130 return *this;
131 }
132
133#ifdef Q_COMPILER_RVALUE_REFS
134 ObjCStrongReference(ObjCStrongReference &&xval)
135 {
136 m_ptr = xval.m_ptr;
137 xval.m_ptr = nil;
138 }
139
140 ObjCStrongReference &operator = (ObjCStrongReference &&xval)
141 {
142 m_ptr = xval.m_ptr;
143 xval.m_ptr = nil;
144 return *this;
145 }
146#endif
147
148 ~ObjCStrongReference()
149 {
150 [m_ptr release];
151 }
152
153 void reset(T *newVal)
154 {
155 if (m_ptr != newVal) {
156 [m_ptr release];
157 m_ptr = [newVal retain];
158 }
159 }
160
161 void resetWithoutRetain(T *newVal)
162 {
163 if (m_ptr != newVal) {
164 [m_ptr release];
165 m_ptr = newVal;
166 }
167 }
168
169 operator T *() const
170 {
171 return m_ptr;
172 }
173
174 T *data() const
175 {
176 return m_ptr;
177 }
178
179 T *take()
180 {
181 T * p = m_ptr;
182 m_ptr = nil;
183 return p;
184 }
185private:
186 T *m_ptr;
187};
188
189// The type 'T' is some XXXRef from CoreFoundation and co.
190// In principle, we can do a trick removing a pointer from a type
191// when template is instantiated, but it's quite a lot of ugly pp-tokens
192// like OSXBluetooth::CFStrongReference<OSXBluetooth::remove_pointer<CFUUIDRref> > strongReference;
193// so instead we use 'T' everywhere, not 'T *' as can expected
194// from a smart pointer.
195template<class T>
196class CFStrongReference {
197public:
198 CFStrongReference()
199 : m_ptr(nullptr)
200 {
201 }
202
203 CFStrongReference(T obj, bool retain)
204 : m_ptr(obj)
205 {
206 if (m_ptr && retain)
207 CFRetain(m_ptr);
208 }
209
210 CFStrongReference(const CFStrongReference &rhs)
211 {
212 if ((m_ptr = rhs.m_ptr))
213 CFRetain(m_ptr);
214 }
215
216 CFStrongReference &operator = (const CFStrongReference &rhs)
217 {
218 // "Old-style" implementation:
219 if (this != &rhs && m_ptr != rhs.m_ptr) {
220 if (m_ptr)
221 CFRelease(m_ptr);
222 if ((m_ptr = rhs.m_ptr))
223 CFRetain(m_ptr);
224 }
225
226 return *this;
227 }
228
229#ifdef Q_COMPILER_RVALUE_REFS
230 CFStrongReference(CFStrongReference &&xval)
231 {
232 m_ptr = xval.m_ptr;
233 xval.m_ptr = nullptr;
234 }
235
236 CFStrongReference &operator = (CFStrongReference &&xval)
237 {
238 m_ptr = xval.m_ptr;
239 xval.m_ptr = nullptr;
240 return *this;
241 }
242#endif
243
244 ~CFStrongReference()
245 {
246 if (m_ptr)
247 CFRelease(m_ptr);
248 }
249
250 void reset(T newVal)
251 {
252 if (m_ptr != newVal) {
253 if (m_ptr)
254 CFRelease(m_ptr);
255 if ((m_ptr = newVal))
256 CFRetain(m_ptr);
257 }
258 }
259
260 operator T() const
261 {
262 return m_ptr;
263 }
264
265 T data() const
266 {
267 return m_ptr;
268 }
269
270 T take()
271 {
272 T p = m_ptr;
273 m_ptr = nullptr;
274 return p;
275 }
276private:
277 T m_ptr;
278};
279
280QString qt_address(NSString *address);
281
282#ifndef QT_IOS_BLUETOOTH
283
284QBluetoothAddress qt_address(const BluetoothDeviceAddress *address);
285BluetoothDeviceAddress iobluetooth_address(const QBluetoothAddress &address);
286
287ObjCStrongReference<IOBluetoothSDPUUID> iobluetooth_uuid(const QBluetoothUuid &uuid);
288QBluetoothUuid qt_uuid(IOBluetoothSDPUUID *uuid);
289QString qt_error_string(IOReturn errorCode);
290void qt_test_iobluetooth_runloop();
291
292#endif // !QT_IOS_BLUETOOTH
293
294QBluetoothUuid qt_uuid(CBUUID *uuid);
295CFStrongReference<CFUUIDRef> cf_uuid(const QBluetoothUuid &qtUuid);
296ObjCStrongReference<CBUUID> cb_uuid(const QBluetoothUuid &qtUuid);
297bool equal_uuids(const QBluetoothUuid &qtUuid, CBUUID *cbUuid);
298bool equal_uuids(CBUUID *cbUuid, const QBluetoothUuid &qtUuid);
299QByteArray qt_bytearray(NSData *data);
300QByteArray qt_bytearray(NSObject *data);
301
302ObjCStrongReference<NSData> data_from_bytearray(const QByteArray &qtData);
303ObjCStrongReference<NSMutableData> mutable_data_from_bytearray(const QByteArray &qtData);
304
305dispatch_queue_t qt_LE_queue();
306
307extern const int defaultLEScanTimeoutMS;
308extern const int maxValueLength;
309
310} // namespace OSXBluetooth
311
312// Logging category for both OS X and iOS.
313Q_DECLARE_LOGGING_CATEGORY(QT_BT_OSX)
314
315QT_END_NAMESPACE
316
317#if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(101300) && QT_MACOS_DEPLOYMENT_TARGET_BELOW(101300)
318
319 // In the macOS 10.13 SDK, the identifier property was moved from the CBPeripheral
320 // and CBCentral classes to a new CBPeer base class. Because CBPeer is only available
321 // on macOS 10.13 and above, the same is true for -[CBPeer identifier]. However,
322 // since we know that the derived classes have always had this property,
323 // we'll explicitly mark its availability here. This will not adversely affect
324 // using the identifier through the CBPeer base class, which will still require macOS 10.13.
325
326@interface CBPeripheral (UnguardedWorkaround)
327@property (readonly, nonatomic) NSUUID *identifier NS_AVAILABLE(10_7, 5_0);
328@end
329
330@interface CBCentral (UnguardedWorkaround)
331@property (readonly, nonatomic) NSUUID *identifier NS_AVAILABLE(10_7, 5_0);
332@end
333
334#endif
335
336#endif
337

Warning: That file was not part of the compilation database. It may have many parsing errors.