simplejsble 0.0.18 → 0.0.19

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.
Files changed (85) hide show
  1. package/NitroSimplejsble.podspec +22 -8
  2. package/dependencies/internal/include/simplejni/VM.hpp +0 -1
  3. package/macos/CMakeLists.txt +21 -0
  4. package/macos/SimpleBLE-macos.xcframework/Info.plist +30 -0
  5. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Adapter.h +102 -0
  6. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/AdapterSafe.h +58 -0
  7. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Advanced.h +50 -0
  8. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Characteristic.h +39 -0
  9. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Config.h +64 -0
  10. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Descriptor.h +30 -0
  11. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Exceptions.h +72 -0
  12. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Logging.h +73 -0
  13. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Peripheral.h +82 -0
  14. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/PeripheralSafe.h +64 -0
  15. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Service.h +34 -0
  16. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/SimpleBLE.h +8 -0
  17. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Types.h +49 -0
  18. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/Utils.h +13 -0
  19. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/export.h +43 -0
  20. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/Headers/simpleble/kvn/kvn_bytearray.h +297 -0
  21. package/macos/SimpleBLE-macos.xcframework/macos-arm64_x86_64/libsimpleble.a +0 -0
  22. package/macos/build_simpleble.sh +61 -0
  23. package/macos/simpleble_macosx_arm64/include/simpleble/Adapter.h +102 -0
  24. package/macos/simpleble_macosx_arm64/include/simpleble/AdapterSafe.h +58 -0
  25. package/macos/simpleble_macosx_arm64/include/simpleble/Advanced.h +50 -0
  26. package/macos/simpleble_macosx_arm64/include/simpleble/Characteristic.h +39 -0
  27. package/macos/simpleble_macosx_arm64/include/simpleble/Config.h +64 -0
  28. package/macos/simpleble_macosx_arm64/include/simpleble/Descriptor.h +30 -0
  29. package/macos/simpleble_macosx_arm64/include/simpleble/Exceptions.h +72 -0
  30. package/macos/simpleble_macosx_arm64/include/simpleble/Logging.h +73 -0
  31. package/macos/simpleble_macosx_arm64/include/simpleble/Peripheral.h +82 -0
  32. package/macos/simpleble_macosx_arm64/include/simpleble/PeripheralSafe.h +64 -0
  33. package/macos/simpleble_macosx_arm64/include/simpleble/Service.h +34 -0
  34. package/macos/simpleble_macosx_arm64/include/simpleble/SimpleBLE.h +8 -0
  35. package/macos/simpleble_macosx_arm64/include/simpleble/Types.h +49 -0
  36. package/macos/simpleble_macosx_arm64/include/simpleble/Utils.h +13 -0
  37. package/macos/simpleble_macosx_arm64/include/simpleble/export.h +43 -0
  38. package/macos/simpleble_macosx_arm64/include/simpleble/kvn/kvn_bytearray.h +297 -0
  39. package/macos/simpleble_macosx_arm64/lib/cmake/simpleble/simpleble-config-release.cmake +19 -0
  40. package/macos/simpleble_macosx_arm64/lib/cmake/simpleble/simpleble-config.cmake +107 -0
  41. package/macos/simpleble_macosx_arm64/lib/libsimpleble.a +0 -0
  42. package/macos/simpleble_macosx_arm64/lib/pkgconfig/simpleble.pc +11 -0
  43. package/macos/simpleble_macosx_universal/include/simpleble/Adapter.h +102 -0
  44. package/macos/simpleble_macosx_universal/include/simpleble/AdapterSafe.h +58 -0
  45. package/macos/simpleble_macosx_universal/include/simpleble/Advanced.h +50 -0
  46. package/macos/simpleble_macosx_universal/include/simpleble/Characteristic.h +39 -0
  47. package/macos/simpleble_macosx_universal/include/simpleble/Config.h +64 -0
  48. package/macos/simpleble_macosx_universal/include/simpleble/Descriptor.h +30 -0
  49. package/macos/simpleble_macosx_universal/include/simpleble/Exceptions.h +72 -0
  50. package/macos/simpleble_macosx_universal/include/simpleble/Logging.h +73 -0
  51. package/macos/simpleble_macosx_universal/include/simpleble/Peripheral.h +82 -0
  52. package/macos/simpleble_macosx_universal/include/simpleble/PeripheralSafe.h +64 -0
  53. package/macos/simpleble_macosx_universal/include/simpleble/Service.h +34 -0
  54. package/macos/simpleble_macosx_universal/include/simpleble/SimpleBLE.h +8 -0
  55. package/macos/simpleble_macosx_universal/include/simpleble/Types.h +49 -0
  56. package/macos/simpleble_macosx_universal/include/simpleble/Utils.h +13 -0
  57. package/macos/simpleble_macosx_universal/include/simpleble/export.h +43 -0
  58. package/macos/simpleble_macosx_universal/include/simpleble/kvn/kvn_bytearray.h +297 -0
  59. package/macos/simpleble_macosx_universal/lib/libsimpleble.a +0 -0
  60. package/macos/simpleble_macosx_x86_64/include/simpleble/Adapter.h +102 -0
  61. package/macos/simpleble_macosx_x86_64/include/simpleble/AdapterSafe.h +58 -0
  62. package/macos/simpleble_macosx_x86_64/include/simpleble/Advanced.h +50 -0
  63. package/macos/simpleble_macosx_x86_64/include/simpleble/Characteristic.h +39 -0
  64. package/macos/simpleble_macosx_x86_64/include/simpleble/Config.h +64 -0
  65. package/macos/simpleble_macosx_x86_64/include/simpleble/Descriptor.h +30 -0
  66. package/macos/simpleble_macosx_x86_64/include/simpleble/Exceptions.h +72 -0
  67. package/macos/simpleble_macosx_x86_64/include/simpleble/Logging.h +73 -0
  68. package/macos/simpleble_macosx_x86_64/include/simpleble/Peripheral.h +82 -0
  69. package/macos/simpleble_macosx_x86_64/include/simpleble/PeripheralSafe.h +64 -0
  70. package/macos/simpleble_macosx_x86_64/include/simpleble/Service.h +34 -0
  71. package/macos/simpleble_macosx_x86_64/include/simpleble/SimpleBLE.h +8 -0
  72. package/macos/simpleble_macosx_x86_64/include/simpleble/Types.h +49 -0
  73. package/macos/simpleble_macosx_x86_64/include/simpleble/Utils.h +13 -0
  74. package/macos/simpleble_macosx_x86_64/include/simpleble/export.h +43 -0
  75. package/macos/simpleble_macosx_x86_64/include/simpleble/kvn/kvn_bytearray.h +297 -0
  76. package/macos/simpleble_macosx_x86_64/lib/cmake/simpleble/simpleble-config-release.cmake +19 -0
  77. package/macos/simpleble_macosx_x86_64/lib/cmake/simpleble/simpleble-config.cmake +107 -0
  78. package/macos/simpleble_macosx_x86_64/lib/libsimpleble.a +0 -0
  79. package/macos/simpleble_macosx_x86_64/lib/pkgconfig/simpleble.pc +11 -0
  80. package/package.json +5 -3
  81. package/react-native.config.js +4 -0
  82. package/lib/index.d.ts +0 -2
  83. package/lib/index.js +0 -2
  84. package/lib/specs/Adapter.nitro.d.ts +0 -9
  85. package/lib/specs/Adapter.nitro.js +0 -1
