From d0099e9084fa9ee870639ba2fc0c244a848c6ba9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <nicolo.caruso@skywarder.eu>
Date: Thu, 8 May 2025 11:14:58 +0200
Subject: [PATCH] [ARP] Added HubData struct for more logging informations

HubData now logs the packets received from the ground and from rocket and the sniffed packets.
---
 scripts/logdecoder/General/logdecoder.cpp |  7 +--
 src/Groundstation/Automated/Hub.cpp       | 12 +++++
 src/Groundstation/Automated/Hub.h         |  3 ++
 src/Groundstation/Automated/HubData.h     | 53 +++++++++++++++++++++++
 4 files changed, 72 insertions(+), 3 deletions(-)
 create mode 100644 src/Groundstation/Automated/HubData.h

diff --git a/scripts/logdecoder/General/logdecoder.cpp b/scripts/logdecoder/General/logdecoder.cpp
index 89f6f1a2b..ba47bc6cd 100644
--- a/scripts/logdecoder/General/logdecoder.cpp
+++ b/scripts/logdecoder/General/logdecoder.cpp
@@ -21,6 +21,8 @@
  */
 
 #include <Groundstation/Automated/Actuators/ActuatorsData.h>
+#include <Groundstation/Automated/HubData.h>
+#include <Groundstation/Automated/LogSniffing.h>
 #include <Groundstation/Automated/PinHandler/PinData.h>
 #include <Groundstation/Automated/SMA/SMAData.h>
 #include <Groundstation/LyraGS/Radio/RadioData.h>
@@ -49,8 +51,6 @@
 #include <logger/Deserializer.h>
 #include <logger/LogTypes.h>
 #include <tscpp/stream.h>
-#include <Groundstation/Automated/LogSniffing.h>
-
 
 #include <fstream>
 #include <iostream>
@@ -142,7 +142,8 @@ void registerTypes(Deserializer& ds)
     ds.registerType<Antennas::SMAStatus>();
     ds.registerType<Antennas::PinChangeData>();
     ds.registerType<LyraGS::MainRadioLog>();
-    ds.registerType<Antennas::LogSniffing();
+    ds.registerType<Antennas::LogSniffing>();
+    ds.registerType<Antennas::HubData>();
 }
 
 void showUsage(const string& cmdName)
diff --git a/src/Groundstation/Automated/Hub.cpp b/src/Groundstation/Automated/Hub.cpp
index 7e95a699c..d9a62adbe 100644
--- a/src/Groundstation/Automated/Hub.cpp
+++ b/src/Groundstation/Automated/Hub.cpp
@@ -44,6 +44,10 @@ using namespace miosix;
 
 void Hub::dispatchOutgoingMsg(const mavlink_message_t& msg)
 {
+    logHubData.timestamp = TimestampTimer::getTimestamp();
+    logHubData.groundRx  = logHubData.groundRx + 1;
+    Logger::getInstance().log(logHubData);
+
     TRACE("[info] Hub: Packet arrived from outgoing messages!!!\n");
     LyraGS::BoardStatus* status  = getModule<LyraGS::BoardStatus>();
     LyraGS::RadioMain* radioMain = getModule<LyraGS::RadioMain>();
@@ -256,6 +260,10 @@ void Hub::dispatchOutgoingMsg(const mavlink_message_t& msg)
         dispatchIncomingMsg(msg);
         LogSniffing sniffing = {TimestampTimer::getTimestamp(), 1};
         Logger::getInstance().log(sniffing);
+
+        logHubData.timestamp = TimestampTimer::getTimestamp();
+        logHubData.sniffedRx = logHubData.sniffedRx + 1;
+        Logger::getInstance().log(logHubData);
     }
 }
 
@@ -268,6 +276,10 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg)
     (void)serial;
 #endif
 
+    logHubData.timestamp = TimestampTimer::getTimestamp();
+    logHubData.rocketRx  = logHubData.rocketRx + 1;
+    Logger::getInstance().log(logHubData);
+
     // Extracting NAS rocket state
     if (msg.msgid == MAVLINK_MSG_ID_ROCKET_FLIGHT_TM)
     {
diff --git a/src/Groundstation/Automated/Hub.h b/src/Groundstation/Automated/Hub.h
index 12c78b197..a61b174e8 100644
--- a/src/Groundstation/Automated/Hub.h
+++ b/src/Groundstation/Automated/Hub.h
@@ -22,6 +22,7 @@
 
 #pragma once
 
+#include <Groundstation/Automated/HubData.h>
 #include <Groundstation/Automated/LogSniffing.h>
 #include <Groundstation/Automated/SMA/SMA.h>
 #include <Groundstation/Common/HubBase.h>
@@ -112,6 +113,8 @@ private:
     bool hasNewNasSet              = false;
     uint64_t lastFlightTMTimestamp = 0;
     uint64_t lastStatsTMTimestamp  = 0;
+
+    HubData logHubData;  // Data for logging
 };
 
 }  // namespace Antennas
diff --git a/src/Groundstation/Automated/HubData.h b/src/Groundstation/Automated/HubData.h
new file mode 100644
index 000000000..ca56c5e4e
--- /dev/null
+++ b/src/Groundstation/Automated/HubData.h
@@ -0,0 +1,53 @@
+/* Copyright (c) 2024 Skyward Experimental Rocketry
+ * Author: Nicolò Caruso
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#pragma once
+
+#include <stdint.h>
+
+#include <iostream>
+#include <string>
+
+namespace Antennas
+{
+
+/**
+ * @brief Structure to save informations about the Hub reception
+ */
+struct HubData
+{
+    uint64_t timestamp = 0;
+    uint16_t groundRx  = 0;
+    uint16_t rocketRx  = 0;
+    uint16_t sniffedRx = 0;
+
+    static std::string header()
+    {
+        return "timestamp,groundRx,rocketRx,sniffedRx\n";
+    }
+
+    void print(std::ostream& os) const
+    {
+        os << timestamp << "," << groundRx << "," << rocketRx << ","
+           << sniffedRx << "\n";
+    }
+};
+}  // namespace Antennas
-- 
GitLab