mirror of
https://github.com/esphome/esphome.git
synced 2025-11-18 07:45:56 +00:00
Move esp32_ble_server to its own component (#1898)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#include "esp32_improv_component.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include "esphome/components/esp32_ble/ble.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_2902.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/components/esp32_ble/ble_2902.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
@@ -12,40 +14,39 @@ static const char *const TAG = "esp32_improv.component";
|
||||
|
||||
ESP32ImprovComponent::ESP32ImprovComponent() { global_improv_component = this; }
|
||||
|
||||
void ESP32ImprovComponent::setup_service() {
|
||||
this->service_ = esp32_ble::global_ble_server->create_service(improv::SERVICE_UUID, true);
|
||||
void ESP32ImprovComponent::setup() {
|
||||
this->service_ = global_ble_server->create_service(improv::SERVICE_UUID, true);
|
||||
this->setup_characteristics();
|
||||
}
|
||||
|
||||
void ESP32ImprovComponent::setup_characteristics() {
|
||||
this->status_ = this->service_->create_characteristic(
|
||||
improv::STATUS_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ | esp32_ble::BLECharacteristic::PROPERTY_NOTIFY);
|
||||
esp32_ble::BLEDescriptor *status_descriptor = new esp32_ble::BLE2902();
|
||||
improv::STATUS_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *status_descriptor = new BLE2902();
|
||||
this->status_->add_descriptor(status_descriptor);
|
||||
|
||||
this->error_ = this->service_->create_characteristic(
|
||||
improv::ERROR_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ | esp32_ble::BLECharacteristic::PROPERTY_NOTIFY);
|
||||
esp32_ble::BLEDescriptor *error_descriptor = new esp32_ble::BLE2902();
|
||||
improv::ERROR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *error_descriptor = new BLE2902();
|
||||
this->error_->add_descriptor(error_descriptor);
|
||||
|
||||
this->rpc_ =
|
||||
this->service_->create_characteristic(improv::RPC_COMMAND_UUID, esp32_ble::BLECharacteristic::PROPERTY_WRITE);
|
||||
this->rpc_ = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE);
|
||||
this->rpc_->on_write([this](const std::vector<uint8_t> &data) {
|
||||
if (data.size() > 0) {
|
||||
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
|
||||
}
|
||||
});
|
||||
esp32_ble::BLEDescriptor *rpc_descriptor = new esp32_ble::BLE2902();
|
||||
BLEDescriptor *rpc_descriptor = new BLE2902();
|
||||
this->rpc_->add_descriptor(rpc_descriptor);
|
||||
|
||||
this->rpc_response_ =
|
||||
this->service_->create_characteristic(improv::RPC_RESULT_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ |
|
||||
esp32_ble::BLECharacteristic::PROPERTY_NOTIFY);
|
||||
esp32_ble::BLEDescriptor *rpc_response_descriptor = new esp32_ble::BLE2902();
|
||||
this->rpc_response_ = this->service_->create_characteristic(
|
||||
improv::RPC_RESULT_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *rpc_response_descriptor = new BLE2902();
|
||||
this->rpc_response_->add_descriptor(rpc_response_descriptor);
|
||||
|
||||
this->capabilities_ =
|
||||
this->service_->create_characteristic(improv::CAPABILITIES_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ);
|
||||
esp32_ble::BLEDescriptor *capabilities_descriptor = new esp32_ble::BLE2902();
|
||||
this->service_->create_characteristic(improv::CAPABILITIES_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
BLEDescriptor *capabilities_descriptor = new BLE2902();
|
||||
this->capabilities_->add_descriptor(capabilities_descriptor);
|
||||
uint8_t capabilities = 0x00;
|
||||
if (this->status_indicator_ != nullptr)
|
||||
@@ -64,13 +65,9 @@ void ESP32ImprovComponent::loop() {
|
||||
if (this->status_indicator_ != nullptr)
|
||||
this->status_indicator_->turn_off();
|
||||
|
||||
if (this->service_->is_created() && !this->setup_complete_) {
|
||||
this->setup_characteristics();
|
||||
}
|
||||
|
||||
if (this->should_start_ && this->setup_complete_) {
|
||||
if (this->service_->is_created() && this->should_start_ && this->setup_complete_) {
|
||||
if (this->service_->is_running()) {
|
||||
this->service_->get_server()->get_advertising()->start();
|
||||
esp32_ble::global_ble->get_advertising()->start();
|
||||
|
||||
this->set_state_(improv::STATE_AWAITING_AUTHORIZATION);
|
||||
this->set_error_(improv::ERROR_NONE);
|
||||
@@ -205,10 +202,7 @@ void ESP32ImprovComponent::stop() {
|
||||
});
|
||||
}
|
||||
|
||||
float ESP32ImprovComponent::get_setup_priority() const {
|
||||
// Before WiFi
|
||||
return setup_priority::AFTER_BLUETOOTH;
|
||||
}
|
||||
float ESP32ImprovComponent::get_setup_priority() const { return setup_priority::AFTER_BLUETOOTH; }
|
||||
|
||||
void ESP32ImprovComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "ESP32 Improv:");
|
||||
|
||||
Reference in New Issue
Block a user