Skip to content

Commit 92f8cd0

Browse files
authoredMay 30, 2024
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
1 parent 1057daf commit 92f8cd0

13 files changed

+149
-226
lines changed
 

‎src/ble/BLEEndPoint.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ void BLEEndPoint::FinalizeClose(uint8_t oldState, uint8_t flags, CHIP_ERROR err)
389389
// Indicate close of chipConnection to peripheral via GATT unsubscribe. Keep end point allocated until
390390
// unsubscribe completes or times out, so platform doesn't close underlying BLE connection before
391391
// we're really sure the unsubscribe request has been sent.
392-
if (!mBle->mPlatformDelegate->UnsubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID))
392+
if (!mBle->mPlatformDelegate->UnsubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID))
393393
{
394394
ChipLogError(Ble, "BtpEngine unsub failed");
395395

@@ -750,7 +750,7 @@ CHIP_ERROR BLEEndPoint::HandleHandshakeConfirmationReceived()
750750
{
751751
// Subscribe to characteristic which peripheral will use to send indications. Prompts peripheral to send
752752
// BLE transport capabilities indication.
753-
VerifyOrExit(mBle->mPlatformDelegate->SubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID),
753+
VerifyOrExit(mBle->mPlatformDelegate->SubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID),
754754
err = BLE_ERROR_GATT_SUBSCRIBE_FAILED);
755755

756756
// We just sent a GATT subscribe request, so make sure to attempt unsubscribe on close.
@@ -1313,14 +1313,14 @@ bool BLEEndPoint::SendWrite(PacketBufferHandle && buf)
13131313
{
13141314
mConnStateFlags.Set(ConnectionStateFlag::kGattOperationInFlight);
13151315

1316-
return mBle->mPlatformDelegate->SendWriteRequest(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_1_ID, std::move(buf));
1316+
return mBle->mPlatformDelegate->SendWriteRequest(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_1_UUID, std::move(buf));
13171317
}
13181318

13191319
bool BLEEndPoint::SendIndication(PacketBufferHandle && buf)
13201320
{
13211321
mConnStateFlags.Set(ConnectionStateFlag::kGattOperationInFlight);
13221322

1323-
return mBle->mPlatformDelegate->SendIndication(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID, std::move(buf));
1323+
return mBle->mPlatformDelegate->SendIndication(mConnObj, &CHIP_BLE_SVC_ID, &CHIP_BLE_CHAR_2_UUID, std::move(buf));
13241324
}
13251325

13261326
CHIP_ERROR BLEEndPoint::StartConnectTimer()

‎src/ble/BleLayer.cpp

+8-22
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,6 @@ class BleEndPointPool
138138
//
139139
static BleEndPointPool sBLEEndPointPool;
140140

141-
// UUIDs used internally by BleLayer:
142-
143-
const ChipBleUUID BleLayer::CHIP_BLE_CHAR_1_ID = { { // 18EE2EF5-263D-4559-959F-4F9C429F9D11
144-
0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42,
145-
0x9F, 0x9D, 0x11 } };
146-
147-
const ChipBleUUID BleLayer::CHIP_BLE_CHAR_2_ID = { { // 18EE2EF5-263D-4559-959F-4F9C429F9D12
148-
0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42,
149-
0x9F, 0x9D, 0x12 } };
150-
151-
const ChipBleUUID BleLayer::CHIP_BLE_CHAR_3_ID = { { // 64630238-8772-45F2-B87D-748A83218F04
152-
0x64, 0x63, 0x02, 0x38, 0x87, 0x72, 0x45, 0xF2, 0xB8, 0x7D, 0x74, 0x8A, 0x83,
153-
0x21, 0x8F, 0x04 } };
154-
155141
// BleTransportCapabilitiesRequestMessage implementation:
156142

157143
void BleTransportCapabilitiesRequestMessage::SetSupportedProtocolVersion(uint8_t index, uint8_t version)
@@ -486,7 +472,7 @@ bool BleLayer::HandleWriteReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleU
486472
PacketBufferHandle && pBuf)
487473
{
488474
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Write received on unknown svc"));
489-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_ID, charId), false, ChipLogError(Ble, "Write received on unknown char"));
475+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_UUID, charId), false, ChipLogError(Ble, "Write received on unknown char"));
490476
VerifyOrReturnError(!pBuf.IsNull(), false, ChipLogError(Ble, "Write received null buffer"));
491477

