← All posts

Official example 06: greeter_plugin — a full data model in 4 C + 4 ODL

Grow through practice

No need to finish reading — test now

One line: greeter_plugin is not “another hello world”; it is the full skeleton of an AMX data model: objects, instances, methods, events, persistence, dynamic parameters, one of each. For a line-by-line ODL walkthrough see ODL parsing (2); this article focuses on how C connects to it.

File layout

FilePurpose
odl/greeter.odlentry: %config, import, include, entry-point
odl/greeter_definition.odldata model: Greeter, History[], Statistics, methods/events
odl/greeter_extra.odlextension: select Greeter adds echo, deferred_echo
odl/greeter_defaults.odldefault instances: welcome message + Info instances
src/greeter_main.centry _greeter_main, stores dm/parser/stats
src/dm_greeter_methods.csay/clear/setMaxHistory/save/load/events/stats
src/dm_greeter_actions.cparameter validation, dynamic Statistics, read-only time
src/dm_greeter_events.cstate event handlers, event printing

ODL: a state machine + a history table

Greeter is a %persistent object; the two key parameters:

%persistent %read-only uint32 MaxHistory = 10 {
    on action validate call check_minimum 1;
}

string State {
    default "Idle";
    on action validate call check_enum ["Idle", "Start", "Running", "Stop"];
    on action validate call check_change;
}
  • MaxHistory: maximum history entries, read-only, minimum validated;
  • State: a state machine; Start/Stop are “commands”, Running/Idle are actual states.

History[] is the history table:

  • counted with NumberOfHistoryEntries: automatic instance count;
  • %template clear(force): template method to clear instances;
  • From/Message read-only + length validation;
  • Retain: retained messages are not auto-deleted;
  • Info[]: %unique %key Alias, demonstrating multi-instance + key;
  • UTC/Local: %volatile + on action read, computed on read.

Statistics is special: it has no parameters in ODL; parameters are provided dynamically by C's stats_read/list/describe, mapping the internal greeter_stats_t.

C: how the 4 files connect

greeter_main.c

  • _greeter_main(reason, dm, parser): START stores dm/parser, STOP clears them;
  • the plugin is loaded by amxrt; it does not need its own main().

dm_greeter_methods.c

  • _Greeter_say: check State == Running → add a History instance in a transaction → print the message;
  • remove_oldest_history: when MaxHistory is reached, delete the oldest non-Retain instance in a transaction;
  • _Greeter_setMaxHistory: when lowering the limit, delete old instances first, then set it in a transaction;
  • _History_clear: force decides whether to delete Retain entries too;
  • _Greeter_save/load: amxo_parser_save_object / clear then amxo_parser_parse_file;
  • _periodic_inform / _Statistics_reset / _send_event / _deferred_echo.

dm_greeter_actions.c

  • _State_check_change: only allows Idle → Start and Running → Stop; other transitions return invalid_value;
  • _stats_read/list/describe: dynamic statistics;
  • _read_utc_time/_read_local_time: %volatile computed on read;
  • check_minimum/check_enum/check_maximum_length: ODL validation actions.

dm_greeter_events.c

  • _enable_greeter: after Idle → Start, set State directly to Running, then emit a changed event;
  • _disable_greeter: after Running → Stop, set it back to Idle;
  • _print_event: count events and dump data.

The state machine: why the handler writes state_param->value

This is the most interesting part of the example.

ODL validation only accepts:

Idle -> Start
Running -> Stop

But Start/Stop are commands, not final states. _enable_greeter must turn Start into Running — if it used normal amxd_object_set_value, validation would run again and reject Start → Running.

So the code bypasses validation and writes the parameter value directly:

amxc_var_set(cstring_t, &state_param->value, "Running");
emit_state_event(object, "Start");

This is deliberate: validation governs what a user may write; event handlers govern which state the system actually enters. Once you see this, the four State values stop being confusing.

Transactions: the template for multi-step changes

_Greeter_say, remove_oldest_history, _History_clear and _Greeter_setMaxHistory all use the same pattern:

amxd_trans_t transaction;
amxd_trans_init(&transaction);
amxd_trans_set_attr(&transaction, amxd_tattr_change_ro, true);
amxd_trans_select_object(&transaction, history_obj);
amxd_trans_add_inst(&transaction, 0, NULL);
amxd_trans_set_param(&transaction, "From", from);
amxd_trans_apply(&transaction, dm);
amxd_trans_clean(&transaction);

Key points:

  • tattr_change_ro: allows writing %read-only parameters (internal use);
  • multiple add/del/set in one apply;
  • on failure, there is no “half-changed” model — something “edit files + reload” cannot give you.

Compile / run on the AMX base environment

Prerequisite: build amx-base:ubuntu first, following “AMX / prplOS development environment”. This article only handles the greeter example; it does not rebuild AMX.

git clone https://github.com/zishuowang696/amx-greeter-demo.git
cd amx-greeter-demo
./build-example.sh
docker run --rm -it amx-greeter-demo:ubuntu

examples/greeter/Dockerfile only does two things:

FROM amx-base:ubuntu AS build
RUN git clone https://gitlab.com/prpl-foundation/components/ambiorix/examples/datamodel/greeter_plugin.git /src/greeter_plugin \
    && git -C /src/greeter_plugin checkout --force f7b3dae7e9e55684265fafe97ab6c55ee5520448
RUN make -C /src/greeter_plugin -j"$(nproc)" && make -C /src/greeter_plugin install
  • source is cloned from Git at a pinned commit; not vendored;
  • compilation reuses the base headers/libs;
  • runtime copies only ODL, greeter.so, and the entrypoint.

The container prints:

  • ubus list → Greeter, Greeter.History, Greeter.History.1, Greeter.Statistics
  • _set State=Start
  • say → {"retval":"Hello from AMX in Docker"}
  • History.2 _get → From=demo, Message=Hello from AMX in Docker

Another shell:

docker exec -it <container> bash
runuser -u demo -- ubus -s /var/run/ubus/ubus.sock list

This stack uses _get/_set; the README's get/set gives Method not found.

Non-root + ACL

In the base image, ubusd runs as root and amxrt runs as demo; the ACL lets demo publish. The ACL file must be root:root 0644; 664 is silently rejected by ubusd — the symptom is ubus_add_object(Greeter) = 2 and an empty ubus list. Details are in the base-environment article.

Test status

  • ✅ prerequisite: amx-base:ubuntu builds from source (~189 MB);
  • ✅ example build: FROM amx-base; clones greeter from Git and compiles; image ~187 MB;
  • ✅ non-root + ACL: amxrt runs as demo; ubus list/_set/say/History.2 _get all pass;
  • ⚠️ method names: _get/_set, not the README's get/set;
  • ❓ edge cases: save/load and extreme MaxHistory reductions were not stress-tested.

One sentence

The value of greeter_plugin is not “features”, but that it strings six AMX mechanisms together: ODL definition, C implementation, transactions, events, dynamic parameters, persistence. Once you understand it, other examples are just “one or two mechanisms fewer”.


Official example: greeter_plugin Related: ODL parsing (2): greeter · 04 greeter_app · AMXRT is a runtime Docker base/ACL: Building an AMX base Docker image Series: AMX 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.