using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using SafeMobileLib; using System.Net; using System.Threading; using System.Diagnostics; namespace MotoRepeater { class SMSSendThread { private byte WAIT_SMS_SEC = 5; // max no of seconds to wait for SMS ack private int MAX_SMS_SIZE = 140; // max SMS allowed size private ushort smsPort = 0; private String mIP; private Int32 mPort; int sms_seq_id; public UdpClient udpClientSMS = null; private bool wait4ack; public SMSSendThread(ushort port, bool wait4ack, UdpClient _udpClientSMS) { smsPort = port; SMSConfirm.fillList(); SMSConfirm.initializeTimer(); this.wait4ack = wait4ack; udpClientSMS = _udpClientSMS; } public void handleConnection() { Utils.WriteLine("♥♥♥ SMS Send Thread Started on port " + smsPort); while (MotoRepeater_GW.isRunning) { Thread.Sleep(100); } } Byte[] PackSMS(string msg_body, bool confirm, int p_sms_seq_id) { int msg_len = msg_body.Length; if (msg_len > MAX_SMS_SIZE) { msg_len = MAX_SMS_SIZE; //Utils.WriteLine("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; 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)(p_sms_seq_id & 0x1f); data[4] = (byte)(0x80 | lsb_id); byte msb_id = (byte)(p_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; } //Console.WriteLine("SMS packed: " + Utils.Byte2String(data, 0, data[1] + 2) + "\n\rSMS_SeQ_ID = " + sms_seq_id); return data; } public void SendSMS(string SUID, string msg, bool is_ack_required, Int64 p_sms_seq_id) { SendSMS(SUID, msg, is_ack_required == true ? 1 : -1, (int)p_sms_seq_id); } public void SendSMS(string SUID, string msg, int msg_idx, int p_sms_seq_id) { try { if (msg.Contains("") == true && msg.Contains("\r\n") == true) { string[] TicketString = msg.Split('^'); msg = TicketString[0]; //comment or message body if (TicketString.Length > 1) { if (TicketString[1] != "") msg += string.Format(" ({0})", TicketString[1]); } if (TicketString.Length > 3) { if (TicketString[4] != "") { msg += " until "; msg += (Convert.ToInt32(TicketString[4])).GetDTLocalFromSeconds().ToString("dd/MM/yyyy HH:mm:ss, ddd"); } } } int wait_confirm_sec = 0; if (msg_idx > 0) wait_confirm_sec = WAIT_SMS_SEC; if (msg_idx == -1) wait_confirm_sec = 0; if (wait_confirm_sec != 0) { Byte[] sendBytes = PackSMS(msg, true, p_sms_seq_id); Utils.WriteLine("««« SMS with confirmation [" + msg +"] to " + SUID); udpClientSMS.Send(sendBytes, sendBytes.Length, Utils.ID2IP("12", SUID), smsPort);//12 } else { Byte[] sendBytes = PackSMS(msg, false, p_sms_seq_id); Utils.WriteLine("««« SMS without confirmation [" + msg + "] to " + SUID); udpClientSMS.Send(sendBytes, sendBytes.Length, Utils.ID2IP("12", SUID), smsPort);//12 } } catch (Exception exc) { Utils.WriteLine("SendSMS : " + exc.ToString(), ConsoleColor.Red); } } public void SendSMSGroup(string SUID, string msg, bool is_ack_required, Int64 p_sms_seq_id) { SendSMSGroup(SUID, msg, is_ack_required == true ? 1 : -1, (int)p_sms_seq_id); } public void SendSMSGroup(string SUID, string msg, int msg_idx, int p_sms_seq_id) { try { Thread.Sleep(1000); Byte[] sendBytes = PackSMS(msg, true, p_sms_seq_id); Utils.WriteLine("««« Group SMS [" + msg + "] to " + SUID); udpClientSMS.Send(sendBytes, sendBytes.Length, Utils.ID2IP("12", SUID), smsPort); } catch (Exception exc) { SM.Debug(exc.ToString()); } } private static void ExecuteCommand(String command) { Process p = new Process(); p = new Process(); p.StartInfo.FileName = "cmd.exe"; String arguments = "/c " + command; p.StartInfo.Arguments = arguments; p.StartInfo.UseShellExecute = false; p.Start(); } } }