A preliminary exploration of CoAP protocol (1)

CoAP is a network transmission protocol based on the REST model. Mainly used for lightweight M2M communication. Since many devices in the Internet of Things are resource-constrained, that is, only a small amount of memory space and limited computing power, the traditional HTTP protocol application in the Internet of Things is too large to be applicable, and CoAP came into being.

This article introduces the CoAP protocol specification supported by the IoT platform;

1. Concept

Support RFC 7252 Constrained Application Protocol. For more information, please refer to RFC 7252

2. Agreement content

CoAP is a complete binary application layer protocol with compact message format and runs on UDP by default.

A CoAP resource can be described by a URI. For example, a device can measure temperature, then the URI of this temperature sensor is described as: CoAP://machine.address:5683/sensors/temperature. Please note that the default UDP port number of CoAP is 5683 .

3.CoAP header

  • [Ver] Version number.

  • [T] Message type. The CoAP protocol defines 4 different types of messages, CON message, NON message, ACK message and RST message.

  • [TKL] CoAP identifier length. There are two identifiers with similar functions in the CoAP protocol, one is Message ID (message number) and the other is Token (identifier). Each message contains the message number, but the identifier is not necessary for the message.

  • [Code] Function code/response code. Code has different manifestations in CoAP request message and response message. Code occupies one byte. It is divided into two parts, the first 3 digits and the last 5 digits. For the convenience of description, it is written as c.dd structure. Among them, 0.XX represents a certain method of CoAP request, and 2.XX, 4.XX or 5.XX represents a specific manifestation of CoAP response.

  • [Message ID] Message number.

  • [Token] The specific content of the identifier, the length of the Token is specified through TKL.

  • [Option] Message options, CoAP host, CoAP URI, CoAP request parameters and load media type can be set through message options.

  • [1111 1111B] Separator between CoAP message and specific load.

4. Method


5. Data Type

  • [Text/plain] The number is 0, indicating that the payload is in the form of a string, and the default is UTF8 encoding.

  • [Application/link-format] The number is 40, and the definition is added in the CoAP resource discovery protocol. This media type is unique to the CoAP protocol.

  • [Application/xml] The number is 41, indicating that the payload type is XML format.

  • [Application/octet-stream] The number is 42, indicating that the payload type is binary format.

  • [Application/exi] The number is 47, which means that the payload type is in "reduced XML" format.

  • [Applicaiton/cbor] The number is 50, which can be understood as a binary JSON format.

6. Message type

  • CON : A request that needs to be confirmed. If a CON request is sent, the other party must respond.

  • NON : A request that does not need to be confirmed. If a NON request is sent, the other party does not need to respond.

  • ACK : Acknowledgement message, a response to the CON message received.

  • RST : Reset message. When the message received by the receiver contains an error, the receiver parses the message or no longer cares about the content sent by the sender, then the reset message will be sent.

7. Channel security

Use DTLS v1.2 to ensure channel security. For more information, see DTLS v1.2 .



Guess you like

Origin blog.csdn.net/Swallow_he/article/details/112362621