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

1/****************************************************************************
2**
3** Copyright (C) 2017 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#include "qbluetoothserver.h"
41#include "qbluetoothserver_p.h"
42#include "qbluetoothsocket.h"
43#include "qbluetoothsocket_winrt_p.h"
44
45#include <QtCore/QLoggingCategory>
46#include <QtCore/private/qeventdispatcher_winrt_p.h>
47#ifdef CLASSIC_APP_BUILD
48#define Q_OS_WINRT
49#endif
50#include <qfunctions_winrt.h>
51
52#include <windows.networking.h>
53#include <windows.networking.connectivity.h>
54#include <windows.networking.sockets.h>
55
56using namespace Microsoft::WRL;
57using namespace Microsoft::WRL::Wrappers;
58using namespace ABI::Windows::Devices;
59using namespace ABI::Windows::Devices::Enumeration;
60using namespace ABI::Windows::Foundation;
61using namespace ABI::Windows::Networking;
62using namespace ABI::Windows::Networking::Sockets;
63using namespace ABI::Windows::Networking::Connectivity;
64
65typedef ITypedEventHandler<StreamSocketListener *, StreamSocketListenerConnectionReceivedEventArgs *> ClientConnectedHandler;
66
67QT_BEGIN_NAMESPACE
68
69Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINRT)
70
71QHash<QBluetoothServerPrivate *, int> __fakeServerPorts;
72
73QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServiceInfo::Protocol sType)
74 : maxPendingConnections(1), serverType(sType), m_lastError(QBluetoothServer::NoError), socket(0)
75{
76#ifdef CLASSIC_APP_BUILD
77 CoInitialize(NULL);
78#endif
79 socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
80}
81
82QBluetoothServerPrivate::~QBluetoothServerPrivate()
83{
84 deactivateActiveListening();
85 __fakeServerPorts.remove(this);
86 if (socket)
87 delete socket;
88#ifdef CLASSIC_APP_BUILD
89 // If we do not reset that pointer, socketListener will go out of scope after CoUninitialize was
90 // called, which will lead to a crash.
91 socketListener = nullptr;
92 CoUninitialize();
93#endif
94}
95
96bool QBluetoothServerPrivate::isListening() const
97{
98 return __fakeServerPorts.contains(const_cast<QBluetoothServerPrivate *>(this));
99}
100
101bool QBluetoothServerPrivate::initiateActiveListening(const QString &serviceName)
102{
103 HStringReference serviceNameRef(reinterpret_cast<LPCWSTR>(serviceName.utf16()));
104
105 ComPtr<IAsyncAction> bindAction;
106 HRESULT hr = socketListener->BindServiceNameAsync(serviceNameRef.Get(), &bindAction);
107 Q_ASSERT_SUCCEEDED(hr);
108 hr = QWinRTFunctions::await(bindAction);
109 Q_ASSERT_SUCCEEDED(hr);
110 return true;
111}
112
113bool QBluetoothServerPrivate::deactivateActiveListening()
114{
115 if (!isListening())
116 return true;
117
118 HRESULT hr;
119 hr = socketListener->remove_ConnectionReceived(connectionToken);
120 Q_ASSERT_SUCCEEDED(hr);
121 return true;
122}
123
124HRESULT QBluetoothServerPrivate::handleClientConnection(IStreamSocketListener *listener,
125 IStreamSocketListenerConnectionReceivedEventArgs *args)
126{
127 Q_Q(QBluetoothServer);
128 if (!socketListener || socketListener.Get() != listener) {
129 qCDebug(QT_BT_WINRT) << "Accepting connection from wrong listener. We should not be here.";
130 Q_UNREACHABLE();
131 return S_OK;
132 }
133
134 HRESULT hr;
135 ComPtr<IStreamSocket> socket;
136 hr = args->get_Socket(&socket);
137 Q_ASSERT_SUCCEEDED(hr);
138 QMutexLocker locker(&pendingConnectionsMutex);
139 if (pendingConnections.count() < maxPendingConnections) {
140 qCDebug(QT_BT_WINRT) << "Accepting connection";
141 pendingConnections.append(socket);
142 q->newConnection();
143 } else {
144 qCDebug(QT_BT_WINRT) << "Refusing connection";
145 }
146
147 return S_OK;
148}
149
150void QBluetoothServer::close()
151{
152 Q_D(QBluetoothServer);
153
154 d->deactivateActiveListening();
155 __fakeServerPorts.remove(d);
156}
157
158bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port)
159{
160 Q_UNUSED(address);
161 Q_D(QBluetoothServer);
162 if (serverType() != QBluetoothServiceInfo::RfcommProtocol) {
163 d->m_lastError = UnsupportedProtocolError;
164 emit error(d->m_lastError);
165 return false;
166 }
167
168 if (isListening())
169 return false;
170
171 HRESULT hr;
172 hr = QEventDispatcherWinRT::runOnXamlThread([d, this] ()
173 {
174 HRESULT hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Networking_Sockets_StreamSocketListener).Get(),
175 &d->socketListener);
176 Q_ASSERT_SUCCEEDED(hr);
177 hr = d->socketListener->add_ConnectionReceived(Callback<ClientConnectedHandler>(d, &QBluetoothServerPrivate::handleClientConnection).Get(),
178 &d->connectionToken);
179 Q_ASSERT_SUCCEEDED(hr);
180 return S_OK;
181 });
182 Q_ASSERT_SUCCEEDED(hr);
183
184 //We can not register an actual Rfcomm port, because the platform does not allow it
185 //but we need a way to associate a server with a service
186 if (port == 0) { //Try to assign a non taken port id
187 for (int i = 1; ; i++){
188 if (__fakeServerPorts.key(i) == 0) {
189 port = i;
190 break;
191 }
192 }
193 }
194
195 if (__fakeServerPorts.key(port) == 0) {
196 __fakeServerPorts[d] = port;
197
198 qCDebug(QT_BT_WINRT) << "Port" << port << "registered";
199 } else {
200 qCWarning(QT_BT_WINRT) << "server with port" << port << "already registered or port invalid";
201 d->m_lastError = ServiceAlreadyRegisteredError;
202 emit error(d->m_lastError);
203 return false;
204 }
205
206 return true;
207}
208
209void QBluetoothServer::setMaxPendingConnections(int numConnections)
210{
211 Q_D(QBluetoothServer);
212 QMutexLocker locker(&d->pendingConnectionsMutex);
213 if (d->pendingConnections.count() > numConnections) {
214 qCWarning(QT_BT_WINRT) << "There are currently more than" << numConnections << "connections"
215 << "pending. Number of maximum pending connections was not changed.";
216 return;
217 }
218
219 d->maxPendingConnections = numConnections;
220}
221
222bool QBluetoothServer::hasPendingConnections() const
223{
224 Q_D(const QBluetoothServer);
225 QMutexLocker locker(&d->pendingConnectionsMutex);
226 return !d->pendingConnections.isEmpty();
227}
228
229QBluetoothSocket *QBluetoothServer::nextPendingConnection()
230{
231 Q_D(QBluetoothServer);
232 if (d->pendingConnections.count() == 0)
233 return nullptr;
234
235 ComPtr<IStreamSocket> socket = d->pendingConnections.takeFirst();
236
237 QBluetoothSocket *newSocket = new QBluetoothSocket();
238 bool success = newSocket->d_ptr->setSocketDescriptor(socket, d->serverType);
239 if (!success) {
240 delete newSocket;
241 newSocket = nullptr;
242 }
243
244 return newSocket;
245}
246
247QBluetoothAddress QBluetoothServer::serverAddress() const
248{
249 return QBluetoothAddress();
250}
251
252quint16 QBluetoothServer::serverPort() const
253{
254 //We return the fake port
255 Q_D(const QBluetoothServer);
256 return __fakeServerPorts.value((QBluetoothServerPrivate*)d, 0);
257}
258
259void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
260{
261 Q_UNUSED(security);
262}
263
264QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const
265{
266 return QBluetooth::NoSecurity;
267}
268
269QT_END_NAMESPACE
270

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