update code with given CI suggestions

This commit is contained in:
NP v/d Spek 2024-08-24 22:02:01 +02:00
parent d193d44e13
commit b22dacc0f2
4 changed files with 31 additions and 31 deletions

View file

@ -57,11 +57,11 @@ struct {
void ESPNowProtocol::setup() { parent_->register_protocol(this); }
bool ESPNowProtocol::write(const uint64_t mac_address, const uint8_t *data, uint8_t len) {
bool ESPNowProtocol::write(uint64_t mac_address, const uint8_t *data, uint8_t len) {
ESPNowPacket packet(mac_address, data, len, this->get_app_id());
return this->parent_->write(packet);
}
bool ESPNowProtocol::write(const uint64_t mac_address, const std::vector<uint8_t> data) {
bool ESPNowProtocol::write(uint64_t mac_address, std::vector<uint8_t> &data) {
ESPNowPacket packet(mac_address, (uint8_t *) data.data(), (uint8_t) data.size(), this->get_app_id());
return this->parent_->write(packet);
}
@ -102,7 +102,7 @@ void ESPNowComponent::setup() {
ESP_LOGI(TAG, "Setting up ESP-NOW...");
#ifdef USE_WIFI
global_wifi_component.disable();
wifi::global_wifi_component.disable();
#else // Set device as a Wi-Fi Station
esp_event_loop_create_default();
@ -181,13 +181,13 @@ esp_err_t ESPNowComponent::add_peer(uint64_t addr) {
uint8_t mac[6];
this->del_peer(addr);
esp_now_peer_info_t peerInfo = {};
memset(&peerInfo, 0, sizeof(esp_now_peer_info_t));
peerInfo.channel = this->wifi_channel_;
peerInfo.encrypt = false;
memcpy((void *) peerInfo.peer_addr, (void *) &addr, 6);
esp_now_peer_info_t peer_info = {};
memset(&peer_info, 0, sizeof(esp_now_peer_info_t));
peer_info.channel = this->wifi_channel_;
peer_info.encrypt = false;
memcpy((void *) peer_info.peer_addr, (void *) &addr, 6);
return esp_now_add_peer(&peerInfo);
return esp_now_add_peer(&peer_info);
}
}
@ -199,7 +199,7 @@ esp_err_t ESPNowComponent::del_peer(uint64_t addr) {
return ESP_OK;
}
ESPNowDefaultProtocol *ESPNowComponent::get_defaultProtocol_() {
ESPNowDefaultProtocol *ESPNowComponent::get_default_protocol() {
if (this->protocols_[ESPNOW_DEFAULT_APP_ID] == nullptr) {
ESPNowDefaultProtocol *tmp = new ESPNowDefaultProtocol();
this->protocols_[ESPNOW_DEFAULT_APP_ID] = tmp;
@ -240,7 +240,7 @@ void ESPNowComponent::on_data_received(const uint8_t *addr, const uint8_t *data,
#endif
{
ESPNowPacket packet;
wifi_pkt_rx_ctrl_t *rx_ctrl = NULL;
wifi_pkt_rx_ctrl_t *rx_ctrl = nullptr;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 1)
uint8_t *addr = recv_info->src_addr;

View file

@ -28,15 +28,15 @@ class ESPNowProtocol : public Parented<ESPNowComponent> {
void setup();
virtual void on_receive(ESPNowPacket packet) { return; };
virtual void on_sent(ESPNowPacket packet, bool status) { return; };
virtual void on_new_peer(ESPNowPacket packet) { return; };
virtual void on_receive(ESPNowPacket packet){};
virtual void on_sent(ESPNowPacket packet, bool status){};
virtual void on_new_peer(ESPNowPacket packet){};
virtual uint32_t get_app_id() = 0;
uint8_t get_next_ref_id() { return next_ref_id_++; }
bool write(const uint64_t mac_address, const uint8_t *data, uint8_t len);
bool write(const uint64_t mac_address, const std::vector<uint8_t> data);
bool write(uint64_t mac_address, const uint8_t *data, uint8_t len);
bool write(uint64_t mac_address, std::vector<uint8_t> &data);
bool write(ESPNowPacket packet);
protected:
@ -45,9 +45,9 @@ class ESPNowProtocol : public Parented<ESPNowComponent> {
class ESPNowDefaultProtocol : public ESPNowProtocol {
public:
void on_receive(ESPNowPacket packet) { this->on_receive_.call(packet); };
void on_sent(ESPNowPacket packet, bool status) { this->on_sent_.call(packet, status); };
void on_new_peer(ESPNowPacket packet) { this->on_new_peer_.call(packet); };
void on_receive(ESPNowPacket packet) override { this->on_receive_.call(packet); };
void on_sent(ESPNowPacket packet, bool status) override { this->on_sent_.call(packet, status); };
void on_new_peer(ESPNowPacket packet) override { this->on_new_peer_.call(packet); };
uint32_t get_app_id() override { return ESPNOW_DEFAULT_APP_ID; };
@ -96,7 +96,7 @@ class ESPNowComponent : public Component {
void register_protocol(ESPNowProtocol *protocol) {
protocol->set_parent(this);
this->protocols_[protocol->get_app_id()] = std::move(protocol);
this->protocols_[protocol->get_app_id()] = protocol;
}
esp_err_t add_peer(uint64_t addr);
@ -111,7 +111,7 @@ class ESPNowComponent : public Component {
bool is_locked() { return this->lock_; }
void unlock() { this->lock_ = false; }
ESPNowDefaultProtocol *get_defaultProtocol_();
ESPNowDefaultProtocol *get_default_protocol();
protected:
bool validate_channel_(uint8_t channel);
@ -151,10 +151,10 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
auto mac = this->mac_.value(x...);
if (this->static_) {
this->parent_->get_defaultProtocol_()->write(mac, this->data_static_);
this->parent_->get_default_protocol()->write(mac, this->data_static_);
} else {
auto val = this->data_func_(x...);
this->parent_->get_defaultProtocol_()->write(mac, val);
this->parent_->get_default_protocol()->write(mac, val);
}
}
@ -192,7 +192,7 @@ template<typename... Ts> class DelPeerAction : public Action<Ts...>, public Pare
class ESPNowSentTrigger : public Trigger<ESPNowPacket, bool> {
public:
explicit ESPNowSentTrigger(ESPNowComponent *parent) {
parent->get_defaultProtocol_()->add_on_sent_callback(
parent->get_default_protocol()->add_on_sent_callback(
[this](ESPNowPacket value, bool status) { this->trigger(value, status); });
}
};
@ -200,14 +200,14 @@ class ESPNowSentTrigger : public Trigger<ESPNowPacket, bool> {
class ESPNowReceiveTrigger : public Trigger<ESPNowPacket> {
public:
explicit ESPNowReceiveTrigger(ESPNowComponent *parent) {
parent->get_defaultProtocol_()->add_on_receive_callback([this](ESPNowPacket value) { this->trigger(value); });
parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacket value) { this->trigger(value); });
}
};
class ESPNowNewPeerTrigger : public Trigger<ESPNowPacket> {
public:
explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) {
parent->get_defaultProtocol_()->add_on_peer_callback([this](ESPNowPacket value) { this->trigger(value); });
parent->get_default_protocol()->add_on_peer_callback([this](ESPNowPacket value) { this->trigger(value); });
}
};

View file

@ -5,7 +5,7 @@
static const char *const TAG = "espnow_packet";
ESPNowPacket::ESPNowPacket(const uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id)
ESPNowPacket::ESPNowPacket(uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id)
: mac64(mac64), size(size), app_id(app_id), retrys(0) {
if (this->mac64 == 0)
this->mac64 = ESPNOW_BROADCAST_ADDR;
@ -29,13 +29,13 @@ inline void ESPNowPacket::info(std::string place) {
bool ESPNowPacket::is_valid() {
uint16_t crc = this->crc16;
recalc();
bool valid = (std::memcmp(&header, &transport_header, 3) == 0);
bool valid = (std::memcmp(&header, &TRANSPORT_HEADER, 3) == 0);
valid &= (this->app_id != 0);
valid &= (this->crc16 == crc);
if (!valid) {
ESP_LOGV("Packet", "Invalid H:%02x%02x%02x A:%06x R:%02x C:%04x ipv. %04x, %d&%d&%d=%d\n", this->header[0],
this->header[1], this->header[2], this->app_id, this->ref_id, crc, this->crc16,
std::memcmp(&header, &transport_header, 3) == 0, (this->app_id != 0), (this->crc16 == crc), valid);
std::memcmp(&header, &TRANSPORT_HEADER, 3) == 0, (this->app_id != 0), (this->crc16 == crc), valid);
}
this->crc16 = crc;

View file

@ -24,7 +24,7 @@ static const uint64_t ESPNOW_BROADCAST_ADDR = 0xFFFFFFFFFFFF;
static espnow_addr_t ESPNOW_ADDR_SELF = {0};
static const uint8_t MAX_ESPNOW_DATA_SIZE = 240;
static const uint32_t transport_header = 0xC19983;
static const uint32_t TRANSPORT_HEADER = 0xC19983;
template<typename... Args> std::string string_format(const std::string &format, Args... args) {
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
@ -61,7 +61,7 @@ struct ESPNowPacket {
};
ESPNowPacket() ESPHOME_ALWAYS_INLINE : retrys(0) {}
ESPNowPacket(const uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id);
ESPNowPacket(uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id);
inline void info(std::string place);