示例#1
0
        /// <summary>
        /// As bytes...endian ness is taken care of in this method
        /// </summary>
        /// <param name="reserved">Not used now</param>
        /// <returns>byte[]</returns>
        public byte[] ToStream(UInt16 reserved)
        {
            if (this.SequenceNumber <= 0xF)
            {
                //single byte
                byte[] retVal = new byte[1];
                retVal[0] = (byte)this.SequenceNumber;
                retVal[0] = (byte)(retVal[0] << 4);                                      //seq number in top 4 bits
                retVal[0] = (this.HasMoreBlocks) ? (byte)(retVal[0] | 0x08) : retVal[0]; //more flag in 5th bit
                retVal[0] = (byte)(retVal[0] | this.SizeExponent);                       //size exponent, last 3 bits

                return(retVal);
            }
            else if (this.SequenceNumber > 0xF && this.SequenceNumber <= 0xFFF)
            {
                //double byte
                byte[] temp = new byte[2];

                if (AbstractByteUtils.IsTargetLittleEndian())
                {
                    temp    = AbstractByteUtils.GetBytes((UInt16)(this._seqNumber << 4));
                    temp[0] = (this.HasMoreBlocks) ? (byte)(temp[0] | 0x08) : (byte)(temp[0] & 0xF7);
                    temp[0] = (byte)(temp[0] | this.SizeExponent);
                }
                else
                {
                    temp    = AbstractByteUtils.GetBytes((UInt16)(this._seqNumber >> 4));
                    temp[1] = (this.HasMoreBlocks) ? (byte)(temp[1] | 0x08) : (byte)(temp[1] & 0xF7);
                    temp[1] = (byte)(temp[1] | this.SizeExponent);
                }
                return(temp);
            }
            else
            {
                //3 bytes...we start with 4(long) and then return last 3 only
                byte[] temp   = new byte[4];
                byte[] retVal = new byte[3];

                if (AbstractByteUtils.IsTargetLittleEndian())
                {
                    temp    = AbstractByteUtils.GetBytes((UInt64)(this._seqNumber << 4));
                    temp[0] = (this.HasMoreBlocks) ? (byte)(temp[0] | 0x08) : (byte)(temp[0] & 0xF7);
                    temp[0] = (byte)(temp[0] | this.SizeExponent);
                    Array.Copy(temp, 0, retVal, 0, 3);
                }
                else
                {
                    //use the index  =2, index 3 will be discarded
                    temp    = AbstractByteUtils.GetBytes((UInt64)(this._seqNumber >> 4));
                    temp[2] = (this.HasMoreBlocks) ? (byte)(temp[1] | 0x08) : (byte)(temp[1] & 0xF7);
                    temp[2] = (byte)(temp[1] | this.SizeExponent);
                    Array.Copy(temp, 1, retVal, 0, 3);
                }

                return(retVal);
            }
        }
示例#2
0
        /// <summary>
        /// The byte array from which we need to construct this block option.
        /// </summary>
        /// <param name="data">
        /// The byte array (max 3 bytes).Endian-ness is taken care of in this method
        /// </param>
        public CoAPBlockOption(byte[] data)
        {
            if (data == null || data.Length == 0 || data.Length > 3)
            {
                throw new ArgumentNullException("Block option byte array must be 1-3 bytes in length");
            }

            if (data.Length == 1 /*single byte*/)
            {
                this._seqNumber = (UInt16)(data[0] >> 4);
                this._hasMore   = (((data[0] & 0x08) >> 3) == 1) ? true : false;
                this._sizeExp   = (byte)(data[0] & 0x07);
            }
            else if (data.Length == 2 /*double byte*/)
            {
                if (AbstractByteUtils.IsTargetLittleEndian())
                {
                    this._seqNumber = (UInt16)(AbstractByteUtils.ToUInt16(data) >> 4);
                    this._hasMore   = (((data[0] & 0x08) >> 3) == 1) ? true : false;
                    this._sizeExp   = (byte)(data[0] & 0x07);
                }
                else
                {
                    this._seqNumber = (UInt16)(AbstractByteUtils.ToUInt16(data) << 4);
                    this._hasMore   = (((data[1] & 0x08) >> 3) == 1) ? true : false;
                    this._sizeExp   = (byte)(data[1] & 0x07);
                }
            }
            else if (data.Length == 3 /*Triple byte*/)
            {
                if (AbstractByteUtils.IsTargetLittleEndian())
                {
                    this._seqNumber = (UInt32)(AbstractByteUtils.ToUInt64(data) >> 4);
                    this._hasMore   = (((data[0] & 0x08) >> 3) == 1) ? true : false;
                    this._sizeExp   = (byte)(data[0] & 0x07);
                }
                else
                {
                    this._seqNumber = (UInt32)(AbstractByteUtils.ToUInt64(data) << 4);
                    this._hasMore   = (((data[2] & 0x08) >> 3) == 1) ? true : false;
                    this._sizeExp   = (byte)(data[2] & 0x07);
                }
            }
        }