void ApplyUriPort(CoapRequest request, CoapMessage message) { if (!request.Options.UriPort.HasValue) { return; } message.Options.Add(_optionFactory.CreateUriPort((uint)request.Options.UriPort.Value)); }
public CoapMessage Convert(CoapRequest request) { if (request is null) { throw new ArgumentNullException(nameof(request)); } var message = new CoapMessage { Type = CoapMessageType.Confirmable, Code = GetMessageCode(request.Method), Options = new List <CoapMessageOption>() }; message.Options.Add(_optionFactory.CreateUriPath(request.Uri)); message.Options.Add(_optionFactory.CreateUriPort(5648)); return(message); }
public void Encode_And_Decode_Payload_Length_12() { var optionBuilder = new CoapMessageOptionFactory(); var message = new CoapMessage { Type = CoapMessageType.Confirmable, Code = CoapMessageCodes.Put, Id = ushort.MaxValue, Payload = Encoding.UTF8.GetBytes("123456789012") }; message.Options = new List <CoapMessageOption> { optionBuilder.CreateUriPort(5648) }; Enocde_And_Decode_Internal(message); }
public void Encode_And_Decode_No_Token() { var optionBuilder = new CoapMessageOptionFactory(); var message = new CoapMessage { Type = CoapMessageType.NonConfirmable, Code = CoapMessageCodes.Put, Id = 0x5876, Token = null, Payload = null }; message.Options = new List <CoapMessageOption> { optionBuilder.CreateUriPort(66000) }; Enocde_And_Decode_Internal(message); }
public void Encode_And_Decode_No_Payload() { var optionBuilder = new CoapMessageOptionFactory(); var message = new CoapMessage { Type = CoapMessageType.Acknowledgement, Code = CoapMessageCodes.Get, Id = 0x5876, Token = new byte[] { 1, 2, 3, 4 }, Payload = null }; message.Options = new List <CoapMessageOption> { optionBuilder.CreateUriPort(50) }; Enocde_And_Decode_Internal(message); }
public void Encode_And_Decode_Full() { var optionBuilder = new CoapMessageOptionFactory(); var message = new CoapMessage { Type = CoapMessageType.Confirmable, Code = CoapMessageCodes.Post, Id = 0x5876, Token = new byte[] { 1, 2, 3, 4 }, Payload = Encoding.UTF8.GetBytes("payloadOver13chars") }; message.Options = new List <CoapMessageOption> { optionBuilder.CreateUriPort(5648) }; Enocde_And_Decode_Internal(message); }
public void Encode_Simple_Get_Request() { var expected = Convert.FromBase64String("QAHLDnIWEP9wYXlsb2Fk"); var optionBuilder = new CoapMessageOptionFactory(); var message = new CoapMessage { Type = CoapMessageType.Confirmable, Code = CoapMessageCodes.Get, Id = 0xcb0e, Payload = Encoding.UTF8.GetBytes("payload") }; message.Options = new List <CoapMessageOption> { optionBuilder.CreateUriPort(5648) }; var messageEncoder = new CoapMessageEncoder(); var messageBuffer = messageEncoder.Encode(message); AssertDataIsEqual(expected, messageBuffer.ToArray()); }
CoapMessageOption CreateOption(CoapMessageOptionNumber number, byte[] value) { if (number == CoapMessageOptionNumber.IfMatch) { return(_optionFactory.CreateIfMatch(value)); } if (number == CoapMessageOptionNumber.UriHost) { return(_optionFactory.CreateUriHost(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.ETag) { return(_optionFactory.CreateETag(value)); } if (number == CoapMessageOptionNumber.IfNoneMatch) { return(_optionFactory.CreateIfNoneMatch()); } if (number == CoapMessageOptionNumber.UriPort) { return(_optionFactory.CreateUriPort(DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.LocationPath) { return(_optionFactory.CreateLocationPath(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.UriPath) { return(_optionFactory.CreateUriPath(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.ContentFormat) { return(_optionFactory.CreateContentFormat((CoapMessageContentFormat)DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.MaxAge) { return(_optionFactory.CreateMaxAge(DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.UriQuery) { return(_optionFactory.CreateUriQuery(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.Accept) { return(_optionFactory.CreateAccept(DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.LocationQuery) { return(_optionFactory.CreateLocationQuery(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.ProxyUri) { return(_optionFactory.CreateProxyUri(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.ProxyScheme) { return(_optionFactory.CreateProxyScheme(System.Text.Encoding.UTF8.GetString(value))); } if (number == CoapMessageOptionNumber.Size1) { return(_optionFactory.CreateSize1(DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.Block1) { return(_optionFactory.CreateBlock1(DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.Block2) { return(_optionFactory.CreateBlock2(DecodeUintOptionValue(value))); } if (number == CoapMessageOptionNumber.Observe) { return(_optionFactory.CreateObserve(DecodeUintOptionValue(value))); } _logger.Warning(nameof(CoapMessageDecoder), "Invalid message: CoAP option number {0} not supported.", number); // We do not throw because new RFCs might use new options. We wrap unknown ones // into a opaque value. return(new CoapMessageOption(number, new CoapMessageOptionOpaqueValue(value))); }
CoapMessageOption CreateOption(int number, byte[] value) { if (number == (int)CoapMessageOptionNumber.IfMatch) { return(_optionFactory.CreateIfMatch(value)); } if (number == (int)CoapMessageOptionNumber.UriHost) { return(_optionFactory.CreateUriHost(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.ETag) { return(_optionFactory.CreateETag(value)); } if (number == (int)CoapMessageOptionNumber.IfNoneMatch) { return(_optionFactory.CreateIfNoneMatch()); } if (number == (int)CoapMessageOptionNumber.UriPort) { return(_optionFactory.CreateUriPort(DecodeUintOptionValue(value))); } if (number == (int)CoapMessageOptionNumber.LocationPath) { return(_optionFactory.CreateLocationPath(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.UriPath) { return(_optionFactory.CreateUriPath(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.ContentFormat) { return(_optionFactory.CreateContentFormat((CoapMessageContentFormat)value[0])); } if (number == (int)CoapMessageOptionNumber.MaxAge) { return(_optionFactory.CreateMaxAge(DecodeUintOptionValue(value))); } if (number == (int)CoapMessageOptionNumber.UriQuery) { return(_optionFactory.CreateUriQuery(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.Accept) { return(_optionFactory.CreateAccept(DecodeUintOptionValue(value))); } if (number == (int)CoapMessageOptionNumber.LocationQuery) { return(_optionFactory.CreateLocationQuery(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.ProxyUri) { return(_optionFactory.CreateProxyUri(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.ProxyScheme) { return(_optionFactory.CreateProxyScheme(System.Text.Encoding.UTF8.GetString(value))); } if (number == (int)CoapMessageOptionNumber.Size1) { return(_optionFactory.CreateSize1(DecodeUintOptionValue(value))); } throw new NotSupportedException(); }