492478
// Find matching connection end point.
@@ -512,7 +498,7 @@ bool BleLayer::HandleIndicationReceived(BLE_CONNECTION_OBJECT connObj, const Chi
512498
PacketBufferHandle && pBuf)
513499
{
514500
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Indication received on unknown svc"));
515-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId), false, ChipLogError(Ble, "Indication received on unknown char"));
501+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId), false, ChipLogError(Ble, "Indication received on unknown char"));
516502
VerifyOrReturnError(!pBuf.IsNull(), false, ChipLogError(Ble, "Indication received null buffer"));
517503

518504
// Find matching connection end point.
@@ -528,7 +514,7 @@ bool BleLayer::HandleIndicationReceived(BLE_CONNECTION_OBJECT connObj, const Chi
528514
bool BleLayer::HandleWriteConfirmation(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
529515
{
530516
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Write confirmation on unknown svc"));
531-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_ID, charId), false, ChipLogError(Ble, "Write confirmation on unknown char"));
517+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_1_UUID, charId), false, ChipLogError(Ble, "Write confirmation on unknown char"));
532518

533519
HandleAckReceived(connObj);
534520
return true;
@@ -537,7 +523,7 @@ bool BleLayer::HandleWriteConfirmation(BLE_CONNECTION_OBJECT connObj, const Chip
537523
bool BleLayer::HandleIndicationConfirmation(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
538524
{
539525
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Indication confirmation on unknown svc"));
540-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId), false,
526+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId), false,
541527
ChipLogError(Ble, "Indication confirmation on unknown char"));
542528

543529
HandleAckReceived(connObj);
@@ -558,7 +544,7 @@ void BleLayer::HandleAckReceived(BLE_CONNECTION_OBJECT connObj)
558544
bool BleLayer::HandleSubscribeReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
559545
{
560546
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Subscribe received on unknown svc"));
561-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false,
547+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false,
562548
ChipLogError(Ble, "Subscribe received on unknown char"));
563549

