Constrained Application Protocol
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol, as defined in RFC 7252, for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.
Features
The work on Constrained Environments aims at realizing the REST architecture in a suitable form for the most constrained nodes and networks. The nodes usually consist of 8-bit microcontrollers with limited amounts of RAM and ROM.
CoAP has the following main features:
- Web protocol fulfilling M2M requirements in constrained environments
- UDP [RFC0768] binding with optional reliability supporting unicast and multicast requests
- Asynchronous message exchanges
- Low header overhead and parsing complexity
- URI and Content-type support
- Simple proxy and caching capabilities
- Stateless HTTP mapping
- Security binding to Datagram Transport Layer Security (DTLS)
The Protocol
CoAP is similar to the client/server model of HTTP. M2M interactions typically result in a CoAP implementation acting in both client and server roles. A request is sent by a client to request an action on a resource (identified by a URI) on a server. The server then sends a response with a response code (equivalent to that of HTTP). Therefore, efficiency is very important, so CoAP uses UDP, a datagram-oriented transport.
CoAP is however a single protocol, with messaging and request/response as just features of the CoAP header
+----------------------+ | Application | +----------------------+ +----------------------+ \ | Requests/Responses | | |----------------------| | CoAP | Messages | | +----------------------+ / +----------------------+ | UDP | +----------------------+
Message Format
By default, the messages are encoded in a simple binary format and transported over UDP. The message format starts with a fixed-size 4-byte header, followed by a variable-length Token value, which can be between 0 and 8 bytes long. Following the Token value comes a sequence of zero or more CoAP Options in Type-Length-Value (TLV) format, optionally followed by a payload that takes up the rest of the datagram.
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Ver| T | TKL | Code | Message ID | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Token (if any, TKL bytes) ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options (if any) ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1 1 1 1 1 1 1 1| Payload (if any) ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Version (Ver) (2 bits)
- Indicates the CoAP Version number. This must set this field to 1 (binary 01). Other values are reserved for future versions.
- Type (T) (2 bits)
- Indicates if this message is of type Confirmable (0), Non-confirmable (1), Acknowledgement (2), or Reset (3).
- Token Length (TKL) (4 bits)
- Indicates the length of the variable-length Token field, which may be 0-8 bytes in length.
- CoAP Request/Response Code (8 bits)
- Splits into a 3-bit class and 5-bit detail.
- Message ID (16 bits)
- Used to detect message duplication and to match messages of type Acknowledgement/Reset to messages of type Confirmable/Non-confirmable.
Implementations
Generic implementations are becoming availible for a variety of platforms.
Constrained Devices
Name | Programming Language | Implemented CoAP version | Client/Server | License | Link | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Erbium for Contiki | C | RFC 7252 | Client + Server | 3-clause BSD | http://www.contiki-os.org/ | ||||||
libcoap | C | RFC 7252 | Client + Server | BSD/GPL | https://libcoap.net/ | ||||||
Eclipse tinydtls | C | Client + Server | EPL+EDL | https://projects.eclipse.org/projects/iot.tinydtls | |||||||
LibNyoci | C | RFC 7252 | Client + Server | MIT | https://github.com/darconeous/libnyoci | ||||||
microcoap | C | RFC 7252 | Client + Server | MIT | https://github.com/1248/microcoap | ||||||
cantcoap | C++/C | RFC 7252 | Client + Server | BSD | https://github.com/staropram/cantcoap | ||||||
Lobaro CoAP | C | RFC 7252 | Client + Server | MIT | https://www.lobaro.com/portfolio/lobaro-coap/ | ||||||
MR-CoAP | Java | RFC 7252 | BSD | https://github.com/MR-CoAP/CoAP
|
aiocoap | Python 3 | RFC 7252 | Client + Server | MIT | https://pypi.python.org/pypi/aiocoap | |
Californium | Java | RFC 7252 | Client + Server | Observe, Blockwise Transfers, DTLS | EPL+EDL | https://www.eclipse.org/californium | |||||
cantcoap | C++/C | RFC 7252 | Client + Server | BSD | https://github.com/staropram/cantcoap | ||||||
Canopus | Go | RFC 7252 | Client + Server | Core | Apache License 2.0 | https://github.com/zubairhamed/canopus | |||||
CoAP implementation for Go | Go | RFC 7252 | Client + Server | Core + Draft Subscribe | MIT | https://github.com/dustin/go-coap | |||||
CoAP.NET | C# | RFC 7252, coap-13, coap-08, coap-03 | Client + Server | Core, Observe, Blockwise Transfers | 3-clause BSD | https://github.com/smeshlink/CoAP.NET | |||||
CoAPSharp | C#, .NET | RFC 7252 | Client + Server | Core, Observe, Block, RD | LGPL | http://www.coapsharp.com | |||||
CoAPthon | Python | RFC 7252 | Client + Server + Forward Proxy + Reverse Proxy | Observe, Multicast server discovery, CoRE Link Format parsing, Block-wise | MIT | https://github.com/Tanganelli/CoAPthon | |||||
CoAP Shell | Java | RFC 7252 | Client | Observe, Blockwise Transfers, DTLS | Apache License 2.0 | https://github.com/tzolov/coap-shell | |||||
Copper | JavaScript (Browser Plugin) | RFC 7252 | Client | Observe, Blockwise Transfers | 3-clause BSD | https://github.com/mkovatsc/Copper https://addons.mozilla.org/firefox/addon/copper-270430/ | |||||
eCoAP | C | RFC 7252 | Client + Server | Core | MIT | https://gitlab.com/jobol/ecoap | |||||
Erbium for Contiki | C | RFC 7252 | Client + Server | Observe, Blockwise Transfers | 3-clause BSD | http://www.contiki-os.org/ (er-rest-example) | |||||
iCoAP | Objective-C | RFC 7252 | Client | Core, Observe, Blockwise Transfers | MIT | https://github.com/stuffrabbit/iCoAP | |||||
jCoAP | Java | RFC 7252 | Client + Server | Observe, Blockwise Transfers | Apache License 2.0 | https://code.google.com/p/jcoap/ | |||||
lobaro-coap | C | RFC 7252 | Client + Server | Observe, Blockwise Transfers | MIT | http://www.lobaro.com/lobaro-coap | |||||
nCoap | Java | RFC 7252 | Client + Server | Observe, Blockwise Transfers, CoRE Link Format, Endpoint-ID-Draft | BSD | https://github.com/okleine/nCoAP | |||||
node-coap | Javascript | RFC 7252 | Client + Server | Core, Observe, Block | MIT | https://github.com/mcollina/node-coap | |||||
Ruby coap | Ruby | RFC 7252 | Client + Server (david) | Core, Observe, Block, RD | MIT, GPL | https://github.com/nning/coap https://github.com/nning/david | |||||
Sensinode C Device Library | C | RFC 7252 | Client + Server | Core, Observe, Block, RD | Commercial | https://silver.arm.com/browse/SEN00 | |||||
Sensinode Java Device Library | Java SE | RFC 7252 | Client + Server | Core, Observe, Block, RD | Commercial | https://silver.arm.com/browse/SEN00 | |||||
Sensinode NanoService Platform | Java SE | RFC 7252 | Cloud Server | Core, Observe, Block, RD | Commercial | https://silver.arm.com/browse/SEN00 | |||||
SwiftCoAP | Swift | RFC 7252 | Client + Server | Core, Observe, Blockwise Transfers | MIT | https://github.com/stuffrabbit/SwiftCoAP | |||||
TinyOS CoapBlip | nesC/C | coap-13 | Client + Server | Observe, Blockwise Transfers | BSD | http://docs.tinyos.net/tinywiki/index.php/CoAP | |||||
txThings | Python (Twisted) | RFC 7252 | Client + Server | Blockwise Transfers, Observe (partial) | MIT | https://github.com/mwasilak/txThings/ | |||||
FreeCoAP | C | RFC 7252 | Client + Server + HTTP/CoAP Proxy | Core, DTLS, Blockwise Transfers | BSD | https://github.com/keith-cullen/FreeCoAP | |||||
coap-rs | Rust | RFC 7252 | Client + Server | MIT | https://github.com/Covertness/coap-rs | ||||||
YaCoAP | C | MIT | https://github.com/RIOT-Makers/YaCoAP |
Courses
References
- RFC 7252: https://tools.ietf.org/html/rfc7252
- CoAP: RFC 7252 Constrained Application Protocol https://coap.technology/
- Learning Internet of Things: https://www.oreilly.com/library/view/learning-internet-of/9781783553532/