175 lines
5.5 KiB
C#
175 lines
5.5 KiB
C#
using System.Net;
|
|
using DotNext;
|
|
|
|
namespace Peripherals.NetConfigClient;
|
|
|
|
static class NetConfigAddr
|
|
{
|
|
const UInt32 BASE = 0x30A7_0000;
|
|
|
|
public static readonly UInt32[] HOST_IP = { BASE + 0, BASE + 1, BASE + 2, BASE + 3 };
|
|
public static readonly UInt32[] BOARD_IP = { BASE + 4, BASE + 5, BASE + 6, BASE + 7 };
|
|
public static readonly UInt32[] HOST_MAC = { BASE + 8, BASE + 9, BASE + 10, BASE + 11, BASE + 12, BASE + 13 };
|
|
public static readonly UInt32[] BOARD_MAC = { BASE + 14, BASE + 15, BASE + 16, BASE + 17, BASE + 18, BASE + 19 };
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// [TODO:description]
|
|
/// </summary>
|
|
public class NetConfig
|
|
{
|
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
readonly int timeout = 2000;
|
|
readonly int taskID;
|
|
readonly int port;
|
|
readonly string address;
|
|
private IPEndPoint ep;
|
|
|
|
/// <summary>
|
|
/// [TODO:description]
|
|
/// </summary>
|
|
/// <param name="address">[TODO:parameter]</param>
|
|
/// <param name="port">[TODO:parameter]</param>
|
|
/// <param name="taskID">[TODO:parameter]</param>
|
|
/// <param name="timeout">[TODO:parameter]</param>
|
|
/// <returns>[TODO:return]</returns>
|
|
public NetConfig(string address, int port, int taskID, int timeout = 2000)
|
|
{
|
|
if (timeout < 0)
|
|
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
|
this.address = address;
|
|
this.taskID = taskID;
|
|
this.port = port;
|
|
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
|
this.timeout = timeout;
|
|
}
|
|
|
|
/// <summary>
|
|
/// [TODO:description]
|
|
/// </summary>
|
|
/// <param name="ip">[TODO:parameter]</param>
|
|
/// <returns>[TODO:return]</returns>
|
|
public async ValueTask<Result<bool>> SetHostIP(IPAddress ip)
|
|
{
|
|
// 清除UDP服务器接收缓冲区
|
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
|
|
|
var ipBytes = ip.GetAddressBytes();
|
|
|
|
{
|
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_IP, ipBytes, this.timeout);
|
|
if (!ret.IsSuccessful)
|
|
{
|
|
logger.Error($"Failed to set host IP: {ret.Error}");
|
|
return new(ret.Error);
|
|
}
|
|
|
|
if (!ret.Value)
|
|
{
|
|
logger.Error($"Failed to set host IP: operation returned false");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// [TODO:description]
|
|
/// </summary>
|
|
/// <param name="ip">[TODO:parameter]</param>
|
|
/// <returns>[TODO:return]</returns>
|
|
public async ValueTask<Result<bool>> SetBoardIP(IPAddress ip)
|
|
{
|
|
// 清除UDP服务器接收缓冲区
|
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
|
|
|
var ipBytes = ip.GetAddressBytes();
|
|
|
|
{
|
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_IP, ipBytes, this.timeout);
|
|
if (!ret.IsSuccessful)
|
|
{
|
|
logger.Error($"Failed to set board IP: {ret.Error}");
|
|
return new(ret.Error);
|
|
}
|
|
|
|
if (!ret.Value)
|
|
{
|
|
logger.Error($"Failed to set board IP: operation returned false");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// [TODO:description]
|
|
/// </summary>
|
|
/// <param name="macAddress">[TODO:parameter]</param>
|
|
/// <returns>[TODO:return]</returns>
|
|
public async ValueTask<Result<bool>> SetHostMAC(byte[] macAddress)
|
|
{
|
|
if (macAddress == null)
|
|
throw new ArgumentNullException(nameof(macAddress));
|
|
if (macAddress.Length != 6)
|
|
throw new ArgumentException("MAC address must be 6 bytes", nameof(macAddress));
|
|
|
|
// 清除UDP服务器接收缓冲区
|
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
|
|
|
{
|
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_MAC, macAddress, this.timeout);
|
|
if (!ret.IsSuccessful)
|
|
{
|
|
logger.Error($"Failed to set host MAC address: {ret.Error}");
|
|
return new(ret.Error);
|
|
}
|
|
|
|
if (!ret.Value)
|
|
{
|
|
logger.Error($"Failed to set host MAC address: operation returned false");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// [TODO:description]
|
|
/// </summary>
|
|
/// <param name="macAddress">[TODO:parameter]</param>
|
|
/// <returns>[TODO:return]</returns>
|
|
public async ValueTask<Result<bool>> SetBoardMAC(byte[] macAddress)
|
|
{
|
|
if (macAddress == null)
|
|
throw new ArgumentNullException(nameof(macAddress));
|
|
if (macAddress.Length != 6)
|
|
throw new ArgumentException("MAC address must be 6 bytes", nameof(macAddress));
|
|
|
|
// 清除UDP服务器接收缓冲区
|
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
|
|
|
{
|
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_MAC, macAddress, this.timeout);
|
|
if (!ret.IsSuccessful)
|
|
{
|
|
logger.Error($"Failed to set board MAC address: {ret.Error}");
|
|
return new(ret.Error);
|
|
}
|
|
|
|
if (!ret.Value)
|
|
{
|
|
logger.Error($"Failed to set board MAC address: operation returned false");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|