SatCat5
satcat5::coap::Writer Class Referencefinal

Detailed Description

Message formatting for the Constrained Applications Protocol (CoAP).

This class implements message formatting for the Constrained Applications Protocol (CoAP) defined in IETF RFC-7252: https://www.rfc-editor.org/rfc/rfc7252

The coap::Writer object is typically ephemeral:

  • Create a coap::Writer object, attached to any Writeable sink.
  • Call write_header(...) to write the CoAP header.
  • Call write_option(...) for each desired option field.
  • Call write_data(...) to write data and finalize the message.

Definition at line 26 of file coap_writer.h.

#include <coap_writer.h>

Collaboration diagram for satcat5::coap::Writer:
[legend]

Public Member Functions

constexpr Writer (satcat5::io::Writeable *dst, bool write_max_age=true)
 Create this object and set the destination. More...
 
bool ready () const
 Is this object ready for writing? Note: This is the only safe method if m_dst is null.
 
bool write_header (u8 type, Code code, u16 msg_id, u64 token=0, u8 tkl=0)
 Always start by writing the header, with optional token. More...
 
bool write_header (Code code, Connection *request)
 Alternate version of write_header for responding to a query. More...
 
bool write_uri (u16 id, const char *str)
 Convert a URI to consecutive Uri-Path options.
 
satcat5::io::Writeablewrite_data ()
 After the last option, start writing message data. More...
 
bool write_finalize ()
 After the last option, finish with an empty message.
 
bool write_option (u16 id, unsigned len, const void *data)
 Write option(s) one at a time, in various formats. More...
 
bool write_option (u16 id, const char *str)
 Write option(s) one at a time, in various formats. More...
 
bool write_option (u16 id, u64 value)
 Write option(s) one at a time, in various formats. More...
 

Protected Member Functions

bool write_optid (u16 id, unsigned len)
 
void write_varint (u64 x, unsigned len)
 
void insert_max_age (u16 next_id=0)
 

Protected Attributes

satcat5::io::Writeable *const m_dst
 
u16 m_last_opt
 Previous option-ID.
 
bool m_insert_max_age
 Add Max-Age=0 to disable caching.
 

Constructor & Destructor Documentation

◆ Writer()

constexpr satcat5::coap::Writer::Writer ( satcat5::io::Writeable dst,
bool  write_max_age = true 
)
inlineconstexpr

Create this object and set the destination.

Note: The resulting object is safe to use even if dst is null.

Definition at line 30 of file coap_writer.h.

Member Function Documentation

◆ write_data()

satcat5::io::Writeable * Writer::write_data ( )

After the last option, start writing message data.

Call this method exactly once, then finalize when ready.

Definition at line 88 of file coap_writer.cc.

◆ write_header() [1/2]

bool Writer::write_header ( Code  code,
Connection request 
)

Alternate version of write_header for responding to a query.

Automatically determines the response type, then copies the message-ID and token directly from the incoming request.

Definition at line 43 of file coap_writer.cc.

◆ write_header() [2/2]

bool Writer::write_header ( u8  type,
Code  code,
u16  msg_id,
u64  token = 0,
u8  tkl = 0 
)

Always start by writing the header, with optional token.

Token length can be set manually (tkl > 0) or automatically.

Definition at line 25 of file coap_writer.cc.

◆ write_option() [1/3]

bool Writer::write_option ( u16  id,
const char *  str 
)

Write option(s) one at a time, in various formats.

Note: Options MUST be written in ascending-ID order.

Definition at line 57 of file coap_writer.cc.

◆ write_option() [2/3]

bool Writer::write_option ( u16  id,
u64  value 
)

Write option(s) one at a time, in various formats.

Note: Options MUST be written in ascending-ID order.

Definition at line 61 of file coap_writer.cc.

◆ write_option() [3/3]

bool Writer::write_option ( u16  id,
unsigned  len,
const void *  data 
)

Write option(s) one at a time, in various formats.

Note: Options MUST be written in ascending-ID order.

Definition at line 49 of file coap_writer.cc.


The documentation for this class was generated from the following files: