luci-app-bmx6: return if json problem detected
[lede-routing/.git] / luci-app-bmx6 / files / usr / lib / lua / luci / model / cbi / bmx6 / main.lua
1 --[[
2     Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18     The full GNU General Public License is included in this distribution in
19     the file called "COPYING".
20 --]]
21
22 local sys = require("luci.sys")
23 local bmx6json = require("luci.model.bmx6json")
24
25 m = Map("bmx6", "bmx6")
26
27 -- Getting json and Checking if bmx6-json is avaiable
28 local options = bmx6json.get("options")
29 if options == nil or options.OPTIONS == nil then
30         m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
31         return m
32 else
33         options = options.OPTIONS
34 end
35
36 -- Getting a list of interfaces
37 local eth_int = luci.sys.net.devices()
38
39 local tunDev = m:section(TypedSection,"tunDev",translate("Tunnel device"),translate("Define incoming ipip tunnel interface name"))
40 tunDev.addremove = true
41 tunDev.anonymous = true
42 tunDev:option(Value,"tunDev",translate("Name"),translate("Name for the tunnel network device"))
43 tunDev:option(Value,"tun4Address", translate("IPv4 address/length"),translate("Specify default IPv4 tunnel address and announced range (ex. 10.1.2.3/24)"))
44 tunDev:option(Value,"tun6Address", translate("IPv6 address/length"),translate("Specify default IPv6 tunnel address and announced range (ex. 2012:0:0:123:0:0:0:1/64)"))
45
46 -- IP section
47 local ipV = m:section(NamedSection,"ipVersion","ipVersion",translate("Miscellaneous IP options"))
48 ipV.addremove = false
49 local lipv = ipV:option(ListValue,"ipVersion",translate("IP version"))
50 lipv:value("6","6")
51 lipv.default = "6"
52
53 -- rest of ip options are optional, getting them from json
54 local ipoptions = {}
55 for _,o in ipairs(options) do
56         if o.name == "ipVersion" and o.CHILD_OPTIONS ~= nil then
57                 ipoptions = o.CHILD_OPTIONS
58                 break
59         end
60 end
61
62 local help = ""
63 local name = ""
64 local value = nil
65
66 for _,o in ipairs(ipoptions) do
67         if o.name ~= nil then
68                 help = ""
69                 name = o.name
70                 if o.help ~= nil then
71                         help = bmx6json.text2html(o.help)
72                 end
73
74                 if o.syntax ~= nil then
75                         help = help .. "<br/><strong>Syntax: </strong>" .. bmx6json.text2html(o.syntax)
76                 end
77
78                 if o.def ~= nil then
79                         help = help .. "<br/><strong> Default: </strong>" .. bmx6json.text2html(o.def)
80                 end
81
82                 value = ipV:option(Value,name,name,help)
83                 value.optional = true
84         end
85 end
86
87 -- Interfaces section
88 local interfaces = m:section(TypedSection,"dev",translate("Devices"),translate("Network devices to mesh with"))
89 interfaces.addremove = true
90 interfaces.anonymous = true
91 local intlv = interfaces:option(ListValue,"dev",translate("Device"))
92
93 for _,i in ipairs(eth_int) do
94         intlv:value(i,i)
95 end
96
97 function m.on_commit(self,map)
98     local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
99     if err ~= 0 then
100         m.message = sys.exec("cat /tmp/bmx6-luci.err.tmp")
101     end
102 end
103
104 return m
105