Constrained Application Protocol

Constrained Application Protocol (CoAP) is a specialized Internet Application Protocol for constrained devices, as defined in RFC 7252. It enables those constrained devices called "nodes" to communicate with the wider Internet using similar protocols. CoAP is designed for use between devices on the same constrained network (e.g., low-power, lossy networks), between devices and general nodes on the Internet, and between devices on different constrained networks both joined by an internet. CoAP is also being used via other mechanisms, such as SMS on mobile communication networks.

CoAP is a service layer protocol that is intended for use in resource-constrained internet devices, such as wireless sensor network nodes. CoAP is designed to easily translate to HTTP for simplified integration with the web, while also meeting specialized requirements such as multicast support, very low overhead, and simplicity.[1][2] Multicast, low overhead, and simplicity are extremely important for Internet of Things (IoT) and Machine-to-Machine (M2M) devices, which tend to be deeply embedded and have much less memory and power supply than traditional internet devices have. Therefore, efficiency is very important. CoAP can run on most devices that support UDP or a UDP analogue.

The Internet Engineering Task Force (IETF) Constrained RESTful Environments Working Group (CoRE) has done the major standardization work for this protocol. In order to make the protocol suitable to IoT and M2M applications, various new functionalities have been added. The core of the protocol is specified in RFC 7252; important extensions are in various stages of the standardization process.

Features

The nodes often have 8-bit microcontrollers with small amounts of ROM and RAM, while constrained networks such as IPv6 over Low-Power Wireless Personal Area Networks (6LoWPANs) often have high packet error rates and a typical throughput of 10s of kbit/s. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.

The CoRE group has designed CoAP with the following features in mind:

  • Overhead and parsing complexity.
  • URI and content-type support.
  • Support for the discovery of resources provided by known CoAP services.
  • Simple subscription for a resource, and resulting push notifications.
  • Simple caching based on max-age.

The mapping of CoAP with HTTP is also defined, allowing proxies to be built providing access to CoAP resources via HTTP in a uniform way.[3]

With the introduction of CoAP, a complete networking stack of open-standard protocols that are suitable for constrained devices and environments becomes available.[4]

From the architecture point of view, the CoAP server will be installed on the end node, which could be a sensor. On the other hand, the CoAP client should be installed on the controller, which manages several end nodes.

Registration of the meanings behind CoAP Code, Options and Content Type is handled by IANA, shown in

Message formats

CoAP makes use of two message types, requests and responses, using a simple, binary, base header format. The base header may be followed by options in an optimized Type-Length-Value format. CoAP is by default bound to UDP and optionally to DTLS, providing a high level of communications security.

Any bytes after the headers in the packet are considered the message body—if any. The length of the message body is implied by the datagram length. When bound to UDP, the entire message MUST fit within a single datagram. When used with 6LoWPAN as defined in RFC 4944, messages SHOULD fit into a single IEEE 802.15.4 frame to minimize fragmentation.

CoAP Request/Response Code

The response code takes the form of

Byte
0 1 2 3 4 5 6 7
CLASS CODE

And in documentation is usually referred to in this format of `<class>`.`<code>`

CoAP Registered Codes

You can find all the latest CoAP registered code in: https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#codes

* Method: 0.XX

   - 0  : EMPTY
   - 1  : GET
   - 2  : POST
   - 3  : PUT
   - 4  : DELETE
   - 5  : FETCH
   - 6  : PATCH
   - 7  : iPATCH

* Success : 2.XX

   - 1  : Created
   - 2  : Deleted
   - 3  : Valid
   - 4  : Changed
   - 5  : Content
   - 31 : Continue

* Client Error : 4.XX

   - 0  : Bad Request
   - 1  : Unauthorized
   - 2  : Bad Option
   - 3  : Forbidden
   - 4  : Not Found
   - 5  : Method Not Allowed
   - 6  : Not Acceptable
   - 8  : Request Entity Incomplete
   - 9  : Conflict
   - 12 : Precondition Failed
   - 13 : Request Entity Too Large
   - 15 : Unsupported Content-Format

* Server Error : 5.XX

   - 0  : Internal Server Error
   - 1  : Not Implemented
   - 2  : Bad Gateway
   - 3  : Service Unavailable
   - 4  : Gateway Timeout
   - 5  : Proxying Not Supported

* Signaling Codes : 7.XX

   - 0  : Unassigned
   - 1  : CSM
   - 2  : Ping
   - 3  : Pong
   - 4  : Release
   - 5  : Abort

CoAP Message Structure

CoAP Message Structure
Byte Byte Byte Byte
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
VER TYPE TKL (Token Length) CoAP Request/Response Code Message ID
Token (If any in TKL bytes) (Maximum of 8 bytes)
Options (If Available) (Options Numbers: https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers)
1 1 1 1 1 1 1 1 Payload (If Available)

The CoAP fixed header is the first 4 bytes. This allows for the smallest CoAP messages to be 4 bytes in length, if omitting Token, Options and Payload.

You can easily extract the information from the fixed header in C via these macros:

#define COAP_HEADER_VERSION(data)  ( (0xC0 & data[0])>>6    )
#define COAP_HEADER_TYPE(data)     ( (0x30 & data[0])>>4    )
#define COAP_HEADER_TKL(data)      ( (0x0F & data[0])>>0    )
#define COAP_HEADER_CLASS(data)    ( ((data[1]>>5)&0x07)    )
#define COAP_HEADER_CODE(data)     ( ((data[1]>>0)&0x1F)    )
#define COAP_HEADER_MID(data)      ( (data[2]<<8)|(data[3]) )

