← All posts

OpenWrt ubus official examples (1): the data model — blob, blobmsg and policy

Grow through practice

No need to finish reading — test now

One line: ubus's “data model” is not a table; it is an object model plus a one-shot blobmsg payload. The object model lives on the bus; the payload model lives only for that call.

Separate the two models

Two things are easy to mix up in ubus:

1) Object model (on the bus)

ubus_object
  ├─ name        "test"
  ├─ type        ubus_object_type
  └─ methods[]   ubus_method (name + handler + policy)

It answers: what objects exist, what methods can be called, what the arguments look like.

2) Payload model (per call)

blob_buf → blob_attr → blobmsg

It answers: what data this call/reply actually carries.

In server.c, UBUS_METHOD("hello", test_hello, hello_policy) touches both:

  • "hello" is the method name in the object model;
  • hello_policy is the parsing rule for the payload model.

blob vs blobmsg

  • blob: id_len + payload; a compact binary attribute; no name/type semantics.
  • blobmsg: adds name + type on top of blob, supports table/array, close to JSON.

So the ubus CLI can turn JSON into blobmsg:

ubus call test hello '{"id":1,"msg":"hello"}'

C code does not parse JSON directly:

struct blob_buf b;
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "id", 1);
blobmsg_add_string(&b, "msg", "hello");

Four policies in the official example

examples/server.c defines three argument sets:

/* hello: id(int32), msg(string) */
static const struct blobmsg_policy hello_policy[] = {
    [HELLO_ID]  = { .name = "id",  .type = BLOBMSG_TYPE_INT32 },
    [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
};

/* watch: id(int32), counter(int32) */
static const struct blobmsg_policy watch_policy[] = { ... };

/* count: to(int32), string(string) */
static const struct blobmsg_policy count_policy[] = { ... };

Parsing:

struct blob_attr *tb[__HELLO_MAX];
blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy),
              tb, blob_data(msg), blob_len(msg));

if (tb[HELLO_MSG]) {
    msgstr = blobmsg_data(tb[HELLO_MSG]);
}

Key points:

  • blobmsg_parse finds attributes by name and checks type;
  • missing fields are not filled with defaults; you must check;
  • unknown fields are usually ignored; policy is not a full schema;
  • use blobmsg_get_u32 / blobmsg_get_string to read values.

What the three methods demonstrate

hello: parse + deferred reply + fd

  • parses id/msg;
  • ubus_defer_request suspends the request;
  • after 1 second, ubus_send_reply returns message;
  • it also passes an fd over a pipe; the client reads msg1: ... through ustream_fd.

This is ubus “async RPC + fd passing”, not plain JSON-RPC.

watch: subscribe + notify

  • the client registers its own object and gets an id;
  • calls test's watch with that id;
  • the server calls ubus_subscribe(ctx, &test_event, id);
  • the client sends ubus_notify, and the server's test_notify receives it;
  • when the client object disappears, test_handle_remove gets the remove callback.

count: request/reply validation

  • the client sends to and string;
  • the server generates a string with count_to_number(to) and compares;
  • replies { "rc": 0/nonzero };
  • the client parses rc in the ubus_invoke callback.

Actually run it

Build the official examples on Ubuntu (BUILD_EXAMPLES=ON) with a private socket:

git clone https://github.com/openwrt/ubus.git
cmake -S ubus -B ubus/build \
  -DBUILD_LUA=OFF -DBUILD_EXAMPLES=ON \
  -DCMAKE_PREFIX_PATH=/usr/local
cmake --build ubus/build -j"$(nproc)"

ubusd -s /tmp/ubus-ex.sock &
./ubus/build/examples/server -s /tmp/ubus-ex.sock &
timeout 6 ./ubus/build/examples/client -s /tmp/ubus-ex.sock

Measured output (excerpt):

Avg time per iteration: 63 usec
Subscribers active: 1
Got fd from the server, watching...
completed request, ret: 0
Got line: msg1: test received a message: blah
Sending count up to '100100'; string has length '592926'
Server validated our count up to '100100'

ubus list shows test; the client does not exit by itself because of its timers, so timeout returning 124 is expected.

Note: with the default ubusd ACL, a non-root client fails ubus_add_object (Invalid argument). This demo runs as root; in production configure an ACL instead of copying root blindly.

Boundaries: it is not TR-181

  • blobmsg is a transient RPC payload; it is not persisted and has no object tree/transactions/events;
  • policy only does “name + type” parsing, not defaults, ranges, or cross-field validation;
  • for config/state models, use UCI or AMX/TR-181; see

From UCI to a data model;

  • nested structures use blobmsg_open_table / blobmsg_open_array, with BLOBMSG_TYPE_TABLE / ARRAY; the official examples only use top-level attributes;
  • messages have size limits; complex data needs its own control.

Verification status (site mission)

  • ✅ source: openwrt/ubus 414c60a, examples/server.c, client.c, count.c, CMakeLists.txt;
  • ✅ PC run: built the examples on Ubuntu; root + private socket; test object, notify, deferred hello and count validation all worked;
  • ⚠️ environment: not run on an OpenWrt device; non-root needs ACL;
  • ❓ not covered: nested table/array, message-size limits, concurrency/error paths.

One sentence

The object model says “what can be called”; the blobmsg model says “what this call carries”. Policy is the bridge; it is lightweight, but it is not a schema.


Related: ubus CLI in practice · minimal C API server/client Source: openwrt/ubus examples (414c60a) Related: From UCI to a data model · UCI guide · prplOS LED plugin Series: OpenWrt ubus Official Examples.

Practice

Found this useful?

I write about AI agents × embedded / edge AI. Subscribe, or email me a concrete problem.

Guestbook →Subscribe (RSS)Support

Comments 0

No comments yet — be the first!

No login required; name is optional.