OpenWrt ubus official examples (1): the data model — blob, blobmsg and policy
Grow through practice
No need to finish reading — test nowOne 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_policyis 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_parsefinds 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_stringto read values.
What the three methods demonstrate
hello: parse + deferred reply + fd
- parses
id/msg; ubus_defer_requestsuspends the request;- after 1 second,
ubus_send_replyreturnsmessage; - it also passes an fd over a pipe; the client reads
msg1: ...throughustream_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'swatchwith thatid; - the server calls
ubus_subscribe(ctx, &test_event, id); - the client sends
ubus_notify, and the server'stest_notifyreceives it; - when the client object disappears,
test_handle_removegets the remove callback.
count: request/reply validation
- the client sends
toandstring; - the server generates a string with
count_to_number(to)and compares; - replies
{ "rc": 0/nonzero }; - the client parses
rcin theubus_invokecallback.
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
ubusdACL, a non-root client failsubus_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
- nested structures use
blobmsg_open_table/blobmsg_open_array, withBLOBMSG_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/ubus414c60a,examples/server.c,client.c,count.c,CMakeLists.txt; - ✅ PC run: built the examples on Ubuntu; root + private socket;
testobject, 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.
Comments 0
No comments yet — be the first!