예제 #1
0
        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="optionNumber">The option number</param>
        /// <param name="optionValue">The option value</param>
        public CoAPHeaderOption(UInt16 optionNumber, byte[] optionValue)
        {
            int maxLengthOfOptionValue = this.GetOptionValueMaxLengthInBytes(optionNumber);
            int minLengthOfOptionValue = this.GetOptionValueMinLengthInBytes(optionNumber);
            int lengthOfOptionValue    = (optionValue == null) ? 0 : optionValue.Length;

            this.Number = optionNumber;
            if (this.IsRecognized())
            {
                if (lengthOfOptionValue < minLengthOfOptionValue || lengthOfOptionValue > maxLengthOfOptionValue)
                {
                    throw new CoAPFormatException("Invalid length of option value for the given option number " + optionNumber);
                }
            }

            if (this.Number == CoAPHeaderOption.CONTENT_FORMAT)
            {
                CoAPContentFormatOption contentFormat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(optionValue));
                if (!contentFormat.IsValid())
                {
                    throw new CoAPFormatException("Content format not supported");
                }
            }

            if (optionValue != null && optionValue.Length > 0)
            {
                this.Value = new byte[optionValue.Length];
                Array.Copy(optionValue, this.Value, optionValue.Length);
                this.ValueSizeInBytes = (UInt16)optionValue.Length;
            }
        }