simplejsble 0.0.4 → 0.0.6
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/cmake/simpleble.pc.in +11 -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,55 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <simpleble/Exceptions.h>
|
|
4
|
+
#include <simpleble/Types.h>
|
|
5
|
+
|
|
6
|
+
#include "../common/AdapterBase.h"
|
|
7
|
+
|
|
8
|
+
#include <kvn_safe_callback.hpp>
|
|
9
|
+
|
|
10
|
+
#include <simplebluezlegacy/Adapter.h>
|
|
11
|
+
|
|
12
|
+
#include <atomic>
|
|
13
|
+
#include <functional>
|
|
14
|
+
#include <memory>
|
|
15
|
+
#include <string>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
namespace SimpleBLE {
|
|
19
|
+
|
|
20
|
+
class PeripheralLinuxLegacy;
|
|
21
|
+
|
|
22
|
+
class AdapterLinuxLegacy : public SimpleBLE::AdapterBase {
|
|
23
|
+
public:
|
|
24
|
+
AdapterLinuxLegacy(std::shared_ptr<SimpleBluezLegacy::Adapter> adapter);
|
|
25
|
+
virtual ~AdapterLinuxLegacy();
|
|
26
|
+
|
|
27
|
+
void* underlying() const override;
|
|
28
|
+
|
|
29
|
+
virtual std::string identifier() override;
|
|
30
|
+
virtual BluetoothAddress address() override;
|
|
31
|
+
|
|
32
|
+
virtual void power_on() override;
|
|
33
|
+
virtual void power_off() override;
|
|
34
|
+
virtual bool is_powered() override;
|
|
35
|
+
|
|
36
|
+
virtual void scan_start() override;
|
|
37
|
+
virtual void scan_stop() override;
|
|
38
|
+
virtual void scan_for(int timeout_ms) override;
|
|
39
|
+
virtual bool scan_is_active() override;
|
|
40
|
+
virtual std::vector<std::shared_ptr<PeripheralBase>> scan_get_results() override;
|
|
41
|
+
|
|
42
|
+
virtual std::vector<std::shared_ptr<PeripheralBase>> get_paired_peripherals() override;
|
|
43
|
+
|
|
44
|
+
virtual bool bluetooth_enabled() override;
|
|
45
|
+
|
|
46
|
+
private:
|
|
47
|
+
std::shared_ptr<SimpleBluezLegacy::Adapter> adapter_;
|
|
48
|
+
|
|
49
|
+
std::atomic_bool is_scanning_;
|
|
50
|
+
|
|
51
|
+
std::map<BluetoothAddress, std::shared_ptr<PeripheralLinuxLegacy>> peripherals_;
|
|
52
|
+
std::map<BluetoothAddress, std::shared_ptr<PeripheralLinuxLegacy>> seen_peripherals_;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
} // namespace SimpleBLE
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#include "AdapterLinuxLegacy.h"
|
|
2
|
+
#include "BackendBase.h"
|
|
3
|
+
#include "BackendUtils.h"
|
|
4
|
+
#include "CommonUtils.h"
|
|
5
|
+
|
|
6
|
+
#include <simplebluezlegacy/Bluez.h>
|
|
7
|
+
|
|
8
|
+
#include <atomic>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <mutex>
|
|
11
|
+
#include <thread>
|
|
12
|
+
|
|
13
|
+
namespace SimpleBLE {
|
|
14
|
+
|
|
15
|
+
class BackendBluezLegacy : public BackendSingleton<BackendBluezLegacy> {
|
|
16
|
+
public:
|
|
17
|
+
BackendBluezLegacy(buildToken);
|
|
18
|
+
virtual ~BackendBluezLegacy();
|
|
19
|
+
|
|
20
|
+
SimpleBluezLegacy::Bluez bluez;
|
|
21
|
+
|
|
22
|
+
virtual SharedPtrVector<AdapterBase> get_adapters() override;
|
|
23
|
+
virtual bool bluetooth_enabled() override;
|
|
24
|
+
std::string name() const noexcept override;
|
|
25
|
+
|
|
26
|
+
private:
|
|
27
|
+
std::thread* async_thread;
|
|
28
|
+
std::atomic_bool async_thread_active;
|
|
29
|
+
void async_thread_function();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
std::shared_ptr<BackendBase> BACKEND_LINUX_LEGACY() { return BackendBluezLegacy::get(); }
|
|
33
|
+
|
|
34
|
+
BackendBluezLegacy::BackendBluezLegacy(buildToken) {
|
|
35
|
+
static std::mutex get_mutex; // Static mutex to ensure thread safety when accessing the logger
|
|
36
|
+
std::scoped_lock lock(get_mutex); // Unlock the mutex on function return
|
|
37
|
+
|
|
38
|
+
bluez.init();
|
|
39
|
+
async_thread_active = true;
|
|
40
|
+
async_thread = new std::thread(&BackendBluezLegacy::async_thread_function, this);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
BackendBluezLegacy::~BackendBluezLegacy() {
|
|
44
|
+
async_thread_active = false;
|
|
45
|
+
while (!async_thread->joinable()) {
|
|
46
|
+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
47
|
+
}
|
|
48
|
+
async_thread->join();
|
|
49
|
+
delete async_thread;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
SharedPtrVector<AdapterBase> BackendBluezLegacy::get_adapters() {
|
|
53
|
+
SharedPtrVector<AdapterBase> adapter_list;
|
|
54
|
+
|
|
55
|
+
auto internal_adapters = bluez.get_adapters();
|
|
56
|
+
for (auto& adapter : internal_adapters) {
|
|
57
|
+
adapter_list.push_back(std::make_shared<AdapterLinuxLegacy>(adapter));
|
|
58
|
+
}
|
|
59
|
+
return adapter_list;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
bool BackendBluezLegacy::bluetooth_enabled() {
|
|
63
|
+
bool enabled = false;
|
|
64
|
+
|
|
65
|
+
auto internal_adapters = bluez.get_adapters();
|
|
66
|
+
for (auto& adapter : internal_adapters) {
|
|
67
|
+
if (adapter->powered()) {
|
|
68
|
+
enabled = true;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return enabled;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
std::string BackendBluezLegacy::name() const noexcept { return "SimpleBluez Legacy"; }
|
|
77
|
+
|
|
78
|
+
void BackendBluezLegacy::async_thread_function() {
|
|
79
|
+
SAFE_RUN({ bluez.register_agent(); });
|
|
80
|
+
|
|
81
|
+
while (async_thread_active) {
|
|
82
|
+
SAFE_RUN({ bluez.run_async(); });
|
|
83
|
+
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
} // namespace SimpleBLE
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
#include "PeripheralLinuxLegacy.h"
|
|
2
|
+
|
|
3
|
+
#include "BuildVec.h"
|
|
4
|
+
#include "BuilderBase.h"
|
|
5
|
+
#include "CharacteristicBase.h"
|
|
6
|
+
#include "DescriptorBase.h"
|
|
7
|
+
#include "ServiceBase.h"
|
|
8
|
+
|
|
9
|
+
#include <simpleble/Config.h>
|
|
10
|
+
#include <simpleble/Characteristic.h>
|
|
11
|
+
#include <simpleble/Descriptor.h>
|
|
12
|
+
#include <simpleble/Exceptions.h>
|
|
13
|
+
#include <simpleble/Service.h>
|
|
14
|
+
#include <simplebluezlegacy/Exceptions.h>
|
|
15
|
+
#include <algorithm>
|
|
16
|
+
#include <thread>
|
|
17
|
+
#include "CommonUtils.h"
|
|
18
|
+
#include "LoggingInternal.h"
|
|
19
|
+
|
|
20
|
+
const SimpleBLE::BluetoothUUID BATTERY_SERVICE_UUID = "0000180f-0000-1000-8000-00805f9b34fb";
|
|
21
|
+
const SimpleBLE::BluetoothUUID BATTERY_CHARACTERISTIC_UUID = "00002a19-0000-1000-8000-00805f9b34fb";
|
|
22
|
+
|
|
23
|
+
using namespace SimpleBLE;
|
|
24
|
+
using namespace std::chrono_literals;
|
|
25
|
+
|
|
26
|
+
PeripheralLinuxLegacy::PeripheralLinuxLegacy(std::shared_ptr<SimpleBluezLegacy::Device> device,
|
|
27
|
+
std::shared_ptr<SimpleBluezLegacy::Adapter> adapter)
|
|
28
|
+
: device_(std::move(device)), adapter_(std::move(adapter)) {}
|
|
29
|
+
|
|
30
|
+
PeripheralLinuxLegacy::~PeripheralLinuxLegacy() {
|
|
31
|
+
// Clear the callbacks to prevent any further events from being sent to the user.
|
|
32
|
+
this->callback_on_connected_.unload();
|
|
33
|
+
this->callback_on_disconnected_.unload();
|
|
34
|
+
|
|
35
|
+
device_->clear_on_disconnected();
|
|
36
|
+
device_->clear_on_services_resolved();
|
|
37
|
+
_cleanup_characteristics();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
void* PeripheralLinuxLegacy::underlying() const { return device_.get(); }
|
|
41
|
+
|
|
42
|
+
std::string PeripheralLinuxLegacy::identifier() { return device_->name(); }
|
|
43
|
+
|
|
44
|
+
BluetoothAddress PeripheralLinuxLegacy::address() { return device_->address(); }
|
|
45
|
+
|
|
46
|
+
BluetoothAddressType PeripheralLinuxLegacy::address_type() {
|
|
47
|
+
std::string address_type = device_->address_type();
|
|
48
|
+
|
|
49
|
+
if (address_type == "public") {
|
|
50
|
+
return BluetoothAddressType::PUBLIC;
|
|
51
|
+
} else if (address_type == "public") {
|
|
52
|
+
return BluetoothAddressType::RANDOM;
|
|
53
|
+
} else {
|
|
54
|
+
return BluetoothAddressType::UNSPECIFIED;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
int16_t PeripheralLinuxLegacy::rssi() { return device_->rssi(); }
|
|
59
|
+
|
|
60
|
+
int16_t PeripheralLinuxLegacy::tx_power() { return device_->tx_power(); }
|
|
61
|
+
|
|
62
|
+
uint16_t PeripheralLinuxLegacy::mtu() {
|
|
63
|
+
if (!is_connected()) return 0;
|
|
64
|
+
|
|
65
|
+
for (auto bluez_service : device_->services()) {
|
|
66
|
+
for (auto bluez_characteristic : bluez_service->characteristics()) {
|
|
67
|
+
// The value provided by Bluez includes an extra 3 bytes from the GATT header
|
|
68
|
+
// which needs to be removed.
|
|
69
|
+
return bluez_characteristic->mtu() - 3;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
void PeripheralLinuxLegacy::connect() {
|
|
76
|
+
device_->clear_on_disconnected();
|
|
77
|
+
device_->set_on_services_resolved([this]() { this->connection_cv_.notify_all(); });
|
|
78
|
+
|
|
79
|
+
// Attempt to connect to the device.
|
|
80
|
+
for (size_t i = 0; i < 5; i++) {
|
|
81
|
+
if (_attempt_connect()) {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Set the on_disconnected callback once the connection attempts are finished, thus
|
|
87
|
+
// preventing disconnection events that should not be seen by the user.
|
|
88
|
+
device_->set_on_disconnected([this]() {
|
|
89
|
+
this->_cleanup_characteristics();
|
|
90
|
+
this->disconnection_cv_.notify_all();
|
|
91
|
+
|
|
92
|
+
SAFE_CALLBACK_CALL(this->callback_on_disconnected_);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (!is_connected()) {
|
|
96
|
+
throw Exception::OperationFailed();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
SAFE_CALLBACK_CALL(this->callback_on_connected_);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void PeripheralLinuxLegacy::disconnect() {
|
|
103
|
+
// Attempt to connect to the device.
|
|
104
|
+
for (size_t i = 0; i < 5; i++) {
|
|
105
|
+
if (_attempt_disconnect()) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (is_connected()) {
|
|
111
|
+
throw Exception::OperationFailed();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
bool PeripheralLinuxLegacy::is_connected() {
|
|
116
|
+
// NOTE: For Bluez, a device being connected means that it's both
|
|
117
|
+
// connected and services have been resolved.
|
|
118
|
+
return device_->connected() && device_->services_resolved();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
bool PeripheralLinuxLegacy::is_connectable() { return device_->name() != ""; }
|
|
122
|
+
|
|
123
|
+
bool PeripheralLinuxLegacy::is_paired() { return device_->paired(); }
|
|
124
|
+
|
|
125
|
+
void PeripheralLinuxLegacy::unpair() {
|
|
126
|
+
if (device_->paired()) {
|
|
127
|
+
adapter_->device_remove(device_->path());
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
SharedPtrVector<ServiceBase> PeripheralLinuxLegacy::available_services() {
|
|
132
|
+
bool is_battery_service_available = false;
|
|
133
|
+
|
|
134
|
+
SharedPtrVector<ServiceBase> service_list;
|
|
135
|
+
for (auto bluez_service : device_->services()) {
|
|
136
|
+
// Check if the service is the battery service.
|
|
137
|
+
if (bluez_service->uuid() == BATTERY_SERVICE_UUID) {
|
|
138
|
+
is_battery_service_available = true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Build the list of characteristics for the service.
|
|
142
|
+
SharedPtrVector<CharacteristicBase> characteristic_list;
|
|
143
|
+
for (auto bluez_characteristic : bluez_service->characteristics()) {
|
|
144
|
+
// Build the list of descriptors for the characteristic.
|
|
145
|
+
SharedPtrVector<DescriptorBase> descriptor_list;
|
|
146
|
+
for (auto bluez_descriptor : bluez_characteristic->descriptors()) {
|
|
147
|
+
descriptor_list.push_back(std::make_shared<DescriptorBase>(bluez_descriptor->uuid()));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
std::vector<std::string> flags = bluez_characteristic->flags();
|
|
151
|
+
|
|
152
|
+
bool can_read = std::find(flags.begin(), flags.end(), "read") != flags.end();
|
|
153
|
+
bool can_write_request = std::find(flags.begin(), flags.end(), "write") != flags.end();
|
|
154
|
+
bool can_write_command = std::find(flags.begin(), flags.end(), "write-without-response") != flags.end();
|
|
155
|
+
bool can_notify = std::find(flags.begin(), flags.end(), "notify") != flags.end();
|
|
156
|
+
bool can_indicate = std::find(flags.begin(), flags.end(), "indicate") != flags.end();
|
|
157
|
+
|
|
158
|
+
characteristic_list.push_back(
|
|
159
|
+
std::make_shared<CharacteristicBase>(bluez_characteristic->uuid(), descriptor_list, can_read,
|
|
160
|
+
can_write_request, can_write_command, can_notify, can_indicate));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
service_list.push_back(std::make_shared<ServiceBase>(bluez_service->uuid(), characteristic_list));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// If the battery service is not available, and the device has the appropriate interface, add it.
|
|
167
|
+
if (!is_battery_service_available && device_->has_battery_interface()) {
|
|
168
|
+
// Emulate the battery service through the Battery1 interface.
|
|
169
|
+
SharedPtrVector<DescriptorBase> descriptor_list;
|
|
170
|
+
SharedPtrVector<CharacteristicBase> characteristic_list = {std::make_shared<CharacteristicBase>(
|
|
171
|
+
BATTERY_CHARACTERISTIC_UUID, descriptor_list, true, false, false, true, false)};
|
|
172
|
+
service_list.push_back(std::make_shared<ServiceBase>(BATTERY_SERVICE_UUID, characteristic_list));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return service_list;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
SharedPtrVector<ServiceBase> PeripheralLinuxLegacy::advertised_services() {
|
|
179
|
+
SharedPtrVector<ServiceBase> service_list;
|
|
180
|
+
for (auto& service_uuid : device_->uuids()) {
|
|
181
|
+
service_list.push_back(std::make_shared<ServiceBase>(service_uuid));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return service_list;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
std::map<uint16_t, ByteArray> PeripheralLinuxLegacy::manufacturer_data() { return device_->manufacturer_data(); }
|
|
188
|
+
|
|
189
|
+
ByteArray PeripheralLinuxLegacy::read(BluetoothUUID const& service, BluetoothUUID const& characteristic) {
|
|
190
|
+
// Check if the user is attempting to read the battery service/characteristic and if so,
|
|
191
|
+
// emulate the battery service through the Battery1 interface if it's not available.
|
|
192
|
+
if (service == BATTERY_SERVICE_UUID && characteristic == BATTERY_CHARACTERISTIC_UUID &&
|
|
193
|
+
device_->has_battery_interface()) {
|
|
194
|
+
// If this point is reached, the battery service needs to be emulated.
|
|
195
|
+
uint8_t battery_percentage = device_->battery_percentage();
|
|
196
|
+
return ByteArray(reinterpret_cast<char*>(&battery_percentage), 1);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Otherwise, attempt to read the characteristic using default mechanisms
|
|
200
|
+
return _get_characteristic(service, characteristic)->read();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
void PeripheralLinuxLegacy::write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic,
|
|
204
|
+
ByteArray const& data) {
|
|
205
|
+
// TODO: Check if the characteristic is writable.
|
|
206
|
+
// TODO: SimpleBluez::Characteristic::write_request() should also take ByteArray by const reference (but that's
|
|
207
|
+
// another library)
|
|
208
|
+
_get_characteristic(service, characteristic)->write_request(data);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
void PeripheralLinuxLegacy::write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic,
|
|
212
|
+
ByteArray const& data) {
|
|
213
|
+
// TODO: Check if the characteristic is writable.
|
|
214
|
+
// TODO: SimpleBluez::Characteristic::write_command() should also take ByteArray by const reference (but that's
|
|
215
|
+
// another library)
|
|
216
|
+
_get_characteristic(service, characteristic)->write_command(data);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
void PeripheralLinuxLegacy::notify(BluetoothUUID const& service, BluetoothUUID const& characteristic,
|
|
220
|
+
std::function<void(ByteArray payload)> callback) {
|
|
221
|
+
// Check if the user is attempting to notify the battery service/characteristic and if so,
|
|
222
|
+
// emulate the battery service through the Battery1 interface if it's not available.
|
|
223
|
+
if (service == BATTERY_SERVICE_UUID && characteristic == BATTERY_CHARACTERISTIC_UUID &&
|
|
224
|
+
device_->has_battery_interface()) {
|
|
225
|
+
// If this point is reached, the battery service needs to be emulated.
|
|
226
|
+
device_->set_on_battery_percentage_changed(
|
|
227
|
+
[callback](uint8_t new_value) { callback(ByteArray(reinterpret_cast<char*>(&new_value), 1)); });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Otherwise, attempt to read the characteristic using default mechanisms
|
|
232
|
+
// TODO: What to do if the characteristic is already being notified?
|
|
233
|
+
// TODO: Check if the property can be notified.
|
|
234
|
+
auto characteristic_object = _get_characteristic(service, characteristic);
|
|
235
|
+
characteristic_object->set_on_value_changed([callback](SimpleBluezLegacy::ByteArray new_value) { callback(new_value); });
|
|
236
|
+
characteristic_object->start_notify();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
void PeripheralLinuxLegacy::indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic,
|
|
240
|
+
std::function<void(ByteArray payload)> callback) {
|
|
241
|
+
notify(service, characteristic, callback);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
void PeripheralLinuxLegacy::unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) {
|
|
245
|
+
// Check if the user is attempting to read the battery service/characteristic and if so,
|
|
246
|
+
// emulate the battery service through the Battery1 interface if it's not available.
|
|
247
|
+
if (service == BATTERY_SERVICE_UUID && characteristic == BATTERY_CHARACTERISTIC_UUID &&
|
|
248
|
+
device_->has_battery_interface()) {
|
|
249
|
+
// If this point is reached, the battery service needs to be emulated.
|
|
250
|
+
device_->clear_on_battery_percentage_changed();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// TODO: What to do if the characteristic is not being notified?
|
|
255
|
+
auto characteristic_object = _get_characteristic(service, characteristic);
|
|
256
|
+
characteristic_object->stop_notify();
|
|
257
|
+
|
|
258
|
+
// Wait for the characteristic to stop notifying.
|
|
259
|
+
// TODO: Upgrade SimpleDBus to provide a way to wait for this signal.
|
|
260
|
+
auto timeout = std::chrono::steady_clock::now() + 5s;
|
|
261
|
+
while (characteristic_object->notifying() && std::chrono::steady_clock::now() < timeout) {
|
|
262
|
+
std::this_thread::sleep_for(50ms);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
ByteArray PeripheralLinuxLegacy::read(BluetoothUUID const& service, BluetoothUUID const& characteristic,
|
|
267
|
+
BluetoothUUID const& descriptor) {
|
|
268
|
+
return _get_descriptor(service, characteristic, descriptor)->read();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
void PeripheralLinuxLegacy::write(BluetoothUUID const& service, BluetoothUUID const& characteristic,
|
|
272
|
+
BluetoothUUID const& descriptor, ByteArray const& data) {
|
|
273
|
+
_get_descriptor(service, characteristic, descriptor)->write(data);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
void PeripheralLinuxLegacy::set_callback_on_connected(std::function<void()> on_connected) {
|
|
277
|
+
if (on_connected) {
|
|
278
|
+
callback_on_connected_.load(std::move(on_connected));
|
|
279
|
+
} else {
|
|
280
|
+
callback_on_connected_.unload();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
void PeripheralLinuxLegacy::set_callback_on_disconnected(std::function<void()> on_disconnected) {
|
|
285
|
+
if (on_disconnected) {
|
|
286
|
+
callback_on_disconnected_.load(std::move(on_disconnected));
|
|
287
|
+
} else {
|
|
288
|
+
callback_on_disconnected_.unload();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Private methods
|
|
293
|
+
|
|
294
|
+
void PeripheralLinuxLegacy::_cleanup_characteristics() noexcept {
|
|
295
|
+
// As this method can be called in multiple stages of a disconnection or object
|
|
296
|
+
// destruction, the entire execution of this method is wrapped in a try-catch
|
|
297
|
+
// block to prevent any exceptions from being thrown, as these will most certainly
|
|
298
|
+
// crash the user application.
|
|
299
|
+
try {
|
|
300
|
+
// Clear all callbacks first to ensure that a failure during `stop_notify`
|
|
301
|
+
// does not leave any dangling callbacks.
|
|
302
|
+
if (device_->has_battery_interface()) {
|
|
303
|
+
device_->clear_on_battery_percentage_changed();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
for (auto bluez_service : device_->services()) {
|
|
307
|
+
for (auto bluez_characteristic : bluez_service->characteristics()) {
|
|
308
|
+
bluez_characteristic->clear_on_value_changed();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Stop notifying all characteristics.
|
|
313
|
+
for (auto bluez_service : device_->services()) {
|
|
314
|
+
for (auto bluez_characteristic : bluez_service->characteristics()) {
|
|
315
|
+
try {
|
|
316
|
+
if (bluez_characteristic->notifying()) {
|
|
317
|
+
bluez_characteristic->stop_notify();
|
|
318
|
+
}
|
|
319
|
+
} catch (std::exception const& e) {
|
|
320
|
+
SIMPLEBLE_LOG_WARN(fmt::format("Exception during characteristic cleanup: {}", e.what()));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
} catch (std::exception const& e) {
|
|
325
|
+
SIMPLEBLE_LOG_WARN(fmt::format("Exception during characteristic cleanup: {}", e.what()));
|
|
326
|
+
} catch (...) {
|
|
327
|
+
// It's possible during the cleanup process that the Bluez device has already
|
|
328
|
+
// been removed, which could cause calls to cleanup methods to throw.
|
|
329
|
+
SIMPLEBLE_LOG_WARN("Unknown exception during characteristic cleanup");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
bool PeripheralLinuxLegacy::_attempt_connect() {
|
|
334
|
+
try {
|
|
335
|
+
device_->connect();
|
|
336
|
+
} catch (SimpleDBusLegacy::Exception::SendFailed const& e) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Wait for the connection to be confirmed.
|
|
341
|
+
// The condition variable will return false if the connection was not established.
|
|
342
|
+
std::unique_lock<std::mutex> lock(connection_mutex_);
|
|
343
|
+
return connection_cv_.wait_for(lock, Config::SimpleBluez::connection_timeout, [this]() { return is_connected(); });
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
bool PeripheralLinuxLegacy::_attempt_disconnect() {
|
|
347
|
+
_cleanup_characteristics();
|
|
348
|
+
|
|
349
|
+
device_->disconnect();
|
|
350
|
+
|
|
351
|
+
// Wait for the disconnection to be confirmed.
|
|
352
|
+
// The condition variable will return false if the connection is still active.
|
|
353
|
+
std::unique_lock<std::mutex> lock(disconnection_mutex_);
|
|
354
|
+
return disconnection_cv_.wait_for(lock, Config::SimpleBluez::disconnection_timeout, [this]() { return !is_connected(); });
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
std::shared_ptr<SimpleBluezLegacy::Characteristic> PeripheralLinuxLegacy::_get_characteristic(
|
|
358
|
+
BluetoothUUID const& service_uuid, BluetoothUUID const& characteristic_uuid) {
|
|
359
|
+
try {
|
|
360
|
+
return device_->get_characteristic(service_uuid, characteristic_uuid);
|
|
361
|
+
} catch (SimpleBluezLegacy::Exception::ServiceNotFoundException& e) {
|
|
362
|
+
throw Exception::ServiceNotFound(service_uuid);
|
|
363
|
+
} catch (SimpleBluezLegacy::Exception::CharacteristicNotFoundException& e) {
|
|
364
|
+
throw Exception::CharacteristicNotFound(characteristic_uuid);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
std::shared_ptr<SimpleBluezLegacy::Descriptor> PeripheralLinuxLegacy::_get_descriptor(BluetoothUUID const& service_uuid,
|
|
369
|
+
BluetoothUUID const& characteristic_uuid,
|
|
370
|
+
BluetoothUUID const& descriptor_uuid) {
|
|
371
|
+
try {
|
|
372
|
+
return device_->get_characteristic(service_uuid, characteristic_uuid)->get_descriptor(descriptor_uuid);
|
|
373
|
+
} catch (SimpleBluezLegacy::Exception::DescriptorNotFoundException& e) {
|
|
374
|
+
throw Exception::DescriptorNotFound(descriptor_uuid);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <simpleble/Exceptions.h>
|
|
4
|
+
#include <simpleble/Service.h>
|
|
5
|
+
#include <simpleble/Types.h>
|
|
6
|
+
|
|
7
|
+
#include <simplebluezlegacy/Adapter.h>
|
|
8
|
+
#include <simplebluezlegacy/Characteristic.h>
|
|
9
|
+
#include <simplebluezlegacy/Device.h>
|
|
10
|
+
|
|
11
|
+
#include "../common/PeripheralBase.h"
|
|
12
|
+
|
|
13
|
+
#include <kvn_safe_callback.hpp>
|
|
14
|
+
|
|
15
|
+
#include <atomic>
|
|
16
|
+
#include <condition_variable>
|
|
17
|
+
#include <memory>
|
|
18
|
+
|
|
19
|
+
namespace SimpleBLE {
|
|
20
|
+
|
|
21
|
+
class PeripheralLinuxLegacy : public SimpleBLE::PeripheralBase {
|
|
22
|
+
public:
|
|
23
|
+
PeripheralLinuxLegacy(std::shared_ptr<SimpleBluezLegacy::Device> device, std::shared_ptr<SimpleBluezLegacy::Adapter> adapter);
|
|
24
|
+
virtual ~PeripheralLinuxLegacy();
|
|
25
|
+
|
|
26
|
+
virtual void* underlying() const override;
|
|
27
|
+
|
|
28
|
+
virtual std::string identifier() override;
|
|
29
|
+
virtual BluetoothAddress address() override;
|
|
30
|
+
virtual BluetoothAddressType address_type() override;
|
|
31
|
+
virtual int16_t rssi() override;
|
|
32
|
+
|
|
33
|
+
virtual int16_t tx_power() override;
|
|
34
|
+
virtual uint16_t mtu() override;
|
|
35
|
+
|
|
36
|
+
virtual void connect() override;
|
|
37
|
+
virtual void disconnect() override;
|
|
38
|
+
virtual bool is_connected() override;
|
|
39
|
+
virtual bool is_connectable() override;
|
|
40
|
+
virtual bool is_paired() override;
|
|
41
|
+
virtual void unpair() override;
|
|
42
|
+
|
|
43
|
+
virtual std::vector<std::shared_ptr<ServiceBase>> available_services() override;
|
|
44
|
+
virtual std::vector<std::shared_ptr<ServiceBase>> advertised_services() override;
|
|
45
|
+
|
|
46
|
+
virtual std::map<uint16_t, ByteArray> manufacturer_data() override;
|
|
47
|
+
|
|
48
|
+
// clang-format off
|
|
49
|
+
virtual ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic) override;
|
|
50
|
+
virtual void write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) override;
|
|
51
|
+
virtual void write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) override;
|
|
52
|
+
virtual void notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) override;
|
|
53
|
+
virtual void indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) override;
|
|
54
|
+
virtual void unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) override;
|
|
55
|
+
|
|
56
|
+
virtual ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor) override;
|
|
57
|
+
virtual void write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data) override;
|
|
58
|
+
// clang-format on
|
|
59
|
+
|
|
60
|
+
virtual void set_callback_on_connected(std::function<void()> on_connected) override;
|
|
61
|
+
virtual void set_callback_on_disconnected(std::function<void()> on_disconnected) override;
|
|
62
|
+
|
|
63
|
+
private:
|
|
64
|
+
std::atomic_bool battery_emulation_required_{false};
|
|
65
|
+
|
|
66
|
+
std::shared_ptr<SimpleBluezLegacy::Adapter> adapter_;
|
|
67
|
+
std::shared_ptr<SimpleBluezLegacy::Device> device_;
|
|
68
|
+
|
|
69
|
+
std::condition_variable connection_cv_;
|
|
70
|
+
std::mutex connection_mutex_;
|
|
71
|
+
|
|
72
|
+
std::condition_variable disconnection_cv_;
|
|
73
|
+
std::mutex disconnection_mutex_;
|
|
74
|
+
|
|
75
|
+
kvn::safe_callback<void()> callback_on_connected_;
|
|
76
|
+
kvn::safe_callback<void()> callback_on_disconnected_;
|
|
77
|
+
|
|
78
|
+
bool _attempt_connect();
|
|
79
|
+
bool _attempt_disconnect();
|
|
80
|
+
void _cleanup_characteristics() noexcept;
|
|
81
|
+
|
|
82
|
+
std::shared_ptr<SimpleBluezLegacy::Characteristic> _get_characteristic(BluetoothUUID const& service_uuid,
|
|
83
|
+
BluetoothUUID const& characteristic_uuid);
|
|
84
|
+
|
|
85
|
+
std::shared_ptr<SimpleBluezLegacy::Descriptor> _get_descriptor(BluetoothUUID const& service_uuid,
|
|
86
|
+
BluetoothUUID const& characteristic_uuid,
|
|
87
|
+
BluetoothUUID const& descriptor_uuid);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
} // namespace SimpleBLE
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <simpledbuslegacy/advanced/Proxy.h>
|
|
4
|
+
|
|
5
|
+
#include <simplebluezlegacy/Device.h>
|
|
6
|
+
#include <simplebluezlegacy/interfaces/Adapter1.h>
|
|
7
|
+
|
|
8
|
+
#include <kvn/kvn_safe_callback.hpp>
|
|
9
|
+
|
|
10
|
+
#include <functional>
|
|
11
|
+
|
|
12
|
+
namespace SimpleBluezLegacy {
|
|
13
|
+
|
|
14
|
+
class Adapter : public SimpleDBusLegacy::Proxy {
|
|
15
|
+
public:
|
|
16
|
+
typedef Adapter1::DiscoveryFilter DiscoveryFilter;
|
|
17
|
+
|
|
18
|
+
Adapter(std::shared_ptr<SimpleDBusLegacy::Connection> conn, const std::string& bus_name, const std::string& path);
|
|
19
|
+
virtual ~Adapter();
|
|
20
|
+
|
|
21
|
+
std::string identifier() const;
|
|
22
|
+
std::string address();
|
|
23
|
+
bool discovering();
|
|
24
|
+
bool powered();
|
|
25
|
+
|
|
26
|
+
void discovery_filter(const DiscoveryFilter& filter);
|
|
27
|
+
void discovery_start();
|
|
28
|
+
void discovery_stop();
|
|
29
|
+
|
|
30
|
+
std::shared_ptr<Device> device_get(const std::string& path);
|
|
31
|
+
void device_remove(const std::string& path);
|
|
32
|
+
void device_remove(const std::shared_ptr<Device>& device);
|
|
33
|
+
std::vector<std::shared_ptr<Device>> device_paired_get();
|
|
34
|
+
|
|
35
|
+
void set_on_device_updated(std::function<void(std::shared_ptr<Device> device)> callback);
|
|
36
|
+
void clear_on_device_updated();
|
|
37
|
+
|
|
38
|
+
private:
|
|
39
|
+
std::shared_ptr<SimpleDBusLegacy::Proxy> path_create(const std::string& path) override;
|
|
40
|
+
|
|
41
|
+
std::shared_ptr<Adapter1> adapter1();
|
|
42
|
+
|
|
43
|
+
kvn::safe_callback<void(std::shared_ptr<Device> device)> _on_device_updated;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
} // namespace SimpleBluezLegacy
|