Implementations

NameProgramming LanguageImplemented CoAP versionClient/ServerImplemented CoAP featuresLicenseLink
aiocoapPython 3RFC 7252Client + ServerBlockwise Transfers, Observe (partial)MIThttps://pypi.python.org/pypi/aiocoap
CaliforniumJavaRFC 7252Client + ServerObserve, Blockwise Transfers, DTLSEPL+EDLhttps://www.eclipse.org/californium
cantcoapC++/CRFC 7252Client + ServerBSDhttps://github.com/staropram/cantcoap
CanopusGoRFC 7252Client + ServerCoreApache License 2.0https://github.com/zubairhamed/canopus
CoAP implementation for GoGoRFC 7252Client + ServerCore + Draft SubscribeMIThttps://github.com/dustin/go-coap
CoAP.NETC#RFC 7252, coap-13, coap-08, coap-03Client + ServerCore, Observe, Blockwise Transfers3-clause BSDhttps://github.com/smeshlink/CoAP.NET
CoAPSharpC#, .NETRFC 7252Client + ServerCore, Observe, Block, RDLGPLhttp://www.coapsharp.com
CoAPthonPythonRFC 7252Client + Server + Forward Proxy + Reverse ProxyObserve, Multicast server discovery, CoRE Link Format parsing, Block-wiseMIThttps://github.com/Tanganelli/CoAPthon
CoAP ShellJavaRFC 7252ClientObserve, Blockwise Transfers, DTLSApache License 2.0https://github.com/tzolov/coap-shell
CopperJavaScript (Browser Plugin)RFC 7252ClientObserve, Blockwise Transfers3-clause BSDhttps://github.com/mkovatsc/Copper https://addons.mozilla.org/firefox/addon/copper-270430/
eCoAPCRFC 7252Client + ServerCoreMIThttps://gitlab.com/jobol/ecoap
Erbium for ContikiCRFC 7252Client + ServerObserve, Blockwise Transfers3-clause BSDhttp://www.contiki-os.org/ (er-rest-example)
iCoAPObjective-CRFC 7252ClientCore, Observe, Blockwise TransfersMIThttps://github.com/stuffrabbit/iCoAP
jCoAPJavaRFC 7252Client + ServerObserve, Blockwise TransfersApache License 2.0https://code.google.com/p/jcoap/
libcoapCRFC 7252Client + ServerObserve, Blockwise Transfers, DTLSBSD/GPLhttps://github.com/obgm/libcoap
LibNyociCRFC 7252Client + ServerCore, Observe, Block, DTLSMIThttps://github.com/darconeous/libnyoci
lobaro-coapCRFC 7252Client + ServerObserve, Blockwise TransfersMIThttp://www.lobaro.com/lobaro-coap
microcoapCRFC 7252Client + ServerMIThttps://github.com/1248/microcoap
nCoapJavaRFC 7252Client + ServerObserve, Blockwise Transfers, CoRE Link Format, Endpoint-ID-DraftBSDhttps://github.com/okleine/nCoAP
node-coapJavascriptRFC 7252Client + ServerCore, Observe, BlockMIThttps://github.com/mcollina/node-coap
Ruby coapRubyRFC 7252Client + Server (david)Core, Observe, Block, RDMIT, GPLhttps://github.com/nning/coap
https://github.com/nning/david
Sensinode C Device LibraryCRFC 7252Client + ServerCore, Observe, Block, RDCommercialhttps://silver.arm.com/browse/SEN00
Sensinode Java Device LibraryJava SERFC 7252Client + ServerCore, Observe, Block, RDCommercialhttps://silver.arm.com/browse/SEN00
Sensinode NanoService PlatformJava SERFC 7252Cloud ServerCore, Observe, Block, RDCommercialhttps://silver.arm.com/browse/SEN00
SwiftCoAPSwiftRFC 7252Client + ServerCore, Observe, Blockwise TransfersMIThttps://github.com/stuffrabbit/SwiftCoAP
TinyOS CoapBlipnesC/Ccoap-13Client + ServerObserve, Blockwise TransfersBSDhttp://docs.tinyos.net/tinywiki/index.php/CoAP
txThingsPython (Twisted)RFC 7252Client + ServerBlockwise Transfers, Observe (partial)MIThttps://github.com/mwasilak/txThings/
FreeCoAPCRFC 7252Client + Server + HTTP/CoAP ProxyCore, DTLS, Blockwise TransfersBSDhttps://github.com/keith-cullen/FreeCoAP
coap-rsRustRFC 7252Client + ServerMIThttps://github.com/Covertness/coap-rs
YaCoAP C MIT https://github.com/RIOT-Makers/YaCoAP

Proxy implementations

CoAP group communication

In many CoAP application domains it is essential to have the ability to address several CoAP resources as a group, instead of addressing each resource individually (e.g. to turn on all the CoAP-enabled lights in a room with a single CoAP request triggered by toggling the light switch). To address this need, the IETF has developed an optional extension for CoAP in the form of an experimental RFC: Group Communication for CoAP - RFC 7390[5] This extension relies on IP multicast to deliver the CoAP request to all group members. The use of multicast has certain benefits such as reducing the number of packets needed to deliver the request to the members. However, multicast also has its limitations such as poor reliability and being cache-unfriendly. An alternative method for CoAP group communication that uses unicasts instead of multicasts relies on having an intermediary where the groups are created. Clients send their group requests to the intermediary, which in turn sends individual unicast requests to the group members, collects the replies from them, and sends back an aggregated reply to the client.[6]

See also

References

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.