SatCat5
coap_writer.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Message formatting for the Constrained Applications Protocol (CoAP)
6 
7 #pragma once
8 
10 #include <satcat5/coap_reader.h>
11 #include <satcat5/io_writeable.h>
12 
13 namespace satcat5 {
14  namespace coap {
26  class Writer final {
27  public:
30  constexpr Writer(
32  bool write_max_age = true)
33  : m_dst(dst), m_last_opt(0), m_insert_max_age(write_max_age) {}
34 
37  inline bool ready() const
38  { return m_dst && m_dst->get_write_space(); }
39 
42  bool write_header(
43  u8 type, Code code, u16 msg_id,
44  u64 token = 0, u8 tkl = 0);
45 
49  bool write_header(Code code, Connection* request);
50 
54  bool write_option(u16 id, unsigned len, const void* data);
55  bool write_option(u16 id, const char* str);
56  bool write_option(u16 id, u64 value);
58 
60  bool write_uri(u16 id, const char* str);
61 
65 
67  inline bool write_finalize()
68  { insert_max_age(); return m_dst->write_finalize(); }
69 
70  protected:
71  // Internal write methods.
72  bool write_optid(u16 id, unsigned len);
73  void write_varint(u64 x, unsigned len);
74  void insert_max_age(u16 next_id = 0);
75 
76  satcat5::io::Writeable* const m_dst;
77  u16 m_last_opt;
79  };
80  }
81 }
CoAP request/response handling for a single client-server connection.
Message formatting for the Constrained Applications Protocol (CoAP).
Definition: coap_writer.h:26
constexpr Writer(satcat5::io::Writeable *dst, bool write_max_age=true)
Create this object and set the destination.
Definition: coap_writer.h:30
bool write_uri(u16 id, const char *str)
Convert a URI to consecutive Uri-Path options.
Definition: coap_writer.cc:70
bool m_insert_max_age
Add Max-Age=0 to disable caching.
Definition: coap_writer.h:78
bool ready() const
Is this object ready for writing? Note: This is the only safe method if m_dst is null.
Definition: coap_writer.h:37
bool write_finalize()
After the last option, finish with an empty message.
Definition: coap_writer.h:67
bool write_option(u16 id, unsigned len, const void *data)
Write option(s) one at a time, in various formats.
Definition: coap_writer.cc:49
satcat5::io::Writeable * write_data()
After the last option, start writing message data.
Definition: coap_writer.cc:88
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.
Definition: coap_writer.cc:25
u16 m_last_opt
Previous option-ID.
Definition: coap_writer.h:77
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
virtual unsigned get_write_space() const =0
How many bytes can be written without blocking?
virtual bool write_finalize()
Mark end of frame and release temporary working data.
Constants relating to the Constrained Applications Protocol (CoAP)
Message parsing for the Constrained Applications Protocol (CoAP)
"Writeable" I/O interface core definitions
CoAP message header CODE field (Section 12.1).