From a20950dabd337557a59467d08f5783fd11bf3286 Mon Sep 17 00:00:00 2001 From: Sam <467704+Sammy1Am@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:55:46 -0700 Subject: [PATCH] Add bridge packet queue limit --- components/mitsubishi_uart/muart_bridge.cpp | 8 +++++++- components/mitsubishi_uart/muart_bridge.h | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/components/mitsubishi_uart/muart_bridge.cpp b/components/mitsubishi_uart/muart_bridge.cpp index 5101f14..b571a61 100644 --- a/components/mitsubishi_uart/muart_bridge.cpp +++ b/components/mitsubishi_uart/muart_bridge.cpp @@ -70,8 +70,14 @@ void ThermostatBridge::loop() { } } +/* Queues a packet to be sent by the bridge. If the queue is full, the packet will not be +enqueued.*/ void MUARTBridge::sendPacket(const Packet &packetToSend) { - pkt_queue.push(packetToSend); + if (pkt_queue.size() <= MAX_QUEUE_SIZE) { + pkt_queue.push(packetToSend) ; + } else { + ESP_LOGW(BRIDGE_TAG, "Packet queue full! %x packet not sent.", packetToSend.getPacketType()); + } } void MUARTBridge::writeRawPacket(const RawPacket &packetToSend) const { diff --git a/components/mitsubishi_uart/muart_bridge.h b/components/mitsubishi_uart/muart_bridge.h index ebb90d5..96a3f63 100644 --- a/components/mitsubishi_uart/muart_bridge.h +++ b/components/mitsubishi_uart/muart_bridge.h @@ -9,6 +9,10 @@ namespace mitsubishi_uart { static const char *BRIDGE_TAG = "muart_bridge"; static const uint32_t RESPONSE_TIMEOUT_MS = 3000; // Maximum amount of time to wait for an expected response packet +/* Maximum number of packets allowed to be queued for sending. In some circumstances the equipment response +time can be very slow and packets would queue up faster than they were being received. TODO: Not sure what size this should +be, 4ish should be enough for almost all situations, so 8 seems plenty.*/ +static const size_t MAX_QUEUE_SIZE = 8; // A UARTComponent wrapper to send and receieve packets class MUARTBridge {