SafeDispatch/LibrarySDR/Requests/PollSDSRequest.cs
2024-02-22 18:43:59 +02:00

154 lines
4.8 KiB
C#

using LibrarySDR.Enums;
using LibrarySDR.Flags;
using System;
using System.Globalization;
using System.Text;
using static LibrarySDR.Helpers.BinaryHexHelper;
namespace LibrarySDR.Requests
{
public class PollSDSRequest
{
private static int sendingReference = 0;
public static int SendingReference
{
get
{
// rest counter to value 0 after max value inside a byte
if (sendingReference > 255)
sendingReference = 0;
return sendingReference;
}
}
private static PDUType PduType = PDUType.DATA_REQ_RESP;
private static ProtocolIdentifier protocolIdentifier = ProtocolIdentifier.SimpleTextWithoutSDSTL;
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 Int16 flags;
public Int16 Flags { get { return flags; } }
private DataFlag dataFlag;
public DataFlag DataFlags { get { return dataFlag; } }
private Int16 lengthBits;
public Int16 LengthBits { get { return lengthBits; } }
private Int16 lengthBytes;
public Int16 LengthBytes { get { return lengthBytes; } }
private String message;
public String Message { get { return message; } }
private String hexMessage;
public String HexMessage { get { return hexMessage; } }
private static Int64 transactionId = 1;
public static Int64 TransactionId
{
get
{
return transactionId++;
}
}
public static String GetInstance(Int64 sourceISSI, Int64 destinationISSI, String text, bool isConfirmationReq)
{
StringBuilder sb = new StringBuilder("");
// PDU Type (8 bits)
sb.Append(GetBits((int)PduType, 8));
// Source ISSI (32 bits)
sb.Append(GetBits(sourceISSI, 32));
// Destination ISSI (32 bits)
sb.Append(GetBits(destinationISSI, 32));
// Protocol Identifier (8 bits)
bool Hytera = false;
sb.Append(GetBits(Hytera ? (int)ProtocolIdentifier.GPS : (int)ProtocolIdentifier.GPS, 8));
// Message reference (8 bits) - ignored for Simple Text)
++sendingReference;
sb.Append(GetBits(SendingReference, 8));
// Area selection (8 bits) - unused
sb.Append(GetBits(0x00, 8));
// Validity period (8 bits) - unused
sb.Append(GetBits(0x00, 8));
// Forward Address (32 bits) - ignored for Simple Text
sb.Append(GetBits(0x00, 32));
// Flags (8 bits)
sb.Append(GetBits(isConfirmationReq ? 0x40 : 0x00, 8));
// Length in bites (16 bits) + the encoding one
sb.Append(GetBits((text.Length/2) * 8, 16));
// Length in bytes (16 bits) + the encoding one
sb.Append(GetBits(text.Length/2, 16));
// Text message
for (int i = 0; i < text.Length / 2; i++)
{
sb.Append(GetBits(Convert.ToInt16(text.Substring(i * 2, 2), 16), 8));
}
//sb.Append(text);
/*
// Transaction Id
sb.Append(GetBits(TransactionId, 32));
// Gw Report
sb.Append(GetBits(0x01, 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, String text, bool isConfirmationReq)
{
String res = GetInstance(sourceISSI, destinationISSI, text, isConfirmationReq);
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;
}
}
}