Skip to content

Commit 68411ff

Browse files
committed
Fix issue #618
1 parent a4e3d43 commit 68411ff

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

cpp_utils/BLEUUID.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,46 +70,56 @@ static void memrcpy(uint8_t* target, uint8_t* source, uint32_t size) {
7070
*/
7171
BLEUUID::BLEUUID(std::string value) {
7272
m_valueSet = true;
73-
if (value.length() == 2) {
73+
if (value.length() == 4) {
7474
m_uuid.len = ESP_UUID_LEN_16;
75-
m_uuid.uuid.uuid16 = value[0] | (value[1] << 8);
75+
m_uuid.uuid.uuid16 = 0;
76+
for(int i=0;i<value.length();){
77+
uint8_t MSB = value.c_str()[i];
78+
uint8_t LSB = value.c_str()[i+1];
79+
80+
if(MSB > '9') MSB -= 7;
81+
if(LSB > '9') LSB -= 7;
82+
m_uuid.uuid.uuid16 += (((MSB&0x0F) <<4) | (LSB & 0x0F))<<(2-i)*4;
83+
i+=2;
84+
}
7685
}
77-
else if (value.length() == 4) {
86+
else if (value.length() == 8) {
7887
m_uuid.len = ESP_UUID_LEN_32;
79-
m_uuid.uuid.uuid32 = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
88+
m_uuid.uuid.uuid32 = 0;
89+
for(int i=0;i<value.length();){
90+
uint8_t MSB = value.c_str()[i];
91+
uint8_t LSB = value.c_str()[i+1];
92+
93+
if(MSB > '9') MSB -= 7;
94+
if(LSB > '9') LSB -= 7;
95+
m_uuid.uuid.uuid32 += (((MSB&0x0F) <<4) | (LSB & 0x0F))<<(6-i)*4;
96+
i+=2;
97+
}
98+
}
99+
}
80100
}
81-
else if (value.length() == 16) {
101+
else if (value.length() == 16) { // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be investigated (lack of time)
82102
m_uuid.len = ESP_UUID_LEN_128;
83-
memrcpy(m_uuid.uuid.uuid128, (uint8_t*) value.data(), 16);
103+
memrcpy(m_uuid.uuid.uuid128, (uint8_t*)value.data(), 16);
84104
}
85105
else if (value.length() == 36) {
86106
// If the length of the string is 36 bytes then we will assume it is a long hex string in
87107
// UUID format.
88108
m_uuid.len = ESP_UUID_LEN_128;
89-
int vals[16];
90-
sscanf(value.c_str(), "%2x%2x%2x%2x-%2x%2x-%2x%2x-%2x%2x-%2x%2x%2x%2x%2x%2x",
91-
&vals[15],
92-
&vals[14],
93-
&vals[13],
94-
&vals[12],
95-
&vals[11],
96-
&vals[10],
97-
&vals[9],
98-
&vals[8],
99-
&vals[7],
100-
&vals[6],
101-
&vals[5],
102-
&vals[4],
103-
&vals[3],
104-
&vals[2],
105-
&vals[1],
106-
&vals[0]
107-
);
108-
109-
for (int i = 0; i < 16; i++) {
110-
m_uuid.uuid.uuid128[i] = vals[i];
109+
int n = 0;
110+
for(int i=0;i<value.length();){
111+
if(value.c_str()[i] == '-')
112+
i++;
113+
uint8_t MSB = value.c_str()[i];
114+
uint8_t LSB = value.c_str()[i+1];
115+
116+
if(MSB > '9') MSB -= 7;
117+
if(LSB > '9') LSB -= 7;
118+
m_uuid.uuid.uuid128[15-n++] = ((MSB&0x0F) <<4) | (LSB & 0x0F);
119+
i+=2;
111120
}
112-
} else {
121+
}
122+
else {
113123
ESP_LOGE(LOG_TAG, "ERROR: UUID value not 2, 4, 16 or 36 bytes");
114124
m_valueSet = false;
115125
}
@@ -249,7 +259,7 @@ BLEUUID BLEUUID::fromString(std::string _uuid) {
249259
}
250260
uint8_t len = _uuid.length() - start; // Calculate the length of the string we are going to use.
251261

252-
if (len == 4) {
262+
if(len == 4) {
253263
uint16_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16);
254264
return BLEUUID(x);
255265
} else if (len == 8) {
@@ -269,7 +279,7 @@ BLEUUID BLEUUID::fromString(std::string _uuid) {
269279
*/
270280
esp_bt_uuid_t* BLEUUID::getNative() {
271281
//ESP_LOGD(TAG, ">> getNative()")
272-
if (!m_valueSet) {
282+
if (m_valueSet == false) {
273283
ESP_LOGD(LOG_TAG, "<< Return of un-initialized UUID!");
274284
return nullptr;
275285
}

cpp_utils/BLEUUID.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class BLEUUID {
3232
static BLEUUID fromString(std::string uuid); // Create a BLEUUID from a string
3333

3434
private:
35-
esp_bt_uuid_t m_uuid; // The underlying UUID structure that this class wraps.
36-
bool m_valueSet; // Is there a value set for this instance.
35+
esp_bt_uuid_t m_uuid; // The underlying UUID structure that this class wraps.
36+
bool m_valueSet = false; // Is there a value set for this instance.
3737
}; // BLEUUID
3838
#endif /* CONFIG_BT_ENABLED */
3939
#endif /* COMPONENTS_CPP_UTILS_BLEUUID_H_ */

0 commit comments

Comments
 (0)