← All posts

Proving with Code: Why AMXD Must Exist (60 Lines)

Grow through practice

No need to finish reading — test now

The conclusion first: Once a multi-component C system shares state, it must have a “single source of truth” that lets everyone read by address, write by transaction, and be notified after a change — otherwise it falls back to global variables and mutual guessing. This post runs that sentence through code.

Counterexample: global variables

int g_enable = 1;
int g_channel = 6;

void wifi_enable(int on) { g_enable = on; }

void some_other_module(void) {
    g_channel = 11;   /* bypass the wifi module and write directly */
}

Four problems, exactly matching the four requirements in that sentence:

RequirementGlobal-variable version
Single source of truthstate is scattered across .c files; you have to remember where
Read by addressno address, only a symbol name
Write by transactiona half-failed change leaves a half-new, half-old state
Notify on changenobody knows; polling or convention only

Fine for a small project; once components multiply, “who changed it” becomes an incident investigation.

The AMXD version (runnable)

#include <stdbool.h>
#include <stdio.h>
#include <amxc/amxc.h>
#include <amxp/amxp.h>
#include <amxd/amxd_dm.h>
#include <amxd/amxd_object.h>
#include <amxd/amxd_parameter.h>
#include <amxd/amxd_object_parameter.h>
#include <amxd/amxd_transaction.h>
#include <amxd/amxd_parameter_action.h>

static amxd_status_t on_write(amxd_object_t* o, amxd_param_t* p, amxd_action_t r,
                              const amxc_var_t* a, amxc_var_t* rv, void* priv) {
    (void)o; (void)r; (void)a; (void)rv; (void)priv;
    printf("  [notify] %s changed\n", amxd_param_get_name(p));
    return amxd_status_function_not_implemented;
}

int main(void) {
    amxd_dm_t* dm = NULL;
    amxd_object_t *dev, *wifi, *ssid, *inst = NULL;
    amxd_param_t* p;

    /* single source of truth: one tree */
    amxd_dm_new(&dm);
    amxd_object_new(&dev, amxd_object_singleton, "Device");
    amxd_dm_add_root_object(dm, dev);
    amxd_object_new(&wifi, amxd_object_singleton, "WiFi");
    amxd_object_add_object(dev, wifi);
    amxd_object_new(&ssid, amxd_object_template, "SSID");
    amxd_object_add_object(wifi, ssid);

    amxd_param_new(&p, "Enable", AMXC_VAR_ID_BOOL);
    amxd_object_add_param(ssid, p);
    amxd_param_new(&p, "Channel", AMXC_VAR_ID_UINT32);
    amxd_object_add_param(ssid, p);

    amxc_var_t* vals = NULL;
    amxc_var_new(&vals);
    amxc_var_set_type(vals, AMXC_VAR_ID_HTABLE);
    amxc_var_add_key(bool, vals, "Enable", true);
    amxc_var_add_key(uint32_t, vals, "Channel", 6);
    amxd_object_add_instance(&inst, ssid, NULL, 1, vals);
    amxc_var_delete(&vals);

    /* read by address */
    amxd_object_t* o = amxd_dm_findf(dm, "Device.WiFi.SSID.1.");
    amxd_status_t st = amxd_status_unknown_error;
    printf("read  Enable=%d Channel=%u\n",
           amxd_object_get_bool(o, "Enable", &st),
           amxd_object_get_uint32_t(o, "Channel", &st));

    /* notify on change */
    amxd_param_add_action_cb(amxd_object_get_param_def(o, "Enable"),
                             action_param_write, on_write, NULL);

    /* write by transaction: commit two parameters atomically */
    amxd_trans_t* t = NULL;
    amxd_trans_new(&t);
    amxd_trans_select_object(t, o);
    amxd_trans_set_bool(t, "Enable", false);
    amxd_trans_set_uint32_t(t, "Channel", 11);
    printf("tx    rc=%d\n", amxd_trans_apply(t, dm));
    amxd_trans_delete(&t);

    printf("after Enable=%d Channel=%u\n",
           amxd_object_get_bool(o, "Enable", &st),
           amxd_object_get_uint32_t(o, "Channel", &st));

    amxd_dm_delete(&dm);
    return 0;
}

Build and run:

cc amxd_why.c -lamxd -lamxc -lamxp -o amxd_why && ./amxd_why

Measured output:

read  Enable=1 Channel=6
  [notify] Enable changed
tx    rc=0
after Enable=0 Channel=11

Four things, line by line

That sentenceCode
Single source of truthamxd_dm_new + one Device.WiFi.SSID tree
Read by addressamxd_dm_findf(dm, "Device.WiFi.SSID.1.") + amxd_object_get_*
Write by transactionamxd_trans_set_bool/uint32_t + amxd_trans_apply (both parameters take effect together)
Notify on changeamxd_param_add_action_cb → [notify] Enable changed
Otherwisethe g_enable / g_channel counterexample above

Boundaries

  • A small program with one module and one maintainer: globals/statics are perfectly fine; do not bring in a model.
  • The trigger is “shared + remote + long-lived”: multiple components read/write the same state, remote management is required (ubus/TR-181/USP), and fields keep being added — then “single source of truth” turns from a nicety into a necessity.

One sentence

AMXD does not make code “advanced”; it makes “who changed the state” answerable. Global variables give you speed; a model gives you traceability — and as components multiply, the latter is more expensive to lose.


Conceptual summary: What AMXD Means Full API version: libamxd in practice

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.