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