NIU: Allow automatic unbridging of LAN ethernet for WAN use
[project/luci.git] / modules / niu / luasrc / niulib.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local ipairs, pairs, require = ipairs, pairs, require
16 local os = require "os"
17
18 local uci = require "luci.model.uci"
19 local cursor = uci.inst
20 local state = uci.inst_state
21
22
23 module "luci.niulib"
24
25 function eth_get_available(except)
26         local nw = require "luci.model.network"
27         nw.init(cursor)
28
29         local ifs = {}
30         for _, iface in ipairs(nw.get_interfaces()) do
31                 if iface:name():find("eth") == 1 then
32                         local net = iface:get_network()
33                         if not net or net:name() == except or os.getenv("LUCI_SYSROOT") then
34                                 ifs[#ifs+1] = iface:name()
35                         end
36                 end
37         end
38         return ifs
39 end
40
41 function eth_get_bridged(except)
42         local devs = state:get("network", except, "device")
43         
44         local ifs = {}
45         local cnt = 0
46         for x in devs:gmatch("[^ ]+") do
47                 cnt = cnt + 1
48                 if x:find("eth") == 1 then
49                         ifs[#ifs+1] = x
50                 end
51         end
52         return cnt > 1 and ifs or {}
53 end
54
55 function wifi_get_available(except)
56         cursor:unload("wireless")
57
58         local iwinfo = require "iwinfo"
59         local used = {}
60         cursor:foreach("wireless", "wifi-iface", function(s)
61                 if s[".name"] ~= except and s._niu == 1 then
62                         used[s.device] = 1
63                 end
64         end)
65
66         for k in pairs(used) do
67                 local t = iwinfo.type(k)
68                 if t and iwinfo[t] then
69                         used[k] = (iwinfo[t].mbssid_support(k) < 1)
70                 end
71         end
72
73         local wifis = {}
74         cursor:foreach("wireless", "wifi-device", function(s)
75                 if not used[s[".name"]] then
76                         wifis[#wifis+1] = s[".name"]
77                 end
78         end)
79         return wifis
80 end
81