Development of Luci modules on OpenWrt routers

Source: Internet
Author: User

"One, the framework of Luci Configuration Interface Development"

Luci is the Web management interface on OpenWrt, Luci uses the MVC three layer architecture, and it is developed using LUA scripting, so the development Luci configuration interface does not need to edit any HTML code, unless you want to create a Web page (view layer) alone, Otherwise, basically we just need to modify the model layer. The official also has a description of how to create the module, although the writing is more obscure: Http://luci.subsignal.org/trac/wiki/Documentation/ModulesHowTo

To add a new module to the Luci, you first need to create two files, one at the controller (/usr/lib/lua/luci/controller/), the module's entry, and the other in model (/usr/lib/lua/luci/ model/cbi/), the actual code for the configuration module.

First we define the module's entry and create a LUA file under/usr/lib/lua/luci/controller/, similar to the following:

Module ("Luci.controller. Controller name", Package.seeall) function index ()        entry (path, call target, _ ("Display Name"), display order)        end

The first line describes the program and module name, such as in the controller/directory to create a Mymodule.lua, then can be written as "Luci.controller.mymodule", if your program is more, may be divided into several modules, Then a subdirectory can be common under the controller, such as controller/myapp/, then you can write "Luci.controller.myapp.mymodule".

The next entry is to add a new module entry, and the official gives the definition of entry, the last two of which can be empty:

Entry (path, target, Title=nil, Order=nil)

The first entry is the path of the access, but the path is given as a string array, such as the path is written as "{" "click", "Here" and "Now"}, then you can access the "Http://192.168.1.1/cgi-bin/luci/click" in the browser. /here/now "To access this script. And usually we want to add a script for the admin menu, then we need to write "{" admin "," first menu Name "," menu item Name "}" as follows, and the system will automatically generate the menu item in the corresponding menu. " For example, if you want to create a menu item under the "Network" menu, the first-level dish sole name can be written as ' network '.

The second is the invocation target, which is divided into three types, namely executing the specified method (Action), accessing the specified page (views), and invoking the CBI Module.

    • The first can call the specified function directly, such as clicking on a menu item to restart the router directly, such as "Call (" Function_name ")", and then write a function named Function_name under the Lua file can be called.
    • The second one can access the specified page, such as "Template (" Myapp/mymodule ") to invoke the/usr/lib/lua/luci/view/myapp/mymodule.htm file.
    • If you want to write a configuration page, then using the third method is the most convenient, such as "CBI (" Myapp/mymodule ")" Can call the/usr/lib/lua/luci/model/cbi/myapp/mymodule.lua file.

While title and order are for the Administrator menu, you can refer to other LUA files to decide what to write.

Here we create the/usr/lib/lua/luci/controller/njitclient.lua file, define our portal, the code is as follows:

