From e0bfd3109afc195a79513aedde8e5191c5b7261f Mon Sep 17 00:00:00 2001
From: Stephen D <webmaster@scd31.com>
Date: Mon, 4 Mar 2024 07:43:13 -0400
Subject: [PATCH] add basic debug flag

---
 config.example.toml |  1 +
 src/config.rs       |  2 ++
 src/gate.rs         |  3 ++-
 src/main.rs         |  2 ++
 src/util.rs         | 21 ++++++++++++++++-----
 5 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/config.example.toml b/config.example.toml
index 765dd9d..d70a741 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -3,6 +3,7 @@ ssid = 0
 icon = 0
 frequency = 430_500_000 # The standard CATS frequency
 enable_tx = true # If false, will be an RX-only I-Gate
+debug = false # If true, print additional packet debug information
 
 [felinet]
 address = "https://felinet.cats.radio"
diff --git a/src/config.rs b/src/config.rs
index e6723ac..96f8f0d 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -47,6 +47,8 @@ pub struct Config {
     pub frequency: u32,
     #[serde(default = "default_enable_tx")]
     pub enable_tx: bool,
+    #[serde(default)]
+    pub debug: bool,
     pub felinet: Option<FelinetConfig>,
     pub beacon: BeaconConfig,
 }
diff --git a/src/gate.rs b/src/gate.rs
index 7c00072..bbf71e1 100644
--- a/src/gate.rs
+++ b/src/gate.rs
@@ -184,6 +184,7 @@ pub async fn packet_handler(
     packet_send: mpsc::Sender<Vec<u8>>,
     mut packet_receive: mpsc::Receiver<(Vec<u8>, f64)>,
     uuid: Uuid,
+    debug: bool,
 ) -> anyhow::Result<()> {
     loop {
         let (packet, rssi) = packet_receive
@@ -192,7 +193,7 @@ pub async fn packet_handler(
             .context("Packet receive channel died")?;
 
         let mut buf = [0; MAX_PACKET_LEN];
-        if let Ok(mut packet) = attempt_decode(&packet, &mut buf, rssi) {
+        if let Some(mut packet) = attempt_decode(&packet, &mut buf, rssi, debug) {
             let mut buf2 = [0; MAX_PACKET_LEN];
             let mut internet_packet = packet.clone_backing(&mut buf2);
             match append_internet_to_packet_route(&callsign, ssid, rssi, &mut internet_packet) {
diff --git a/src/main.rs b/src/main.rs
index 3253781..f4b0963 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -143,6 +143,7 @@ async fn gate_forever(
         let packet_send = packet_send.clone();
         let ssid = config.ssid;
         let callsign = config.callsign.clone();
+        let debug = config.debug;
 
         tokio::task::spawn(async move {
             if let Err(e) = packet_handler(
@@ -152,6 +153,7 @@ async fn gate_forever(
                 packet_send,
                 packet_receive,
                 uuid,
+                debug,
             )
             .await
             {
diff --git a/src/util.rs b/src/util.rs
index eda22ad..776e44d 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,5 +1,4 @@
 use crate::radio::MAX_PACKET_LEN;
-use anyhow::anyhow;
 use colored::{Color, Colorize};
 use ham_cats::{
     identity::Identity,
@@ -126,13 +125,25 @@ pub fn attempt_decode<'a>(
     packet: &[u8],
     buf: &'a mut [u8; MAX_PACKET_LEN],
     rssi: f64,
-) -> anyhow::Result<Packet<'a, MAX_PACKET_LEN>> {
-    let packet: Packet<MAX_PACKET_LEN> =
-        Packet::fully_decode(packet, buf).map_err(|e| anyhow!("Could not decode packet: {e:?}"))?;
+    debug: bool,
+) -> Option<Packet<'a, MAX_PACKET_LEN>> {
+    if debug {
+        eprintln!("Attempt decode with len: {}", packet.len());
+    }
+    let packet: Packet<MAX_PACKET_LEN> = match Packet::fully_decode(packet, buf) {
+        Ok(x) => x,
+        Err(e) => {
+            if debug {
+                eprintln!("Could not decode packet: {e}");
+            }
+
+            return None;
+        }
+    };
 
     print_packet(&packet, Some(rssi));
 
-    Ok(packet)
+    Some(packet)
 }
 
 // i-gating is kind of weird.
-- 
GitLab