← All posts

Ambiorix ODL parsing (1): from ODL to the data model (amx-scheduler)

Grow through practice

No need to finish reading — test now

One line: ODL = a declarative sequence of amxd_* calls + symbol binding. The parser is libamxo; it turns ODL into an object tree, then binds call / entry-point to C functions in the .so.

The parse chain

amxrt
  → amxo_parser_parse_file()
    → %config        parse config
    → import         dlopen the .so
    → include        recursive parse
    → %define        amxd_object_new / amxd_param_new
    → %populate      add_instance / slot_connect
    → entry-point    dlsym + invoke

amx-scheduler's ODL, section by section

%config: parser config

%config {
    storage-path = "${rw_data_path}/${name}/";
    definition_file = "${name}_definition.odl";
    defaults_dir = "${cfg-dir}/${name}/defaults/";
}
  • these keys go into parser->config;
  • in C, read them with amxo_parser_get_config(parser, "storage-path") or amxrt_get_config();
  • ${name} / ${rw_data_path} are variable expansions; amxrt sets name from the executable name (or -o name=...).

import: load the .so

import "amx-scheduler.so" as "${name}";
  • libamxo calls dlopen;
  • later, call xxx / entry-point symbols are resolved by amxo_import_resolver.c, which builds _xxx (or _<module>_xxx) and calls dlsym;
  • with import-dbg = true you see [IMPORT-DBG] - dlopen ... and symbol _task_added resolved ....

include: recursive parse

include "${definition_file}";
  • equivalent to another amxo_parser_parse_file;
  • parsed into the same DM; entry_points are shared.

entry-point: the entry function

%define {
    entry-point amx-scheduler.scheduler_main;
}
  • the parser calls amxo_parser_add_entry_point(parser, fn);
  • amxrt invokes amxo_parser_invoke_entry_points(parser, dm, AMXO_START|AMXO_STOP) at start/stop;
  • that calls _scheduler_main(reason, dm, parser) in C.

%define: the object tree

%define {
    %persistent object Scheduler {
        %persistent object Task[] {
            %persistent %unique %key string Alias;
            %persistent bool Enable = false;
            %persistent uint32 Duration = 0;
            %read-only string Status = "";
        }
    }
}

Mapping:

ODLC
object Scheduler {}amxd_object_new(&o, amxd_object_singleton, "Scheduler") + attach to DM
object Task[] {}amxd_object_new(&t, amxd_object_template, "Task"); instances via amxd_object_add_instance
string Aliasamxd_param_new(&p, "Alias", AMXC_VAR_ID_CSTRING) + amxd_object_add_param
bool EnableAMXC_VAR_ID_BOOL
uint32 DurationAMXC_VAR_ID_UINT32
%persistentamxd_param_set_attr(p, amxd_pattr_persistent, true)
%keyamxd_param_set_attr(p, amxd_pattr_key, true)
%uniqueamxd_param_set_attr(p, amxd_pattr_unique, true)
%read-onlyamxd_param_set_attr(p, amxd_pattr_read_only, true)

%populate: instances and events

%populate {
    on event "dm:instance-added" call task_added
        filter 'path == "Scheduler.Task."';
    on event "dm:instance-removed" call task_deleted;
    on event "dm:object-changed" call task_changed
        filter 'path matches "Scheduler\.Task\.[0-9]+\." &&
                not contains("parameters.Status")';
}
  • on event ... call f → amxp_slot_connect_filtered() / amxp_slot_connect();
  • f resolves to _f (_task_added / _task_deleted / _task_changed);
  • C signature:
  void _task_added(const char* const event_name,
                   const amxc_var_t* const data,
                   void* const priv);
  • filter becomes the slot filter expression — not an if in C.

The equivalent handwritten C

The %define above is roughly equivalent to:

amxd_dm_t* dm;
amxd_object_t *sched, *task;
amxd_param_t* p;

amxd_dm_new(&dm);

amxd_object_new(&sched, amxd_object_singleton, "Scheduler");
amxd_dm_add_root_object(dm, sched);

amxd_object_new(&task, amxd_object_template, "Task");
amxd_object_add_object(sched, task);

amxd_param_new(&p, "Alias", AMXC_VAR_ID_CSTRING);
amxd_param_set_attr(p, amxd_pattr_persistent, true);
amxd_param_set_attr(p, amxd_pattr_key, true);
amxd_param_set_attr(p, amxd_pattr_unique, true);
amxd_object_add_param(task, p);

ODL is just that code written declaratively. For the manual version, see libamxd in code.

Where the parser source is

  • libamxo/src/amxo_parser_dm_utils.c: objects / instances / events;
  • libamxo/src/amxo_parser_dm_param_utils.c: parameters and attributes;
  • libamxo/src/amxo_import_resolver.c: dlopen + _<symbol> resolution;
  • libamxrt/src/amxrt.c: amxo_parser_invoke_entry_points (START/STOP).

Read these four files and every ODL keyword maps to a C call.

Debugging

  • import-dbg = true: see dlopen / symbol resolution;
  • amxrt -dconfig: see the parsed config;
  • ubus call Scheduler _describe: see the object tree/parameters/attributes;
  • symbol not found: check the C function is _xxx, and that the .so was imported.

Three resolvers

call xxx / entry-point are not direct calls — they are resolved to function pointers first:

ResolverWhat it does
auto (default)finds by naming rules
ftabfunction table; registered with amxo_resolver_ftab_add() (e.g. check_enum)
importdlopened .so, dlsym for _<symbol>

Default order (visible in -dconfig):

auto-resolver-order = ["ftab", "import", "*"]

What ODL can / cannot do

Can:

  • declare objects, parameters, functions, events, instances;
  • bind C functions (on action / call / entry-point);
  • set initial values, add instances, subscribe to events with %populate.

Cannot:

  • implement behavior (functions/actions must be in C);
  • act as a general-purpose language (it has expressions/filters, but not complex computation/loops);
  • replace business code.

ODL is “declare + bind”; C is “implement”.

Syntax cheat sheet

SyntaxPurpose
%config { ... }parser/runtime config
import "x.so"load a plugin
include "x.odl"recursive parse
?include "a":"b"optional include / fallback
%define { object ... }object tree
%populate { ... }defaults/instances/events
on action read call fread callback
on event "..." call fevent subscription
entry-point x.ystartup entry

FAQ

What is ODL?

Ambiorix's object definition language: declares the object tree and binds C functions; not a plain config file.

How do ODL and C relate?

ODL declares, C implements; on action/call/entry-point bind them.

How is ODL parsed?

amxo_parser_parse_file: %config → import → include → %define (amxd_object_new) → %populate (instances/events) → entry-point.

How does ODL relate to prplOS?

prplOS uses AMX for the data model/management plane; ODL is how that model is declared.

Why is ${name} equal to amxrt?

amxrt uses the executable name as name; use -o name=xxx or an install symlink.

Next

A bigger ODL: greeter (4 ODL files with instances/templates/async/resolver).

One sentence

ODL is not “configuration”; it is declarative source that builds the object tree and binds functions. Once you read ODL, you can see the amxd_* and amxp_* calls it generates.


Next: Ambiorix ODL parsing (2): greeter Reference: ODL handbook Examples: 02 amx-scheduler · 03 cpu-info Deep dive: How %config is parsed 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.