コード例 #1
0
ファイル: CoapMessageOption.cs プロジェクト: sorisum/CoAPnet
 public CoapMessageOption(CoapMessageOptionNumber number, CoapMessageOptionValue value)
 {
     Number = number;
     Value  = value ?? throw new ArgumentNullException(nameof(value));
 }
コード例 #2
0
ファイル: CoapMessageEncoder.cs プロジェクト: sorisum/CoAPnet
        void EncodeOptions(IEnumerable <CoapMessageOption> options, CoapMessageWriter writer)
        {
            if (options == null)
            {
                return;
            }

            CoapMessageOptionNumber previousOptionNumber = 0;

            foreach (var option in options.OrderBy(o => o.Number))
            {
                // As per RFC: Only the delta of the option number is stored.
                var delta = option.Number - previousOptionNumber;
                previousOptionNumber = option.Number;

                byte[] value;

                if (option.Value is CoapMessageOptionEmptyValue)
                {
                    value = _emptyArray;
                }
                else if (option.Value is CoapMessageOptionUintValue uintValue)
                {
                    value = EncodeUintOptioNValue(uintValue.Value);
                }
                else if (option.Value is CoapMessageOptionStringValue stringValue)
                {
                    value = System.Text.Encoding.UTF8.GetBytes(stringValue.Value);
                }
                else if (option.Value is CoapMessageOptionOpaqueValue opaqueValue)
                {
                    value = opaqueValue.Value;
                }
                else
                {
                    throw new CoapProtocolViolationException("The specified option is not supported.");
                }

                var length = value.Length;

                EncodeOptionValue(delta, out var deltaNibble);
                writer.WriteBits(deltaNibble, 4);

                EncodeOptionValue(length, out var lengthNibble);
                writer.WriteBits(lengthNibble, 4);

                if (deltaNibble == 13)
                {
                    writer.WriteBits(delta - 13, 8);
                }
                else if (deltaNibble == 14)
                {
                    writer.WriteBits(delta - 269, 16);
                }

                if (lengthNibble == 13)
                {
                    writer.WriteBits(length - 13, 8);
                }
                else if (lengthNibble == 14)
                {
                    writer.WriteBits(length - 269, 16);
                }

                if (value.Length > 0)
                {
                    writer.WriteBytes(value);
                }
            }
        }
コード例 #3
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)));
        }