Expand description
CoAP (RFC 7252) message codec.
Hand-rolled on purpose: the server’s encoder and the coap / coap-lite crates used
as the test peer are then genuinely independent implementations, so a round trip
through them is evidence rather than a tautology.
Covers the 4-byte binary header, the four message types, tokens, the option
delta/length encoding with both extension forms, the payload marker, and the
request/response code space, and the option values Observe (RFC 7641) and Block-wise
transfer (RFC 7959) carry: BlockValue and the Observe sequence. What those options
mean — registrations, reassembly, the transfer cache — is observe.rs and
blockwise.rs; this file only reads and writes them. Not covered: DTLS.
Structs§
- Block
Value - A Block1 or Block2 option value:
NUM << 4 | M << 3 | SZX(RFC 7959 §2.2). - Coap
Message - A decoded CoAP message.
- Message
Prefix - The part of a message that lives at fixed offsets: the header and the token.
Enums§
- Decode
Error - Why a datagram is not a CoAP message.
- Encode
Error - Why a message cannot be put on the wire.
- Message
Type - CoAP message type (the
Tfield).
Constants§
- CODE_
BAD_ OPTION - CODE_
BAD_ REQUEST - CODE_
CONTENT - CODE_
CONTINUE - CODE_
DELETE - CODE_
EMPTY - The empty code, 0.00 — used by ACK and RST, and by a CoAP ping.
- CODE_
GET - CODE_
INTERNAL_ SERVER_ ERROR - CODE_
METHOD_ NOT_ ALLOWED - CODE_
NOT_ FOUND - CODE_
POST - CODE_
PUT - CODE_
REQUEST_ ENTITY_ INCOMPLETE - CODE_
REQUEST_ ENTITY_ TOO_ LARGE - CODE_
SERVICE_ UNAVAILABLE - HEADER_
LEN - Fixed CoAP header length: Ver/T/TKL, Code, Message ID.
- MAX_
BLOCK_ NUM - Largest block number a three-byte option value can carry.
- MAX_
MESSAGE_ LEN - Largest inbound datagram this server will process, in bytes.
- MAX_
OPTION_ LEN - Longest value a single option may carry, in octets.
- MAX_
PAYLOAD_ LEN - Largest payload this server will put in one message, in bytes — the largest Block2 block (SZX 6).
- MAX_
REPRESENTATION_ LEN - Largest representation the model may return, in bytes — and the largest request body a Block1 upload may reassemble to.
- MAX_SZX
- The largest size exponent: SZX 6 is 1024 bytes. 7 is reserved (BERT, CoAP over TCP only).
- MAX_
TOKEN_ LEN - Longest token a message may carry, in octets (RFC 7252 §3).
- OPT_
ACCEPT - OPT_
BLOC K1 - Block1 (RFC 7959 §2.1): which block of the request body this is.
- OPT_
BLOC K2 - Block2 (RFC 7959 §2.1): which block of the response this is, or which one is wanted.
- OPT_
CONTENT_ FORMAT - OPT_
ETAG - OPT_
IF_ MATCH - OPT_
IF_ NONE_ MATCH - OPT_
LOCATION_ PATH - OPT_
LOCATION_ QUERY - OPT_
MAX_ AGE - OPT_
OBSERVE - Observe (RFC 7641 §2): 0 registers, 1 deregisters, and in a notification it is the sequence number.
- OPT_
SIZE1 - Size1 (RFC 7252 §5.10.9, RFC 7959 §4): the whole request body’s length, or in a 4.13 the largest one the server will take.
- OPT_
SIZE2 - Size2 (RFC 7959 §4): the whole representation’s length.
- OPT_
URI_ HOST - OPT_
URI_ PATH - OPT_
URI_ PORT - OPT_
URI_ QUERY - PAYLOAD_
MARKER - Marks the start of the payload, after any options.
- VERSION
- CoAP version implemented here.
Functions§
- code
- Build a code byte from the human
class.detailpair, e.g.2.05->code(2, 5). - code_
class - Class portion of a code byte (the digit before the dot).
- code_
detail - Detail portion of a code byte (the two digits after the dot).
- code_
to_ string - Render a code byte in the conventional
c.ddnotation. - content_
format_ id - Content-Format identifier for a media type name.
- content_
format_ name - Media type name for a Content-Format identifier.
- message_
prefix - Read the header and token of a datagram without touching its options or payload.
- method_
name - Method name for a request code, or
Noneif this is not a request code. - parse_
code_ string - Parse
"2.05"(or"2.05 Content") into a code byte. - reset_
for - An empty Reset for the given message id (RFC 7252 §4.2).
- response_
to - Build the piggybacked/separate response shell for a request.
- response_
to_ prefix - The same shell, built from a
MessagePrefix— for a datagram that was refused before it was decoded, which therefore has noCoapMessageto answer from. - szx_
size - Block size for a size exponent,
2^(szx + 4), clamped to SZX 6. - uint_
option - A CoAP unsigned integer option value: minimum-length big-endian (RFC 7252 §3.2), so zero is the empty string rather than a zero byte.