Changes for bytebuffer update;

set_value core function takes a vector instead of a ByteBuffer
lambda for set_value requires a vector
Sundry cleanup.
This commit is contained in:
clydebarrow 2024-11-01 12:24:56 +11:00
parent c3c49288ad
commit 2727e7bce1
9 changed files with 48 additions and 39 deletions

View File

@ -7,16 +7,16 @@ import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_ID, CONF_ID,
CONF_MODEL, CONF_MODEL,
CONF_UUID,
CONF_SERVICES,
CONF_VALUE,
CONF_NOTIFY, CONF_NOTIFY,
CONF_ON_CONNECT, CONF_ON_CONNECT,
CONF_ON_DISCONNECT, CONF_ON_DISCONNECT,
CONF_SERVICES,
CONF_UUID,
CONF_VALUE,
) )
from esphome.core import CORE from esphome.core import CORE
AUTO_LOAD = ["esp32_ble"] AUTO_LOAD = ["esp32_ble", "bytebuffer"]
CODEOWNERS = ["@jesserockz", "@clydebarrow", "@Rapsssito"] CODEOWNERS = ["@jesserockz", "@clydebarrow", "@Rapsssito"]
DEPENDENCIES = ["esp32"] DEPENDENCIES = ["esp32"]
@ -67,8 +67,9 @@ BLECharacteristicSetValueAction = esp32_ble_server_automations_ns.class_(
BLECharacteristicNotifyAction = esp32_ble_server_automations_ns.class_( BLECharacteristicNotifyAction = esp32_ble_server_automations_ns.class_(
"BLECharacteristicNotifyAction", automation.Action "BLECharacteristicNotifyAction", automation.Action
) )
ByteBuffer_ns = cg.esphome_ns.namespace("ByteBuffer") bytebuffer_ns = cg.esphome_ns.namespace("bytebuffer")
ByteBuffer = cg.esphome_ns.class_("ByteBuffer") ByteBuffer_ns = bytebuffer_ns.namespace("ByteBuffer")
ByteBuffer = bytebuffer_ns.class_("ByteBuffer")
PROPERTY_MAP = { PROPERTY_MAP = {
@ -331,8 +332,7 @@ async def parse_value(value, str_encoding, buffer_id, args, byte_length=None):
return await cg.templatable( return await cg.templatable(
value, value,
args, args,
ByteBuffer, cg.std_vector.template(cg.uint8),
ByteBuffer_ns.wrap,
) )
val, val_method = bytebuffer_parser_(value, str_encoding) val, val_method = bytebuffer_parser_(value, str_encoding)
@ -356,11 +356,11 @@ async def parse_value(value, str_encoding, buffer_id, args, byte_length=None):
def calculate_num_handles(service_config): def calculate_num_handles(service_config):
total = 1 total = 1 + len(service_config[CONF_CHARACTERISTICS]) * 2
for char_conf in service_config[CONF_CHARACTERISTICS]: total += sum(
total += 2 # One for the char_conf itself and one for the value len(char_conf[CONF_DESCRIPTORS])
for _ in char_conf[CONF_DESCRIPTORS]: for char_conf in service_config[CONF_CHARACTERISTICS]
total += 1 )
return total return total
@ -416,7 +416,7 @@ async def to_code_characteristic(service_var, char_conf):
value_action = await ble_server_characteristic_set_value( value_action = await ble_server_characteristic_set_value(
action_conf, action_conf,
char_conf[CONF_CHAR_VALUE_ACTION_ID_], char_conf[CONF_CHAR_VALUE_ACTION_ID_],
cg.TemplateArguments(None), cg.TemplateArguments(),
{}, {},
) )
cg.add(value_action.play()) cg.add(value_action.play())

View File

@ -32,11 +32,16 @@ BLECharacteristic::BLECharacteristic(const ESPBTUUID uuid, uint32_t properties)
this->set_write_no_response_property((properties & PROPERTY_WRITE_NR) != 0); this->set_write_no_response_property((properties & PROPERTY_WRITE_NR) != 0);
} }
void BLECharacteristic::set_value(ByteBuffer buffer) { void BLECharacteristic::set_value(ByteBuffer buffer) { this->set_value(buffer.get_data()); }
void BLECharacteristic::set_value(const std::vector<uint8_t> &buffer) {
xSemaphoreTake(this->set_value_lock_, 0L); xSemaphoreTake(this->set_value_lock_, 0L);
this->value_ = buffer.get_data(); this->value_ = buffer;
xSemaphoreGive(this->set_value_lock_); xSemaphoreGive(this->set_value_lock_);
} }
void BLECharacteristic::set_value(const std::string &buffer) {
this->set_value(std::vector<uint8_t>(buffer.begin(), buffer.end()));
}
void BLECharacteristic::notify() { void BLECharacteristic::notify() {
if (this->service_ == nullptr || this->service_->get_server() == nullptr || if (this->service_ == nullptr || this->service_->get_server() == nullptr ||

View File

@ -3,7 +3,7 @@
#include "ble_descriptor.h" #include "ble_descriptor.h"
#include "esphome/components/esp32_ble/ble_uuid.h" #include "esphome/components/esp32_ble/ble_uuid.h"
#include "esphome/core/event_emitter.h" #include "esphome/core/event_emitter.h"
#include "esphome/core/bytebuffer.h" #include "esphome/components/bytebuffer/bytebuffer.h"
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
@ -22,6 +22,7 @@ namespace esphome {
namespace esp32_ble_server { namespace esp32_ble_server {
using namespace esp32_ble; using namespace esp32_ble;
using namespace bytebuffer;
class BLEService; class BLEService;
@ -42,6 +43,8 @@ class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, s
~BLECharacteristic(); ~BLECharacteristic();
void set_value(ByteBuffer buffer); void set_value(ByteBuffer buffer);
void set_value(const std::vector<uint8_t> &buffer);
void set_value(const std::string &buffer);
void set_broadcast_property(bool value); void set_broadcast_property(bool value);
void set_indicate_property(bool value); void set_indicate_property(bool value);

View File

@ -2,7 +2,7 @@
#include "esphome/components/esp32_ble/ble_uuid.h" #include "esphome/components/esp32_ble/ble_uuid.h"
#include "esphome/core/event_emitter.h" #include "esphome/core/event_emitter.h"
#include "esphome/core/bytebuffer.h" #include "esphome/components/bytebuffer/bytebuffer.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
@ -13,6 +13,7 @@ namespace esphome {
namespace esp32_ble_server { namespace esp32_ble_server {
using namespace esp32_ble; using namespace esp32_ble;
using namespace bytebuffer;
class BLECharacteristic; class BLECharacteristic;

View File

@ -4,7 +4,6 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/version.h" #include "esphome/core/version.h"
#include "esphome/core/bytebuffer.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
@ -112,21 +111,20 @@ bool BLEServer::create_device_characteristics_() {
if (this->model_.has_value()) { if (this->model_.has_value()) {
BLECharacteristic *model = BLECharacteristic *model =
this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ); this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ);
model->set_value(ByteBuffer::wrap(std::vector<uint8_t>(this->model_.value().begin(), this->model_.value().end()))); model->set_value(this->model_.value());
} else { } else {
BLECharacteristic *model = BLECharacteristic *model =
this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ); this->device_information_service_->create_characteristic(MODEL_UUID, BLECharacteristic::PROPERTY_READ);
model->set_value(ByteBuffer::wrap(ESPHOME_BOARD)); model->set_value(std::string(ESPHOME_BOARD));
} }
BLECharacteristic *version = BLECharacteristic *version =
this->device_information_service_->create_characteristic(VERSION_UUID, BLECharacteristic::PROPERTY_READ); this->device_information_service_->create_characteristic(VERSION_UUID, BLECharacteristic::PROPERTY_READ);
version->set_value(ByteBuffer::wrap("ESPHome " ESPHOME_VERSION)); version->set_value(std::string("ESPHome " ESPHOME_VERSION));
BLECharacteristic *manufacturer = BLECharacteristic *manufacturer =
this->device_information_service_->create_characteristic(MANUFACTURER_UUID, BLECharacteristic::PROPERTY_READ); this->device_information_service_->create_characteristic(MANUFACTURER_UUID, BLECharacteristic::PROPERTY_READ);
manufacturer->set_value( manufacturer->set_value(this->manufacturer_);
ByteBuffer::wrap(std::vector<uint8_t>(this->manufacturer_.begin(), this->manufacturer_.end())));
return true; return true;
} }

View File

@ -4,13 +4,10 @@
#include "ble_characteristic.h" #include "ble_characteristic.h"
#include "esphome/components/esp32_ble/ble.h" #include "esphome/components/esp32_ble/ble.h"
#include "esphome/components/esp32_ble/ble_advertising.h"
#include "esphome/components/esp32_ble/ble_uuid.h" #include "esphome/components/esp32_ble/ble_uuid.h"
#include "esphome/components/esp32_ble/queue.h" #include "esphome/components/bytebuffer/bytebuffer.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -19,13 +16,13 @@
#ifdef USE_ESP32 #ifdef USE_ESP32
#include <esp_gap_ble_api.h>
#include <esp_gatts_api.h> #include <esp_gatts_api.h>
namespace esphome { namespace esphome {
namespace esp32_ble_server { namespace esp32_ble_server {
using namespace esp32_ble; using namespace esp32_ble;
using namespace bytebuffer;
namespace BLEServerEvt { namespace BLEServerEvt {
enum EmptyEvt { enum EmptyEvt {
@ -78,16 +75,16 @@ class BLEServer : public Component,
void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); } void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); }
void remove_client_(uint16_t conn_id) { this->clients_.erase(conn_id); } void remove_client_(uint16_t conn_id) { this->clients_.erase(conn_id); }
std::string manufacturer_; std::string manufacturer_{};
optional<std::string> model_; optional<std::string> model_{};
std::vector<uint8_t> manufacturer_data_; std::vector<uint8_t> manufacturer_data_{};
esp_gatt_if_t gatts_if_{0}; esp_gatt_if_t gatts_if_{0};
bool registered_{false}; bool registered_{false};
std::unordered_set<uint16_t> clients_; std::unordered_set<uint16_t> clients_;
std::unordered_map<std::string, BLEService *> services_; std::unordered_map<std::string, BLEService *> services_{};
std::vector<BLEService *> services_to_start_; std::vector<BLEService *> services_to_start_{};
BLEService *device_information_service_; BLEService *device_information_service_{};
enum State : uint8_t { enum State : uint8_t {
INIT = 0x00, INIT = 0x00,

View File

@ -6,7 +6,6 @@
#include "esphome/core/event_emitter.h" #include "esphome/core/event_emitter.h"
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
#include "esphome/core/bytebuffer.h"
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
@ -59,7 +58,8 @@ class BLECharacteristicSetValueActionManager
template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> { template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
public: public:
BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {} BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
TEMPLATABLE_VALUE(ByteBuffer, buffer) TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
void play(Ts... x) override { void play(Ts... x) override {
// If the listener is already set, do nothing // If the listener is already set, do nothing
if (BLECharacteristicSetValueActionManager::get_instance()->get_listener(this->parent_) == this->listener_id_) if (BLECharacteristicSetValueActionManager::get_instance()->get_listener(this->parent_) == this->listener_id_)

View File

@ -4,13 +4,15 @@
#include "esphome/components/esp32_ble_server/ble_2902.h" #include "esphome/components/esp32_ble_server/ble_2902.h"
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/bytebuffer.h" #include "esphome/components/bytebuffer/bytebuffer.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
namespace esphome { namespace esphome {
namespace esp32_improv { namespace esp32_improv {
using namespace bytebuffer;
static const char *const TAG = "esp32_improv.component"; static const char *const TAG = "esp32_improv.component";
static const char *const ESPHOME_MY_LINK = "https://my.home-assistant.io/redirect/config_flow_start?domain=esphome"; static const char *const ESPHOME_MY_LINK = "https://my.home-assistant.io/redirect/config_flow_start?domain=esphome";

View File

@ -45,6 +45,9 @@ esp32_ble_server:
ESP_LOGD("BLE", "Characteristic received: %s from %d", std::string(x.begin(), x.end()).c_str(), id); ESP_LOGD("BLE", "Characteristic received: %s from %d", std::string(x.begin(), x.end()).c_str(), id);
- ble_server.characteristic.set_value: - ble_server.characteristic.set_value:
id: test_change_characteristic id: test_change_characteristic
value: !lambda 'return ByteBuffer::wrap({0x00, 0x01, 0x02});' value: !lambda 'return bytebuffer::ByteBuffer::wrap({0x00, 0x01, 0x02}).get_data();'
- ble_server.characteristic.set_value:
id: test_change_characteristic
value: [1, 2, 3, 4]
- ble_server.characteristic.notify: - ble_server.characteristic.notify:
id: test_notify_characteristic id: test_notify_characteristic