Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbluetoothutils_winrt_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBLUETOOTHUTILS_WINRT_P_H
5#define QBLUETOOTHUTILS_WINRT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18// Workaround for Windows SDK bug.
19// See https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/issues/47
20 #include <winrt/base.h>
21#include <QtCore/private/qfactorycacheregistration_p.h>
22namespace winrt::impl
23{
24 template <typename Async>
25 auto wait_for(Async const& async, Windows::Foundation::TimeSpan const& timeout);
26}
27
28#include <QtCore/QtGlobal>
29#include <QtCore/private/qglobal_p.h>
30
31#include <wrl/client.h>
32
33#include <functional>
34#include <type_traits>
35
36// Macros for guarding C++/WinRT projection calls. Each accessor on a winrt
37// projection runs check_hresult() on the underlying ABI HRESULT and throws
38// winrt::hresult_error on failure. After the Bluetooth stack tears down state
39// (suspend/resume, radio toggle, service restart), those calls routinely fail
40// and the exception propagates out of completion lambdas, crashing the process.
41//
42// SAFE(expr): returns expr's value on success, a logged-and-defaulted value on
43// a caught hresult_error. Default-constructible types (primitives, enums,
44// winrt::hstring, generic interfaces like IAsyncOperation<T>) take T{};
45// runtimeclass projections that only declare an explicit Class(nullptr_t)
46// constructor fall back to T(nullptr).
47// HR(stmt): runs stmt and returns the resulting winrt::hresult (S_OK on
48// success, e.code() on failure).
49// TRY(stmt): runs stmt and returns true on success, false on failure.
50// LOG_HRESULT(hr): warning-log helper used by the wrappers.
51//
52// Callsites must have the QT_BT_WINDOWS logging category in scope.
53
54#define LOG_HRESULT(hr) qCWarning(QT_BT_WINDOWS) << "HRESULT:" << quint32(hr)
55
56#define HR(x)
57 std::invoke([&]() {
58 try {
59 x;
60 } catch (winrt::hresult_error const &e) {
61 LOG_HRESULT(e.code()) << "/*" << #x << "*/";
62 return e.code();
63 }
64 return winrt::hresult{ S_OK };
65 })
66
67#define TRY(x)
68 std::invoke([&]() {
69 try {
70 x;
71 } catch (winrt::hresult_error const &e) {
72 LOG_HRESULT(e.code()) << "/*" << #x << "*/";
73 return false;
74 }
75 return true;
76 })
77
78#define SAFE(x)
79 std::invoke([&]() -> decltype(x) {
80 try { return (x); }
81 catch (winrt::hresult_error const &e) {
82 LOG_HRESULT(e.code()) << "/*" << #x << "*/";
83 if constexpr (std::is_default_constructible_v<decltype(x)>)
84 return decltype(x){};
85 else
86 return decltype(x)(nullptr);
87 }
88 })
89
90namespace ABI {
91 namespace Windows {
92 namespace Storage {
93 namespace Streams {
94 struct IBuffer;
95 }
96 }
97 }
98}
99
100QT_BEGIN_NAMESPACE
101
102using NativeBuffer = ABI::Windows::Storage::Streams::IBuffer;
103QByteArray byteArrayFromBuffer(const Microsoft::WRL::ComPtr<NativeBuffer> &buffer,
104 bool isWCharString = false);
105
106// The calls to Co(Un)init must be balanced
107void mainThreadCoInit(void* caller);
108void mainThreadCoUninit(void* caller);
109
110QT_END_NAMESPACE
111
112#endif // QBLUETOOTHSOCKET_WINRT_P_H
void deviceAdded(const QBluetoothAddress &address)
void deviceRemoved(const QBluetoothAddress &address)
void updateMode(winrt::hstring adapterId, QBluetoothLocalDevice::HostMode mode)
void modeChanged(winrt::hstring id, QBluetoothLocalDevice::HostMode mode)
QList< QBluetoothAddress > connectedDevices()
void adapterRemoved(winrt::hstring id)
void removeClient(winrt::hstring adapterId)
QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *=nullptr, QBluetoothAddress=QBluetoothAddress())
void init(AdapterManager *manager, AddedSlot onAdded, RemovedSlot onRemoved)
WatcherWrapper(winrt::hstring selector)
auto wait_for(Async const &async, Windows::Foundation::TimeSpan const &timeout)
Windows::Foundation::AsyncStatus AsyncStatus
static DeviceInformationCollection getAvailableAdapters()
DeviceInformationPairing pairingInfoFromAddress(const QBluetoothAddress &address)
static const int kMaximumAttempts
static BluetoothInfo getBluetoothInfo(winrt::hstring id)
QByteArray byteArrayFromBuffer(const Microsoft::WRL::ComPtr< NativeBuffer > &buffer, bool isWCharString=false)
#define LOG_HRESULT(hr)
void mainThreadCoInit(void *caller)
void mainThreadCoUninit(void *caller)
~PairingWorker()=default
PairingWorker(QBluetoothLocalDevice *device)
void pairAsync(const QBluetoothAddress &addr, QBluetoothLocalDevice::Pairing pairing)