From c666e757882dfc874792fcc947d999b1c53c74e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <niccolo.caruso@skywarder.eu>
Date: Sat, 8 Feb 2025 10:58:20 +0100
Subject: [PATCH] [ARP] EthernetSniffing fix due to deferenced shared pointer

Fix hardfault: The ethernet sniffer by using the std::move for the Wiz5500 was deferencing the pointer and threfore in the recvfrom an HardFault occured.
Debug: Removed and changed some TRACE strings
Fix UDP socket: The sniffing socket was initialized opend correctly but then the init had switched arguments. This surely impacted on the recvfrom.
---
 .../Common/Ports/EthernetBase.cpp             | 15 +++--------
 .../Common/Ports/EthernetSniffer.cpp          | 26 +++++++------------
 2 files changed, 14 insertions(+), 27 deletions(-)

diff --git a/src/Groundstation/Common/Ports/EthernetBase.cpp b/src/Groundstation/Common/Ports/EthernetBase.cpp
index e66be196a..dbae92339 100644
--- a/src/Groundstation/Common/Ports/EthernetBase.cpp
+++ b/src/Groundstation/Common/Ports/EthernetBase.cpp
@@ -67,7 +67,7 @@ Boardcore::Wiz5500::PhyState EthernetBase::getState()
 
 bool EthernetBase::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
 {
-    this->wiz5500 = std::move(wiz5500);
+    this->wiz5500 = wiz5500;
 
     // Reset the device
     this->wiz5500->reset();
@@ -103,12 +103,6 @@ bool EthernetBase::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
         return false;
     }
 
-    if (!this->wiz5500->openUdp(1, SEND_PORT, {255, 255, 255, 255}, RECV_PORT,
-                                500))
-    {
-        return false;
-    }
-
     auto mav_handler = [this](EthernetMavDriver* channel,
                               const mavlink_message_t& msg) { handleMsg(msg); };
 
@@ -116,17 +110,16 @@ bool EthernetBase::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
 
     if (!mav_driver->start())
         return false;
-    TRACE("[info] mavlink driver started correctly\n");
 
-    // Create and start a second mavlink driver to sniff the ethernet port
+    // In case of sniffing mode initialize and start the EthernetSniffer
     if (sniffOtherGs)
     {
-        getModule<EthernetSniffer>()->init(1, RECV_PORT, SEND_PORT);
+        getModule<EthernetSniffer>()->init(1, SEND_PORT, RECV_PORT);
         if (!getModule<EthernetSniffer>()->start(wiz5500))
             return false;
     }
 
-    TRACE("[info] Ethernet sniffing started correctly\n");
+    TRACE("[info] Ethernet module started correctly\n");
     return true;
 }
 
diff --git a/src/Groundstation/Common/Ports/EthernetSniffer.cpp b/src/Groundstation/Common/Ports/EthernetSniffer.cpp
index 8b6be2b7f..564024f89 100644
--- a/src/Groundstation/Common/Ports/EthernetSniffer.cpp
+++ b/src/Groundstation/Common/Ports/EthernetSniffer.cpp
@@ -58,31 +58,25 @@ void EthernetSniffer::init(uint16_t portNumber, uint16_t srcPort,
 
 bool EthernetSniffer::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
 {
-    TRACE("Movin\n");
-    this->wiz5500 = std::move(wiz5500);
+    this->wiz5500 = wiz5500;
 
-    TRACE("Opening\n");
-    // We open the port for sniffing using the port we specified
-    // if (!this->wiz5500->openUdp(portNr, srcPort, {255, 255, 255, 255},
-    // dstPort,
-    //                             500))
-    // {
-    //     return false;
-    // }
-
-    TRACE("Mavlinker\n");
+    TRACE("[info] Opening sniffing UDP socket\n");
+    // We open the UDP socket for sniffing
+    if (!this->wiz5500->openUdp(1, SEND_PORT, {255, 255, 255, 255}, RECV_PORT,
+                                500))
+    {
+        return false;
+    }
 
     auto mav_handler = [this](EthernetMavDriver* channel,
                               const mavlink_message_t& msg) { handleMsg(msg); };
 
-    mav_driver = std::make_unique<EthernetMavDriver>(this, mav_handler, 1, 10);
-
-    TRACE("Starting\n");
+    mav_driver = std::make_unique<EthernetMavDriver>(this, mav_handler, 0, 10);
 
     if (!mav_driver->start())
         return false;
 
-    TRACE("Start ok\n");
+    TRACE("[info] EthernetSniffer start ok\n");
 
     return true;
 }
-- 
GitLab