/// <summary> /// Get the token value /// </summary> /// <returns>long</returns> public UInt64 GetTokenValue() { if (this.Token == null || this.Token.Length == 0) { return(0); } else { return(AbstractByteUtils.ToUInt64(this.Token.Value)); } }
/// <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); } } }
/// <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); } }