ESPHome  2024.7.1
ble_event.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include <vector>
6 
7 #include <esp_gap_ble_api.h>
8 #include <esp_gattc_api.h>
9 #include <esp_gatts_api.h>
10 
11 namespace esphome {
12 namespace esp32_ble {
13 // Received GAP, GATTC and GATTS events are only queued, and get processed in the main loop().
14 // This class stores each event in a single type.
15 class BLEEvent {
16  public:
17  BLEEvent(esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) {
18  this->event_.gap.gap_event = e;
19  memcpy(&this->event_.gap.gap_param, p, sizeof(esp_ble_gap_cb_param_t));
20  this->type_ = GAP;
21  };
22 
23  BLEEvent(esp_gattc_cb_event_t e, esp_gatt_if_t i, esp_ble_gattc_cb_param_t *p) {
24  this->event_.gattc.gattc_event = e;
25  this->event_.gattc.gattc_if = i;
26  memcpy(&this->event_.gattc.gattc_param, p, sizeof(esp_ble_gattc_cb_param_t));
27  // Need to also make a copy of relevant event data.
28  switch (e) {
29  case ESP_GATTC_NOTIFY_EVT:
30  this->data.assign(p->notify.value, p->notify.value + p->notify.value_len);
31  this->event_.gattc.gattc_param.notify.value = this->data.data();
32  break;
33  case ESP_GATTC_READ_CHAR_EVT:
34  case ESP_GATTC_READ_DESCR_EVT:
35  this->data.assign(p->read.value, p->read.value + p->read.value_len);
36  this->event_.gattc.gattc_param.read.value = this->data.data();
37  break;
38  default:
39  break;
40  }
41  this->type_ = GATTC;
42  };
43 
44  BLEEvent(esp_gatts_cb_event_t e, esp_gatt_if_t i, esp_ble_gatts_cb_param_t *p) {
45  this->event_.gatts.gatts_event = e;
46  this->event_.gatts.gatts_if = i;
47  memcpy(&this->event_.gatts.gatts_param, p, sizeof(esp_ble_gatts_cb_param_t));
48  // Need to also make a copy of relevant event data.
49  switch (e) {
50  case ESP_GATTS_WRITE_EVT:
51  this->data.assign(p->write.value, p->write.value + p->write.len);
52  this->event_.gatts.gatts_param.write.value = this->data.data();
53  break;
54  default:
55  break;
56  }
57  this->type_ = GATTS;
58  };
59 
60  union {
61  // NOLINTNEXTLINE(readability-identifier-naming)
62  struct gap_event {
63  esp_gap_ble_cb_event_t gap_event;
64  esp_ble_gap_cb_param_t gap_param;
65  } gap;
66 
67  // NOLINTNEXTLINE(readability-identifier-naming)
68  struct gattc_event {
69  esp_gattc_cb_event_t gattc_event;
70  esp_gatt_if_t gattc_if;
71  esp_ble_gattc_cb_param_t gattc_param;
72  } gattc;
73 
74  // NOLINTNEXTLINE(readability-identifier-naming)
75  struct gatts_event {
76  esp_gatts_cb_event_t gatts_event;
77  esp_gatt_if_t gatts_if;
78  esp_ble_gatts_cb_param_t gatts_param;
79  } gatts;
80  } event_;
81 
82  std::vector<uint8_t> data{};
83  // NOLINTNEXTLINE(readability-identifier-naming)
84  enum ble_event_t : uint8_t {
85  GAP,
88  } type_;
89 };
90 
91 } // namespace esp32_ble
92 } // namespace esphome
93 
94 #endif
esp_ble_gattc_cb_param_t gattc_param
Definition: ble_event.h:71
struct esphome::esp32_ble::BLEEvent::@76::gattc_event gattc
esp_gap_ble_cb_event_t gap_event
Definition: ble_event.h:63
BLEEvent(esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p)
Definition: ble_event.h:17
esp_ble_gatts_cb_param_t gatts_param
Definition: ble_event.h:78
BLEEvent(esp_gatts_cb_event_t e, esp_gatt_if_t i, esp_ble_gatts_cb_param_t *p)
Definition: ble_event.h:44
esp_gatts_cb_event_t gatts_event
Definition: ble_event.h:76
esp_gattc_cb_event_t gattc_event
Definition: ble_event.h:69
esp_ble_gap_cb_param_t gap_param
Definition: ble_event.h:64
struct esphome::esp32_ble::BLEEvent::@76::gap_event gap
enum esphome::esp32_ble::BLEEvent::ble_event_t type_
union esphome::esp32_ble::BLEEvent::@76 event_
std::vector< uint8_t > data
Definition: ble_event.h:82
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
struct esphome::esp32_ble::BLEEvent::@76::gatts_event gatts
BLEEvent(esp_gattc_cb_event_t e, esp_gatt_if_t i, esp_ble_gattc_cb_param_t *p)
Definition: ble_event.h:23