ESPHome  2022.12.8
exposure_notifications.cpp
Go to the documentation of this file.
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 #ifdef USE_ESP32
6 
7 namespace esphome {
8 namespace exposure_notifications {
9 
10 using namespace esp32_ble_tracker;
11 
12 static const char *const TAG = "exposure_notifications";
13 
15  // See also https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
16  if (device.get_service_uuids().size() != 1)
17  return false;
18 
19  // Exposure notifications have Service UUID FD 6F
20  ESPBTUUID uuid = device.get_service_uuids()[0];
21  // constant service identifier
22  const ESPBTUUID expected_uuid = ESPBTUUID::from_uint16(0xFD6F);
23  if (uuid != expected_uuid)
24  return false;
25  if (device.get_service_datas().size() != 1)
26  return false;
27 
28  // The service data should be 20 bytes
29  // First 16 bytes are the rolling proximity identifier (RPI)
30  // Then 4 bytes of encrypted metadata follow which can be used to get the transmit power level.
31  ServiceData service_data = device.get_service_datas()[0];
32  if (service_data.uuid != expected_uuid)
33  return false;
34  auto data = service_data.data;
35  if (data.size() != 20)
36  return false;
37  ExposureNotification notification{};
38  memcpy(&notification.address[0], device.address(), 6);
39  memcpy(&notification.rolling_proximity_identifier[0], &data[0], 16);
40  memcpy(&notification.associated_encrypted_metadata[0], &data[16], 4);
41  notification.rssi = device.get_rssi();
42  this->trigger(notification);
43  return true;
44 }
45 
46 } // namespace exposure_notifications
47 } // namespace esphome
48 
49 #endif
const std::vector< ESPBTUUID > & get_service_uuids() const
const std::vector< ServiceData > & get_service_datas() const
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: a4988.cpp:4
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override