## 2 CoAP Overview
### Base CoAP  
CoAP uses the same RESTful principles as HTTP, but it is much lighter so that it can run on constrained devices [6,7]. To achieve this, CoAP has a much lower header overhead and parsing complexity than HTTP. It uses a 4-bytes base binary header that may be followed by compact binary options and payload. Figure 1 shows the CoAP message format as specified in version 18 of the draft. This version was approved by the Internet Engineering Steering Group (IESG) in July 2013 and was at the time of writing this article being edited by the RFC editor to convert the draft into an RFC. Thus, it is expected that this will be the final CoAP message format. The CoAP interaction model is similar to the client/server model of HTTP. A client can send a CoAP request, requesting an action specified by a method code (GET, PUT, POST or DELETE) on a resource (identified by a URI) on a server. The CoAP server processes the request and sends back a response containing a response code and payload. Unlike HTTP, CoAP deals with these interchanges asynchronously over a datagram-oriented transport layer such as UDP and thus also supports multicast  
Figure 1: CoAP Message Format consisting of a 4-bytes base binary header followed by optional extensions. requests. This allows CoAP to be used for point-to-multipoint interactions which are commonly required in automation. Optional reliability is supported within CoAP itself by using a simple stop-and-wait reliability mechanism upon request. Secure communication is also supported through the optional use of Datagram Transport Layer Security (DTLS) [8]. As can be seen in Figure 1 all CoAP messages start with a 4-bytes base binary header that consists of the following fields:  
* _Version (V)_: indicates the CoAP version number. Current version is 1. * _Type (T)_: indicates if this message is of type Confirmable, Non-Confirmable, Acknowledgement or Reset. * _Token Length (TKL)_: indicates the length of the variable-length Token field. * _Code_: indicates if the message carries a request (1-31), a response (64-191), or is empty (0). In case of a request, the Code field indicates the Request Method (GET, POST, PUT and DELETE); in case of a response a Response Code. * _Message ID_: is used for the detection of message duplication, and to match messages of type Acknowledgement/Reset to messages of type Confirmable/Non-confirmable. To be able to offer communication needs that cannot be satisfied by the base binary header alone, the base 4-bytes header may be followed by one or more of the following optional fields:  
* _Token_: the Token is used to correlate requests and responses. * _Options_: an Option can be followed by the end of the message, by another Option, or by the Payload Marker and the payload. * _Payload_: if present and of non-zero length, it is prefixed by a fixed, one-byte Payload Marker (0 xFF) which indicates the end of options and the start of the payload. The payload data extends from after the marker to the end of the UDP datagram, _i.e._, the Payload Length is calculated from the datagram size. The absence of the Payload Marker denotes a zero-length payload. CoAP defines a number of options which can be included in a message. Both requests and responses may include a list of one or more options. Each option instance in a message specifies the Option Number, the Option Length and the Option Value of the defined CoAP option. As an example of a simple CoAP option consider the Content-Format option. This option indicates the representation format of the message payload. This option has the Option Number 12 and its Option Length is between zero and two bytes. The Option Value itself is a numeric content format identifier that is defined in the CoAP Content Format Registry (Section 12.3 of the draft [5]). Another example is the Max-Age option which has the Option Number 14. This option indicates the maximum time a response may be cached before it is considered not fresh. The Option Value is an integer number of seconds between 0 and \(2^{32}-1\) inclusive (about 136 years).