ESPHome  2024.5.4
ble_uuid.cpp
Go to the documentation of this file.
1 #include "ble_uuid.h"
2 
3 #ifdef USE_ESP32
4 
5 #include <cstring>
6 #include <cstdio>
7 #include <cinttypes>
8 #include "esphome/core/log.h"
9 
10 namespace esphome {
11 namespace esp32_ble {
12 
13 static const char *const TAG = "esp32_ble";
14 
15 ESPBTUUID::ESPBTUUID() : uuid_() {}
17  ESPBTUUID ret;
18  ret.uuid_.len = ESP_UUID_LEN_16;
19  ret.uuid_.uuid.uuid16 = uuid;
20  return ret;
21 }
23  ESPBTUUID ret;
24  ret.uuid_.len = ESP_UUID_LEN_32;
25  ret.uuid_.uuid.uuid32 = uuid;
26  return ret;
27 }
28 ESPBTUUID ESPBTUUID::from_raw(const uint8_t *data) {
29  ESPBTUUID ret;
30  ret.uuid_.len = ESP_UUID_LEN_128;
31  memcpy(ret.uuid_.uuid.uuid128, data, ESP_UUID_LEN_128);
32  return ret;
33 }
34 ESPBTUUID ESPBTUUID::from_raw(const std::string &data) {
35  ESPBTUUID ret;
36  if (data.length() == 4) {
37  ret.uuid_.len = ESP_UUID_LEN_16;
38  ret.uuid_.uuid.uuid16 = 0;
39  for (int i = 0; i < data.length();) {
40  uint8_t msb = data.c_str()[i];
41  uint8_t lsb = data.c_str()[i + 1];
42 
43  if (msb > '9')
44  msb -= 7;
45  if (lsb > '9')
46  lsb -= 7;
47  ret.uuid_.uuid.uuid16 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << (2 - i) * 4;
48  i += 2;
49  }
50  } else if (data.length() == 8) {
51  ret.uuid_.len = ESP_UUID_LEN_32;
52  ret.uuid_.uuid.uuid32 = 0;
53  for (int i = 0; i < data.length();) {
54  uint8_t msb = data.c_str()[i];
55  uint8_t lsb = data.c_str()[i + 1];
56 
57  if (msb > '9')
58  msb -= 7;
59  if (lsb > '9')
60  lsb -= 7;
61  ret.uuid_.uuid.uuid32 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << (6 - i) * 4;
62  i += 2;
63  }
64  } else if (data.length() == 16) { // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be
65  // investigated (lack of time)
66  ret.uuid_.len = ESP_UUID_LEN_128;
67  memcpy(ret.uuid_.uuid.uuid128, (uint8_t *) data.data(), 16);
68  } else if (data.length() == 36) {
69  // If the length of the string is 36 bytes then we will assume it is a long hex string in
70  // UUID format.
71  ret.uuid_.len = ESP_UUID_LEN_128;
72  int n = 0;
73  for (int i = 0; i < data.length();) {
74  if (data.c_str()[i] == '-')
75  i++;
76  uint8_t msb = data.c_str()[i];
77  uint8_t lsb = data.c_str()[i + 1];
78 
79  if (msb > '9')
80  msb -= 7;
81  if (lsb > '9')
82  lsb -= 7;
83  ret.uuid_.uuid.uuid128[15 - n++] = ((msb & 0x0F) << 4) | (lsb & 0x0F);
84  i += 2;
85  }
86  } else {
87  ESP_LOGE(TAG, "ERROR: UUID value not 2, 4, 16 or 36 bytes - %s", data.c_str());
88  }
89  return ret;
90 }
91 ESPBTUUID ESPBTUUID::from_uuid(esp_bt_uuid_t uuid) {
92  ESPBTUUID ret;
93  ret.uuid_.len = uuid.len;
94  if (uuid.len == ESP_UUID_LEN_16) {
95  ret.uuid_.uuid.uuid16 = uuid.uuid.uuid16;
96  } else if (uuid.len == ESP_UUID_LEN_32) {
97  ret.uuid_.uuid.uuid32 = uuid.uuid.uuid32;
98  } else if (uuid.len == ESP_UUID_LEN_128) {
99  memcpy(ret.uuid_.uuid.uuid128, uuid.uuid.uuid128, ESP_UUID_LEN_128);
100  }
101  return ret;
102 }
104  if (this->uuid_.len == ESP_UUID_LEN_128) {
105  return *this;
106  }
107  uint8_t data[] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
108  uint32_t uuid32;
109  if (this->uuid_.len == ESP_UUID_LEN_32) {
110  uuid32 = this->uuid_.uuid.uuid32;
111  } else {
112  uuid32 = this->uuid_.uuid.uuid16;
113  }
114  for (uint8_t i = 0; i < this->uuid_.len; i++) {
115  data[12 + i] = ((uuid32 >> i * 8) & 0xFF);
116  }
117  return ESPBTUUID::from_raw(data);
118 }
119 bool ESPBTUUID::contains(uint8_t data1, uint8_t data2) const {
120  if (this->uuid_.len == ESP_UUID_LEN_16) {
121  return (this->uuid_.uuid.uuid16 >> 8) == data2 && (this->uuid_.uuid.uuid16 & 0xFF) == data1;
122  } else if (this->uuid_.len == ESP_UUID_LEN_32) {
123  for (uint8_t i = 0; i < 3; i++) {
124  bool a = ((this->uuid_.uuid.uuid32 >> i * 8) & 0xFF) == data1;
125  bool b = ((this->uuid_.uuid.uuid32 >> (i + 1) * 8) & 0xFF) == data2;
126  if (a && b)
127  return true;
128  }
129  } else {
130  for (uint8_t i = 0; i < 15; i++) {
131  if (this->uuid_.uuid.uuid128[i] == data1 && this->uuid_.uuid.uuid128[i + 1] == data2)
132  return true;
133  }
134  }
135  return false;
136 }
137 bool ESPBTUUID::operator==(const ESPBTUUID &uuid) const {
138  if (this->uuid_.len == uuid.uuid_.len) {
139  switch (this->uuid_.len) {
140  case ESP_UUID_LEN_16:
141  if (uuid.uuid_.uuid.uuid16 == this->uuid_.uuid.uuid16) {
142  return true;
143  }
144  break;
145  case ESP_UUID_LEN_32:
146  if (uuid.uuid_.uuid.uuid32 == this->uuid_.uuid.uuid32) {
147  return true;
148  }
149  break;
150  case ESP_UUID_LEN_128:
151  for (int i = 0; i < ESP_UUID_LEN_128; i++) {
152  if (uuid.uuid_.uuid.uuid128[i] != this->uuid_.uuid.uuid128[i]) {
153  return false;
154  }
155  }
156  return true;
157  break;
158  }
159  } else {
160  return this->as_128bit() == uuid.as_128bit();
161  }
162  return false;
163 }
164 esp_bt_uuid_t ESPBTUUID::get_uuid() const { return this->uuid_; }
165 std::string ESPBTUUID::to_string() const {
166  switch (this->uuid_.len) {
167  case ESP_UUID_LEN_16:
168  return str_snprintf("0x%02X%02X", 6, this->uuid_.uuid.uuid16 >> 8, this->uuid_.uuid.uuid16 & 0xff);
169  case ESP_UUID_LEN_32:
170  return str_snprintf("0x%02" PRIX32 "%02" PRIX32 "%02" PRIX32 "%02" PRIX32, 10, (this->uuid_.uuid.uuid32 >> 24),
171  (this->uuid_.uuid.uuid32 >> 16 & 0xff), (this->uuid_.uuid.uuid32 >> 8 & 0xff),
172  this->uuid_.uuid.uuid32 & 0xff);
173  default:
174  case ESP_UUID_LEN_128:
175  std::string buf;
176  for (int8_t i = 15; i >= 0; i--) {
177  buf += str_snprintf("%02X", 2, this->uuid_.uuid.uuid128[i]);
178  if (i == 6 || i == 8 || i == 10 || i == 12)
179  buf += "-";
180  }
181  return buf;
182  }
183  return "";
184 }
185 
186 } // namespace esp32_ble
187 } // namespace esphome
188 
189 #endif
bool operator==(const ESPBTUUID &uuid) const
Definition: ble_uuid.cpp:137
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:91
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: ble_uuid.cpp:22
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
std::string to_string() const
Definition: ble_uuid.cpp:165
bool contains(uint8_t data1, uint8_t data2) const
Definition: ble_uuid.cpp:119
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
ESPBTUUID as_128bit() const
Definition: ble_uuid.cpp:103
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:298
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:164