Module ("Luci.controller.njitclient", Package.seeall) function index ()        entry ({"admin", "Network", "Njitclient"}, CBI ("Njitclient"), _ ("NJIT Client"),        end

"Ii. development of the Luci configuration module with Lua and UCI interfaces "

What we want to do is actually want to be able to store the user name, password and other information in the router file, while the router can start automatically run Njit-client according to the configured configuration, and we also want to be able to dynamically disable and enable Njit-client and so on. So the easiest way to do this is to use the CBI Module, which we added in the previous section, and then we'll create the/usr/lib/lua/luci/model/cbi/njitclient.lua file based on the path written above.

There are many ways to develop the Luci configuration module, compared with the basic can be used simpleform, similar to the development of ordinary Web applications, of course, the most convenient is the use of UCI (Unified configuration Interface, unified interface) way, Because using the UCI interface allows the Luci to be free from the need to consider how the configuration file is stored and read (This also automatically creates "Save & Apply", "Save" and "Reset" three buttons), which can also be easily stored and read in bash files.

For the use of UCI, we first need to create the corresponding configuration file (if the configuration file does not exist, the Access Configuration page will be error), the format is the Linux configuration file format, the file needs to be stored in the/etc/config, such as the file path is "/etc/config/ Njitclient ", the contents are as follows:

Config login    option username '    option password ' option    ifname ' eth0 '    option domain '

Then we need to map the relationship to the storage file first in the CBI module's LUA file, such as:

m = Map ("Profile file name", "Configuration page title", "Configuration page description")

The first parameter is the file name stored in the configuration file, not including the path, such as created by the above, should be written as "Njitclient", and the second and third parameters are used on the page to display, such as the following diagram:

Next you need to create the corresponding section,section in the configuration file into two types, namedsection and Typedsection, the former based on the section name in the configuration file, the latter based on the section type in the configuration file, here we use the , the code is as follows. At the same time we set not to allow adding or removing sections (". AddRemove = False") and not showing the section's name (". Anonymous = True").

s = m:section (typedsection, "Login", "") S.addremove = Falses.anonymous = True

Next we need to create the interaction of different content in the section (create option), such as the value (text box), ListValue (dropdown box), Flag (selection box), and so on, in detail can refer to the Official document:/HTTP Luci.subsignal.org/trac/wiki/documentation/cbi

The process of creating the option is very simple, and the system will automatically handle the problem of reading and writing to the configuration file after creation. However, according to the above requirements, we may want to enable, disable, or restart njit-client after the configuration is applied, so we also need to determine at the end of the page whether the user clicked the "Apply" button, which is the same as the ASP page, and so on, we can use the following code to determine whether or not clicked " Apply button:

Local apply = Luci.http.formvalue ("cbi.apply") if apply then    --[[        code that needs to be processed    ]]--end

Because the rest of the code is very simple, so this part of the entire code is shown below:

1 require ("Luci.sys") 2  3 m = Map ("Njitclient", Translate ("NJIT client"), translate ("Configure NJIT 802.11x client.") ) 4  5 s = m:section (typedsection, "Login", "") 6 S.addremove = False 7 S.anonymous = True 8  9 enable = S:option (Fl AG, "Enable", Translate ("Enable")), name = S:option (value, "username", translate ("username")) One pass = s:option (value, " Password ", Translate (" password ")) Pass.password = true13 domain = s:option (Value," domain ", translate (" domain "))  fname = S:option (ListValue, "ifname", Translate ("Interfaces")) for K, V in Ipairs (Luci.sys.net.devices ()) Do17     if V ~= "Lo" then18         ifname:value (v)     end20 end21, local apply = Luci.http.formvalue ("cbi.apply") if apply then24< C6/>io.popen ("/etc/init.d/njitclient restart") end26 return m

where Luci all class library function definition and usage instructions can refer to the following address: http://luci.subsignal.org/api/luci/index.html

"Iii. calling the UCI interface in a bash file"

Above we have completed the development of the Luci Configuration interface, in the configuration interface we have been able to read and save the configuration file. Next we will write the/etc/init.d/njitclient script, so that the program can eventually run. For an official description of the UCI interface in the script file, refer to: http://wiki.openwrt.org/doc/devel/config-scripting

To use the UCI invoke script, the first step is to read the configuration file, the command is "config_load config filename", for example, we can read into the configuration file just now:

Config_load njitclient

Next, to traverse the section in the configuration file, you can use the "Config_foreach Traversal Function Name section type", for example we can:

Config_foreach Run_njit Login

Then we'll write a function called "Run_njit", in which we can get the value of a variable using the "Config_get Variable name section name of the section parameter name", or use the "Config_get_bool variable name, section name The section parameter name "Gets the value of the Boolean type. So the entire code is shown below:

1 #!/bin/sh/etc/rc.common 2 start=50 3  4 Run_njit () 5 {6     local enable 7     Config_get_bool enable enable 8
   9     if [$enable]; then10         local username11         local password12         local domain13         local ifname14         15         config_get username $ username16         config_get password $ password17         config_get domain $ domain18         CONFIG_GET ifname $ ifname19         If ["$domain"! = ""]; Then21             njit-client [email protected] $domain $password $ifname &22         else23             njit-client $username $ Password $ifname &24         fi25         echo "NJIT Client has started."     fi28}29 () {     config_load njitclient33     config_foreach Run_njit login34}35 37 Stop () {38< C25/>killall njit-client39     killall udhcpc40     echo "Njit Client has stoped." 42}

"Iv. procedures for compiling and developing "

If the above 4 files are created as described above, then the configuration page and program will be able to run on the OpenWrt. But if you want to package your own program, you also need to create a openwrt makefile to compile with the OpenWrt SDK.

For official instructions on configuring makefile on Luci, see this address: http://luci.subsignal.org/trac/wiki/Documentation/Modules

is to define the package name (Pkg_name), version and number of builds (Pkg_version, pkg_release), classification descriptions in Menuconfig, etc. (define package/ Luci-app-njitclient) and the installation (define Package/luci-app-njitclient/install) and so on. There are three types of files installed, namely, configuration files, executables, and other data files, which are automatically added to execute permissions when the executable is configured, so no additional processing is required. Makefile all the code see below:

 1 include $ (topdir)/rules.mk 2 3 pkg_name:=luci-app-njitclient 4 pkg_version=1.0 5 pkg_release:=1 6 7 pkg_build_dir:=$ ( Build_dir)/$ (pkg_name) 8 9 include $ (include_dir)/package.mk10 one define package/luci-app-njitclient12 section:=luci1 3 category:=luci14 submenu:=3. APPLICATIONS15 Title:=njit 802.1X Client for LuCI16 pkgarch:=all17 endef18 define PACKAGE/LUCI-APP-NJITCLIENT/D Escription20 the contains LuCI configuration pages for njit8021xclient.21 endef22 define BUILD/PREPARE24 E Ndef25 define Build/configure27 endef28 define BUILD/COMPILE30 endef31-Define Package/luci-app-njitclient/instal L33 $ (Install_dir) $ (1)/etc/config34 $ (install_dir) $ (1)/etc/init.d35 $ (install_dir) $ (1)/usr/lib/lua/luci/mod El/cbi36 $ (Install_dir) $ (1)/usr/lib/lua/luci/controller37 $ (install_conf)./files/root/etc/config/njitclie NT $ (1)/etc/config/njitclient39 $ (install_bin)./files/root/etc/init.d/njitclient $ (1)/ETC/INIT.D/NJITCLIENT40 $ (install_data)./files/root/usr/lib/lua/luci/model/cbi/njitclient.lua $ (1)/usr/lib/lua/luci/model/cbi/ Njitclient.lua41 $ (install_data)./files/root/usr/lib/lua/luci/controller/njitclient.lua $ (1)/usr/lib/lua/luci/ Controller/njitclient.lua42 endef43 $ (eval $ (call buildpackage,luci-app-njitclient))

Development of Luci modules on OpenWrt routers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.