@@ -0,0 +1,82 @@
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+ #include <functional>
5
+ #include <map>
6
+ #include <memory>
7
+ #include <string>
8
+ #include <vector>
9
+
10
+ #include <simpleble/export.h>
11
+
12
+ #include <simpleble/Exceptions.h>
13
+ #include <simpleble/Service.h>
14
+ #include <simpleble/Types.h>
15
+
16
+ namespace SimpleBLE {
17
+
18
+ class PeripheralBase;
19
+
20
+ class SIMPLEBLE_EXPORT Peripheral {
21
+ public:
22
+ Peripheral() = default;
23
+ virtual ~Peripheral() = default;
24
+
25
+ bool initialized() const;
26
+ void* underlying() const;
27
+
28
+ std::string identifier();
29
+ BluetoothAddress address();
30
+ BluetoothAddressType address_type();
31
+ int16_t rssi();
32
+
33
+ /**
34
+ * @brief Provides the advertised transmit power in dBm.
35
+ *
36
+ * @note If the field has not been advertised by the peripheral,
37
+ * the returned value will be -32768.
38
+ */
39
+ int16_t tx_power();
40
+ uint16_t mtu();
41
+
42
+ void connect();
43
+ void disconnect();
44
+ bool is_connected();
45
+ bool is_connectable();
46
+ bool is_paired();
47
+ void unpair();
48
+
49
+ /**
50
+ * @brief Provides a list of all services that are available on the peripheral.
51
+ *
52
+ * @note If the peripheral is not connected, it will return a list of services
53
+ * that were advertised by the device.
54
+ */
55
+ std::vector<Service> services();
56
+ std::map<uint16_t, ByteArray> manufacturer_data();
57
+
58
+ /* Calling any of the methods below when the device is not connected will throw
59
+ Exception::NotConnected */
60
+ // clang-format off
61
+ ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic);
62
+ void write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data);
63
+ void write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data);
64
+ void notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback);
65
+ void indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback);
66
+ void unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic);
67
+
68
+ ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor);
69
+ void write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data);
70
+ // clang-format on
71
+
72
+ void set_callback_on_connected(std::function<void()> on_connected);
73
+ void set_callback_on_disconnected(std::function<void()> on_disconnected);
74
+
75
+ protected:
76
+ PeripheralBase* operator->();
77
+ const PeripheralBase* operator->() const;
78
+
79
+ std::shared_ptr<PeripheralBase> internal_;
80
+ };
81
+
82
+ } // namespace SimpleBLE
@@ -0,0 +1,64 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <optional>
5
+
6
+ #include <simpleble/export.h>
7
+
8
+ #include <simpleble/Peripheral.h>
9
+ #include <simpleble/Service.h>
10
+
11
+ namespace SimpleBLE {
12
+
13
+ namespace Safe {
14
+
15
+ class SIMPLEBLE_EXPORT Peripheral {
16
+ public:
17
+ Peripheral(SimpleBLE::Peripheral& peripheral);
18
+ Peripheral(SimpleBLE::Peripheral&& peripheral);
19
+ virtual ~Peripheral() = default;
20
+
21
+ std::optional<std::string> identifier() noexcept;
22
+ std::optional<BluetoothAddress> address() noexcept;
23
+ std::optional<BluetoothAddressType> address_type() noexcept;
24
+ std::optional<int16_t> rssi() noexcept;
25
+ std::optional<int16_t> tx_power() noexcept;
26
+ std::optional<uint16_t> mtu() noexcept;
27
+
28
+ bool connect() noexcept;
29
+ bool disconnect() noexcept;
30
+ std::optional<bool> is_connected() noexcept;
31
+ std::optional<bool> is_connectable() noexcept;
32
+ std::optional<bool> is_paired() noexcept;
33
+ bool unpair() noexcept;
34
+
35
+ std::optional<std::vector<Service>> services() noexcept;
36
+ std::optional<std::map<uint16_t, ByteArray>> manufacturer_data() noexcept;
37
+
38
+ // clang-format off
39
+ std::optional<ByteArray> read(BluetoothUUID const& service, BluetoothUUID const& characteristic) noexcept;
40
+ bool write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) noexcept;
41
+ bool write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) noexcept;
42
+ bool notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) noexcept;
43
+ bool indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) noexcept;
44
+ bool unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) noexcept;
45
+
46
+ std::optional<ByteArray> read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor) noexcept;
47
+ bool write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data) noexcept;
48
+ // clang-format on
49
+
50
+ bool set_callback_on_connected(std::function<void()> on_connected) noexcept;
51
+ bool set_callback_on_disconnected(std::function<void()> on_disconnected) noexcept;
52
+
53
+ /**
54
+ * Get the underlying peripheral object.
55
+ */
56
+ operator SimpleBLE::Peripheral() const noexcept;
57
+
58
+ protected:
59
+ SimpleBLE::Peripheral internal_;
60
+ };
61
+
62
+ } // namespace Safe
63
+
64
+ } // namespace SimpleBLE
@@ -0,0 +1,34 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <vector>
5
+
6
+ #include <simpleble/export.h>
7
+
8
+ #include <simpleble/Exceptions.h>
9
+ #include <simpleble/Types.h>
10
+ #include "simpleble/Characteristic.h"
11
+
12
+ namespace SimpleBLE {
13
+
14
+ class ServiceBase;
15
+
16
+ class SIMPLEBLE_EXPORT Service {
17
+ public:
18
+ Service() = default;
19
+ virtual ~Service() = default;
20
+
21
+ bool initialized() const;
22
+
23
+ BluetoothUUID uuid();
24
+ ByteArray data();
25
+ std::vector<Characteristic> characteristics();
26
+
27
+ protected:
28
+ const ServiceBase* operator->() const;
29
+ ServiceBase* operator->();
30
+
31
+ std::shared_ptr<ServiceBase> internal_;
32
+ };
33
+
34
+ } // namespace SimpleBLE
@@ -0,0 +1,8 @@
1
+ #pragma once
2
+
3
+ #include <simpleble/Config.h>
4
+ #include <simpleble/Adapter.h>
5
+ #include <simpleble/AdapterSafe.h>
6
+ #include <simpleble/Peripheral.h>
7
+ #include <simpleble/PeripheralSafe.h>
8
+ #include <simpleble/Utils.h>
@@ -0,0 +1,49 @@
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+ #include <string>
5
+ #include <vector>
6
+ #include "kvn/kvn_bytearray.h"
7
+
8
+ /**
9
+ * @file Types.h
10
+ * @brief Defines types and enumerations used throughout the SimpleBLE library.
11
+ */
12
+
13
+ namespace SimpleBLE {
14
+
15
+ using BluetoothAddress = std::string;
16
+
17
+ // IDEA: Extend BluetoothUUID to include a `uuid` function that
18
+ // returns the same string, but provides a homogeneous interface.
19
+ using BluetoothUUID = std::string;
20
+
21
+ /**
22
+ * @typedef ByteArray
23
+ * @brief Represents a byte array using kvn::bytearray from the external library.
24
+ */
25
+ using ByteArray = kvn::bytearray;
26
+
27
+ #ifdef ANDROID
28
+ #pragma push_macro("ANDROID")
29
+ #undef ANDROID
30
+ #define ANDROID_WAS_DEFINED
31
+ #endif
32
+
33
+ enum class OperatingSystem {
34
+ WINDOWS,
35
+ MACOS,
36
+ IOS,
37
+ LINUX,
38
+ ANDROID,
39
+ };
40
+
41
+ #ifdef ANDROID_WAS_DEFINED
42
+ #pragma pop_macro("ANDROID")
43
+ #undef ANDROID_WAS_DEFINED
44
+ #endif
45
+
46
+ // TODO: Add to_string functions for all enums.
47
+ enum BluetoothAddressType : int32_t { PUBLIC = 0, RANDOM = 1, UNSPECIFIED = 2 };
48
+
49
+ } // namespace SimpleBLE
@@ -0,0 +1,13 @@
1
+ #pragma once
2
+
3
+ #include <simpleble/export.h>
4
+
5
+ #include <simpleble/Types.h>
6
+
7
+ namespace SimpleBLE {
8
+
9
+ OperatingSystem SIMPLEBLE_EXPORT get_operating_system();
10
+
11
+ std::string SIMPLEBLE_EXPORT get_simpleble_version();
12
+
13
+ } // namespace SimpleBLE
@@ -0,0 +1,43 @@
1
+
2
+ #ifndef SIMPLEBLE_EXPORT_H
3
+ #define SIMPLEBLE_EXPORT_H
4
+
5
+ #ifdef SIMPLEBLE_STATIC_DEFINE
6
+ # define SIMPLEBLE_EXPORT
7
+ # define SIMPLEBLE_NO_EXPORT
8
+ #else
9
+ # ifndef SIMPLEBLE_EXPORT
10
+ # ifdef simpleble_EXPORTS
11
+ /* We are building this library */
12
+ # define SIMPLEBLE_EXPORT
13
+ # else
14
+ /* We are using this library */
15
+ # define SIMPLEBLE_EXPORT
16
+ # endif
17
+ # endif
18
+
19
+ # ifndef SIMPLEBLE_NO_EXPORT
20
+ # define SIMPLEBLE_NO_EXPORT
21
+ # endif
22
+ #endif
23
+
24
+ #ifndef SIMPLEBLE_DEPRECATED
25
+ # define SIMPLEBLE_DEPRECATED __attribute__ ((__deprecated__))
26
+ #endif
27
+
28
+ #ifndef SIMPLEBLE_DEPRECATED_EXPORT
29
+ # define SIMPLEBLE_DEPRECATED_EXPORT SIMPLEBLE_EXPORT SIMPLEBLE_DEPRECATED
30
+ #endif
31
+
32
+ #ifndef SIMPLEBLE_DEPRECATED_NO_EXPORT
33
+ # define SIMPLEBLE_DEPRECATED_NO_EXPORT SIMPLEBLE_NO_EXPORT SIMPLEBLE_DEPRECATED
34
+ #endif
35
+
36
+ /* NOLINTNEXTLINE(readability-avoid-unconditional-preprocessor-if) */
37
+ #if 0 /* DEFINE_NO_DEPRECATED */
38
+ # ifndef SIMPLEBLE_NO_DEPRECATED
39
+ # define SIMPLEBLE_NO_DEPRECATED
40
+ # endif
41
+ #endif
42
+
43
+ #endif /* SIMPLEBLE_EXPORT_H */
@@ -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
@@ -0,0 +1,61 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5
+ MACOS_DEPLOYMENT_TARGET="${MACOS_DEPLOYMENT_TARGET:-13.0}"
6
+
7
+ build_for_arch() {
8
+ local ARCH=$1 # arm64 or x86_64
9
+
10
+ local BUILD_DIR="${SCRIPT_DIR}/build_macosx_${ARCH}"
11
+ local INSTALL_DIR="${SCRIPT_DIR}/simpleble_macosx_${ARCH}"
12
+
13
+ echo "Building SimpleBLE for macOS (${ARCH})..."
14
+
15
+ cmake -B "${BUILD_DIR}" -S "${SCRIPT_DIR}" \
16
+ -DCMAKE_BUILD_TYPE=Release \
17
+ -DCMAKE_SYSTEM_NAME=Darwin \
18
+ -DCMAKE_OSX_SYSROOT=macosx \
19
+ -DCMAKE_OSX_ARCHITECTURES="${ARCH}" \
20
+ -DCMAKE_OSX_DEPLOYMENT_TARGET="${MACOS_DEPLOYMENT_TARGET}" \
21
+ -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
22
+ -DBUILD_SHARED_LIBS=OFF \
23
+ -DSIMPLEBLE_EXCLUDE_C=ON
24
+
25
+ cmake --build "${BUILD_DIR}" --config Release --parallel
26
+ cmake --install "${BUILD_DIR}" --config Release
27
+
28
+ echo "Installed to ${INSTALL_DIR}"
29
+ }
30
+
31
+ # Build for Apple Silicon
32
+ build_for_arch "arm64"
33
+
34
+ # Build for Intel
35
+ build_for_arch "x86_64"
36
+
37
+ # Create XCFramework from the static libraries
38
+ echo "Creating XCFramework..."
39
+ XCFRAMEWORK_PATH="${SCRIPT_DIR}/SimpleBLE-macos.xcframework"
40
+ rm -rf "${XCFRAMEWORK_PATH}"
41
+
42
+ # Create a universal (fat) library first, then wrap in XCFramework
43
+ # This is needed because XCFramework expects one library per platform
44
+ UNIVERSAL_DIR="${SCRIPT_DIR}/simpleble_macosx_universal"
45
+ mkdir -p "${UNIVERSAL_DIR}/lib"
46
+
47
+ lipo -create \
48
+ "${SCRIPT_DIR}/simpleble_macosx_arm64/lib/libsimpleble.a" \
49
+ "${SCRIPT_DIR}/simpleble_macosx_x86_64/lib/libsimpleble.a" \
50
+ -output "${UNIVERSAL_DIR}/lib/libsimpleble.a"
51
+
52
+ # Copy headers from arm64 build (headers are the same for both architectures)
53
+ cp -R "${SCRIPT_DIR}/simpleble_macosx_arm64/include" "${UNIVERSAL_DIR}/"
54
+
55
+ xcodebuild -create-xcframework \
56
+ -library "${UNIVERSAL_DIR}/lib/libsimpleble.a" \
57
+ -headers "${UNIVERSAL_DIR}/include" \
58
+ -output "${XCFRAMEWORK_PATH}"
59
+
60
+ echo "SimpleBLE macOS build complete!"
61
+ echo "XCFramework created at: ${XCFRAMEWORK_PATH}"