b701309efc79ebd5cdf8f50aa6ff89a184fcd691
[feed/routing.git] / files / usr / lib / lua / luci / controller / bmx6.lua
1 --[[
2 Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3 Contributors Jo-Philipp Wich <xm@subsignal.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21 --]]
22
23 local bmx6json = require("luci.model.bmx6json")
24
25 module("luci.controller.bmx6", package.seeall)
26
27 function index()
28 local place = {}
29 local ucim = require "luci.model.uci"
30 local uci = ucim.cursor()
31 -- checking if ignore is on
32 if uci:get("luci-bmx6","luci","ignore") == "1" then
33 return nil
34 end
35
36 -- getting value from uci database
37 local uci_place = uci:get("luci-bmx6","luci","place")
38
39 -- default values
40 if uci_place == nil then
41 place = {"bmx6"}
42 else
43 local util = require "luci.util"
44 place = util.split(uci_place," ")
45 end
46 ---------------------------
47 -- Starting with the pages
48 ---------------------------
49
50 --- neighbours/descriptions (default)
51 entry(place,call("action_neighbours_j"),place[#place])
52
53 table.insert(place,"neighbours_nojs")
54 entry(place, call("action_neighbours"), nil)
55 table.remove(place)
56
57 --- status (this is default one)
58 table.insert(place,"Status")
59 entry(place,call("action_status"),"Status")
60 table.remove(place)
61
62 --- links
63 table.insert(place,"Links")
64 entry(place,call("action_links"),"Links").leaf = true
65 table.remove(place)
66
67 -- Gateways
68 table.insert(place,"Gateways")
69 entry(place,call("action_gateways_j"),"Gateways").leaf = true
70 table.remove(place)
71
72 --- chat
73 table.insert(place,"Chat")
74 entry(place,call("action_chat"),"Chat")
75 table.remove(place)
76
77 --- Graph
78 table.insert(place,"Graph")
79 entry(place, template("bmx6/graph"), "Graph")
80 table.remove(place)
81
82 --- Topology (hidden)
83 table.insert(place,"topology")
84 entry(place, call("action_topology"), nil)
85 table.remove(place)
86
87 --- configuration (CBI)
88 table.insert(place,"Configuration")
89 entry(place, cbi("bmx6/main"), "Configuration").dependent=false
90
91 table.insert(place,"Advanced")
92 entry(place, cbi("bmx6/advanced"), "Advanced")
93 table.remove(place)
94
95 table.insert(place,"Interfaces")
96 entry(place, cbi("bmx6/interfaces"), "Interfaces")
97 table.remove(place)
98
99 table.insert(place,"Plugins")
100 entry(place, cbi("bmx6/plugins"), "Plugins")
101 table.remove(place)
102
103 table.insert(place,"HNA")
104 entry(place, cbi("bmx6/hna"), "HNA")
105 table.remove(place)
106
107 table.remove(place)
108
109 end
110
111 function action_status()
112 local status = bmx6json.get("status").status or nil
113 local interfaces = bmx6json.get("interfaces").interfaces or nil
114
115 if status == nil or interfaces == nil then
116 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
117 else
118 luci.template.render("bmx6/status", {status=status,interfaces=interfaces})
119 end
120 end
121
122 function action_neighbours()
123 local orig_list = bmx6json.get("originators").originators or nil
124
125 if orig_list == nil then
126 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
127 return nil
128 end
129
130 local originators = {}
131 local desc = nil
132 local orig = nil
133 local name = ""
134 local ipv4 = ""
135
136 for _,o in ipairs(orig_list) do
137 orig = bmx6json.get("originators/"..o.name) or {}
138 desc = bmx6json.get("descriptions/"..o.name) or {}
139
140 if string.find(o.name,'.') then
141 name = luci.util.split(o.name,'.')[1]
142 else
143 name = o.name
144 end
145
146 --Not sure about that, but trying to find main ipv4 from HNA6 published by each node
147 if desc.DESC_ADV ~= nil then
148 for _,h in ipairs(desc.DESC_ADV.extensions[2].HNA6_EXTENSION) do
149
150 if h ~= nil and string.find(h.address,"::ffff:") then
151 ipv4=string.gsub(h.address,"::ffff:","")
152 break
153 end
154 end
155 end
156
157 if ipv4 == "" then
158 ipv4="0.0.0.0"
159 end
160
161 table.insert(originators,{name=name,ipv4=ipv4,orig=orig,desc=desc})
162 end
163
164 luci.template.render("bmx6/neighbours", {originators=originators})
165 end
166
167 function action_neighbours_j()
168 local http = require "luci.http"
169 local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/neighbours_nojs'
170
171 luci.template.render("bmx6/neighbours_j", {link_non_js=link_non_js})
172 end
173
174 function action_gateways_j()
175 luci.template.render("bmx6/gateways_j", {})
176 end
177
178
179 function action_links(host)
180 local links = bmx6json.get("links", host)
181 local devlinks = {}
182 local _,l
183
184 if links ~= nil then
185 links = links.links
186 for _,l in ipairs(links) do
187 devlinks[l.viaDev] = {}
188 end
189 for _,l in ipairs(links) do
190 l.globalId = luci.util.split(l.globalId,'.')[1]
191 table.insert(devlinks[l.viaDev],l)
192 end
193 end
194
195 luci.template.render("bmx6/links", {links=devlinks})
196 end
197
198 function action_topology()
199 local originators = bmx6json.get("originators/all")
200 local o,i,l,i2
201 local first = true
202 luci.http.prepare_content("application/json")
203 luci.http.write('[ ')
204
205 for i,o in ipairs(originators) do
206 local links = bmx6json.get("links",o.primaryIp)
207 if links then
208 if first then
209 first = false
210 else
211 luci.http.write(', ')
212 end
213
214 luci.http.write('{ "globalId": "%s", "links": [' %o.globalId:match("^[^%.]+"))
215
216 local first2 = true
217
218 for i2,l in ipairs(links.links) do
219 if first2 then
220 first2 = false
221 else
222 luci.http.write(', ')
223 end
224
225 luci.http.write('{ "globalId": "%s", "rxRate": %s, "txRate": %s }'
226 %{ l.globalId:match("^[^%.]+"), l.rxRate, l.txRate })
227
228 end
229
230 luci.http.write(']}')
231 end
232
233 end
234 luci.http.write(' ]')
235 end
236
237
238 function action_chat()
239 local sms_dir = "/var/run/bmx6/sms"
240 local rcvd_dir = sms_dir .. "/rcvdSms"
241 local send_file = sms_dir .. "/sendSms/chat"
242 local sms_list = bmx6json.get("rcvdSms")
243 local sender = ""
244 local sms_file = ""
245 local chat = {}
246 local to_send = nil
247 local sent = ""
248 local fd = nil
249
250 if luci.sys.call("test -d " .. sms_dir) ~= 0 then
251 luci.template.render("bmx6/error", {txt="sms plugin disabled or some problem with directory " .. sms_dir})
252 return nil
253 end
254
255 sms_list = luci.util.split(luci.util.exec("ls "..rcvd_dir.."/*:chat"))
256
257 for _,sms_path in ipairs(sms_list) do
258 if #sms_path > #rcvd_dir then
259 sms_file = luci.util.split(sms_path,'/')
260 sms_file = sms_file[#sms_file]
261 sender = luci.util.split(sms_file,':')[1]
262
263 -- Trying to clean the name
264 if string.find(sender,".") ~= nil then
265 sender = luci.util.split(sender,".")[1]
266 end
267
268 fd = io.open(sms_path,"r")
269 chat[sender] = fd:read()
270 fd:close()
271 end
272 end
273
274 to_send = luci.http.formvalue("toSend")
275 if to_send ~= nil and #to_send > 1 then
276 fd = io.open(send_file,"w")
277 fd:write(to_send)
278 fd:close()
279 sent = to_send
280 else
281 sent = luci.util.exec("cat "..send_file)
282 end
283
284 luci.template.render("bmx6/chat", {chat=chat,sent=sent})
285 end
286