ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
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
8
9ESPHOME_LOG_TAG(TAG, "esp32_ble_server.service");
10
11BLEService::BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
12 : uuid_(uuid), num_handles_(num_handles), inst_id_(inst_id), advertise_(advertise) {}
13
15 for (auto &chr : this->characteristics_)
16 delete chr; // NOLINT(cppcoreguidelines-owning-memory)
17}
18
20 for (auto *chr : this->characteristics_) {
21 if (chr->get_uuid() == uuid)
22 return chr;
23 }
24 return nullptr;
25}
26
30BLECharacteristic *BLEService::create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties,
31 esp_gatt_perm_t permissions) {
32 return create_characteristic(ESPBTUUID::from_uint16(uuid), properties, permissions);
33}
34BLECharacteristic *BLEService::create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties,
35 esp_gatt_perm_t permissions) {
36 return create_characteristic(ESPBTUUID::from_raw(uuid), properties, permissions);
37}
38BLECharacteristic *BLEService::create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties,
39 esp_gatt_perm_t permissions) {
40 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
41 BLECharacteristic *characteristic = new BLECharacteristic(uuid, properties, permissions);
42 this->characteristics_.push_back(characteristic);
43 return characteristic;
44}
45
47 this->server_ = server;
48
49 esp_gatt_srvc_id_t srvc_id;
50 srvc_id.is_primary = true;
51 srvc_id.id.inst_id = this->inst_id_;
52 srvc_id.id.uuid = this->uuid_.get_uuid();
53
54 esp_err_t err = esp_ble_gatts_create_service(server->get_gatts_if(), &srvc_id, this->num_handles_);
55 if (err != ESP_OK) {
56 ESP_LOGE(TAG, "esp_ble_gatts_create_service failed: %d", err);
57 this->state_ = FAILED;
58 return;
59 }
60 this->state_ = CREATING;
61}
62
64 if (this->state_ == DELETING || this->state_ == DELETED)
65 return;
66 this->state_ = DELETING;
68 this->last_created_characteristic_ = nullptr;
69 // Call all characteristics to delete
70 for (auto *characteristic : this->characteristics_)
71 characteristic->do_delete();
72 this->stop_();
73 esp_err_t err = esp_ble_gatts_delete_service(this->handle_);
74 if (err != ESP_OK) {
75 ESP_LOGE(TAG, "esp_ble_gatts_delete_service failed: %d", err);
76 return;
77 }
78}
79
81 if (this->created_characteristic_count_ >= this->characteristics_.size() &&
82 (this->last_created_characteristic_ == nullptr || this->last_created_characteristic_->is_created()))
83 return false; // Signifies there are no characteristics, or they are all finished being created.
84
86 return true; // Signifies that the previous characteristic is still being created.
87
88 auto *characteristic = this->characteristics_[this->created_characteristic_count_++];
89 this->last_created_characteristic_ = characteristic;
90 characteristic->do_create(this);
91 return true;
92}
93
96 return;
97 should_start_ = true;
98
99 this->state_ = STARTING;
100 esp_err_t err = esp_ble_gatts_start_service(this->handle_);
101 if (err != ESP_OK) {
102 ESP_LOGE(TAG, "esp_ble_gatts_start_service failed: %d", err);
103 return;
104 }
105 if (this->advertise_)
107}
108
110 should_start_ = false;
111 this->stop_();
112}
113
115 if (this->state_ == STOPPING || this->state_ == STOPPED)
116 return;
117 this->state_ = STOPPING;
118 esp_err_t err = esp_ble_gatts_stop_service(this->handle_);
119 if (err != ESP_OK) {
120 ESP_LOGE(TAG, "esp_ble_gatts_stop_service failed: %d", err);
121 return;
122 }
123 if (this->advertise_)
125}
126
128 if (this->state_ == FAILED)
129 return true;
130 bool failed = false;
131 for (auto *characteristic : this->characteristics_)
132 failed |= characteristic->is_failed();
133
134 if (failed)
135 this->state_ = FAILED;
136 return this->state_ == FAILED;
137}
138
139void BLEService::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
140 esp_ble_gatts_cb_param_t *param) {
141 switch (event) {
142 case ESP_GATTS_CREATE_EVT: {
143 if (this->uuid_ == ESPBTUUID::from_uuid(param->create.service_id.id.uuid) &&
144 this->inst_id_ == param->create.service_id.id.inst_id) {
145 this->handle_ = param->create.service_handle;
146 this->state_ = CREATED;
147 if (this->should_start_)
148 this->start();
149 }
150 break;
151 }
152 case ESP_GATTS_DELETE_EVT:
153 if (param->del.service_handle == this->handle_) {
154 this->state_ = DELETED;
155 }
156 break;
157 case ESP_GATTS_START_EVT: {
158 if (param->start.service_handle == this->handle_) {
159 this->state_ = RUNNING;
160 }
161 break;
162 }
163 case ESP_GATTS_STOP_EVT: {
164 if (param->stop.service_handle == this->handle_) {
165 this->state_ = STOPPED;
166 }
167 break;
168 }
169 default:
170 break;
171 }
172
173 for (auto *characteristic : this->characteristics_) {
174 characteristic->gatts_event_handler(event, gatts_if, param);
175 }
176}
177
178} // namespace esphome::esp32_ble_server
179
180#endif
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Source compatibility with the historical esp32_ble API (esp32 builds only).
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_raw(const uint8_t *data)
Construct from raw 16-byte little-endian UUID.
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:242
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:258
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
void do_create(BLEServer *server)
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties, esp_gatt_perm_t permissions=ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
BLECharacteristic * last_created_characteristic_
Definition ble_service.h:59
std::vector< BLECharacteristic * > characteristics_
Definition ble_service.h:58
BLECharacteristic * get_characteristic(ESPBTUUID uuid)
ESP32BLE * global_ble
Definition ble.cpp:908
ESPHOME_LOG_TAG(TAG, "esp32_ble")