← 全部文章

AMX 怎么调用 UCI:CLI、libuci、后端模块

在练习中成长

不用等读完,先测一下

AMX 里“调用 UCI”不是一个 API,而是三种不同层次的集成方式。 选错层次,要么写成脚本补丁,要么把事件循环堵死。

方式 1:Shell

最快,适合调试和启动脚本:

uci set network.lan.ipaddr='192.168.1.1'
uci commit network
/etc/init.d/network reload

要点:

  • set 先写 /tmp/.uci;
  • commit 才写 /etc/config/network;
  • reload 让服务重新读取,不等于重新加载内核模块。

方式 2:C + libuci

后端模块里直接调用 libuci:

#include <uci.h>

static int set_lan_ip(const char *value)
{
    struct uci_context *ctx = uci_alloc_context();
    struct uci_ptr ptr;
    int rc = -1;

    if (uci_lookup_ptr(ctx, &ptr, "network.lan.ipaddr", true) != UCI_OK)
        goto out;

    ptr.value = value;
    if (uci_set(ctx, &ptr) != UCI_OK)
        goto out;
    if (uci_commit(ctx, &ptr.pkg, false) != UCI_OK)
        goto out;

    rc = 0;
out:
    uci_free_context(ctx);
    return rc;
}

注意:

  • uci_set 不保证立刻落盘;
  • commit 需要 package 指针;
  • 新 option 可能需要先 uci_add/uci_set 建出来;
  • 不要在 uloop 回调里 system("uci ...");libuci 的文件 I/O 也要控制频率。

方式 3:AMX UCI 后端模块

这是 prplOS 的正常做法:

TR-181 对象变化
  → UCI 后端模块(监听 dm:object-changed)
  → libuci set/commit
  → /etc/config/network
  → netifd 生效

例如 tr181-routing 使用 mod-routing-uci 处理静态/OS 层路由;BGP/OSPF 则由 mod-routing-bird2 处理。ODL 只定义模型,不负责执行 UCI 命令。

一个模块的核心职责:

  1. 监听对象/参数变化;
  1. 把 TR-181 语义翻译成 UCI section/option;
  1. 调 libuci 提交;
  1. 处理失败、重试和回滚边界。

什么时候用哪种

场景选择
临时调试shell uci
自定义 C 服务libuci
prplOS 管理面AMX + UCI 后端模块
uloop 回调不要阻塞,异步/延后提交

系列:prplOS 与 AMX 相关:AMX 数据模型存在哪 · UCI 后端案例 · 从 UCI 到数据模型

本页练习

觉得有用?

我持续记录 AI Agent × 嵌入式 / 边缘 AI 的实践。订阅更新,或来邮件交流一个具体问题。

去留言板 →订阅 RSS赞助

评论 0

还没有评论,来抢沙发~

无需登录即可评论;昵称可选。