From 10688406f16dadde4a496bc373572bda599ea2c7 Mon Sep 17 00:00:00 2001 From: Jakub Latusek Date: Thu, 30 May 2024 21:52:05 +0200 Subject: [PATCH] Make StringToUUID constexpr (#33649) * Implement StringToUUID as constexpr * Change local variable to use one global constexpr * restyle * Typo * Remove strings from tizen and linux platform * refactor * Fix review issue --- src/ble/BLEEndPoint.cpp | 8 +- src/ble/BleLayer.cpp | 30 ++--- src/ble/BleLayer.h | 7 -- src/ble/BleUUID.cpp | 39 ------- src/ble/BleUUID.h | 70 +++++++++++- src/ble/tests/TestBleLayer.cpp | 42 +++---- src/ble/tests/TestBleUUID.cpp | 33 +++--- src/platform/Linux/bluez/BluezConnection.cpp | 8 +- src/platform/Linux/bluez/BluezEndpoint.cpp | 8 +- .../Linux/bluez/ChipDeviceScanner.cpp | 2 +- src/platform/Linux/bluez/Types.h | 12 -- src/platform/Tizen/BLEManagerImpl.cpp | 107 +++++------------- src/platform/Tizen/ChipDeviceScanner.cpp | 9 +- 13 files changed, 149 insertions(+), 226 deletions(-) diff --git a/src/ble/BLEEndPoint.cpp b/src/ble/BLEEndPoint.cpp index 0e69e590df7f49..78e74bdec78b1a 100644 --- a/src/ble/BLEEndPoint.cpp +++ b/src/ble/BLEEndPoint.cpp @@ -389,7 +389,7 @@ void BLEEndPoint::FinalizeClose(uint8_t oldState, uint8_t flags, CHIP_ERROR err) // Indicate close of chipConnection to peripheral via GATT unsubscribe. Keep end point allocated until // unsubscribe completes or times out, so platform doesn't close underlying BLE connection before // we're really sure the unsubscribe request has been sent. - if (!mBle->mPlatformDelegate->UnsubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID)) + if (!mBle->mPlatformDelegate->UnsubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID)) { ChipLogError(Ble, "BtpEngine unsub failed"); @@ -750,7 +750,7 @@ CHIP_ERROR BLEEndPoint::HandleHandshakeConfirmationReceived() { // Subscribe to characteristic which peripheral will use to send indications. Prompts peripheral to send // BLE transport capabilities indication. - VerifyOrExit(mBle->mPlatformDelegate->SubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID), + VerifyOrExit(mBle->mPlatformDelegate->SubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID), err = BLE_ERROR_GATT_SUBSCRIBE_FAILED); // We just sent a GATT subscribe request, so make sure to attempt unsubscribe on close. @@ -1313,14 +1313,14 @@ bool BLEEndPoint::SendWrite(PacketBufferHandle && buf) { mConnStateFlags.Set(ConnectionStateFlag::kGattOperationInFlight); - return mBle->mPlatformDelegate->SendWriteRequest(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_1_ID, std::move(buf)); + return mBle->mPlatformDelegate->SendWriteRequest(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID, std::move(buf)); } bool BLEEndPoint::SendIndication(PacketBufferHandle && buf) { mConnStateFlags.Set(ConnectionStateFlag::kGattOperationInFlight); - return mBle->mPlatformDelegate->SendIndication(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID, std::move(buf)); + return mBle->mPlatformDelegate->SendIndication(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID, std::move(buf)); } CHIP_ERROR BLEEndPoint::StartConnectTimer() diff --git a/src/ble/BleLayer.cpp b/src/ble/BleLayer.cpp index de1f8458402efa..3e1473a4a1c2c2 100644 --- a/src/ble/BleLayer.cpp +++ b/src/ble/BleLayer.cpp @@ -138,20 +138,6 @@ class BleEndPointPool // static BleEndPointPool sBLEEndPointPool; -// UUIDs used internally by BleLayer: - -const ChipBleUUID BleLayer::CHIP_BLE_CHAR_1_ID = { { // 18EE2EF5-263D-4559-959F-4F9C429F9D11 - 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, - 0x9F, 0x9D, 0x11 } }; - -const ChipBleUUID BleLayer::CHIP_BLE_CHAR_2_ID = { { // 18EE2EF5-263D-4559-959F-4F9C429F9D12 - 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, - 0x9F, 0x9D, 0x12 } }; - -const ChipBleUUID BleLayer::CHIP_BLE_CHAR_3_ID = { { // 64630238-8772-45F2-B87D-748A83218F04 - 0x64, 0x63, 0x02, 0x38, 0x87, 0x72, 0x45, 0xF2, 0xB8, 0x7D, 0x74, 0x8A, 0x83, - 0x21, 0x8F, 0x04 } }; - // BleTransportCapabilitiesRequestMessage implementation: void BleTransportCapabilitiesRequestMessage::SetSupportedProtocolVersion(uint8_t index, uint8_t version) @@ -486,7 +472,7 @@ bool BleLayer::HandleWriteReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleU PacketBufferHandle && pBuf) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Write received on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_ID, charId), false, ChipLogError(Ble, "Write received on unknown char")); + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_UUID, charId), false, ChipLogError(Ble, "Write received on unknown char")); VerifyOrReturnError(!pBuf.IsNull(), false, ChipLogError(Ble, "Write received null buffer")); // Find matching connection end point. @@ -512,7 +498,7 @@ bool BleLayer::HandleIndicationReceived(BLE_CONNECTION_OBJECT connObj, const Chi PacketBufferHandle && pBuf) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Indication received on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId), false, ChipLogError(Ble, "Indication received on unknown char")); + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId), false, ChipLogError(Ble, "Indication received on unknown char")); VerifyOrReturnError(!pBuf.IsNull(), false, ChipLogError(Ble, "Indication received null buffer")); // Find matching connection end point. @@ -528,7 +514,7 @@ bool BleLayer::HandleIndicationReceived(BLE_CONNECTION_OBJECT connObj, const Chi bool BleLayer::HandleWriteConfirmation(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Write confirmation on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_ID, charId), false, ChipLogError(Ble, "Write confirmation on unknown char")); + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_UUID, charId), false, ChipLogError(Ble, "Write confirmation on unknown char")); HandleAckReceived(connObj); return true; @@ -537,7 +523,7 @@ bool BleLayer::HandleWriteConfirmation(BLE_CONNECTION_OBJECT connObj, const Chip bool BleLayer::HandleIndicationConfirmation(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Indication confirmation on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId), false, + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId), false, ChipLogError(Ble, "Indication confirmation on unknown char")); HandleAckReceived(connObj); @@ -558,7 +544,7 @@ void BleLayer::HandleAckReceived(BLE_CONNECTION_OBJECT connObj) bool BleLayer::HandleSubscribeReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Subscribe received on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false, + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false, ChipLogError(Ble, "Subscribe received on unknown char")); // Find end point already associated with BLE connection, if any. @@ -572,7 +558,7 @@ bool BleLayer::HandleSubscribeReceived(BLE_CONNECTION_OBJECT connObj, const Chip bool BleLayer::HandleSubscribeComplete(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Subscribe complete on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false, + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false, ChipLogError(Ble, "Subscribe complete on unknown char")); BLEEndPoint * endPoint = sBLEEndPointPool.Find(connObj); @@ -585,7 +571,7 @@ bool BleLayer::HandleSubscribeComplete(BLE_CONNECTION_OBJECT connObj, const Chip bool BleLayer::HandleUnsubscribeReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Unsubscribe received on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false, + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false, ChipLogError(Ble, "Unsubscribe received on unknown char")); // Find end point already associated with BLE connection, if any. @@ -599,7 +585,7 @@ bool BleLayer::HandleUnsubscribeReceived(BLE_CONNECTION_OBJECT connObj, const Ch bool BleLayer::HandleUnsubscribeComplete(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Unsubscribe complete on unknown svc")); - VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false, + VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false, ChipLogError(Ble, "Unsubscribe complete on unknown char")); // Find end point already associated with BLE connection, if any. diff --git a/src/ble/BleLayer.h b/src/ble/BleLayer.h index e7d619fe13420e..7056a09e5e14d6 100644 --- a/src/ble/BleLayer.h +++ b/src/ble/BleLayer.h @@ -313,13 +313,6 @@ class DLL_EXPORT BleLayer private: // Private data members: - // UUID of CHIP service characteristic used for central writes. - static const ChipBleUUID CHIP_BLE_CHAR_1_ID; - // UUID of CHIP service characteristic used for peripheral indications. - static const ChipBleUUID CHIP_BLE_CHAR_2_ID; - // UUID of CHIP service characteristic used for additional data - static const ChipBleUUID CHIP_BLE_CHAR_3_ID; - BleConnectionDelegate * mConnectionDelegate; BlePlatformDelegate * mPlatformDelegate; BleApplicationDelegate * mApplicationDelegate; diff --git a/src/ble/BleUUID.cpp b/src/ble/BleUUID.cpp index 098595fd721e5c..f42fc982e403d6 100644 --- a/src/ble/BleUUID.cpp +++ b/src/ble/BleUUID.cpp @@ -26,15 +26,6 @@ namespace chip { namespace Ble { -const ChipBleUUID CHIP_BLE_SVC_ID = { { // 0000FFF6-0000-1000-8000-00805F9B34FB - 0x00, 0x00, 0xFF, 0xF6, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, - 0xFB } }; - -inline static uint8_t HexDigitToInt(const char c) -{ - return static_cast(c >= '0' && c <= '9' ? c - '0' : tolower(c) - 'a' + 10); -} - bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo) { if ((idOne == nullptr) || (idTwo == nullptr)) @@ -44,35 +35,5 @@ bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo) return (memcmp(idOne->bytes, idTwo->bytes, 16) == 0); } -// Convert a string like "0000FFF6-0000-1000-8000-00805F9B34FB" to binary UUID -bool StringToUUID(const char * str, ChipBleUUID & uuid) -{ - constexpr size_t NUM_UUID_NIBBLES = sizeof(uuid.bytes) * 2; - size_t nibbleId = 0; - - for (; *str; ++str) - { - if (*str == '-') // skip separators - continue; - - if (!isxdigit(*str)) // invalid character! - return false; - - if (nibbleId >= NUM_UUID_NIBBLES) // too long string! - return false; - - uint8_t & byte = uuid.bytes[nibbleId / 2]; - if (nibbleId % 2 == 0) - byte = static_cast(HexDigitToInt(*str) << 4); - else - byte = static_cast(byte | HexDigitToInt(*str)); - - ++nibbleId; - } - - // All bytes were initialized? - return nibbleId == NUM_UUID_NIBBLES; -} - } /* namespace Ble */ } /* namespace chip */ diff --git a/src/ble/BleUUID.h b/src/ble/BleUUID.h index 315af9e8c76a32..6470cfe36e8494 100644 --- a/src/ble/BleUUID.h +++ b/src/ble/BleUUID.h @@ -22,7 +22,9 @@ #error "Please include instead!" #endif +#include #include +#include namespace chip { namespace Ble { @@ -36,11 +38,73 @@ struct ChipBleUUID uint8_t bytes[16]; }; -// UUID of CHIP BLE service. Exposed for use in scan filter. -extern const ChipBleUUID CHIP_BLE_SVC_ID; +constexpr bool isValidHexChar(char c) +{ + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} + +constexpr uint8_t HexDigitToInt(const char c) +{ + if (c >= '0' && c <= '9') + return static_cast(c - '0'); + else + return static_cast((c >= 'a' ? c - 'a' : c - 'A') + 10); +} bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo); -bool StringToUUID(const char * str, ChipBleUUID & uuid); + +/* + * StringToUUID converts a string representation of a UUID to a binary UUID. + * The string representation must be in the format "0000FFF6-0000-1000-8000-00805F9B34FB". + * The function returns a pair of a boolean indicating whether the conversion was successful + * and the binary UUID. + * + */ +template +constexpr std::pair StringToUUID(const char (&str)[N]) +{ + constexpr size_t UUID_LEN = 16; + constexpr size_t NUM_UUID_NIBBLES = UUID_LEN * 2; + static_assert(N >= NUM_UUID_NIBBLES); + ChipBleUUID uuid{}; + + size_t nibbleId = 0; + for (size_t i = 0; i < N - 1; ++i) + { + if (str[i] == '-') + continue; + if (!isValidHexChar(str[i])) + return { false, {} }; + if (nibbleId >= NUM_UUID_NIBBLES) + return { false, {} }; + uint8_t & byte = uuid.bytes[nibbleId / 2]; + if (nibbleId % 2 == 0) + byte = static_cast(HexDigitToInt(str[i]) << 4); + else + byte = static_cast(byte | HexDigitToInt(str[i])); + ++nibbleId; + } + return { nibbleId == NUM_UUID_NIBBLES, uuid }; +} + +#define StringToUUIDConstexpr(str) \ + []() { \ + constexpr std::pair res = ::chip::Ble::StringToUUID(str); \ + static_assert(res.first, "Argument: \"" #str "\" is not valid hex string"); \ + return res.second; \ + }(); + +// UUID of CHIP BLE service. +inline constexpr char CHIP_BLE_DESC_SHORT_UUID_STR[] = "2902"; +inline constexpr char CHIP_BLE_SERVICE_SHORT_UUID_STR[] = "FFF6"; +inline constexpr char CHIP_BLE_SERVICE_LONG_UUID_STR[] = "0000FFF6-0000-1000-8000-00805F9B34FB"; +inline constexpr char CHIP_BLE_CHAR_1_UUID_STR[] = "18EE2EF5-263D-4559-959F-4F9C429F9D11"; +inline constexpr char CHIP_BLE_CHAR_2_UUID_STR[] = "18EE2EF5-263D-4559-959F-4F9C429F9D12"; +inline constexpr char CHIP_BLE_CHAR_3_UUID_STR[] = "64630238-8772-45F2-B87D-748A83218F04"; +inline constexpr ChipBleUUID CHIP_BLE_SVC_ID = StringToUUIDConstexpr("0000FFF6-0000-1000-8000-00805F9B34FB"); +inline constexpr ChipBleUUID CHIP_BLE_CHAR_1_UUID = StringToUUIDConstexpr("18EE2EF5-263D-4559-959F-4F9C429F9D11"); +inline constexpr ChipBleUUID CHIP_BLE_CHAR_2_UUID = StringToUUIDConstexpr("18EE2EF5-263D-4559-959F-4F9C429F9D12"); +inline constexpr ChipBleUUID CHIP_BLE_CHAR_3_UUID = StringToUUIDConstexpr("64630238-8772-45F2-B87D-748A83218F04"); } /* namespace Ble */ } /* namespace chip */ diff --git a/src/ble/tests/TestBleLayer.cpp b/src/ble/tests/TestBleLayer.cpp index 9b79037acd0ea1..968293d8a72b8f 100644 --- a/src/ble/tests/TestBleLayer.cpp +++ b/src/ble/tests/TestBleLayer.cpp @@ -44,14 +44,6 @@ namespace Ble { namespace { constexpr ChipBleUUID uuidZero{}; -constexpr ChipBleUUID uuidSvc = { { 0x00, 0x00, 0xFF, 0xF6, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, - 0xFB } }; -constexpr ChipBleUUID uuidChar1 = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, - 0x11 } }; -constexpr ChipBleUUID uuidChar2 = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, - 0x12 } }; -constexpr ChipBleUUID uuidChar3 = { { 0x64, 0x63, 0x02, 0x38, 0x87, 0x72, 0x45, 0xF2, 0xB8, 0x7D, 0x74, 0x8A, 0x83, 0x21, 0x8F, - 0x04 } }; }; // namespace @@ -111,14 +103,14 @@ class TestBleLayer : public BleLayer, { constexpr uint8_t capReq[] = { 0x65, 0x6c, 0x54, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x06 }; auto buf = System::PacketBufferHandle::NewWithData(capReq, sizeof(capReq)); - return HandleWriteReceived(connObj, &uuidSvc, &uuidChar1, std::move(buf)); + return HandleWriteReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID, std::move(buf)); } // Processing subscription request after capabilities request should finalize // connection establishment. bool HandleSubscribeReceivedOnChar2(BLE_CONNECTION_OBJECT connObj) { - return HandleSubscribeReceived(connObj, &uuidSvc, &uuidChar2); + return HandleSubscribeReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID); } /// @@ -220,7 +212,7 @@ TEST_F(TestBleLayer, HandleSubscribeReceivedInvalidUUID) { auto connObj = GetConnectionObject(); EXPECT_FALSE(HandleSubscribeReceived(connObj, &uuidZero, &uuidZero)); - EXPECT_FALSE(HandleSubscribeReceived(connObj, &uuidSvc, &uuidChar1)); + EXPECT_FALSE(HandleSubscribeReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID)); } TEST_F(TestBleLayer, HandleSubscribeReceived) @@ -234,7 +226,7 @@ TEST_F(TestBleLayer, HandleSubscribeCompleteInvalidUUID) { auto connObj = GetConnectionObject(); EXPECT_FALSE(HandleSubscribeComplete(connObj, &uuidZero, &uuidZero)); - EXPECT_FALSE(HandleSubscribeComplete(connObj, &uuidSvc, &uuidChar1)); + EXPECT_FALSE(HandleSubscribeComplete(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID)); } TEST_F(TestBleLayer, HandleSubscribeComplete) @@ -243,14 +235,14 @@ TEST_F(TestBleLayer, HandleSubscribeComplete) ASSERT_TRUE(HandleWriteReceivedCapabilitiesRequest(connObj)); ASSERT_TRUE(HandleSubscribeReceivedOnChar2(connObj)); - EXPECT_TRUE(HandleSubscribeComplete(connObj, &uuidSvc, &uuidChar2)); + EXPECT_TRUE(HandleSubscribeComplete(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID)); } TEST_F(TestBleLayer, HandleUnsubscribeReceivedInvalidUUID) { auto connObj = GetConnectionObject(); EXPECT_FALSE(HandleUnsubscribeReceived(connObj, &uuidZero, &uuidZero)); - EXPECT_FALSE(HandleUnsubscribeReceived(connObj, &uuidSvc, &uuidChar1)); + EXPECT_FALSE(HandleUnsubscribeReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID)); } TEST_F(TestBleLayer, HandleUnsubscribeReceived) @@ -259,14 +251,14 @@ TEST_F(TestBleLayer, HandleUnsubscribeReceived) ASSERT_TRUE(HandleWriteReceivedCapabilitiesRequest(connObj)); ASSERT_TRUE(HandleSubscribeReceivedOnChar2(connObj)); - EXPECT_TRUE(HandleUnsubscribeReceived(connObj, &uuidSvc, &uuidChar2)); + EXPECT_TRUE(HandleUnsubscribeReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID)); } TEST_F(TestBleLayer, HandleUnsubscribeCompleteInvalidUUID) { auto connObj = GetConnectionObject(); EXPECT_FALSE(HandleUnsubscribeComplete(connObj, &uuidZero, &uuidZero)); - EXPECT_FALSE(HandleUnsubscribeComplete(connObj, &uuidSvc, &uuidChar1)); + EXPECT_FALSE(HandleUnsubscribeComplete(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID)); } TEST_F(TestBleLayer, HandleUnsubscribeComplete) @@ -275,7 +267,7 @@ TEST_F(TestBleLayer, HandleUnsubscribeComplete) ASSERT_TRUE(HandleWriteReceivedCapabilitiesRequest(connObj)); ASSERT_TRUE(HandleSubscribeReceivedOnChar2(connObj)); - EXPECT_TRUE(HandleUnsubscribeComplete(connObj, &uuidSvc, &uuidChar2)); + EXPECT_TRUE(HandleUnsubscribeComplete(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID)); } TEST_F(TestBleLayer, HandleWriteReceivedInvalidUUID) @@ -285,7 +277,7 @@ TEST_F(TestBleLayer, HandleWriteReceivedInvalidUUID) ASSERT_FALSE(buf.IsNull()); EXPECT_FALSE(HandleWriteReceived(connObj, &uuidZero, &uuidZero, buf.Retain())); - EXPECT_FALSE(HandleWriteReceived(connObj, &uuidSvc, &uuidChar3, std::move(buf))); + EXPECT_FALSE(HandleWriteReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_3_UUID, std::move(buf))); } TEST_F(TestBleLayer, HandleWriteReceived) @@ -300,14 +292,14 @@ TEST_F(TestBleLayer, HandleWriteReceived) auto buf = System::PacketBufferHandle::NewWithData(data, sizeof(data)); ASSERT_FALSE(buf.IsNull()); - EXPECT_TRUE(HandleWriteReceived(connObj, &uuidSvc, &uuidChar1, std::move(buf))); + EXPECT_TRUE(HandleWriteReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID, std::move(buf))); } TEST_F(TestBleLayer, HandleWriteConfirmationInvalidUUID) { auto connObj = GetConnectionObject(); EXPECT_FALSE(HandleWriteConfirmation(connObj, &uuidZero, &uuidZero)); - EXPECT_FALSE(HandleWriteConfirmation(connObj, &uuidSvc, &uuidChar2)); + EXPECT_FALSE(HandleWriteConfirmation(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID)); } TEST_F(TestBleLayer, HandleWriteConfirmationUninitialized) @@ -320,7 +312,7 @@ TEST_F(TestBleLayer, HandleWriteConfirmation) auto connObj = GetConnectionObject(); ASSERT_TRUE(HandleWriteReceivedCapabilitiesRequest(connObj)); - EXPECT_TRUE(HandleWriteConfirmation(connObj, &uuidSvc, &uuidChar1)); + EXPECT_TRUE(HandleWriteConfirmation(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID)); } TEST_F(TestBleLayer, HandleIndicationReceivedInvalidUUID) @@ -330,7 +322,7 @@ TEST_F(TestBleLayer, HandleIndicationReceivedInvalidUUID) ASSERT_FALSE(buf.IsNull()); EXPECT_FALSE(HandleIndicationReceived(connObj, &uuidZero, &uuidZero, buf.Retain())); - EXPECT_FALSE(HandleIndicationReceived(connObj, &uuidSvc, &uuidChar1, std::move(buf))); + EXPECT_FALSE(HandleIndicationReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID, std::move(buf))); } TEST_F(TestBleLayer, HandleIndicationReceived) @@ -345,14 +337,14 @@ TEST_F(TestBleLayer, HandleIndicationReceived) auto buf = System::PacketBufferHandle::NewWithData(data, sizeof(data)); ASSERT_FALSE(buf.IsNull()); - EXPECT_TRUE(HandleIndicationReceived(connObj, &uuidSvc, &uuidChar2, std::move(buf))); + EXPECT_TRUE(HandleIndicationReceived(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID, std::move(buf))); } TEST_F(TestBleLayer, HandleIndicationConfirmationInvalidUUID) { auto connObj = GetConnectionObject(); EXPECT_FALSE(HandleIndicationConfirmation(connObj, &uuidZero, &uuidZero)); - EXPECT_FALSE(HandleIndicationConfirmation(connObj, &uuidSvc, &uuidChar1)); + EXPECT_FALSE(HandleIndicationConfirmation(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID)); } TEST_F(TestBleLayer, HandleIndicationConfirmation) @@ -360,7 +352,7 @@ TEST_F(TestBleLayer, HandleIndicationConfirmation) auto connObj = GetConnectionObject(); ASSERT_TRUE(HandleWriteReceivedCapabilitiesRequest(connObj)); - EXPECT_TRUE(HandleIndicationConfirmation(connObj, &uuidSvc, &uuidChar2)); + EXPECT_TRUE(HandleIndicationConfirmation(connObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID)); } TEST_F(TestBleLayer, HandleConnectionError) diff --git a/src/ble/tests/TestBleUUID.cpp b/src/ble/tests/TestBleUUID.cpp index 4dbc6dd4c0fcdb..445535594fae4b 100644 --- a/src/ble/tests/TestBleUUID.cpp +++ b/src/ble/tests/TestBleUUID.cpp @@ -34,6 +34,9 @@ using namespace chip::Ble; namespace { +constexpr ChipBleUUID expectedUUID = { { 0x00, 0x00, 0xFF, 0xF6, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, + 0xFB } }; + TEST(TestBleUUID, CheckUUIDsMatch_NULL) { // Test that NULL pointer UUIDs are not equal @@ -43,46 +46,36 @@ TEST(TestBleUUID, CheckUUIDsMatch_NULL) TEST(TestBleUUID, CheckStringToUUID_ChipUUID) { // Test positive scenario - CHIP Service UUID - ChipBleUUID uuid; - EXPECT_TRUE(StringToUUID("0000FFF6-0000-1000-8000-00805F9B34FB", uuid)); - EXPECT_TRUE(UUIDsMatch(&uuid, &CHIP_BLE_SVC_ID)); + ChipBleUUID uuid = StringToUUIDConstexpr("0000FFF6-0000-1000-8000-00805F9B34FB"); + EXPECT_TRUE(UUIDsMatch(&uuid, &expectedUUID)); } TEST(TestBleUUID, CheckStringToUUID_ChipUUID_RandomCase) { // Test that letter case doesn't matter - ChipBleUUID uuid; - EXPECT_TRUE(StringToUUID("0000FfF6-0000-1000-8000-00805f9B34Fb", uuid)); - EXPECT_TRUE(UUIDsMatch(&uuid, &CHIP_BLE_SVC_ID)); + ChipBleUUID uuid = StringToUUIDConstexpr("0000FfF6-0000-1000-8000-00805f9B34Fb"); + EXPECT_TRUE(UUIDsMatch(&uuid, &expectedUUID)); } TEST(TestBleUUID, CheckStringToUUID_ChipUUID_NoSeparators) { // Test that separators don't matter - ChipBleUUID uuid; - EXPECT_TRUE(StringToUUID("0000FFF600001000800000805F9B34FB", uuid)); - EXPECT_TRUE(UUIDsMatch(&uuid, &CHIP_BLE_SVC_ID)); + ChipBleUUID uuid = StringToUUIDConstexpr("0000FFF600001000800000805F9B34FB"); + EXPECT_TRUE(UUIDsMatch(&uuid, &expectedUUID)); } TEST(TestBleUUID, CheckStringToUUID_TooLong) { // Test that even one more digit is too much - ChipBleUUID uuid; - EXPECT_FALSE(StringToUUID("0000FFF600001000800000805F9B34FB0", uuid)); -} - -TEST(TestBleUUID, CheckStringToUUID_TooShort) -{ - // Test that even one less digit is too little - ChipBleUUID uuid; - EXPECT_FALSE(StringToUUID("0000FFF600001000800000805F9B34F", uuid)); + auto result = StringToUUID("0000FFF600001000800000805F9B34FB0"); + EXPECT_FALSE(result.first); } TEST(TestBleUUID, CheckStringToUUID_InvalidChar) { // Test that non-hex digits don't pass - ChipBleUUID uuid; - EXPECT_FALSE(StringToUUID("0000GFF6-0000-1000-8000-00805F9B34FB0", uuid)); + auto result = StringToUUID("0000GFF6-0000-1000-8000-00805F9B34FB"); + EXPECT_FALSE(result.first); } } // namespace diff --git a/src/platform/Linux/bluez/BluezConnection.cpp b/src/platform/Linux/bluez/BluezConnection.cpp index eeb1e7fb8f1343..6ed15b5bb7cb45 100644 --- a/src/platform/Linux/bluez/BluezConnection.cpp +++ b/src/platform/Linux/bluez/BluezConnection.cpp @@ -94,7 +94,7 @@ CHIP_ERROR BluezConnection::Init(const BluezEndpoint & aEndpoint) if (service != nullptr) { if ((BluezIsServiceOnDevice(service, mDevice.get())) == TRUE && - (strcmp(bluez_gatt_service1_get_uuid(service), CHIP_BLE_UUID_SERVICE_STRING) == 0)) + (strcmp(bluez_gatt_service1_get_uuid(service), Ble::CHIP_BLE_SERVICE_LONG_UUID_STR) == 0)) { mService.reset(service); break; @@ -111,18 +111,18 @@ CHIP_ERROR BluezConnection::Init(const BluezEndpoint & aEndpoint) if (char1 != nullptr) { if ((BluezIsCharOnService(char1, mService.get()) == TRUE) && - (strcmp(bluez_gatt_characteristic1_get_uuid(char1), CHIP_PLAT_BLE_UUID_C1_STRING) == 0)) + (strcmp(bluez_gatt_characteristic1_get_uuid(char1), Ble::CHIP_BLE_CHAR_1_UUID_STR) == 0)) { mC1.reset(char1); } else if ((BluezIsCharOnService(char1, mService.get()) == TRUE) && - (strcmp(bluez_gatt_characteristic1_get_uuid(char1), CHIP_PLAT_BLE_UUID_C2_STRING) == 0)) + (strcmp(bluez_gatt_characteristic1_get_uuid(char1), Ble::CHIP_BLE_CHAR_2_UUID_STR) == 0)) { mC2.reset(char1); } #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING else if ((BluezIsCharOnService(char1, mService.get()) == TRUE) && - (strcmp(bluez_gatt_characteristic1_get_uuid(char1), CHIP_PLAT_BLE_UUID_C3_STRING) == 0)) + (strcmp(bluez_gatt_characteristic1_get_uuid(char1), Ble::CHIP_BLE_CHAR_3_UUID_STR) == 0)) { mC3.reset(char1); } diff --git a/src/platform/Linux/bluez/BluezEndpoint.cpp b/src/platform/Linux/bluez/BluezEndpoint.cpp index 151c09e09f662d..55b127e1b17014 100644 --- a/src/platform/Linux/bluez/BluezEndpoint.cpp +++ b/src/platform/Linux/bluez/BluezEndpoint.cpp @@ -438,10 +438,10 @@ void BluezEndpoint::SetupGattService() static const char * const c3_flags[] = { "read", nullptr }; #endif - mService.reset(CreateGattService(CHIP_BLE_UUID_SERVICE_SHORT_STRING)); + mService.reset(CreateGattService(Ble::CHIP_BLE_SERVICE_SHORT_UUID_STR)); // C1 characteristic - mC1.reset(CreateGattCharacteristic(mService.get(), "c1", CHIP_PLAT_BLE_UUID_C1_STRING, c1_flags)); + mC1.reset(CreateGattCharacteristic(mService.get(), "c1", Ble::CHIP_BLE_CHAR_1_UUID_STR, c1_flags)); g_signal_connect(mC1.get(), "handle-read-value", G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt, BluezEndpoint * self) { return self->BluezCharacteristicReadValue(aChar, aInv, aOpt); }), @@ -455,7 +455,7 @@ void BluezEndpoint::SetupGattService() g_signal_connect(mC1.get(), "handle-confirm", G_CALLBACK(BluezCharacteristicConfirmError), nullptr); // C2 characteristic - mC2.reset(CreateGattCharacteristic(mService.get(), "c2", CHIP_PLAT_BLE_UUID_C2_STRING, c2_flags)); + mC2.reset(CreateGattCharacteristic(mService.get(), "c2", Ble::CHIP_BLE_CHAR_2_UUID_STR, c2_flags)); g_signal_connect(mC2.get(), "handle-read-value", G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt, BluezEndpoint * self) { return self->BluezCharacteristicReadValue(aChar, aInv, aOpt); }), @@ -478,7 +478,7 @@ void BluezEndpoint::SetupGattService() #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING ChipLogDetail(DeviceLayer, "CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING is TRUE"); // Additional data characteristics - mC3.reset(CreateGattCharacteristic(mService.get(), "c3", CHIP_PLAT_BLE_UUID_C3_STRING, c3_flags)); + mC3.reset(CreateGattCharacteristic(mService.get(), "c3", Ble::CHIP_BLE_CHAR_3_UUID_STR, c3_flags)); g_signal_connect(mC3.get(), "handle-read-value", G_CALLBACK(+[](BluezGattCharacteristic1 * aChar, GDBusMethodInvocation * aInv, GVariant * aOpt, BluezEndpoint * self) { return self->BluezCharacteristicReadValue(aChar, aInv, aOpt); }), diff --git a/src/platform/Linux/bluez/ChipDeviceScanner.cpp b/src/platform/Linux/bluez/ChipDeviceScanner.cpp index dcb113b7f479c9..3323f1e7da57af 100644 --- a/src/platform/Linux/bluez/ChipDeviceScanner.cpp +++ b/src/platform/Linux/bluez/ChipDeviceScanner.cpp @@ -42,7 +42,7 @@ bool BluezGetChipDeviceInfo(BluezDevice1 & aDevice, chip::Ble::ChipBLEDeviceIden GVariant * serviceData = bluez_device1_get_service_data(&aDevice); VerifyOrReturnError(serviceData != nullptr, false); - GAutoPtr dataValue(g_variant_lookup_value(serviceData, CHIP_BLE_UUID_SERVICE_STRING, nullptr)); + GAutoPtr dataValue(g_variant_lookup_value(serviceData, Ble::CHIP_BLE_SERVICE_LONG_UUID_STR, nullptr)); VerifyOrReturnError(dataValue != nullptr, false); size_t dataLen = 0; diff --git a/src/platform/Linux/bluez/Types.h b/src/platform/Linux/bluez/Types.h index 95fd669efc1226..4bd2c89d9a7966 100644 --- a/src/platform/Linux/bluez/Types.h +++ b/src/platform/Linux/bluez/Types.h @@ -110,18 +110,6 @@ namespace Internal { #define ADVERTISING_INTERFACE BLUEZ_INTERFACE ".LEAdvertisement1" #define DEVICE_INTERFACE BLUEZ_INTERFACE ".Device1" -#define CHIP_PLAT_BLE_UUID_C1_STRING "18ee2ef5-263d-4559-959f-4f9c429f9d11" -#define CHIP_PLAT_BLE_UUID_C2_STRING "18ee2ef5-263d-4559-959f-4f9c429f9d12" -#define CHIP_PLAT_BLE_UUID_C3_STRING "64630238-8772-45F2-B87D-748A83218F04" - -#define CHIP_BLE_BASE_SERVICE_UUID_STRING "-0000-1000-8000-00805f9b34fb" -#define CHIP_BLE_SERVICE_PREFIX_LENGTH 8 -#define CHIP_BLE_BASE_SERVICE_PREFIX "0000" -#define CHIP_BLE_UUID_SERVICE_SHORT_STRING "fff6" - -#define CHIP_BLE_UUID_SERVICE_STRING \ - CHIP_BLE_BASE_SERVICE_PREFIX CHIP_BLE_UUID_SERVICE_SHORT_STRING CHIP_BLE_BASE_SERVICE_UUID_STRING - #define BLUEZ_ADV_TYPE_FLAGS 0x01 #define BLUEZ_ADV_TYPE_SERVICE_DATA 0x16 diff --git a/src/platform/Tizen/BLEManagerImpl.cpp b/src/platform/Tizen/BLEManagerImpl.cpp index a78efd9c944cce..944447d2e759df 100644 --- a/src/platform/Tizen/BLEManagerImpl.cpp +++ b/src/platform/Tizen/BLEManagerImpl.cpp @@ -74,14 +74,6 @@ namespace Internal { namespace { -/* CHIPoBLE UUID strings */ -constexpr char chip_ble_service_uuid[] = "0000FFF6-0000-1000-8000-00805F9B34FB"; -constexpr char chip_ble_char_c1_tx_uuid[] = "18EE2EF5-263D-4559-959F-4F9C429F9D11"; -constexpr char chip_ble_char_c2_rx_uuid[] = "18EE2EF5-263D-4559-959F-4F9C429F9D12"; - -constexpr char chip_ble_desc_uuid_short[] = "2902"; -constexpr char chip_ble_service_uuid_short[] = "FFF6"; - constexpr System::Clock::Timeout kNewConnectionScanTimeout = System::Clock::Seconds16(20); constexpr System::Clock::Timeout kConnectTimeout = System::Clock::Seconds16(20); constexpr System::Clock::Timeout kFastAdvertiseTimeout = @@ -582,12 +574,12 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer() VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_server_create() failed: %s", get_error_message(ret))); // Create Service (BTP Service) - ret = bt_gatt_service_create(chip_ble_service_uuid, BT_GATT_SERVICE_TYPE_PRIMARY, &service); + ret = bt_gatt_service_create(Ble::CHIP_BLE_SERVICE_LONG_UUID_STR, BT_GATT_SERVICE_TYPE_PRIMARY, &service); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_service_create() failed: %s", get_error_message(ret))); // Create 1st Characteristic (Client TX Buffer) ret = bt_gatt_characteristic_create( - chip_ble_char_c1_tx_uuid, BT_GATT_PERMISSION_WRITE, + Ble::CHIP_BLE_CHAR_1_UUID_STR, BT_GATT_PERMISSION_WRITE, BT_GATT_PROPERTY_WRITE, // Write Request is not coming if we use WITHOUT_RESPONSE property. Let's use WRITE property and // consider to use WITHOUT_RESPONSE property in the future according to the CHIP Spec 4.16.3.2. BTP // GATT Service @@ -611,7 +603,7 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer() ChipLogError(DeviceLayer, "bt_gatt_service_add_characteristic() failed: %s", get_error_message(ret))); // Create 2nd Characteristic (Client RX Buffer) - ret = bt_gatt_characteristic_create(chip_ble_char_c2_rx_uuid, BT_GATT_PERMISSION_READ, + ret = bt_gatt_characteristic_create(Ble::CHIP_BLE_CHAR_2_UUID_STR, BT_GATT_PERMISSION_READ, BT_GATT_PROPERTY_READ | BT_GATT_PROPERTY_INDICATE, "CHIPoBLE_C2", strlen("CHIPoBLE_C2"), &char2); VerifyOrExit(ret == BT_ERROR_NONE, @@ -637,8 +629,8 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer() get_error_message(ret))); // Create CCC Descriptor - ret = bt_gatt_descriptor_create(chip_ble_desc_uuid_short, BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE, desc_value, - sizeof(desc_value), &desc); + ret = bt_gatt_descriptor_create(Ble::CHIP_BLE_DESC_SHORT_UUID_STR, BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE, + desc_value, sizeof(desc_value), &desc); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_descriptor_create() failed: %s", get_error_message(ret))); ret = bt_gatt_characteristic_add_descriptor(char2, desc); VerifyOrExit(ret == BT_ERROR_NONE, @@ -711,7 +703,8 @@ CHIP_ERROR BLEManagerImpl::StartBLEAdvertising() VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "GetBLEDeviceIdentificationInfo() failed: %" CHIP_ERROR_FORMAT, err.Format())); - ret = bt_adapter_le_add_advertising_service_data(mAdvertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, chip_ble_service_uuid_short, + ret = bt_adapter_le_add_advertising_service_data(mAdvertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, + Ble::CHIP_BLE_SERVICE_SHORT_UUID_STR, reinterpret_cast(&deviceIdInfo), sizeof(deviceIdInfo)); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_add_advertising_service_data() failed: %s", get_error_message(ret))); @@ -779,12 +772,12 @@ static bool __GattClientForeachCharCb(int total, int index, bt_gatt_h charHandle VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from CHAR handle: %s", get_error_message(ret))); - if (strcasecmp(uuid.get(), chip_ble_char_c1_tx_uuid) == 0) + if (strcasecmp(uuid.get(), Ble::CHIP_BLE_CHAR_1_UUID_STR) == 0) { ChipLogProgress(DeviceLayer, "CHIP Char C1 TX Found [%s]", StringOrNullMarker(uuid.get())); conn->gattCharC1Handle = charHandle; } - else if (strcasecmp(uuid.get(), chip_ble_char_c2_rx_uuid) == 0) + else if (strcasecmp(uuid.get(), Ble::CHIP_BLE_CHAR_2_UUID_STR) == 0) { ChipLogProgress(DeviceLayer, "CHIP Char C2 RX Found [%s]", StringOrNullMarker(uuid.get())); conn->gattCharC2Handle = charHandle; @@ -806,7 +799,7 @@ static bool __GattClientForeachServiceCb(int total, int index, bt_gatt_h svcHand VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from SVC handle: %s", get_error_message(ret))); - if (strcasecmp(uuid.get(), chip_ble_service_uuid) == 0) + if (strcasecmp(uuid.get(), chip::Ble::CHIP_BLE_SERVICE_LONG_UUID_STR) == 0) { ChipLogProgress(DeviceLayer, "CHIP Service UUID Found [%s]", StringOrNullMarker(uuid.get())); @@ -1142,36 +1135,21 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv } break; case DeviceEventType::kPlatformTizenBLEWriteComplete: { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_write_uuid; - - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c1_tx_uuid, char_write_uuid); - - HandleWriteConfirmation(apEvent->Platform.BLEWriteComplete.mConnection, &service_uuid, &char_write_uuid); + HandleWriteConfirmation(apEvent->Platform.BLEWriteComplete.mConnection, &Ble::CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_1_UUID); break; } case DeviceEventType::kPlatformTizenBLESubscribeOpComplete: { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_notif_uuid; - - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notif_uuid); - if (apEvent->Platform.BLESubscribeOpComplete.mIsSubscribed) - HandleSubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &service_uuid, &char_notif_uuid); + HandleSubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &Ble::CHIP_BLE_SVC_ID, + &chip::Ble::CHIP_BLE_CHAR_2_UUID); else - HandleUnsubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &service_uuid, &char_notif_uuid); + HandleUnsubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &Ble::CHIP_BLE_SVC_ID, + &chip::Ble::CHIP_BLE_CHAR_2_UUID); break; } case DeviceEventType::kPlatformTizenBLEIndicationReceived: { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_notif_uuid; - - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notif_uuid); - - HandleIndicationReceived(apEvent->Platform.BLEIndicationReceived.mConnection, &service_uuid, &char_notif_uuid, + HandleIndicationReceived(apEvent->Platform.BLEIndicationReceived.mConnection, &Ble::CHIP_BLE_SVC_ID, + &chip::Ble::CHIP_BLE_CHAR_2_UUID, System::PacketBufferHandle::Adopt(apEvent->Platform.BLEIndicationReceived.mData)); break; } @@ -1182,43 +1160,29 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_notification_uuid; - Ble::ChipBleUUID char_write_uuid; - switch (event->Type) { case DeviceEventType::kCHIPoBLESubscribe: ChipLogProgress(DeviceLayer, "CHIPoBLESubscribe"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notification_uuid); - - HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &service_uuid, &char_notification_uuid); + HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &Ble::CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID); NotifyBLEConnectionEstablished(event->CHIPoBLESubscribe.ConId, CHIP_NO_ERROR); break; case DeviceEventType::kCHIPoBLEUnsubscribe: ChipLogProgress(DeviceLayer, "CHIPoBLEUnsubscribe"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notification_uuid); - - HandleUnsubscribeReceived(event->CHIPoBLESubscribe.ConId, &service_uuid, &char_notification_uuid); + HandleUnsubscribeReceived(event->CHIPoBLESubscribe.ConId, &Ble::CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID); break; case DeviceEventType::kCHIPoBLEWriteReceived: ChipLogProgress(DeviceLayer, "CHIPoBLEWriteReceived"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c1_tx_uuid, char_write_uuid); - HandleWriteReceived(event->CHIPoBLEWriteReceived.ConId, &service_uuid, &char_write_uuid, + HandleWriteReceived(event->CHIPoBLEWriteReceived.ConId, &Ble::CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_1_UUID, System::PacketBufferHandle::Adopt(event->CHIPoBLEWriteReceived.Data)); break; case DeviceEventType::kCHIPoBLEIndicateConfirm: ChipLogProgress(DeviceLayer, "CHIPoBLEIndicateConfirm"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notification_uuid); - HandleIndicationConfirmation(event->CHIPoBLEIndicateConfirm.ConId, &service_uuid, &char_notification_uuid); + HandleIndicationConfirmation(event->CHIPoBLEIndicateConfirm.ConId, &Ble::CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID); break; case DeviceEventType::kCHIPoBLEConnectionError: ChipLogProgress(DeviceLayer, "CHIPoBLEConnectionError"); @@ -1241,19 +1205,14 @@ uint16_t BLEManagerImpl::GetMTU(BLE_CONNECTION_OBJECT conId) const bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId) { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_notif_uuid; int ret; ChipLogProgress(DeviceLayer, "SubscribeCharacteristic"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notif_uuid); - VerifyOrExit(conId != nullptr, ChipLogError(DeviceLayer, "Invalid Connection")); - VerifyOrExit(Ble::UUIDsMatch(svcId, &service_uuid), + VerifyOrExit(Ble::UUIDsMatch(svcId, &Ble::CHIP_BLE_SVC_ID), ChipLogError(DeviceLayer, "SubscribeCharacteristic() called with invalid service ID")); - VerifyOrExit(Ble::UUIDsMatch(charId, &char_notif_uuid), + VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_2_UUID), ChipLogError(DeviceLayer, "SubscribeCharacteristic() called with invalid characteristic ID")); VerifyOrExit(conId->gattCharC2Handle != nullptr, ChipLogError(DeviceLayer, "Char C2 is null")); @@ -1274,19 +1233,14 @@ bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId) { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_notif_uuid; int ret; ChipLogProgress(DeviceLayer, "UnSubscribeCharacteristic"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c2_rx_uuid, char_notif_uuid); - VerifyOrExit(conId != nullptr, ChipLogError(DeviceLayer, "Invalid Connection")); - VerifyOrExit(Ble::UUIDsMatch(svcId, &service_uuid), + VerifyOrExit(Ble::UUIDsMatch(svcId, &Ble::CHIP_BLE_SVC_ID), ChipLogError(DeviceLayer, "UnSubscribeCharacteristic() called with invalid service ID")); - VerifyOrExit(Ble::UUIDsMatch(charId, &char_notif_uuid), + VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_2_UUID), ChipLogError(DeviceLayer, "UnSubscribeCharacteristic() called with invalid characteristic ID")); VerifyOrExit(conId->gattCharC2Handle != nullptr, ChipLogError(DeviceLayer, "Char C2 is null")); @@ -1360,19 +1314,14 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::Chip bool BLEManagerImpl::SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, System::PacketBufferHandle pBuf) { - Ble::ChipBleUUID service_uuid; - Ble::ChipBleUUID char_write_uuid; int ret; ChipLogProgress(DeviceLayer, "SendWriteRequest"); - StringToUUID(chip_ble_service_uuid, service_uuid); - StringToUUID(chip_ble_char_c1_tx_uuid, char_write_uuid); - VerifyOrExit(conId != nullptr, ChipLogError(DeviceLayer, "Invalid Connection")); - VerifyOrExit(Ble::UUIDsMatch(svcId, &service_uuid), + VerifyOrExit(Ble::UUIDsMatch(svcId, &Ble::CHIP_BLE_SVC_ID), ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid service ID")); - VerifyOrExit(Ble::UUIDsMatch(charId, &char_write_uuid), + VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_1_UUID), ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid characteristic ID")); VerifyOrExit(conId->gattCharC1Handle != nullptr, ChipLogError(DeviceLayer, "Char C1 is null")); @@ -1455,7 +1404,7 @@ void BLEManagerImpl::InitiateScan(BleScanState scanType) } /* Send StartChipScan Request to Scanner Class */ - strcpy(data.service_uuid, chip_ble_service_uuid_short); + strcpy(data.service_uuid, Ble::CHIP_BLE_DESC_SHORT_UUID_STR); err = mDeviceScanner->StartChipScan(kNewConnectionScanTimeout, ScanFilterType::kServiceData, data); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to start BLE scan")); diff --git a/src/platform/Tizen/ChipDeviceScanner.cpp b/src/platform/Tizen/ChipDeviceScanner.cpp index aea5aabd7d0799..2b022f537d79b9 100644 --- a/src/platform/Tizen/ChipDeviceScanner.cpp +++ b/src/platform/Tizen/ChipDeviceScanner.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -40,10 +41,6 @@ namespace chip { namespace DeviceLayer { namespace Internal { -// CHIPoBLE UUID strings -const char chip_service_uuid[] = "0000FFF6-0000-1000-8000-00805F9B34FB"; -const char chip_service_uuid_short[] = "FFF6"; - ChipDeviceScanner::ChipDeviceScanner(ChipDeviceScannerDelegate * delegate) : mDelegate(delegate) {} ChipDeviceScanner::~ChipDeviceScanner() @@ -100,8 +97,8 @@ static bool __IsChipThingDevice(bt_adapter_le_device_scan_result_info_s * info, { for (int i = 0; i < count; i++) { - if (g_strcmp0(dataList[i].service_uuid, chip_service_uuid) == 0 || - g_strcmp0(dataList[i].service_uuid, chip_service_uuid_short) == 0) + if (g_strcmp0(dataList[i].service_uuid, chip::Ble::CHIP_BLE_CHAR_1_UUID_STR) == 0 || + g_strcmp0(dataList[i].service_uuid, chip::Ble::CHIP_BLE_SERVICE_SHORT_UUID_STR) == 0) { ChipLogProgress(DeviceLayer, "CHIP Thing Device Found! [Service Data UUID] = %s", dataList[i].service_uuid); // Print full Service Data