/// <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; } }
/// <summary> /// Set the content format of the payload /// </summary> /// <param name="contentFormat">The content format identifier</param> public void SetContentFormat(CoAPContentFormatOption contentFormat) { if (this.Options == null) { this.Options = new CoAPHeaderOptions(); } this.Options.Add(new CoAPHeaderOption(CoAPHeaderOption.CONTENT_FORMAT, contentFormat.GetValueAsBytes())); }
/// <summary> /// Convert to a string representation /// </summary> /// <returns>string</returns> public override string ToString() { string optionValueAsString = (this.Value != null) ? AbstractByteUtils.ByteToStringUTF8(this.Value) : ""; switch (this.Number) { case CoAPHeaderOption.ACCEPT: return("Accept : " + AbstractByteUtils.ToUInt16(this.Value).ToString()); case CoAPHeaderOption.BLOCK1: CoAPBlockOption cbo1 = new CoAPBlockOption(this.Value); return("Block1 : " + cbo1.ToString()); case CoAPHeaderOption.BLOCK2: CoAPBlockOption cbo2 = new CoAPBlockOption(this.Value); return("Block2 : " + cbo2.ToString()); case CoAPHeaderOption.CONTENT_FORMAT: CoAPContentFormatOption ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(this.Value)); return("Content-Format : " + ccformat.ToString()); case CoAPHeaderOption.ETAG: return("ETag : " + optionValueAsString); case CoAPHeaderOption.IF_MATCH: return("If-Match : " + optionValueAsString); case CoAPHeaderOption.IF_NONE_MATCH: return("If-None-Match : "); case CoAPHeaderOption.OBSERVE: return("Observe : " + AbstractByteUtils.ToUInt64(this.Value).ToString()); //We have no data structure for 3-bytes case CoAPHeaderOption.LOCATION_PATH: return("Location-Path : " + optionValueAsString); case CoAPHeaderOption.LOCATION_QUERY: return("Location-Query : " + optionValueAsString); case CoAPHeaderOption.MAX_AGE: return("Max-Age : " + AbstractByteUtils.ToUInt64(this.Value).ToString()); case CoAPHeaderOption.PROXY_SCHEME: return("Proxy-Scheme : " + optionValueAsString); case CoAPHeaderOption.PROXY_URI: return("Proxy-Uri : " + optionValueAsString); case CoAPHeaderOption.SIZE1: return("Size1 : " + AbstractByteUtils.ToUInt64(this.Value).ToString()); case CoAPHeaderOption.SIZE2: return("Size2 : " + AbstractByteUtils.ToUInt64(this.Value).ToString()); case CoAPHeaderOption.URI_HOST: return("Uri-Host : " + optionValueAsString); case CoAPHeaderOption.URI_PATH: return("Uri-Path : " + optionValueAsString); case CoAPHeaderOption.URI_PORT: return("Uri-Port : " + AbstractByteUtils.ToUInt16(this.Value)); case CoAPHeaderOption.URI_QUERY: return("Uri-Query : " + optionValueAsString); default: return("Unknown : " + optionValueAsString); } }