SatCat5
coap_constants.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
12 
13 #pragma once
14 
15 #include <satcat5/types.h>
16 
17 namespace satcat5 {
18  namespace coap {
20  constexpr u8
21  PAYLOAD_MARKER = 255, // Payload marker
22  TYPE_CON = 0x00, // Confirmable
23  TYPE_NON = 0x10, // Non-confirmable
24  TYPE_ACK = 0x20, // Acknowledgement
25  TYPE_RST = 0x30, // Reset
26  VERSION1 = (1 << 6); // Version 1 (RFC7252)
27 
29  struct Code {
30  u8 value;
31 
32  // Constructors.
33  explicit constexpr Code(u8 val)
34  : value(val) {}
35  constexpr Code(u8 x, u8 yy)
36  : value((x << 5) | yy) {}
37  constexpr Code(const Code& other)
38  : value(other.value) {}
39  Code& operator=(const Code& other)
40  { value = other.value; return *this; }
41 
42  // Basic accessors.
43  constexpr u8 category() const
44  { return (value >> 5) & 0x7; }
45  constexpr u8 subtype() const
46  { return (value & 0x1F); }
47 
48  // Comparison operators.
49  bool operator==(const Code& other) const
50  { return (value == other.value); }
51  bool operator!=(const Code& other) const
52  { return (value != other.value); }
53 
61  constexpr bool is_empty() const
62  { return (value == 0); }
63  constexpr bool is_request() const
64  { return (category() == 0); }
65  constexpr bool is_success() const
66  { return (category() == 2); }
67  constexpr bool is_error() const
68  { return (category() == 4) || (category() == 5); }
69  constexpr bool is_response() const
70  { return is_empty() || is_success() || is_error(); }
72  };
73 
74  constexpr Code // Request codes (Section 12.1.1)
75  CODE_EMPTY (0,0),
76  CODE_GET (0,1),
77  CODE_POST (0,2),
78  CODE_PUT (0,3),
79  CODE_DELETE (0,4);
80  constexpr Code // Response codes (Section 12.1.2)
81  CODE_CREATED (2,1),
82  CODE_DELETED (2,2),
83  CODE_VALID (2,3),
84  CODE_CHANGED (2,4),
85  CODE_CONTENT (2,5),
86  CODE_CONTINUE (2,31),
95  CODE_TOO_LARGE (4,13),
96  CODE_BAD_FORMAT (4,15),
98  CODE_NOT_IMPL (5,1),
103 
104  // Option numbers (Section 12.2)
105  constexpr u16
123 
124  // Content-format codes (Section 12.3)
125  // (These are used with OPTION_FORMAT and OPTION_ACCEPT.)
126  constexpr u16 // Equivalent MIME specifier:
128  FORMAT_LINK = 40,
129  FORMAT_XML = 41,
131  FORMAT_EXI = 47,
132  FORMAT_JSON = 50,
133  FORMAT_CBOR = 60,
135  }
136 }
constexpr u16 OPTION_PROXY_URI
Option: Proxy-Uri.
constexpr Code CODE_BAD_FORMAT(4, 15)
< 4.13 Request Entity Too Large
constexpr u16 FORMAT_EXI
application/exi
constexpr Code CODE_BAD_REQUEST(4, 0)
< 2.31 Continue
constexpr u16 FORMAT_LINK
application/link-format
constexpr u16 OPTION_URI_QUERY
Option: Uri-Query.
constexpr u16 OPTION_LOC_PATH
Option: Location-Path.
constexpr Code CODE_BAD_GATEWAY(5, 2)
< 5.01 Not Implemented
constexpr Code CODE_CHANGED(2, 4)
< 2.03 Valid
constexpr u16 FORMAT_TEXT
text/plain;charset=utf-8
constexpr Code CODE_UNAVAILABLE(5, 3)
< 5.02 Bad Gateway
constexpr u16 OPTION_LOC_QUERY
Option: Location-Query.
constexpr u16 OPTION_URI_PORT
Option: Uri-Port.
constexpr Code CODE_DELETED(2, 2)
< 2.01 Created
constexpr Code CODE_PUT(0, 3)
< 0.02 POST request
constexpr Code CODE_FORBIDDEN(4, 3)
< 4.02 Bad Option
constexpr u16 OPTION_MAX_AGE
Option: Max-Age.
constexpr Code CODE_NO_PROXY(5, 5)
< 5.04 Gateway Timeout
constexpr Code CODE_POST(0, 2)
< 0.01 GET request
constexpr Code CODE_DELETE(0, 4)
< 0.03 PUT request
constexpr Code CODE_BAD_OPTION(4, 2)
< 4.01 Unauthorized
constexpr u8 PAYLOAD_MARKER
CoAP message header VERSION and TYPE fields (Section 3).
constexpr Code CODE_VALID(2, 3)
< 2.02 Deleted
constexpr u16 FORMAT_XML
application/xml
constexpr u16 OPTION_IF_NONE
Option: If-None-Match.
constexpr u16 OPTION_ETAG
Option: ETag.
constexpr Code CODE_PRECND_FAIL(4, 12)
< 4.06 Not Acceptable
constexpr u16 OPTION_IF_MATCH
Option: If-Match.
constexpr Code CODE_BAD_METHOD(4, 5)
< 4.04 Not Found
constexpr u16 OPTION_PROXY_SCH
Option: Proxy-Scheme.
constexpr Code CODE_NOT_FOUND(4, 4)
< 4.03 Forbidden
constexpr Code CODE_GET(0, 1)
< 0.00 Empty message
constexpr u16 OPTION_BLOCK2
Option: Block transfer (RFC7959)
constexpr u16 OPTION_ACCEPT
Option: Accept.
constexpr u16 FORMAT_CBOR
application/cbor (RFC8949)
constexpr u16 FORMAT_JSON
application/json
constexpr Code CODE_GATE_TIMEOUT(5, 4)
< 5.03 Service Unavailable
constexpr u16 FORMAT_BYTES
application/octet-stream
constexpr u16 FORMAT_CBOR_SEQ
application/cbor-seq (RFC8742)
constexpr Code CODE_UNAUTHORIZED(4, 1)
< 4.00 Bad Request
constexpr Code CODE_NOT_ACCEPT(4, 6)
< 4.05 Method Not Allowed
constexpr u16 OPTION_URI_HOST
Option: Uri-Host.
constexpr u16 OPTION_SIZE1
Option: Size1.
constexpr Code CODE_CONTINUE(2, 31)
< 2.05 Content
constexpr Code CODE_SERVER_ERROR(5, 0)
< 4.15 Unsupported Content-Format
constexpr u16 OPTION_BLOCK1
Option: Block transfer (RFC7959)
constexpr u16 OPTION_URI_PATH
Option: Uri-Path.
constexpr u16 OPTION_FORMAT
Option: Content-Format.
constexpr Code CODE_NOT_IMPL(5, 1)
< 5.00 Internal Server Error
constexpr Code CODE_CONTENT(2, 5)
< 2.04 Changed
constexpr Code CODE_TOO_LARGE(4, 13)
< 4.12 Precondition Failed
CoAP message header CODE field (Section 12.1).
constexpr Code(u8 x, u8 yy)
Create x.yy from x and yy.
constexpr u8 category() const
Return the "x" from x.yy.
constexpr bool is_request() const
Category tests: 0.00 = Empty (may be request or response) 0.01-0.31 = Request 2.00-2....
Code & operator=(const Code &other)
Assignment operator.
constexpr u8 subtype() const
Return the "yy" from x.yy.
constexpr Code(const Code &other)
Copy constructor.
constexpr Code(u8 val)
Create x.yy from raw byte.
u8 value
Raw value.
constexpr bool is_empty() const
Category tests: 0.00 = Empty (may be request or response) 0.01-0.31 = Request 2.00-2....
constexpr bool is_error() const
Category tests: 0.00 = Empty (may be request or response) 0.01-0.31 = Request 2.00-2....
constexpr bool is_success() const
Category tests: 0.00 = Empty (may be request or response) 0.01-0.31 = Request 2.00-2....
constexpr bool is_response() const
Category tests: 0.00 = Empty (may be request or response) 0.01-0.31 = Request 2.00-2....
Basic type aliases and prototypes used throughout SatCat5.