Пример #1
0
        /// <summary>
        /// decode message from parsed pdu data
        /// </summary>
        /// <param name="pdu"></param>
        /// <returns></returns>
        public static Message Decode(PDU pdu)
        {
            var      message = new Message();
            IDecoder msgDecoder;
            var      esBits = message.getBits(pdu.encodingScheme);

            //if (!esBits[6] && !esBits[7]) {
            if (esBits[5])
            {
                msgDecoder = new UnicodeDecoder();
            }
            else
            {
                var bits = Convert.ToByte(esBits[3]) << 1 | Convert.ToByte(esBits[2]);
                switch (bits)
                {
                case 0:
                    msgDecoder = new SevenBitDecoder();
                    break;

                case 1:
                    msgDecoder = new EightBitDecoder();
                    break;

                case 2:
                    msgDecoder = new UnicodeDecoder();
                    break;

                case 3:
                default:
                    throw new Exception("Unknown PDU Message Encoding Scheme!");
                }
            }
            //} else {
            //    throw new Exception ("Unknown PDU Message Encoding Scheme!");
            //}
            message.content = msgDecoder.Decode(pdu.data).Trim('\0');

            message.smscNumber   = message.byteReverse(pdu.smscNumber);
            message.senderNumber = message.byteReverse(pdu.senderNumber);

            var timeStampAsString = message.byteReverse(pdu.timeStamp);
            var dateTimeAsString  = "20" + timeStampAsString.Substring(0, 12);

            message.dateTime = DateTime.ParseExact(dateTimeAsString, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            // var timeZone = int.Parse (timeStampAsString.Substring (12, 2));
            // if ((timeZone >> 7) == 0) {
            //     message.dateTimeUtc = message.dateTime - new TimeSpan (timeZone / 4, 0, 0);
            // }

            message.isSplit = pdu.dataHeader.length != 0 && pdu.dataHeader.splitLength != 0;
            if (message.isSplit)
            {
                message.splitCount = pdu.dataHeader.splitCount;
                message.splitIndex = pdu.dataHeader.splitIndex;
                message.splitId    = BitConverter.ToString(pdu.dataHeader.splitId);
            }
            return(message);
        }
Пример #2
0
        public static PDU Parse(byte[] rawPdu)
        {
            PDU parsed;

            using (var ms = new MemoryStream(rawPdu)) {
                parsed = PDU.Parse(ms);
            }
            return(parsed);
        }
Пример #3
0
        public static PDU Parse(Stream rawPdu)
        {
            PDU pdu = new PDU();

            rawPdu.Position    = 0;
            pdu.smscByteLength = (byte)rawPdu.ReadByte();
            pdu.smscType       = (byte)rawPdu.ReadByte();
            pdu.smscNumber     = new byte[pdu.smscByteLength - 1];
            rawPdu.Read(pdu.smscNumber, 0, pdu.smscByteLength - 1);

            pdu.pduHeader    = (byte)rawPdu.ReadByte();
            pdu.senderLength = (byte)rawPdu.ReadByte();
            pdu.senderType   = (byte)rawPdu.ReadByte();
            pdu.senderNumber = new byte[pdu.senderByteLength];
            rawPdu.Read(pdu.senderNumber, 0, pdu.senderByteLength);

            pdu.protocalId     = (byte)rawPdu.ReadByte();
            pdu.encodingScheme = (byte)rawPdu.ReadByte();
            pdu.timeStamp      = new byte[7];
            rawPdu.Read(pdu.timeStamp, 0, 7);

            pdu.dataLength = (byte)rawPdu.ReadByte();
            var dataHeader = pdu.dataHeader = new DataHeader();
            int dataLengthExcludeHeader = pdu.dataLength;

            if (pdu.includedDataHeader)
            {
                dataHeader.length      = (byte)rawPdu.ReadByte();
                dataHeader.type        = (byte)rawPdu.ReadByte();
                dataHeader.splitLength = (byte)rawPdu.ReadByte();
                dataHeader.splitId     = new byte[dataHeader.splitLength - 2];
                rawPdu.Read(dataHeader.splitId, 0, dataHeader.splitLength - 2);
                dataHeader.splitCount    = (byte)rawPdu.ReadByte();
                dataHeader.splitIndex    = (byte)rawPdu.ReadByte();
                dataLengthExcludeHeader -= dataHeader.length + 1;
            }
            pdu.data = new byte[dataLengthExcludeHeader];
            rawPdu.Read(pdu.data, 0, dataLengthExcludeHeader);

            return(pdu);
        }