mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 20:53:26 +02:00
ac0d921413
* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
207 lines
6.3 KiB
C++
207 lines
6.3 KiB
C++
#ifdef USE_ESP32
|
|
|
|
#include "ble.h"
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
#include <nvs_flash.h>
|
|
#include <freertos/FreeRTOSConfig.h>
|
|
#include <esp_bt_main.h>
|
|
#include <esp_bt.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
#include <esp_gap_ble_api.h>
|
|
|
|
#ifdef USE_ARDUINO
|
|
#include <esp32-hal-bt.h>
|
|
#endif
|
|
|
|
namespace esphome {
|
|
namespace esp32_ble {
|
|
|
|
static const char *const TAG = "esp32_ble";
|
|
|
|
void ESP32BLE::setup() {
|
|
global_ble = this;
|
|
ESP_LOGCONFIG(TAG, "Setting up BLE...");
|
|
|
|
if (!ble_setup_()) {
|
|
ESP_LOGE(TAG, "BLE could not be set up");
|
|
this->mark_failed();
|
|
return;
|
|
}
|
|
|
|
this->advertising_ = new BLEAdvertising(); // NOLINT(cppcoreguidelines-owning-memory)
|
|
|
|
this->advertising_->set_scan_response(true);
|
|
this->advertising_->set_min_preferred_interval(0x06);
|
|
this->advertising_->start();
|
|
|
|
ESP_LOGD(TAG, "BLE setup complete");
|
|
}
|
|
|
|
void ESP32BLE::mark_failed() {
|
|
Component::mark_failed();
|
|
#ifdef USE_ESP32_BLE_SERVER
|
|
if (this->server_ != nullptr) {
|
|
this->server_->mark_failed();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
bool ESP32BLE::ble_setup_() {
|
|
esp_err_t err = nvs_flash_init();
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "nvs_flash_init failed: %d", err);
|
|
return false;
|
|
}
|
|
|
|
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
|
|
// start bt controller
|
|
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
|
|
esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
|
err = esp_bt_controller_init(&cfg);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_bt_controller_init failed: %s", esp_err_to_name(err));
|
|
return false;
|
|
}
|
|
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
|
|
;
|
|
}
|
|
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
|
|
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_bt_controller_enable failed: %s", esp_err_to_name(err));
|
|
return false;
|
|
}
|
|
}
|
|
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
|
|
ESP_LOGE(TAG, "esp bt controller enable failed");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
|
|
|
err = esp_bluedroid_init();
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err);
|
|
return false;
|
|
}
|
|
err = esp_bluedroid_enable();
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err);
|
|
return false;
|
|
}
|
|
err = esp_ble_gap_register_callback(ESP32BLE::gap_event_handler);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ble_gap_register_callback failed: %d", err);
|
|
return false;
|
|
}
|
|
|
|
if (this->has_server()) {
|
|
err = esp_ble_gatts_register_callback(ESP32BLE::gatts_event_handler);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ble_gatts_register_callback failed: %d", err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (this->has_client()) {
|
|
err = esp_ble_gattc_register_callback(ESP32BLE::gattc_event_handler);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ble_gattc_register_callback failed: %d", err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
std::string name = App.get_name();
|
|
if (name.length() > 20) {
|
|
if (App.is_name_add_mac_suffix_enabled()) {
|
|
name.erase(name.begin() + 13, name.end() - 7); // Remove characters between 13 and the mac address
|
|
} else {
|
|
name = name.substr(0, 20);
|
|
}
|
|
}
|
|
|
|
err = esp_ble_gap_set_device_name(name.c_str());
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ble_gap_set_device_name failed: %d", err);
|
|
return false;
|
|
}
|
|
|
|
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
|
|
err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ble_gap_set_security_param failed: %d", err);
|
|
return false;
|
|
}
|
|
|
|
// BLE takes some time to be fully set up, 200ms should be more than enough
|
|
delay(200); // NOLINT
|
|
|
|
return true;
|
|
}
|
|
|
|
void ESP32BLE::loop() {
|
|
BLEEvent *ble_event = this->ble_events_.pop();
|
|
while (ble_event != nullptr) {
|
|
switch (ble_event->type_) {
|
|
case BLEEvent::GATTS:
|
|
this->real_gatts_event_handler_(ble_event->event_.gatts.gatts_event, ble_event->event_.gatts.gatts_if,
|
|
&ble_event->event_.gatts.gatts_param);
|
|
break;
|
|
case BLEEvent::GAP:
|
|
this->real_gap_event_handler_(ble_event->event_.gap.gap_event, &ble_event->event_.gap.gap_param);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
delete ble_event; // NOLINT(cppcoreguidelines-owning-memory)
|
|
ble_event = this->ble_events_.pop();
|
|
}
|
|
}
|
|
|
|
void ESP32BLE::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
|
|
BLEEvent *new_event = new BLEEvent(event, param); // NOLINT(cppcoreguidelines-owning-memory)
|
|
global_ble->ble_events_.push(new_event);
|
|
} // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
|
|
|
|
void ESP32BLE::real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
|
|
ESP_LOGV(TAG, "(BLE) gap_event_handler - %d", event);
|
|
switch (event) {
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ESP32BLE::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
|
esp_ble_gatts_cb_param_t *param) {
|
|
BLEEvent *new_event = new BLEEvent(event, gatts_if, param); // NOLINT(cppcoreguidelines-owning-memory)
|
|
global_ble->ble_events_.push(new_event);
|
|
} // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
|
|
|
|
void ESP32BLE::real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
|
esp_ble_gatts_cb_param_t *param) {
|
|
ESP_LOGV(TAG, "(BLE) gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
|
|
#ifdef USE_ESP32_BLE_SERVER
|
|
this->server_->gatts_event_handler(event, gatts_if, param);
|
|
#endif
|
|
}
|
|
|
|
void ESP32BLE::real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
|
|
esp_ble_gattc_cb_param_t *param) {
|
|
// this->client_->gattc_event_handler(event, gattc_if, param);
|
|
}
|
|
|
|
float ESP32BLE::get_setup_priority() const { return setup_priority::BLUETOOTH; }
|
|
|
|
void ESP32BLE::dump_config() { ESP_LOGCONFIG(TAG, "ESP32 BLE:"); }
|
|
|
|
ESP32BLE *global_ble = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
|
|
} // namespace esp32_ble
|
|
} // namespace esphome
|
|
|
|
#endif
|