ESPHome 2026.5.1
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
9static const char *const 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 return create_characteristic(ESPBTUUID::from_uint16(uuid), properties);
32}
33BLECharacteristic *BLEService::create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties) {
34 return create_characteristic(ESPBTUUID::from_raw(uuid), properties);
35}
36BLECharacteristic *BLEService::create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties) {
37 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
38 BLECharacteristic *characteristic = new BLECharacteristic(uuid, properties);
39 this->characteristics_.push_back(characteristic);
40 return characteristic;
41}
42
44 this->server_ = server;
45
46 esp_gatt_srvc_id_t srvc_id;
47 srvc_id.is_primary = true;
48 srvc_id.id.inst_id = this->inst_id_;
49 srvc_id.id.uuid = this->uuid_.get_uuid();
50
51 esp_err_t err = esp_ble_gatts_create_service(server->get_gatts_if(), &srvc_id, this->num_handles_);
52 if (err != ESP_OK) {
53 ESP_LOGE(TAG, "esp_ble_gatts_create_service failed: %d", err);
54 this->state_ = FAILED;
55 return;
56 }
57 this->state_ = CREATING;
58}
59
61 if (this->state_ == DELETING || this->state_ == DELETED)
62 return;
63 this->state_ = DELETING;
65 this->last_created_characteristic_ = nullptr;
66 // Call all characteristics to delete
67 for (auto *characteristic : this->characteristics_)
68 characteristic->do_delete();
69 this->stop_();
70 esp_err_t err = esp_ble_gatts_delete_service(this->handle_);
71 if (err != ESP_OK) {
72 ESP_LOGE(TAG, "esp_ble_gatts_delete_service failed: %d", err);
73 return;
74 }
75}
76
78 if (this->created_characteristic_count_ >= this->characteristics_.size() &&
79 (this->last_created_characteristic_ == nullptr || this->last_created_characteristic_->is_created()))
80 return false; // Signifies there are no characteristics, or they are all finished being created.
81
83 return true; // Signifies that the previous characteristic is still being created.
84
85 auto *characteristic = this->characteristics_[this->created_characteristic_count_++];
86 this->last_created_characteristic_ = characteristic;
87 characteristic->do_create(this);
88 return true;
89}
90
93 return;
94 should_start_ = true;
95
96 this->state_ = STARTING;
97 esp_err_t err = esp_ble_gatts_start_service(this->handle_);
98 if (err != ESP_OK) {
99 ESP_LOGE(TAG, "esp_ble_gatts_start_service failed: %d", err);
100 return;
101 }
102 if (this->advertise_)
104}
105
107 should_start_ = false;
108 this->stop_();
109}
110
112 if (this->state_ == STOPPING || this->state_ == STOPPED)
113 return;
114 this->state_ = STOPPING;
115 esp_err_t err = esp_ble_gatts_stop_service(this->handle_);
116 if (err != ESP_OK) {
117 ESP_LOGE(TAG, "esp_ble_gatts_stop_service failed: %d", err);
118 return;
119 }
120 if (this->advertise_)
122}
123
125 if (this->state_ == FAILED)
126 return true;
127 bool failed = false;
128 for (auto *characteristic : this->characteristics_)
129 failed |= characteristic->is_failed();
130
131 if (failed)
132 this->state_ = FAILED;
133 return this->state_ == FAILED;
134}
135
136void BLEService::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
137 esp_ble_gatts_cb_param_t *param) {
138 switch (event) {
139 case ESP_GATTS_CREATE_EVT: {
140 if (this->uuid_ == ESPBTUUID::from_uuid(param->create.service_id.id.uuid) &&
141 this->inst_id_ == param->create.service_id.id.inst_id) {
142 this->handle_ = param->create.service_handle;
143 this->state_ = CREATED;
144 if (this->should_start_)
145 this->start();
146 }
147 break;
148 }
149 case ESP_GATTS_DELETE_EVT:
150 if (param->del.service_handle == this->handle_) {
151 this->state_ = DELETED;
152 }
153 break;
154 case ESP_GATTS_START_EVT: {
155 if (param->start.service_handle == this->handle_) {
156 this->state_ = RUNNING;
157 }
158 break;
159 }
160 case ESP_GATTS_STOP_EVT: {
161 if (param->stop.service_handle == this->handle_) {
162 this->state_ = STOPPED;
163 }
164 break;
165 }
166 default:
167 break;
168 }
169
170 for (auto *characteristic : this->characteristics_) {
171 characteristic->gatts_event_handler(event, gatts_if, param);
172 }
173}
174
175} // namespace esphome::esp32_ble_server
176
177#endif
void advertising_add_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:131
void advertising_remove_service_uuid(ESPBTUUID uuid)
Definition ble.cpp:137
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition ble_uuid.cpp:84
static ESPBTUUID from_uint16(uint16_t uuid)
Definition ble_uuid.cpp:17
static ESPBTUUID from_raw(const uint8_t *data)
Definition ble_uuid.cpp:29
esp_bt_uuid_t get_uuid() const
Definition ble_uuid.cpp:145
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties)
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
void do_create(BLEServer *server)
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:55
std::vector< BLECharacteristic * > characteristics_
Definition ble_service.h:54
BLECharacteristic * get_characteristic(ESPBTUUID uuid)
ESP32BLE * global_ble
Definition ble.cpp:718