SafeNet/.svn/pristine/2e/2ef3819fa54b05369028e5ac7287549a93ca0030.svn-base

259 lines
9.5 KiB
Plaintext
Raw Normal View History

2021-02-24 11:50:23 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using SafeNetLib;
using SN_Server.ServerMSGS;
using SN_Server;
/*
< rsp sid="SID" id="100111" mode="MOD" HW_Type="Hw_type"
subscriber="Phone_nr" RFID="RFID" time="TIME"
Latitude="" Longitude="" Speed="" ai1="" ai2="" ai3="" ai4=""
ai5="" ai6="" ai7="" ai8="" di="" ao1="" ao2="" do=""> </rsp>
*/
namespace MotoRepeater_SOC
{
class ReceiveSMSThread
{
public UInt16 port;
//public MotoTRBOGW parent;
public ReceiveSMSThread(ushort p_port)
{
port = p_port;
}
// -------------------------------------------------------------------
// Main
// -------------------------------------------------------------------
public void HandleConnection()
{
while (true)
{
try
{
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Utils.ConsWrite(DebugMSG_Type.SMS, "Init SMS Thread on port " + port +" ..waiting for SMS:");
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = MotoRepeater_GW.smsUDPclient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Utils.ConsWrite(DebugMSG_Type.SMS, "\n--------------------");
Utils.ConsWrite(DebugMSG_Type.SMS, "SMSThRecv(" + port + "): " + LocationThread.Byte2String(receiveBytes, 0, receiveBytes[1] + 2));
Utils.ConsWrite(DebugMSG_Type.SMS, "From " + RemoteIpEndPoint.Address.ToString() +
":" + RemoteIpEndPoint.Port.ToString());
header_T hret = DecodePacket(receiveBytes, RemoteIpEndPoint);
//we got ACK for message add it to DB
if ((hret.header == 0xBF) || (hret.header == 0x9F))
{
Utils.ConsWrite(DebugMSG_Type.SMS, "Got ACK with header:" + hret.header + " with seq_id :" + hret.seq_no);
lock (SM_SMS.waitConfSMSList.SyncRoot)
{
int index = -1;
int count = 0;
foreach (SMS msg in SM_SMS.waitConfSMSList)
{
if (msg.seq_no == hret.seq_no)
{
Utils.ConsWrite(DebugMSG_Type.SMS, "Posting in confSMSQueue:");
index = count;
SM_SMS.confSMSQueue.PostItem(msg);
}
count++;
}
if (index > -1)//we've found the message
{
Utils.ConsWrite(DebugMSG_Type.SMS, "Removing from waitConfSMSList");
SM_SMS.waitConfSMSList.RemoveAt(index);
}
}
}
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "Exception in SMSThread:HandleConnection()" +ex.ToString());
}
Thread.Sleep(1);
} // end while
}
// -------------------------------------------------------------------
// Aux functions
// -------------------------------------------------------------------
struct header_T
{
public string address;
public bool ext, ext2, ack, cntl;
public byte pdu_type;
public byte seq_no;
public byte encoding;
public byte header;
}
header_T DecodePacket(Byte[] data, IPEndPoint RemoteIpEndPoint)
{
header_T hret = new header_T();
int i, j, pdata;
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();
pdata = (int)data[0];
pdata <<= 8;
pdata |= (int)data[1];
/*
Utils.ConsWrite(DebugMSG_Type.SMS,"Length =" + pdata + "(0x" + data[0].ToString("X") + "," + data[1].ToString("X") + ")");
Console.Write("Data: ");
for (i = 2; i < pdata + 2; i++)
Console.Write(" 0x" + data[i].ToString("X"));
Utils.ConsWrite(DebugMSG_Type.SMS,);
*/
// parse header
int header = data[2];
hret.header = data[2];
//Utils.ConsWrite(DebugMSG_Type.SMS,"Header: " + header.ToString("X"));
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;
if (addrsize == 0)
Utils.ConsWrite(DebugMSG_Type.SMS,"no len");
else Utils.ConsWrite(DebugMSG_Type.SMS,"");
// 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++;
Utils.ConsWrite(DebugMSG_Type.SMS,"Seq no: " + hret.seq_no);
if (hret.ext2)
{ // parse third header
hret.encoding = (byte)(data[i] & 0x0f);
//Utils.ConsWrite(DebugMSG_Type.SMS,"Encoding: " + hret.encoding);
i++;
}
}
if (hret.ack)
{
Thread.Sleep(500);
// The client needs an ACK
Byte[] sendBytes = new Byte[10];
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)
MotoRepeater_GW.smsUDPclient.Send(sendBytes, sendBytes[1] + 2, RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port);
Utils.ConsWrite(DebugMSG_Type.SMS,"SMS ACK sent");
}
int crc = 0;
if ((!hret.cntl) && (hret.pdu_type == 0))
{ // the rest is the txt message
//Console.Write("SMS 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];
}
}
// save message in inbox ht
string s = new string(cs, 0, k);
s.Replace("\r\n", "");
s = s.Replace(System.Environment.NewLine, string.Empty);
Utils.ConsWrite(DebugMSG_Type.SMS,"Message [" + s + "]");
if (s.StartsWith("Rfid"))
{
string rfid_string = new string(cs, 4, s.Length - 4);
Utils.ConsWrite(DebugMSG_Type.SMS,"RFID detected:" + rfid_string);
OC4Jrfid rfid = new OC4Jrfid();
rfid.suid = suid;
rfid.rfid = rfid_string;
//MotoTRBOGW.DBQueueRFID.PostItem(rfid);
}
else
{
//insert message into queue
SMS sms = new SMS();
sms.RadioID = suid;
sms.SUDBid = MotoRepeater_GW.unitList.GetDBid(suid);
sms.Msg = s;
SM_SMS.recvSMSQueue.PostItem(sms);
}
}
//Utils.ConsWrite(DebugMSG_Type.SMS,"i=" + i);
return hret;
}
}
}