couple of other fixes

This commit is contained in:
NP v/d Spek 2024-09-19 20:15:51 +02:00
parent ab31977886
commit ad41f67a36
4 changed files with 31 additions and 32 deletions

View file

@ -87,13 +87,13 @@ bool ESPNowPacket::is_valid() {
/* ESPNowProtocol ********************************************************************** */
bool ESPNowProtocol::write(uint64_t mac_address, const uint8_t *data, uint8_t len) {
ESPNowPacket *packet = new ESPNowPacket(mac_address, data, len, this->get_protocol_id());
bool ESPNowProtocol::write(uint64_t peer, const uint8_t *data, uint8_t len) {
ESPNowPacket *packet = new ESPNowPacket(peer, data, len, this->get_protocol_id());
return this->parent_->write(packet);
}
bool ESPNowProtocol::write(uint64_t mac_address, std::vector<uint8_t> &data) {
bool ESPNowProtocol::write(uint64_t peer, std::vector<uint8_t> &data) {
ESPNowPacket *packet =
new ESPNowPacket(mac_address, (uint8_t *) data.data(), (uint8_t) data.size(), this->get_protocol_id());
new ESPNowPacket(peer, (uint8_t *) data.data(), (uint8_t) data.size(), this->get_protocol_id());
return this->parent_->write(packet);
}
bool ESPNowProtocol::write(ESPNowPacket *packet) {
@ -289,8 +289,12 @@ void ESPNowComponent::on_data_received(const uint8_t *addr, const uint8_t *data,
#endif
ESPNowPacket packet((uint64_t) *addr, data, size);
packet.broadcast(broadcast);
if (rx_ctrl != nullptr) {
packet.rssi(rx_ctrl->rssi);
packet.timestamp(rx_ctrl->timestamp);
} else {
packet.timestamp(millis());
}
ESP_LOGVV(TAG, "Read: %s |H:%02x%02x%02x A:%02x%02x%02x %02x T:%02x C:%02x%02x S:%02d", packet.content_bytes(),
packet.content(0), packet.content(1), packet.content(2), packet.content(3), packet.content(4),
packet.content(5), packet.content(6), packet.content(7), packet.content(8), packet.content(9),
@ -346,7 +350,7 @@ void ESPNowComponent::runner() {
if (!esp_now_is_peer_exist(mac)) {
if (!this->auto_add_peer_) {
this->defer([this, packet, mac]() { this->on_new_peer_((ESPNowPacket *) &packet); });
this->defer([this, packet]() { this->on_new_peer_((ESPNowPacket *) &packet); });
} else {
this->add_peer(packet->peer());
}

View file

@ -42,8 +42,6 @@ static const uint32_t ESPNOW_MAIN_PROTOCOL_ID = 0x11CFAF;
static uint8_t last_ref_id = 0;
struct ESPNowData {
union {
struct {
uint64_t peer{0};
uint8_t rssi{0};
uint8_t attempts{0};
@ -51,19 +49,16 @@ struct ESPNowData {
uint32_t timestamp{0};
uint8_t size{0};
uint8_t content[251]{0};
};
uint8_t bytes[251 + 16];
};
};
class ESPNowPacket {
public:
ESPNowPacket() {
memset((void *) &(this->data_.bytes), 0, sizeof(this->data_));
memset((void *) &(this->data_), 0, sizeof(ESPNowData));
this->content_ = new ByteBuffer(251);
this->content_->put_uint24(TRANSPORT_HEADER);
};
// Create packet to be send.
ESPNowPacket(uint64_t peer, const uint8_t *data, uint8_t size, uint32_t app_id);
ESPNowPacket(uint64_t peer, const uint8_t *data, uint8_t size, uint32_t protocol);
// Load received packet's.
ESPNowPacket(uint64_t peer, const uint8_t *data, uint8_t size);
@ -71,7 +66,7 @@ class ESPNowPacket {
ESPNowPacket(ESPNowData data) : ESPNowPacket() { this->store(data); }
void store(ESPNowData data) {
memcpy((void *) &(this->data_.bytes), (void *) &(data.bytes), sizeof(ESPNowData));
memcpy((void *) &(this->data_), (void *) &(data), sizeof(ESPNowData));
this->content_->clear();
this->content_->put_bytes((uint8_t *) &(data.content), data.size);
}
@ -79,7 +74,7 @@ class ESPNowPacket {
uint8_t *retrieve() {
memcpy((void *) &(this->data_.content), this->content_bytes(), this->size());
this->data_.size = this->size();
return (uint8_t *) &(this->data_.bytes);
return (uint8_t *) &(this->data_);
}
uint64_t peer() { return this->data_.peer; }

View file

@ -7,9 +7,9 @@
namespace esphome {
ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len, Endian endianness) {
ByteBuffer ByteBuffer::wrap(const uint8_t *value, size_t length, Endian endianness) {
// there is a double copy happening here, could be optimized but at cost of clarity.
std::vector<uint8_t> data(ptr, ptr + len);
std::vector<uint8_t> data(value, value + length);
ByteBuffer buffer = {data};
buffer.endianness_ = endianness;
return buffer;
@ -131,11 +131,11 @@ std::vector<uint8_t> ByteBuffer::get_vector(size_t length) {
this->update_used_space_();
return {start, start + length};
}
void ByteBuffer::get_bytes(uint8_t *data, size_t length) {
void ByteBuffer::get_bytes(uint8_t *value, size_t length) {
size_t index = 0;
assert(this->get_remaining() >= length);
while (length-- != 0) {
*(data + index++) = this->data_[this->position_++];
*(value + index++) = this->data_[this->position_++];
}
this->update_used_space_();
}
@ -180,11 +180,11 @@ void ByteBuffer::put_vector(const std::vector<uint8_t> &value) {
this->position_ += value.size();
this->update_used_space_();
}
void ByteBuffer::put_bytes(const uint8_t *data, size_t length) {
void ByteBuffer::put_bytes(const uint8_t *value, size_t length) {
assert(this->get_remaining() >= length);
auto index = 0;
while (length-- != 0) {
this->data_[this->position_++] = static_cast<uint8_t>(*(data + index++));
this->data_[this->position_++] = static_cast<uint8_t>(*(value + index++));
}
this->update_used_space_();
}

View file

@ -46,7 +46,7 @@ class ByteBuffer {
/**
* Wrap an existing array in a ByteBuffer. Note that this will create a copy of the data.
*/
static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE);
static ByteBuffer wrap(const uint8_t *value, size_t length, Endian endianness = LITTLE);
// Convenience functions to create a ByteBuffer from a value
static ByteBuffer wrap(uint8_t value);
static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE);
@ -92,7 +92,7 @@ class ByteBuffer {
bool get_bool() { return this->get_uint8(); }
// Get vector of bytes, increment by length
std::vector<uint8_t> get_vector(size_t length);
void get_bytes(uint8_t *data, size_t length);
void get_bytes(uint8_t *value, size_t length);
// Put values into the buffer, increment the position accordingly
// put any integral value, length represents the number of bytes
@ -113,7 +113,7 @@ class ByteBuffer {
void put_double(double value);
void put_bool(bool value) { this->put_uint8(value); }
void put_vector(const std::vector<uint8_t> &value);
void put_bytes(const uint8_t *data, size_t size);
void put_bytes(const uint8_t *value, size_t length);
inline size_t get_capacity() const { return this->data_.size(); }
inline size_t get_position() const { return this->position_; }