Compare commits
10 Commits
jesserockz
...
bluetooth-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22351be1e4 | ||
|
|
e285196709 | ||
|
|
17c6bf57cd | ||
|
|
4125b48b86 | ||
|
|
6d341ce4e7 | ||
|
|
964410bd64 | ||
|
|
d72ab25d46 | ||
|
|
af755380b7 | ||
|
|
04db724295 | ||
|
|
863bee28d9 |
@@ -230,6 +230,7 @@ BluetoothConnection *BluetoothProxy::get_connection_(uint64_t address, bool rese
|
||||
}
|
||||
|
||||
void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest &msg) {
|
||||
ESP_LOGD(TAG, "Event received: %d", msg.request_type);
|
||||
switch (msg.request_type) {
|
||||
case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITH_CACHE:
|
||||
case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE:
|
||||
|
||||
@@ -12,7 +12,7 @@ std::string DebugComponent::get_reset_reason_() { return lt_get_reboot_reason_na
|
||||
uint32_t DebugComponent::get_free_heap_() { return lt_heap_get_free(); }
|
||||
|
||||
void DebugComponent::get_device_info_(std::string &device_info) {
|
||||
reset_reason = get_reset_reason_();
|
||||
str::string reset_reason = get_reset_reason_();
|
||||
ESP_LOGD(TAG, "LibreTiny Version: %s", lt_get_version());
|
||||
ESP_LOGD(TAG, "Chip: %s (%04x) @ %u MHz", lt_cpu_get_model_name(), lt_cpu_get_model(), lt_cpu_get_freq_mhz());
|
||||
ESP_LOGD(TAG, "Chip ID: 0x%06X", lt_cpu_get_mac_id());
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cinttypes>
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -31,15 +31,14 @@ ESPBTUUID ESPBTUUID::from_raw(const uint8_t *data) {
|
||||
memcpy(ret.uuid_.uuid.uuid128, data, ESP_UUID_LEN_128);
|
||||
return ret;
|
||||
}
|
||||
ESPBTUUID ESPBTUUID::from_raw(const std::string &data) { return ESPBTUUID::from_raw(data.data(), data.length()); }
|
||||
ESPBTUUID ESPBTUUID::from_raw(const char *data, size_t len) {
|
||||
ESPBTUUID ESPBTUUID::from_raw(const std::string &data) {
|
||||
ESPBTUUID ret;
|
||||
if (len == 4) {
|
||||
if (data.length() == 4) {
|
||||
ret.uuid_.len = ESP_UUID_LEN_16;
|
||||
ret.uuid_.uuid.uuid16 = 0;
|
||||
for (int i = 0; i < len;) {
|
||||
uint8_t msb = data[i];
|
||||
uint8_t lsb = data[i + 1];
|
||||
for (int i = 0; i < data.length();) {
|
||||
uint8_t msb = data.c_str()[i];
|
||||
uint8_t lsb = data.c_str()[i + 1];
|
||||
|
||||
if (msb > '9')
|
||||
msb -= 7;
|
||||
@@ -48,12 +47,12 @@ ESPBTUUID ESPBTUUID::from_raw(const char *data, size_t len) {
|
||||
ret.uuid_.uuid.uuid16 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << (2 - i) * 4;
|
||||
i += 2;
|
||||
}
|
||||
} else if (len == 8) {
|
||||
} else if (data.length() == 8) {
|
||||
ret.uuid_.len = ESP_UUID_LEN_32;
|
||||
ret.uuid_.uuid.uuid32 = 0;
|
||||
for (int i = 0; i < len;) {
|
||||
uint8_t msb = data[i];
|
||||
uint8_t lsb = data[i + 1];
|
||||
for (int i = 0; i < data.length();) {
|
||||
uint8_t msb = data.c_str()[i];
|
||||
uint8_t lsb = data.c_str()[i + 1];
|
||||
|
||||
if (msb > '9')
|
||||
msb -= 7;
|
||||
@@ -62,20 +61,20 @@ ESPBTUUID ESPBTUUID::from_raw(const char *data, size_t len) {
|
||||
ret.uuid_.uuid.uuid32 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << (6 - i) * 4;
|
||||
i += 2;
|
||||
}
|
||||
} else if (len == 16) { // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be
|
||||
// investigated (lack of time)
|
||||
} else if (data.length() == 16) { // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be
|
||||
// investigated (lack of time)
|
||||
ret.uuid_.len = ESP_UUID_LEN_128;
|
||||
memcpy(ret.uuid_.uuid.uuid128, (uint8_t *) data, 16);
|
||||
} else if (len == 36) {
|
||||
memcpy(ret.uuid_.uuid.uuid128, (uint8_t *) data.data(), 16);
|
||||
} else if (data.length() == 36) {
|
||||
// If the length of the string is 36 bytes then we will assume it is a long hex string in
|
||||
// UUID format.
|
||||
ret.uuid_.len = ESP_UUID_LEN_128;
|
||||
int n = 0;
|
||||
for (int i = 0; i < len;) {
|
||||
if (data[i] == '-')
|
||||
for (int i = 0; i < data.length();) {
|
||||
if (data.c_str()[i] == '-')
|
||||
i++;
|
||||
uint8_t msb = data[i];
|
||||
uint8_t lsb = data[i + 1];
|
||||
uint8_t msb = data.c_str()[i];
|
||||
uint8_t lsb = data.c_str()[i + 1];
|
||||
|
||||
if (msb > '9')
|
||||
msb -= 7;
|
||||
@@ -85,7 +84,7 @@ ESPBTUUID ESPBTUUID::from_raw(const char *data, size_t len) {
|
||||
i += 2;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "ERROR: UUID value not 2, 4, 16 or 36 bytes - %s", data);
|
||||
ESP_LOGE(TAG, "ERROR: UUID value not 2, 4, 16 or 36 bytes - %s", data.c_str());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -163,19 +162,14 @@ bool ESPBTUUID::operator==(const ESPBTUUID &uuid) const {
|
||||
return false;
|
||||
}
|
||||
esp_bt_uuid_t ESPBTUUID::get_uuid() const { return this->uuid_; }
|
||||
std::string ESPBTUUID::to_string() {
|
||||
if (!this->string_.empty())
|
||||
return this->string_;
|
||||
|
||||
std::string ESPBTUUID::to_string() const {
|
||||
switch (this->uuid_.len) {
|
||||
case ESP_UUID_LEN_16:
|
||||
this->string_ = str_snprintf("0x%02X%02X", 6, this->uuid_.uuid.uuid16 >> 8, this->uuid_.uuid.uuid16 & 0xff);
|
||||
return this->string_;
|
||||
return str_snprintf("0x%02X%02X", 6, this->uuid_.uuid.uuid16 >> 8, this->uuid_.uuid.uuid16 & 0xff);
|
||||
case ESP_UUID_LEN_32:
|
||||
this->string_ = str_snprintf("0x%02" PRIX32 "%02" PRIX32 "%02" PRIX32 "%02" PRIX32, 10,
|
||||
(this->uuid_.uuid.uuid32 >> 24), (this->uuid_.uuid.uuid32 >> 16 & 0xff),
|
||||
(this->uuid_.uuid.uuid32 >> 8 & 0xff), this->uuid_.uuid.uuid32 & 0xff);
|
||||
return this->string_;
|
||||
return str_snprintf("0x%02" PRIX32 "%02" PRIX32 "%02" PRIX32 "%02" PRIX32, 10, (this->uuid_.uuid.uuid32 >> 24),
|
||||
(this->uuid_.uuid.uuid32 >> 16 & 0xff), (this->uuid_.uuid.uuid32 >> 8 & 0xff),
|
||||
this->uuid_.uuid.uuid32 & 0xff);
|
||||
default:
|
||||
case ESP_UUID_LEN_128:
|
||||
std::string buf;
|
||||
@@ -184,7 +178,6 @@ std::string ESPBTUUID::to_string() {
|
||||
if (i == 6 || i == 8 || i == 10 || i == 12)
|
||||
buf += "-";
|
||||
}
|
||||
this->string_ = buf;
|
||||
return buf;
|
||||
}
|
||||
return "";
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <esp_bt_defs.h>
|
||||
#include <string>
|
||||
#include <esp_bt_defs.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_ble {
|
||||
@@ -23,8 +23,6 @@ class ESPBTUUID {
|
||||
|
||||
static ESPBTUUID from_raw(const std::string &data);
|
||||
|
||||
static ESPBTUUID from_raw(const char *uuid, size_t len);
|
||||
|
||||
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid);
|
||||
|
||||
ESPBTUUID as_128bit() const;
|
||||
@@ -36,11 +34,10 @@ class ESPBTUUID {
|
||||
|
||||
esp_bt_uuid_t get_uuid() const;
|
||||
|
||||
std::string to_string();
|
||||
std::string to_string() const;
|
||||
|
||||
protected:
|
||||
esp_bt_uuid_t uuid_;
|
||||
std::string string_;
|
||||
};
|
||||
|
||||
} // namespace esp32_ble
|
||||
|
||||
@@ -8,9 +8,6 @@ namespace esp32_ble_server {
|
||||
|
||||
BLE2901::BLE2901(const std::string &value) : BLE2901((uint8_t *) value.data(), value.length()) {}
|
||||
BLE2901::BLE2901(const uint8_t *data, size_t length) : BLEDescriptor(esp32_ble::ESPBTUUID::from_uint16(0x2901)) {
|
||||
if (this->state_ == FAILED) {
|
||||
return;
|
||||
}
|
||||
this->set_value(data, length);
|
||||
this->permissions_ = ESP_GATT_PERM_READ;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "ble_2902.h"
|
||||
#include "esphome/components/esp32_ble/ble_uuid.h"
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <cstring>
|
||||
@@ -11,9 +9,6 @@ namespace esphome {
|
||||
namespace esp32_ble_server {
|
||||
|
||||
BLE2902::BLE2902() : BLEDescriptor(esp32_ble::ESPBTUUID::from_uint16(0x2902)) {
|
||||
if (this->state_ == FAILED) {
|
||||
return;
|
||||
}
|
||||
this->value_.attr_len = 2;
|
||||
uint8_t data[2] = {0, 0};
|
||||
memcpy(this->value_.attr_value, data, 2);
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
#include "ble_characteristic.h"
|
||||
#include "ble_2901.h"
|
||||
#include "ble_2902.h"
|
||||
#include "ble_server.h"
|
||||
#include "ble_service.h"
|
||||
|
||||
@@ -14,17 +12,14 @@ namespace esp32_ble_server {
|
||||
static const char *const TAG = "esp32_ble_server.characteristic";
|
||||
|
||||
BLECharacteristic::~BLECharacteristic() {
|
||||
this->descriptors_.clear();
|
||||
for (auto *descriptor : this->descriptors_) {
|
||||
delete descriptor; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
vSemaphoreDelete(this->set_value_lock_);
|
||||
}
|
||||
|
||||
BLECharacteristic::BLECharacteristic(const ESPBTUUID uuid, uint32_t properties) : uuid_(uuid) {
|
||||
this->set_value_lock_ = xSemaphoreCreateBinary();
|
||||
if (this->set_value_lock_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create set_value_lock_ semaphore");
|
||||
this->state_ = FAILED;
|
||||
return;
|
||||
}
|
||||
xSemaphoreGive(this->set_value_lock_);
|
||||
|
||||
this->properties_ = (esp_gatt_char_prop_t) 0;
|
||||
@@ -108,36 +103,14 @@ void BLECharacteristic::notify(bool notification) {
|
||||
}
|
||||
}
|
||||
|
||||
void BLECharacteristic::add_descriptor(std::shared_ptr<BLEDescriptor> descriptor) {
|
||||
this->descriptors_.push_back(descriptor);
|
||||
}
|
||||
void BLECharacteristic::add_descriptor(BLEDescriptor *descriptor) { this->descriptors_.push_back(descriptor); }
|
||||
|
||||
void BLECharacteristic::remove_descriptor(std::shared_ptr<BLEDescriptor> descriptor) {
|
||||
void BLECharacteristic::remove_descriptor(BLEDescriptor *descriptor) {
|
||||
this->descriptors_.erase(std::remove(this->descriptors_.begin(), this->descriptors_.end(), descriptor),
|
||||
this->descriptors_.end());
|
||||
}
|
||||
|
||||
std::shared_ptr<BLE2901> BLECharacteristic::make_2901_descriptor(const std::string &value) {
|
||||
auto descriptor = std::make_shared<BLE2901>(value);
|
||||
if (descriptor == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to allocate BLE2901 descriptor");
|
||||
return nullptr;
|
||||
}
|
||||
this->add_descriptor(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
std::shared_ptr<BLE2902> BLECharacteristic::make_2902_descriptor() {
|
||||
auto descriptor = std::make_shared<BLE2902>();
|
||||
if (descriptor == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to allocate BLE2902 descriptor");
|
||||
return nullptr;
|
||||
}
|
||||
this->add_descriptor(descriptor);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
void BLECharacteristic::do_create(std::shared_ptr<BLEService> service) {
|
||||
void BLECharacteristic::do_create(BLEService *service) {
|
||||
this->service_ = service;
|
||||
esp_attr_control_t control;
|
||||
control.auto_rsp = ESP_GATT_RSP_BY_APP;
|
||||
@@ -164,7 +137,7 @@ bool BLECharacteristic::is_created() {
|
||||
return false;
|
||||
|
||||
bool created = true;
|
||||
for (auto descriptor : this->descriptors_) {
|
||||
for (auto *descriptor : this->descriptors_) {
|
||||
created &= descriptor->is_created();
|
||||
}
|
||||
if (created)
|
||||
@@ -177,7 +150,7 @@ bool BLECharacteristic::is_failed() {
|
||||
return true;
|
||||
|
||||
bool failed = false;
|
||||
for (auto descriptor : this->descriptors_) {
|
||||
for (auto *descriptor : this->descriptors_) {
|
||||
failed |= descriptor->is_failed();
|
||||
}
|
||||
if (failed)
|
||||
@@ -235,8 +208,8 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
|
||||
if (this->uuid_ == ESPBTUUID::from_uuid(param->add_char.char_uuid)) {
|
||||
this->handle_ = param->add_char.attr_handle;
|
||||
|
||||
for (auto descriptor : this->descriptors_) {
|
||||
descriptor->do_create(shared_from_this());
|
||||
for (auto *descriptor : this->descriptors_) {
|
||||
descriptor->do_create(this);
|
||||
}
|
||||
|
||||
this->state_ = CREATING_DEPENDENTS;
|
||||
@@ -340,7 +313,7 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
|
||||
break;
|
||||
}
|
||||
|
||||
for (auto descriptor : this->descriptors_) {
|
||||
for (auto *descriptor : this->descriptors_) {
|
||||
descriptor->gatts_event_handler(event, gatts_if, param);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "ble_2901.h"
|
||||
#include "ble_2902.h"
|
||||
#include "ble_descriptor.h"
|
||||
|
||||
#include "esphome/components/esp32_ble/ble_uuid.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <esp_bt_defs.h>
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <esp_gatt_defs.h>
|
||||
#include <esp_gattc_api.h>
|
||||
#include <esp_gatts_api.h>
|
||||
#include <esp_bt_defs.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
@@ -26,7 +22,7 @@ using namespace esp32_ble;
|
||||
|
||||
class BLEService;
|
||||
|
||||
class BLECharacteristic : public std::enable_shared_from_this<BLECharacteristic> {
|
||||
class BLECharacteristic {
|
||||
public:
|
||||
BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
|
||||
~BLECharacteristic();
|
||||
@@ -51,18 +47,15 @@ class BLECharacteristic : public std::enable_shared_from_this<BLECharacteristic>
|
||||
|
||||
void notify(bool notification = true);
|
||||
|
||||
void do_create(std::shared_ptr<BLEService> service);
|
||||
void do_create(BLEService *service);
|
||||
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
|
||||
void on_write(const std::function<void(const std::vector<uint8_t> &)> &&func) { this->on_write_ = func; }
|
||||
|
||||
void add_descriptor(std::shared_ptr<BLEDescriptor> descriptor);
|
||||
void remove_descriptor(std::shared_ptr<BLEDescriptor> descriptor);
|
||||
void add_descriptor(BLEDescriptor *descriptor);
|
||||
void remove_descriptor(BLEDescriptor *descriptor);
|
||||
|
||||
std::shared_ptr<BLE2901> make_2901_descriptor(const std::string &value);
|
||||
std::shared_ptr<BLE2902> make_2902_descriptor();
|
||||
|
||||
std::shared_ptr<BLEService> get_service() { return this->service_; }
|
||||
BLEService *get_service() { return this->service_; }
|
||||
ESPBTUUID get_uuid() { return this->uuid_; }
|
||||
std::vector<uint8_t> &get_value() { return this->value_; }
|
||||
|
||||
@@ -78,7 +71,7 @@ class BLECharacteristic : public std::enable_shared_from_this<BLECharacteristic>
|
||||
|
||||
protected:
|
||||
bool write_event_{false};
|
||||
std::shared_ptr<BLEService> service_;
|
||||
BLEService *service_;
|
||||
ESPBTUUID uuid_;
|
||||
esp_gatt_char_prop_t properties_;
|
||||
uint16_t handle_{0xFFFF};
|
||||
@@ -87,7 +80,7 @@ class BLECharacteristic : public std::enable_shared_from_this<BLECharacteristic>
|
||||
std::vector<uint8_t> value_;
|
||||
SemaphoreHandle_t set_value_lock_;
|
||||
|
||||
std::vector<std::shared_ptr<BLEDescriptor>> descriptors_;
|
||||
std::vector<BLEDescriptor *> descriptors_;
|
||||
|
||||
std::function<void(const std::vector<uint8_t> &)> on_write_;
|
||||
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
#include "ble_descriptor.h"
|
||||
#include "ble_characteristic.h"
|
||||
#include "ble_service.h"
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#ifdef USE_ESP32
|
||||
@@ -20,23 +16,12 @@ BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len) {
|
||||
this->uuid_ = uuid;
|
||||
this->value_.attr_len = 0;
|
||||
this->value_.attr_max_len = max_len;
|
||||
|
||||
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||
this->value_.attr_value = allocator.allocate(max_len);
|
||||
if (this->value_.attr_value == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to allocate %d bytes for value", max_len);
|
||||
this->state_ = FAILED;
|
||||
return;
|
||||
}
|
||||
this->value_.attr_value = (uint8_t *) malloc(max_len); // NOLINT
|
||||
}
|
||||
|
||||
BLEDescriptor::~BLEDescriptor() {
|
||||
ExternalRAMAllocator<uint8_t> deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||
deallocator.deallocate(this->value_.attr_value, this->value_.attr_max_len);
|
||||
this->value_.attr_value = nullptr;
|
||||
}
|
||||
BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
|
||||
|
||||
void BLEDescriptor::do_create(std::shared_ptr<BLECharacteristic> characteristic) {
|
||||
void BLEDescriptor::do_create(BLECharacteristic *characteristic) {
|
||||
this->characteristic_ = characteristic;
|
||||
esp_attr_control_t control;
|
||||
control.auto_rsp = ESP_GATT_AUTO_RSP;
|
||||
|
||||
@@ -18,7 +18,7 @@ class BLEDescriptor {
|
||||
public:
|
||||
BLEDescriptor(ESPBTUUID uuid, uint16_t max_len = 100);
|
||||
virtual ~BLEDescriptor();
|
||||
void do_create(std::shared_ptr<BLECharacteristic> characteristic);
|
||||
void do_create(BLECharacteristic *characteristic);
|
||||
|
||||
void set_value(const std::string &value);
|
||||
void set_value(const uint8_t *data, size_t length);
|
||||
@@ -29,7 +29,7 @@ class BLEDescriptor {
|
||||
bool is_failed() { return this->state_ == FAILED; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<BLECharacteristic> characteristic_{nullptr};
|
||||
BLECharacteristic *characteristic_{nullptr};
|
||||
ESPBTUUID uuid_;
|
||||
uint16_t handle_{0xFFFF};
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
#include "ble_server.h"
|
||||
|
||||
#include "esphome/components/esp32_ble/ble.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/version.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <esp_bt.h>
|
||||
#include <esp_bt_main.h>
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <freertos/FreeRTOSConfig.h>
|
||||
#include <freertos/task.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <freertos/FreeRTOSConfig.h>
|
||||
#include <esp_bt_main.h>
|
||||
#include <esp_bt.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_gap_ble_api.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_ble_server {
|
||||
@@ -58,8 +58,9 @@ void BLEServer::loop() {
|
||||
pair.second->do_create(this);
|
||||
}
|
||||
if (this->device_information_service_ == nullptr) {
|
||||
this->create_service(ESPBTUUID::from_uint16(DEVICE_INFORMATION_SERVICE_UUID));
|
||||
this->device_information_service_ =
|
||||
this->create_service(ESPBTUUID::from_uint16(DEVICE_INFORMATION_SERVICE_UUID));
|
||||
this->get_service(ESPBTUUID::from_uint16(DEVICE_INFORMATION_SERVICE_UUID));
|
||||
this->create_device_characteristics_();
|
||||
}
|
||||
this->state_ = STARTING_SERVICE;
|
||||
@@ -93,58 +94,56 @@ void BLEServer::restart_advertising_() {
|
||||
}
|
||||
|
||||
bool BLEServer::create_device_characteristics_() {
|
||||
std::shared_ptr<BLECharacteristic> model =
|
||||
this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
if (this->model_.has_value()) {
|
||||
BLECharacteristic *model =
|
||||
this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
model->set_value(this->model_.value());
|
||||
} else {
|
||||
BLECharacteristic *model =
|
||||
this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
model->set_value(ESPHOME_BOARD);
|
||||
}
|
||||
|
||||
std::shared_ptr<BLECharacteristic> version =
|
||||
BLECharacteristic *version =
|
||||
this->device_information_service_->create_characteristic(VERSION_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
version->set_value("ESPHome " ESPHOME_VERSION);
|
||||
|
||||
std::shared_ptr<BLECharacteristic> manufacturer =
|
||||
BLECharacteristic *manufacturer =
|
||||
this->device_information_service_->create_characteristic(MANUFACTURER_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
manufacturer->set_value(this->manufacturer_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<BLEService> BLEServer::create_service(ESPBTUUID uuid, bool advertise, uint16_t num_handles,
|
||||
uint8_t inst_id) {
|
||||
std::string uuid_str = uuid.to_string();
|
||||
void BLEServer::create_service(ESPBTUUID uuid, bool advertise, uint16_t num_handles, uint8_t inst_id) {
|
||||
ESP_LOGV(TAG, "Creating BLE service - %s", uuid.to_string().c_str());
|
||||
// If the service already exists, do nothing
|
||||
std::shared_ptr<BLEService> service = this->get_service(uuid);
|
||||
BLEService *service = this->get_service(uuid);
|
||||
if (service != nullptr) {
|
||||
ESP_LOGW(TAG, "BLE service %s already exists", uuid_str.c_str());
|
||||
return service;
|
||||
ESP_LOGW(TAG, "BLE service %s already exists", uuid.to_string().c_str());
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Creating BLE service - %s", uuid_str.c_str());
|
||||
service = std::make_shared<BLEService>(uuid, num_handles, inst_id, advertise);
|
||||
this->services_.emplace(uuid_str, service);
|
||||
service = new BLEService(uuid, num_handles, inst_id, advertise); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->services_.emplace(uuid.to_string(), service);
|
||||
service->do_create(this);
|
||||
return service;
|
||||
}
|
||||
|
||||
void BLEServer::remove_service(ESPBTUUID uuid) {
|
||||
std::string uuid_str = uuid.to_string();
|
||||
std::shared_ptr<BLEService> service = this->get_service(uuid);
|
||||
ESP_LOGV(TAG, "Removing BLE service - %s", uuid.to_string().c_str());
|
||||
BLEService *service = this->get_service(uuid);
|
||||
if (service == nullptr) {
|
||||
ESP_LOGW(TAG, "BLE service %s not found", uuid_str.c_str());
|
||||
ESP_LOGW(TAG, "BLE service %s not found", uuid.to_string().c_str());
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Removing BLE service - %s", uuid_str.c_str());
|
||||
service->do_delete();
|
||||
this->services_.erase(uuid_str);
|
||||
delete service; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->services_.erase(uuid.to_string());
|
||||
}
|
||||
|
||||
std::shared_ptr<BLEService> BLEServer::get_service(ESPBTUUID uuid) {
|
||||
std::string uuid_str = uuid.to_string();
|
||||
std::shared_ptr<BLEService> service = nullptr;
|
||||
if (this->services_.count(uuid_str) > 0) {
|
||||
service = this->services_.at(uuid_str);
|
||||
BLEService *BLEServer::get_service(ESPBTUUID uuid) {
|
||||
BLEService *service = nullptr;
|
||||
if (this->services_.count(uuid.to_string()) > 0) {
|
||||
service = this->services_.at(uuid.to_string());
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "ble_characteristic.h"
|
||||
#include "ble_service.h"
|
||||
#include "ble_characteristic.h"
|
||||
|
||||
#include "esphome/components/esp32_ble/ble.h"
|
||||
#include "esphome/components/esp32_ble/ble_advertising.h"
|
||||
@@ -12,8 +12,8 @@
|
||||
#include "esphome/core/preferences.h"
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
@@ -51,10 +51,9 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv
|
||||
this->restart_advertising_();
|
||||
}
|
||||
|
||||
std::shared_ptr<BLEService> create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15,
|
||||
uint8_t inst_id = 0);
|
||||
void create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15, uint8_t inst_id = 0);
|
||||
void remove_service(ESPBTUUID uuid);
|
||||
std::shared_ptr<BLEService> get_service(ESPBTUUID uuid);
|
||||
BLEService *get_service(ESPBTUUID uuid);
|
||||
|
||||
esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
|
||||
uint32_t get_connected_client_count() { return this->connected_clients_; }
|
||||
@@ -82,8 +81,8 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv
|
||||
|
||||
uint32_t connected_clients_{0};
|
||||
std::unordered_map<uint16_t, void *> clients_;
|
||||
std::unordered_map<std::string, std::shared_ptr<BLEService>> services_;
|
||||
std::shared_ptr<BLEService> device_information_service_;
|
||||
std::unordered_map<std::string, BLEService *> services_;
|
||||
BLEService *device_information_service_;
|
||||
|
||||
std::vector<BLEServiceComponent *> service_components_;
|
||||
|
||||
|
||||
@@ -12,33 +12,31 @@ static const char *const TAG = "esp32_ble_server.service";
|
||||
BLEService::BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
|
||||
: uuid_(uuid), num_handles_(num_handles), inst_id_(inst_id), advertise_(advertise) {}
|
||||
|
||||
std::shared_ptr<BLECharacteristic> BLEService::get_characteristic(ESPBTUUID uuid) {
|
||||
for (auto chr : this->characteristics_) {
|
||||
BLEService::~BLEService() {
|
||||
for (auto &chr : this->characteristics_)
|
||||
delete chr; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
|
||||
BLECharacteristic *BLEService::get_characteristic(ESPBTUUID uuid) {
|
||||
for (auto *chr : this->characteristics_) {
|
||||
if (chr->get_uuid() == uuid)
|
||||
return chr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<BLECharacteristic> BLEService::get_characteristic(uint16_t uuid) {
|
||||
BLECharacteristic *BLEService::get_characteristic(uint16_t uuid) {
|
||||
return this->get_characteristic(ESPBTUUID::from_uint16(uuid));
|
||||
}
|
||||
std::shared_ptr<BLECharacteristic> BLEService::create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties) {
|
||||
BLECharacteristic *BLEService::create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties) {
|
||||
return create_characteristic(ESPBTUUID::from_uint16(uuid), properties);
|
||||
}
|
||||
std::shared_ptr<BLECharacteristic> BLEService::create_characteristic(const std::string &uuid,
|
||||
esp_gatt_char_prop_t properties) {
|
||||
BLECharacteristic *BLEService::create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties) {
|
||||
return create_characteristic(ESPBTUUID::from_raw(uuid), properties);
|
||||
}
|
||||
std::shared_ptr<BLECharacteristic> BLEService::create_characteristic(const char *uuid, size_t len,
|
||||
esp_gatt_char_prop_t properties) {
|
||||
return create_characteristic(ESPBTUUID::from_raw(uuid, len), properties);
|
||||
}
|
||||
std::shared_ptr<BLECharacteristic> BLEService::create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties) {
|
||||
auto characteristic = std::make_shared<BLECharacteristic>(uuid, properties);
|
||||
if (characteristic == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
BLECharacteristic *BLEService::create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties) {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
|
||||
BLECharacteristic *characteristic = new BLECharacteristic(uuid, properties);
|
||||
this->characteristics_.push_back(characteristic);
|
||||
return characteristic;
|
||||
}
|
||||
@@ -82,9 +80,9 @@ bool BLEService::do_create_characteristics_() {
|
||||
if (this->last_created_characteristic_ != nullptr && !this->last_created_characteristic_->is_created())
|
||||
return true; // Signifies that the previous characteristic is still being created.
|
||||
|
||||
auto characteristic = this->characteristics_[this->created_characteristic_count_++];
|
||||
auto *characteristic = this->characteristics_[this->created_characteristic_count_++];
|
||||
this->last_created_characteristic_ = characteristic;
|
||||
characteristic->do_create(shared_from_this());
|
||||
characteristic->do_create(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -126,7 +124,7 @@ bool BLEService::is_failed() {
|
||||
if (this->init_state_ == FAILED)
|
||||
return true;
|
||||
bool failed = false;
|
||||
for (auto characteristic : this->characteristics_)
|
||||
for (auto *characteristic : this->characteristics_)
|
||||
failed |= characteristic->is_failed();
|
||||
|
||||
if (failed)
|
||||
@@ -168,7 +166,7 @@ void BLEService::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t g
|
||||
break;
|
||||
}
|
||||
|
||||
for (auto characteristic : this->characteristics_) {
|
||||
for (auto *characteristic : this->characteristics_) {
|
||||
characteristic->gatts_event_handler(event, gatts_if, param);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "ble_characteristic.h"
|
||||
#include "esphome/components/esp32_ble/ble_uuid.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_ESP32
|
||||
@@ -21,21 +20,19 @@ class BLEServer;
|
||||
|
||||
using namespace esp32_ble;
|
||||
|
||||
class BLEService : public std::enable_shared_from_this<BLEService> {
|
||||
class BLEService {
|
||||
public:
|
||||
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise);
|
||||
~BLEService();
|
||||
BLECharacteristic *get_characteristic(ESPBTUUID uuid);
|
||||
BLECharacteristic *get_characteristic(uint16_t uuid);
|
||||
|
||||
std::shared_ptr<BLECharacteristic> get_characteristic(ESPBTUUID uuid);
|
||||
std::shared_ptr<BLECharacteristic> get_characteristic(uint16_t uuid);
|
||||
|
||||
std::shared_ptr<BLECharacteristic> create_characteristic(const char *uuid, size_t len,
|
||||
esp_gatt_char_prop_t properties);
|
||||
std::shared_ptr<BLECharacteristic> create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties);
|
||||
std::shared_ptr<BLECharacteristic> create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties);
|
||||
std::shared_ptr<BLECharacteristic> create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties);
|
||||
BLECharacteristic *create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties);
|
||||
BLECharacteristic *create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties);
|
||||
BLECharacteristic *create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties);
|
||||
|
||||
ESPBTUUID get_uuid() { return this->uuid_; }
|
||||
std::shared_ptr<BLECharacteristic> get_last_created_characteristic() { return this->last_created_characteristic_; }
|
||||
BLECharacteristic *get_last_created_characteristic() { return this->last_created_characteristic_; }
|
||||
uint16_t get_handle() { return this->handle_; }
|
||||
|
||||
BLEServer *get_server() { return this->server_; }
|
||||
@@ -55,8 +52,8 @@ class BLEService : public std::enable_shared_from_this<BLEService> {
|
||||
bool is_deleted() { return this->init_state_ == DELETED; }
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<BLECharacteristic>> characteristics_;
|
||||
std::shared_ptr<BLECharacteristic> last_created_characteristic_{nullptr};
|
||||
std::vector<BLECharacteristic *> characteristics_;
|
||||
BLECharacteristic *last_created_characteristic_{nullptr};
|
||||
uint32_t created_characteristic_count_{0};
|
||||
BLEServer *server_;
|
||||
ESPBTUUID uuid_;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#include "esp32_improv_component.h"
|
||||
|
||||
#include "esphome/components/esp32_ble/ble.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_2902.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
static constexpr size_t MAX_UUID_LENGTH = 37;
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_improv {
|
||||
|
||||
@@ -29,72 +28,35 @@ void ESP32ImprovComponent::setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ESP32ImprovComponent::setup_characteristics() {
|
||||
this->status_ =
|
||||
this->service_->create_characteristic(improv::STATUS_UUID, strnlen(improv::STATUS_UUID, MAX_UUID_LENGTH),
|
||||
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
if (this->status_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create status characteristic");
|
||||
return false;
|
||||
}
|
||||
void ESP32ImprovComponent::setup_characteristics() {
|
||||
this->status_ = this->service_->create_characteristic(
|
||||
improv::STATUS_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *status_descriptor = new BLE2902();
|
||||
this->status_->add_descriptor(status_descriptor);
|
||||
|
||||
if (!this->status_->make_2902_descriptor()) {
|
||||
ESP_LOGE(TAG, "Failed to create status descriptor");
|
||||
return false;
|
||||
}
|
||||
this->error_ = this->service_->create_characteristic(
|
||||
improv::ERROR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *error_descriptor = new BLE2902();
|
||||
this->error_->add_descriptor(error_descriptor);
|
||||
|
||||
this->error_ =
|
||||
this->service_->create_characteristic(improv::ERROR_UUID, strnlen(improv::ERROR_UUID, MAX_UUID_LENGTH),
|
||||
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
if (this->error_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create error characteristic");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this->error_->make_2902_descriptor()) {
|
||||
ESP_LOGE(TAG, "Failed to create error descriptor");
|
||||
return false;
|
||||
}
|
||||
|
||||
this->rpc_ = this->service_->create_characteristic(
|
||||
improv::RPC_COMMAND_UUID, strnlen(improv::RPC_COMMAND_UUID, MAX_UUID_LENGTH), BLECharacteristic::PROPERTY_WRITE);
|
||||
if (this->rpc_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create rpc characteristic");
|
||||
return false;
|
||||
}
|
||||
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.empty()) {
|
||||
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
|
||||
}
|
||||
});
|
||||
if (this->rpc_->make_2902_descriptor() == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create rpc descriptor");
|
||||
return false;
|
||||
}
|
||||
BLEDescriptor *rpc_descriptor = new BLE2902();
|
||||
this->rpc_->add_descriptor(rpc_descriptor);
|
||||
|
||||
this->rpc_response_ =
|
||||
this->service_->create_characteristic(improv::RPC_RESULT_UUID, strnlen(improv::RPC_RESULT_UUID, MAX_UUID_LENGTH),
|
||||
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
if (this->rpc_response_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create rpc response characteristic");
|
||||
return false;
|
||||
}
|
||||
if (!this->rpc_response_->make_2902_descriptor()) {
|
||||
ESP_LOGE(TAG, "Failed to create rpc response descriptor");
|
||||
return false;
|
||||
}
|
||||
|
||||
this->capabilities_ = this->service_->create_characteristic(
|
||||
improv::CAPABILITIES_UUID, strnlen(improv::CAPABILITIES_UUID, MAX_UUID_LENGTH), BLECharacteristic::PROPERTY_READ);
|
||||
if (this->capabilities_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create capabilities characteristic");
|
||||
return false;
|
||||
}
|
||||
if (!this->capabilities_->make_2902_descriptor()) {
|
||||
ESP_LOGE(TAG, "Failed to create capabilities descriptor");
|
||||
return false;
|
||||
}
|
||||
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, BLECharacteristic::PROPERTY_READ);
|
||||
BLEDescriptor *capabilities_descriptor = new BLE2902();
|
||||
this->capabilities_->add_descriptor(capabilities_descriptor);
|
||||
uint8_t capabilities = 0x00;
|
||||
#ifdef USE_OUTPUT
|
||||
if (this->status_indicator_ != nullptr)
|
||||
@@ -102,7 +64,6 @@ bool ESP32ImprovComponent::setup_characteristics() {
|
||||
#endif
|
||||
this->capabilities_->set_value(capabilities);
|
||||
this->setup_complete_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ESP32ImprovComponent::loop() {
|
||||
@@ -114,11 +75,9 @@ void ESP32ImprovComponent::loop() {
|
||||
if (this->service_ == nullptr) {
|
||||
// Setup the service
|
||||
ESP_LOGD(TAG, "Creating Improv service");
|
||||
this->service_ = global_ble_server->create_service(ESPBTUUID::from_raw(improv::SERVICE_UUID), true);
|
||||
if (!this->setup_characteristics()) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
global_ble_server->create_service(ESPBTUUID::from_raw(improv::SERVICE_UUID), true);
|
||||
this->service_ = global_ble_server->get_service(ESPBTUUID::from_raw(improv::SERVICE_UUID));
|
||||
this->setup_characteristics();
|
||||
}
|
||||
|
||||
if (!this->incoming_data_.empty())
|
||||
|
||||
@@ -34,7 +34,7 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
|
||||
void dump_config() override;
|
||||
void loop() override;
|
||||
void setup() override;
|
||||
bool setup_characteristics();
|
||||
void setup_characteristics();
|
||||
void on_client_disconnect() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
@@ -68,12 +68,12 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
|
||||
std::vector<uint8_t> incoming_data_;
|
||||
wifi::WiFiAP connecting_sta_;
|
||||
|
||||
std::shared_ptr<BLEService> service_{nullptr};
|
||||
std::shared_ptr<BLECharacteristic> status_;
|
||||
std::shared_ptr<BLECharacteristic> error_;
|
||||
std::shared_ptr<BLECharacteristic> rpc_;
|
||||
std::shared_ptr<BLECharacteristic> rpc_response_;
|
||||
std::shared_ptr<BLECharacteristic> capabilities_;
|
||||
BLEService *service_ = nullptr;
|
||||
BLECharacteristic *status_;
|
||||
BLECharacteristic *error_;
|
||||
BLECharacteristic *rpc_;
|
||||
BLECharacteristic *rpc_response_;
|
||||
BLECharacteristic *capabilities_;
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
binary_sensor::BinarySensor *authorizer_{nullptr};
|
||||
|
||||
@@ -269,6 +269,30 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff
|
||||
ESP_LOGV(TAG, "Network status requested, reported as %i", wifi_status);
|
||||
break;
|
||||
}
|
||||
case TuyaCommandType::EXTENDED_SERVICES: {
|
||||
uint8_t subcommand = buffer[0];
|
||||
switch ((TuyaExtendedServicesCommandType) subcommand) {
|
||||
case TuyaExtendedServicesCommandType::RESET_NOTIFICATION: {
|
||||
this->send_command_(
|
||||
TuyaCommand{.cmd = TuyaCommandType::EXTENDED_SERVICES,
|
||||
.payload = std::vector<uint8_t>{
|
||||
static_cast<uint8_t>(TuyaExtendedServicesCommandType::RESET_NOTIFICATION), 0x00}});
|
||||
ESP_LOGV(TAG, "Reset status notification enabled");
|
||||
break;
|
||||
}
|
||||
case TuyaExtendedServicesCommandType::MODULE_RESET: {
|
||||
ESP_LOGE(TAG, "EXTENDED_SERVICES::MODULE_RESET is not handled");
|
||||
break;
|
||||
}
|
||||
case TuyaExtendedServicesCommandType::UPDATE_IN_PROGRESS: {
|
||||
ESP_LOGE(TAG, "EXTENDED_SERVICES::UPDATE_IN_PROGRESS is not handled");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ESP_LOGE(TAG, "Invalid extended services subcommand (0x%02X) received", subcommand);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ESP_LOGE(TAG, "Invalid command (0x%02X) received", command);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,13 @@ enum class TuyaCommandType : uint8_t {
|
||||
WIFI_RSSI = 0x24,
|
||||
VACUUM_MAP_UPLOAD = 0x28,
|
||||
GET_NETWORK_STATUS = 0x2B,
|
||||
EXTENDED_SERVICES = 0x34,
|
||||
};
|
||||
|
||||
enum class TuyaExtendedServicesCommandType : uint8_t {
|
||||
RESET_NOTIFICATION = 0x04,
|
||||
MODULE_RESET = 0x05,
|
||||
UPDATE_IN_PROGRESS = 0x0A,
|
||||
};
|
||||
|
||||
enum class TuyaInitState : uint8_t {
|
||||
|
||||
@@ -71,6 +71,12 @@ void VoiceAssistant::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up Voice Assistant...");
|
||||
|
||||
global_voice_assistant = this;
|
||||
}
|
||||
|
||||
bool VoiceAssistant::allocate_buffers_() {
|
||||
if (this->send_buffer_ != nullptr) {
|
||||
return true; // Already allocated
|
||||
}
|
||||
|
||||
#ifdef USE_SPEAKER
|
||||
if (this->speaker_ != nullptr) {
|
||||
@@ -78,8 +84,7 @@ void VoiceAssistant::setup() {
|
||||
this->speaker_buffer_ = speaker_allocator.allocate(SPEAKER_BUFFER_SIZE);
|
||||
if (this->speaker_buffer_ == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not allocate speaker buffer");
|
||||
this->mark_failed();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -88,8 +93,7 @@ void VoiceAssistant::setup() {
|
||||
this->input_buffer_ = allocator.allocate(INPUT_BUFFER_SIZE);
|
||||
if (this->input_buffer_ == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not allocate input buffer");
|
||||
this->mark_failed();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef USE_ESP_ADF
|
||||
@@ -99,17 +103,71 @@ void VoiceAssistant::setup() {
|
||||
this->ring_buffer_ = RingBuffer::create(BUFFER_SIZE * sizeof(int16_t));
|
||||
if (this->ring_buffer_ == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not allocate ring buffer");
|
||||
this->mark_failed();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||
this->send_buffer_ = send_allocator.allocate(SEND_BUFFER_SIZE);
|
||||
if (send_buffer_ == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not allocate send buffer");
|
||||
this->mark_failed();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void VoiceAssistant::clear_buffers_() {
|
||||
if (this->send_buffer_ != nullptr) {
|
||||
memset(this->send_buffer_, 0, SEND_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
if (this->input_buffer_ != nullptr) {
|
||||
memset(this->input_buffer_, 0, INPUT_BUFFER_SIZE * sizeof(int16_t));
|
||||
}
|
||||
|
||||
if (this->ring_buffer_ != nullptr) {
|
||||
this->ring_buffer_->reset();
|
||||
}
|
||||
|
||||
#ifdef USE_SPEAKER
|
||||
if (this->speaker_buffer_ != nullptr) {
|
||||
memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE);
|
||||
|
||||
this->speaker_buffer_size_ = 0;
|
||||
this->speaker_buffer_index_ = 0;
|
||||
this->speaker_bytes_received_ = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void VoiceAssistant::deallocate_buffers_() {
|
||||
ExternalRAMAllocator<uint8_t> send_deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||
send_deallocator.deallocate(this->send_buffer_, SEND_BUFFER_SIZE);
|
||||
this->send_buffer_ = nullptr;
|
||||
|
||||
if (this->ring_buffer_ != nullptr) {
|
||||
this->ring_buffer_.reset();
|
||||
this->ring_buffer_ = nullptr;
|
||||
}
|
||||
|
||||
#ifdef USE_ESP_ADF
|
||||
if (this->vad_instance_ != nullptr) {
|
||||
vad_destroy(this->vad_instance_);
|
||||
this->vad_instance_ = nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
ExternalRAMAllocator<int16_t> input_deallocator(ExternalRAMAllocator<int16_t>::ALLOW_FAILURE);
|
||||
input_deallocator.deallocate(this->input_buffer_, INPUT_BUFFER_SIZE);
|
||||
this->input_buffer_ = nullptr;
|
||||
|
||||
#ifdef USE_SPEAKER
|
||||
if (this->speaker_buffer_ != nullptr) {
|
||||
ExternalRAMAllocator<uint8_t> speaker_deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||
speaker_deallocator.deallocate(this->speaker_buffer_, SPEAKER_BUFFER_SIZE);
|
||||
this->speaker_buffer_ = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int VoiceAssistant::read_microphone_() {
|
||||
@@ -138,14 +196,13 @@ void VoiceAssistant::loop() {
|
||||
}
|
||||
this->continuous_ = false;
|
||||
this->signal_stop_();
|
||||
this->clear_buffers_();
|
||||
return;
|
||||
}
|
||||
switch (this->state_) {
|
||||
case State::IDLE: {
|
||||
if (this->continuous_ && this->desired_state_ == State::IDLE) {
|
||||
this->idle_trigger_->trigger();
|
||||
|
||||
this->ring_buffer_->reset();
|
||||
#ifdef USE_ESP_ADF
|
||||
if (this->use_wake_word_) {
|
||||
this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD);
|
||||
@@ -161,8 +218,15 @@ void VoiceAssistant::loop() {
|
||||
}
|
||||
case State::START_MICROPHONE: {
|
||||
ESP_LOGD(TAG, "Starting Microphone");
|
||||
memset(this->send_buffer_, 0, SEND_BUFFER_SIZE);
|
||||
memset(this->input_buffer_, 0, INPUT_BUFFER_SIZE * sizeof(int16_t));
|
||||
if (!this->allocate_buffers_()) {
|
||||
this->status_set_error("Failed to allocate buffers");
|
||||
return;
|
||||
}
|
||||
if (this->status_has_error()) {
|
||||
this->status_clear_error();
|
||||
}
|
||||
this->clear_buffers_();
|
||||
|
||||
this->mic_->start();
|
||||
this->high_freq_.start();
|
||||
this->set_state_(State::STARTING_MICROPHONE);
|
||||
@@ -343,10 +407,9 @@ void VoiceAssistant::loop() {
|
||||
this->speaker_->stop();
|
||||
this->cancel_timeout("speaker-timeout");
|
||||
this->cancel_timeout("playing");
|
||||
this->speaker_buffer_size_ = 0;
|
||||
this->speaker_buffer_index_ = 0;
|
||||
this->speaker_bytes_received_ = 0;
|
||||
memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE);
|
||||
|
||||
this->clear_buffers_();
|
||||
|
||||
this->wait_for_stream_end_ = false;
|
||||
this->stream_ended_ = false;
|
||||
|
||||
@@ -507,7 +570,6 @@ void VoiceAssistant::request_start(bool continuous, bool silence_detection) {
|
||||
if (this->state_ == State::IDLE) {
|
||||
this->continuous_ = continuous;
|
||||
this->silence_detection_ = silence_detection;
|
||||
this->ring_buffer_->reset();
|
||||
#ifdef USE_ESP_ADF
|
||||
if (this->use_wake_word_) {
|
||||
this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD);
|
||||
|
||||
@@ -151,6 +151,10 @@ class VoiceAssistant : public Component {
|
||||
void set_wake_word(const std::string &wake_word) { this->wake_word_ = wake_word; }
|
||||
|
||||
protected:
|
||||
bool allocate_buffers_();
|
||||
void clear_buffers_();
|
||||
void deallocate_buffers_();
|
||||
|
||||
int read_microphone_();
|
||||
void set_state_(State state);
|
||||
void set_state_(State state, State desired_state);
|
||||
|
||||
@@ -48,7 +48,7 @@ WaveshareEPaper2P9InBV3 = waveshare_epaper_ns.class_(
|
||||
WaveshareEPaper2P9InV2R2 = waveshare_epaper_ns.class_(
|
||||
"WaveshareEPaper2P9InV2R2", WaveshareEPaper
|
||||
)
|
||||
GDEY029T94 = waveshare_epaper_ns.class_("GDEY029T94", WaveshareEPaper)
|
||||
GDEW029T5 = waveshare_epaper_ns.class_("GDEW029T5", WaveshareEPaper)
|
||||
WaveshareEPaper2P9InDKE = waveshare_epaper_ns.class_(
|
||||
"WaveshareEPaper2P9InDKE", WaveshareEPaper
|
||||
)
|
||||
@@ -110,7 +110,7 @@ MODELS = {
|
||||
"2.13in-ttgo-b74": ("a", WaveshareEPaperTypeAModel.TTGO_EPAPER_2_13_IN_B74),
|
||||
"2.90in": ("a", WaveshareEPaperTypeAModel.WAVESHARE_EPAPER_2_9_IN),
|
||||
"2.90inv2": ("a", WaveshareEPaperTypeAModel.WAVESHARE_EPAPER_2_9_IN_V2),
|
||||
"gdey029t94": ("c", GDEY029T94),
|
||||
"gdew029t5": ("c", GDEW029T5),
|
||||
"2.70in": ("b", WaveshareEPaper2P7In),
|
||||
"2.70in-b": ("b", WaveshareEPaper2P7InB),
|
||||
"2.70in-bv2": ("b", WaveshareEPaper2P7InBV2),
|
||||
|
||||
@@ -1514,7 +1514,7 @@ void WaveshareEPaper2P9InV2R2::set_full_update_every(uint32_t full_update_every)
|
||||
// - https://github.com/adafruit/Adafruit_EPD/blob/master/src/panels/ThinkInk_290_Grayscale4_T5.h
|
||||
// ========================================================
|
||||
|
||||
void GDEY029T94::initialize() {
|
||||
void GDEW029T5::initialize() {
|
||||
// from https://www.waveshare.com/w/upload/b/bb/2.9inch-e-paper-b-specification.pdf, page 37
|
||||
// EPD hardware init start
|
||||
this->reset_();
|
||||
@@ -1560,7 +1560,7 @@ void GDEY029T94::initialize() {
|
||||
|
||||
// EPD hardware init end
|
||||
}
|
||||
void HOT GDEY029T94::display() {
|
||||
void HOT GDEW029T5::display() {
|
||||
// COMMAND DATA START TRANSMISSION 2 (B/W only)
|
||||
this->command(0x13);
|
||||
delay(2);
|
||||
@@ -1580,11 +1580,11 @@ void HOT GDEY029T94::display() {
|
||||
// NOTE: power off < deep sleep
|
||||
this->command(0x02);
|
||||
}
|
||||
int GDEY029T94::get_width_internal() { return 128; }
|
||||
int GDEY029T94::get_height_internal() { return 296; }
|
||||
void GDEY029T94::dump_config() {
|
||||
int GDEW029T5::get_width_internal() { return 128; }
|
||||
int GDEW029T5::get_height_internal() { return 296; }
|
||||
void GDEW029T5::dump_config() {
|
||||
LOG_DISPLAY("", "Waveshare E-Paper (Good Display)", this);
|
||||
ESP_LOGCONFIG(TAG, " Model: 2.9in Greyscale GDEY029T94");
|
||||
ESP_LOGCONFIG(TAG, " Model: 2.9in Greyscale GDEW029T5");
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||
LOG_PIN(" DC Pin: ", this->dc_pin_);
|
||||
LOG_PIN(" Busy Pin: ", this->busy_pin_);
|
||||
|
||||
@@ -227,7 +227,7 @@ class WaveshareEPaper2P7InBV2 : public WaveshareEPaperBWR {
|
||||
int get_height_internal() override;
|
||||
};
|
||||
|
||||
class GDEY029T94 : public WaveshareEPaper {
|
||||
class GDEW029T5 : public WaveshareEPaper {
|
||||
public:
|
||||
void initialize() override;
|
||||
|
||||
|
||||
@@ -37,4 +37,4 @@ async def to_code(config):
|
||||
cg.add_library("FS", None)
|
||||
cg.add_library("Update", None)
|
||||
# https://github.com/esphome/ESPAsyncWebServer/blob/master/library.json
|
||||
cg.add_library("esphome/ESPAsyncWebServer-esphome", "3.2.0")
|
||||
cg.add_library("esphome/ESPAsyncWebServer-esphome", "3.2.2")
|
||||
|
||||
@@ -58,7 +58,7 @@ lib_deps =
|
||||
SPI ; spi (Arduino built-in)
|
||||
Wire ; i2c (Arduino built-int)
|
||||
heman/AsyncMqttClient-esphome@1.0.0 ; mqtt
|
||||
esphome/ESPAsyncWebServer-esphome@3.2.0 ; web_server_base
|
||||
esphome/ESPAsyncWebServer-esphome@3.2.2 ; web_server_base
|
||||
fastled/FastLED@3.3.2 ; fastled_base
|
||||
mikalhart/TinyGPSPlus@1.0.2 ; gps
|
||||
freekode/TM1651@1.0.1 ; tm1651
|
||||
|
||||
Reference in New Issue
Block a user