Skip to content

Commit

Permalink
refactor requests allocations (m3_vedirect)
Browse files Browse the repository at this point in the history
  • Loading branch information
krahabb committed Nov 29, 2024
1 parent 45efbd6 commit 7a92475
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions esphome/components/m3_vedirect/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ void Manager::request_set(register_id_t register_id, const void *data, HEXFRAME:
request_callback_t callback, request_callback_param_t callback_param) {
// Request(s) in our storage are re-used as far as they're expired (millis == 0)
Request *request = nullptr;
for (auto &it : this->requests_) {
if (!it.millis) {
request = ⁢
for (auto it : this->requests_) {
if (!it->millis) {
request = it;
goto _setup_request;
}
}
// When no cached Request structs are available we increase our
// storage. This will never be reduced/compacted though, hoping it doesn't grow
// too much due to a fast burst of requests coming in (a pending request expires
// either when replied or after a VEDIRECT_COMMAND_TIMEOUT_MILLIS timeout).
this->requests_.push_back(Request());
request = &this->requests_.back();
this->requests_.push_back(new Request());
request = this->requests_.back();
request->tag = std::to_string(this->requests_.size());

_setup_request:
Expand All @@ -130,6 +130,8 @@ void Manager::request_set(register_id_t register_id, const void *data, HEXFRAME:
this->send_hexframe(request->hex_frame);
this->set_timeout(request->tag, VEDIRECT_COMMAND_TIMEOUT_MILLIS, [this, request]() {
if (request->millis) {
ESP_LOGD(this->logtag_, "HEX FRAME: timeout on request(tag=%s) %s", request->tag.c_str(),
request->hex_frame.encoded());
// This means the SET command wasn't (yet) replied so we just timeout it.
request->callback(request->callback_param, nullptr);
request->millis = 0;
Expand Down Expand Up @@ -200,13 +202,13 @@ const char *FRAME_ERRORS[] = {

#if defined(VEDIRECT_USE_HEXFRAME)
void Manager::requests_match_get_or_set_(const RxHexFrame &rx_hex_frame) {
for (auto &request : this->requests_) {
if ((request.hex_frame.command() == rx_hex_frame.command()) &&
(request.hex_frame.register_id() == rx_hex_frame.register_id())) {
ESP_LOGD(this->logtag_, "HEX FRAME: received reply %s for request %s", rx_hex_frame.encoded(),
request.hex_frame.encoded());
request.callback(request.callback_param, &rx_hex_frame);
request.millis = 0;
for (auto request : this->requests_) {
if ((request->hex_frame.command() == rx_hex_frame.command()) &&
(request->hex_frame.register_id() == rx_hex_frame.register_id())) {
ESP_LOGD(this->logtag_, "HEX FRAME: received reply %s (flags: 0x%02X) for request %s", rx_hex_frame.encoded(),
rx_hex_frame.flags(), request->hex_frame.encoded());
request->callback(request->callback_param, &rx_hex_frame);
request->millis = 0;
--this->pending_requests_;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion esphome/components/m3_vedirect/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Manager : public uart::UARTDevice, public Component, protected FrameHandle
request_callback_param_t callback_param{};
};

std::vector<Request> requests_;
std::vector<Request *> requests_;
uint32_t pending_requests_{0};
void requests_match_get_or_set_(const RxHexFrame &hexframe);

Expand Down

0 comments on commit 7a92475

Please sign in to comment.