ESPHome  2024.11.0
mopeka_ble.cpp
Go to the documentation of this file.
1 #include "mopeka_ble.h"
2 
3 #include "esphome/core/log.h"
4 
5 #ifdef USE_ESP32
6 
7 namespace esphome {
8 namespace mopeka_ble {
9 
10 static const char *const TAG = "mopeka_ble";
11 
12 // Mopeka Std (CC2540) sensor details
13 static const uint16_t SERVICE_UUID_CC2540 = 0xADA0;
14 static const uint16_t MANUFACTURER_CC2540_ID = 0x000D; // Texas Instruments (TI)
15 static const uint8_t MANUFACTURER_CC2540_DATA_LENGTH = 23;
16 
17 // Mopeka Pro (NRF52) sensor details
18 static const uint16_t SERVICE_UUID_NRF52 = 0xFEE5;
19 static const uint16_t MANUFACTURER_NRF52_ID = 0x0059; // Nordic
20 static const uint8_t MANUFACTURER_NRF52_DATA_LENGTH = 10;
21 
39  // Fetch information about BLE device.
40  const auto &service_uuids = device.get_service_uuids();
41  if (service_uuids.size() != 1) {
42  return false;
43  }
44  const auto &service_uuid = service_uuids[0];
45 
46  const auto &manu_datas = device.get_manufacturer_datas();
47  if (manu_datas.size() != 1) {
48  return false;
49  }
50  const auto &manu_data = manu_datas[0];
51 
52  // Is the device maybe a Mopeka Std (CC2540) sensor.
53  if (service_uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(SERVICE_UUID_CC2540)) {
54  if (manu_data.uuid != esp32_ble_tracker::ESPBTUUID::from_uint16(MANUFACTURER_CC2540_ID)) {
55  return false;
56  }
57 
58  if (manu_data.data.size() != MANUFACTURER_CC2540_DATA_LENGTH) {
59  return false;
60  }
61 
62  const bool sync_button_pressed = (manu_data.data[3] & 0x80) != 0;
63 
64  if (this->show_sensors_without_sync_ || sync_button_pressed) {
65  ESP_LOGI(TAG, "MOPEKA STD (CC2540) SENSOR FOUND: %s", device.address_str().c_str());
66  }
67 
68  // Is the device maybe a Mopeka Pro (NRF52) sensor.
69  } else if (service_uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(SERVICE_UUID_NRF52)) {
70  if (manu_data.uuid != esp32_ble_tracker::ESPBTUUID::from_uint16(MANUFACTURER_NRF52_ID)) {
71  return false;
72  }
73 
74  if (manu_data.data.size() != MANUFACTURER_NRF52_DATA_LENGTH) {
75  return false;
76  }
77 
78  const bool sync_button_pressed = (manu_data.data[2] & 0x80) != 0;
79 
80  if (this->show_sensors_without_sync_ || sync_button_pressed) {
81  ESP_LOGI(TAG, "MOPEKA PRO (NRF52) SENSOR FOUND: %s", device.address_str().c_str());
82  }
83  }
84 
85  return false;
86 }
87 
88 } // namespace mopeka_ble
89 } // namespace esphome
90 
91 #endif
const std::vector< ServiceData > & get_manufacturer_datas() const
const std::vector< ESPBTUUID > & get_service_uuids() const
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
Parse all incoming BLE payloads to see if it is a Mopeka BLE advertisement.
Definition: mopeka_ble.cpp:38
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7