summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/darwin/btutility_p.h
blob: 87f4a719e39d23d4f9608f25186cf68c26559260 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef BTUTILITY_P_H
#define BTUTILITY_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "btraii_p.h"

#include <QtCore/qloggingcategory.h>
#include <QtCore/qglobal.h>

#include <QtCore/private/qcore_mac_p.h>

#include <Foundation/Foundation.h>

#include <CoreBluetooth/CoreBluetooth.h>

#ifdef Q_OS_MACOS
#include <IOBluetooth/IOBluetooth.h>
#endif // Q_OS_MACOS

QT_BEGIN_NAMESPACE

class QLowEnergyCharacteristicData;
class QBluetoothAddress;
class QBluetoothUuid;
class QString;

namespace DarwinBluetooth {

template<class T>
class ObjCScopedPointer
{
public:
    ObjCScopedPointer() = default;
    ObjCScopedPointer(T *ptr, RetainPolicy policy)
        : m_ptr(ptr, policy)
    {
    }
    void swap(ObjCScopedPointer &other)
    {
        m_ptr.swap(other.m_ptr);
    }
    void reset()
    {
        m_ptr.reset();
    }
    void reset(T *ptr, RetainPolicy policy)
    {
        m_ptr.reset(ptr, policy);
    }
    operator T*() const
    {
        return m_ptr.getAs<T>();
    }
    T *get() const
    {
        // operator T * above does not work when accessing
        // properties using '.' syntax.
        return m_ptr.getAs<T>();
    }
private:
    // Copy and move disabled by m_ptr:
    ScopedPointer m_ptr;
};

#define QT_BT_MAC_AUTORELEASEPOOL const QMacAutoReleasePool pool;

template<class T>
class ObjCStrongReference final : public StrongReference {
public:
    using StrongReference::StrongReference;

    operator T *() const
    {
        return this->getAs<T>();
    }

    T *data() const
    {
        return this->getAs<T>();
    }
};

QString qt_address(NSString *address);

#ifndef QT_IOS_BLUETOOTH

QBluetoothAddress qt_address(const BluetoothDeviceAddress *address);
BluetoothDeviceAddress iobluetooth_address(const QBluetoothAddress &address);

ObjCStrongReference<IOBluetoothSDPUUID> iobluetooth_uuid(const QBluetoothUuid &uuid);
QBluetoothUuid qt_uuid(IOBluetoothSDPUUID *uuid);
QString qt_error_string(IOReturn errorCode);
void qt_test_iobluetooth_runloop();

#endif // !QT_IOS_BLUETOOTH

QBluetoothUuid qt_uuid(CBUUID *uuid);
ObjCStrongReference<CBUUID> cb_uuid(const QBluetoothUuid &qtUuid);
bool equal_uuids(const QBluetoothUuid &qtUuid, CBUUID *cbUuid);
bool equal_uuids(CBUUID *cbUuid, const QBluetoothUuid &qtUuid);
QByteArray qt_bytearray(NSData *data);
QByteArray qt_bytearray(NSObject *data);

ObjCStrongReference<NSData> data_from_bytearray(const QByteArray &qtData);
ObjCStrongReference<NSMutableData> mutable_data_from_bytearray(const QByteArray &qtData);

dispatch_queue_t qt_LE_queue();

extern const int defaultLEScanTimeoutMS;
extern const int maxValueLength;
extern const int defaultMtu;

} // namespace DarwinBluetooth

Q_DECLARE_LOGGING_CATEGORY(QT_BT_DARWIN)

QT_END_NAMESPACE

#endif // BTUTILITY_P_H