/// <summary> /// Parse the input byte stream starting from the given index and populate the Options /// </summary> /// <param name="coapMsgStream">The CoAP message stream as bytes (in network byte order)</param> /// <param name="startIndex">The index to start looking for options</param> /// <param name="extraInfo">Not used</param> /// <returns>The next index that points towards payload marker</returns> public int Parse(byte[] coapMsgStream, int startIndex, UInt16 extraInfo) { if (coapMsgStream == null || coapMsgStream.Length <= AbstractCoAPMessage.HEADER_LENGTH || startIndex < AbstractCoAPMessage.HEADER_LENGTH || startIndex >= coapMsgStream.Length) { return(startIndex); //Nothing to parse...no options present...just point to the next location } byte optionHeader = coapMsgStream[startIndex]; UInt16 previousOptionNumber = 0; int nextIndex = startIndex; while ((optionHeader & 0xF0) != 0xF0 /*not a payload indicator*/ && optionHeader != 0) { CoAPHeaderOption headerOption = new CoAPHeaderOption(); nextIndex = headerOption.Parse(coapMsgStream, nextIndex, previousOptionNumber); this.Add(headerOption); previousOptionNumber = ((CoAPHeaderOption)this[this.Count - 1]).Number; if (nextIndex >= coapMsgStream.Length) { break; } optionHeader = coapMsgStream[nextIndex]; } return(nextIndex); }
private void InsertOrderedOption(CoAPHeaderOption o) { int i = 0; for (i = 0; i < this.Count; i++) { CoAPHeaderOption h = (CoAPHeaderOption)this[i]; if (o.Number < h.Number) { break; } } this.Insert(i, o); }
/// <summary> /// Add a new option to the list of options /// </summary> /// <param name="optionNumber">The option number</param> /// <param name="optionValue">The associated option value as UTF-8string. This will be converted to bytes internally</param> public virtual void AddOption(UInt16 optionNumber, string optionValue) { CoAPHeaderOption headerOption = new CoAPHeaderOption(optionNumber, AbstractByteUtils.StringToByteUTF8(optionValue)); if (!headerOption.IsRepeatable() && this.HasOption(optionNumber)) { //Specs say that if an option is not repeatable and it still appears multiple times, //each subsequent option must be treated as un-recognized.... //In this implementation, we do not allow non-repetable options to be added to the list throw new CoAPFormatException("Non-repeatable option already present in collection. Cannot add more."); } else { this.Add(headerOption); } }