summaryrefslogtreecommitdiffhomepage
path: root/modules/niu/luasrc/niulib.lua
blob: 2a46355531004422d9a7ea2b9d24a9b47fb06ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--[[
LuCI - Lua Configuration Interface

Copyright 2009 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--

local ipairs, pairs, require = ipairs, pairs, require
local os = require "os"

local cursor = require "luci.model.uci".inst


module "luci.niulib"

function eth_get_available(except)
	local nw = require "luci.model.network"
	nw.init(cursor)

	local ifs = {}
	for _, iface in ipairs(nw.get_interfaces()) do
		if iface:name():find("eth") == 1 then
			local net = iface:get_network()
			if not net or net:name() == except or os.getenv("LUCI_SYSROOT") then
				ifs[#ifs+1] = iface:name()
			end
		end
	end
	return ifs
end

function wifi_get_available(except)
	cursor:unload("wireless")

	local iwinfo = require "iwinfo"
	local used = {}
	cursor:foreach("wireless", "wifi-iface", function(s)
		if s[".name"] ~= except and s._niu == 1 then
			used[s.device] = 1
		end
	end)

	for k in pairs(used) do
		local t = iwinfo.type(k)
		if t and iwinfo[t] then
			used[k] = (iwinfo[t].mbssid_support() < 1)
		end
	end

	local wifis = {}
	cursor:foreach("wireless", "wifi-device", function(s)
		if not used[s[".name"]] then
			wifis[#wifis+1] = s[".name"]
		end
	end)
	return wifis
end