diff --git a/CoAP.Example/CoAP.Client/CoAP.Client.NET45.csproj b/CoAP.Example/CoAP.Client/CoAP.Client.NET45.csproj index 90119b6..abdffb5 100644 --- a/CoAP.Example/CoAP.Client/CoAP.Client.NET45.csproj +++ b/CoAP.Example/CoAP.Client/CoAP.Client.NET45.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,7 +10,7 @@ Properties Com.AugustCellars.CoAP.Examples CoAPClient - v4.5 + v4.6.2 512 @@ -43,14 +43,17 @@ 4 - - ..\..\packages\PeterO.Cbor.3.0.0\lib\netstandard1.0\CBOR.dll + + ..\..\packages\Portable.BouncyCastle.1.8.2\lib\net40\BouncyCastle.Crypto.dll - - ..\..\packages\Com.AugustCellars.COSE.1.1.0\lib\netstandard1.0\COSE.dll + + ..\..\packages\PeterO.Cbor.3.0.3\lib\netstandard1.0\CBOR.dll - - ..\..\packages\PeterO.Numbers.1.0.0\lib\netstandard1.0\Numbers.dll + + ..\..\packages\Com.AugustCellars.COSE.1.2.0\lib\netstandard1.0\COSE.dll + + + ..\..\packages\PeterO.Numbers.1.0.2\lib\netstandard1.0\Numbers.dll diff --git a/CoAP.Example/CoAP.Client/app.config b/CoAP.Example/CoAP.Client/app.config index 68fc31b..2ffb337 100644 --- a/CoAP.Example/CoAP.Client/app.config +++ b/CoAP.Example/CoAP.Client/app.config @@ -1,11 +1,19 @@ - + - - + + + + + + + + + + - \ No newline at end of file + diff --git a/CoAP.Example/CoAP.Client/packages.config b/CoAP.Example/CoAP.Client/packages.config index 32523e2..3da4dbf 100644 --- a/CoAP.Example/CoAP.Client/packages.config +++ b/CoAP.Example/CoAP.Client/packages.config @@ -1,31 +1,31 @@  - + - - - + + + - + - - + + - + - + - - - + + + @@ -33,6 +33,6 @@ - + \ No newline at end of file diff --git a/CoAP.Example/CoAP.Server/CoAP.Server.NET45.csproj b/CoAP.Example/CoAP.Server/CoAP.Server.NET45.csproj index 1e68389..f28a741 100644 --- a/CoAP.Example/CoAP.Server/CoAP.Server.NET45.csproj +++ b/CoAP.Example/CoAP.Server/CoAP.Server.NET45.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,7 +10,7 @@ Properties Com.AugustCellars.CoAP.Examples CoAPServer - v4.5 + v4.6.2 512 @@ -43,17 +43,17 @@ 4 - - ..\..\packages\Portable.BouncyCastle.1.8.1.3\lib\net40\BouncyCastle.Crypto.dll + + ..\..\packages\Portable.BouncyCastle.1.8.2\lib\net40\BouncyCastle.Crypto.dll - - ..\..\packages\PeterO.Cbor.3.0.0\lib\netstandard1.0\CBOR.dll + + ..\..\packages\PeterO.Cbor.3.0.3\lib\netstandard1.0\CBOR.dll - - ..\..\packages\Com.AugustCellars.COSE.1.1.0\lib\netstandard1.0\COSE.dll + + ..\..\packages\Com.AugustCellars.COSE.1.2.0\lib\netstandard1.0\COSE.dll - - ..\..\packages\PeterO.Numbers.1.0.0\lib\netstandard1.0\Numbers.dll + + ..\..\packages\PeterO.Numbers.1.0.2\lib\netstandard1.0\Numbers.dll diff --git a/CoAP.Example/CoAP.Server/app.config b/CoAP.Example/CoAP.Server/app.config index 067484f..2ffb337 100644 --- a/CoAP.Example/CoAP.Server/app.config +++ b/CoAP.Example/CoAP.Server/app.config @@ -1,15 +1,19 @@ - + - - + + - - + + + + + + - \ No newline at end of file + diff --git a/CoAP.Example/CoAP.Server/packages.config b/CoAP.Example/CoAP.Server/packages.config index 32523e2..3da4dbf 100644 --- a/CoAP.Example/CoAP.Server/packages.config +++ b/CoAP.Example/CoAP.Server/packages.config @@ -1,31 +1,31 @@  - + - - - + + + - + - - + + - + - + - - - + + + @@ -33,6 +33,6 @@ - + \ No newline at end of file diff --git a/CoAP.NET/CoAP.NET45.csproj b/CoAP.NET/CoAP.NET45.csproj index 45ae970..4da146a 100644 --- a/CoAP.NET/CoAP.NET45.csproj +++ b/CoAP.NET/CoAP.NET45.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,7 +10,7 @@ Properties Com.AugustCellars.CoAP CoAP - v4.5 + v4.6.2 512 @@ -49,11 +49,11 @@ coapnet.snk - - ..\packages\Portable.BouncyCastle.1.8.1.3\lib\net40\BouncyCastle.Crypto.dll + + ..\packages\Portable.BouncyCastle.1.8.2\lib\net40\BouncyCastle.Crypto.dll - - ..\packages\PeterO.Cbor.3.0.0\lib\netstandard1.0\CBOR.dll + + ..\packages\PeterO.Cbor.3.0.3\lib\netstandard1.0\CBOR.dll ..\packages\Common.Logging.3.0.0\lib\net40\Common.Logging.dll @@ -61,11 +61,11 @@ ..\packages\Common.Logging.Core.3.0.0\lib\net40\Common.Logging.Core.dll - - ..\packages\Com.AugustCellars.COSE.1.1.0\lib\netstandard1.0\COSE.dll + + ..\packages\Com.AugustCellars.COSE.1.2.0\lib\netstandard1.0\COSE.dll - - ..\packages\PeterO.Numbers.1.0.0\lib\netstandard1.0\Numbers.dll + + ..\packages\PeterO.Numbers.1.0.2\lib\netstandard1.0\Numbers.dll diff --git a/CoAP.NET/CoapClient.cs b/CoAP.NET/CoapClient.cs index 499cad6..645c386 100644 --- a/CoAP.NET/CoapClient.cs +++ b/CoAP.NET/CoapClient.cs @@ -84,7 +84,9 @@ public CoapClient(string uri, ICoapConfig config) : this(new Uri(uri), config) /// the config public CoapClient(Uri uri, ICoapConfig config) { - Uri = uri; + UriPath = uri.AbsolutePath; + UriQuery = uri.Query; + Uri = new Uri(uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped)); _config = config ?? CoapConfig.Default; } @@ -209,16 +211,22 @@ public IEnumerable Discover(int mediaType = MediaType.Undefined) return Discover(null, mediaType); } + public IEnumerable Discover(string query, int mediaType = MediaType.Undefined) + { + return Discover(CoapConstants.DefaultWellKnownURI, query, mediaType); + } + /// /// Discovers remote resources. /// + /// path to be queried /// the query to filter resources /// format to use - defaults to any /// the descoverd representing remote resources, or null if no response - public IEnumerable Discover(String query, int mediaType = MediaType.Undefined) + public IEnumerable Discover(string uriPath, String query, int mediaType = MediaType.Undefined) { Request discover = Prepare(Request.NewGet()); - discover.ClearUriPath().ClearUriQuery().UriPath = CoapConstants.DefaultWellKnownURI; + discover.ClearUriPath().ClearUriQuery().UriPath = uriPath; if (!String.IsNullOrEmpty(query)) { discover.UriQuery = query; } @@ -236,10 +244,10 @@ public IEnumerable Discover(String query, int mediaType = MediaType.Und case MediaType.ApplicationLinkFormat: return LinkFormat.Parse(links.PayloadString); - case MediaType.ApplicationCbor: + case MediaType.ApplicationLinkFormatCbor: return LinkFormat.ParseCbor(links.Payload); - case MediaType.ApplicationJson: + case MediaType.ApplicationLinkFormatJson: return LinkFormat.ParseJson(links.PayloadString); default: diff --git a/CoAP.NET/Codec/MessageDecoder.cs b/CoAP.NET/Codec/MessageDecoder.cs index e65471d..cc9cf47 100644 --- a/CoAP.NET/Codec/MessageDecoder.cs +++ b/CoAP.NET/Codec/MessageDecoder.cs @@ -79,10 +79,11 @@ public virtual Boolean IsRequest /// public virtual Boolean IsResponse { - get + get { - return m_code >= CoapConstants.ResponseCodeLowerBound && - m_code <= CoapConstants.ResponseCodeUpperBound; + return (m_code >= CoapConstants.ResponseCodeLowerBound && + m_code <= CoapConstants.ResponseCodeUpperBound) || + m_code == (int) SignalCode.Pong; } } diff --git a/CoAP.NET/DTLS/DTLSClient.cs b/CoAP.NET/DTLS/DTLSClient.cs index ebaaf19..28fb3f1 100644 --- a/CoAP.NET/DTLS/DTLSClient.cs +++ b/CoAP.NET/DTLS/DTLSClient.cs @@ -113,6 +113,13 @@ internal MyTlsAuthentication(TlsContext context) this._mContext = context; } +#if SUPPORT_RPK + public virtual void NotifyServerCertificate(AbstractCertificate x) + { + + } +#endif + public virtual void NotifyServerCertificate(Certificate serverCertificate) { /* diff --git a/CoAP.NET/EndPoint/Resources/RemoteResource.cs b/CoAP.NET/EndPoint/Resources/RemoteResource.cs index 4d715f4..c794e4f 100644 --- a/CoAP.NET/EndPoint/Resources/RemoteResource.cs +++ b/CoAP.NET/EndPoint/Resources/RemoteResource.cs @@ -23,7 +23,7 @@ public static RemoteResource NewRoot(String linkFormat, int mediaType = MediaTyp case MediaType.ApplicationLinkFormat: return LinkFormat.Deserialize(linkFormat); - case MediaType.ApplicationJson: + case MediaType.ApplicationLinkFormatJson: return LinkFormat.DeserializeJson(linkFormat); default: @@ -37,10 +37,10 @@ public static RemoteResource NewRoot(byte[] linkFormat, int mediaType = MediaTyp case MediaType.ApplicationLinkFormat: return LinkFormat.Deserialize(Encoding.UTF8.GetString(linkFormat)); - case MediaType.ApplicationCbor: + case MediaType.ApplicationLinkFormatCbor: return LinkFormat.DeserializeCbor(linkFormat); - case MediaType.ApplicationJson: + case MediaType.ApplicationLinkFormatJson: return LinkFormat.DeserializeJson(Encoding.UTF8.GetString(linkFormat)); default: diff --git a/CoAP.NET/LinkFormat.cs b/CoAP.NET/LinkFormat.cs index 7bd7580..b83e476 100644 --- a/CoAP.NET/LinkFormat.cs +++ b/CoAP.NET/LinkFormat.cs @@ -107,7 +107,7 @@ public static class LinkFormat private static readonly ILogger _Log = LogManager.GetLogger(typeof(LinkFormat)); // Mapping defined in the RFC - public static readonly Dictionary _CborAttributeKeys = new Dictionary() { + public static readonly Dictionary CborAttributeKeys = new Dictionary() { ["href"] = CBORObject.FromObject(1), ["rel"] = CBORObject.FromObject(2), ["anchor"] = CBORObject.FromObject(3), @@ -165,7 +165,7 @@ public static byte[] SerializeCbor(IResource root, IEnumerable queries) if (queries != null) queryList = queries.ToList(); foreach (IResource child in root.Children) { - SerializeTree(child, queryList, linkFormat, _CborAttributeKeys); + SerializeTree(child, queryList, linkFormat, CborAttributeKeys); } return linkFormat.EncodeToBytes(); @@ -229,7 +229,7 @@ public static IEnumerable Parse(string linkFormat) public static IEnumerable ParseCbor(byte[] linkFormat) { CBORObject links = CBORObject.DecodeFromBytes(linkFormat); - return ParseCommon(links, _CborAttributeKeys); + return ParseCommon(links, CborAttributeKeys); } public static IEnumerable ParseJson(string linkFormat) @@ -263,6 +263,7 @@ private static IEnumerable ParseCommon(CBORObject links, Dictionary ParseCommon(CBORObject links, Dictionary queries, CBOR } } - public static void SerializeResource(IResource resource, StringBuilder sb) + public static void SerializeResource(IResource resource, StringBuilder sb, ResourceAttributes otherAttributes = null, + Uri uriRelative = null) { - sb.Append("<") - .Append(resource.Path) - .Append(resource.Name) - .Append(">"); - SerializeAttributes(resource.Attributes, sb); + sb.Append("<"); + if (uriRelative != null) { + sb.Append(new Uri(uriRelative, resource.Path + resource.Name)); + } + else { + sb.Append(resource.Path) + .Append(resource.Name); + } + sb.Append(">"); + SerializeAttributes(resource.Attributes, sb, uriRelative); + if (otherAttributes != null) { + SerializeAttributes(otherAttributes, sb, uriRelative); + } } - public static void SerializeResource(IResource resource, CBORObject cbor, Dictionary dictionary) + public static void SerializeResource(IResource resource, CBORObject cbor, Dictionary dictionary, + ResourceAttributes otherAttributes = null, Uri uriRelative = null) { CBORObject obj = CBORObject.NewMap(); + string href; + if (uriRelative == null) { + href = resource.Path + resource.Name; + } + else { + href = new Uri(uriRelative, resource.Path + resource.Name).ToString(); + } - if (dictionary == null) obj.Add("href", resource.Path + resource.Name); - else obj.Add(1, resource.Path + resource.Name); - SerializeAttributes(resource.Attributes, obj, dictionary); + if (dictionary == null) { + obj.Add("href", href); + } + else { + obj.Add(1, href); + } + SerializeAttributes(resource.Attributes, obj, dictionary, uriRelative); + if (otherAttributes != null) { + SerializeAttributes(otherAttributes, obj, dictionary, uriRelative); + } cbor.Add(obj); } - private static void SerializeAttributes(ResourceAttributes attributes, StringBuilder sb) + private static void SerializeAttributes(ResourceAttributes attributes, StringBuilder sb, Uri uriRelative) { List keys = new List(attributes.Keys); keys.Sort(); foreach (string name in keys) { List values = new List(attributes.GetValues(name)); - if (values.Count == 0) continue; + if (values.Count == 0) { + continue; + } + + if (uriRelative != null && name == "anchor") { + List newValues = new List(); + foreach (string val in values) { + newValues.Add(new Uri(uriRelative, val).ToString()); + } + + values = newValues; + } sb.Append(Separator); SerializeAttribute(name, values, sb); } } - private static void SerializeAttributes(ResourceAttributes attributes, CBORObject cbor, Dictionary dictionary) + private static void SerializeAttributes(ResourceAttributes attributes, CBORObject cbor, Dictionary dictionary, Uri uriRelative) { List keys = new List(attributes.Keys); keys.Sort(); foreach (string name in keys) { List values = new List(attributes.GetValues(name)); - if (values.Count == 0) continue; + if (values.Count == 0) { + continue; + } + + if (uriRelative != null && name == "anchor") { + List newValues = new List(); + foreach (string val in values) { + newValues.Add(new Uri(uriRelative, val).ToString()); + } + + values = newValues; + } SerializeAttribute(name, values, cbor, dictionary); } @@ -579,7 +626,7 @@ private static RemoteResource DeserializeCbor(CBORObject cbor) string keyName; if (key.Type == CBORType.Number) { keyName = null; - foreach (KeyValuePair kvp in _CborAttributeKeys) { + foreach (KeyValuePair kvp in CborAttributeKeys) { if (key.Equals(kvp.Value)) { keyName = kvp.Key; break; diff --git a/CoAP.NET/MediaType.cs b/CoAP.NET/MediaType.cs index 890eb10..dc2e7d9 100644 --- a/CoAP.NET/MediaType.cs +++ b/CoAP.NET/MediaType.cs @@ -118,6 +118,14 @@ public class MediaType /// public const int ApplicationCbor = 60; /// + /// application/link-format+cbor - [RFC TBD] + /// + public const int ApplicationLinkFormatCbor = 64; + /// + /// application/link-format+json - [RFC TBD] + /// + public const int ApplicationLinkFormatJson = 504; + /// /// any /// public const Int32 Any = 0xFF; diff --git a/CoAP.NET/Option.cs b/CoAP.NET/Option.cs index 79e584b..450e86e 100644 --- a/CoAP.NET/Option.cs +++ b/CoAP.NET/Option.cs @@ -262,8 +262,32 @@ public static IEnumerable