Skip to content

Commit

Permalink
Make StringToUUID constexpr (#33649)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jlatusek authored and pull[bot] committed Oct 17, 2024
1 parent cd29c0e commit 1068840
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 226 deletions.
8 changes: 4 additions & 4 deletions src/ble/BLEEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 8 additions & 22 deletions src/ble/BleLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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.
Expand Down
7 changes: 0 additions & 7 deletions src/ble/BleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 0 additions & 39 deletions src/ble/BleUUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(c >= '0' && c <= '9' ? c - '0' : tolower(c) - 'a' + 10);
}

bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo)
{
if ((idOne == nullptr) || (idTwo == nullptr))
Expand All @@ -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<uint8_t>(HexDigitToInt(*str) << 4);
else
byte = static_cast<uint8_t>(byte | HexDigitToInt(*str));

++nibbleId;
}

// All bytes were initialized?
return nibbleId == NUM_UUID_NIBBLES;
}

} /* namespace Ble */
} /* namespace chip */
70 changes: 67 additions & 3 deletions src/ble/BleUUID.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#error "Please include <ble/Ble.h> instead!"
#endif

#include <cstddef>
#include <cstdint>
#include <utility>

namespace chip {
namespace Ble {
Expand All @@ -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<uint8_t>(c - '0');
else
return static_cast<uint8_t>((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 <size_t N>
constexpr std::pair<bool, ChipBleUUID> 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<uint8_t>(HexDigitToInt(str[i]) << 4);
else
byte = static_cast<uint8_t>(byte | HexDigitToInt(str[i]));
++nibbleId;
}
return { nibbleId == NUM_UUID_NIBBLES, uuid };
}

#define StringToUUIDConstexpr(str) \
[]() { \
constexpr std::pair<bool, ::chip::Ble::ChipBleUUID> 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 */
Loading

0 comments on commit 1068840

Please sign in to comment.