SafeDispatch/LibrarySDR/Responses/GwSDSReportResponse.cs

157 lines
6.4 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using LibrarySDR.Enums;
using LibrarySDR.Flags;
using LibrarySDR.Requests;
using System;
using System.Globalization;
using System.Text;
using static LibrarySDR.Helpers.BinaryHexHelper;
namespace LibrarySDR.Responses
{
public class GwSDSReportResponse
{
public PDUType PduType => PDUType.REPORT_REQ_RESP;
private Int64 sourceISSI;
public Int64 SourceISSI { get { return sourceISSI; } }
private Int64 destinationISSI;
public Int64 DestinationISSI { get { return destinationISSI; } }
private ProtocolIdentifier pi;
public ProtocolIdentifier ProtocolIdentifier { get { return pi; } }
private Int16 messageReference;
public Int16 MessageRefence { get { return messageReference; } }
private Int16 areaSelection;
public Int16 AreaSelection { get { return areaSelection; } }
private Int16 validityPeriod;
public Int16 ValidityPeriod { get { return validityPeriod; } }
private Int64 forwardAddress;
public Int64 ForwardAddress { get { return forwardAddress; } }
private ReportFlag reportFlag;
public ReportFlag ReportFlag { get { return reportFlag; } }
private DeliveredStatus deliveryStatus;
public DeliveredStatus DeliveryStatus { get { return deliveryStatus; } }
public GwSDSReportResponse(String hexResponsePayload)
{
String resp = hexResponsePayload;
int i = 0;
int prevIdx = 0;
// get source SSI (32 bits = 4 byte = 8 hex characters)
String hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 4 * 2));
Int64.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out sourceISSI);
// get destination SSI (32 bits = 4 byte = 8 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 4 * 2));
Int64.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out destinationISSI);
// get protocol identifier (8 bits = 1 byte = 2 hex characters)
Int16 pi = 0;
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out pi);
this.pi = (ProtocolIdentifier)pi;
// get message reference (8 bits = 1 byte = 2 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out messageReference);
// get area selection (8 bits = 1 byte = 2 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out areaSelection);
// get delivery status (8 bits = 1 byte = 2 hex characters)
Int16 deliveryStat = 0;
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out deliveryStat);
// parse the data flag
deliveryStatus = (DeliveredStatus)deliveryStat;
// get validity period (8 bits = 1 byte = 2 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out validityPeriod);
// get forward address (32 bits = 4 byte = 8 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 4 * 2));
Int64.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out forwardAddress);
// get report flags (8 bits = 1 byte = 2 hex characters)
Int16 repFlag;
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out repFlag);
// parse the report flag
reportFlag = new ReportFlag(repFlag);
}
public static String GetInstance(Int64 sourceISSI, Int64 destinationISSI, ProtocolIdentifier protocolIdentifier, Int16 messageReference,
DeliveredStatus deliveryStatus)
{
StringBuilder sb = new StringBuilder("");
// PDU Type (8 bits)
sb.Append(GetBits((int)PDUType.REPORT_REQ_RESP, 8));
// Source ISSI (32 bits)
sb.Append(GetBits(sourceISSI, 32));
// Destination ISSI (32 bits)
sb.Append(GetBits(destinationISSI, 32));
// Protocol Identifier (8 bits)
sb.Append(GetBits((int)protocolIdentifier, 8));
// Message Reference (8 bits)
sb.Append(GetBits((int)messageReference, 8));
// Area selection (8 bits)
sb.Append(GetBits((int)0x00, 8));
// Delivery Status (8 bits)
sb.Append(GetBits((int)deliveryStatus, 8));
// Validity period (8 bits)
sb.Append(GetBits((int)0x00, 8));
// Forward address (32 bits)
sb.Append(GetBits((int)0x00, 32));
// Flags (8 bits)
sb.Append(GetBits((int)0x00, 8));
// get the header with the length
String header = Header.GetHeaderBinary(sb.ToString().Length / 8);
// add header in front of the request
return BinaryStringToHexString($"{header}{sb.ToString()}", Padding.RIGHT);
}
public static byte[] GetBytes(Int64 sourceISSI, Int64 destinationISSI, ProtocolIdentifier protocolIdentifier, Int16 messageReference,
DeliveredStatus deliveryStatus)
{
String res = GetInstance(sourceISSI, destinationISSI, protocolIdentifier, messageReference, deliveryStatus);
byte[] response = new byte[res.Length / 2];
for (int i = 0; i < res.Length / 2; i++)
response[i] = (byte)Convert.ToInt16(res.Substring(i * 2, 2), 16);
return response;
}
public override String ToString()
{
return $"Source ISSI : {sourceISSI} | Destination ISSI : {destinationISSI} | Message Ref : {messageReference} | Delivery Status : {deliveryStatus} ";
}
}
}