ESPHome  2025.2.0
ble_service.cpp
Go to the documentation of this file.
1 #include "ble_service.h"
2 #include "ble_server.h"
3 #include "esphome/core/log.h"
4 
5 #ifdef USE_ESP32
6 
7 namespace esphome {
8 namespace esp32_ble_server {
9 
10 static const char *const TAG = "esp32_ble_server.service";
11 
12 BLEService::BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
13  : uuid_(uuid), num_handles_(num_handles), inst_id_(inst_id), advertise_(advertise) {}
14 
16  for (auto &chr : this->characteristics_)
17  delete chr; // NOLINT(cppcoreguidelines-owning-memory)
18 }
19 
21  for (auto *chr : this->characteristics_) {
22  if (chr->get_uuid() == uuid)
23  return chr;
24  }
25  return nullptr;
26 }
27 
29  return this->get_characteristic(ESPBTUUID::from_uint16(uuid));
30 }
31 BLECharacteristic *BLEService::create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties) {
32  return create_characteristic(ESPBTUUID::from_uint16(uuid), properties);
33 }
34 BLECharacteristic *BLEService::create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties) {
35  return create_characteristic(ESPBTUUID::from_raw(uuid), properties);
36 }
37 BLECharacteristic *BLEService::create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties) {
38  // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
39  BLECharacteristic *characteristic = new BLECharacteristic(uuid, properties);
40  this->characteristics_.push_back(characteristic);
41  return characteristic;
42 }
43 
45  this->server_ = server;
46 
47  esp_gatt_srvc_id_t srvc_id;
48  srvc_id.is_primary = true;
49  srvc_id.id.inst_id = this->inst_id_;
50  srvc_id.id.uuid = this->uuid_.get_uuid();
51 
52  esp_err_t err = esp_ble_gatts_create_service(server->get_gatts_if(), &srvc_id, this->num_handles_);
53  if (err != ESP_OK) {
54  ESP_LOGE(TAG, "esp_ble_gatts_create_service failed: %d", err);
55  this->state_ = FAILED;
56  return;
57  }
58  this->state_ = CREATING;
59 }
60 
62  if (this->state_ == DELETING || this->state_ == DELETED)
63  return;
64  this->state_ = DELETING;
66  this->last_created_characteristic_ = nullptr;
67  // Call all characteristics to delete
68  for (auto *characteristic : this->characteristics_)
69  characteristic->do_delete();
70  this->stop_();
71  esp_err_t err = esp_ble_gatts_delete_service(this->handle_);
72  if (err != ESP_OK) {
73  ESP_LOGE(TAG, "esp_ble_gatts_delete_service failed: %d", err);
74  return;
75  }
76 }
77 
79  if (this->created_characteristic_count_ >= this->characteristics_.size() &&
81  return false; // Signifies there are no characteristics, or they are all finished being created.
82 
84  return true; // Signifies that the previous characteristic is still being created.
85 
86  auto *characteristic = this->characteristics_[this->created_characteristic_count_++];
87  this->last_created_characteristic_ = characteristic;
88  characteristic->do_create(this);
89  return true;
90 }
91 
93  if (this->do_create_characteristics_())
94  return;
95  should_start_ = true;
96 
97  this->state_ = STARTING;
98  esp_err_t err = esp_ble_gatts_start_service(this->handle_);
99  if (err != ESP_OK) {
100  ESP_LOGE(TAG, "esp_ble_gatts_start_service failed: %d", err);
101  return;
102  }
103  if (this->advertise_)
105 }
106 
108  should_start_ = false;
109  this->stop_();
110 }
111 
113  if (this->state_ == STOPPING || this->state_ == STOPPED)
114  return;
115  this->state_ = STOPPING;
116  esp_err_t err = esp_ble_gatts_stop_service(this->handle_);
117  if (err != ESP_OK) {
118  ESP_LOGE(TAG, "esp_ble_gatts_stop_service failed: %d", err);
119  return;
120  }
121  if (this->advertise_)
123 }
124 
126  if (this->state_ == FAILED)
127  return true;
128  bool failed = false;
129  for (auto *characteristic : this->characteristics_)
130  failed |= characteristic->is_failed();
131 
132  if (failed)
133  this->state_ = FAILED;
134  return this->state_ == FAILED;
135 }
136 
137 void BLEService::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
138  esp_ble_gatts_cb_param_t *param) {
139  switch (event) {
140  case ESP_GATTS_CREATE_EVT: {
141  if (this->uuid_ == ESPBTUUID::from_uuid(param->create.service_id.id.uuid) &&
142  this->inst_id_ == param->create.service_id.id.inst_id) {
143  this->handle_ = param->create.service_handle;
144  this->state_ = CREATED;
145  if (this->should_start_)
146  this->start();
147  }
148  break;
149  }
150  case ESP_GATTS_DELETE_EVT:
151  if (param->del.service_handle == this->handle_) {
152  this->state_ = DELETED;
153  }
154  break;
155  case ESP_GATTS_START_EVT: {
156  if (param->start.service_handle == this->handle_) {
157  this->state_ = RUNNING;
158  }
159  break;
160  }
161  case ESP_GATTS_STOP_EVT: {
162  if (param->start.service_handle == this->handle_) {
163  this->state_ = STOPPED;
164  }
165  break;
166  }
167  default:
168  break;
169  }
170 
171  for (auto *characteristic : this->characteristics_) {
172  characteristic->gatts_event_handler(event, gatts_if, param);
173  }
174 }
175 
176 } // namespace esp32_ble_server
177 } // namespace esphome
178 
179 #endif
ESP32BLE * global_ble
Definition: ble.cpp:438
BLECharacteristic * last_created_characteristic_
Definition: ble_service.h:56
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition: ble.cpp:95
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
Definition: ble_service.cpp:12
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:97
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
void do_create(BLEServer *server)
Definition: ble_service.cpp:44
std::vector< BLECharacteristic * > characteristics_
Definition: ble_service.h:55
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
BLECharacteristic * get_characteristic(ESPBTUUID uuid)
Definition: ble_service.cpp:20
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties)
Definition: ble_service.cpp:34
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition: ble.cpp:89
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:170