Official example 06: greeter_plugin — a full data model in 4 C + 4 ODL
Grow through practice
No need to finish reading — test nowOne line:
greeter_pluginis 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
| File | Purpose |
|---|---|
odl/greeter.odl | entry: %config, import, include, entry-point |
odl/greeter_definition.odl | data model: Greeter, History[], Statistics, methods/events |
odl/greeter_extra.odl | extension: select Greeter adds echo, deferred_echo |
odl/greeter_defaults.odl | default instances: welcome message + Info instances |
src/greeter_main.c | entry _greeter_main, stores dm/parser/stats |
src/dm_greeter_methods.c | say/clear/setMaxHistory/save/load/events/stats |
src/dm_greeter_actions.c | parameter validation, dynamic Statistics, read-only time |
src/dm_greeter_events.c | state 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/Stopare “commands”,Running/Idleare actual states.
History[] is the history table:
counted with NumberOfHistoryEntries: automatic instance count;%template clear(force): template method to clear instances;From/Messageread-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 storesdm/parser, STOP clears them;- the plugin is loaded by
amxrt; it does not need its ownmain().
dm_greeter_methods.c
_Greeter_say: checkState == Running→ add aHistoryinstance in a transaction → print the message;remove_oldest_history: whenMaxHistoryis reached, delete the oldest non-Retaininstance in a transaction;_Greeter_setMaxHistory: when lowering the limit, delete old instances first, then set it in a transaction;_History_clear:forcedecides whether to deleteRetainentries too;_Greeter_save/load:amxo_parser_save_object/ clear thenamxo_parser_parse_file;_periodic_inform/_Statistics_reset/_send_event/_deferred_echo.
dm_greeter_actions.c
_State_check_change: only allowsIdle → StartandRunning → Stop; other transitions returninvalid_value;_stats_read/list/describe: dynamic statistics;_read_utc_time/_read_local_time:%volatilecomputed on read;check_minimum/check_enum/check_maximum_length: ODL validation actions.
dm_greeter_events.c
_enable_greeter: afterIdle → Start, setStatedirectly toRunning, then emit a changed event;_disable_greeter: afterRunning → Stop, set it back toIdle;_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-onlyparameters (internal use);- multiple
add/del/setin oneapply; - 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=Startsay→{"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'sget/setgivesMethod 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:ubuntubuilds from source (~189 MB); - ✅ example build:
FROM amx-base; clones greeter from Git and compiles; image ~187 MB; - ✅ non-root + ACL:
amxrtruns asdemo;ubus list/_set/say/History.2 _getall pass; - ⚠️ method names:
_get/_set, not the README'sget/set; - ❓ edge cases:
save/loadand extremeMaxHistoryreductions were not stress-tested.
One sentence
The value of
greeter_pluginis 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.
Comments 0
No comments yet — be the first!