Esempio n. 1
0
        public virtual void Decode(byte[] message)
        {
            int  index  = 0;
            byte header = message[index++];

            if (header >> 0x06 != 1)
            {
                throw new CoapVersionMismatchException("Coap Version 1 is only supported version for Coap response.");
            }

            this.MessageType = (CoapMessageType)Convert.ToInt32((header >> 0x04) & 0x03);
            this.TokenLength = (byte)(header & 0x0F);

            byte code = message[index++];

            this.Code = (CodeType)(((code >> 0x05) * 100) + (code & 0x1F));

            this.MessageId = (ushort)(message[index++] << 0x08 | message[index++]);
            byte[] tokenBytes = new byte[this.TokenLength];
            Buffer.BlockCopy(message, index, tokenBytes, 0, this.TokenLength);
            this.Token = tokenBytes;

            //get the options
            index += this.TokenLength;
            int previous = 0;
            int delta    = 0;

            bool marker = ((message[index] & 0xFF) == 0xFF);

            while (!marker)
            {
                delta = (message[index] >> 0x04);
                CoapOption CoapOption = CoapOption.Decode(message, index, previous, out index);
                this.Options.Add(CoapOption);
                previous += delta;
                if (index < message.Length)
                {
                    marker = ((message[index] & 0xFF) == 0xFF);
                }
                else
                {
                    break;
                }
            }

            if (marker) //grab the payload
            {
                index++;
                this.Payload = new byte[message.Length - index];
                Buffer.BlockCopy(message, index, this.Payload, 0, this.Payload.Length);
            }

            this.HasContentFormat = this.Options.ContainsContentFormat();

            ReadOptions(this);
        }
Esempio n. 2
0
        public void Append(CoapOption option)
        {
            int typeInt = (int)option.Type;

            if (optionDict.ContainsKey(typeInt))
            {
                optionDict[typeInt] = optionDict[typeInt]++;
            }
            else
            {
                optionDict.Add(typeInt, 0);
            }

            list.Add((int)option.Type * 1000 + optionDict[typeInt], option);
        }
Esempio n. 3
0
        public override void Decode(byte[] message)
        {
            //CoapRequest message = new CoapRequest();

            int  index  = 0;
            byte header = message[index++];

            if (header >> 0x06 != 1)
            {
                throw new CoapVersionMismatchException("Coap Version 1 is only supported version for Coap request.");
            }

            this.RequestType = (RequestMessageType)Convert.ToInt32((header >> 0x04) & 0x03);

            this.TokenLength = Convert.ToByte(header & 0x0F);

            this.Method = (MethodType)message[index++];

            this.MessageId = (ushort)(message[index++] << 0x08 | message[index++]);
            byte[] tokenBytes = new byte[this.TokenLength];
            Buffer.BlockCopy(message, index, tokenBytes, 0, this.TokenLength);
            this.Token = tokenBytes;

            //get the options
            index += this.TokenLength;
            int previous = 0;
            int delta    = 0;

            bool marker = ((message[index] & 0xFF) == 0xFF);

            while (!marker)
            {
                delta = (message[index] >> 0x04);
                CoapOption CoapOption = CoapOption.Decode(message, index, previous, out index);
                this.Options.Add(CoapOption);
                previous += delta;
                marker    = ((message[index] & 0xFF) == 0xFF);
            }

            if (marker) //grab the payload
            {
                index++;
                this.Payload = new byte[message.Length - index];
                Buffer.BlockCopy(message, index, this.Payload, 0, this.Payload.Length);
            }

            ReadOptions(this);
        }
Esempio n. 4
0
        public override void Decode(byte[] message)
        {
            int  index  = 0;
            byte header = message[index++];

            if (header >> 0x06 != 1)
            {
                throw new CoapVersionMismatchException("Coap Version 1 is only supported version for Coap response.");
            }

            ResponseType = (ResponseMessageType)Convert.ToInt32((header >> 0x04) & 0x03);

            TokenLength = Convert.ToByte(header & 0x0F);

            byte code = message[index++];

            ResponseCode = (ResponseCodeType)((code >> 0x05) * 100 + (code & 0x1F));

            MessageId = (ushort)((message[index++] << 0x08) | message[index++]);
            byte[] tokenBytes = new byte[TokenLength];
            Buffer.BlockCopy(message, index, tokenBytes, 0, TokenLength);
            Token = tokenBytes;

            index += TokenLength;
            int  previous = 0;
            bool marker   = (message[index] & 0xFF) == 0xFF;

            while (!marker)
            {
                int        delta      = message[index] >> 0x04;
                CoapOption CoapOption = CoapOption.Decode(message, index, previous, out index);
                Options.Add(CoapOption);
                previous += delta;
                marker    = (message[index] & 0xFF) == 0xFF;
            }

            if (marker)
            {
                index++;
                Payload = new byte[message.Length - index];
                Buffer.BlockCopy(message, index, Payload, 0, Payload.Length);
            }

            Error = !Options.ContainsContentFormat();

            ReadOptions(this);
        }