Ambiorix ODL parsing (2): a bigger ODL (greeter)
Grow through practice
No need to finish reading — test nowPart 1 used
amx-scheduler(2 C + 2 ODL). This one usesgreeter: 4 ODL files, covering instances, templates, events, validate, resolvers and async.
The four files
| File | Purpose |
|---|---|
greeter.odl | %config / import / include / entry-point |
greeter_definition.odl | object tree: parameters, functions, events, multi-instance |
greeter_defaults.odl | %populate: add instances, set defaults |
greeter_extra.odl | select + resolver instruction + %async |
greeter.odl: entry config
%config {
name = "greeter";
storage-path = "${rw_data_path}/${name}/";
odl = {
load-dm-events = false,
dm-save = true,
dm-save-on-changed = true,
dm-save-delay = 1000,
directory = "${storage-path}/odl"
};
ubus = { watch-ubus-events = true; };
definition_file = "${name}_definition.odl";
extra_file = "${name}_extra.odl";
defaults_file = "${name}_defaults.odl";
}
import "${name}.so" as "${name}";
include "${definition_file}";
#include "${extra_file}";
?include "${storage-path}/odl/":"${defaults_file}";
%define {
entry-point greeter.greeter_main;
}
odl/ubusin%configare nested htables;includeloads the definition first;#include extrais an optional include; if the file exists it is loaded (soecho/deferred_echoare available);?include "a":"b": try the saved file, fall back to defaults;entry-pointpoints to_greeter_main.
greeter_definition.odl: the object tree
Root object
%persistent object Greeter {
%persistent %read-only uint32 MaxHistory = 10 {
on action validate call check_minimum 1;
}
...
}
%persistent: persist object/parameter;%read-only: read-only;on action validate: validate before write (check_minimumis a built-in ftab function).
State machine
string State {
default "Idle";
on action validate call check_enum ["Idle", "Start", "Running", "Stop"];
on action validate call check_change;
}
check_enum: only allow the listed values;check_change: a greeter-specific ftab function (_State_check_change), not built-in.
RPC functions
string say(%in %mandatory string from,
%in %mandatory string message,
%in bool retain = false);
uint32 setMaxHistory(%in %mandatory uint32 max);
bool save(%in %strict string file = "${save_file}");
bool load(%in %strict string file = "${save_file}");
event 'MyEvent!';
void send_event(%in %mandatory variant data);
%ininput,%mandatoryrequired,%strictstrict type;- bodies live in C (
_Greeter_say, etc.).
Multi-instance History[]
%persistent %read-only object History[] {
counted with NumberOfHistoryEntries;
%template uint32 clear(%in bool force = false);
%persistent %read-only string From {
on action validate call check_maximum_length 64;
}
%persistent %read-only string Message {
on action validate call check_maximum_length 256;
}
%persistent bool Retain = false;
object Info[] {
%unique %key string Alias;
string Text = "";
uint32 Number = 0;
int32 SignedNumber = -100;
ssv_string Flags = "";
bool Disabled = false;
%volatile datetime UTC {
on action read call read_utc_time;
}
%volatile datetime Local {
on action read call read_local_time;
}
}
}
History[]: template object; instances are created by%populateor at runtime;counted with: auto-maintained instance counter;%template clear: a function only on the template/instances;%unique %key Alias: unique key forInfo;%volatile: no change events; computed-on-read is done viaon action read;on action read: read callback.
Statistics: dynamic parameters
%read-only object Statistics {
void periodic_inform(%in uint32 secs);
void reset();
on action read call stats_read;
on action list call stats_list;
on action describe call stats_describe;
}
Parameters are not defined in ODL; C provides them dynamically (stats_read/list/describe).
Event subscriptions
%populate {
on event "dm:object-changed" call enable_greeter
filter 'object == "Greeter." && parameters.State.from == "Idle" && parameters.State.to == "Start"';
on event "dm:object-changed" call disable_greeter
filter '...';
on event "*" call print_event;
}
on event→amxp_slot_connect_filtered();filteris the slot filter expression;callresolves to_enable_greeter, etc.
greeter_defaults.odl: %populate instances
%populate {
object Greeter.History {
instance add (0,"") {
!read-only parameter From = "odl parser";
!read-only parameter Message = "Welcome to the Greeter App";
parameter Retain = true;
object Info {
instance add(0, "Info1") { parameter Text = "..."; }
}
}
}
}
instance add(index, name)→amxd_object_add_instance;!read-only: temporarily bypass read-only during populate;- this is how defaults/instances are written.
greeter_extra.odl: select + resolver + async
%define {
select Greeter {
variant echo(%in %mandatory variant data)<!import:${name}:_function_dump!>;
%async variant deferred_echo(%in %mandatory variant data, %in uint32 time);
}
}
select: append functions to an existing object;< !import:${name}:_function_dump!>: resolver instruction, bind directly to_function_dumpin the.so;%async: deferred return (implementation returnsamxd_status_deferred).
ODL ↔ C (additional)
| ODL | C / effect |
|---|---|
%persistent | persistence attribute on object/parameter |
%read-only | amxd_pattr_read_only |
%unique %key | amxd_pattr_unique / amxd_pattr_key |
%template | template object / template function |
counted with | automatic instance counter |
%volatile | computed on read |
%async | deferred function |
%in/%mandatory/%strict | function argument attributes |
<!import:...!> | import resolver instruction |
select | append definitions to an existing object |
Parse order (important)
%configingreeter.odl;
importthe plugin;
includethe definition;
?include: saved file first, otherwise defaults;
entry-point: START/STOP.
#include extra is optional; if the file exists it is parsed.
Running
This is a static walkthrough. Build/run instructions are in the official greeter README (depends on libamxc/libamxd/libamxo/libamxp etc.). For generic compile/test on the AMX base environment steps, see 01 wait_for.
One sentence
greeter uses most of ODL in one place: objects, parameters, functions, events, instances, templates, validate, resolvers, async. Read these four files, and other ODLs are just vocabulary.
Previous: Ambiorix ODL parsing (1): amx-scheduler Reference: ODL handbook Official example: greeter_plugin C implementation: 06 greeter_plugin Series: AMX Official Examples.
Comments 0
No comments yet — be the first!