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

1/****************************************************************************
2**
3** Copyright (C) 2019 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 "qbluetoothlocaldevice.h"
41#include "qbluetoothaddress.h"
42
43#include "qbluetoothlocaldevice_p.h"
44
45#ifdef CLASSIC_APP_BUILD
46#define Q_OS_WINRT
47#endif
48#include <QtCore/qfunctions_winrt.h>
49
50#include <robuffer.h>
51#include <windows.devices.bluetooth.h>
52#include <wrl.h>
53
54using namespace ABI::Windows::Foundation;
55using namespace ABI::Windows::Devices::Bluetooth;
56using namespace ABI::Windows::Devices::Enumeration;
57using namespace Microsoft::WRL;
58using namespace Microsoft::WRL::Wrappers;
59
60QT_BEGIN_NAMESPACE
61
62template <class DeviceStatics, class OpResult, class Device, class Device2>
63ComPtr<IDeviceInformationPairing> getPairingInfo(ComPtr<DeviceStatics> deviceStatics,
64 const QBluetoothAddress &address)
65{
66 ComPtr<IAsyncOperation<OpResult *>> op;
67 if (!deviceStatics)
68 return nullptr;
69 HRESULT hr = deviceStatics->FromBluetoothAddressAsync(address.toUInt64(), &op);
70 RETURN_IF_FAILED("Could not obtain device from address", return nullptr);
71 ComPtr<Device> device;
72 hr = QWinRTFunctions::await(op, device.GetAddressOf(),
73 QWinRTFunctions::ProcessMainThreadEvents, 5000);
74 if (FAILED(hr) || !device) {
75 qErrnoWarning("Could not obtain device from address");
76 return nullptr;
77 }
78 ComPtr<Device2> device2;
79 hr = device.As(&device2);
80 RETURN_IF_FAILED("Could not cast device", return nullptr);
81 ComPtr<IDeviceInformation> deviceInfo;
82 hr = device2->get_DeviceInformation(&deviceInfo);
83 if (FAILED(hr) || !deviceInfo) {
84 qErrnoWarning("Could not obtain device information");
85 return nullptr;
86 }
87 ComPtr<IDeviceInformation2> deviceInfo2;
88 hr = deviceInfo.As(&deviceInfo2);
89 RETURN_IF_FAILED("Could not cast device information", return nullptr);
90 ComPtr<IDeviceInformationPairing> pairingInfo;
91 hr = deviceInfo2->get_Pairing(&pairingInfo);
92 RETURN_IF_FAILED("Could not obtain pairing information", return nullptr);
93 return pairingInfo;
94}
95
96QBluetoothLocalDevice::QBluetoothLocalDevice(QObject *parent) :
97 QObject(parent),
98 d_ptr(new QBluetoothLocalDevicePrivate(this, QBluetoothAddress()))
99{
100 registerQBluetoothLocalDeviceMetaType();
101}
102
103QBluetoothLocalDevice::QBluetoothLocalDevice(const QBluetoothAddress &address, QObject *parent) :
104 QObject(parent),
105 d_ptr(new QBluetoothLocalDevicePrivate(this, address))
106{
107 registerQBluetoothLocalDeviceMetaType();
108}
109
110QBluetoothLocalDevicePrivate::QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, QBluetoothAddress)
111 : q_ptr(q)
112{
113 GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_BluetoothLEDevice).Get(), &mLEStatics);
114 GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_BluetoothDevice).Get(), &mStatics);
115}
116
117bool QBluetoothLocalDevicePrivate::isValid() const
118{
119 return (mStatics != nullptr && mLEStatics != nullptr);
120}
121
122void QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, Pairing pairing)
123{
124 Q_UNUSED(address);
125 Q_UNUSED(pairing);
126 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
127 Q_ARG(QBluetoothLocalDevice::Error,
128 QBluetoothLocalDevice::PairingError));
129}
130
131QBluetoothLocalDevice::Pairing QBluetoothLocalDevice::pairingStatus(
132 const QBluetoothAddress &address) const
133{
134 if (!isValid() || address.isNull())
135 return QBluetoothLocalDevice::Unpaired;
136
137 ComPtr<IDeviceInformationPairing> pairingInfo = getPairingInfo<IBluetoothLEDeviceStatics,
138 BluetoothLEDevice, IBluetoothLEDevice, IBluetoothLEDevice2>(d_ptr->mLEStatics, address);
139 if (!pairingInfo)
140 pairingInfo = getPairingInfo<IBluetoothDeviceStatics, BluetoothDevice,
141 IBluetoothDevice, IBluetoothDevice2>(d_ptr->mStatics, address);
142 if (!pairingInfo)
143 return QBluetoothLocalDevice::Unpaired;
144 boolean isPaired;
145 HRESULT hr = pairingInfo->get_IsPaired(&isPaired);
146 RETURN_IF_FAILED("Could not obtain device pairing", return QBluetoothLocalDevice::Unpaired);
147 if (!isPaired)
148 return QBluetoothLocalDevice::Unpaired;
149
150 ComPtr<IDeviceInformationPairing2> pairingInfo2;
151 hr = pairingInfo.As(&pairingInfo2);
152 RETURN_IF_FAILED("Could not cast pairing info", return QBluetoothLocalDevice::Paired);
153 DevicePairingProtectionLevel protection = DevicePairingProtectionLevel_None;
154 hr = pairingInfo2->get_ProtectionLevel(&protection);
155 RETURN_IF_FAILED("Could not obtain pairing protection level", return QBluetoothLocalDevice::Paired);
156 if (protection == DevicePairingProtectionLevel_Encryption
157 || protection == DevicePairingProtectionLevel_EncryptionAndAuthentication)
158 return QBluetoothLocalDevice::AuthorizedPaired;
159 return QBluetoothLocalDevice::Paired;
160}
161
162void QBluetoothLocalDevice::pairingConfirmation(bool confirmation)
163{
164 Q_UNUSED(confirmation);
165}
166
167void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode mode)
168{
169 Q_UNUSED(mode);
170}
171
172QBluetoothLocalDevice::HostMode QBluetoothLocalDevice::hostMode() const
173{
174 return HostConnectable;
175}
176
177QList<QBluetoothAddress> QBluetoothLocalDevice::connectedDevices() const
178{
179 return QList<QBluetoothAddress>();
180}
181
182void QBluetoothLocalDevice::powerOn()
183{
184}
185
186QString QBluetoothLocalDevice::name() const
187{
188 return QString();
189}
190
191QBluetoothAddress QBluetoothLocalDevice::address() const
192{
193 return QBluetoothAddress();
194}
195
196QList<QBluetoothHostInfo> QBluetoothLocalDevice::allDevices()
197{
198 QList<QBluetoothHostInfo> localDevices;
199 return localDevices;
200}
201
202QT_END_NAMESPACE
203

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