Add XML documentation; Add copyright headers; Small code cleanup

main
php 1 year ago
parent bab878ade4
commit 23cb53a9cb

@ -1,9 +1,24 @@
using YamlDotNet.Serialization; // SPDX-License-Identifier: AGPL-3.0-only
using System.IO; /**
* Digital Voice Modem - DVMUSRP
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DVMUSRP
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2024 Caleb, KO4UYJ
*
*/
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization.NamingConventions;
namespace dvmusrp namespace dvmusrp
{ {
/// <summary>
/// Configuration object
/// </summary>
public class Config public class Config
{ {
public DvmConfig dvm = new DvmConfig(); public DvmConfig dvm = new DvmConfig();
@ -11,6 +26,11 @@ namespace dvmusrp
public Config() { /* stub */ } public Config() { /* stub */ }
/// <summary>
/// Helper to load YAML config file
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static Config Load(string filePath) public static Config Load(string filePath)
{ {
var yamlContent = File.ReadAllText(filePath); var yamlContent = File.ReadAllText(filePath);
@ -21,6 +41,9 @@ namespace dvmusrp
} }
} }
/// <summary>
/// DVM Configuration object
/// </summary>
public class DvmConfig public class DvmConfig
{ {
public string ReceiveAddress { get; set; } public string ReceiveAddress { get; set; }
@ -32,6 +55,9 @@ namespace dvmusrp
public DvmConfig() { /* sub */ } public DvmConfig() { /* sub */ }
} }
/// <summary>
/// USRP Configuration object
/// </summary>
public class UsrpConfig public class UsrpConfig
{ {
public string ReceiveAddress { get; set; } public string ReceiveAddress { get; set; }

@ -1,19 +1,49 @@
using System.Net.Sockets; // SPDX-License-Identifier: AGPL-3.0-only
/**
* Digital Voice Modem - DVMUSRP
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DVMUSRP
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2024 Caleb, KO4UYJ
*
*/
using System.Net.Sockets;
using System.Text; using System.Text;
namespace dvmusrp namespace dvmusrp
{ {
/// <summary>
/// DVM networking class
/// </summary>
public class NetworkDVM : Network public class NetworkDVM : Network
{ {
private NetworkUSRP usrpNetwork; private NetworkUSRP usrpNetwork;
/// <summary>
/// Creates an instance of <see cref="NetworkDVM"/>
/// </summary>
/// <param name="receivePort"></param>
/// <param name="sendPort"></param>
/// <param name="receiveAddress"></param>
/// <param name="sendAddress"></param>
public NetworkDVM(int receivePort, int sendPort, string receiveAddress, string sendAddress) : base(receivePort, sendPort, receiveAddress, sendAddress) { } public NetworkDVM(int receivePort, int sendPort, string receiveAddress, string sendAddress) : base(receivePort, sendPort, receiveAddress, sendAddress) { }
/// <summary>
/// Helper to set the instance of <see cref="NetworkUSRP"/>
/// </summary>
/// <param name="usrpNetwork"></param>
public void SetUSRPNetwork(NetworkUSRP usrpNetwork) public void SetUSRPNetwork(NetworkUSRP usrpNetwork)
{ {
this.usrpNetwork = usrpNetwork; this.usrpNetwork = usrpNetwork;
} }
/// <summary>
/// Start the receive loop
/// </summary>
public override void Start() public override void Start()
{ {
Task.Run(async () => Task.Run(async () =>
@ -37,28 +67,43 @@ namespace dvmusrp
}); });
} }
/// <summary>
/// Forward, parse, and convert DVM to USRP
/// </summary>
/// <param name="packet"></param>
private void ForwardToUSRP(byte[] packet) private void ForwardToUSRP(byte[] packet)
{ {
byte[] usrpData = new byte[352]; byte[] usrpData = new byte[352]; // 32 byte header + PCM data
byte[] audio = new byte[320]; byte[] audio = new byte[320];
byte[] usrpHeader = new byte[32]; byte[] usrpHeader = new byte[32];
if (packet.Length != 324) if (packet.Length != 324)
{ {
Console.WriteLine("DVM meta data is not yet supported"); Console.WriteLine($"Unexpected DVM data length: {packet.Length}");
return; return;
} }
if (packet.Length == 332)
{
Array.Copy(packet, 12, audio, 0, audio.Length); // Skip DVM meta data
} else
{
Array.Copy(packet, 4, audio, 0, audio.Length); // No meta data, just skip PCM length
}
Array.Copy(Encoding.ASCII.GetBytes("USRP"), usrpHeader, 4); Array.Copy(Encoding.ASCII.GetBytes("USRP"), usrpHeader, 4);
Array.Copy(usrpHeader, usrpData, usrpHeader.Length); Array.Copy(usrpHeader, usrpData, usrpHeader.Length);
Array.Copy(packet, 4, audio, 0, audio.Length);
Array.Copy(audio, 0, usrpData, 32, audio.Length); Array.Copy(audio, 0, usrpData, 32, audio.Length);
// Console.WriteLine(Utils.HexDump(usrpData)); // Console.WriteLine("USRP DATA:\n" + Utils.HexDump(usrpData));
usrpNetwork.SendData(usrpData); usrpNetwork.SendData(usrpData);
} }
/// <summary>
/// Helper to send data to DVM
/// </summary>
/// <param name="data"></param>
public void SendData(byte[] data) public void SendData(byte[] data)
{ {
Send(data, sendAddress, sendPort); Send(data, sendAddress, sendPort);

@ -1,19 +1,48 @@
using System.Net.Sockets; // SPDX-License-Identifier: AGPL-3.0-only
using System.Text; /**
* Digital Voice Modem - DVMUSRP
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DVMUSRP
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2024 Caleb, KO4UYJ
*
*/
using System.Net.Sockets;
namespace dvmusrp namespace dvmusrp
{ {
/// <summary>
/// USRP networking class
/// </summary>
public class NetworkUSRP : Network public class NetworkUSRP : Network
{ {
private NetworkDVM dvmNetwork; private NetworkDVM dvmNetwork;
/// <summary>
/// Creates an instance of <see cref="NetworkDVM"/>
/// </summary>
/// <param name="receivePort"></param>
/// <param name="sendPort"></param>
/// <param name="receiveAddress"></param>
/// <param name="sendAddress"></param>
public NetworkUSRP(int receivePort, int sendPort, string receiveAddress, string sendAddress) : base(receivePort, sendPort, receiveAddress, sendAddress) { } public NetworkUSRP(int receivePort, int sendPort, string receiveAddress, string sendAddress) : base(receivePort, sendPort, receiveAddress, sendAddress) { }
/// <summary>
/// Helper to set the <see cref="NetworkDVM"/> instance
/// </summary>
/// <param name="dvmNetwork"></param>
public void SetDVMNetwork(NetworkDVM dvmNetwork) public void SetDVMNetwork(NetworkDVM dvmNetwork)
{ {
this.dvmNetwork = dvmNetwork; this.dvmNetwork = dvmNetwork;
} }
/// <summary>
/// Start the receive loop
/// </summary>
public override void Start() public override void Start()
{ {
Task.Run(async () => Task.Run(async () =>
@ -37,10 +66,14 @@ namespace dvmusrp
}); });
} }
/// <summary>
/// Forward, parse, and convert USRP to DVM
/// </summary>
/// <param name="packet"></param>
private void ForwardToDVM(byte[] packet) private void ForwardToDVM(byte[] packet)
{ {
// TODO: Parse meta data for possible DVM use // TODO: Parse USRP header for possible DVM use
byte[] audio = new byte[324]; byte[] audio = new byte[324]; // PCM length + PCM data
byte[] usrpHeader = new byte[32]; byte[] usrpHeader = new byte[32];
Array.Copy(packet, usrpHeader, usrpHeader.Length); Array.Copy(packet, usrpHeader, usrpHeader.Length);
@ -62,6 +95,10 @@ namespace dvmusrp
dvmNetwork.SendData(audio); dvmNetwork.SendData(audio);
} }
/// <summary>
/// Helper to send data to USRP
/// </summary>
/// <param name="data"></param>
public void SendData(byte[] data) public void SendData(byte[] data)
{ {
Send(data, sendAddress, sendPort); Send(data, sendAddress, sendPort);

@ -1,12 +1,23 @@
using System; // SPDX-License-Identifier: AGPL-3.0-only
using System.Collections.Generic; /**
using System.Linq; * Digital Voice Modem - DVMUSRP
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DVMUSRP
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2024 Caleb, KO4UYJ
*
*/
using System.Net.Sockets; using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace dvmusrp namespace dvmusrp
{ {
/// <summary>
/// Base networking class for UDP communications
/// </summary>
public abstract class Network public abstract class Network
{ {
protected int receivePort; protected int receivePort;
@ -17,6 +28,13 @@ namespace dvmusrp
protected UdpClient udpClient; protected UdpClient udpClient;
/// <summary>
/// Creatse an instance of <see cref="Network"/>
/// </summary>
/// <param name="receivePort"></param>
/// <param name="sendPort"></param>
/// <param name="receiveAddress"></param>
/// <param name="sendAddress"></param>
protected Network(int receivePort, int sendPort, string receiveAddress, string sendAddress) protected Network(int receivePort, int sendPort, string receiveAddress, string sendAddress)
{ {
this.receivePort = receivePort; this.receivePort = receivePort;
@ -25,8 +43,17 @@ namespace dvmusrp
this.sendAddress = sendAddress; this.sendAddress = sendAddress;
} }
/// <summary>
/// Start the receive loop
/// </summary>
public abstract void Start(); public abstract void Start();
/// <summary>
/// Helper to send data
/// </summary>
/// <param name="data"></param>
/// <param name="destinationIp"></param>
/// <param name="destinationPort"></param>
protected void Send(byte[] data, string destinationIp, int destinationPort) protected void Send(byte[] data, string destinationIp, int destinationPort)
{ {
using (var client = new UdpClient()) using (var client = new UdpClient())

@ -1,7 +1,28 @@
using dvmusrp; // SPDX-License-Identifier: AGPL-3.0-only
/**
* Digital Voice Modem - DVMUSRP
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DVMUSRP
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2024 Caleb, KO4UYJ
*
*/
using dvmusrp;
/// <summary>
///
/// </summary>
internal class Program internal class Program
{ {
/// <summary>
/// Application entry point
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
var configFilePath = GetConfigFilePath(args); var configFilePath = GetConfigFilePath(args);
@ -12,10 +33,10 @@ internal class Program
return; return;
} }
var config = Config.Load(configFilePath); Config config = Config.Load(configFilePath);
var usrpNetwork = new NetworkUSRP(config.usrp.ReceivePort, config.usrp.SendPort, config.usrp.ReceiveAddress, config.usrp.SendAddress); NetworkUSRP usrpNetwork = new NetworkUSRP(config.usrp.ReceivePort, config.usrp.SendPort, config.usrp.ReceiveAddress, config.usrp.SendAddress);
var dvmNetwork = new NetworkDVM(config.dvm.ReceivePort, config.dvm.SendPort, config.dvm.ReceiveAddress, config.dvm.ReceiveAddress); NetworkDVM dvmNetwork = new NetworkDVM(config.dvm.ReceivePort, config.dvm.SendPort, config.dvm.ReceiveAddress, config.dvm.ReceiveAddress);
usrpNetwork.SetDVMNetwork(dvmNetwork); usrpNetwork.SetDVMNetwork(dvmNetwork);
dvmNetwork.SetUSRPNetwork(usrpNetwork); dvmNetwork.SetUSRPNetwork(usrpNetwork);
@ -23,9 +44,26 @@ internal class Program
usrpNetwork.Start(); usrpNetwork.Start();
dvmNetwork.Start(); dvmNetwork.Start();
Console.WriteLine("USRP: ");
Console.WriteLine($" Receive Port: {config.usrp.ReceivePort}");
Console.WriteLine($" Send Port: {config.usrp.SendPort}");
Console.WriteLine($" Receive Address: {config.usrp.ReceiveAddress}");
Console.WriteLine($" Send Address: {config.usrp.SendAddress}");
Console.WriteLine("DVM: ");
Console.WriteLine($" Receive Port: {config.dvm.ReceivePort}");
Console.WriteLine($" Send Port: {config.dvm.SendPort}");
Console.WriteLine($" Receive Address: {config.dvm.ReceiveAddress}");
Console.WriteLine($" Send Address: {config.dvm.SendAddress}");
Console.WriteLine("DVMUSRP Started");
await Task.Delay(Timeout.Infinite); await Task.Delay(Timeout.Infinite);
} }
/// <summary>
/// Helper to psrse args
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
static string GetConfigFilePath(string[] args) static string GetConfigFilePath(string[] args)
{ {
for (int i = 0; i < args.Length; i++) for (int i = 0; i < args.Length; i++)

@ -1,11 +1,23 @@
# DVMBridge UDP configuration
dvm: dvm:
# Receive address
receiveAddress: 127.0.0.1 receiveAddress: 127.0.0.1
# Receive port
receivePort: 33000 receivePort: 33000
# Send port
sendPort: 35000 sendPort: 35000
# Send address
sendAddress: 127.0.0.1 sendAddress: 127.0.0.1
# USRP UDP Configuration
usrp: usrp:
# Receive address
receiveAddress: 0.0.0.0 receiveAddress: 0.0.0.0
# Receive port
receivePort: 34001 receivePort: 34001
# Send port
sendPort: 32001 sendPort: 32001
# Send address
sendAddress: 192.168.198.131 sendAddress: 192.168.198.131
Loading…
Cancel
Save

Powered by TurnKey Linux.