simplejsble 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/VERSION +1 -0
- package/android/CMakeLists.txt +62 -20
- package/cmake/epilogue.cmake +81 -0
- package/cmake/find/FindDBus1.cmake +20 -0
- package/cmake/find/Findfmt.cmake +41 -0
- package/cmake/find/Findgoogletest.cmake +18 -0
- package/cmake/parse_version.cmake +45 -0
- package/cmake/prelude.cmake +8 -0
- package/dependencies/README +4 -0
- package/dependencies/external/kvn/kvn_bytearray.h +297 -0
- package/dependencies/external/kvn/kvn_safe_callback.hpp +66 -0
- package/dependencies/external/kvn/logfwd.hpp +35 -0
- package/dependencies/internal/include/fmt/LICENSE +27 -0
- package/dependencies/internal/include/fmt/args.h +220 -0
- package/dependencies/internal/include/fmt/base.h +2962 -0
- package/dependencies/internal/include/fmt/chrono.h +2338 -0
- package/dependencies/internal/include/fmt/color.h +610 -0
- package/dependencies/internal/include/fmt/compile.h +539 -0
- package/dependencies/internal/include/fmt/core.h +5 -0
- package/dependencies/internal/include/fmt/format-inl.h +1949 -0
- package/dependencies/internal/include/fmt/format.h +4244 -0
- package/dependencies/internal/include/fmt/os.h +427 -0
- package/dependencies/internal/include/fmt/ostream.h +166 -0
- package/dependencies/internal/include/fmt/printf.h +633 -0
- package/dependencies/internal/include/fmt/ranges.h +850 -0
- package/dependencies/internal/include/fmt/std.h +726 -0
- package/dependencies/internal/include/fmt/xchar.h +373 -0
- package/dependencies/internal/include/nanopb/pb.h +948 -0
- package/dependencies/internal/include/nanopb/pb_common.h +49 -0
- package/dependencies/internal/include/nanopb/pb_decode.h +204 -0
- package/dependencies/internal/include/nanopb/pb_encode.h +195 -0
- package/dependencies/internal/include/simplejni/Common.hpp +598 -0
- package/dependencies/internal/include/simplejni/References.hpp +151 -0
- package/dependencies/internal/include/simplejni/Registry.hpp +249 -0
- package/dependencies/internal/include/simplejni/VM.hpp +93 -0
- package/dependencies/internal/src/nanopb/pb_common.c +388 -0
- package/dependencies/internal/src/nanopb/pb_decode.c +1763 -0
- package/dependencies/internal/src/nanopb/pb_encode.c +1009 -0
- package/dependencies/internal/src/simplejni/Common.cpp +4 -0
- package/package.json +5 -1
- package/simpleble/CMakeLists.txt +519 -0
- package/simpleble/include/simpleble/Adapter.h +102 -0
- package/simpleble/include/simpleble/AdapterSafe.h +58 -0
- package/simpleble/include/simpleble/Advanced.h +50 -0
- package/simpleble/include/simpleble/Characteristic.h +39 -0
- package/simpleble/include/simpleble/Config.h +64 -0
- package/simpleble/include/simpleble/Descriptor.h +30 -0
- package/simpleble/include/simpleble/Exceptions.h +72 -0
- package/simpleble/include/simpleble/Logging.h +73 -0
- package/simpleble/include/simpleble/Peripheral.h +82 -0
- package/simpleble/include/simpleble/PeripheralSafe.h +64 -0
- package/simpleble/include/simpleble/Service.h +34 -0
- package/simpleble/include/simpleble/SimpleBLE.h +8 -0
- package/simpleble/include/simpleble/Types.h +49 -0
- package/simpleble/include/simpleble/Utils.h +13 -0
- package/simpleble/include/simpleble_c/DEPRECATED +2 -0
- package/simpleble/include/simpleble_c/adapter.h +188 -0
- package/simpleble/include/simpleble_c/logging.h +37 -0
- package/simpleble/include/simpleble_c/peripheral.h +304 -0
- package/simpleble/include/simpleble_c/simpleble.h +21 -0
- package/simpleble/include/simpleble_c/types.h +73 -0
- package/simpleble/include/simpleble_c/utils.h +27 -0
- package/simpleble/src/CommonUtils.h +63 -0
- package/simpleble/src/Config.cpp +25 -0
- package/simpleble/src/Exceptions.cpp +31 -0
- package/simpleble/src/Logging.cpp +136 -0
- package/simpleble/src/LoggingInternal.h +85 -0
- package/simpleble/src/Utils.cpp +24 -0
- package/simpleble/src/backends/android/AdapterAndroid.cpp +101 -0
- package/simpleble/src/backends/android/AdapterAndroid.h +67 -0
- package/simpleble/src/backends/android/BackendAndroid.cpp +41 -0
- package/simpleble/src/backends/android/BackendAndroid.h +26 -0
- package/simpleble/src/backends/android/PeripheralAndroid.cpp +365 -0
- package/simpleble/src/backends/android/PeripheralAndroid.h +90 -0
- package/simpleble/src/backends/android/bridge/BluetoothGattCallback.cpp +432 -0
- package/simpleble/src/backends/android/bridge/BluetoothGattCallback.h +102 -0
- package/simpleble/src/backends/android/bridge/ScanCallback.cpp +142 -0
- package/simpleble/src/backends/android/bridge/ScanCallback.h +55 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.cpp +107 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.h +55 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.cpp +68 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.h +54 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.cpp +115 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.h +75 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.cpp +142 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.h +66 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.cpp +67 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.h +46 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.cpp +106 -0
- package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.h +47 -0
- package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.cpp +47 -0
- package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.h +37 -0
- package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.cpp +69 -0
- package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.h +41 -0
- package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.cpp +63 -0
- package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.h +42 -0
- package/simpleble/src/backends/android/types/android/os/ParcelUUID.cpp +32 -0
- package/simpleble/src/backends/android/types/android/os/ParcelUUID.h +30 -0
- package/simpleble/src/backends/android/types/android/util/SparseArray.cpp +54 -0
- package/simpleble/src/backends/android/types/android/util/SparseArray.h +37 -0
- package/simpleble/src/backends/android/types/java/util/Iterator.cpp +36 -0
- package/simpleble/src/backends/android/types/java/util/Iterator.h +28 -0
- package/simpleble/src/backends/android/types/java/util/List.cpp +29 -0
- package/simpleble/src/backends/android/types/java/util/List.h +27 -0
- package/simpleble/src/backends/android/types/java/util/Set.cpp +33 -0
- package/simpleble/src/backends/android/types/java/util/Set.h +28 -0
- package/simpleble/src/backends/android/types/java/util/UUID.cpp +26 -0
- package/simpleble/src/backends/android/types/java/util/UUID.h +29 -0
- package/simpleble/src/backends/common/AdapterBase.cpp +53 -0
- package/simpleble/src/backends/common/AdapterBase.h +81 -0
- package/simpleble/src/backends/common/AdapterBaseTypes.h +22 -0
- package/simpleble/src/backends/common/BackendBase.h +20 -0
- package/simpleble/src/backends/common/BackendUtils.h +33 -0
- package/simpleble/src/backends/common/CharacteristicBase.cpp +28 -0
- package/simpleble/src/backends/common/CharacteristicBase.h +38 -0
- package/simpleble/src/backends/common/DescriptorBase.cpp +7 -0
- package/simpleble/src/backends/common/DescriptorBase.h +19 -0
- package/simpleble/src/backends/common/PeripheralBase.h +82 -0
- package/simpleble/src/backends/common/ServiceBase.cpp +18 -0
- package/simpleble/src/backends/common/ServiceBase.h +28 -0
- package/simpleble/src/backends/dongl/AdapterBaseTypes.h +22 -0
- package/simpleble/src/backends/dongl/AdapterDongl.cpp +369 -0
- package/simpleble/src/backends/dongl/AdapterDongl.h +60 -0
- package/simpleble/src/backends/dongl/BackendDongl.cpp +35 -0
- package/simpleble/src/backends/dongl/PeripheralDongl.cpp +562 -0
- package/simpleble/src/backends/dongl/PeripheralDongl.h +136 -0
- package/simpleble/src/backends/dongl/protocol/basic.pb.c +33 -0
- package/simpleble/src/backends/dongl/protocol/basic.pb.h +172 -0
- package/simpleble/src/backends/dongl/protocol/d2h.pb.c +18 -0
- package/simpleble/src/backends/dongl/protocol/d2h.pb.h +118 -0
- package/simpleble/src/backends/dongl/protocol/h2d.pb.c +12 -0
- package/simpleble/src/backends/dongl/protocol/h2d.pb.h +69 -0
- package/simpleble/src/backends/dongl/protocol/simpleble.pb.c +123 -0
- package/simpleble/src/backends/dongl/protocol/simpleble.pb.h +908 -0
- package/simpleble/src/backends/dongl/protocol/softdevice.pb.c +18 -0
- package/simpleble/src/backends/dongl/protocol/softdevice.pb.h +815 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.c +339 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.h +2086 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.c +114 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.h +772 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.c +117 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.h +766 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.c +207 -0
- package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.h +1686 -0
- package/simpleble/src/backends/dongl/serial/Protocol.cpp +887 -0
- package/simpleble/src/backends/dongl/serial/Protocol.h +117 -0
- package/simpleble/src/backends/dongl/serial/ProtocolBase.cpp +95 -0
- package/simpleble/src/backends/dongl/serial/ProtocolBase.h +54 -0
- package/simpleble/src/backends/dongl/serial/Wire.cpp +133 -0
- package/simpleble/src/backends/dongl/serial/Wire.h +116 -0
- package/simpleble/src/backends/dongl/usb/UsbHelper.cpp +53 -0
- package/simpleble/src/backends/dongl/usb/UsbHelper.h +32 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperApple.cpp +266 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperApple.h +34 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperImpl.h +29 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperLinux.cpp +21 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperLinux.h +22 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperNull.cpp +21 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperNull.h +22 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperWindows.cpp +21 -0
- package/simpleble/src/backends/dongl/usb/UsbHelperWindows.h +22 -0
- package/simpleble/src/backends/linux/AdapterLinux.cpp +102 -0
- package/simpleble/src/backends/linux/AdapterLinux.h +55 -0
- package/simpleble/src/backends/linux/BackendBluez.cpp +90 -0
- package/simpleble/src/backends/linux/PeripheralLinux.cpp +394 -0
- package/simpleble/src/backends/linux/PeripheralLinux.h +90 -0
- package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.cpp +102 -0
- package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.h +55 -0
- package/simpleble/src/backends/linux_legacy/BackendBluezLegacy.cpp +87 -0
- package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.cpp +376 -0
- package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.h +90 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Adapter.h +46 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Agent.h +52 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Bluez.h +37 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrg.h +22 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrgBluez.h +26 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezRoot.h +32 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Characteristic.h +47 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Descriptor.h +33 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Device.h +64 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Exceptions.h +43 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Service.h +27 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Types.h +9 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Adapter1.h +49 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Agent1.h +100 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/AgentManager1.h +24 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Battery1.h +32 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Device1.h +62 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattCharacteristic1.h +48 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattDescriptor1.h +41 -0
- package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattService1.h +28 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Interface.h +67 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/InterfaceRegistry.h +62 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Proxy.h +117 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Connection.h +50 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Exceptions.h +56 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Holder.h +147 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Logging.h +57 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Message.h +89 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Path.h +24 -0
- package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/interfaces/ObjectManager.h +26 -0
- package/simpleble/src/backends/linux_legacy/src/Adapter.cpp +78 -0
- package/simpleble/src/backends/linux_legacy/src/Agent.cpp +72 -0
- package/simpleble/src/backends/linux_legacy/src/Bluez.cpp +45 -0
- package/simpleble/src/backends/linux_legacy/src/BluezOrg.cpp +20 -0
- package/simpleble/src/backends/linux_legacy/src/BluezOrgBluez.cpp +23 -0
- package/simpleble/src/backends/linux_legacy/src/BluezRoot.cpp +44 -0
- package/simpleble/src/backends/linux_legacy/src/Characteristic.cpp +64 -0
- package/simpleble/src/backends/linux_legacy/src/Descriptor.cpp +27 -0
- package/simpleble/src/backends/linux_legacy/src/Device.cpp +104 -0
- package/simpleble/src/backends/linux_legacy/src/Exceptions.cpp +29 -0
- package/simpleble/src/backends/linux_legacy/src/Service.cpp +33 -0
- package/simpleble/src/backends/linux_legacy/src/advanced/Interface.cpp +155 -0
- package/simpleble/src/backends/linux_legacy/src/advanced/Proxy.cpp +374 -0
- package/simpleble/src/backends/linux_legacy/src/base/Connection.cpp +219 -0
- package/simpleble/src/backends/linux_legacy/src/base/Exceptions.cpp +39 -0
- package/simpleble/src/backends/linux_legacy/src/base/Holder.cpp +739 -0
- package/simpleble/src/backends/linux_legacy/src/base/Message.cpp +622 -0
- package/simpleble/src/backends/linux_legacy/src/base/Path.cpp +129 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/Adapter1.cpp +123 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/Agent1.cpp +143 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/AgentManager1.cpp +34 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/Battery1.cpp +30 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/Device1.cpp +170 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/GattCharacteristic1.cpp +118 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/GattDescriptor1.cpp +78 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/GattService1.cpp +28 -0
- package/simpleble/src/backends/linux_legacy/src/interfaces/ObjectManager.cpp +58 -0
- package/simpleble/src/backends/macos/AdapterBaseMacOS.h +29 -0
- package/simpleble/src/backends/macos/AdapterBaseMacOS.mm +235 -0
- package/simpleble/src/backends/macos/AdapterMac.h +78 -0
- package/simpleble/src/backends/macos/AdapterMac.mm +156 -0
- package/simpleble/src/backends/macos/BackendCoreBluetooth.mm +45 -0
- package/simpleble/src/backends/macos/PeripheralBaseMacOS.h +49 -0
- package/simpleble/src/backends/macos/PeripheralBaseMacOS.mm +714 -0
- package/simpleble/src/backends/macos/PeripheralMac.h +81 -0
- package/simpleble/src/backends/macos/PeripheralMac.mm +229 -0
- package/simpleble/src/backends/macos/Utils.h +9 -0
- package/simpleble/src/backends/macos/Utils.mm +23 -0
- package/simpleble/src/backends/plain/AdapterPlain.cpp +65 -0
- package/simpleble/src/backends/plain/AdapterPlain.h +49 -0
- package/simpleble/src/backends/plain/BackendPlain.cpp +30 -0
- package/simpleble/src/backends/plain/PeripheralPlain.cpp +159 -0
- package/simpleble/src/backends/plain/PeripheralPlain.h +72 -0
- package/simpleble/src/backends/windows/AdapterWindows.cpp +338 -0
- package/simpleble/src/backends/windows/AdapterWindows.h +89 -0
- package/simpleble/src/backends/windows/BackendWinRT.cpp +67 -0
- package/simpleble/src/backends/windows/BackendWinRT.h +18 -0
- package/simpleble/src/backends/windows/MtaManager.cpp +49 -0
- package/simpleble/src/backends/windows/MtaManager.h +90 -0
- package/simpleble/src/backends/windows/PeripheralWindows.cpp +487 -0
- package/simpleble/src/backends/windows/PeripheralWindows.h +129 -0
- package/simpleble/src/backends/windows/Utils.cpp +146 -0
- package/simpleble/src/backends/windows/Utils.h +47 -0
- package/simpleble/src/builders/BuildVec.h +32 -0
- package/simpleble/src/builders/BuilderBase.h +87 -0
- package/simpleble/src/external/TaskRunner.hpp +99 -0
- package/simpleble/src/external/ThreadRunner.h +52 -0
- package/simpleble/src/external/kvn_safe_callback.hpp +66 -0
- package/simpleble/src/external/kvn_safe_map.hpp +94 -0
- package/simpleble/src/external/kvn_threadrunner.hpp +70 -0
- package/simpleble/src/external/logfwd.hpp +35 -0
- package/simpleble/src/frontends/base/Adapter.cpp +111 -0
- package/simpleble/src/frontends/base/Advanced.cpp +34 -0
- package/simpleble/src/frontends/base/Backend.cpp +95 -0
- package/simpleble/src/frontends/base/Backend.h +76 -0
- package/simpleble/src/frontends/base/Characteristic.cpp +56 -0
- package/simpleble/src/frontends/base/Descriptor.cpp +21 -0
- package/simpleble/src/frontends/base/Peripheral.cpp +113 -0
- package/simpleble/src/frontends/base/Service.cpp +26 -0
- package/simpleble/src/frontends/safe/AdapterSafe.cpp +158 -0
- package/simpleble/src/frontends/safe/PeripheralSafe.cpp +219 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <simpleble/Exceptions.h>
|
|
4
|
+
#include <simpleble/Service.h>
|
|
5
|
+
#include <simpleble/Types.h>
|
|
6
|
+
#include "PeripheralBase.h"
|
|
7
|
+
|
|
8
|
+
#include <TaskRunner.hpp>
|
|
9
|
+
#include <kvn_safe_callback.hpp>
|
|
10
|
+
|
|
11
|
+
#include <atomic>
|
|
12
|
+
#include <map>
|
|
13
|
+
#include <memory>
|
|
14
|
+
#include <mutex>
|
|
15
|
+
|
|
16
|
+
namespace SimpleBLE {
|
|
17
|
+
|
|
18
|
+
class PeripheralPlain : public PeripheralBase {
|
|
19
|
+
public:
|
|
20
|
+
PeripheralPlain();
|
|
21
|
+
virtual ~PeripheralPlain();
|
|
22
|
+
|
|
23
|
+
void* underlying() const override;
|
|
24
|
+
|
|
25
|
+
virtual std::string identifier() override;
|
|
26
|
+
virtual BluetoothAddress address() override;
|
|
27
|
+
virtual BluetoothAddressType address_type() override;
|
|
28
|
+
virtual int16_t rssi() override;
|
|
29
|
+
virtual int16_t tx_power() override;
|
|
30
|
+
virtual uint16_t mtu() override;
|
|
31
|
+
|
|
32
|
+
virtual void connect() override;
|
|
33
|
+
virtual void disconnect() override;
|
|
34
|
+
virtual bool is_connected() override;
|
|
35
|
+
virtual bool is_connectable() override;
|
|
36
|
+
virtual bool is_paired() override;
|
|
37
|
+
virtual void unpair() override;
|
|
38
|
+
|
|
39
|
+
virtual std::vector<std::shared_ptr<ServiceBase>> available_services() override;
|
|
40
|
+
virtual std::vector<std::shared_ptr<ServiceBase>> advertised_services() override;
|
|
41
|
+
|
|
42
|
+
virtual std::map<uint16_t, ByteArray> manufacturer_data() override;
|
|
43
|
+
|
|
44
|
+
// clang-format off
|
|
45
|
+
virtual ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic) override;
|
|
46
|
+
virtual void write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) override;
|
|
47
|
+
virtual void write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) override;
|
|
48
|
+
virtual void notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) override;
|
|
49
|
+
virtual void indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) override;
|
|
50
|
+
virtual void unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) override;
|
|
51
|
+
|
|
52
|
+
virtual ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor) override;
|
|
53
|
+
virtual void write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data) override;
|
|
54
|
+
// clang-format on
|
|
55
|
+
|
|
56
|
+
virtual void set_callback_on_connected(std::function<void()> on_connected) override;
|
|
57
|
+
virtual void set_callback_on_disconnected(std::function<void()> on_disconnected) override;
|
|
58
|
+
|
|
59
|
+
private:
|
|
60
|
+
std::atomic_bool connected_{false};
|
|
61
|
+
std::atomic_bool paired_{false};
|
|
62
|
+
|
|
63
|
+
kvn::safe_callback<void()> callback_on_connected_;
|
|
64
|
+
kvn::safe_callback<void()> callback_on_disconnected_;
|
|
65
|
+
|
|
66
|
+
std::mutex callback_mutex_;
|
|
67
|
+
std::map<std::pair<BluetoothUUID, BluetoothUUID>, std::function<void(ByteArray payload)>> callbacks_;
|
|
68
|
+
|
|
69
|
+
TaskRunner task_runner_;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
} // namespace SimpleBLE
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// This weird pragma is required for the compiler to properly include the necessary namespaces.
|
|
2
|
+
#pragma comment(lib, "windowsapp")
|
|
3
|
+
#include "AdapterWindows.h"
|
|
4
|
+
#include "BackendWinRT.h"
|
|
5
|
+
|
|
6
|
+
#include "BuilderBase.h"
|
|
7
|
+
#include "CommonUtils.h"
|
|
8
|
+
#include "LoggingInternal.h"
|
|
9
|
+
#include "PeripheralWindows.h"
|
|
10
|
+
#include "Utils.h"
|
|
11
|
+
#include "MtaManager.h"
|
|
12
|
+
|
|
13
|
+
#include "winrt/Windows.Devices.Bluetooth.h"
|
|
14
|
+
#include "winrt/Windows.Devices.Radios.h"
|
|
15
|
+
#include "winrt/Windows.Devices.Enumeration.h"
|
|
16
|
+
#include "winrt/Windows.Foundation.Collections.h"
|
|
17
|
+
#include "winrt/Windows.Foundation.h"
|
|
18
|
+
#include "winrt/base.h"
|
|
19
|
+
|
|
20
|
+
#include <algorithm>
|
|
21
|
+
#include <iostream>
|
|
22
|
+
#include <iterator>
|
|
23
|
+
#include <memory>
|
|
24
|
+
#include <sstream>
|
|
25
|
+
#include <string>
|
|
26
|
+
#include <utility>
|
|
27
|
+
#include <vector>
|
|
28
|
+
|
|
29
|
+
using namespace SimpleBLE;
|
|
30
|
+
using namespace SimpleBLE::WinRT;
|
|
31
|
+
using namespace std::chrono_literals;
|
|
32
|
+
|
|
33
|
+
AdapterWindows::AdapterWindows(std::string device_id)
|
|
34
|
+
: adapter_(async_get(BluetoothAdapter::FromIdAsync(winrt::to_hstring(device_id)))),
|
|
35
|
+
radio_(async_get(adapter_.GetRadioAsync())) {
|
|
36
|
+
// IMPORTANT NOTE: This function must be executed in the MTA context. In this case, this is managed by the BackendWinRT class.
|
|
37
|
+
auto device_information = async_get(
|
|
38
|
+
Devices::Enumeration::DeviceInformation::CreateFromIdAsync(winrt::to_hstring(device_id)));
|
|
39
|
+
identifier_ = winrt::to_string(device_information.Name());
|
|
40
|
+
|
|
41
|
+
// Configure the scanner object
|
|
42
|
+
scanner_ = Advertisement::BluetoothLEAdvertisementWatcher();
|
|
43
|
+
|
|
44
|
+
// Register member functions directly as callback handlers
|
|
45
|
+
radio_state_changed_token_ = radio_.StateChanged({this, &AdapterWindows::on_power_state_changed});
|
|
46
|
+
scanner_stopped_token_ = scanner_.Stopped({this, &AdapterWindows::_on_scanner_stopped});
|
|
47
|
+
scanner_received_token_ = scanner_.Received({this, &AdapterWindows::_on_scanner_received});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
AdapterWindows::~AdapterWindows() {
|
|
51
|
+
_callback_on_scan_stop.unload();
|
|
52
|
+
|
|
53
|
+
MtaManager::get().execute_sync([this]() {
|
|
54
|
+
scanner_.Stop();
|
|
55
|
+
|
|
56
|
+
if (scanner_received_token_) {
|
|
57
|
+
scanner_.Received(scanner_received_token_);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (scanner_stopped_token_) {
|
|
61
|
+
scanner_.Stopped(scanner_stopped_token_);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// FIXME: this should return wether this particular adapter is enabled, not if any adapter is enabled.
|
|
67
|
+
bool AdapterWindows::bluetooth_enabled() { return BackendWinRT::get()->bluetooth_enabled(); }
|
|
68
|
+
|
|
69
|
+
void* AdapterWindows::underlying() const { return reinterpret_cast<void*>(const_cast<BluetoothAdapter*>(&adapter_)); }
|
|
70
|
+
|
|
71
|
+
std::string AdapterWindows::identifier() { return identifier_; }
|
|
72
|
+
|
|
73
|
+
BluetoothAddress AdapterWindows::address() {
|
|
74
|
+
return MtaManager::get().execute_sync<BluetoothAddress>([this]() {
|
|
75
|
+
return _mac_address_to_str(adapter_.BluetoothAddress());
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
void AdapterWindows::power_on() {
|
|
80
|
+
MtaManager::get().execute_sync([this]() { async_get(radio_.SetStateAsync(RadioState::On)); });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
void AdapterWindows::power_off() {
|
|
84
|
+
MtaManager::get().execute_sync([this]() { async_get(radio_.SetStateAsync(RadioState::Off)); });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
bool AdapterWindows::is_powered() {
|
|
88
|
+
return MtaManager::get().execute_sync<bool>([this]() { return radio_.State() == RadioState::On; });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
void AdapterWindows::scan_start() {
|
|
92
|
+
this->seen_peripherals_.clear();
|
|
93
|
+
|
|
94
|
+
MtaManager::get().execute_sync([this]() {
|
|
95
|
+
scanner_.ScanningMode(Advertisement::BluetoothLEScanningMode::Active);
|
|
96
|
+
scan_is_active_ = true;
|
|
97
|
+
scanner_.Start();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
SAFE_CALLBACK_CALL(this->_callback_on_scan_start);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
void AdapterWindows::scan_stop() {
|
|
104
|
+
MtaManager::get().execute_sync([this]() {
|
|
105
|
+
scanner_.Stop();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
std::unique_lock<std::mutex> lock(scan_stop_mutex_);
|
|
109
|
+
if (scan_stop_cv_.wait_for(lock, 1s, [this] { return !this->scan_is_active_; })) {
|
|
110
|
+
// Scan stopped
|
|
111
|
+
} else {
|
|
112
|
+
// Scan did not stop, this can be because some other process
|
|
113
|
+
// is using the adapter.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
void AdapterWindows::scan_for(int timeout_ms) {
|
|
118
|
+
scan_start();
|
|
119
|
+
std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms));
|
|
120
|
+
scan_stop();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
bool AdapterWindows::scan_is_active() { return scan_is_active_; }
|
|
124
|
+
|
|
125
|
+
SharedPtrVector<PeripheralBase> AdapterWindows::scan_get_results() { return Util::values(seen_peripherals_); }
|
|
126
|
+
|
|
127
|
+
SharedPtrVector<PeripheralBase> AdapterWindows::get_paired_peripherals() {
|
|
128
|
+
return MtaManager::get().execute_sync<SharedPtrVector<PeripheralBase>>([this]() {
|
|
129
|
+
SharedPtrVector<PeripheralBase> peripherals;
|
|
130
|
+
winrt::hstring aqs_filter = BluetoothLEDevice::GetDeviceSelectorFromPairingState(true);
|
|
131
|
+
auto dev_info_collection = async_get(Devices::Enumeration::DeviceInformation::FindAllAsync(aqs_filter));
|
|
132
|
+
|
|
133
|
+
for (const auto& dev_info : dev_info_collection) {
|
|
134
|
+
try {
|
|
135
|
+
BluetoothLEDevice device = async_get(BluetoothLEDevice::FromIdAsync(dev_info.Id()));
|
|
136
|
+
if (device == nullptr) {
|
|
137
|
+
SIMPLEBLE_LOG_WARN(fmt::format("Could not get BluetoothLEDevice for paired device ID: {}", winrt::to_string(dev_info.Id())));
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
BluetoothAddress address = _mac_address_to_str(device.BluetoothAddress());
|
|
142
|
+
if (this->peripherals_.count(address) == 0) {
|
|
143
|
+
// If the peripheral has never been seen before, create and save a reference to it.
|
|
144
|
+
auto base_peripheral = std::make_shared<PeripheralWindows>(device);
|
|
145
|
+
this->peripherals_.insert(std::make_pair(address, base_peripheral));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
peripherals.push_back(this->peripherals_.at(address));
|
|
149
|
+
} catch (const Exception::WinRTException& e){
|
|
150
|
+
SIMPLEBLE_LOG_ERROR(fmt::format("WinRT error processing paired device {} : {}", winrt::to_string(dev_info.Id()), e.what()));
|
|
151
|
+
|
|
152
|
+
// NOTE: For debugging purposes, we'll print the error message and continue.
|
|
153
|
+
fmt::print("WinRT error processing paired device {} : {}", winrt::to_string(dev_info.Id()), e.what());
|
|
154
|
+
//throw Exception::WinRTException(e.code().value, winrt::to_string(e.message()));
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return peripherals;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
SharedPtrVector<PeripheralBase> AdapterWindows::get_connected_peripherals() {
|
|
163
|
+
return MtaManager::get().execute_sync<SharedPtrVector<PeripheralBase>>([this]() {
|
|
164
|
+
SharedPtrVector<PeripheralBase> peripherals;
|
|
165
|
+
winrt::hstring aqs_filter = BluetoothLEDevice::GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus::Connected);
|
|
166
|
+
auto dev_info_collection = async_get(Devices::Enumeration::DeviceInformation::FindAllAsync(aqs_filter));
|
|
167
|
+
|
|
168
|
+
for (const auto& dev_info : dev_info_collection) {
|
|
169
|
+
try {
|
|
170
|
+
BluetoothLEDevice device = async_get(BluetoothLEDevice::FromIdAsync(dev_info.Id()));
|
|
171
|
+
if (device == nullptr) {
|
|
172
|
+
SIMPLEBLE_LOG_WARN(fmt::format("Could not get BluetoothLEDevice for connected device ID: {}", winrt::to_string(dev_info.Id())));
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
BluetoothAddress address = _mac_address_to_str(device.BluetoothAddress());
|
|
177
|
+
if (this->peripherals_.count(address) == 0) {
|
|
178
|
+
// If the peripheral has never been seen before, create and save a reference to it.
|
|
179
|
+
auto base_peripheral = std::make_shared<PeripheralWindows>(device);
|
|
180
|
+
this->peripherals_.insert(std::make_pair(address, base_peripheral));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
peripherals.push_back(this->peripherals_.at(address));
|
|
184
|
+
} catch (const winrt::hresult_error& e) {
|
|
185
|
+
SIMPLEBLE_LOG_ERROR(fmt::format("WinRT error processing connected device {} : {}", winrt::to_string(dev_info.Id()), winrt::to_string(e.message())));
|
|
186
|
+
|
|
187
|
+
// NOTE: For debugging purposes, we'll print the error message and continue.
|
|
188
|
+
fmt::print("WinRT error processing connected device {} : {}", winrt::to_string(dev_info.Id()), winrt::to_string(e.message()));
|
|
189
|
+
// throw Exception::WinRTException(e.code().value, winrt::to_string(e.message()));
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return peripherals;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Private functions
|
|
198
|
+
|
|
199
|
+
void AdapterWindows::_scan_stopped_callback() {
|
|
200
|
+
std::lock_guard<std::mutex> lock(scan_update_mutex_);
|
|
201
|
+
scan_is_active_ = false;
|
|
202
|
+
scan_stop_cv_.notify_all();
|
|
203
|
+
|
|
204
|
+
SAFE_CALLBACK_CALL(this->_callback_on_scan_stop);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
void AdapterWindows::_scan_received_callback(advertising_data_t data) {
|
|
208
|
+
if (this->peripherals_.count(data.mac_address) == 0) {
|
|
209
|
+
// If the incoming peripheral has never been seen before, create and save a reference to it.
|
|
210
|
+
auto base_peripheral = std::make_shared<PeripheralWindows>(data);
|
|
211
|
+
this->peripherals_.insert(std::make_pair(data.mac_address, base_peripheral));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Update the received advertising data.
|
|
215
|
+
auto base_peripheral = this->peripherals_.at(data.mac_address);
|
|
216
|
+
base_peripheral->update_advertising_data(data);
|
|
217
|
+
|
|
218
|
+
// Convert the base object into an external-facing Peripheral object
|
|
219
|
+
Peripheral peripheral = Factory::build(base_peripheral);
|
|
220
|
+
|
|
221
|
+
// Check if the device has been seen before, to forward the correct call to the user.
|
|
222
|
+
if (this->seen_peripherals_.count(data.mac_address) == 0) {
|
|
223
|
+
// Store it in our table of seen peripherals
|
|
224
|
+
this->seen_peripherals_.insert(std::make_pair(data.mac_address, base_peripheral));
|
|
225
|
+
SAFE_CALLBACK_CALL(this->_callback_on_scan_found, peripheral);
|
|
226
|
+
} else {
|
|
227
|
+
SAFE_CALLBACK_CALL(this->_callback_on_scan_updated, peripheral);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
void AdapterWindows::on_power_state_changed(Radio const& sender, Foundation::IInspectable const&) {
|
|
232
|
+
auto state = sender.State();
|
|
233
|
+
if (state == RadioState::On) {
|
|
234
|
+
SAFE_CALLBACK_CALL(this->_callback_on_power_on);
|
|
235
|
+
} else if (state == RadioState::Off) {
|
|
236
|
+
SAFE_CALLBACK_CALL(this->_callback_on_power_off);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
void AdapterWindows::_on_scanner_stopped(
|
|
241
|
+
const Advertisement::BluetoothLEAdvertisementWatcher& watcher,
|
|
242
|
+
const Advertisement::BluetoothLEAdvertisementWatcherStoppedEventArgs args) {
|
|
243
|
+
// This callback is already in the MTA context as it's called by WinRT
|
|
244
|
+
SAFE_CALLBACK_CALL(this->_callback_on_scan_stop);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
void AdapterWindows::_on_scanner_received(
|
|
248
|
+
const Advertisement::BluetoothLEAdvertisementWatcher& watcher,
|
|
249
|
+
const Advertisement::BluetoothLEAdvertisementReceivedEventArgs args) {
|
|
250
|
+
// This callback is already in the MTA context as it's called by WinRT
|
|
251
|
+
std::lock_guard<std::mutex> lock(this->scan_update_mutex_);
|
|
252
|
+
if (!this->scan_is_active_) return;
|
|
253
|
+
|
|
254
|
+
advertising_data_t data;
|
|
255
|
+
data.mac_address = _mac_address_to_str(args.BluetoothAddress());
|
|
256
|
+
Bluetooth::BluetoothAddressType addr_type_enum = args.BluetoothAddressType();
|
|
257
|
+
switch (addr_type_enum) {
|
|
258
|
+
case Bluetooth::BluetoothAddressType::Public:
|
|
259
|
+
data.address_type = SimpleBLE::BluetoothAddressType::PUBLIC;
|
|
260
|
+
break;
|
|
261
|
+
|
|
262
|
+
case Bluetooth::BluetoothAddressType::Random:
|
|
263
|
+
data.address_type = SimpleBLE::BluetoothAddressType::RANDOM;
|
|
264
|
+
break;
|
|
265
|
+
|
|
266
|
+
case Bluetooth::BluetoothAddressType::Unspecified:
|
|
267
|
+
data.address_type = SimpleBLE::BluetoothAddressType::UNSPECIFIED;
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
data.identifier = winrt::to_string(args.Advertisement().LocalName());
|
|
272
|
+
data.connectable = args.IsConnectable();
|
|
273
|
+
data.rssi = args.RawSignalStrengthInDBm();
|
|
274
|
+
|
|
275
|
+
if (args.TransmitPowerLevelInDBm()) {
|
|
276
|
+
data.tx_power = args.TransmitPowerLevelInDBm().Value();
|
|
277
|
+
} else {
|
|
278
|
+
data.tx_power = INT16_MIN;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Parse manufacturer data
|
|
282
|
+
const auto manufacturer_data = args.Advertisement().ManufacturerData();
|
|
283
|
+
for (const auto& item : manufacturer_data) {
|
|
284
|
+
uint16_t company_id = item.CompanyId();
|
|
285
|
+
ByteArray manufacturer_data_buffer = ibuffer_to_bytearray(item.Data());
|
|
286
|
+
data.manufacturer_data[company_id] = manufacturer_data_buffer;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Parse service data.
|
|
290
|
+
const auto& sections = args.Advertisement().DataSections();
|
|
291
|
+
for (const auto& section : sections) {
|
|
292
|
+
ByteArray section_data_buffer = ibuffer_to_bytearray(section.Data());
|
|
293
|
+
|
|
294
|
+
std::string service_uuid;
|
|
295
|
+
ByteArray service_data;
|
|
296
|
+
|
|
297
|
+
if (section.DataType() == Advertisement::BluetoothLEAdvertisementDataTypes::ServiceData128BitUuids()) {
|
|
298
|
+
service_uuid = fmt::format(
|
|
299
|
+
"{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-"
|
|
300
|
+
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
|
301
|
+
(uint8_t)section_data_buffer[15], (uint8_t)section_data_buffer[14],
|
|
302
|
+
(uint8_t)section_data_buffer[13], (uint8_t)section_data_buffer[12],
|
|
303
|
+
(uint8_t)section_data_buffer[11], (uint8_t)section_data_buffer[10],
|
|
304
|
+
(uint8_t)section_data_buffer[9], (uint8_t)section_data_buffer[8],
|
|
305
|
+
(uint8_t)section_data_buffer[7], (uint8_t)section_data_buffer[6],
|
|
306
|
+
(uint8_t)section_data_buffer[5], (uint8_t)section_data_buffer[4],
|
|
307
|
+
(uint8_t)section_data_buffer[3], (uint8_t)section_data_buffer[2],
|
|
308
|
+
(uint8_t)section_data_buffer[1], (uint8_t)section_data_buffer[0]);
|
|
309
|
+
service_data = section_data_buffer.slice_from(16);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
else if (section.DataType() ==
|
|
313
|
+
Advertisement::BluetoothLEAdvertisementDataTypes::ServiceData32BitUuids()) {
|
|
314
|
+
service_uuid = fmt::format("{:02x}{:02x}{:02x}{:02x}-0000-1000-8000-00805f9b34fb",
|
|
315
|
+
(uint8_t)section_data_buffer[3], (uint8_t)section_data_buffer[2],
|
|
316
|
+
(uint8_t)section_data_buffer[1], (uint8_t)section_data_buffer[0]);
|
|
317
|
+
service_data = section_data_buffer.slice_from(4);
|
|
318
|
+
} else if (section.DataType() ==
|
|
319
|
+
Advertisement::BluetoothLEAdvertisementDataTypes::ServiceData16BitUuids()) {
|
|
320
|
+
service_uuid = fmt::format("0000{:02x}{:02x}-0000-1000-8000-00805f9b34fb",
|
|
321
|
+
(uint8_t)section_data_buffer[1], (uint8_t)section_data_buffer[0]);
|
|
322
|
+
service_data = section_data_buffer.slice_from(2);
|
|
323
|
+
} else {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
data.service_data.emplace(std::make_pair(service_uuid, service_data));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Parse service uuids
|
|
331
|
+
const auto service_data = args.Advertisement().ServiceUuids();
|
|
332
|
+
for (const auto& service_guid : service_data) {
|
|
333
|
+
std::string service_uuid = guid_to_uuid(service_guid);
|
|
334
|
+
data.service_data.emplace(std::make_pair(service_uuid, ByteArray()));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
this->_scan_received_callback(data);
|
|
338
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <simpleble/Exceptions.h>
|
|
4
|
+
#include <simpleble/Peripheral.h>
|
|
5
|
+
#include <simpleble/Types.h>
|
|
6
|
+
|
|
7
|
+
#include "AdapterBase.h"
|
|
8
|
+
#include "AdapterBaseTypes.h"
|
|
9
|
+
|
|
10
|
+
#include <kvn_safe_callback.hpp>
|
|
11
|
+
|
|
12
|
+
#include "winrt/Windows.Devices.Bluetooth.Advertisement.h"
|
|
13
|
+
#include "winrt/Windows.Devices.Bluetooth.h"
|
|
14
|
+
|
|
15
|
+
#include <atomic>
|
|
16
|
+
#include <condition_variable>
|
|
17
|
+
#include <functional>
|
|
18
|
+
#include <memory>
|
|
19
|
+
#include <string>
|
|
20
|
+
#include <vector>
|
|
21
|
+
|
|
22
|
+
using namespace winrt::Windows;
|
|
23
|
+
using namespace winrt::Windows::Devices;
|
|
24
|
+
using namespace winrt::Windows::Devices::Bluetooth;
|
|
25
|
+
using namespace winrt::Windows::Devices::Radios;
|
|
26
|
+
|
|
27
|
+
namespace SimpleBLE {
|
|
28
|
+
|
|
29
|
+
class PeripheralWindows;
|
|
30
|
+
class PeripheralBase;
|
|
31
|
+
|
|
32
|
+
class AdapterWindows : public AdapterBase {
|
|
33
|
+
public:
|
|
34
|
+
AdapterWindows(std::string device_id);
|
|
35
|
+
virtual ~AdapterWindows();
|
|
36
|
+
|
|
37
|
+
virtual void* underlying() const override;
|
|
38
|
+
|
|
39
|
+
virtual std::string identifier() override;
|
|
40
|
+
virtual BluetoothAddress address() override;
|
|
41
|
+
|
|
42
|
+
virtual void power_on() override;
|
|
43
|
+
virtual void power_off() override;
|
|
44
|
+
virtual bool is_powered() override;
|
|
45
|
+
|
|
46
|
+
virtual void scan_start() override;
|
|
47
|
+
virtual void scan_stop() override;
|
|
48
|
+
virtual void scan_for(int timeout_ms) override;
|
|
49
|
+
virtual bool scan_is_active() override;
|
|
50
|
+
virtual std::vector<std::shared_ptr<PeripheralBase>> scan_get_results() override;
|
|
51
|
+
|
|
52
|
+
virtual std::vector<std::shared_ptr<PeripheralBase>> get_paired_peripherals() override;
|
|
53
|
+
virtual std::vector<std::shared_ptr<PeripheralBase>> get_connected_peripherals() override;
|
|
54
|
+
|
|
55
|
+
virtual bool bluetooth_enabled() override;
|
|
56
|
+
|
|
57
|
+
private:
|
|
58
|
+
BluetoothAdapter adapter_;
|
|
59
|
+
std::string identifier_;
|
|
60
|
+
|
|
61
|
+
struct Advertisement::BluetoothLEAdvertisementWatcher scanner_;
|
|
62
|
+
winrt::event_token scanner_received_token_;
|
|
63
|
+
winrt::event_token scanner_stopped_token_;
|
|
64
|
+
|
|
65
|
+
Radio radio_;
|
|
66
|
+
winrt::event_token radio_state_changed_token_;
|
|
67
|
+
|
|
68
|
+
std::atomic_bool scan_is_active_{false};
|
|
69
|
+
std::condition_variable scan_stop_cv_;
|
|
70
|
+
std::mutex scan_stop_mutex_;
|
|
71
|
+
std::mutex scan_update_mutex_;
|
|
72
|
+
std::map<BluetoothAddress, std::shared_ptr<PeripheralWindows>> peripherals_;
|
|
73
|
+
std::map<BluetoothAddress, std::shared_ptr<PeripheralBase>> seen_peripherals_;
|
|
74
|
+
|
|
75
|
+
void on_power_state_changed(Radio const& sender, Foundation::IInspectable const&);
|
|
76
|
+
|
|
77
|
+
void _on_scanner_stopped(
|
|
78
|
+
const Advertisement::BluetoothLEAdvertisementWatcher& watcher,
|
|
79
|
+
const Advertisement::BluetoothLEAdvertisementWatcherStoppedEventArgs args);
|
|
80
|
+
|
|
81
|
+
void _on_scanner_received(
|
|
82
|
+
const Advertisement::BluetoothLEAdvertisementWatcher& watcher,
|
|
83
|
+
const Advertisement::BluetoothLEAdvertisementReceivedEventArgs args);
|
|
84
|
+
|
|
85
|
+
void _scan_stopped_callback();
|
|
86
|
+
void _scan_received_callback(advertising_data_t data);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
} // namespace SimpleBLE
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#include "BackendWinRT.h"
|
|
2
|
+
|
|
3
|
+
#include "AdapterWindows.h"
|
|
4
|
+
#include "CommonUtils.h"
|
|
5
|
+
|
|
6
|
+
#include "winrt/Windows.Devices.Bluetooth.h"
|
|
7
|
+
#include "winrt/Windows.Devices.Enumeration.h"
|
|
8
|
+
#include "winrt/Windows.Devices.Radios.h"
|
|
9
|
+
#include "winrt/Windows.Foundation.Collections.h"
|
|
10
|
+
|
|
11
|
+
#include "simpleble/Config.h"
|
|
12
|
+
#include "Utils.h"
|
|
13
|
+
#include "MtaManager.h"
|
|
14
|
+
|
|
15
|
+
namespace SimpleBLE {
|
|
16
|
+
|
|
17
|
+
using namespace SimpleBLE::WinRT;
|
|
18
|
+
|
|
19
|
+
std::shared_ptr<BackendBase> BACKEND_WINDOWS() { return BackendWinRT::get(); }
|
|
20
|
+
|
|
21
|
+
BackendWinRT::BackendWinRT(buildToken) {
|
|
22
|
+
if (Config::WinRT::experimental_reinitialize_winrt_apartment_on_main_thread) {
|
|
23
|
+
initialize_winrt();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
bool BackendWinRT::bluetooth_enabled() {
|
|
28
|
+
return MtaManager::get().execute_sync<bool>([this]() {
|
|
29
|
+
bool enabled = false;
|
|
30
|
+
auto radio_collection = async_get(Radio::GetRadiosAsync());
|
|
31
|
+
for (uint32_t i = 0; i < radio_collection.Size(); i++) {
|
|
32
|
+
auto radio = radio_collection.GetAt(i);
|
|
33
|
+
|
|
34
|
+
// Skip non-bluetooth radios
|
|
35
|
+
if (radio.Kind() != RadioKind::Bluetooth) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Assume that bluetooth is enabled if any of the radios are enabled
|
|
40
|
+
if (radio.State() == RadioState::On) {
|
|
41
|
+
enabled = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return enabled;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
SharedPtrVector<AdapterBase> BackendWinRT::get_adapters() {
|
|
51
|
+
return MtaManager::get().execute_sync<SharedPtrVector<AdapterBase>>([this]() {
|
|
52
|
+
auto device_selector = BluetoothAdapter::GetDeviceSelector();
|
|
53
|
+
auto device_information_collection = async_get(
|
|
54
|
+
Devices::Enumeration::DeviceInformation::FindAllAsync(device_selector));
|
|
55
|
+
|
|
56
|
+
SharedPtrVector<AdapterBase> adapter_list;
|
|
57
|
+
for (uint32_t i = 0; i < device_information_collection.Size(); i++) {
|
|
58
|
+
auto dev_info = device_information_collection.GetAt(i);
|
|
59
|
+
adapter_list.push_back(std::make_shared<AdapterWindows>(winrt::to_string(dev_info.Id())));
|
|
60
|
+
}
|
|
61
|
+
return adapter_list;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
std::string BackendWinRT::name() const noexcept { return "WinRT"; }
|
|
66
|
+
|
|
67
|
+
}; // namespace SimpleBLE
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "../common/BackendBase.h"
|
|
4
|
+
#include "../common/BackendUtils.h"
|
|
5
|
+
|
|
6
|
+
namespace SimpleBLE {
|
|
7
|
+
|
|
8
|
+
class BackendWinRT : public BackendSingleton<BackendWinRT> {
|
|
9
|
+
public:
|
|
10
|
+
BackendWinRT(buildToken);
|
|
11
|
+
virtual ~BackendWinRT() = default;
|
|
12
|
+
|
|
13
|
+
virtual std::vector<std::shared_ptr<AdapterBase>> get_adapters() override;
|
|
14
|
+
virtual bool bluetooth_enabled() override;
|
|
15
|
+
virtual std::string name() const noexcept override;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
} // namespace SimpleBLE
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#include "MtaManager.h"
|
|
2
|
+
|
|
3
|
+
namespace SimpleBLE {
|
|
4
|
+
namespace WinRT {
|
|
5
|
+
|
|
6
|
+
MtaManager::MtaManager() {
|
|
7
|
+
mta_thread_ = std::thread(&MtaManager::mta_thread_func, this);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
MtaManager::~MtaManager() {
|
|
11
|
+
stop();
|
|
12
|
+
if (mta_thread_.joinable()) {
|
|
13
|
+
mta_thread_.join();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
void MtaManager::submit_task(std::function<void()> task) {
|
|
18
|
+
{
|
|
19
|
+
std::lock_guard<std::mutex> lock(task_mutex_);
|
|
20
|
+
task_queue_.push(std::move(task));
|
|
21
|
+
}
|
|
22
|
+
task_cv_.notify_one();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void MtaManager::stop() {
|
|
26
|
+
{
|
|
27
|
+
std::lock_guard<std::mutex> lock(task_mutex_);
|
|
28
|
+
running_ = false;
|
|
29
|
+
}
|
|
30
|
+
task_cv_.notify_one();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void MtaManager::mta_thread_func() {
|
|
34
|
+
winrt::init_apartment(winrt::apartment_type::multi_threaded);
|
|
35
|
+
while (running_) {
|
|
36
|
+
std::function<void()> task;
|
|
37
|
+
{
|
|
38
|
+
std::unique_lock<std::mutex> lock(task_mutex_);
|
|
39
|
+
task_cv_.wait(lock, [this] { return !task_queue_.empty() || !running_; });
|
|
40
|
+
if (!running_ && task_queue_.empty()) break;
|
|
41
|
+
task = std::move(task_queue_.front());
|
|
42
|
+
task_queue_.pop();
|
|
43
|
+
}
|
|
44
|
+
if (task) task();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
} // namespace WinRT
|
|
49
|
+
} // namespace SimpleBLE
|