ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
codec.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file codec.h
6 * @brief One binary codec interface; a wire encoding is an instance of it.
7 *
8 * The interface fixes the operations, their order, and their signatures; a format supplies the
9 * function pointers. The order is the field order of the table, so a format whose operations drift
10 * out of order fails to compile.
11 *
12 * Dispatch is a `static const` table of function pointers in rodata.
13 *
14 * The region types come from spatium.h and the byte verbs from octetus_introitus_exitus.h. A codec
15 * allocates nothing and owns no buffer: it writes into a mmgr_span the caller bound and reads from a
16 * mmgr_cspan.
17 *
18 * @author Douglas Quigg (dstroy0)
19 * @date 2026
20 */
21
22#ifndef PROTOCORE_CODEC_H
23#define PROTOCORE_CODEC_H
24
25#include "spatium/spatium.h"
26
27#include "protocore_config.h" // PROTOCORE_ENABLE_CBOR / PROTOCORE_ENABLE_MSGPACK gate the instances below
28
29EMBED_BEGIN_DECLS
30
31/**
32 * @brief The next item's type, reported by protocore_codec::peek without consuming it.
33 *
34 * One set of names across every format: CBOR calls a byte string "bytes" and MessagePack calls it
35 * "bin"; CBOR has "null" and MessagePack "nil". They are the same item, so they get one name here
36 * and the format maps its own tag onto it.
37 */
38EMBED_ENUM_BYTE(protocore_codec_type, PROTOCORE_CODEC_UINT = 0, PROTOCORE_CODEC_INT, PROTOCORE_CODEC_BYTES,
39 PROTOCORE_CODEC_STR, PROTOCORE_CODEC_ARRAY, PROTOCORE_CODEC_MAP, PROTOCORE_CODEC_BOOL,
40 PROTOCORE_CODEC_NULL, PROTOCORE_CODEC_FLOAT,
41 PROTOCORE_CODEC_INVALID ///< end of buffer, a prior error, or an item this format does not carry
42);
43
44/**
45 * @brief A wire encoding: the ten writes, the peek, and the nine reads.
46 *
47 * Field order is the operation order every format declares and implements in. `int`, `bool` and
48 * `float` are keywords, so the members carry the put_ / get_ prefix that says which direction they
49 * run in.
50 */
51typedef struct
52{
53 // --- encode into a caller-bound mmgr_span ---
54 void (*put_uint)(mmgr_span *w, uint64_t v);
55 void (*put_int)(mmgr_span *w, int64_t v);
56 void (*put_bytes)(mmgr_span *w, const uint8_t *data, size_t len);
57 void (*put_str)(mmgr_span *w, const char *s);
58 void (*put_str_n)(mmgr_span *w, const char *s, size_t len);
59 void (*put_bool)(mmgr_span *w, embed_bool b);
60 void (*put_null)(mmgr_span *w);
61 void (*put_float)(mmgr_span *w, float f);
62 void (*put_array)(mmgr_span *w, size_t count);
63 void (*put_map)(mmgr_span *w, size_t count);
64
65 /**
66 * @brief Emit a map key, given both spellings of it.
67 *
68 * A spec often names the same field differently per encoding: RFC 8428 labels a SenML base name
69 * `"bn"` in JSON and `-2` in CBOR. That is the encoding's business, not the caller's, so the
70 * caller hands over both and the format picks the one it is specified to write. Without this the
71 * difference leaks upward and every producer keeps one walk per encoding.
72 */
73 void (*put_label)(mmgr_span *w, const char *name, int64_t num);
74
75 // --- decode from a caller-bound mmgr_cspan ---
76 protocore_codec_type (*peek)(mmgr_cspan *r);
77 embed_bool (*get_uint)(mmgr_cspan *r, uint64_t *out);
78 embed_bool (*get_int)(mmgr_cspan *r, int64_t *out);
79 embed_bool (*get_bytes)(mmgr_cspan *r, const uint8_t **out, size_t *len);
80 embed_bool (*get_str)(mmgr_cspan *r, const char **out, size_t *len);
81 embed_bool (*get_array)(mmgr_cspan *r, size_t *count);
82 embed_bool (*get_map)(mmgr_cspan *r, size_t *count);
83 embed_bool (*get_bool)(mmgr_cspan *r, embed_bool *out);
84 embed_bool (*get_null)(mmgr_cspan *r);
85 embed_bool (*get_float)(mmgr_cspan *r, float *out);
87
88// Each format declares its own instance in its own header: cbor.h has Cbor, msgpack.h has
89// MsgPack. The instance is the storage, so the format that owns the operations owns the
90// table built from them, and a build that compiles a format out has neither.
91
92EMBED_END_DECLS
93
94#endif // PROTOCORE_CODEC_H
EMBED_BEGIN_DECLS EMBED_ENUM_BYTE(protocore_codec_type, PROTOCORE_CODEC_UINT=0, PROTOCORE_CODEC_INT, PROTOCORE_CODEC_BYTES, PROTOCORE_CODEC_STR, PROTOCORE_CODEC_ARRAY, PROTOCORE_CODEC_MAP, PROTOCORE_CODEC_BOOL, PROTOCORE_CODEC_NULL, PROTOCORE_CODEC_FLOAT, PROTOCORE_CODEC_INVALID)
The next item's type, reported by protocore_codec::peek without consuming it.
A wire encoding: the ten writes, the peek, and the nine reads.
Definition codec.h:52
embed_bool(* get_str)(mmgr_cspan *r, const char **out, size_t *len)
Definition codec.h:80
void(* put_bytes)(mmgr_span *w, const uint8_t *data, size_t len)
Definition codec.h:56
void(* put_str)(mmgr_span *w, const char *s)
Definition codec.h:57
embed_bool(* get_int)(mmgr_cspan *r, int64_t *out)
Definition codec.h:78
void(* put_bool)(mmgr_span *w, embed_bool b)
Definition codec.h:59
embed_bool(* get_float)(mmgr_cspan *r, float *out)
Definition codec.h:85
void(* put_uint)(mmgr_span *w, uint64_t v)
Definition codec.h:54
embed_bool(* get_bool)(mmgr_cspan *r, embed_bool *out)
Definition codec.h:83
embed_bool(* get_null)(mmgr_cspan *r)
Definition codec.h:84
embed_bool(* get_uint)(mmgr_cspan *r, uint64_t *out)
Definition codec.h:77
void(* put_float)(mmgr_span *w, float f)
Definition codec.h:61
void(* put_map)(mmgr_span *w, size_t count)
Definition codec.h:63
void(* put_label)(mmgr_span *w, const char *name, int64_t num)
Emit a map key, given both spellings of it.
Definition codec.h:73
embed_bool(* get_bytes)(mmgr_cspan *r, const uint8_t **out, size_t *len)
Definition codec.h:79
void(* put_array)(mmgr_span *w, size_t count)
Definition codec.h:62
embed_bool(* get_map)(mmgr_cspan *r, size_t *count)
Definition codec.h:82
void(* put_int)(mmgr_span *w, int64_t v)
Definition codec.h:55
embed_bool(* get_array)(mmgr_cspan *r, size_t *count)
Definition codec.h:81
protocore_codec_type(* peek)(mmgr_cspan *r)
Definition codec.h:76
void(* put_null)(mmgr_span *w)
Definition codec.h:60
void(* put_str_n)(mmgr_span *w, const char *s, size_t len)
Definition codec.h:58