Loading...
Searching...
No Matches
coap.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
20
21#include <stdbool.h>
22
23#include "bitarithm.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
32#define COAP_PORT (5683)
33
37#define COAPS_PORT (5684)
38
39#define COAP_V1 (1)
40
45#define COAP_OPT_IF_MATCH (1)
46#define COAP_OPT_URI_HOST (3)
47#define COAP_OPT_ETAG (4)
48#define COAP_OPT_IF_NONE_MATCH (5)
49#define COAP_OPT_OBSERVE (6)
50#define COAP_OPT_LOCATION_PATH (8)
59#define COAP_OPT_OSCORE (9)
60#define COAP_OPT_URI_PATH (11)
61#define COAP_OPT_CONTENT_FORMAT (12)
62#define COAP_OPT_MAX_AGE (14)
63#define COAP_OPT_URI_QUERY (15)
71#define COAP_OPT_HOP_LIMIT (16)
72#define COAP_OPT_ACCEPT (17)
80#define COAP_OPT_Q_BLOCK1 (19)
81#define COAP_OPT_LOCATION_QUERY (20)
90#define COAP_OPT_EDHOC (21)
91#define COAP_OPT_BLOCK2 (23)
92#define COAP_OPT_BLOCK1 (27)
103#define COAP_OPT_SIZE2 (28)
111#define COAP_OPT_Q_BLOCK2 (31)
112#define COAP_OPT_PROXY_URI (35)
113#define COAP_OPT_PROXY_SCHEME (39)
126#define COAP_OPT_SIZE1 (60)
135#define COAP_OPT_ECHO (252)
140#define COAP_OPT_NO_RESPONSE (258)
149#define COAP_OPT_REQUEST_TAG (292)
151
156#define COAP_TYPE_CON (0)
157#define COAP_TYPE_NON (1)
158#define COAP_TYPE_ACK (2)
159#define COAP_TYPE_RST (3)
161
166#define COAP_CLASS_REQ (0)
167
180
181
186#define COAP_CODE_EMPTY (0)
188
193#define COAP_CLASS_SUCCESS (2)
195#define COAP_CODE_CREATED ((2 << 5) | 1)
196#define COAP_CODE_DELETED ((2 << 5) | 2)
197#define COAP_CODE_VALID ((2 << 5) | 3)
198#define COAP_CODE_CHANGED ((2 << 5) | 4)
199#define COAP_CODE_204 ((2 << 5) | 4)
201#define COAP_CODE_CONTENT ((2 << 5) | 5)
202#define COAP_CODE_205 ((2 << 5) | 5)
204#define COAP_CODE_CONTINUE ((2 << 5) | 31)
205#define COAP_CODE_231 ((2 << 5) | 31)
208
213#define COAP_CLASS_CLIENT_FAILURE (4)
215#define COAP_CODE_BAD_REQUEST ((4 << 5) | 0)
216#define COAP_CODE_UNAUTHORIZED ((4 << 5) | 1)
217#define COAP_CODE_BAD_OPTION ((4 << 5) | 2)
218#define COAP_CODE_FORBIDDEN ((4 << 5) | 3)
219#define COAP_CODE_PATH_NOT_FOUND ((4 << 5) | 4)
220#define COAP_CODE_404 ((4 << 5) | 4)
222#define COAP_CODE_METHOD_NOT_ALLOWED ((4 << 5) | 5)
223#define COAP_CODE_NOT_ACCEPTABLE ((4 << 5) | 6)
224#define COAP_CODE_REQUEST_ENTITY_INCOMPLETE ((4 << 5) | 8)
225#define COAP_CODE_CONFLICT ((4 << 5) | 9)
226#define COAP_CODE_PRECONDITION_FAILED ((4 << 5) | 12)
227#define COAP_CODE_REQUEST_ENTITY_TOO_LARGE ((4 << 5) | 13)
228#define COAP_CODE_UNSUPPORTED_CONTENT_FORMAT ((4 << 5) | 15)
229#define COAP_CODE_UNPROCESSABLE_ENTITY ((4 << 5) | 22)
230#define COAP_CODE_TOO_MANY_REQUESTS ((4 << 5) | 29)
232
237#define COAP_CLASS_SERVER_FAILURE (5)
239#define COAP_CODE_INTERNAL_SERVER_ERROR ((5 << 5) | 0)
240#define COAP_CODE_NOT_IMPLEMENTED ((5 << 5) | 1)
241#define COAP_CODE_BAD_GATEWAY ((5 << 5) | 2)
242#define COAP_CODE_SERVICE_UNAVAILABLE ((5 << 5) | 3)
243#define COAP_CODE_GATEWAY_TIMEOUT ((5 << 5) | 4)
244#define COAP_CODE_PROXYING_NOT_SUPPORTED ((5 << 5) | 5)
246
256#define COAP_FORMAT_TEXT (0)
261#define COAP_FORMAT_COSE_ENCRYPT0 (16)
266#define COAP_FORMAT_COSE_MAC0 (17)
271#define COAP_FORMAT_COSE_SIGN1 (18)
276#define COAP_FORMAT_ACE_CBOR (19)
281#define COAP_FORMAT_IMAGE_GIF (21)
286#define COAP_FORMAT_IMAGE_JPEG (22)
291#define COAP_FORMAT_IMAGE_PNG (23)
292#define COAP_FORMAT_LINK (40)
293#define COAP_FORMAT_XML (41)
294#define COAP_FORMAT_OCTET (42)
295#define COAP_FORMAT_EXI (47)
296#define COAP_FORMAT_JSON (50)
297#define COAP_FORMAT_JSON_PATCH_JSON (51)
298#define COAP_FORMAT_MERGE_PATCH_JSON (52)
299#define COAP_FORMAT_CBOR (60)
304#define COAP_FORMAT_CWT (61)
309#define COAP_FORMAT_MULTIPART_CORE (62)
314#define COAP_FORMAT_CBOR_SEQ (63)
319#define COAP_FORMAT_COSE_ENCRYPT (96)
324#define COAP_FORMAT_COSE_MAC (97)
329#define COAP_FORMAT_COSE_SIGN (98)
334#define COAP_FORMAT_COSE_KEY (101)
339#define COAP_FORMAT_COSE_KEY_SET (102)
340#define COAP_FORMAT_SENML_JSON (110)
341#define COAP_FORMAT_SENSML_JSON (111)
342#define COAP_FORMAT_SENML_CBOR (112)
343#define COAP_FORMAT_SENSML_CBOR (113)
344#define COAP_FORMAT_SENML_EXI (114)
345#define COAP_FORMAT_SENSML_EXI (115)
350#define COAP_FORMAT_YANG_DATA_CBOR_SID (140)
355#define COAP_FORMAT_COAP_GROUP_JSON (256)
360#define COAP_FORMAT_PROBLEM_DETAILS_CBOR (257)
365#define COAP_FORMAT_SWID_CBOR (258)
371#define COAP_FORMAT_PKIXCMP (259)
376#define COAP_FORMAT_DOTS_CBOR (271)
381#define COAP_FORMAT_MISSING_BLOCKS_CBOR_SEQ (272)
388#define COAP_FORMAT_PKCS7_MIME_SERVER_GEN (280)
394#define COAP_FORMAT_PKCS7_MIME_CERTS_ONLY (281)
401#define COAP_FORMAT_PKCS8 (284)
407#define COAP_FORMAT_CSRATTRS (285)
414#define COAP_FORMAT_PKCS10 (286)
420#define COAP_FORMAT_PKIX_CERT (287)
425#define COAP_FORMAT_AIF_CBOR (290)
430#define COAP_FORMAT_AIF_JSON (291)
431#define COAP_FORMAT_SENML_XML (310)
432#define COAP_FORMAT_SENSML_XML (311)
437#define COAP_FORMAT_SNML_ETCH_JSON (320)
442#define COAP_FORMAT_SNML_ETCH_CBOR (322)
447#define COAP_FORMAT_YAML_DATA_CBOR (340)
452#define COAP_FORMAT_YAML_DATA_CBOR_ID_NAME (341)
457#define COAP_FORMAT_TD_JSON (432)
462#define COAP_FORMAT_TM_JSON (433)
467#define COAP_FORMAT_DNS_MESSAGE (553)
473#define COAP_FORMAT_VOUCER_COSE_CBOR (836)
477#define COAP_FORMAT_VND_OCF_CBOR (10000)
482#define COAP_FORMAT_OSCORE (10001)
487#define COAP_FORMAT_JAVASCRIPT (10002)
493#define COAP_FORMAT_JSON_DEFLATE (11050)
499#define COAP_FORMAT_CBOR_DEFLATE (11060)
504#define COAP_FORMAT_VND_OMA_LWM2M_TLV (11542)
509#define COAP_FORMAT_VND_OMA_LWM2M_JSON (11543)
514#define COAP_FORMAT_VND_OMA_LWM2M_CBOR (11544)
519#define COAP_FORMAT_TEXT_CSS (20000)
524#define COAP_FORMAT_IMAGE_SVG_XML (30000)
526
531#define COAP_OBS_REGISTER (0)
532#define COAP_OBS_DEREGISTER (1)
533#define COAP_OBS_MAX_VALUE_MASK (0xffffff)
535
540#define COAP_TOKEN_LENGTH_MAX (8)
542
547#define COAP_ETAG_LENGTH_MAX (8U)
549
553#define COAP_PAYLOAD_MARKER (0xFF)
554#define COAP_PAYLOAD_MARKER_SIZE (1U)
555
578#ifndef CONFIG_COAP_ACK_TIMEOUT_MS
579#define CONFIG_COAP_ACK_TIMEOUT_MS (2000)
580#endif
581
591#ifndef CONFIG_COAP_RANDOM_FACTOR_1000
592#define CONFIG_COAP_RANDOM_FACTOR_1000 (1500)
593#endif
594
599#ifndef CONFIG_COAP_SEPARATE_RESPONSE_TIMEOUT_MS
600#define CONFIG_COAP_SEPARATE_RESPONSE_TIMEOUT_MS (10 * MS_PER_SEC)
601#endif
602
604#ifndef CONFIG_COAP_MAX_RETRANSMIT
605#define CONFIG_COAP_MAX_RETRANSMIT (4)
606#endif
609
614#define COAP_NSTART (1)
615#define COAP_DEFAULT_LEISURE (5)
617
622#define COAP_BLOCKWISE_NUM_OFF (4)
623#define COAP_BLOCKWISE_NUM_MAX (0xfffffU)
624#define COAP_BLOCKWISE_MORE_OFF (3)
625#define COAP_BLOCKWISE_SZX_MASK (0x07)
626#define COAP_BLOCKWISE_SZX_MAX (7)
628
632typedef enum {
633 COAP_BLOCKSIZE_16 = 0,
634 COAP_BLOCKSIZE_32,
635 COAP_BLOCKSIZE_64,
636 COAP_BLOCKSIZE_128,
637 COAP_BLOCKSIZE_256,
638 COAP_BLOCKSIZE_512,
639 COAP_BLOCKSIZE_1024,
641
656#define COAP_SZX2SIZE(szx) \
657 (1U << (4U + (szx)))
658
672static inline unsigned coap_szx2size(coap_blksize_t szx)
673{
674 if ((unsigned)szx > (unsigned)COAP_BLOCKSIZE_1024) {
675 return 0;
676 }
677
678 return COAP_SZX2SIZE(szx);
679}
680
693static inline coap_blksize_t coap_size2szx(unsigned size)
694{
695 if (size < 32) {
696 return COAP_BLOCKSIZE_16;
697 }
698
699 if (size >= 1024) {
700 return COAP_BLOCKSIZE_1024;
701 }
702
703 return bitarithm_msb(size >> 4);
704}
705
706#ifdef __cplusplus
707}
708#endif
709
Helper functions for bit arithmetic.
static unsigned bitarithm_msb(unsigned v)
Returns the number of the highest '1' bit in a value.
Definition bitarithm.h:149
coap_blksize_t
CoAP block-wise-transfer size SZX.
Definition coap.h:632
static coap_blksize_t coap_size2szx(unsigned size)
Get the closest blocksize enum for given size.
Definition coap.h:693
static unsigned coap_szx2size(coap_blksize_t szx)
Convert a coap_blksize_t constant to a size in bytes.
Definition coap.h:672
#define COAP_SZX2SIZE(szx)
Convert a coap_blksize_t constant to a size in bytes without error checking.
Definition coap.h:656
coap_method_t
CoAP method codes used in request.
Definition coap.h:171
@ COAP_METHOD_GET
GET request (no paylod)
Definition coap.h:172
@ COAP_METHOD_IPATCH
iPATCH request (RFC 8132)
Definition coap.h:178
@ COAP_METHOD_PUT
PUT request (update resource with payload)
Definition coap.h:174
@ COAP_METHOD_POST
POST request (resource processes payload)
Definition coap.h:173
@ COAP_METHOD_PATCH
PATCH request (RFC 8132)
Definition coap.h:177
@ COAP_METHOD_DELETE
DELETE request (no payload, remove resource)
Definition coap.h:175
@ COAP_METHOD_FETCH
FETCH request (RFC 8132)
Definition coap.h:176