SafeDispatch/MotoTrbo_GW/SMSThread.cs
2024-02-22 18:43:59 +02:00

175 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using SafeMobileLib;
using System.Threading;
using MotoTRBO_GW;
namespace MotoTrbo_GW
{
class SMSThread
{
public UInt16 port;
private byte[] data = new byte[1024];
private UdpMulticast udpMulticast;
private static UdpClient udpClient;
private String mIP;
private Int32 mPort;
public SMSThread(UInt16 smsPort, String multicastID, String multicastPort)
{
port = smsPort;
mIP = multicastID;
mPort = Int32.Parse(multicastPort);
}
public void handleConnection()
{
Console.WriteLine("SMS Thread - Initialized on port " + port);
udpClient = null;
try
{
IPEndPoint ie;
ie = new IPEndPoint(IPAddress.Any, port);
udpClient = new UdpClient(ie);
}
catch (Exception ex)
{
Console.WriteLine("SMS Thread handleConnection exception: " + ex.ToString());
}
try
{
udpMulticast = new UdpMulticast(mIP, mPort);
Console.WriteLine("SMS thread successfully registered to multicast group");
}
catch (Exception ex)
{
Console.WriteLine("SMS Thread exception while joining the multicast group: " + ex.ToString());
}
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
// Blocks until a message returns on this socket from a remote host.
Byte[] receivedBytes = udpClient.Receive(ref remoteIpEndPoint);
char[] separator = { '.' };
string[] su = remoteIpEndPoint.Address.ToString().Split(separator);
uint radioID = (Convert.ToUInt32(su[1])) * 256 * 256 + (Convert.ToUInt32(su[2])) * 256 + Convert.ToUInt32(su[3]);
string suid = radioID.ToString();
Console.Write("SMS Thread received data from radio " + radioID + ": ");
Utils.printBytesArray(receivedBytes);
header_T hret = DecodePacket(receivedBytes);
if (hret.ack)
{
Thread.Sleep(1000);
// The client needs an ACK
Byte[] sendBytes = new Byte[5];
sendBytes[0] = 0x00; // len (2B)
sendBytes[1] = 0x03;
sendBytes[2] = 0xBF; // first header (req)
sendBytes[3] = 0x00; // addr len
sendBytes[4] = hret.seq_no; // 2nd header (opt)
Console.WriteLine("SMS thread ACK sent: " + Utils.Byte2String(sendBytes, 0, sendBytes[1] + 2));
udpClient.Send(sendBytes, sendBytes[1] + 2, remoteIpEndPoint);
}
//put information on message bus
Byte[] toSendMulticast = Utils.createMulticastMessage(132, suid, receivedBytes);
udpMulticast.Send(toSendMulticast, toSendMulticast.Length);
Console.WriteLine("SMS thread sent data to message bus");
Thread.Sleep(1000);
}
}
header_T DecodePacket(Byte[] data)
{
header_T hret = new header_T();
int i, j, pdata;
pdata = (int)data[0];
pdata <<= 8;
pdata |= (int)data[1];
// parse header
int header = data[2];
hret.header = data[2];
if ((header & 0x80) != 0)
hret.ext = true;
else hret.ext = false;
if ((header & 0x40) != 0)
hret.ack = true;
else hret.ack = false;
if ((header & 0x10) != 0)
hret.cntl = true;
else hret.cntl = false; // txt message
hret.pdu_type = (byte)(header & 0x0f);
// parse address
int addrsize = data[3];
i = 4;
//Console.Write("Address: ");
for (j = 0; j < addrsize; j++)
{
//Console.Write(data[i + j].ToString("X"));
}
i += j;
// parse rest of headers
if (hret.ext)
{
byte h2 = data[i];
if ((h2 & 0x80) != 0)
hret.ext2 = true;
else hret.ext2 = false;
hret.seq_no = (byte)(h2 & 0x1F);
i++;
if (hret.ext2)
{ // parse third header
hret.encoding = (byte)(data[i] & 0x0f);
i++;
}
}
int crc = 0;
if ((!hret.cntl) && (hret.pdu_type == 0))
{ // the rest is the txt message
Char[] cs = new Char[100];
int k;
for (j = i, k = 0; j < pdata + 2; j++)
{
//Console.Write(" 0x" + data[j].ToString("X"));
if (data[j] != 0)
{
cs[k++] = (Char)data[j];
crc |= (Char)data[j];
}
}
Console.WriteLine();
// save message in inbox ht
string s = new string(cs, 0, k);
s.Replace("\r\n", "");
s = s.Replace(System.Environment.NewLine, string.Empty);
Console.WriteLine("Message [" + s + "]");
}
return hret;
}
}
}