Skip to content

Constrained Applciaiton Protocol (CoAP) [RFC7252] for .Net Standard

License

Notifications You must be signed in to change notification settings

MgenChina/CoAP.Net-1

 
 

Repository files navigation

CoAP.Net Build status Tests Status Coverage Status NuGet MyGet Pre Release license

About

This library encodes and decodes CoAP protocol packets that is transport agnostic. IT also provides a CoapClient and CoapServer for communicating over CoAP

Since CoAP is designed for unreliable transport layers. (6LoWPAN, UDP, etc...) it made sense to not worry about the transport implementaions and allow the applicatrion to provide their own.

If you're after a UDP transport example, see CoAPNet.Udp

Changelog

All relevant changes are logged in Changelog.md

Status

  • CoapClient - Simple client for communicating over the CoAP protocol

    • Send messages
      • Resolve host names using mDNS or DNS
      • Proper multicast handling
      • Retransmit confirmable (CON) messages, throwing an exception on failure
      • Await message response for confirmable (CON) messages.
      • Await message response for non-confirmable (NON) messages
    • Receive messages
      • Rejects messages when
        • Malform messages with appropiate error code.
        • Exceptions are thrown during processing
    • Correctly parse CoAP packets
  • CoapServer - Simple server for binding to local transports and processing requests

  • CoapHandler - Template request handler to be used by CoapServer

    • CoapResourceHandler - Example handler that specificaly serves CoapResources

Todo

  • Create unit tests to cover as much of RFC7252 as possible.
  • An application layer friendly API (This is a very low level library at the time of writing)

Examples

Client Example

using System;
using System.Text;
using System.Threading.Tasks;
using CoAPNet;
using CoAPNet.Udp;

namespace CoAPDevices
{
    class Program
    {
        static void Main(string[] args)
        {

            Task.Run(async () =>
            {
                // Create a new client using a UDP endpoint (defaults to 0.0.0.0 with any available port number)
                var client = new CoapClient(new CoapUdpEndPoint());

                var messageId = await client.GetAsync("coap://127.0.0.1/hello");

                var response = await client.GetResponseAsync(messageId);

                Console.WriteLine("got a response");
                Console.WriteLine(Encoding.UTF8.GetString(response.Payload));
            }).GetAwaiter().GetResult();

            Console.WriteLine("Press <Enter> to exit");
            Console.ReadLine();

        }
    }
}

Server example

using System;
using System.Net;
using System.Text;
using System.Threading;
using CoAPNet;
using CoAPNet.Options;
using CoAPNet.Udp;

namespace CoAPDevices
{
    class Program
    {
        static void Main(string[] args)
        {
            var failed = false;

            var myHandler = new CoapResourceHandler();

            myHandler.Resources.Add(new HelloResource("/hello"));

            var myServer = new CoapServer(new CoapUdpTransportFactory());

            try
            {
                myServer.BindTo(new CoapUdpEndPoint(IPAddress.Loopback, Coap.Port));
                myServer.BindTo(new CoapUdpEndPoint(IPAddress.IPv6Loopback, Coap.Port));

                myServer.StartAsync(myHandler, CancellationToken.None).GetAwaiter().GetResult();

                Console.WriteLine("Server Started!");

                Console.WriteLine("Press <Enter> to exit");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                failed = true;
                Console.WriteLine($"{ex.GetType().Name} occured: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                Console.WriteLine("Shutting Down Server");
                myServer.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            }

            if (failed)
                Console.ReadLine();
        }
    }

    public class HelloResource : CoapResource
    {
        public HelloResource(string uri) : base(uri)
        {
            Metadata.InterfaceDescription.Add("read");

            Metadata.ResourceTypes.Add("message");
            Metadata.Title = "Hello World";
        }

        public override CoapMessage Get(CoapMessage request)
        {
            return new CoapMessage
            {
                Code = CoapMessageCode.Content,
                Options = {new ContentFormat(ContentFormatType.TextPlain)},
                Payload = Encoding.UTF8.GetBytes("Hello World!")
            };
        }
    }
}

About

Constrained Applciaiton Protocol (CoAP) [RFC7252] for .Net Standard

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%