1module("luci.controller.admin.thread", package.seeall) 2 3function index() 4 page = entry({"admin", "network", "thread"}, template("admin_thread/thread_overview"), translate("Thread"), 16) 5 page.leaf = true 6 7 page = entry({"admin", "network", "thread_state"}, call("thread_state"), nil) 8 page.leaf = true 9 10 page = entry({"admin", "network", "thread_neighbors"}, call("thread_neighbors"), nil) 11 page.leaf = true 12 13 page = entry({"admin", "network", "thread_graph"}, call("thread_graph"), nil) 14 page.leaf = true 15 16 page = entry({"admin", "network", "thread_scan"}, template("admin_thread/thread_scan"), nil) 17 page.leaf = true 18 19 page = entry({"admin", "network", "thread_create"}, template("admin_thread/thread_setting"), nil) 20 page.leaf = true 21 22 page = entry({"admin", "network", "thread_setting"}, template("admin_thread/thread_setting"), nil) 23 page.leaf = true 24 25 page = entry({"admin", "network", "thread_add"}, post("thread_add"), nil) 26 page.leaf = true 27 28 page = entry({"admin", "network", "thread_add_page"}, template("admin_thread/thread_add"), nil) 29 page.leaf = true 30 31 page = entry({"admin", "network", "thread_view"}, template("admin_thread/thread_view"), nil) 32 page.leaf = true 33 34 page = entry({"admin", "network", "thread_join"}, template("admin_thread/thread_join"), nil) 35 page.leaf = true 36 37 page = entry({"admin", "network", "joiner_remove"}, post("joiner_remove"), nil) 38 page.leaf = true 39 40 page = entry({"admin", "network", "thread_attach"}, post("thread_attach"), nil) 41 page.leaf = true 42 43 page = entry({"admin", "network", "thread_add_joiner"}, post("thread_add_joiner"), nil) 44 page.leaf = true 45 46 page = entry({"admin", "network", "thread_handler_setting"}, post("thread_handler_setting"), nil) 47 page.leaf = true 48 49 page = entry({"admin", "network", "thread_stop"}, post("thread_stop"), nil) 50 page.leaf = true 51end 52 53function thread_handler_setting() 54 local ubus = require "ubus" 55 local tpl = require "luci.template" 56 local http = require "luci.http" 57 local networkname = luci.http.formvalue("threadname") 58 local channel = luci.http.formvalue("channel") + 0 59 local panid = luci.http.formvalue("panid") 60 local extpanid = luci.http.formvalue("extpanid") 61 local mode = luci.http.formvalue("mode") 62 local networkkey = luci.http.formvalue("networkkey") 63 local pskc = luci.http.formvalue("pskc") 64 local macfilter = luci.http.formvalue("macfilterselect") 65 local macfilteradd = luci.http.formvalue("macfilteradd") 66 local macfilterremove = luci.http.formvalue("macfilterremove") 67 local submitcontent = luci.http.formvalue("submitcontent") 68 local jumpflag = 0 69 70 local vError = 0 71 local result 72 73 local conn = ubus.connect() 74 75 if not conn then 76 error("Failed to connect to ubusd") 77 end 78 79 if submitcontent == "enable" then 80 result = conn:call("otbr", "threadstart", {}) 81 vError = result.Error 82 elseif submitcontent == "disable" then 83 result = conn:call("otbr", "threadstop", {}) 84 vError = result.Error 85 elseif submitcontent == "leave" then 86 result = conn:call("otbr", "leave", {}) 87 vError = result.Error 88 elseif submitcontent == "clearAddr" then 89 result = conn:call("otbr", "macfilterclear", {}) 90 vError = result.Error 91 jumpflag = 1 92 elseif submitcontent == "addAddr" then 93 result = conn:call("otbr", "macfilteradd", { addr = macfilteradd }) 94 conn:call("otbr", "macfiltersetstate", { state = macfilter }) 95 vError = result.Error 96 jumpflag = 1 97 elseif submitcontent == "removeAddr" then 98 if type(macfilterremove) == "table" then 99 local removeAddrIndex = luci.http.formvalue("removeAddrIndex") + 0 100 result = conn:call("otbr", "macfilterremove", { addr = macfilterremove[removeAddrIndex] }) 101 else 102 result = conn:call("otbr", "macfilterremove", { addr = macfilterremove }) 103 end 104 conn:call("otbr", "macfiltersetstate", { state = macfilter }) 105 vError = result.Error 106 jumpflag = 1 107 else 108 if(threadget("state").State == "disabled")then 109 conn:call("otbr", "setnetworkname", { networkname = networkname }) 110 conn:call("otbr", "setchannel", { channel = channel }) 111 conn:call("otbr", "setpanid", { panid = panid }) 112 conn:call("otbr", "setextpanid", { extpanid = extpanid }) 113 conn:call("otbr", "setmode", { mode = mode }) 114 conn:call("otbr", "setnetworkkey", { networkkey = networkkey }) 115 conn:call("otbr", "setpskc", { pskc = pskc }) 116 conn:call("otbr", "macfiltersetstate", { state = macfilter }) 117 result = conn:call("otbr", "threadstart", {}) 118 else 119 result = conn:call("otbr", "mgmtset", { networkkey = networkkey, networkname = networkname, extpanid = extpanid, panid = panid, channel = tostring(channel), pskc = pskc }) 120 conn:call("otbr", "macfiltersetstate", { state = macfilter }) 121 end 122 vError = result.Error 123 end 124 125 if(jumpflag == 0) then 126 local stat, dsp = pcall(require, "luci.dispatcher") 127 stat = 0 128 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread") .. "?error=" .. vError) 129 else 130 local stat, dsp = pcall(require, "luci.dispatcher") 131 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread_setting") .. "?error=" .. vError) 132 end 133end 134 135function thread_add() 136 local ubus = require "ubus" 137 local tpl = require "luci.template" 138 local http = require "luci.http" 139 140 local conn = ubus.connect() 141 142 if not conn then 143 error("Failed to connect to ubusd") 144 end 145 146 result = conn:call("otbr", "commissionerstart", {}) 147 local vError = result.Error 148 149 local stat, dsp = pcall(require, "luci.dispatcher") 150 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread_add_page") .. "?error=" .. vError) 151end 152 153function thread_add_joiner() 154 local ubus = require "ubus" 155 local tpl = require "luci.template" 156 local http = require "luci.http" 157 local pskd = luci.http.formvalue("pskd") 158 local eui64 = luci.http.formvalue("eui64") 159 160 local conn = ubus.connect() 161 162 if not conn then 163 error("Failed to connect to ubusd") 164 end 165 166 result = conn:call("otbr", "joineradd", { pskd = pskd, eui64 = eui64}) 167 vError = result.Error 168 169 local stat, dsp = pcall(require, "luci.dispatcher") 170 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread_view") .. "?error=" .. vError) 171end 172 173function joiner_remove() 174 local ubus = require "ubus" 175 local tpl = require "luci.template" 176 local http = require "luci.http" 177 local eui64 = luci.http.formvalue("eui64") 178 179 local conn = ubus.connect() 180 181 if not conn then 182 error("Failed to connect to ubusd") 183 end 184 185 result = conn:call("otbr", "joinerremove", { eui64 = eui64 }) 186 vError = result.Error 187 188 local stat, dsp = pcall(require, "luci.dispatcher") 189 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread_view") .. "?error=" .. vError) 190end 191 192function thread_attach() 193 local ubus = require "ubus" 194 local tpl = require "luci.template" 195 local http = require "luci.http" 196 local panid = luci.http.formvalue("panid") 197 local channel = luci.http.formvalue("channel") + 0 198 local networkkey = luci.http.formvalue("networkkey") 199 200 local conn = ubus.connect() 201 202 if not conn then 203 error("Failed to connect to ubusd") 204 end 205 206 conn:call("otbr", "setpanid", { panid = panid }) 207 conn:call("otbr", "setchannel", { channel = channel }) 208 conn:call("otbr", "setnetworkkey", { networkkey = networkkey }) 209 result = conn:call("otbr", "threadstart", {}) 210 vError = result.Error 211 212 local stat, dsp = pcall(require, "luci.dispatcher") 213 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread") .. "?error=" .. vError) 214end 215 216function thread_stop() 217 local ubus = require "ubus" 218 local tpl = require "luci.template" 219 local http = require "luci.http" 220 221 local conn = ubus.connect() 222 223 if not conn then 224 error("Failed to connect to ubusd") 225 end 226 227 result = conn:call("otbr", "threadstop", {}) 228 vError = result.Error 229 230 local stat, dsp = pcall(require, "luci.dispatcher") 231 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread") .. "?error=" .. vError) 232end 233 234function thread_join() 235 local tpl = require "luci.template" 236 local http = require "luci.http" 237 238 local stat, dsp = pcall(require, "luci.dispatcher") 239 luci.http.redirect(stat and dsp.build_url("admin", "network", "thread_join")) 240end 241 242function thread_state() 243 luci.http.prepare_content("application/json") 244 245 local result = {} 246 result.state = threadget("state").State 247 248 if(result.state ~= "disabled") then 249 result.panid = threadget("panid").PanId 250 result.channel = threadget("channel").Channel 251 result.networkname = threadget("networkname").NetworkName 252 end 253 luci.http.write_json(result) 254end 255 256function thread_graph() 257 luci.http.prepare_content("application/json") 258 259 luci.http.write_json(networkdata()) 260end 261 262function thread_neighbors() 263 luci.http.prepare_content("application/json") 264 265 local result = {} 266 local neighbor = neighborlist() 267 268 result.neighbor = neighbor.neighborlist 269 270 local joiner = joinerlist() 271 result.joinernum = joiner.joinernum 272 result.joinerlist = joiner.joinerlist 273 274 result.state = threadget("state").State 275 276 luci.http.write_json(result) 277end 278 279function networkdata() 280 local k, v, m, n 281 local data = { } 282 local l = { } 283 284 local result = connect_ubus("networkdata") 285 286 for k, v in pairs(result) do 287 l[#l+1] = v 288 end 289 290 data.connect = l 291 data.state = threadget("state").State 292 data.rloc16 = threadget("rloc16").rloc16 293 data.joinernum = threadget("joinernum").joinernum 294 data.leader = threadget("leaderdata").leaderdata.LeaderRouterId 295 return data 296end 297 298function joinerlist() 299 local k, v, result 300 local l = { } 301 local data = { } 302 303 result = connect_ubus("joinernum") 304 data.joinernum = result.joinernum 305 306 if result.Joinernum ~= 0 then 307 for k, v in pairs(result.joinerList) do 308 l[#l+1] = v 309 end 310 end 311 312 data.joinerlist = l 313 return data 314end 315 316function neighborlist() 317 local k, v, result, tmpResult 318 local l = { } 319 local data = { } 320 321 local state = threadget("state").State 322 323 if state == 'child' then 324 tmpResult = connect_ubus("parent") 325 result = tmpResult.parent_list 326 else 327 tmpResult = connect_ubus("neighbor") 328 result = tmpResult.neighbor_list 329 end 330 331 for k, v in pairs(result) do 332 l[#l+1] = v 333 end 334 335 data.neighborlist = l 336 return data 337end 338 339function connect_ubus(methods) 340 local ubus = require "ubus" 341 local result 342 local conn = ubus.connect() 343 344 if not conn then 345 error("Failed to connect to ubusd") 346 end 347 348 result = conn:call("otbr", methods, {}) 349 350 return result 351end 352 353function threadget(action) 354 local result = connect_ubus(action) 355 356 return result 357end 358