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