SafeDispatch/SafeMobileLIB_DLL/MessageDecoders/ARSdecoder.cs

144 lines
4.3 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using SafeMobileLib;
namespace SafeMobileLib.MessageDecoders
{
public class ARSdecoder
{
private string SUID;
private byte[] data;
private DBsubsOperationManager DBsub;
public Boolean isOFF = true;
public ARSdecoder(string suid, byte[] data, DBsubsOperationManager DBsub)
{
SUID = suid;
this.data = data;
this.DBsub = DBsub;
header_T reth = DecodePacket(data);
switch (reth.pdu_type)
{
case 0x00: // registration
goto case 0x0F;
case 0x0F: // query reply
DBsub.updateSUStatus(suid.ToString(), 1);
isOFF = false;
break;
case 0x01: // deregistration
DBsub.updateSUStatus(suid.ToString(), 0);
break;
default: break;
}
}
public ARSdecoder()
{
}
public header_T DecodePacket(Byte[] data)
{
int i, pdata;
Console.WriteLine("--------------------");
pdata = (int)data[0];
pdata <<= 8;
pdata |= (int)data[1];
// ----------------------------------------
// parse header
// ----------------------------------------
header_T header = new header_T();
header.ext = false;
if ((data[2] & 0x80) != 0)
header.ext = true;
header.ack = false;
if ((data[2] & 0x40) != 0)
header.ack = true;
header.priority = false;
if ((data[2] & 0x20) != 0)
header.priority = true;
header.cntl = false;
if ((data[2] & 0x10) != 0)
header.cntl = true;
header.pdu_type = (byte)(data[2] & 0x0F);
Console.WriteLine("Header : ext=" + header.ext + " ack=" + header.ack + " priority=" + header.priority + " cntl=" + header.cntl + " type=" + header.pdu_type);
switch (header.pdu_type)
{
case 0x00: Console.WriteLine("ARS Registration"); break;
case 0x0F: Console.WriteLine("Query ACK"); break;
case 0x01: Console.WriteLine("Deregistration message"); break;
default: Console.WriteLine("Unknown ARS message"); break;
}
i = 3;
if (i >= pdata + 2)
{
Console.WriteLine("--------------------");
return header;
}
if (header.ext)
{
// read second header octet
byte evt = data[i];
evt &= 0x70;
evt >>= 5;
switch (evt)
{
case 0: header.events = header_event.Unqualified_Event; break;
case 1: header.events = header_event.Initial_Event; break;
case 2: header.events = header_event.Refresh_Event; break;
default: header.events = header_event.UNKNOWN; break;
}
header.events = (header_event)evt;
header.encoding = (byte)(data[i] & 0x0f);
Console.WriteLine("HeaderExt : event=" + header.events + " encoding=" + header.encoding);
i++;
}
if (i >= pdata + 2)
{
Console.WriteLine("--------------------");
return header;
}
// ----------------------------------------
// parse address len
// ----------------------------------------
int addr_len = data[i];
i++;
if (i >= pdata + 2)
{
Console.WriteLine("--------------------");
return header;
}
i -= 2;
Console.WriteLine("--------------------");
return header;
}
//AUx
public struct header_T
{
public bool ext, ack, priority, cntl;
public byte pdu_type;
public header_event events;
public byte encoding;
}
}
}