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 QBLUETOOTHSERVER_P_H
41#define QBLUETOOTHSERVER_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 <QtGlobal>
55#include <QList>
56#include <QtBluetooth/QBluetoothSocket>
57#include "qbluetoothserver.h"
58#include "qbluetooth.h"
59
60#if QT_CONFIG(bluez)
61QT_FORWARD_DECLARE_CLASS(QSocketNotifier)
62#endif
63
64#ifdef QT_ANDROID_BLUETOOTH
65#include <QtAndroidExtras/QAndroidJniEnvironment>
66#include <QtAndroidExtras/QAndroidJniObject>
67#include <QtBluetooth/QBluetoothUuid>
68
69class ServerAcceptanceThread;
70#endif
71
72#ifdef QT_WINRT_BLUETOOTH
73#include <QtCore/QMutex>
74
75#include <wrl.h>
76// No forward declares because QBluetoothServerPrivate::listener does not work with them
77#include <windows.networking.sockets.h>
78#endif
79
80#ifdef QT_OSX_BLUETOOTH
81
82#include "osx/btdelegates_p.h"
83#include "osx/btraii_p.h"
84
85#include <QtCore/qvector.h>
86
87#endif // QT_OSX_BLUETOOTH
88
89QT_BEGIN_NAMESPACE
90
91class QBluetoothAddress;
92class QBluetoothSocket;
93
94class QBluetoothServer;
95
96class QBluetoothServerPrivate
97#ifdef QT_OSX_BLUETOOTH
98 : public DarwinBluetooth::SocketListener
99#endif
100{
101 Q_DECLARE_PUBLIC(QBluetoothServer)
102
103public:
104 QBluetoothServerPrivate(QBluetoothServiceInfo::Protocol serverType);
105 ~QBluetoothServerPrivate();
106
107#if QT_CONFIG(bluez)
108 void _q_newConnection();
109 void setSocketSecurityLevel(QBluetooth::SecurityFlags requestedSecLevel, int *errnoCode);
110 QBluetooth::SecurityFlags socketSecurityLevel() const;
111 static QBluetoothSocket *createSocketForServer(
112 QBluetoothServiceInfo::Protocol socketType = QBluetoothServiceInfo::RfcommProtocol);
113#endif
114
115public:
116 QBluetoothSocket *socket;
117
118 int maxPendingConnections;
119 QBluetooth::SecurityFlags securityFlags;
120 QBluetoothServiceInfo::Protocol serverType;
121
122protected:
123 QBluetoothServer *q_ptr;
124
125private:
126 QBluetoothServer::Error m_lastError;
127#if QT_CONFIG(bluez)
128 QSocketNotifier *socketNotifier = nullptr;
129#elif defined(QT_ANDROID_BLUETOOTH)
130 ServerAcceptanceThread *thread;
131 QString m_serviceName;
132 QBluetoothUuid m_uuid;
133public:
134 bool isListening() const;
135 bool initiateActiveListening(const QBluetoothUuid& uuid, const QString &serviceName);
136 bool deactivateActiveListening();
137#elif defined(QT_WINRT_BLUETOOTH)
138 EventRegistrationToken connectionToken {-1};
139
140 mutable QMutex pendingConnectionsMutex;
141 QVector<Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocket>> pendingConnections;
142
143 Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocketListener> socketListener;
144 HRESULT handleClientConnection(ABI::Windows::Networking::Sockets::IStreamSocketListener *listener,
145 ABI::Windows::Networking::Sockets::IStreamSocketListenerConnectionReceivedEventArgs *args);
146
147public:
148 bool isListening() const;
149 Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocketListener> listener() { return socketListener; }
150 bool initiateActiveListening(const QString &serviceName);
151 bool deactivateActiveListening();
152#endif
153
154#ifdef QT_OSX_BLUETOOTH
155
156public:
157
158 friend class QBluetoothServer;
159 friend class QBluetoothServiceInfoPrivate;
160
161private:
162 bool startListener(quint16 realPort);
163 void stopListener();
164 bool isListening() const;
165
166 // SocketListener (delegate):
167 void openNotifyRFCOMM(void *channel) override;
168 void openNotifyL2CAP(void *channel) override;
169
170 // Either a "temporary" channelID/PSM assigned by QBluetoothServer::listen,
171 // or a real channelID/PSM returned by IOBluetooth after we've registered
172 // a service.
173 quint16 port;
174
175 DarwinBluetooth::StrongReference listener;
176
177 // These static functions below
178 // deal with differences between bluetooth sockets
179 // (bluez and QtBluetooth's API) and IOBluetooth, where it's not possible
180 // to have a real PSM/channelID _before_ a service is registered,
181 // the solution - "fake" ports.
182 // These functions require external locking - using channelMapMutex.
183 static QMutex &channelMapMutex();
184
185 static bool channelIsBusy(quint16 channelID);
186 static quint16 findFreeChannel();
187
188 static bool psmIsBusy(quint16 psm);
189 static quint16 findFreePSM();
190
191 static void registerServer(QBluetoothServerPrivate *server, quint16 port);
192 static QBluetoothServerPrivate *registeredServer(quint16 port, QBluetoothServiceInfo::Protocol protocol);
193 static void unregisterServer(QBluetoothServerPrivate *server);
194
195 using PendingConnection = DarwinBluetooth::StrongReference;
196 QVector<PendingConnection> pendingConnections;
197
198#endif // QT_OSX_BLUETOOTH
199};
200
201QT_END_NAMESPACE
202
203#endif
204