From 4ee7b34a86392dda68f2bcdd2cbf3429931fe2f1 Mon Sep 17 00:00:00 2001 From: Cameron Bielstein Date: Wed, 23 Apr 2025 23:00:12 -0700 Subject: [PATCH 1/3] Starting to sketch out new flag. --- src/APRSsharp/Program.cs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/APRSsharp/Program.cs b/src/APRSsharp/Program.cs index f15c15c..38e1339 100644 --- a/src/APRSsharp/Program.cs +++ b/src/APRSsharp/Program.cs @@ -5,6 +5,7 @@ using System.CommandLine; using System.CommandLine.Invocation; using System.Linq; + using System.Text; using System.Threading.Tasks; using AprsSharp.AprsIsClient; using AprsSharp.AprsParser; @@ -131,10 +132,6 @@ public static void Main(string[] args) aliases: new string[] { "--filter", "-f" }, getDefaultValue: () => AprsIsClient.AprsIsConstants.DefaultFilter, description: "A user filter parsed as a string. Should not include the word 'filter', just the logic string."), - new Option( - aliases: new string[] { "--verbosity", "-v" }, - getDefaultValue: () => LogLevel.Warning, - description: "Set the verbosity of console logging."), new Option( aliases: new string[] { "--display-unsupported" }, description: "If specified, includes output of unknown or unsupported info types values. If not, such packets are not displayed."), @@ -142,6 +139,10 @@ public static void Main(string[] args) aliases: new string[] { "--serialPort" }, getDefaultValue: () => null, description: "A serial port for use with serial TNCs."), + new Option( + aliases: new string[] { "--print-parse-failures" }, + description: "If specified, prints packets that failed to parse. Helpful for development." + ), }; #pragma warning restore CA1861 // Avoid constant arrays as arguments rootCommand.Name = "APRSsharp"; @@ -152,8 +153,8 @@ public static void Main(string[] args) // The parameters of the handler method are matched according to the names of the options rootCommand.Handler = CommandHandler - .Create(async (Mode mode, string callsign, string password, string server, int port, string filter, LogLevel verbosity, bool displayUnsupported, string? serialPort) - => await Execute(mode, callsign, password, server, port, filter, verbosity, displayUnsupported, serialPort)); + .Create(async (Mode mode, string callsign, string password, string server, int port, string filter, bool displayUnsupported, string? serialPort, bool printParseFailures) + => await Execute(mode, callsign, password, server, port, filter, displayUnsupported, serialPort, printParseFailures)); rootCommand.Invoke(args); } @@ -202,9 +203,9 @@ private static void RunTncMode(Tnc tnc, string callsign) /// A port to use for connection to a TNC via TCP. /// The filter that will be used for receiving the packets. /// This parameter shouldn't include the `filter` at the start, just the logic string itself. - /// The minimum level for an event to be logged to the console. /// If true, display packets with unsupported info field types. If false, such packets are not displayed. /// A serial port to use for connection to a TNC via serial connection. + /// Whether to print parse failures. /// A representing the asynchronous operation. private static async Task Execute( Mode mode, @@ -213,17 +214,10 @@ private static async Task Execute( string server, int port, string filter, - LogLevel verbosity, bool displayUnsupported, - string? serialPort) + string? serialPort, + bool printParseFailures) { - using ILoggerFactory loggerFactory = LoggerFactory.Create(config => - { - config.ClearProviders() - .AddConsole() - .SetMinimumLevel(verbosity); - }); - Program.displayUnsupported = displayUnsupported; switch (mode) From 656d163cad1a2c246005ccc4a8532a5a2743675b Mon Sep 17 00:00:00 2001 From: Cameron Bielstein Date: Mon, 12 May 2025 22:02:14 -0700 Subject: [PATCH 2/3] Switch to display-parse-failures and implements logic. --- src/APRSsharp/Program.cs | 56 +++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/APRSsharp/Program.cs b/src/APRSsharp/Program.cs index 38e1339..3ed0397 100644 --- a/src/APRSsharp/Program.cs +++ b/src/APRSsharp/Program.cs @@ -140,7 +140,7 @@ public static void Main(string[] args) getDefaultValue: () => null, description: "A serial port for use with serial TNCs."), new Option( - aliases: new string[] { "--print-parse-failures" }, + aliases: new string[] { "--display-parse-failures" }, description: "If specified, prints packets that failed to parse. Helpful for development." ), }; @@ -153,19 +153,38 @@ public static void Main(string[] args) // The parameters of the handler method are matched according to the names of the options rootCommand.Handler = CommandHandler - .Create(async (Mode mode, string callsign, string password, string server, int port, string filter, bool displayUnsupported, string? serialPort, bool printParseFailures) - => await Execute(mode, callsign, password, server, port, filter, displayUnsupported, serialPort, printParseFailures)); + .Create(async (Mode mode, string callsign, string password, string server, int port, string filter, bool displayUnsupported, string? serialPort, bool displayParseFailures) + => await Execute(mode, callsign, password, server, port, filter, displayUnsupported, serialPort, displayParseFailures)); rootCommand.Invoke(args); } - private static void RunTncMode(Tnc tnc, string callsign) + /// + /// Executes the logic for running in TNC mode. + /// + /// A with which to interface. + /// The callsign to use for sent messages. + /// Whether or not to print message parse failures to the command line. + private static void RunTncMode(Tnc tnc, string callsign, bool displayParseFailures) { tnc.FrameReceivedEvent += (sender, args) => { var byteArray = args.Data.ToArray(); - var packet = new Packet(byteArray); - PrintPacket(packet); + +#pragma warning disable CA1031 // Do not catch general exception types + try + { + var packet = new Packet(byteArray); + PrintPacket(packet); + } + catch (Exception ex) + { + if (displayParseFailures) + { + PrintParseFailure(ex, Encoding.ASCII.GetString(byteArray)); + } + } +#pragma warning restore CA1031 // Do not catch general exception types }; tnc.SetTxDelay(50); @@ -193,6 +212,18 @@ private static void RunTncMode(Tnc tnc, string callsign) while (true); } + /// + /// Prints a parse failure to the user. + /// + /// The that occurred. + /// The that failed to decode. + private static void PrintParseFailure(Exception ex, string attempted) + { + Console.WriteLine("Failed to decode."); + Console.WriteLine($" Packet: {attempted}"); + Console.WriteLine($" Exception: {ex}"); + } + /// /// Executes functionality using the provided command line arguments. /// @@ -205,7 +236,7 @@ private static void RunTncMode(Tnc tnc, string callsign) /// This parameter shouldn't include the `filter` at the start, just the logic string itself. /// If true, display packets with unsupported info field types. If false, such packets are not displayed. /// A serial port to use for connection to a TNC via serial connection. - /// Whether to print parse failures. + /// Whether to print parse failures. /// A representing the asynchronous operation. private static async Task Execute( Mode mode, @@ -216,7 +247,7 @@ private static async Task Execute( string filter, bool displayUnsupported, string? serialPort, - bool printParseFailures) + bool displayParseFailures) { Program.displayUnsupported = displayUnsupported; @@ -229,6 +260,11 @@ private static async Task Execute( using AprsIsClient n = new AprsIsClient(); n.ReceivedPacket += PrintPacket; + if (displayParseFailures) + { + n.DecodeFailed += PrintParseFailure; + } + Task receive = n.Receive(callsign, password, server, filter); while (true) @@ -254,7 +290,7 @@ private static async Task Execute( tcp.Connect(server, port); using Tnc tnc = new TcpTnc(tcp, 0); - RunTncMode(tnc, callsign); + RunTncMode(tnc, callsign, displayParseFailures); break; } @@ -272,7 +308,7 @@ private static async Task Execute( using SerialConnection serial = new SerialConnection(serialPort); using Tnc tnc = new SerialTnc(serial, 0); - RunTncMode(tnc, callsign); + RunTncMode(tnc, callsign, displayParseFailures); break; } From 378f5c42266a1d6cfc0d9e1d3d2f1595964b9b91 Mon Sep 17 00:00:00 2001 From: Cameron Bielstein Date: Mon, 12 May 2025 22:09:14 -0700 Subject: [PATCH 3/3] Fixes for the linter. --- src/APRSsharp/Program.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/APRSsharp/Program.cs b/src/APRSsharp/Program.cs index 3ed0397..a844bad 100644 --- a/src/APRSsharp/Program.cs +++ b/src/APRSsharp/Program.cs @@ -141,8 +141,7 @@ public static void Main(string[] args) description: "A serial port for use with serial TNCs."), new Option( aliases: new string[] { "--display-parse-failures" }, - description: "If specified, prints packets that failed to parse. Helpful for development." - ), + description: "If specified, prints packets that failed to parse. Helpful for development."), }; #pragma warning restore CA1861 // Avoid constant arrays as arguments rootCommand.Name = "APRSsharp";