diff options
author | Oliver Wolff <oliver.wolff@qt.io> | 2019-04-03 13:34:20 +0200 |
---|---|---|
committer | Oliver Wolff <oliver.wolff@qt.io> | 2019-04-15 12:24:15 +0000 |
commit | 25d556c2097445d16cc05b3961f86f1b6ae60ed1 (patch) | |
tree | 05e3141d8cff0bf688e22b8b797eb6fedc14ef8c | |
parent | 1bf25e130d4d6ae859c4f2aabe2a752532aee5e5 (diff) |
winrt: Move byteArrayFromBuffer into qbluetoothutils_winrt
That code has been (unneededly) duplicated several times. We should have
that helper function just once.
Change-Id: I28fc9c5f7f7218b7870dc30bec228c9af8c6b090
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp | 15 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp | 18 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothutils_winrt.cpp | 25 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothutils_winrt_p.h | 16 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_winrt.cpp | 19 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_winrt_new.cpp | 32 |
6 files changed, 43 insertions, 82 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp index e0209693..177b8082 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp @@ -87,21 +87,6 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINRT) continue; \ } -static QByteArray byteArrayFromBuffer(const ComPtr<IBuffer> &buffer) -{ - ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteAccess; - HRESULT hr = buffer.As(&byteAccess); - Q_ASSERT_SUCCEEDED(hr); - char *data; - hr = byteAccess->Buffer(reinterpret_cast<byte **>(&data)); - Q_ASSERT_SUCCEEDED(hr); - UINT32 size; - hr = buffer->get_Length(&size); - Q_ASSERT_SUCCEEDED(hr); - return QByteArray(data, int(size)); -} - - static ManufacturerData extractManufacturerData(ComPtr<IBluetoothLEAdvertisement> ad) { ManufacturerData ret; diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp index c6b00346..9f82a202 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp @@ -81,24 +81,6 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINRT) #define TYPE_STRING 37 #define TYPE_SEQUENCE 53 -static QByteArray byteArrayFromBuffer(const ComPtr<IBuffer> &buffer, bool isWCharString = false) -{ - ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteAccess; - HRESULT hr = buffer.As(&byteAccess); - Q_ASSERT_SUCCEEDED(hr); - char *data; - hr = byteAccess->Buffer(reinterpret_cast<byte **>(&data)); - Q_ASSERT_SUCCEEDED(hr); - UINT32 size; - hr = buffer->get_Length(&size); - Q_ASSERT_SUCCEEDED(hr); - if (isWCharString) { - QString valueString = QString::fromUtf16(reinterpret_cast<ushort *>(data)).left(size / 2); - return valueString.toUtf8(); - } - return QByteArray(data, size); -} - class QWinRTBluetoothServiceDiscoveryWorker : public QObject { Q_OBJECT diff --git a/src/bluetooth/qbluetoothutils_winrt.cpp b/src/bluetooth/qbluetoothutils_winrt.cpp index 1d44221b..de4355c6 100644 --- a/src/bluetooth/qbluetoothutils_winrt.cpp +++ b/src/bluetooth/qbluetoothutils_winrt.cpp @@ -42,12 +42,15 @@ #include <QtCore/qfunctions_winrt.h> +#include <robuffer.h> #include <wrl.h> #include <windows.foundation.metadata.h> +#include <windows.storage.streams.h> using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; using namespace ABI::Windows::Foundation::Metadata; +using namespace ABI::Windows::Storage::Streams; QT_BEGIN_NAMESPACE @@ -76,4 +79,26 @@ bool supportsNewLEApi() return apiPresent; } +QByteArray byteArrayFromBuffer(const ComPtr<NativeBuffer> &buffer, bool isWCharString) +{ + if (!buffer) { + qErrnoWarning("nullptr passed to byteArrayFromBuffer"); + return QByteArray(); + } + ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteAccess; + HRESULT hr = buffer.As(&byteAccess); + RETURN_IF_FAILED("Could not cast buffer", return QByteArray()) + char *data; + hr = byteAccess->Buffer(reinterpret_cast<byte **>(&data)); + RETURN_IF_FAILED("Could not obtain buffer data", return QByteArray()) + UINT32 size; + hr = buffer->get_Length(&size); + RETURN_IF_FAILED("Could not obtain buffer size", return QByteArray()) + if (isWCharString) { + QString valueString = QString::fromUtf16(reinterpret_cast<ushort *>(data)).left(size / 2); + return valueString.toUtf8(); + } + return QByteArray(data, qint32(size)); +} + QT_END_NAMESPACE diff --git a/src/bluetooth/qbluetoothutils_winrt_p.h b/src/bluetooth/qbluetoothutils_winrt_p.h index c272bae1..93950fc9 100644 --- a/src/bluetooth/qbluetoothutils_winrt_p.h +++ b/src/bluetooth/qbluetoothutils_winrt_p.h @@ -53,10 +53,26 @@ #include <QtCore/QtGlobal> +#include <wrl/client.h> + +namespace ABI { + namespace Windows { + namespace Storage { + namespace Streams { + struct IBuffer; + } + } + } +} + QT_BEGIN_NAMESPACE bool supportsNewLEApi(); +using NativeBuffer = ABI::Windows::Storage::Streams::IBuffer; +QByteArray byteArrayFromBuffer(const Microsoft::WRL::ComPtr<NativeBuffer> &buffer, + bool isWCharString = false); + QT_END_NAMESPACE #endif // QBLUETOOTHSOCKET_WINRT_P_H diff --git a/src/bluetooth/qlowenergycontroller_winrt.cpp b/src/bluetooth/qlowenergycontroller_winrt.cpp index 989c5443..a9eb523b 100644 --- a/src/bluetooth/qlowenergycontroller_winrt.cpp +++ b/src/bluetooth/qlowenergycontroller_winrt.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "qlowenergycontroller_winrt_p.h" +#include "qbluetoothutils_winrt_p.h" #include <QtBluetooth/QLowEnergyCharacteristicData> #include <QtBluetooth/QLowEnergyDescriptorData> @@ -104,24 +105,6 @@ static QVector<QBluetoothUuid> getIncludedServiceIds(const ComPtr<IGattDeviceSer return result; } -static QByteArray byteArrayFromBuffer(const ComPtr<IBuffer> &buffer, bool isWCharString = false) -{ - ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteAccess; - HRESULT hr = buffer.As(&byteAccess); - Q_ASSERT_SUCCEEDED(hr); - char *data; - hr = byteAccess->Buffer(reinterpret_cast<byte **>(&data)); - Q_ASSERT_SUCCEEDED(hr); - UINT32 size; - hr = buffer->get_Length(&size); - Q_ASSERT_SUCCEEDED(hr); - if (isWCharString) { - QString valueString = QString::fromUtf16(reinterpret_cast<ushort *>(data)).left(size / 2); - return valueString.toUtf8(); - } - return QByteArray(data, size); -} - static QByteArray byteArrayFromGattResult(const ComPtr<IGattReadResult> &gattResult, bool isWCharString = false) { ComPtr<ABI::Windows::Storage::Streams::IBuffer> buffer; diff --git a/src/bluetooth/qlowenergycontroller_winrt_new.cpp b/src/bluetooth/qlowenergycontroller_winrt_new.cpp index 5501c4f3..45a80252 100644 --- a/src/bluetooth/qlowenergycontroller_winrt_new.cpp +++ b/src/bluetooth/qlowenergycontroller_winrt_new.cpp @@ -39,6 +39,7 @@ #include "qlowenergycontroller_winrt_new_p.h" #include "qlowenergycontroller_winrt_p.h" +#include "qbluetoothutils_winrt_p.h" #include <QtBluetooth/QLowEnergyCharacteristicData> #include <QtBluetooth/QLowEnergyDescriptorData> @@ -119,37 +120,6 @@ QLowEnergyControllerPrivate *createWinRTLowEnergyController() return new QLowEnergyControllerPrivateWinRT(); } -static QByteArray byteArrayFromBuffer(const ComPtr<IBuffer> &buffer, bool isWCharString = false) -{ - if (!buffer) { - qCWarning(QT_BT_WINRT) << "nullptr passed to byteArrayFromBuffer"; - return QByteArray(); - } - ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteAccess; - HRESULT hr = buffer.As(&byteAccess); - if (FAILED(hr)) { - qCWarning(QT_BT_WINRT) << "Could not cast Buffer to ByteAccess"; - return QByteArray(); - } - char *data; - hr = byteAccess->Buffer(reinterpret_cast<byte **>(&data)); - if (FAILED(hr)) { - qCWarning(QT_BT_WINRT) << "Could not obtain buffer data"; - return QByteArray(); - } - UINT32 size; - hr = buffer->get_Length(&size); - if (FAILED(hr)) { - qCWarning(QT_BT_WINRT) << "Could not obtain buffer length"; - return QByteArray(); - } - if (isWCharString) { - QString valueString = QString::fromUtf16(reinterpret_cast<ushort *>(data)).left(size / 2); - return valueString.toUtf8(); - } - return QByteArray(data, int(size)); -} - static QByteArray byteArrayFromGattResult(const ComPtr<IGattReadResult> &gattResult, bool isWCharString = false) { |