OmniBuf
for C99 Nano.
Freestanding C99 runtime for microcontrollers and embedded targets. Zero heap allocation, statically bounded structs, RODATA descriptor tables, and 2-pass streaming callbacks that talk directly to UART, SPI, or flash.
# Homebrew (macOS & Linux)
brew install omnibuf/tap/protoc-gen-omnibuf-nano
# mise
mise use -g github:omnibuf/omnibuf-nano
# Go
go install github.com/omnibuf/omnibuf-nano/cmd/protoc-gen-omnibuf-nano@latestPredictable RAM, flat flash growth.
One shared C99 encode/decode engine interprets generated const ob_msgdesc_t tables in ROM, wire-compatible with every other OmniBuf runtime.
Static bounds via BSR options or sidecar files
Annotate fields inline with buf.build/omnibuf/nano options or keep shared .proto files untouched by placing bounds in a sidecar .options file. Sidecar rules take precedence over inline annotations, and unbounded fields fail at codegen time.
syntax = "proto3";
package telemetry;
import "omnibuf/nano/v1/options.proto";
message SensorFrame {
uint32 device_id = 1;
string location = 2 [(omnibuf.nano.v1.field).max_size = 32];
bytes mac_addr = 3 [(omnibuf.nano.v1.field) = {
max_size: 6
fixed_length: true
}];
repeated float samples = 4 [(omnibuf.nano.v1.field).max_count = 16];
}# Sidecar .options overrides (same wildcard syntax as nanopb)
*.* max_size:32 max_count:8
SensorFrame.location max_size:64
SensorFrame.mac_addr max_size:6 fixed_length:true- Generated C99 type
- Protobuf type
- uint32_t / uint64_t
- uint32, fixed32 / uint64, fixed64
- int32_t / int64_t
- int32, sint32, sfixed32 / int64, sint64, sfixed64
- bool
- bool
- float / double
- float / double
- typedef enum { ... }
- enum
- char field[MAX_SIZE]
- string (null-terminated)
- OB_BYTES_ARRAY_T(MAX_SIZE)
- bytes (or uint8_t[MAX_SIZE] with fixed_length)
- bool has_x; SubMsg x;
- message (embedded value)
- uint16_t x_count; T x[MAX_COUNT];
- repeated T
- uint16_t which_x; union { ... } x;
- oneof
- uint16_t x_count; Entry x[MAX_COUNT];
- map<K, V>
Every string, bytes, repeated, and map field requires static bounds via omnibuf.nano.v1 proto options or a sidecar .options file. Unbounded fields fail at codegen time so RAM usage stays fixed at compile time.
Generate .ob.h and .ob.c descriptors
protoc-gen-omnibuf-nano emits plain C99 structs, tag constants, _init_zero macros, and tag-sorted const ob_field_t arrays placed in RODATA. Link the generated .ob.c alongside the three runtime files (ob_varint.c, ob_encode.c, ob_decode.c).
version: v2
plugins:
- local: protoc-gen-omnibuf-nano
out: gen/c
opt:
- options_path=proto$ arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -Os -ffreestanding -std=c99 \
-Iruntime runtime/ob_*.c gen/c/sensor.ob.c -c2-pass streaming encode & single-pass decode
Nested messages, arrays, and maps size themselves with a null-callback pass first, then stream bytes directly to the output callback. That avoids memmove, removes sub-message size caps, and lets firmware stream straight over UART or SPI.
#include "ob_encode.h"
#include "ob_decode.h"
#include "sensor.ob.h"
uint8_t buf[256];
telemetry_SensorFrame tx = telemetry_SensorFrame_init_zero;
tx.device_id = 42;
strcpy(tx.location, "rack-04");
tx.samples_count = 2;
tx.samples[0] = 21.5f;
tx.samples[1] = 22.0f;
/* 2-pass streaming encode (zero malloc) */
ob_ostream_t os = ob_ostream_from_buffer(buf, sizeof(buf));
bool ok = ob_encode(&os, &telemetry_SensorFrame_msg, &tx);
/* Single-pass streaming decode */
telemetry_SensorFrame rx;
ob_istream_t is = ob_istream_from_buffer(buf, os.bytes_written);
ok = ob_decode(&is, &telemetry_SensorFrame_msg, &rx);static bool uart_write_cb(ob_ostream_t *stream, const uint8_t *buf, size_t count) {
uart_t *uart = (uart_t *)stream->state;
return uart_tx_blocking(uart, buf, count);
}
/* Stream directly to UART with no packet buffer in RAM */
ob_ostream_t uart_stream = { uart_write_cb, &uart0, 0, 0, NULL };
ob_encode(&uart_stream, &telemetry_SensorFrame_msg, &tx);C99 Nano questions.
Does the runtime call malloc or depend on a hosted libc?
No. runtime/ compiles cleanly under -ffreestanding -std=c99 -pedantic and references only memcpy and memset. Every struct size is fixed at compile time from max_size and max_count.
How does flash and speed compare to nanopb?
On the 11-field PeerUpdate struct (976 B RAM for both libraries), omnibuf-nano encodes 1.10x faster and decodes 1.22x faster than nanopb 0.4.9.1 at -O3. At -Os, the three runtime objects total 9,700 bytes of code with 0 bytes of .data/.bss, roughly 13% smaller than nanopb.
What happens when a microcontroller receives Field #0 or unknown fields?
Every OmniBuf tag carries its 4-bit wire type. The decoder skips Field #0 (SchemaMeta) and any unrecognized tag using a 32-byte stack scratch buffer in a loop, so firmware stays forward-compatible with backend schema additions.