NIU:
[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 cursor = require "luci.model.uci".inst
19
20
21 module "luci.niulib"
22
23 function eth_get_available(except)
24         local nw = require "luci.model.network"
25         nw.init(cursor)
26
27         local ifs = {}
28         for _, iface in ipairs(nw.get_interfaces()) do
29                 if iface:name():find("eth") == 1 then
30                         local net = iface:get_network()
31                         if not net or net:name() == except or os.getenv("LUCI_SYSROOT") then
32                                 ifs[#ifs+1] = iface:name()
33                         end
34                 end
35         end
36         return ifs
37 end
38
39 function wifi_get_available(except)
40         cursor:unload("wireless")
41
42         local iwinfo = require "iwinfo"
43         local used = {}
44         cursor:foreach("wireless", "wifi-iface", function(s)
45                 if s[".name"] ~= except and s._niu == 1 then
46                         used[s.device] = 1
47                 end
48         end)
49
50         for k in pairs(used) do
51                 local t = iwinfo.type(k)
52                 if t and iwinfo[t] then
53                         used[k] = (iwinfo[t].mbssid_support() < 1)
54                 end
55         end
56
57         local wifis = {}
58         cursor:foreach("wireless", "wifi-device", function(s)
59                 if not used[s[".name"]] then
60                         wifis[#wifis+1] = s[".name"]
61                 end
62         end)
63         return wifis
64 end
65