├── .circleci └── config.yml ├── .gitattributes ├── .gitignore ├── .nuget └── packages.config ├── .travis.yml ├── CoAP.Example ├── CoAP.Client │ ├── CoAP.Client.Std10.csproj │ ├── ExampleClient.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── README.md │ ├── ServerKeys.cbor │ ├── ServerKeys.diag │ ├── app.config │ └── packages.config └── CoAP.Server │ ├── CoAP.Server.Std10.csproj │ ├── ExampleServer.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── README.md │ ├── Resources │ ├── CarelessResource.cs │ ├── FibonacciResource.cs │ ├── HelloWorldResource.cs │ ├── ImageResource.cs │ ├── LargeResource.cs │ ├── MirrorResource.cs │ ├── SeparateResource.cs │ ├── StorageResource.cs │ └── TimeResource.cs │ └── app.config ├── CoAP.NET ├── BlockOption.cs ├── CacheKey.cs ├── Channel │ ├── DataReceivedEventArgs.cs │ ├── IChannel.cs │ ├── IPAddressExtensions.cs │ ├── UDPChannel.NET20.cs │ ├── UDPChannel.NET40.cs │ └── UDPChannel.cs ├── CoAP.Std10.csproj ├── CoAPException.cs ├── CoapClient.cs ├── CoapConfig.cs ├── CoapConstants.cs ├── CoapObserveRelation.cs ├── Code.cs ├── Codec │ ├── DatagramReader.cs │ ├── DatagramWriter.cs │ ├── IMessageDecoder.cs │ ├── IMessageEncoder.cs │ ├── MessageDecoder.cs │ └── MessageEncoder.cs ├── Coral │ ├── CoralBase.cs │ ├── CoralBaseDirective.cs │ ├── CoralBody.cs │ ├── CoralDictionary.cs │ ├── CoralDocument.cs │ ├── CoralForm.cs │ ├── CoralFormField.cs │ ├── CoralItem.cs │ ├── CoralLink.cs │ └── CoralUsing.cs ├── DTLS │ ├── DTLSChannel.cs │ ├── DTLSClient.cs │ ├── DTLSClientChannel.cs │ ├── DTLSClientEndPoint.cs │ ├── DTLSEndPoint.cs │ ├── DTLSSession.cs │ ├── DtlsServer.cs │ ├── QueueItem.cs │ ├── TlsEvent.cs │ └── TlsKey.cs ├── Deduplication │ ├── CropRotation.cs │ ├── DeduplicatorFactory.cs │ ├── IDeduplicator.cs │ ├── NoopDeduplicator.cs │ └── SweepDeduplicator.cs ├── EmptyMessage.cs ├── EndPoint │ └── Resources │ │ ├── RemoteResource.cs │ │ └── Resource.cs ├── ICoapConfig.cs ├── ISecureSession.cs ├── ISession.cs ├── LinkAttribute.cs ├── LinkFormat.cs ├── Log │ ├── CommonLoggingManager.cs │ ├── ConsoleLogManager.cs │ ├── FileLogManager.cs │ ├── ILogManager.cs │ ├── ILogger.cs │ ├── LogManager.cs │ ├── NopLogManager.cs │ └── TextWriterLogger.cs ├── MediaType.cs ├── Message.cs ├── MessageType.cs ├── Net │ ├── ClientMessageDeliverer.cs │ ├── CoAPEndPoint.cs │ ├── EndPointManager.cs │ ├── Exchange.cs │ ├── IEndPoint.cs │ ├── IMatcher.cs │ ├── IMessageDeliverer.cs │ ├── IOutbox.cs │ └── Matcher.cs ├── OSCOAP │ ├── HKDF.cs │ ├── OscoapLayer.cs │ ├── OscoapOption.cs │ ├── OscoreEvent.cs │ ├── SecureBlockwiseLayer.cs │ ├── SecurityContext.cs │ └── SecurityContextSet.cs ├── Observe │ ├── ObserveManager.cs │ ├── ObserveNotificationOrderer.cs │ ├── ObserveRelation.cs │ ├── ObservingEndpoint.cs │ └── ReregisterEventArgs.cs ├── Option.cs ├── OptionType.cs ├── Properties │ └── AssemblyInfo.cs ├── README ├── Request.cs ├── Response.cs ├── ResponseEventArgs.cs ├── Server │ ├── CoapServer.cs │ ├── IServer.cs │ ├── Resources │ │ ├── CoapExchange.cs │ │ ├── DiscoveryResource.cs │ │ ├── IResource.cs │ │ ├── Resource.cs │ │ ├── ResourceAttributes.cs │ │ └── TimerResource.cs │ └── ServerMessageDeliverer.cs ├── SessionEventArgs.cs ├── SignalMessage.cs ├── Spec.cs ├── Stack │ ├── AbstractLayer.cs │ ├── BlockwiseLayer.cs │ ├── BlockwiseStatus.cs │ ├── Chain.cs │ ├── CoapStack.cs │ ├── IEntry.cs │ ├── ILayer.cs │ ├── LayerStack.cs │ ├── ObserveLayer.cs │ ├── ReliabilityLayer.cs │ └── TokenLayer.cs ├── Threading │ ├── Executors.NET20.cs │ ├── Executors.NET40.cs │ ├── IExecutor.cs │ ├── NoThreadingExecutor.cs │ ├── TaskExecutor.cs │ └── ThreadPoolExecutor.cs ├── Util │ ├── ArrayEqualityComparer.cs │ ├── ByteArrayUtils.cs │ ├── Cori.cs │ ├── Delegates.cs │ ├── Scanner.cs │ ├── System.Collections.Concurrent │ │ ├── ConcurrentDictionary.cs │ │ ├── ConcurrentQueue.cs │ │ └── IProducerConsumerCollection.cs │ ├── System.Collections.Generic │ │ ├── BitHelper.cs │ │ ├── HashHelpers.cs │ │ ├── HashSet.cs │ │ └── SynchronizedCollection.cs │ ├── System.Threading │ │ ├── SpinWait.cs │ │ └── Volatile.cs │ ├── System │ │ └── Funcs.cs │ ├── ThrowHelper.cs │ ├── Utils.cs │ └── WaitFuture.cs ├── WebLink.cs ├── app.config └── coapnet.snk ├── CoAP.Std10.sln ├── CoAP.Std10.sln.DotSettings ├── CoAP.Test ├── BlockOptionTest.cs ├── BlockwiseTransferTest.cs ├── Channel │ ├── IPAddressTest.cs │ └── MulticastTests.cs ├── CoAP.Test.Std10.csproj ├── CoRAL │ ├── Klaus.csv │ ├── TestBaseDirective.cs │ ├── TestCoralBody.cs │ ├── TestCoralDocument.cs │ ├── TestCoralForm.cs │ ├── TestCoralLink.cs │ ├── TestCoralUsing.cs │ └── TestDictionary.cs ├── CoapClientTest.cs ├── Codec │ ├── DatagramReadWriteTest.cs │ └── MessageTest.cs ├── DTLS │ ├── DTLSClientEndPoint.cs │ ├── DTLSResourceTest.cs │ ├── DtlsEvents.cs │ └── DtlsX509.cs ├── MemoryLeakingMapTest.cs ├── MessageTypeTest.cs ├── MockDriver │ ├── MockChannel.cs │ ├── MockDeliverer.cs │ ├── MockEndpoint.cs │ ├── MockMessagePump.cs │ ├── MockQueueItem.cs │ ├── MockSession.cs │ └── MockStack.cs ├── OSCOAP │ ├── Callbacks.cs │ ├── Oscoap.cs │ ├── SecurityContext.cs │ └── TestInnerBlockwise.cs ├── Observe │ ├── ObserveTests.cs │ └── ObserveTests2.cs ├── OptionTest.cs ├── Properties │ └── AssemblyInfo.cs ├── RandomAccessBlockTest.cs ├── ResourceAttributesTest.cs ├── ResourceTest.cs ├── ResourceTreeTest.cs ├── Stack │ └── BlockwiseTest.cs ├── StartStopTest.cs ├── Utils │ └── TestCiri.cs ├── app.config └── packages.config ├── LICENSE ├── README.md ├── appveyor.yml ├── build.sh └── coap.nuspec /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: mcr.microsoft.com/dotnet/core/sdk:2.1 6 | 7 | working_directory: ~/repo 8 | 9 | steps: 10 | - checkout 11 | 12 | - run: 13 | name: Get Examples 14 | command: git clone --depth 1 https://github.com/cose-wg/Examples Regressions 15 | 16 | - run: 17 | name: Unit Tests 18 | command: | 19 | dotnet test --framework=netcoreapp2.0 CoAP.Std10.sln 20 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | 19 | .csproj eol=crlf 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | lib 5 | .svn 6 | Apps 7 | cov-int 8 | _ReSharper.Caches 9 | 10 | # DNX project file with a complete list of resolved dependencies 11 | project.lock.json 12 | 13 | # User-specific files 14 | *.suo 15 | *.user 16 | *.sln.docstates 17 | *.dev.csproj 18 | nuspec.bat 19 | 20 | # MSTest test Results 21 | [Tt]est[Rr]esult*/ 22 | [Bb]uild[Ll]og.* 23 | 24 | # Visual Studio not saved 25 | .vs 26 | *.snk 27 | 28 | 29 | # NuGet Packages Directory 30 | packages/ 31 | *.nupkg 32 | 33 | # Resharper 34 | _ReSharper.Caches 35 | 36 | # Emacs turds 37 | *# 38 | *~ 39 | *.bak 40 | .#* 41 | *.kept 42 | -------------------------------------------------------------------------------- /.nuget/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Travis-CI Build for dynamitey 2 | # see travis-ci.org for details 3 | 4 | language: csharp 5 | os: 6 | - linux 7 | - osx 8 | 9 | env: 10 | - SLN=CoAP.Std10.sln 11 | TARGET=Std10 12 | VERSION=Release 13 | 14 | install: 15 | - curl -L -o nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe 16 | - mono nuget.exe install NUnit.Runners -Version 3.8.0 -OutputDirectory testrunner 17 | 18 | script: 19 | - ./build.sh 20 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/CoAP.Client.Std10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Exe 6 | netcoreapp2.0;net462 7 | 8 | 9 | 10 | TRACE 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("CoAP Client")] 9 | [assembly: AssemblyDescription("A sample of CoAP client to show how the CoAP framework in C# works")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("SmeshLink Technology")] 12 | [assembly: AssemblyProduct("CoAP.NET")] 13 | [assembly: AssemblyCopyright("Copyright © SmeshLink 2011-2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("24a48ec6-9e93-491d-8916-5cbba26b658d")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 内部版本号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("0.13.*")] 36 | [assembly: AssemblyFileVersion("0.13")] 37 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/README.md: -------------------------------------------------------------------------------- 1 | CoAP.Client - A CoAP Example Client 2 | =================================== 3 | 4 | This project demonstrates how to access remote CoAP resource 5 | with CoAP **[Request] (Request.cs)**. 6 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/ServerKeys.cbor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Com-AugustCellars/CoAP-CSharp/de0711e29e1038b5f2bf0b5640d083433af234d8/CoAP.Example/CoAP.Client/ServerKeys.cbor -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/ServerKeys.diag: -------------------------------------------------------------------------------- 1 | [ 2 | {1: 4, 2: h'4b65792330', 3: 10, "KDF": "dir+kdf", "RecipID": h'636c69656e74', "SenderID": h'736572766572', -1: h'0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223', "usage":"oscoap"}, 3 | {1: 4, 2: h'4b65792331', 3: 10, "KDF": "dir+kdf", "RecipID": h'636c69656e74', "SenderID": h'736572766572', -1: h'020406080a0c0e10121416181a1c1e20222426282a2c2e30323436383a3c3e40424446', "usage":"oscoap"}, 4 | {1: 4, 2: h'4b65792332', 3: 10, "KDF": "dir+kdf", "RecipID": h'636c69656e74', "SenderID": h'736572766572', -1: h'0306090c0f1215181b1e2124272a2d303336393c3f4245484b4e5154575a5d60636669', "usage":"oscoap"}, 5 | {1: 4, 2: h'4b65792332', 3: 30, "KDF": "dir+kdf", "RecipID": h'636c69656e74', "SenderID": h'736572766572', -1: h'04080c1014181c2024282c3034383c4044484c5054585c6064686c7074787c8084888c', "usage":"oscoap"}, 6 | {1: 4, 2: h'636c69656e7F', 3: 30, "KDF": "dir+kdf", "RecipID": h'636c69656e7F', "SenderID": h'736572766572', -1: h'04080c1014181c2024282c3034383c4044484c5054585c6064686c7074787c8084888c', "usage":"edhoc"}, 7 | {1: 4, 2: h'636c69656e74', 3: 10, "KDF": "dir+kdf", "RecipID": h'636c69656e74', "SenderID": h'736572766572', -1: h'0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223', "usage":"oscoap"}, 8 | {1: 4, 2: h'6d656d6265723031', 3: 10, "SenderID": h'6d656d6265723032', -1: h'080c1014181c2024282c3034383c4044484c5054585c6064686c7074787c8084888c8992', "usage":"oscoap-group", 9 | "recipients":[{"RecipID": h'6d656d6265723031'}, {"RecipID": h'6d656d6265723033'}]} 10 | 11 | ] 12 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Client/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/CoAP.Server.Std10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Exe 6 | netcoreapp2.0;net462 7 | 8 | 9 | 10 | TRACE 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/ExampleServer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Com.AugustCellars.CoAP.Examples.Resources; 3 | using Com.AugustCellars.CoAP.Server; 4 | 5 | namespace Com.AugustCellars.CoAP.Examples 6 | { 7 | public class ExampleServer 8 | { 9 | public static void Main(String[] args) 10 | { 11 | CoapServer server = new CoapServer(); 12 | 13 | server.Add(new HelloWorldResource("hello")); 14 | server.Add(new FibonacciResource("fibonacci")); 15 | server.Add(new StorageResource("storage")); 16 | server.Add(new MirrorResource("mirror")); 17 | server.Add(new LargeResource("large")); 18 | server.Add(new CarelessResource("careless")); 19 | server.Add(new SeparateResource("separate")); 20 | server.Add(new TimeResource("time")); 21 | 22 | try 23 | { 24 | server.Start(); 25 | 26 | Console.Write("CoAP server [{0}] is listening on", server.Config.Version); 27 | 28 | foreach (var item in server.EndPoints) 29 | { 30 | Console.Write(" "); 31 | Console.Write(item.LocalEndPoint); 32 | } 33 | Console.WriteLine(); 34 | } 35 | catch (Exception ex) 36 | { 37 | Console.WriteLine(ex.Message); 38 | } 39 | 40 | Console.WriteLine("Press any key to exit."); 41 | Console.ReadKey(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("CoAP Server")] 9 | [assembly: AssemblyDescription("A sample of CoAP server to show how the CoAP framework in C# works")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("SmeshLink Technology")] 12 | [assembly: AssemblyProduct("CoAP.NET")] 13 | [assembly: AssemblyCopyright("Copyright © SmeshLink 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("319a1e78-2ba9-49f4-838d-76d050cd5913")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 内部版本号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("0.13.*")] 36 | [assembly: AssemblyFileVersion("0.13")] 37 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/README.md: -------------------------------------------------------------------------------- 1 | CoAP.Server - A CoAP Example Server 2 | =================================== 3 | 4 | This project demonstrates how to build a CoAP server with CoAP.NET. 5 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/CarelessResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Com.AugustCellars.CoAP.Server.Resources; 3 | 4 | namespace Com.AugustCellars.CoAP.Examples.Resources 5 | { 6 | /// 7 | /// Represents a resource that forgets to return a separate response. 8 | /// 9 | class CarelessResource : Resource 10 | { 11 | public CarelessResource(String name) 12 | : base(name) 13 | { 14 | Attributes.Title = "This resource will ACK anything, but never send a separate response"; 15 | Attributes.AddResourceType("SepararateResponseTester"); 16 | } 17 | 18 | protected override void DoGet(CoapExchange exchange) 19 | { 20 | // Accept the request to promise the client this request will be acted. 21 | exchange.Accept(); 22 | 23 | // ... and then do nothing. Pretty mean... 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/FibonacciResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Com.AugustCellars.CoAP.Server.Resources; 3 | 4 | namespace Com.AugustCellars.CoAP.Examples.Resources 5 | { 6 | /// 7 | /// This resource computes the Fibonacci numbers and therefore needs 8 | /// a lot of computing power to respond to a request. Use the query ?n=20 to 9 | /// compute the 20. Fibonacci number, e.g.: coap://localhost:5683/fibonacci?n=20. 10 | /// 11 | class FibonacciResource : Resource 12 | { 13 | public FibonacciResource(String name) 14 | : base(name) 15 | { } 16 | 17 | protected override void DoGet(CoapExchange exchange) 18 | { 19 | Int32? n = null; 20 | foreach (String query in exchange.Request.UriQueries) 21 | { 22 | String[] tmp = query.Split('='); 23 | if (tmp.Length != 2 || tmp[0] != "n") 24 | continue; 25 | n = Int32.Parse(tmp[1]); 26 | } 27 | if (n.HasValue) 28 | exchange.Respond("Fibonacci(" + n.Value + ") = " + Fibonacci(n.Value)); 29 | else 30 | exchange.Respond("Missing n in query"); 31 | } 32 | 33 | private UInt64 Fibonacci(Int32 n) 34 | { 35 | return Fibs(n)[1]; 36 | } 37 | 38 | private UInt64[] Fibs(Int32 n) 39 | { 40 | if (n == 1) 41 | return new[] { 0UL, 1UL }; 42 | UInt64[] fibs = Fibs(n - 1); 43 | return new[] { fibs[1], fibs[0] + fibs[1] }; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/HelloWorldResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Com.AugustCellars.CoAP.Server.Resources; 3 | 4 | namespace Com.AugustCellars.CoAP.Examples.Resources 5 | { 6 | /// 7 | /// This resource responds with a kind "hello world" to GET requests. 8 | /// 9 | class HelloWorldResource : Resource 10 | { 11 | public HelloWorldResource(String name) 12 | : base(name) 13 | { 14 | Attributes.Title = "GET a friendly greeting!"; 15 | Attributes.AddResourceType("HelloWorldDisplayer"); 16 | } 17 | 18 | protected override void DoGet(CoapExchange exchange) 19 | { 20 | exchange.Respond("Hello World!"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/ImageResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using Com.AugustCellars.CoAP.Server.Resources; 4 | 5 | namespace Com.AugustCellars.CoAP.Examples.Resources 6 | { 7 | class ImageResource : Resource 8 | { 9 | private Int32[] _supported = new Int32[] { 10 | MediaType.ImageJpeg, 11 | MediaType.ImagePng 12 | }; 13 | 14 | public ImageResource(String name) 15 | : base(name) 16 | { 17 | Attributes.Title = "GET an image with different content-types"; 18 | Attributes.AddResourceType("Image"); 19 | 20 | foreach (Int32 item in _supported) 21 | { 22 | Attributes.AddContentType(item); 23 | } 24 | 25 | Attributes.MaximumSizeEstimate = 18029; 26 | } 27 | 28 | protected override void DoGet(CoapExchange exchange) 29 | { 30 | String file = "data\\image\\"; 31 | Int32 ct = MediaType.ImagePng; 32 | Request request = exchange.Request; 33 | 34 | if ((ct = MediaType.NegotiationContent(ct, _supported, request.GetOptions(OptionType.Accept))) 35 | == MediaType.Undefined) 36 | { 37 | exchange.Respond(StatusCode.NotAcceptable); 38 | } 39 | else 40 | { 41 | file += "image." + MediaType.ToFileExtension(ct); 42 | if (File.Exists(file)) 43 | { 44 | Byte[] data = null; 45 | 46 | try 47 | { 48 | data = File.ReadAllBytes(file); 49 | } 50 | catch (Exception ex) 51 | { 52 | exchange.Respond(StatusCode.InternalServerError, "IO error"); 53 | Console.WriteLine(ex.Message); 54 | } 55 | 56 | Response response = new Response(StatusCode.Content); 57 | response.Payload = data; 58 | response.ContentType = ct; 59 | exchange.Respond(response); 60 | } 61 | else 62 | { 63 | exchange.Respond(StatusCode.InternalServerError, "Image file not found"); 64 | } 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/MirrorResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using Com.AugustCellars.CoAP.Net; 4 | using Com.AugustCellars.CoAP.Server.Resources; 5 | 6 | namespace Com.AugustCellars.CoAP.Examples.Resources 7 | { 8 | /// 9 | /// This resource responds with the data from a request in its payload. This 10 | /// resource responds to GET, POST, PUT and DELETE requests. 11 | /// 12 | class MirrorResource : Resource 13 | { 14 | public MirrorResource(String name) 15 | : base(name) 16 | { } 17 | 18 | public override void HandleRequest(Exchange exchange) 19 | { 20 | Request request = exchange.Request; 21 | StringBuilder buffer = new StringBuilder(); 22 | buffer.Append("resource ").Append(Uri).Append(" received request") 23 | .Append("\n").Append("Code: ").Append(request.Code) 24 | .Append("\n").Append("Source: ").Append(request.Source) 25 | .Append("\n").Append("Type: ").Append(request.Type) 26 | .Append("\n").Append("MID: ").Append(request.ID) 27 | .Append("\n").Append("Token: ").Append(request.TokenString) 28 | //.Append("\n").Append(request.Options) 29 | ; 30 | Response response = new Response(StatusCode.Content); 31 | response.PayloadString = buffer.ToString(); 32 | exchange.SendResponse(response); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/SeparateResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using Com.AugustCellars.CoAP.Server.Resources; 4 | 5 | namespace Com.AugustCellars.CoAP.Examples.Resources 6 | { 7 | /// 8 | /// Represents a resource that returns a response in a separate CoAP message. 9 | /// 10 | class SeparateResource : Resource 11 | { 12 | public SeparateResource(String name) 13 | : base(name) 14 | { 15 | Attributes.Title = "GET a response in a separate CoAP Message"; 16 | Attributes.AddResourceType("SepararateResponseTester"); 17 | } 18 | 19 | protected override void DoGet(CoapExchange exchange) 20 | { 21 | // Accept the request to promise the client this request will be acted. 22 | exchange.Accept(); 23 | 24 | // Do sth. time-consuming 25 | Thread.Sleep(2000); 26 | 27 | // Now respond the previous request. 28 | Response response = new Response(StatusCode.Content); 29 | response.PayloadString = "This message was sent by a separate response.\n" + 30 | "Your client will need to acknowledge it, otherwise it will be retransmitted."; 31 | 32 | exchange.Respond(response); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/StorageResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Com.AugustCellars.CoAP.Server.Resources; 5 | 6 | namespace Com.AugustCellars.CoAP.Examples.Resources 7 | { 8 | class StorageResource : Resource 9 | { 10 | private String _content; 11 | 12 | public StorageResource(String name) 13 | : base(name) 14 | { } 15 | 16 | protected override void DoGet(CoapExchange exchange) 17 | { 18 | if (_content != null) 19 | { 20 | exchange.Respond(_content); 21 | } 22 | else 23 | { 24 | String subtree = LinkFormat.Serialize(this, null); 25 | exchange.Respond(StatusCode.Content, subtree, MediaType.ApplicationLinkFormat); 26 | } 27 | } 28 | 29 | protected override void DoPost(CoapExchange exchange) 30 | { 31 | String payload = exchange.Request.PayloadString; 32 | if (payload == null) 33 | payload = String.Empty; 34 | String[] parts = payload.Split('\\'); 35 | String[] path = parts[0].Split('/'); 36 | IResource resource = Create(new LinkedList(path)); 37 | 38 | Response response = new Response(StatusCode.Created); 39 | response.LocationPath = resource.Uri; 40 | exchange.Respond(response); 41 | } 42 | 43 | protected override void DoPut(CoapExchange exchange) 44 | { 45 | _content = exchange.Request.PayloadString; 46 | exchange.Respond(StatusCode.Changed); 47 | } 48 | 49 | protected override void DoDelete(CoapExchange exchange) 50 | { 51 | this.Delete(); 52 | exchange.Respond(StatusCode.Deleted); 53 | } 54 | 55 | private IResource Create(LinkedList path) 56 | { 57 | String segment; 58 | 59 | do 60 | { 61 | if (path.Count == 0) 62 | return this; 63 | segment = path.First.Value; 64 | path.RemoveFirst(); 65 | } while (segment.Length == 0 || segment.Equals("/")); 66 | 67 | StorageResource resource = new StorageResource(segment); 68 | Add(resource); 69 | return resource.Create(path); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /CoAP.Example/CoAP.Server/Resources/TimeResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using Com.AugustCellars.CoAP.Server.Resources; 5 | #if false 6 | using PeterO.Cbor; 7 | #endif 8 | 9 | namespace Com.AugustCellars.CoAP.Examples.Resources 10 | { 11 | class TimeResource : Resource 12 | { 13 | private Timer _timer; 14 | private DateTime _now; 15 | 16 | public TimeResource(String name) 17 | : base(name) 18 | { 19 | Attributes.Title = "GET the current time"; 20 | Attributes.AddResourceType("CurrentTime"); 21 | Observable = true; 22 | 23 | _timer = new Timer(Timed, null, 0, 2000*30*30); 24 | } 25 | 26 | private void Timed(Object o) 27 | { 28 | _now = DateTime.Now; 29 | Changed(); 30 | } 31 | 32 | protected override void DoGet(CoapExchange exchange) 33 | { 34 | #if true 35 | exchange.Respond(StatusCode.Content, _now.ToString(), MediaType.TextPlain); 36 | #else 37 | Request request = exchange.Request; 38 | 39 | IEnumerable