Skip to main content

Module codec

Module codec 

Source
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§

BlockValue
A Block1 or Block2 option value: NUM << 4 | M << 3 | SZX (RFC 7959 §2.2).
CoapMessage
A decoded CoAP message.
MessagePrefix
The part of a message that lives at fixed offsets: the header and the token.

Enums§

DecodeError
Why a datagram is not a CoAP message.
EncodeError
Why a message cannot be put on the wire.
MessageType
CoAP message type (the T field).

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_BLOCK1
Block1 (RFC 7959 §2.1): which block of the request body this is.
OPT_BLOCK2
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.detail pair, 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.dd notation.
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 None if 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 no CoapMessage to 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.