293 lines
10 KiB
C#
293 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
using System.Threading;
|
|
using SafeNetLib;
|
|
|
|
namespace ConnectPlus_SOC
|
|
{
|
|
|
|
class SendSMSThread
|
|
{
|
|
private byte WAIT_SMS_SEC = 5; // max no of seconds to wait for SMS ack
|
|
int remoteSMSport;
|
|
public SendSMSThread(int p_remoteSMSport)
|
|
{
|
|
remoteSMSport = p_remoteSMSport;
|
|
}
|
|
|
|
public void HandleConnection()
|
|
{
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.SMS, "Waiting for new SMS!!!");
|
|
//MotoTRBOcmdMsg msg = MotoTRBOGW.locationQueue.GetItem(100);
|
|
SMSmsg msg = SN_Queues.sendSMSQueue.GetItem(100);//block until message is in queue
|
|
if (msg != null)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "SendSMSthread.cs -> HandleConnection");
|
|
SendSMSmsg(msg);
|
|
if (msg.req_conf)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Requesting Receive Confirmation");
|
|
lock (SN_Queues.waitConfSMSList.SyncRoot)
|
|
{
|
|
msg.seq_no = this.sms_seq_id - 1;//-1 because was alraedy incremented for next id
|
|
msg.waitConfSMSList_time = DateTime.Now;
|
|
|
|
SN_Queues.waitConfSMSList.Add(msg);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Thread.Sleep(1200);//minimum interval between SMS as per MotoTRBO specs
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Exception in OC4JconnThread:HandleConnection(): \r\n\tData=" +
|
|
ex.Data + "\r\n\tSource=" +
|
|
ex.Source + "\r\n\tStackTrace=" +
|
|
ex.StackTrace + "\r\n\tMessage=" +
|
|
ex.Message + "\r\n\tInnerException=" +
|
|
ex.InnerException);
|
|
}
|
|
} // end while (true)
|
|
}
|
|
|
|
public bool SendSMSmsg(SMSmsg p_msg)
|
|
{
|
|
try
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "SendSMSthread.cs->SendSMSmsg msg:" + p_msg.msg);
|
|
SendSMS_with_addrs("10000.4@tserv<mailto:" + p_msg.suid + ".1>", p_msg.msg, p_msg.conf);
|
|
|
|
//LOGS.LOG("Sending SMS suid:"+p_msg.suid+" "+ p_msg.msg);
|
|
|
|
return true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, exc.ToString());
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void SendSMS_with_addrs(string address, string msg, bool conf)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Sending SMS to:" + address + " msg:" + msg);
|
|
try
|
|
{
|
|
IPAddress ctrIP = null;
|
|
if (!IPAddress.TryParse(ConnectPlus_GW.cfg.ctrlIP, out ctrIP))
|
|
{
|
|
ctrIP = Dns.GetHostAddresses(ConnectPlus_GW.cfg.ctrlIP)[0];
|
|
}
|
|
if (ctrIP == null)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Location Thread invalid host address for: " + ConnectPlus_GW.cfg.ctrlIP);
|
|
}
|
|
|
|
if (conf)
|
|
{
|
|
Byte[] sendBytes = PackSMS_with_addrs(address, msg, true);
|
|
//Console.WriteLine("Send SMS to SU " + SUID + " with confirmation");
|
|
|
|
ConnectPlus_GW.smsUDPclient.Send(sendBytes, sendBytes.Length, ctrIP.ToString(), ConnectPlus_GW.cfg.smsPort);
|
|
}
|
|
else
|
|
{
|
|
Byte[] sendBytes = PackSMS_with_addrs(address, msg, false);
|
|
//Console.WriteLine("Send SMS to SU " + SUID + " without confirmation");
|
|
ConnectPlus_GW.smsUDPclient.Send(sendBytes, sendBytes.Length, ctrIP.ToString(), ConnectPlus_GW.cfg.smsPort);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always,"Exc in:SendSMS_with_addrs: \n"+ exc.ToString());
|
|
Utils.ConsWrite(DebugMSG_Type.always, "ConnectPlus_GW.cfg.ctrlIP" + ConnectPlus_GW.cfg.ctrlIP);
|
|
Utils.ConsWrite(DebugMSG_Type.always, "ConnectPlus_GW.cfg.smsPort" + ConnectPlus_GW.cfg.smsPort);
|
|
}
|
|
}
|
|
|
|
public static void Send_Reg_msg(string address)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Sending TMS Service Availability to:" + address);
|
|
try
|
|
{
|
|
Byte[] sendBytes = Pack_TMS(address);
|
|
//Console.WriteLine("Send SMS to SU " + SUID + " with confirmation");
|
|
ConnectPlus_GW.smsUDPclient.Send(sendBytes, sendBytes.Length, ConnectPlus_GW.cfg.ctrlIP, ConnectPlus_GW.cfg.smsPort);
|
|
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, exc.ToString());
|
|
}
|
|
}
|
|
|
|
byte[] PackSMS_with_addrs(string addrs, string msg_body, bool confirm)
|
|
{
|
|
int msg_len = msg_body.Length;
|
|
int addrs_len = addrs.Length;
|
|
|
|
if (msg_len > MAX_SMS_SIZE)
|
|
{
|
|
msg_len = MAX_SMS_SIZE;
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Warning: SMS truncated to " + msg_len + " characters");
|
|
}
|
|
|
|
if (msg_len > msg_body.Length)
|
|
{
|
|
msg_body = msg_body.Remove(MAX_SMS_SIZE);
|
|
}
|
|
// compute the len
|
|
int len = msg_len + addrs_len;
|
|
|
|
len *= 2; // each character is encoded on 2B
|
|
len += 4; // this is for the headers
|
|
Byte[] data = new Byte[len + 2]; //+2 for the msg size
|
|
|
|
int msb = (len & 0xff00) >> 8;
|
|
data[0] = (byte)(msb & 0x00ff);
|
|
data[1] = (byte)(len & 0x00ff);
|
|
|
|
// put the headers
|
|
if (confirm)
|
|
{
|
|
data[2] = 0xe0; // first header
|
|
}
|
|
else
|
|
{
|
|
data[2] = 0xa0; // first header
|
|
}
|
|
|
|
data[3] = (byte)(addrs_len * 2); // addr size
|
|
for (int i = 0; i < addrs_len; i++)
|
|
{
|
|
data[4 + i * 2] = (byte)addrs[i];
|
|
data[4 + i * 2 + 1] = 0;
|
|
}
|
|
|
|
byte lsb_id = (byte)(sms_seq_id & 0x1f);
|
|
data[4 + addrs_len * 2] = (byte)(0x80 | lsb_id);
|
|
byte msb_id = (byte)(sms_seq_id & 0x60); // keep bits 6:5
|
|
data[5 + addrs_len * 2] = (byte)(msb_id | 0x04);
|
|
|
|
for (int i = 0; i < msg_len; i++)
|
|
{
|
|
data[6 + i * 2 + addrs_len * 2] = (byte)msg_body[i];
|
|
data[6 + i * 2 + 1 + addrs_len * 2] = 0;
|
|
}
|
|
sms_seq_id++;
|
|
//Console.WriteLine("SMS packed: " + Utils.Byte2String(data, 0, data[1] + 2) + "\n\rSMS_SeQ_ID = " + sms_seq_id);
|
|
return data;
|
|
}
|
|
|
|
static byte[] Pack_TMS(string addrs)
|
|
{
|
|
int msg_len = 0;
|
|
int addrs_len = addrs.Length;
|
|
// compute the len
|
|
int len = msg_len + addrs_len;
|
|
|
|
len *= 2; // each character is encoded on 2B
|
|
len += 3; // this is for the headers
|
|
Byte[] data = new Byte[len + 2]; //+2 for the msg size
|
|
|
|
int msb = (len & 0xff00) >> 8;
|
|
data[0] = (byte)(msb & 0x00ff);
|
|
data[1] = (byte)(len & 0x00ff);
|
|
|
|
// put the headers
|
|
|
|
data[2] = 0xD0; // first header
|
|
|
|
data[3] = (byte)(addrs_len * 2); // addr size
|
|
for (int i = 0; i < addrs_len; i++)
|
|
{
|
|
data[4 + i * 2] = (byte)addrs[i];
|
|
data[4 + i * 2 + 1] = 0;
|
|
}
|
|
//second header
|
|
data[4 + addrs_len * 2] = 0x00;
|
|
|
|
//Console.WriteLine("SMS packed: " + Utils.Byte2String(data, 0, data[1] + 2) + "\n\rSMS_SeQ_ID = " + sms_seq_id);
|
|
return data;
|
|
}
|
|
|
|
int MAX_SMS_SIZE = 120;
|
|
int MAX_ID_VAL = 100;
|
|
int sms_seq_id = 1;
|
|
Byte[] PackSMS(string msg_body, bool confirm)
|
|
{
|
|
/*
|
|
Byte[] sendBytes = {
|
|
0x00 , 0x0E , // len
|
|
0xE0 , // header 1
|
|
0x00 , // addr size
|
|
0x88 , 0x04 , // header 2,3
|
|
0x41 , 0x00 , 0x64 , 0x00 , 0x67, 0x00 , 0x6A , 0x00, 0x6D , 0x00 //data
|
|
};
|
|
*/
|
|
|
|
int msg_len = msg_body.Length;
|
|
if (msg_len > MAX_SMS_SIZE)
|
|
{
|
|
msg_len = MAX_SMS_SIZE;
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Warning: SMS truncked to " + msg_len);
|
|
}
|
|
if (sms_seq_id > MAX_ID_VAL)
|
|
sms_seq_id = 1;
|
|
|
|
if (msg_len > msg_body.Length)
|
|
{
|
|
msg_body = msg_body.Remove(MAX_SMS_SIZE);
|
|
}
|
|
// compute the len
|
|
int len = msg_len;
|
|
|
|
len *= 2; // each character is encoded on 2B
|
|
len += 4; // this is for the headers
|
|
Byte[] data = new Byte[len + 2]; //+2 for the msg size
|
|
|
|
int msb = (len & 0xff00) >> 8;
|
|
data[0] = (byte)(msb & 0x00ff);
|
|
data[1] = (byte)(len & 0x00ff);
|
|
|
|
// put the headers
|
|
if (confirm)
|
|
{
|
|
data[2] = 0xe0; // first header
|
|
}
|
|
else
|
|
{
|
|
data[2] = 0xa0; // first header
|
|
}
|
|
|
|
data[3] = 0x00; // addr size
|
|
|
|
byte lsb_id = (byte)(sms_seq_id & 0x1f);
|
|
data[4] = (byte)(0x80 | lsb_id);
|
|
byte msb_id = (byte)(sms_seq_id & 0x60); // keep bits 6:5
|
|
data[5] = (byte)(msb_id | 0x04);
|
|
|
|
for (int i = 0; i < msg_len; i++)
|
|
{
|
|
data[6 + i * 2] = (byte)msg_body[i];
|
|
data[6 + i * 2 + 1] = 0;
|
|
}
|
|
|
|
sms_seq_id++;
|
|
//Console.WriteLine("SMS packed: " + LocationThread.Byte2String(data, 0, data[1] + 2) + "\n\rSMS_SeQ_ID = " + sms_seq_id);
|
|
return data;
|
|
}
|
|
}
|
|
}
|