← All posts

Ambiorix ODL parsing (2): a bigger ODL (greeter)

Grow through practice

No need to finish reading — test now

Part 1 used amx-scheduler (2 C + 2 ODL). This one uses greeter: 4 ODL files, covering instances, templates, events, validate, resolvers and async.

The four files

FilePurpose
greeter.odl%config / import / include / entry-point
greeter_definition.odlobject tree: parameters, functions, events, multi-instance
greeter_defaults.odl%populate: add instances, set defaults
greeter_extra.odlselect + 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 / ubus in %config are nested htables;
  • include loads the definition first;
  • #include extra is an optional include; if the file exists it is loaded (so echo/deferred_echo are available);
  • ?include "a":"b": try the saved file, fall back to defaults;
  • entry-point points 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_minimum is 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);
  • %in input, %mandatory required, %strict strict 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 %populate or at runtime;
  • counted with: auto-maintained instance counter;
  • %template clear: a function only on the template/instances;
  • %unique %key Alias: unique key for Info;
  • %volatile: no change events; computed-on-read is done via on 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();
  • filter is the slot filter expression;
  • call resolves 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_dump in the .so;
  • %async: deferred return (implementation returns amxd_status_deferred).

ODL ↔ C (additional)

ODLC / effect
%persistentpersistence attribute on object/parameter
%read-onlyamxd_pattr_read_only
%unique %keyamxd_pattr_unique / amxd_pattr_key
%templatetemplate object / template function
counted withautomatic instance counter
%volatilecomputed on read
%asyncdeferred function
%in/%mandatory/%strictfunction argument attributes
<!import:...!>import resolver instruction
selectappend definitions to an existing object

Parse order (important)

  1. %config in greeter.odl;
  1. import the plugin;
  1. include the definition;
  1. ?include: saved file first, otherwise defaults;
  1. 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.

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.