From 613c61c6312c61f791c03fcd666dfcff6c4b16a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <nicolo.caruso@skywarder.eu>
Date: Wed, 28 May 2025 19:20:19 +0200
Subject: [PATCH] [MavlinkDriver] Increased buffer to MTU (1500B) and allocate
 on the heap

- Buffer input size: Changed to 1500B (MTU for Ethernet). This is due to
the fact that the MavlinkDriver by doing so consumes the MTU completely
reading from the wiz5500. This fixes the issue for which packets larger
than the buffer or multiple packets in queue are dropped.
- Buffer is allocated on the heap to avoid stack overflow issues.
---
 src/shared/radio/MavlinkDriver/MavlinkDriver.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/shared/radio/MavlinkDriver/MavlinkDriver.h b/src/shared/radio/MavlinkDriver/MavlinkDriver.h
index a1186fc25..a4bdcf780 100644
--- a/src/shared/radio/MavlinkDriver/MavlinkDriver.h
+++ b/src/shared/radio/MavlinkDriver/MavlinkDriver.h
@@ -178,11 +178,12 @@ private:
     size_t outBufferMaxAge;
     uint16_t pollingTime = 100;  // ms
 
-    // Buffers
-    static constexpr size_t MAV_IN_BUFFER_SIZE = 256;
+    // Buffers (equal to MTU for ethernet)
+    static constexpr size_t MAV_IN_BUFFER_SIZE = 1500;
 
     SyncPacketQueue<PktLength, OutQueueSize> outQueue;
-    uint8_t rcvBuffer[MAV_IN_BUFFER_SIZE];
+    std::unique_ptr<uint8_t[]> rcvBuffer =
+        std::make_unique<uint8_t[]>(MAV_IN_BUFFER_SIZE);
 
     // Status
     MavlinkStatus status;
@@ -329,7 +330,7 @@ void MavlinkDriver<PktLength, OutQueueSize, MavMsgLength>::runReceiver()
     while (!stopFlag)
     {
         // Check for a new message on the device
-        rcvSize = device->receive(rcvBuffer, MAV_IN_BUFFER_SIZE);
+        rcvSize = device->receive(rcvBuffer.get(), MAV_IN_BUFFER_SIZE);
 
         // If there's a new message ...
         if (rcvSize > 0)
-- 
GitLab