From d22db6ef8c6aaeacaa500b388e7ad2d642553a61 Mon Sep 17 00:00:00 2001 From: h2zero Date: Thu, 1 Aug 2024 15:46:05 -0600 Subject: [PATCH] Fix 16 and 32 bit UUID comparison. --- src/NimBLEUUID.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/NimBLEUUID.cpp b/src/NimBLEUUID.cpp index 3226dba..d5d400e 100644 --- a/src/NimBLEUUID.cpp +++ b/src/NimBLEUUID.cpp @@ -272,6 +272,10 @@ const NimBLEUUID& NimBLEUUID::reverseByteOrder() { * @brief Convenience operator to check if this UUID is equal to another. */ bool NimBLEUUID::operator==(const NimBLEUUID& rhs) const { + if (!this->bitSize() || !rhs.bitSize()) { + return false; + } + if (this->bitSize() != rhs.bitSize()) { uint8_t uuid128[sizeof(ble_base_uuid)]; memcpy(uuid128, &ble_base_uuid, sizeof(ble_base_uuid)); @@ -287,11 +291,19 @@ bool NimBLEUUID::operator==(const NimBLEUUID& rhs) const { } } - if (this->bitSize() != BLE_UUID_TYPE_128) { - return this->getValue() == rhs.getValue(); + if (this->bitSize() == BLE_UUID_TYPE_16) { + return *reinterpret_cast(this->getValue()) == *reinterpret_cast(rhs.getValue()); } - return memcmp(this->getValue(), rhs.getValue(), 16) == 0; + if (this->bitSize() == BLE_UUID_TYPE_32) { + return *reinterpret_cast(this->getValue()) == *reinterpret_cast(rhs.getValue()); + } + + if (this->bitSize() == BLE_UUID_TYPE_128) { + return memcmp(this->getValue(), rhs.getValue(), 16) == 0; + } + + return false; } // operator== /**