openwrt设置语言的过程

本文详细介绍了LuCI系统的配置文件结构及语言设置方法,并解释了LuCI在OpenWrt平台上的启动流程,包括web服务器uHTTPd如何加载LuCI以及LuCI内部的调度过程。

设置语言的流程
一、关联的配置文件
/etc/config/luci
查看配置文件内容如下:
root@hbg:/# cat /etc/config/luci

config core 'main'
        option mediaurlbase '/luci-static/openwrt.org'
        option resourcebase '/luci-static/resources'
        option lang 'zh_cn'     // 目前使用的语言zh_cn,中文

config extern 'flash_keep'
        option uci '/etc/config/'
        option dropbear '/etc/dropbear/'
        option openvpn '/etc/openvpn/'
        option passwd '/etc/passwd'
        option opkg '/etc/opkg.conf'
        option firewall '/etc/firewall.user'
        option uploads '/lib/uci/upload/'

config internal 'languages'    // 语言选项
        option zh_cn 'chinese' // 中文
        option en 'English'    // 英语

config internal 'sauth'
        option sessionpath '/tmp/luci-sessions'
        option sessiontime '3600'

config internal 'ccache'
        option enable '1'

config internal 'themes'
        option Bootstrap '/luci-static/bootstrap'
  
二、调用过程 

在dispatcher.lua中httpdispatch() 函数中调用了函数dispatch(),
在dispatch()函数中实现:

        local conf = require "luci.config"    // 配置文件为/etc/config/luci
        assert(conf.main,                     // 查看配置文件中是否有main段,若无,提示错误。
                "/etc/config/luci seems to be corrupt, unable to find section 'main'")

        local lang = conf.main.lang

  if lang == nil then
   lang = "zh_cn"
            luci.sys.call("uci set luci.main.lang=zh_cn; uci commit luci")   
        end   

        require "luci.i18n".setlanguage(lang)  // 设置语言

查找调用dispatch()的地方,可以找到:
1)cgi.lua 
2)uhttpd.lua
两个脚本都位 ./feeds/luci/modules/base/luasrc/sgi目录下,
并且都是调用这句
local x = coroutine.create(luci.dispatcher.httpdispatch)
调用cgi.lua脚本的地方为./feeds/luci/modules/base/htdocs/cgi-bin/luci
文件内容如下:
#!/usr/bin/lua
require "luci.cacheloader"
require "luci.sgi.cgi"    // 包含头文件cgi.lua
luci.dispatcher.indexcache = "/tmp/luci-indexcache"
luci.sgi.cgi.run()        // 调用cgi.lua中的run函数。

三、luci的启动-uhttpd
uhttpd是一个简单的web服务器程序,主要就是cgi的处理,openwrt是利用uhttpd作为web服务器,
实现客户端web页面配置功能。对于request处理方式,采用的是cgi,而所用的cgi程序就是luci.

四、luci的启动-luci
在web server中的cgi-bin目录下,运行 luci 文件(权限一般是 755,luci的代码如下:

  #!/usr/bin/lua      --cgi的执行命令的路径

  require"luci.cacheloader"    --导入cacheloader包

  require"luci.sgi.cgi"         --导入sgi.cgi包 

  luci.dispatcher.indexcache = "/tmp/luci-indexcache"  --cache缓存路径地址

  luci.sgi.cgi.run()  --执行run,此方法位于*/luci/sgi/cgi.lua

五、Luci-- Web
  a.登录

  输入: http://x.x.x.x/ 登录LuCI.

    调用 /www/cgi-bin/luci.

  b. 进入主菜单‘status’

  输入: http://x.x.x.x/cgi-bin/luci/admin/status/即可访问status页面。
    Luci则会调用 /luci/admin/目录下的status.lua脚本:

  module("luci.controller.admin.status", package.seeall)

  /usr/lib/lua/luci/controller/admin/status.lua->index()

 

转载于:https://www.cnblogs.com/rohens-hbg/p/5363364.html

### OpenWrt 中 ubus 的注册过程OpenWrt 平台中,ubus 是一种高效的进程间通信机制。为了使服务能够在 ubus 上正常工作并被其他客户端访问,必须先完成注册过程。 当一个新对象想要加入到 ubus 服务器时,它会向 `ubusd` 发送请求来创建一个新的上下文环境[^1]。这个过程中涉及到的主要函数有: - **初始化连接**:通过调用 `ubus_connect()` 函数建立与 ubus daemon (`ubusd`) 的连接。 ```c struct ubus_context *ctx; ctx = ubus_connect(NULL); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); exit(1); } ``` - **定义回调处理程序**:设置好事件监听器以及数据接收后的处理器,这通常是在应用程序启动初期完成的工作之一。例如,在 C 语言中可以通过如下方式指定方法响应函数: ```c static const struct blobmsg_policy my_object_policy[] = {}; static int handle_my_method(struct ubus_context *ctx, uint32_t objid, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { // 方法的具体逻辑实现... } static const struct ubus_method my_methods[] = UBUS_METHOD("mymethod", handle_my_method, my_object_policy); static struct ubus_object_type my_obj_type = UBUS_OBJECT_TYPE("myobjectname", my_methods); static struct ubus_object my_object = { .type = &my_obj_type, .methods = my_methods, .n_methods = ARRAY_SIZE(my_methods), }; ``` - **注册对象至 ubus**:一旦准备好上述组件之后,则可以利用 `ubus_add_object(ctx, &my_object)` 将该对象正式添加进 ubus 系统内供其它模块发现和交互使用了[^3]。 以上就是整个 ubus 对象的注册流程概述,实际应用可能还会涉及更多细节配置取决于具体需求场景下的功能扩展情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值