ESPHome  2024.11.0
ble_service.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ble_characteristic.h"
5 
6 #include <vector>
7 
8 #ifdef USE_ESP32
9 
10 #include <esp_bt_defs.h>
11 #include <esp_gap_ble_api.h>
12 #include <esp_gatt_defs.h>
13 #include <esp_gattc_api.h>
14 #include <esp_gatts_api.h>
15 
16 namespace esphome {
17 namespace esp32_ble_server {
18 
19 class BLEServer;
20 
21 using namespace esp32_ble;
22 
23 class BLEService {
24  public:
25  BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise);
26  ~BLEService();
27  BLECharacteristic *get_characteristic(ESPBTUUID uuid);
28  BLECharacteristic *get_characteristic(uint16_t uuid);
29 
30  BLECharacteristic *create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties);
31  BLECharacteristic *create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties);
32  BLECharacteristic *create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties);
33 
34  ESPBTUUID get_uuid() { return this->uuid_; }
35  BLECharacteristic *get_last_created_characteristic() { return this->last_created_characteristic_; }
36  uint16_t get_handle() { return this->handle_; }
37 
38  BLEServer *get_server() { return this->server_; }
39 
40  void do_create(BLEServer *server);
41  void do_delete();
42  void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
43 
44  void start();
45  void stop();
46 
47  bool is_created();
48  bool is_failed();
49 
50  bool is_running() { return this->running_state_ == RUNNING; }
51  bool is_starting() { return this->running_state_ == STARTING; }
52  bool is_deleted() { return this->init_state_ == DELETED; }
53 
54  protected:
55  std::vector<BLECharacteristic *> characteristics_;
56  BLECharacteristic *last_created_characteristic_{nullptr};
57  uint32_t created_characteristic_count_{0};
60  uint16_t num_handles_;
61  uint16_t handle_{0xFFFF};
62  uint8_t inst_id_;
63  bool advertise_{false};
64  bool should_start_{false};
65 
66  bool do_create_characteristics_();
67  void stop_();
68 
69  enum InitState : uint8_t {
70  FAILED = 0x00,
77  } init_state_{INIT};
78 
79  enum RunningState : uint8_t {
84  } running_state_{STOPPED};
85 };
86 
87 } // namespace esp32_ble_server
88 } // namespace esphome
89 
90 #endif
BLECharacteristic * get_last_created_characteristic()
Definition: ble_service.h:35
std::vector< BLECharacteristic * > characteristics_
Definition: ble_service.h:55
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7