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
package/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.10.4
|
package/android/CMakeLists.txt
CHANGED
|
@@ -1,35 +1,77 @@
|
|
|
1
|
-
project(NitroSimplejsble)
|
|
2
1
|
cmake_minimum_required(VERSION 3.9.0)
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
# Determine if we're in the SimpleBLE monorepo or npm package
|
|
4
|
+
# In monorepo: packages/simplejsble/android → ../../../../ has cmake/
|
|
5
|
+
# In npm: node_modules/simplejsble/android → ../ has cmake/ (bundled)
|
|
6
|
+
if(EXISTS "${CMAKE_SOURCE_DIR}/../cmake/prelude.cmake")
|
|
7
|
+
# npm package - bundled cmake
|
|
8
|
+
set(PROJECT_ROOT_DIR ${CMAKE_SOURCE_DIR}/..)
|
|
9
|
+
elseif(EXISTS "${CMAKE_SOURCE_DIR}/../../../../cmake/prelude.cmake")
|
|
10
|
+
# Monorepo
|
|
11
|
+
set(PROJECT_ROOT_DIR ${CMAKE_SOURCE_DIR}/../../../..)
|
|
12
|
+
else()
|
|
13
|
+
message(FATAL_ERROR "Cannot find SimpleBLE cmake files. Make sure you're using a complete package.")
|
|
14
|
+
endif()
|
|
15
|
+
|
|
16
|
+
# Read version from VERSION file
|
|
17
|
+
file(READ "${PROJECT_ROOT_DIR}/VERSION" VERSION_FILE_CONTENTS)
|
|
18
|
+
string(STRIP "${VERSION_FILE_CONTENTS}" SIMPLEBLE_VERSION_BASE)
|
|
19
|
+
|
|
20
|
+
# Include SimpleBLE prelude
|
|
21
|
+
include(${PROJECT_ROOT_DIR}/cmake/prelude.cmake)
|
|
22
|
+
|
|
23
|
+
project(NitroSimplejsble VERSION ${SIMPLEBLE_VERSION_BASE} LANGUAGES CXX)
|
|
24
|
+
|
|
25
|
+
# Include SimpleBLE epilogue
|
|
26
|
+
include(${PROJECT_ROOT_DIR}/cmake/epilogue.cmake)
|
|
27
|
+
|
|
28
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
29
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
30
|
+
|
|
31
|
+
# Build SimpleBLE as static library
|
|
32
|
+
set(BUILD_SHARED_LIBS OFF)
|
|
33
|
+
add_subdirectory(${PROJECT_ROOT_DIR}/simpleble ${CMAKE_BINARY_DIR}/simpleble)
|
|
34
|
+
set(BUILD_SHARED_LIBS ON)
|
|
35
|
+
|
|
36
|
+
# Remove nativehelper from SimpleBLE (not available in React Native)
|
|
37
|
+
get_target_property(SIMPLEBLE_LINK_LIBS simpleble INTERFACE_LINK_LIBRARIES)
|
|
38
|
+
if(SIMPLEBLE_LINK_LIBS)
|
|
39
|
+
list(REMOVE_ITEM SIMPLEBLE_LINK_LIBS nativehelper)
|
|
40
|
+
set_target_properties(simpleble PROPERTIES INTERFACE_LINK_LIBRARIES "${SIMPLEBLE_LINK_LIBS}")
|
|
41
|
+
endif()
|
|
42
|
+
|
|
43
|
+
# Glob shared C++ files
|
|
44
|
+
file(GLOB_RECURSE SHARED_CPP_FILES "../cpp/*.cpp")
|
|
7
45
|
|
|
8
46
|
# Define C++ library and add all sources
|
|
9
|
-
add_library(
|
|
10
|
-
|
|
11
|
-
|
|
47
|
+
add_library(NitroSimplejsble SHARED
|
|
48
|
+
src/main/cpp/cpp-adapter.cpp
|
|
49
|
+
${SHARED_CPP_FILES}
|
|
12
50
|
)
|
|
13
51
|
|
|
14
|
-
# Add Nitrogen specs
|
|
52
|
+
# Add Nitrogen specs
|
|
15
53
|
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/NitroSimplejsble+autolinking.cmake)
|
|
16
54
|
|
|
17
|
-
# Add simpleble
|
|
18
|
-
# Path: packages/simplejsble/android → ../../../../simpleble
|
|
19
|
-
add_subdirectory(${CMAKE_SOURCE_DIR}/../../../../simpleble ${CMAKE_BINARY_DIR}/simpleble)
|
|
20
|
-
|
|
21
55
|
# Set up local includes
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
56
|
+
target_include_directories(NitroSimplejsble PRIVATE
|
|
57
|
+
src/main/cpp
|
|
58
|
+
../cpp
|
|
25
59
|
)
|
|
26
60
|
|
|
27
61
|
find_library(LOG_LIB log)
|
|
28
62
|
|
|
29
63
|
# Link all libraries together
|
|
30
|
-
target_link_libraries(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
64
|
+
target_link_libraries(NitroSimplejsble
|
|
65
|
+
${LOG_LIB}
|
|
66
|
+
simpleble::simpleble
|
|
67
|
+
android
|
|
68
|
+
-ldl
|
|
35
69
|
)
|
|
70
|
+
|
|
71
|
+
# Allow undefined JNI symbols (resolved at runtime)
|
|
72
|
+
set_target_properties(NitroSimplejsble PROPERTIES
|
|
73
|
+
LINK_FLAGS "-Wl,--allow-shlib-undefined -Wl,--unresolved-symbols=ignore-in-object-files"
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
# Add SimpleBLE compile definitions
|
|
77
|
+
target_compile_definitions(NitroSimplejsble PRIVATE FMT_HEADER_ONLY FMT_UNICODE=0)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Detect if the project is being build within a project or standalone.
|
|
2
|
+
if(PROJECT_IS_TOP_LEVEL)
|
|
3
|
+
# Configure the build path
|
|
4
|
+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
|
5
|
+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
|
6
|
+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
|
7
|
+
|
|
8
|
+
# Nice hack to automatically ignore the build directory
|
|
9
|
+
file(WRITE ${CMAKE_BINARY_DIR}/.gitignore "*")
|
|
10
|
+
endif()
|
|
11
|
+
|
|
12
|
+
if(CMAKE_GENERATOR_PLATFORM MATCHES "^[Ww][Ii][Nn]32$")
|
|
13
|
+
set(WINDOWS_TARGET_ARCH x86)
|
|
14
|
+
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Xx]64$")
|
|
15
|
+
set(WINDOWS_TARGET_ARCH x64)
|
|
16
|
+
endif()
|
|
17
|
+
|
|
18
|
+
macro(apply_build_options
|
|
19
|
+
TARGET
|
|
20
|
+
PRIVATE_COMPILE_DEFINITIONS
|
|
21
|
+
PRIVATE_COMPILE_OPTIONS
|
|
22
|
+
PRIVATE_LINK_OPTIONS
|
|
23
|
+
PUBLIC_LINK_OPTIONS)
|
|
24
|
+
|
|
25
|
+
foreach(DEFINITION ${PRIVATE_COMPILE_DEFINITIONS})
|
|
26
|
+
message(STATUS "Applying definition: ${DEFINITION}")
|
|
27
|
+
target_compile_definitions(${TARGET} PRIVATE ${DEFINITION})
|
|
28
|
+
endforeach()
|
|
29
|
+
|
|
30
|
+
foreach(OPTION ${PRIVATE_COMPILE_OPTIONS})
|
|
31
|
+
message(STATUS "Applying private compile option: ${OPTION}")
|
|
32
|
+
target_compile_options(${TARGET} PRIVATE ${OPTION})
|
|
33
|
+
endforeach()
|
|
34
|
+
|
|
35
|
+
foreach(OPTION ${PRIVATE_LINK_OPTIONS})
|
|
36
|
+
message(STATUS "Applying private link option: ${OPTION}")
|
|
37
|
+
target_link_options(${TARGET} PRIVATE ${OPTION})
|
|
38
|
+
endforeach()
|
|
39
|
+
|
|
40
|
+
foreach(OPTION ${PUBLIC_LINK_OPTIONS})
|
|
41
|
+
message(STATUS "Applying public link option: ${OPTION}")
|
|
42
|
+
target_link_options(${TARGET} PUBLIC ${OPTION})
|
|
43
|
+
endforeach()
|
|
44
|
+
|
|
45
|
+
endmacro()
|
|
46
|
+
|
|
47
|
+
macro(append_sanitize_options MODIFIER)
|
|
48
|
+
|
|
49
|
+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
|
|
50
|
+
# Note to self: When dealing with memory issues/leaks on Darwin, the following documentation is useful:
|
|
51
|
+
# - https://clang.llvm.org/docs/AutomaticReferenceCounting.html
|
|
52
|
+
# - https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html
|
|
53
|
+
|
|
54
|
+
if(${MODIFIER} MATCHES "Address")
|
|
55
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fsanitize=address)
|
|
56
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fsanitize-recover=address)
|
|
57
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fno-omit-frame-pointer)
|
|
58
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fno-common)
|
|
59
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -g)
|
|
60
|
+
list(APPEND PUBLIC_LINK_OPTIONS -fsanitize=address)
|
|
61
|
+
|
|
62
|
+
elseif(${MODIFIER} MATCHES "Thread")
|
|
63
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fsanitize=thread)
|
|
64
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fno-omit-frame-pointer)
|
|
65
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -fno-common)
|
|
66
|
+
list(APPEND PRIVATE_COMPILE_OPTIONS -g)
|
|
67
|
+
list(APPEND PUBLIC_LINK_OPTIONS -fsanitize=thread)
|
|
68
|
+
|
|
69
|
+
endif()
|
|
70
|
+
|
|
71
|
+
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
|
|
72
|
+
# TODO: Figure out how to properly link the MSVC address sanitizer libraries.
|
|
73
|
+
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fsanitize=address /Zi /MD /DEBUG")
|
|
74
|
+
endif()
|
|
75
|
+
|
|
76
|
+
endmacro()
|
|
77
|
+
|
|
78
|
+
# Define the list of source files with relative paths
|
|
79
|
+
set(SIMPLEJNI_SOURCES
|
|
80
|
+
"${CMAKE_CURRENT_LIST_DIR}/../dependencies/internal/src/simplejni/Common.cpp"
|
|
81
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Attempt to load DBus1 as CONFIG silently
|
|
2
|
+
find_package(DBus1 CONFIG QUIET)
|
|
3
|
+
|
|
4
|
+
# If DBus1 was not found, try to find it as MODULE
|
|
5
|
+
if(NOT DBus1_FOUND)
|
|
6
|
+
|
|
7
|
+
find_package(PkgConfig REQUIRED)
|
|
8
|
+
pkg_search_module(PC_DBUS REQUIRED dbus-1)
|
|
9
|
+
|
|
10
|
+
set(DBus1_LIBRARIES ${PC_DBUS_LIBRARIES})
|
|
11
|
+
set(DBus1_INCLUDE_DIRS ${PC_DBUS_INCLUDE_DIRS})
|
|
12
|
+
|
|
13
|
+
# setup imported target
|
|
14
|
+
add_library(dbus-1 SHARED IMPORTED)
|
|
15
|
+
set_property(TARGET dbus-1 APPEND PROPERTY IMPORTED_LOCATION ${PC_DBUS_LINK_LIBRARIES})
|
|
16
|
+
set_property(TARGET dbus-1 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${DBus1_INCLUDE_DIRS})
|
|
17
|
+
|
|
18
|
+
set(DBus1_FOUND ${PC_DBUS_FOUND})
|
|
19
|
+
|
|
20
|
+
endif()
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Read the documentation of FetchContent first!
|
|
2
|
+
# https://cmake.org/cmake/help/latest/module/FetchContent.html
|
|
3
|
+
|
|
4
|
+
# TODO: We need to upgrade to a newer version due to the following issue:
|
|
5
|
+
# https://github.com/fmtlib/fmt/issues/3540
|
|
6
|
+
|
|
7
|
+
include(FetchContent)
|
|
8
|
+
|
|
9
|
+
if(LIBFMT_VENDORIZE)
|
|
10
|
+
|
|
11
|
+
# Load default parameters passed in through the command line.
|
|
12
|
+
if(NOT LIBFMT_GIT_REPOSITORY)
|
|
13
|
+
set(LIBFMT_GIT_REPOSITORY "https://github.com/fmtlib/fmt.git")
|
|
14
|
+
endif()
|
|
15
|
+
if(NOT LIBFMT_GIT_TAG)
|
|
16
|
+
set(LIBFMT_GIT_TAG "a33701196adfad74917046096bf5a2aa0ab0bb50") # v9.1.0
|
|
17
|
+
endif()
|
|
18
|
+
|
|
19
|
+
if(NOT LIBFMT_LOCAL_PATH)
|
|
20
|
+
# Library includes
|
|
21
|
+
FetchContent_Declare(
|
|
22
|
+
fmt
|
|
23
|
+
GIT_REPOSITORY ${LIBFMT_GIT_REPOSITORY}
|
|
24
|
+
GIT_TAG ${LIBFMT_GIT_TAG}
|
|
25
|
+
)
|
|
26
|
+
FetchContent_MakeAvailable(fmt)
|
|
27
|
+
else()
|
|
28
|
+
add_subdirectory(${LIBFMT_LOCAL_PATH} ${CMAKE_CURRENT_BINARY_DIR}/libfmt)
|
|
29
|
+
endif()
|
|
30
|
+
|
|
31
|
+
# Because we are in a find module, we are solely responsible for resolution.
|
|
32
|
+
# Setting this *_FOUND variable to a truthy value will signal to the calling
|
|
33
|
+
# find_package() command that we were successful.
|
|
34
|
+
# More relevant info regarding find modules and what variables they use can be
|
|
35
|
+
# found in the documentation of find_package() and
|
|
36
|
+
# https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html
|
|
37
|
+
set(fmt_FOUND 1)
|
|
38
|
+
|
|
39
|
+
else()
|
|
40
|
+
find_package(fmt CONFIG REQUIRED)
|
|
41
|
+
endif()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Read the documentation of FetchContent first!
|
|
2
|
+
# https://cmake.org/cmake/help/latest/module/FetchContent.html
|
|
3
|
+
|
|
4
|
+
include(FetchContent)
|
|
5
|
+
|
|
6
|
+
# Library includes
|
|
7
|
+
FetchContent_Declare(googletest
|
|
8
|
+
URL https://github.com/google/googletest/archive/refs/tags/v1.13.0.zip
|
|
9
|
+
)
|
|
10
|
+
FetchContent_MakeAvailable(googletest)
|
|
11
|
+
|
|
12
|
+
# Because we are in a find module, we are solely responsible for resolution.
|
|
13
|
+
# Setting this *_FOUND variable to a truthy value will signal to the calling
|
|
14
|
+
# find_package() command that we were successful.
|
|
15
|
+
# More relevant info regarding find modules and what variables they use can be
|
|
16
|
+
# found in the documentation of find_package() and
|
|
17
|
+
# https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html
|
|
18
|
+
set(googletest_FOUND 1)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Copyright (C) 2020 Google, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
# parse_version() reads and parses the version string from FILE, assigning the
|
|
16
|
+
# version string to ${PROJECT}_VERSION and the parsed version to
|
|
17
|
+
# ${PROJECT}_VERSION_MAJOR, ${PROJECT}_VERSION_MINOR, ${PROJECT}_VERSION_PATCH,
|
|
18
|
+
# and the optional ${PROJECT}_VERSION_FLAVOR.
|
|
19
|
+
#
|
|
20
|
+
# The version string take one of the forms:
|
|
21
|
+
# <major>.<minor>.<patch>
|
|
22
|
+
# <major>.<minor>.<patch>-<flavor>
|
|
23
|
+
function(parse_version FILE PROJECT)
|
|
24
|
+
configure_file(${FILE} "${CMAKE_CURRENT_BINARY_DIR}/VERSION") # Required to re-run cmake on version change
|
|
25
|
+
file(READ ${FILE} VERSION_STR)
|
|
26
|
+
string(STRIP ${VERSION_STR} VERSION_STR)
|
|
27
|
+
if(${VERSION_STR} MATCHES "([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[a-zA-Z0-9]+)?")
|
|
28
|
+
set(FLAVOR "")
|
|
29
|
+
if(NOT "${CMAKE_MATCH_4}" STREQUAL "")
|
|
30
|
+
string(SUBSTRING ${CMAKE_MATCH_4} 1 -1 FLAVOR)
|
|
31
|
+
endif()
|
|
32
|
+
set("${PROJECT}_VERSION_MAJOR" ${CMAKE_MATCH_1} PARENT_SCOPE)
|
|
33
|
+
set("${PROJECT}_VERSION_MINOR" ${CMAKE_MATCH_2} PARENT_SCOPE)
|
|
34
|
+
set("${PROJECT}_VERSION_PATCH" ${CMAKE_MATCH_3} PARENT_SCOPE)
|
|
35
|
+
set("${PROJECT}_VERSION_FLAVOR" ${FLAVOR} PARENT_SCOPE)
|
|
36
|
+
set("${PROJECT}_VERSION"
|
|
37
|
+
"${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}${CMAKE_MATCH_4}"
|
|
38
|
+
PARENT_SCOPE)
|
|
39
|
+
set("${PROJECT}_VERSION_BASE"
|
|
40
|
+
"${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}"
|
|
41
|
+
PARENT_SCOPE)
|
|
42
|
+
else()
|
|
43
|
+
message(FATAL_ERROR "Unable to parse version from '${FILE}'")
|
|
44
|
+
endif()
|
|
45
|
+
endfunction()
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.21)
|
|
2
|
+
|
|
3
|
+
# Common configuration used by all projects in this repository
|
|
4
|
+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
5
|
+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/find)
|
|
6
|
+
|
|
7
|
+
include(${CMAKE_CURRENT_LIST_DIR}/parse_version.cmake)
|
|
8
|
+
parse_version(${CMAKE_CURRENT_LIST_DIR}/../VERSION SIMPLEBLE)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
This folder is intended to contain external and internal dependencies for the project.
|
|
2
|
+
Initially we expect to have header only dependencies.
|
|
3
|
+
External dependencies will be bundled as part of the install directory.
|
|
4
|
+
Internal dependencies are only consumed internally and don't affect any public API.
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
#ifndef KVN_BYTEARRAY_H
|
|
2
|
+
#define KVN_BYTEARRAY_H
|
|
3
|
+
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <cstring>
|
|
6
|
+
#include <iomanip>
|
|
7
|
+
#include <iostream>
|
|
8
|
+
#include <memory>
|
|
9
|
+
#include <sstream>
|
|
10
|
+
#include <stdexcept>
|
|
11
|
+
#include <type_traits>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
namespace kvn {
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @class bytearray
|
|
18
|
+
* @brief A class to handle byte arrays and their conversion from/to hex strings.
|
|
19
|
+
*/
|
|
20
|
+
class bytearray {
|
|
21
|
+
public:
|
|
22
|
+
/**
|
|
23
|
+
* @brief Default constructor.
|
|
24
|
+
*/
|
|
25
|
+
bytearray() = default;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @brief Constructs byte array from a vector of uint8_t.
|
|
29
|
+
* @param vec A vector of uint8_t.
|
|
30
|
+
*/
|
|
31
|
+
bytearray(const std::vector<uint8_t>& vec) : data_(vec) {}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @brief Constructs byte array from a raw pointer and size.
|
|
35
|
+
* @param ptr A pointer to uint8_t data.
|
|
36
|
+
* @param size The size of the data.
|
|
37
|
+
*/
|
|
38
|
+
bytearray(const uint8_t* ptr, size_t size) : data_(ptr, ptr + size) {}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @brief Constructs byte array from iterators.
|
|
42
|
+
* @tparam InputIt Iterator type.
|
|
43
|
+
* @param first Iterator to the first element.
|
|
44
|
+
* @param last Iterator to one past the last element.
|
|
45
|
+
*/
|
|
46
|
+
template <typename InputIt>
|
|
47
|
+
bytearray(InputIt first, InputIt last) : data_(first, last) {}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @brief Constructs byte array from a std::string.
|
|
51
|
+
* @param byteArr A string containing byte data.
|
|
52
|
+
*/
|
|
53
|
+
bytearray(const std::string& byteArr) : data_(byteArr.begin(), byteArr.end()) {}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @brief Constructs byte array from a C-style string and size.
|
|
57
|
+
* @param byteArr A C-style string.
|
|
58
|
+
* @param size The size of the string.
|
|
59
|
+
*/
|
|
60
|
+
bytearray(const char* byteArr, size_t size) : bytearray(std::string(byteArr, size)) {}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @brief Constructs byte array from a C-style string.
|
|
64
|
+
* @param byteArr A C-style string.
|
|
65
|
+
*/
|
|
66
|
+
bytearray(const char* byteArr) : bytearray(std::string(byteArr)) {}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @brief Constructs a byte array of specified size, initialized with zeros.
|
|
70
|
+
* @param size The number of bytes to allocate.
|
|
71
|
+
*/
|
|
72
|
+
explicit bytearray(size_t size) : data_(size) {}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @brief Creates a ByteArray from a hex string.
|
|
76
|
+
*
|
|
77
|
+
* Case is ignored and the string may have a '0x' hex prefix or not.
|
|
78
|
+
*
|
|
79
|
+
* @param hexStr A string containing hex data.
|
|
80
|
+
* @return A ByteArray object.
|
|
81
|
+
* @throws std::invalid_argument If the hex string contains non-hexadecimal characters.
|
|
82
|
+
* @throws std::length_error If the hex string length is not even.
|
|
83
|
+
*/
|
|
84
|
+
static bytearray fromHex(const std::string& hexStr) {
|
|
85
|
+
std::string cleanString(hexStr);
|
|
86
|
+
|
|
87
|
+
// Check and skip the '0x' prefix if present
|
|
88
|
+
if (cleanString.size() >= 2 && cleanString.substr(0, 2) == "0x") {
|
|
89
|
+
cleanString = cleanString.substr(2);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
size_t size = cleanString.size();
|
|
93
|
+
if (size % 2 != 0) {
|
|
94
|
+
throw std::length_error("Hex string length must be even.");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
bytearray byteArray;
|
|
98
|
+
byteArray.data_.reserve(size / 2);
|
|
99
|
+
|
|
100
|
+
for (size_t i = 0; i < size; i += 2) {
|
|
101
|
+
uint8_t byte = static_cast<uint8_t>(std::stoi(cleanString.substr(i, 2), nullptr, 16));
|
|
102
|
+
byteArray.data_.push_back(byte);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return byteArray;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @overload
|
|
110
|
+
*/
|
|
111
|
+
static bytearray fromHex(const char* byteArr) { return fromHex(std::string(byteArr)); }
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @overload
|
|
115
|
+
*/
|
|
116
|
+
static bytearray fromHex(const char* byteArr, size_t size) { return fromHex(std::string(byteArr, size)); }
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @brief Converts the byte array to a lowercase hex string without '0x' prefix.
|
|
120
|
+
* @param spacing Whether to include spaces between bytes.
|
|
121
|
+
*
|
|
122
|
+
* @return A hex string representation of the byte array.
|
|
123
|
+
*/
|
|
124
|
+
std::string toHex(bool spacing = false) const {
|
|
125
|
+
std::ostringstream oss;
|
|
126
|
+
for (auto byte : data_) {
|
|
127
|
+
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
|
|
128
|
+
if (spacing) {
|
|
129
|
+
oss << " ";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return oss.str();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @brief Slices the byte array from a specified start index to an end index.
|
|
137
|
+
*
|
|
138
|
+
* This method creates a new byte array containing bytes from the specified range.
|
|
139
|
+
* The start index is inclusive, while the end index is exclusive.
|
|
140
|
+
*
|
|
141
|
+
* @param start The starting index from which to begin slicing.
|
|
142
|
+
* @param end The ending index up to which to slice (exclusive).
|
|
143
|
+
* @return byte array A new byte array containing the sliced segment.
|
|
144
|
+
* @throws std::out_of_range If the start index is greater than the end index or if the end index is out of bounds.
|
|
145
|
+
*/
|
|
146
|
+
bytearray slice(size_t start, size_t end) const {
|
|
147
|
+
if (start > end || end > data_.size()) {
|
|
148
|
+
throw std::out_of_range("Invalid slice range");
|
|
149
|
+
}
|
|
150
|
+
return bytearray(std::vector<uint8_t>(data_.begin() + start, data_.begin() + end));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @brief Slices the byte array from a specified start index to the end of the array.
|
|
155
|
+
*
|
|
156
|
+
* This method creates a new byte array containing all bytes from the specified start index to the end of the
|
|
157
|
+
* byte array.
|
|
158
|
+
*
|
|
159
|
+
* @param start The starting index from which to begin slicing.
|
|
160
|
+
* @return byte array A new byte array containing the sliced segment from the start index to the end.
|
|
161
|
+
* @throws std::out_of_range If the start index is out of the bounds of the byte array.
|
|
162
|
+
*/
|
|
163
|
+
bytearray slice_from(size_t start) const { return slice(start, data_.size()); }
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @brief Slices the byte array from the beginning to a specified end index.
|
|
167
|
+
*
|
|
168
|
+
* This method creates a new byte array containing all bytes from the beginning of the byte array to the specified
|
|
169
|
+
* end index.
|
|
170
|
+
*
|
|
171
|
+
* @param end The ending index up to which to slice (exclusive).
|
|
172
|
+
* @return byte array A new byte array containing the sliced segment from the beginning to the end index.
|
|
173
|
+
* @throws std::out_of_range If the end index is out of the bounds of the byte array.
|
|
174
|
+
*/
|
|
175
|
+
bytearray slice_to(size_t end) const { return slice(0, end); }
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @brief Overloaded stream insertion operator for byte array.
|
|
179
|
+
* @param os The output stream.
|
|
180
|
+
* @param byteArray The byte array object.
|
|
181
|
+
* @return The output stream.
|
|
182
|
+
*/
|
|
183
|
+
friend std::ostream& operator<<(std::ostream& os, const bytearray& byteArray) {
|
|
184
|
+
os << byteArray.toHex(true);
|
|
185
|
+
return os;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @brief Conversion operator to convert byte array to std::string.
|
|
190
|
+
*
|
|
191
|
+
* @note This is provided to support code that relies on byte array
|
|
192
|
+
* being representd as a string.
|
|
193
|
+
* @return String containing the raw bytes of the byte array
|
|
194
|
+
*/
|
|
195
|
+
operator std::string() const { return std::string(data_.begin(), data_.end()); }
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @brief Conversion operator to convert byte array to std::vector<uint8_t>.
|
|
199
|
+
* @return Vector containing the raw bytes of the byte array
|
|
200
|
+
*/
|
|
201
|
+
operator std::vector<uint8_t>() const { return data_; }
|
|
202
|
+
|
|
203
|
+
//! @cond Doxygen_Suppress
|
|
204
|
+
// Expose vector-like functionality
|
|
205
|
+
size_t size() const { return data_.size(); }
|
|
206
|
+
const uint8_t* data() const { return data_.data(); }
|
|
207
|
+
uint8_t* data() { return data_.data(); }
|
|
208
|
+
bool empty() const { return data_.empty(); }
|
|
209
|
+
void clear() { data_.clear(); }
|
|
210
|
+
uint8_t& operator[](size_t index) { return data_[index]; }
|
|
211
|
+
const uint8_t& operator[](size_t index) const { return data_[index]; }
|
|
212
|
+
void push_back(uint8_t byte) { data_.push_back(byte); }
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @brief Appends a uint16_t value as 2 little-endian bytes.
|
|
216
|
+
* @param value The uint16_t value to append.
|
|
217
|
+
*/
|
|
218
|
+
void push_back(uint16_t value) {
|
|
219
|
+
for (size_t i = 0; i < 2; ++i) {
|
|
220
|
+
data_.push_back(static_cast<uint8_t>(value >> (i * 8)));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @brief Appends a uint32_t value as 4 little-endian bytes.
|
|
226
|
+
* @param value The uint32_t value to append.
|
|
227
|
+
*/
|
|
228
|
+
void push_back(uint32_t value) {
|
|
229
|
+
for (size_t i = 0; i < 4; ++i) {
|
|
230
|
+
data_.push_back(static_cast<uint8_t>(value >> (i * 8)));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @brief Appends a uint64_t value as 8 little-endian bytes.
|
|
236
|
+
* @param value The uint64_t value to append.
|
|
237
|
+
*/
|
|
238
|
+
void push_back(uint64_t value) {
|
|
239
|
+
for (size_t i = 0; i < 8; ++i) {
|
|
240
|
+
data_.push_back(static_cast<uint8_t>(value >> (i * 8)));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
auto begin() { return data_.begin(); }
|
|
244
|
+
auto begin() const { return data_.begin(); }
|
|
245
|
+
auto end() { return data_.end(); }
|
|
246
|
+
auto end() const { return data_.end(); }
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @brief Inserts a single byte at the specified position.
|
|
250
|
+
* @param pos Iterator to the position where the element is inserted.
|
|
251
|
+
* @param value The byte to insert.
|
|
252
|
+
* @return Iterator pointing to the inserted element.
|
|
253
|
+
*/
|
|
254
|
+
auto insert(typename std::vector<uint8_t>::iterator pos, uint8_t value) { return data_.insert(pos, value); }
|
|
255
|
+
auto insert(typename std::vector<uint8_t>::const_iterator pos, uint8_t value) { return data_.insert(pos, value); }
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @brief Inserts multiple copies of a byte at the specified position.
|
|
259
|
+
* @param pos Iterator to the position where the elements are inserted.
|
|
260
|
+
* @param count Number of copies to insert.
|
|
261
|
+
* @param value The byte to insert.
|
|
262
|
+
* @return Iterator pointing to the first inserted element.
|
|
263
|
+
*/
|
|
264
|
+
auto insert(typename std::vector<uint8_t>::iterator pos, size_t count, uint8_t value) { return data_.insert(pos, count, value); }
|
|
265
|
+
auto insert(typename std::vector<uint8_t>::const_iterator pos, size_t count, uint8_t value) { return data_.insert(pos, count, value); }
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @brief Inserts elements from a range at the specified position.
|
|
269
|
+
* @tparam InputIt Iterator type.
|
|
270
|
+
* @param pos Iterator to the position where the elements are inserted.
|
|
271
|
+
* @param first Iterator to the first element of the range.
|
|
272
|
+
* @param last Iterator to one past the last element of the range.
|
|
273
|
+
* @return Iterator pointing to the first inserted element.
|
|
274
|
+
*/
|
|
275
|
+
template <typename InputIt>
|
|
276
|
+
auto insert(typename std::vector<uint8_t>::iterator pos, InputIt first, InputIt last) { return data_.insert(pos, first, last); }
|
|
277
|
+
template <typename InputIt>
|
|
278
|
+
auto insert(typename std::vector<uint8_t>::const_iterator pos, InputIt first, InputIt last) { return data_.insert(pos, first, last); }
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* @brief Inserts another bytearray at the specified position.
|
|
282
|
+
* @param pos Iterator to the position where the elements are inserted.
|
|
283
|
+
* @param other The bytearray to insert.
|
|
284
|
+
* @return Iterator pointing to the first inserted element.
|
|
285
|
+
*/
|
|
286
|
+
auto insert(typename std::vector<uint8_t>::iterator pos, const bytearray& other) { return data_.insert(pos, other.begin(), other.end()); }
|
|
287
|
+
auto insert(typename std::vector<uint8_t>::const_iterator pos, const bytearray& other) { return data_.insert(pos, other.begin(), other.end()); }
|
|
288
|
+
|
|
289
|
+
//! @endcond
|
|
290
|
+
|
|
291
|
+
private:
|
|
292
|
+
std::vector<uint8_t> data_;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
} // namespace kvn
|
|
296
|
+
|
|
297
|
+
#endif // KVN_BYTEARRAY_H
|