diff options
author | Sven Gothel <[email protected]> | 2021-09-03 01:39:27 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-09-03 01:39:27 +0200 |
commit | 897cf8fa26812f5587efc01a7223219fb0594dce (patch) | |
tree | 044fd8d55706fb78f34bd7f0f85513351fc5aaee | |
parent | e0e469b3bd3f4332488b75334b1326a0a452dce8 (diff) |
uuid_t: Add ctor(string) for all specialisations and add uuid_t::create(const string&) for convenience
-rw-r--r-- | api/direct_bt/UUID.hpp | 7 | ||||
-rw-r--r-- | src/direct_bt/UUID.cpp | 60 | ||||
-rw-r--r-- | test/direct_bt/test_uuid.cpp | 41 |
3 files changed, 106 insertions, 2 deletions
diff --git a/api/direct_bt/UUID.hpp b/api/direct_bt/UUID.hpp index 81b55e5e..9ab1f578 100644 --- a/api/direct_bt/UUID.hpp +++ b/api/direct_bt/UUID.hpp @@ -68,6 +68,7 @@ protected: public: static TypeSize toTypeSize(const jau::nsize_t size); static std::unique_ptr<const uuid_t> create(TypeSize const t, uint8_t const * const buffer, jau::nsize_t const byte_offset, bool const littleEndian); + static std::unique_ptr<const uuid_t> create(const std::string& str); virtual ~uuid_t() noexcept {} @@ -102,6 +103,8 @@ public: uuid16_t(uint16_t const v) noexcept : uuid_t(TypeSize::UUID16_SZ), value(v) { } + uuid16_t(const std::string& str); + uuid16_t(uint8_t const * const buffer, jau::nsize_t const byte_offset, bool const littleEndian) noexcept : uuid_t(TypeSize::UUID16_SZ), value(jau::get_uint16(buffer, byte_offset, littleEndian)) { } @@ -129,6 +132,8 @@ public: uuid32_t(uint32_t const v) noexcept : uuid_t(TypeSize::UUID32_SZ), value(v) {} + uuid32_t(const std::string& str); + uuid32_t(uint8_t const * const buffer, jau::nsize_t const byte_offset, bool const littleEndian) noexcept : uuid_t(TypeSize::UUID32_SZ), value(jau::get_uint32(buffer, byte_offset, littleEndian)) { } @@ -158,7 +163,7 @@ public: uuid128_t(jau::uint128_t const v) noexcept : uuid_t(TypeSize::UUID128_SZ), value(v) {} - uuid128_t(const std::string str); + uuid128_t(const std::string& str); uuid128_t(uint8_t const * const buffer, jau::nsize_t const byte_offset, bool const littleEndian) noexcept : uuid_t(TypeSize::UUID128_SZ), value(jau::get_uint128(buffer, byte_offset, littleEndian)) { } diff --git a/src/direct_bt/UUID.cpp b/src/direct_bt/UUID.cpp index 55e27397..d851b3ff 100644 --- a/src/direct_bt/UUID.cpp +++ b/src/direct_bt/UUID.cpp @@ -54,6 +54,23 @@ std::unique_ptr<const uuid_t> uuid_t::create(TypeSize t, uint8_t const * const b } throw jau::IllegalArgumentException("Unknown Type "+std::to_string(static_cast<jau::nsize_t>(t)), E_FILE_LINE); } +std::unique_ptr<const uuid_t> uuid_t::create(const std::string& str) { + const size_t len = str.length(); + switch( len ) { + case 4: // 16 + return std::unique_ptr<const uuid_t>(new uuid16_t(str)); + case 8: // 32 + return std::unique_ptr<const uuid_t>(new uuid32_t(str)); + case 36: // 128 + return std::unique_ptr<const uuid_t>(new uuid128_t(str)); + default: { + std::string msg("UUID string not of length 4, 8 or 36 but "); + msg.append(std::to_string(str.length())); + msg.append(": "+str); + throw jau::IllegalArgumentException(msg, E_FILE_LINE); + } + } +} uuid128_t uuid_t::toUUID128(uuid128_t const & base_uuid, jau::nsize_t const uuid32_le_octet_index) const noexcept { switch(type) { @@ -158,7 +175,48 @@ std::string uuid128_t::toString() const noexcept { return str; } -uuid128_t::uuid128_t(const std::string str) +uuid16_t::uuid16_t(const std::string& str) +: uuid_t(TypeSize::UUID16_SZ), value(0) +{ + uint16_t part0; + + if( 4 != str.length() ) { + std::string msg("UUID16 string not of length 4 but "); + msg.append(std::to_string(str.length())); + msg.append(": "+str); + throw jau::IllegalArgumentException(msg, E_FILE_LINE); + } + if ( sscanf(str.c_str(), "%04hx", &part0) != 1 ) { + std::string msg("UUID16 string not in format '0000' but "+str); + throw jau::IllegalArgumentException(msg, E_FILE_LINE); + } + // sscanf provided host data type, in which we store the values, + // hence no endian conversion + value = part0; +} + +uuid32_t::uuid32_t(const std::string& str) +: uuid_t(TypeSize::UUID32_SZ) +{ + uint32_t part0; + + if( 8 != str.length() ) { + std::string msg("UUID32 string not of length 8 but "); + msg.append(std::to_string(str.length())); + msg.append(": "+str); + throw jau::IllegalArgumentException(msg, E_FILE_LINE); + } + // if ( sscanf(str.c_str(), "%08x-%04hx-%04hx-%04hx-%08x%04hx", + if ( sscanf(str.c_str(), "%08x", &part0) != 1 ) { + std::string msg("UUID32 string not in format '00000000' but "+str); + throw jau::IllegalArgumentException(msg, E_FILE_LINE); + } + // sscanf provided host data type, in which we store the values, + // hence no endian conversion + value = part0; +} + +uuid128_t::uuid128_t(const std::string& str) : uuid_t(TypeSize::UUID128_SZ) { uint32_t part0, part4; diff --git a/test/direct_bt/test_uuid.cpp b/test/direct_bt/test_uuid.cpp index 44c43f4a..da1abafa 100644 --- a/test/direct_bt/test_uuid.cpp +++ b/test/direct_bt/test_uuid.cpp @@ -59,4 +59,45 @@ TEST_CASE( "UUID Test 01", "[datatype][uuid]" ) { REQUIRE( 0 == memcmp(v01.data(), v02->data(), 2) ); REQUIRE( v01.toString() == v02->toString() ); } + + + + { + const uuid16_t v01("1234"); + REQUIRE(v01.getTypeSizeInt() == uuid_t::number( uuid_t::TypeSize::UUID16_SZ )); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value)); + REQUIRE(0x1234 == v01.value); + REQUIRE("1234" == v01.toString()); + } + { + const uuid32_t v01("12345678"); + REQUIRE(v01.getTypeSizeInt() == uuid_t::number( uuid_t::TypeSize::UUID32_SZ )); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value)); + REQUIRE(0x12345678 == v01.value); + REQUIRE("12345678" == v01.toString()); + } + { + const uuid128_t v01("00001234-5678-100A-800B-00805F9B34FB"); + REQUIRE(v01.getTypeSizeInt() == uuid_t::number( uuid_t::TypeSize::UUID128_SZ) ); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value)); + REQUIRE("00001234-5678-100a-800b-00805f9b34fb" == v01.toString()); + } + + + + { + std::shared_ptr<const uuid_t> v01 = uuid_t::create("1234"); + REQUIRE(v01->getTypeSizeInt() == uuid_t::number( uuid_t::TypeSize::UUID16_SZ )); + REQUIRE("1234" == v01->toString()); + } + { + std::shared_ptr<const uuid_t> v01 = uuid_t::create("12345678"); + REQUIRE(v01->getTypeSizeInt() == uuid_t::number( uuid_t::TypeSize::UUID32_SZ)); + REQUIRE("12345678" == v01->toString()); + } + { + std::shared_ptr<const uuid_t> v01 = uuid_t::create("00001234-5678-100A-800B-00805F9B34FB"); + REQUIRE(v01->getTypeSizeInt() == uuid_t::number( uuid_t::TypeSize::UUID128_SZ )); + REQUIRE("00001234-5678-100a-800b-00805f9b34fb" == v01->toString()); + } } |