예제 #1
0
        public void Encode_And_Decode_Multiple_Uri_Path()
        {
            var optionBuilder = new CoapMessageOptionFactory();

            var message = new CoapMessage
            {
                Type    = CoapMessageType.Acknowledgement,
                Code    = CoapMessageCodes.Delete,
                Id      = 0x50,
                Token   = null,
                Payload = null
            };

            message.Options = new List <CoapMessageOption>
            {
                optionBuilder.CreateUriPath("a"),
                optionBuilder.CreateUriPath("b"),
                optionBuilder.CreateUriPath("c")
            };

            Enocde_And_Decode_Internal(message);
        }
예제 #2
0
        void ApplyUriPath(CoapRequest request, CoapMessage message)
        {
            if (string.IsNullOrEmpty(request.Options.UriPath))
            {
                return;
            }

            var paths = request.Options.UriPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var path in paths)
            {
                message.Options.Add(_optionFactory.CreateUriPath(path));
            }
        }
        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);
        }
예제 #4
0
        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)));
        }
예제 #5
0
        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();
        }