564550
// Find end point already associated with BLE connection, if any.
@@ -572,7 +558,7 @@ bool BleLayer::HandleSubscribeReceived(BLE_CONNECTION_OBJECT connObj, const Chip
572558
bool BleLayer::HandleSubscribeComplete(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
573559
{
574560
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Subscribe complete on unknown svc"));
575-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false,
561+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false,
576562
ChipLogError(Ble, "Subscribe complete on unknown char"));
577563

578564
BLEEndPoint * endPoint = sBLEEndPointPool.Find(connObj);
@@ -585,7 +571,7 @@ bool BleLayer::HandleSubscribeComplete(BLE_CONNECTION_OBJECT connObj, const Chip
585571
bool BleLayer::HandleUnsubscribeReceived(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
586572
{
587573
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Unsubscribe received on unknown svc"));
588-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false,
574+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false,
589575
ChipLogError(Ble, "Unsubscribe received on unknown char"));
590576

591577
// Find end point already associated with BLE connection, if any.
@@ -599,7 +585,7 @@ bool BleLayer::HandleUnsubscribeReceived(BLE_CONNECTION_OBJECT connObj, const Ch
599585
bool BleLayer::HandleUnsubscribeComplete(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId)
600586
{
601587
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_SVC_ID, svcId), false, ChipLogError(Ble, "Unsubscribe complete on unknown svc"));
602-
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_ID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_ID, charId), false,
588+
VerifyOrReturnError(UUIDsMatch(&CHIP_BLE_CHAR_2_UUID, charId) || UUIDsMatch(&CHIP_BLE_CHAR_3_UUID, charId), false,
603589
ChipLogError(Ble, "Unsubscribe complete on unknown char"));
604590

605591
// Find end point already associated with BLE connection, if any.

‎src/ble/BleLayer.h

-7
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,6 @@ class DLL_EXPORT BleLayer
313313
private:
314314
// Private data members:
315315

316-
// UUID of CHIP service characteristic used for central writes.
317-
static const ChipBleUUID CHIP_BLE_CHAR_1_ID;
318-
// UUID of CHIP service characteristic used for peripheral indications.
319-
static const ChipBleUUID CHIP_BLE_CHAR_2_ID;
320-
// UUID of CHIP service characteristic used for additional data
321-
static const ChipBleUUID CHIP_BLE_CHAR_3_ID;
322-
323316
BleConnectionDelegate * mConnectionDelegate;
324317
BlePlatformDelegate * mPlatformDelegate;
325318
BleApplicationDelegate * mApplicationDelegate;

‎src/ble/BleUUID.cpp

-39
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@
2626
namespace chip {
2727
namespace Ble {
2828

29-
const ChipBleUUID CHIP_BLE_SVC_ID = { { // 0000FFF6-0000-1000-8000-00805F9B34FB
30-
0x00, 0x00, 0xFF, 0xF6, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34,
31-
0xFB } };
32-
33-
inline static uint8_t HexDigitToInt(const char c)
34-
{
35-
return static_cast<uint8_t>(c >= '0' && c <= '9' ? c - '0' : tolower(c) - 'a' + 10);
36-
}
37-
3829
bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo)
3930
{
4031
if ((idOne == nullptr) || (idTwo == nullptr))
@@ -44,35 +35,5 @@ bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo)
4435
return (memcmp(idOne->bytes, idTwo->bytes, 16) == 0);
4536
}
4637

47-
// Convert a string like "0000FFF6-0000-1000-8000-00805F9B34FB" to binary UUID
48-
bool StringToUUID(const char * str, ChipBleUUID & uuid)
49-
{
50-
constexpr size_t NUM_UUID_NIBBLES = sizeof(uuid.bytes) * 2;
51-
size_t nibbleId = 0;
52-
53-
for (; *str; ++str)
54-
{
55-
if (*str == '-') // skip separators
56-
continue;
57-
58-
if (!isxdigit(*str)) // invalid character!
59-
return false;
60-
61-
if (nibbleId >= NUM_UUID_NIBBLES) // too long string!
62-
return false;
63-
64-
uint8_t & byte = uuid.bytes[nibbleId / 2];
65-
if (nibbleId % 2 == 0)
66-
byte = static_cast<uint8_t>(HexDigitToInt(*str) << 4);
67-
else
68-
byte = static_cast<uint8_t>(byte | HexDigitToInt(*str));
69-
70-
++nibbleId;
71-
}
72-
73-
// All bytes were initialized?
74-
return nibbleId == NUM_UUID_NIBBLES;
75-
}
76-
7738
} /* namespace Ble */
7839
} /* namespace chip */

‎src/ble/BleUUID.h

+67-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#error "Please include <ble/Ble.h> instead!"
2323
#endif
2424

25+
#include <cstddef>
2526
#include <cstdint>
27+
#include <utility>
2628

2729
namespace chip {
2830
namespace Ble {
@@ -36,11 +38,73 @@ struct ChipBleUUID
3638
uint8_t bytes[16];
3739
};
3840

39-
// UUID of CHIP BLE service. Exposed for use in scan filter.
40-
extern const ChipBleUUID CHIP_BLE_SVC_ID;
41+
constexpr bool isValidHexChar(char c)
42+
{
43+
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
44+
}
45+
46+
constexpr uint8_t HexDigitToInt(const char c)
47+
{
48+
if (c >= '0' && c <= '9')
49+
return static_cast<uint8_t>(c - '0');
50+
else
51+
return static_cast<uint8_t>((c >= 'a' ? c - 'a' : c - 'A') + 10);
52+
}
4153

4254
bool UUIDsMatch(const ChipBleUUID * idOne, const ChipBleUUID * idTwo);
43-
bool StringToUUID(const char * str, ChipBleUUID & uuid);
55+
56+
/*
57+
* StringToUUID converts a string representation of a UUID to a binary UUID.
58+
* The string representation must be in the format "0000FFF6-0000-1000-8000-00805F9B34FB".
59+
* The function returns a pair of a boolean indicating whether the conversion was successful
60+
* and the binary UUID.
61+
*
62+
*/
63+
template <size_t N>
64+
constexpr std::pair<bool, ChipBleUUID> StringToUUID(const char (&str)[N])
65+
{
66+
constexpr size_t UUID_LEN = 16;
67+
constexpr size_t NUM_UUID_NIBBLES = UUID_LEN * 2;
68+
static_assert(N >= NUM_UUID_NIBBLES);
69+
ChipBleUUID uuid{};
70+
71+
size_t nibbleId = 0;
72+
for (size_t i = 0; i < N - 1; ++i)
73+
{
74+
if (str[i] == '-')
75+
continue;
76+
if (!isValidHexChar(str[i]))
77+
return { false, {} };
78+
if (nibbleId >= NUM_UUID_NIBBLES)
79+
return { false, {} };
80+
uint8_t & byte = uuid.bytes[nibbleId / 2];
81+
if (nibbleId % 2 == 0)
82+
byte = static_cast<uint8_t>(HexDigitToInt(str[i]) << 4);
83+
else
84+
byte = static_cast<uint8_t>(byte | HexDigitToInt(str[i]));
85+
++nibbleId;
86+
}
87+
return { nibbleId == NUM_UUID_NIBBLES, uuid };
88+
}
89+
90+
#define StringToUUIDConstexpr(str) \
91+
[]() { \
92+
constexpr std::pair<bool, ::chip::Ble::ChipBleUUID> res = ::chip::Ble::StringToUUID(str); \
93+
static_assert(res.first, "Argument: \"" #str "\" is not valid hex string"); \
94+
return res.second; \
95+
}();
96+
97+
// UUID of CHIP BLE service.
98+
inline constexpr char CHIP_BLE_DESC_SHORT_UUID_STR[] = "2902";
99+
inline constexpr char CHIP_BLE_SERVICE_SHORT_UUID_STR[] = "FFF6";
100+
inline constexpr char CHIP_BLE_SERVICE_LONG_UUID_STR[] = "0000FFF6-0000-1000-8000-00805F9B34FB";
101+
inline constexpr char CHIP_BLE_CHAR_1_UUID_STR[] = "18EE2EF5-263D-4559-959F-4F9C429F9D11";
102+
inline constexpr char CHIP_BLE_CHAR_2_UUID_STR[] = "18EE2EF5-263D-4559-959F-4F9C429F9D12";
103+
inline constexpr char CHIP_BLE_CHAR_3_UUID_STR[] = "64630238-8772-45F2-B87D-748A83218F04";
104+
inline constexpr ChipBleUUID CHIP_BLE_SVC_ID = StringToUUIDConstexpr("0000FFF6-0000-1000-8000-00805F9B34FB");
105+
inline constexpr ChipBleUUID CHIP_BLE_CHAR_1_UUID = StringToUUIDConstexpr("18EE2EF5-263D-4559-959F-4F9C429F9D11");
106+
inline constexpr ChipBleUUID CHIP_BLE_CHAR_2_UUID = StringToUUIDConstexpr("18EE2EF5-263D-4559-959F-4F9C429F9D12");
107+
inline constexpr ChipBleUUID CHIP_BLE_CHAR_3_UUID = StringToUUIDConstexpr("64630238-8772-45F2-B87D-748A83218F04");
44108

45109
} /* namespace Ble */
46110
} /* namespace chip */

0 commit comments

Comments
 (0)
Please sign in to comment.