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; /* < 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=""> */ namespace ConnectPlus_SOC { class ReceiveSMSThread { public UInt16 port; //public MotoTRBOGW parent; string gwID; string connStr; DBhandle DB; public ReceiveSMSThread(ushort p_port, string p_connStr, string p_gwID) { port = p_port; gwID = p_gwID; connStr = p_connStr; DB = new DBhandle(p_connStr, p_gwID); } // ------------------------------------------------------------------- // 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); // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = ConnectPlus_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()); // update lastActivityTime ConnectPlus_GW.lastActivityTime = DateTime.Now; 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 (SN_Queues.waitConfSMSList.SyncRoot) { int index = -1; int count = 0; foreach (SMSmsg msg in SN_Queues.waitConfSMSList) { if (msg.seq_no == hret.seq_no) { index = count; SN_Queues.confSMSQueue.PostItem(msg); } count++; } if (index > -1)//we've found the message SN_Queues.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; Utils.ConsWrite(DebugMSG_Type.SMS, "--------------------"); pdata = (int)data[0]; pdata <<= 8; pdata |= (int)data[1]; // parse header int header = data[2]; hret.header = data[2]; //Console.WriteLine("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; //remove second byte of encoding( ex: "1" is ecncoded 0x31 0x00) string adrs = ""; for (int ind = i; ind < addrsize + 2; ind += 2) { adrs += Encoding.UTF8.GetString(new byte[] { data[ind] }); } hret.address = adrs; Utils.ConsWrite(DebugMSG_Type.SMS, "Address: " + adrs); for (j = 0; j < addrsize; j++) { //Console.Write(data[i + j].ToString("X")); } i += j; string suid = hret.address.Split('.')[0]; // 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 byte seqNr_MSB = (byte)(data[i] & 0x60); hret.seq_no += seqNr_MSB; hret.encoding = (byte)(data[i] & 0x0f); i++; } } if (hret.ack) { Thread.Sleep(500); // The client needs an ACK //string addrs = "10000.4@tserv"; string addrs = "10000.4"; int addrs_len = addrs.Length; // The client needs an ACK byte extension_header = 0; if (hret.seq_no > 0x1F) extension_header = 1; Byte[] sendBytes = new Byte[5 + addrs_len * 2 + extension_header]; sendBytes[0] = 0x00; // len (2B) sendBytes[1] = (byte)(3 + addrs_len * 2 + extension_header); sendBytes[2] = 0xBF; // first header (req) sendBytes[3] = (byte)(addrs_len * 2); // addr size for (int indx = 0; indx < addrs_len; indx++) { sendBytes[4 + indx * 2] = (byte)addrs[indx]; sendBytes[4 + indx * 2 + 1] = 0; } sendBytes[4 + addrs_len * 2] = (byte)(hret.seq_no & 0x1F); // 2nd header (opt) if (extension_header == 1) { sendBytes[4 + addrs_len * 2] += 0x80; //added the extension header info sendBytes[4 + addrs_len * 2 + 1] = (byte)(hret.seq_no & 0x60); } Utils.ConsWrite(DebugMSG_Type.SMS, "SMSReceiveThread ACK sent address: " + addrs + "seq_id: " + hret.seq_no); //MotoTRBOGW.smsUDPclient.Send(sendBytes, sendBytes[1] + 2, new IPEndPoint(IPAddress.Parse(ConnectPlus_GW.cfg.ctrlIP), Convert.ToInt32(ConnectPlus_GW.cfg.smsPort))); UdpClient uc = new UdpClient(); uc.Send(sendBytes, sendBytes[1] + 2, new IPEndPoint(IPAddress.Parse(ConnectPlus_GW.cfg.ctrlIP), Convert.ToInt32(ConnectPlus_GW.cfg.smsPort))); ConnectPlus_GW.smsUDPclient.Send(sendBytes, sendBytes[1] + 2, RemoteIpEndPoint); 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]; } } Utils.ConsWrite(DebugMSG_Type.SMS,""); // 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 database if (!DB.insertSMSinDB("" + suid, s)) { SMSmsg sms = new SMSmsg(); sms.suid = suid; sms.msg = "ERROR sending message to server!"; sms.req_conf = false; SN_Queues.sendSMSQueue.PostItem(sms); } else { //LOGS.LOG("Message ID:" + suid + " " + s); try { string email_addr = ""; string email_body = ""; if (s.Contains("@") && s.Contains(":") && s.Contains(".")) { // this may be an email so analyze further char[] sep = { ':' }; string[] all_msg = s.Split(sep); email_addr = all_msg[0]; email_body = all_msg[1]; if (email_addr.Contains("@") && email_addr.Contains(".")) { // this is email EmailHandler.sendMail(email_addr, email_body, suid); } } } catch (Exception ex) { Utils.ConsWrite(DebugMSG_Type.always, ex.ToString()); } } } } //Console.WriteLine("i=" + i); Utils.ConsWrite(DebugMSG_Type.SMS, "--------------------"); return hret; } } }