- Web protocol fulfilling M2M requirements
- UDP Binding
- Asynchronous message exchanges
- Low header overhead
- Deduplication and package regrouping
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) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 1: Message Format
- Version (Ver): 2-bit unsigned integer. Indicates the CoAP version number (01 binary).
- Type (T): 2-bit unsigned integer. Indicates if this message is of type CON (0), NON (1), ACK (2), or RST (3).
- Token Length (TKL): 4-bit unsigned integer. Indicates the length of the variable-length Token field (0-8 bytes). Lengths 9-15 are reserved.
- Code: 8-bit unsigned integer, split into a 3-bit class and a 5-bit detail as "c.dd" where "c" is 0 to 7 "dd" is from 00 to 31.
- Message ID: 16-bit unsigned integer in network byte order.
Option Format Each option instance in a message specifies the Option Number of the defined CoAP option, the length of the Option Value, and the Option Value itself. Instead of specifying the Option Number directly, the instances MUST appear in order of their Option Numbers and a delta encoding is used between them: the Option Number for each instance is calculated as the sum of its delta and the Option Number of the preceding instance in the message.
0 1 2 3 4 5 6 7
+---------------+---------------+
| Option Delta | Option Length | 1 byte
+---------------+---------------+
| Option Delta | 0-2 bytes
| (extended) |
+-------------------------------+
| Option Length | 0-2 bytes
| (extended) |
+-------------------------------+
| Option Value | 0 or more bytes
+-------------------------------+
Figure 2: Option Format
- Option Delta: 4-bit unsigned integer. A value between 0 and 12 indicates the Option Delta. 13, 14, 15 are reserved.
- Option Length: 4-bit unsigned integer. A value between 0 and 12 indicates the length of the Option Value, in bytes. 13, 14, 15 are reserved.
- Value: A sequence of exactly Option Length bytes (int, string).
Message Transmission As CoAP is bound to unreliable transports, messages may arrive out of order, appear duplicated, or go missing without notice. For this reason, CoAP implements a reliability mechanism that has the following features
- Duplicate detection.
- Package regroup for uploading.
- Request drop in case of invalid or corrupted format.
Messages Transmitted Reliably The reliable transmission of a message is initiated by marking the message as CON in the CoAP header. A recipient MUST either
- Acknowledge a CON message with an ACK message
- Reject the message if it can not process the message properly Rejecting a CON message is effected by sending a matching RST message. The ACK message MUST carry a response or be Empty. The RST message MUST be Empty.
Messages Transmitted without Reliability A message can be transmitted less reliably by marking the message as NON. A NON message always MUST NOT be Empty or be acknowledged by the recipient. A recipient MUST reject the message if it lacks context to process the message properly by sending a matching RST message.
+----------+-----+-----+-----+-----+
| | CON | NON | ACK | RST |
+----------+-----+-----+-----+-----+
| Request | X | X | - | - |
| Response | X | X | X | - |
| Empty | * | - | X | X |
+----------+-----+-----+-----+-----+
Table 1: Usage of Message Types
"*" means that the combination is used to elicit a RST message ("CoAP ping").
- The Content-Format Option indicates the format type of the message payload.
- The Location-Path indicates a path.
- Size1 indivates the total file size for uploading/downloading.
- FILES
ID | OPCODE | OPERATION | MSG_TYPE | ORD_NO_RULE | OPER_PARAM | METHOD |
---|---|---|---|---|---|---|
1. | 00 | UPLOAD | CONFIRMABLE | >0 OR =0 | [CONTENT] OR NED | PUT |
2. | 01 | DOWNLOAD | FTC | =0 | NED | GET |
3. | 02 | MOVE | FTC | = 0 | [NEW_FILEPATH] | POST |
4. | 03 | DELETE | FTC | = 0 | NED | DELETE |
5. | 04 | RENAME | FTC | = 0 | [NEW_NAME] | JOMAG4 |
- DIRECTORIES
ID | OPCODE | OPERATION | MSG_TYPE | ORD_NO_RULE | OPER_PARAM | METHOD |
---|---|---|---|---|---|---|
6. | 05 | CREATE | FTC | = 0 | NED | PUT |
7. | 06 | MOVE | FTC | = 0 | [NEW_PATH] | POST |
8. | 07 | DELETE | FTC | = 0 | NED | DELETE |
9. | 08 | RENAME | FTC | = 0 | [NEW_NAME] | JOMAG4 |
- FILES
ID | OPCODE | OPERATION | MSG_TYPE | ORD_NO_RULE | OPER_PARAM | METHOD |
---|---|---|---|---|---|---|
1. | 00 | UPLOAD | NON | =0 | NED | PUT |
2. | 01 | DOWNLOAD | NON | >0 OR =0 | [CONTENT] OR NED | GET |
3. | 02 | MOVE | NON | = 0 | NED | POST |
4. | 03 | DELETE | NON | = 0 | NED | DELET |
5. | 04 | RENAME | NON | = 0 | NED | JOMAG4 |
- DIRECTORIES
ID | OPCODE | OPERATION | MSG_TYPE | ORD_NO_RULE | OPER_PARAM | METHOD |
---|---|---|---|---|---|---|
6. | 05 | CREATE | NON | = 0 | NED | PUT |
7. | 06 | MOVE | NON | = 0 | NED | POST |
8. | 07 | DELETE | NON | = 0 | NED | DELETE |
9. | 08 | RENAME | NON | = 0 | NED | JOMAG4 |
-
DICTIONARY
FTC = FREE TO CHOOSE.
MSG_TYPE = MESSAGE TYPE.
ORD_NO_RULE = ORDER NUMBER RULE.
OPER_PARAM = OPERATION PARAMETERS.
NED = NO EXTRA DATA WILL BE PRESENT IN PAYLOAD.
EOT = END OF TRANSMISSION -
DESCRIPTIONS
OPCODE – PART OF PAYLOAD. SPECIFIES THE OPERATION CODE.
MSG_TYPE – THE TYPE OF MESSAGE THROUGH WHICH THE REQUEST WAS SENT.
ORD_NO (DOWNLOAD/UPLOAD USE) – PART OF PAYLOAD. IDENTIFIES IN ORDER PARTS OF THE SAME FILE (>0), OR MARKS THE EOT FOR THE FILE (=0)
ORD_NO_RULE – THE ACCEPTED FORMAT FOR ORD_NO DETERMINED BY OPERATION.
OPER_PARAM – PART OF PAYLOAD. OPERATION SPECIFIC PARAMETERS.
METHOD – THE METHOD USED TO SENT THE REQUEST. -
PACKAGE PARTs ORDER
OP_CODE – 4 BITs
ORD_NO – 2 BYTEs
OPER_PARAM CONTENT – AS MUCH AS NEED FROM WHAT IS LEFT IN PAYLOAD SPACE -
SIZEs
ORD_NO SIZE IS 2 BYTEs (2^16-1 POSSIBLE VALUES).
OP_CODE SIZE IS 4 BITs (15 POSSIBLE VALUES, 9 VALUES USED). -
SERVER RESPONSE
THE RESPONSE WILL CONTAIN THE SAME OPCODE AS THE REQUEST.
ORD_NO WILL BE EQUAL TO 0, EXCEPT FOR DOWNLOAD OPERATION WHEN IT WILL KEEP THE PRESENTED MEANING (PART IDENTIFIER OR EOT MARKER).
OPER_PARAM WILL BE NED FOR ALL OPERATIONS, EXCEPT FOR DOWNLOAD WHEN IT WILL CONTAIN THE REQUESTED CONTENT. -
OTHERS
AFTER TRANSMITTING THE MSG WITH THE LAST PART OF THE FILE, TO MARK THE EOT (FOR THAT FILE), A NEW MSG WILL BE SENT WITH THE SAME TKN, THE SAME OPCODE, BUT THE ORD_NO EQUAL TO 0.
JOMAG4 METHOD IS FOR RENAME OPERATION.
THE PRESENTED REQUEST FORMAT IS MANDATORY.
FAILURE IN APPLYING THE PAYLOAD FORMAT LEADS TO IGNORING THE REQUEST WITH RETURNING A RESPONSE CODE 4.00 - BAD REQUEST.
- The GET method retrieves a representation for the information that currently corresponds to the resource identified by the request URI. Upon success, a 2.05 (Content) or 2.03 (Valid) Response Code should be present in the response.
- According to the POST method, the representation enclosed in the request must be processed. The actual function performed by the POST method is determined by the origin server and dependent on the target resource. It usually results in a new resource being created or the target resource being updated.
- The PUT method requests that the resource identified by the request URI be updated or created with the enclosed representation. The representation format is specified by the media type and content coding given in the Content-Format Option, if provided.
- The DELETE method requests that the resource identified by the request URI be deleted.
This class of Response Code indicates that the clients request was successfully received, understood, and accepted.
2.01 Created - Used in response to PUT requests (upload and create functions).
2.02 Deleted - Used in response to requests that cause the resource to cease being available (for delete function).
2.04 Changed - Used in response to POST and JOMAG4 requests (remove and rename functions).
2.05 Content - Used in response to GET requests. The payload returned with the response is a representation of the target resource (download function).
This class of Response Code is intended for cases in which the client seems to have erred. These Response Codes are applicable to any request method.
4.00 Bad Request - The payload format was not applied as specified
4.02 Bad Option - The request could not be understood by the server due to one or more unrecognized or malformed options.
4.04 Not Found
This class of Response Code indicates cases in which the server is aware that it has erred or is incapable of performing the request. These Response Codes are applicable to any request method.
5.00 Internal Server Error
- RFC 7252: The Constrained Application Protocol (CoAP) - https://www.rfc-editor.org/rfc/rfc7252
- Python documentation - https://docs.python.org/3/