SafeNet/.svn/pristine/4f/4fd730f30466dbbbd5699a9c725...

151 lines
4.9 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using SafeNetLib;
namespace MotoTRBO_SOC
{
public class SendSMSThread
{
int remoteSMSport;
public SendSMSThread(int p_remoteSMSport)
{
remoteSMSport = p_remoteSMSport;
}
public void HandleConnection()
{
while (MotoTRBO_GW.running)
{
try
{
//MotoTRBOcmdMsg msg = MotoTRBOGW.locationQueue.GetItem(100);
SMSmsg msg = SN_Queues.sendSMSQueue.GetItem(-1);//block until message is in queue
SendSMSmsg(msg);
if (msg.req_conf)
{
//Utils.ConsWrite(DebugMSG_Type.SMS, "Requesting SMS 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
{
Byte[] sendBytes = PackSMS(p_msg.msg, p_msg.req_conf);
Utils.ConsWrite(DebugMSG_Type.SMS, "««« SMS [" + p_msg.msg + "] to " + p_msg.suid);
MotoTRBO_GW.smsUDPclient.Send(sendBytes, sendBytes.Length, (new RadioID2IP("12", p_msg.suid)).GetIP(), remoteSMSport);
if(OnSMSSent != null)
OnSMSSent(p_msg.msg, p_msg.suid);
// update into database
return true;
}
catch (Exception exc)
{
Utils.ConsWrite(DebugMSG_Type.always, exc.ToString());
}
return false;
}
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++;
//Utils.ConsWrite(DebugMSG_Type.SMS, "SMS packed: " + LocationThread.Byte2String(data, 0, data[1] + 2) + "\n\rSMS_SeQ_ID = " + sms_seq_id);
return data;
}
public delegate void SMSSentDEl(String message, String radioID);
public event SMSSentDEl OnSMSSent;
}
}