From 99df2d2e17d21f71b55464d9b8447bb14e227406 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <niccolo.caruso@skywarder.eu>
Date: Thu, 6 Mar 2025 16:45:30 +0100
Subject: [PATCH] [ARP] Better Hub debug printing, EthernetSniffer not
 hardcoded

Hub: Now better debug printing for the sniffing to see from which source the messages arrive
EthernetBase: Changed MAC initialization to avoid switch filtering such messages
EthernetSniffer: No more hardcoded ports
---
 src/Groundstation/Automated/Hub.cpp           | 20 ++++++++++++-------
 .../Common/Ports/EthernetBase.cpp             | 13 +++++++++++-
 .../Common/Ports/EthernetSniffer.cpp          |  8 ++++----
 3 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/Groundstation/Automated/Hub.cpp b/src/Groundstation/Automated/Hub.cpp
index 523568364..f77e4bcaf 100644
--- a/src/Groundstation/Automated/Hub.cpp
+++ b/src/Groundstation/Automated/Hub.cpp
@@ -247,7 +247,8 @@ void Hub::dispatchOutgoingMsg(const mavlink_message_t& msg)
         msg.msgid == MAVLINK_MSG_ID_ROCKET_STATS_TM)
     {
         TRACE(
-            "[info] Hub: A MAIN packet was received from ground packet "
+            "[info][SNIFFING] Hub: A MAIN packet was received from ground "
+            "packet "
             "(ethernet probably and NOT radio)\n");
         /* The message received by ethernet (outgoing) in reality is not a
          * command but the telemetry spoofed, therefore is then used as incoming
@@ -272,7 +273,8 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg)
         mavlink_msg_rocket_flight_tm_decode(&msg, &rocketTM);
         uint64_t timestamp = mavlink_msg_rocket_flight_tm_get_timestamp(&msg);
         TRACE(
-            "[info] Hub: A FLIGHT_ROCKET_TM packet was received from ground "
+            "[info][Radio/Sniffing] Hub: A FLIGHT_ROCKET_TM packet was "
+            "received "
             "packet with ts %llu\n",
             timestamp);
         /* Messages older and within the discard interval are treated as old
@@ -280,8 +282,10 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg)
         if (timestamp <= lastFlightTMTimestamp &&
             lastFlightTMTimestamp > timestamp + DISCARD_MSG_DELAY)
             return;
-        TRACE("[info] Hub: A FLIGHT_ROCKET_TM packet is valid with ts %llu\n",
-              timestamp);
+        TRACE(
+            "[info][Radio/Sniffing] Hub: A FLIGHT_ROCKET_TM packet is valid "
+            "with ts %llu\n",
+            timestamp);
         lastFlightTMTimestamp = timestamp;
         NASState nasState{
             mavlink_msg_rocket_flight_tm_get_timestamp(&msg),
@@ -302,7 +306,7 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg)
         mavlink_rocket_stats_tm_t rocketST;
         mavlink_msg_rocket_stats_tm_decode(&msg, &rocketST);
         TRACE(
-            "[info] Hub: A ROCKET_STAT_TM packet was received from ground "
+            "[info][Radio/Sniffing] Hub: A ROCKET_STAT_TM packet was received "
             "packet with ts %llu\n",
             rocketST.timestamp);
         /* Messages older and within the discard interval are treated as old
@@ -310,8 +314,10 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg)
         if (rocketST.timestamp <= lastStatsTMTimestamp &&
             lastStatsTMTimestamp > rocketST.timestamp + DISCARD_MSG_DELAY)
             return;
-        TRACE("[info] Hub: A ROCKET_STAT_TM packet is valid, with ts %llu\n",
-              rocketST.timestamp);
+        TRACE(
+            "[info][Radio/Sniffing] Hub: A ROCKET_STAT_TM packet is valid, "
+            "with ts %llu\n",
+            rocketST.timestamp);
         lastStatsTMTimestamp = rocketST.timestamp;
 
         // TODO: The origin should have its own struct since only timestamp and
diff --git a/src/Groundstation/Common/Ports/EthernetBase.cpp b/src/Groundstation/Common/Ports/EthernetBase.cpp
index dbae92339..76e66d89a 100644
--- a/src/Groundstation/Common/Ports/EthernetBase.cpp
+++ b/src/Groundstation/Common/Ports/EthernetBase.cpp
@@ -85,6 +85,17 @@ bool EthernetBase::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
         WizMac mac = MAC_BASE;
         // Add to the mac address the offset set on the dipswitch
         mac.c += 1 + ipOffset;
+        // In case of sniffing change ulteriorly the MAC to avoid switch to
+        // filter based on not whole MAC...
+        if (sniffOtherGs)
+        {
+            mac.a += 1;
+            mac.b += 1;
+            mac.c += 1;
+            mac.d += 1;
+            mac.e += 1;
+            mac.f += 1;
+        }
         this->wiz5500->setSourceMac(mac);
     }
     else
@@ -115,7 +126,7 @@ bool EthernetBase::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
     if (sniffOtherGs)
     {
         getModule<EthernetSniffer>()->init(1, SEND_PORT, RECV_PORT);
-        if (!getModule<EthernetSniffer>()->start(wiz5500))
+        if (!getModule<EthernetSniffer>()->start(this->wiz5500))
             return false;
     }
 
diff --git a/src/Groundstation/Common/Ports/EthernetSniffer.cpp b/src/Groundstation/Common/Ports/EthernetSniffer.cpp
index 564024f89..e08e1c538 100644
--- a/src/Groundstation/Common/Ports/EthernetSniffer.cpp
+++ b/src/Groundstation/Common/Ports/EthernetSniffer.cpp
@@ -51,9 +51,9 @@ Boardcore::Wiz5500::PhyState EthernetSniffer::getState()
 void EthernetSniffer::init(uint16_t portNumber, uint16_t srcPort,
                            uint16_t dstPort)
 {
-    portNr  = portNumber;
-    srcPort = srcPort;
-    dstPort = dstPort;
+    portNr        = portNumber;
+    this->srcPort = srcPort;
+    this->dstPort = dstPort;
 }
 
 bool EthernetSniffer::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
@@ -62,7 +62,7 @@ bool EthernetSniffer::start(std::shared_ptr<Boardcore::Wiz5500> wiz5500)
 
     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,
+    if (!this->wiz5500->openUdp(portNr, srcPort, {255, 255, 255, 255}, dstPort,
                                 500))
     {
         return false;
-- 
GitLab