diff options
author | Sven Gothel <[email protected]> | 2023-10-28 12:19:18 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-10-28 12:19:18 +0200 |
commit | 36ef6950ccfa09538832880f2d3d1628cbb4e521 (patch) | |
tree | 1e477ffcf50ad09b3349b98d12ce47eb17a4990e | |
parent | be7bb35a3c1c908278bb732743a23c6d85a5f853 (diff) |
DBGattServer/Value: Add convenience make[_gatt]() for initializer list (byte array) with extra capacity and sizev3.2.2
-rw-r--r-- | api/direct_bt/DBGattServer.hpp | 11 | ||||
-rw-r--r-- | java/org/direct_bt/DBGattValue.java | 8 |
2 files changed, 19 insertions, 0 deletions
diff --git a/api/direct_bt/DBGattServer.hpp b/api/direct_bt/DBGattServer.hpp index d32de77a..d24f34df 100644 --- a/api/direct_bt/DBGattServer.hpp +++ b/api/direct_bt/DBGattServer.hpp @@ -535,6 +535,17 @@ namespace direct_bt { return jau::POctets(sourcelist, jau::endian::little); } + /** Convenience jau::POctets ctor function to create DBGattChar or DBGattDesc values. */ + inline jau::POctets make_gvalue(const jau::nsize_t capacity, const jau::nsize_t size, std::initializer_list<uint8_t> sourcelist) { + jau::POctets p(capacity, size, jau::endian::little); + p.bzero(); + const jau::nsize_t max_size = std::min(size, sourcelist.size()); + if( 0 < max_size ) { + std::memcpy(p.get_wptr(), sourcelist.begin(), max_size); + } + return p; + } + /** * Representing a Gatt Service object from the ::GATTRole::Server perspective. * diff --git a/java/org/direct_bt/DBGattValue.java b/java/org/direct_bt/DBGattValue.java index b695c5cd..070ee7b4 100644 --- a/java/org/direct_bt/DBGattValue.java +++ b/java/org/direct_bt/DBGattValue.java @@ -76,6 +76,14 @@ public class DBGattValue return new DBGattValue(p, p.length); } + /** Convenience {@link DBGattValue} ctor function. */ + public static DBGattValue make(final int capacity, final int size, final byte[] s) { + final byte[] p = new byte[size]; + final int max_size = Math.min(size, s.length); + System.arraycopy(s, 0, p, 0, max_size); + return new DBGattValue(p, capacity, true /* variable_length */); + } + /** * Constructor * @param value the data, which length defines the maximum writable fixed length if variable length is false. |