summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/BLEUUID.cpp
blob: 9ca7cdd737d20f981230237d54d50dbe81b4c4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * BLEUUID.cpp
 *
 *  Created on: Jun 21, 2017
 *      Author: kolban
 */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <esp_log.h>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "BLEUUID.h"
static const char* LOG_TAG = "BLEUUID";
 
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
 
/**
 * @brief Copy memory from source to target but in reverse order.
 *
 * When we move memory from one location it is normally:
 *
 * ```
 * [0][1][2]...[n] -> [0][1][2]...[n]
 * ```
 *
 * with this function, it is:
 *
 * ```
 * [0][1][2]...[n] -> [n][n-1][n-2]...[0]
 * ```
 *
 * @param [in] target The target of the copy
 * @param [in] source The source of the copy
 * @param [in] size The number of bytes to copy
 */
static void memrcpy(uint8_t* target, uint8_t* source, uint32_t size) {
assert(size > 0);
target+=(size-1); // Point target to the last byte of the target data 
while (size > 0{
*target = *source;
target--;
source++;
size--;
}
} // memrcpy 
 
 
/**
 * @brief Create a UUID from a string.
 *
 * Create a UUID from a string.  There will be two possible stories here.  Either the string represents
 * a binary data field or the string represents a hex encoding of a UUID.
 * For the hex encoding, here is an example:
 *
 * ```
 * "beb5483e-36e1-4688-b7f5-ea07361b26a8"
 *  0 1 2 3  4 5  6 7  8 9  0 1 2 3 4 5
 *  12345678-90ab-cdef-1234-567890abcdef
 * ```
 *
 * This has a length of 36 characters.  We need to parse this into 16 bytes.
 *
 * @param [in] value The string to build a UUID from.
 */
BLEUUID::BLEUUID(std::string value) {
m_valueSet = true;
if (value.length() == 2{
m_uuid.len         = ESP_UUID_LEN_16;
m_uuid.uuid.uuid16 = value[0] | (value[1] << 8);
}
else if (value.length() == 4{
m_uuid.len         = ESP_UUID_LEN_32;
m_uuid.uuid.uuid32 = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
}
else if (value.length() == 16{
m_uuid.len = ESP_UUID_LEN_128;
memrcpy(m_uuid.uuid.uuid128, (uint8_t*)value.data(), 16);
}
else if (value.length() == 36{
// If the length of the string is 36 bytes then we will assume it is a long hex string in 
// UUID format. 
m_uuid.len = ESP_UUID_LEN_128;
int vals[16];
sscanf(value.c_str(), "%2x%2x%2x%2x-%2x%2x-%2x%2x-%2x%2x-%2x%2x%2x%2x%2x%2x",
&vals[15],
&vals[14],
&vals[13],
&vals[12],
&vals[11],
&vals[10],
&vals[9],
&vals[8],
&vals[7],
&vals[6],
&vals[5],
&vals[4],
&vals[3],
&vals[2],
&vals[1],
&vals[0]
);
 
int i;
for (i=0; i<16; i++) {
m_uuid.uuid.uuid128[i] = vals[i];
}
}
else {
ESP_LOGE(LOG_TAG, "ERROR: UUID value not 2, 4, 16 or 36 bytes");
m_valueSet = false;
}
} //BLEUUID(std::string) 
 
 
/**
 * @brief Create a UUID from 16 bytes of memory.
 *
 * @param [in] pData The pointer to the start of the UUID.
 * @param [in] size The size of the data.
 * @param [in] msbFirst Is the MSB first in pData memory?
 */
BLEUUID::BLEUUID(uint8_t* pData, size_t size, bool msbFirst) {
if (size != 16{
ESP_LOGE(LOG_TAG, "ERROR: UUID length not 16 bytes");
return;
}
m_uuid.len = ESP_UUID_LEN_128;
if (msbFirst) {
memrcpy(m_uuid.uuid.uuid128, pData, 16);
} else {
memcpy(m_uuid.uuid.uuid128, pData, 16);
}
m_valueSet         = true;
} // BLEUUID 
 
 
/**
 * @brief Create a UUID from the 16bit value.
 *
 * @param [in] uuid The 16bit short form UUID.
 */
BLEUUID::BLEUUID(uint16_t uuid) {
m_uuid.len         = ESP_UUID_LEN_16;
m_uuid.uuid.uuid16 = uuid;
m_valueSet         = true;
 
} // BLEUUID 
 
 
/**
 * @brief Create a UUID from the 32bit value.
 *
 * @param [in] uuid The 32bit short form UUID.
 */
BLEUUID::BLEUUID(uint32_t uuid) {
m_uuid.len         = ESP_UUID_LEN_32;
m_uuid.uuid.uuid32 = uuid;
m_valueSet         = true;
} // BLEUUID 
 
 
/**
 * @brief Create a UUID from the native UUID.
 *
 * @param [in] uuid The native UUID.
 */
BLEUUID::BLEUUID(esp_bt_uuid_t uuid) {
m_uuid     = uuid;
m_valueSet = true;
} // BLEUUID 
 
 
/**
 * @brief Create a UUID from the ESP32 esp_gat_id_t.
 *
 * @param [in] gattId The data to create the UUID from.
 */
BLEUUID::BLEUUID(esp_gatt_id_t gattId) : BLEUUID(gattId.uuid) {
} // BLEUUID 
 
 
BLEUUID::BLEUUID() {
m_valueSet = false;
} // BLEUUID 
 
 
/**
 * @brief Get the number of bits in this uuid.
 * @return The number of bits in the UUID.  One of 16, 32 or 128.
 */
int BLEUUID::bitSize() {
if (m_valueSet == false{
return 0;
}
switch(m_uuid.len{
case ESP_UUID_LEN_16: {
return 16;
}
case ESP_UUID_LEN_32: {
return 32;
}
case ESP_UUID_LEN_128: {
return 128;
}
default{
ESP_LOGE(LOG_TAG, "Unknown UUID length: %d", m_uuid.len);
return 0;
}
} // End of switch 
} // bitSize 
 
 
/**
 * @brief Compare a UUID against this UUID.
 *
 * @param [in] uuid The UUID to compare against.
 * @return True if the UUIDs are equal and false otherwise.
 */
bool BLEUUID::equals(BLEUUID uuid) {
//ESP_LOGD(TAG, "Comparing: %s to %s", toString().c_str(), uuid.toString().c_str()); 
if (m_valueSet == false || uuid.m_valueSet == false{
return false;
}
 
if (uuid.m_uuid.len != m_uuid.len{
return uuid.toString() == toString();
}
 
if (uuid.m_uuid.len == ESP_UUID_LEN_16) {
return uuid.m_uuid.uuid.uuid16 == m_uuid.uuid.uuid16;
}
 
if (uuid.m_uuid.len == ESP_UUID_LEN_32) {
return uuid.m_uuid.uuid.uuid32 == m_uuid.uuid.uuid32;
}
 
return memcmp(uuid.m_uuid.uuid.uuid128, m_uuid.uuid.uuid12816) == 0;
} // equals 
 
 
/**
 * Create a BLEUUID from a string of the form:
 * 0xNNNN
 * 0xNNNNNNNN
 * 0x<UUID>
 * NNNN
 * NNNNNNNN
 * <UUID>
 */
BLEUUID BLEUUID::fromString(std::string _uuid){
uint8_t start = 0;
if (strstr(_uuid.c_str(), "0x") != nullptr{ // If the string starts with 0x, skip those characters. 
start = 2;
}
uint8_t len = _uuid.length() - start; // Calculate the length of the string we are going to use. 
 
if( len == 4{
uint16_t x = strtoul(_uuid.substr(start, len).c_str(), NULL16);
return BLEUUID(x);
} else if (len == 8{
uint32_t x = strtoul(_uuid.substr(start, len).c_str(), NULL16);
return BLEUUID(x);
} else if (len == 36{
return BLEUUID(_uuid);
}
return BLEUUID();
} // fromString 
 
 
/**
 * @brief Get the native UUID value.
 *
 * @return The native UUID value or NULL if not set.
 */
esp_bt_uuid_t* BLEUUID::getNative() {
//ESP_LOGD(TAG, ">> getNative()") 
if (m_valueSet == false{
ESP_LOGD(LOG_TAG, "<< Return of un-initialized UUID!");
return nullptr;
}
//ESP_LOGD(TAG, "<< getNative()"); 
return &m_uuid;
} // getNative 
 
 
/**
 * @brief Convert a UUID to its 128 bit representation.
 *
 * A UUID can be internally represented as 16bit, 32bit or the full 128bit.  This method
 * will convert 16 or 32 bit representations to the full 128bit.
 */
BLEUUID BLEUUID::to128() {
//ESP_LOGD(LOG_TAG, ">> toFull() - %s", toString().c_str()); 
 
// If we either don't have a value or are already a 128 bit UUID, nothing further to do. 
if (m_valueSet == false || m_uuid.len == ESP_UUID_LEN_128) {
return *this;
}
 
// If we are 16 bit or 32 bit, then set the 4 bytes of the variable part of the UUID. 
if (m_uuid.len == ESP_UUID_LEN_16) {
uint16_t temp = m_uuid.uuid.uuid16;
m_uuid.uuid.uuid128[15] = 0;
m_uuid.uuid.uuid128[14] = 0;
m_uuid.uuid.uuid128[13] = (temp >> 8) & 0xff;
m_uuid.uuid.uuid128[12] = temp & 0xff;
 
}
else if (m_uuid.len == ESP_UUID_LEN_32) {
uint32_t temp = m_uuid.uuid.uuid32;
m_uuid.uuid.uuid128[15] = (temp >> 24) & 0xff;
m_uuid.uuid.uuid128[14] = (temp >> 16) & 0xff;
m_uuid.uuid.uuid128[13] = (temp >> 8) & 0xff;
m_uuid.uuid.uuid128[12] = temp & 0xff;
}
 
// Set the fixed parts of the UUID. 
m_uuid.uuid.uuid128[11] = 0x00;
m_uuid.uuid.uuid128[10] = 0x00;
 
m_uuid.uuid.uuid128[9]  = 0x10;
m_uuid.uuid.uuid128[8]  = 0x00;
 
m_uuid.uuid.uuid128[7]  = 0x80;
m_uuid.uuid.uuid128[6]  = 0x00;
 
m_uuid.uuid.uuid128[5]  = 0x00;
m_uuid.uuid.uuid128[4]  = 0x80;
m_uuid.uuid.uuid128[3]  = 0x5f;
m_uuid.uuid.uuid128[2]  = 0x9b;
m_uuid.uuid.uuid128[1]  = 0x34;
m_uuid.uuid.uuid128[0]  = 0xfb;
 
m_uuid.len = ESP_UUID_LEN_128;
//ESP_LOGD(TAG, "<< toFull <-  %s", toString().c_str()); 
return *this;
} // to128 
 
 
 
 
/**
 * @brief Get a string representation of the UUID.
 *
 * The format of a string is:
 * 01234567 8901 2345 6789 012345678901
 * 0000180d-0000-1000-8000-00805f9b34fb
 * 0 1 2 3  4 5  6 7  8 9  0 1 2 3 4 5
 *
 * @return A string representation of the UUID.
 */
std::string BLEUUID::toString() {
if (m_valueSet == false{   // If we have no value, nothing to format. 
return "<NULL>";
}
 
// If the UUIDs are 16 or 32 bit, pad correctly. 
std::stringstream ss;
 
if (m_uuid.len == ESP_UUID_LEN_16) {  // If the UUID is 16bit, pad correctly. 
ss << "0000" <<
std::hex <<
std::setfill('0') <<
std::setw(4) <<
m_uuid.uuid.uuid16 <<
"-0000-1000-8000-00805f9b34fb";
return ss.str();                    // Return the string 
} // End 16bit UUID 
 
if (m_uuid.len == ESP_UUID_LEN_32) {  // If the UUID is 32bit, pad correctly. 
ss << std::hex <<
std::setfill('0') <<
std::setw(8) <<
m_uuid.uuid.uuid32 <<
"-0000-1000-8000-00805f9b34fb";
return ss.str();                    // return the string 
} // End 32bit UUID 
 
// The UUID is not 16bit or 32bit which means that it is 128bit. 
// 
// UUID string format: 
// AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP 
// 
ss << std::hex << std::setfill('0') <<
std::setw(2) << (int)m_uuid.uuid.uuid128[15] <<
std::setw(2) << (int)m_uuid.uuid.uuid128[14] <<
std::setw(2) << (int)m_uuid.uuid.uuid128[13] <<
std::setw(2) << (int)m_uuid.uuid.uuid128[12] << "-" <<
std::setw(2) << (int)m_uuid.uuid.uuid128[11] <<
std::setw(2) << (int)m_uuid.uuid.uuid128[10] << "-" <<
std::setw(2) << (int)m_uuid.uuid.uuid128[9]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[8]  << "-" <<
std::setw(2) << (int)m_uuid.uuid.uuid128[7]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[6]  << "-" <<
std::setw(2) << (int)m_uuid.uuid.uuid128[5]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[4]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[3]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[2]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[1]  <<
std::setw(2) << (int)m_uuid.uuid.uuid128[0];
return ss.str();
} // toString 
 
#endif /* CONFIG_BT_ENABLED */