2013-07-24 18:49:07 +02:00
|
|
|
local log = require('util.logger')
|
2013-07-25 01:06:00 +02:00
|
|
|
local utils = require('util.utils')
|
2013-07-24 18:49:07 +02:00
|
|
|
local uci = require('uci').cursor()
|
|
|
|
local iwinfo = require('iwinfo')
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2013-07-25 01:06:00 +02:00
|
|
|
-- NOTE: fallback device 'radio0' is required because sometimes the wlan0 device disappears
|
2013-07-24 18:49:07 +02:00
|
|
|
M.DFL_DEVICE = 'wlan0'
|
|
|
|
M.DFL_DEVICE_FALLBACK = 'radio0'
|
|
|
|
M.NET = 'wlan'
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-25 01:06:00 +02:00
|
|
|
-- NOTE: deviceApi is returned by iwinfo.tpe(deviceName)
|
|
|
|
local deviceName, deviceApi
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
-- if a substitution of baseApSsid is requested, cachedApSsid is returned if not nil
|
|
|
|
local cachedApSsid, baseApSsid = nil, nil
|
|
|
|
|
|
|
|
function M.getSubstitutedSsid(unformattedSsid)
|
|
|
|
if unformattedSsid == baseApSsid and cachedApSsid ~= nil then return cachedApSsid end
|
2013-07-24 18:49:07 +02:00
|
|
|
if not unformattedSsid or type(unformattedSsid) ~= 'string' then return nil end
|
2013-07-17 22:55:27 +02:00
|
|
|
|
|
|
|
local macTail = M.getMacAddress():sub(7)
|
|
|
|
|
|
|
|
baseApSsid = unformattedSsid
|
|
|
|
cachedApSsid = unformattedSsid:gsub('%%%%MAC_ADDR_TAIL%%%%', macTail)
|
|
|
|
|
|
|
|
return cachedApSsid
|
|
|
|
end
|
|
|
|
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
--- Map device mode as reported by iwinfo to device mode as required by UCI
|
|
|
|
-- Note that this function is quite naive.
|
|
|
|
-- @param mode mode text as reported by iwinfo
|
|
|
|
-- @param masterIsAp set to true to map 'Master' to 'ap' instead of 'sta' (optional)
|
|
|
|
function M.mapDeviceMode(mode, masterIsAp)
|
|
|
|
local modeMap = {
|
2013-07-24 18:49:07 +02:00
|
|
|
['Master'] = masterIsAp and 'ap' or 'sta',
|
|
|
|
['Client'] = 'sta',
|
|
|
|
['Ad-Hoc'] = 'adhoc'
|
2013-04-04 10:18:08 +02:00
|
|
|
}
|
2013-04-05 16:22:19 +02:00
|
|
|
return modeMap[mode] or mode
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
2013-04-09 14:37:31 +02:00
|
|
|
--[[
|
|
|
|
- TODO: several modes need to be tested (wep, psk2, mixed-psk)
|
|
|
|
- See: http://wiki.openwrt.org/doc/uci/wireless#wpa.modes
|
|
|
|
]]
|
|
|
|
function M.mapEncryptionType(scanEncrTbl)
|
2013-07-24 18:49:07 +02:00
|
|
|
local wpaModeMap = { [1] = 'psk', [2] = 'psk2', [3] = 'mixed-psk' }
|
2013-04-09 14:37:31 +02:00
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
if scanEncrTbl.enabled == false then return 'none' end
|
|
|
|
if scanEncrTbl.wep == true then return 'wep' end
|
2013-04-09 14:37:31 +02:00
|
|
|
|
|
|
|
return wpaModeMap[scanEncrTbl.wpa] or scanEncrTbl.description
|
|
|
|
end
|
|
|
|
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
--- Initialize WiFi helper library
|
|
|
|
-- @param device wireless device to operate on (optional, defaults to DFL_DEVICE)
|
|
|
|
-- @return true on success or false+error on failure
|
|
|
|
function M.init(device)
|
2013-07-25 01:06:00 +02:00
|
|
|
deviceName = device or M.DFL_DEVICE
|
|
|
|
deviceApi = iwinfo.type(deviceName)
|
|
|
|
if not deviceApi then
|
|
|
|
local devInitial = deviceName
|
|
|
|
deviceName = M.DFL_DEVICE_FALLBACK
|
|
|
|
deviceApi = iwinfo.type(deviceName)
|
2013-07-08 19:02:20 +02:00
|
|
|
|
2013-07-25 01:06:00 +02:00
|
|
|
log:info("wireless device '" .. devInitial .. "' not found, trying fallback '" .. deviceName .. "'")
|
|
|
|
|
|
|
|
if not deviceApi then
|
|
|
|
return false, "No such wireless device: '" .. devInitial .. "' (and fallback '" .. deviceName .. "' does not exist either)"
|
2013-07-08 19:02:20 +02:00
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function M.getDeviceState()
|
2013-07-25 01:06:00 +02:00
|
|
|
local iw = iwinfo[deviceApi]
|
|
|
|
local encDescription = type(iw.encryption) == 'function' and iw.encryption(deviceName) or '<unknown>'
|
2013-04-04 10:18:08 +02:00
|
|
|
local result = {
|
2013-07-25 01:06:00 +02:00
|
|
|
['ssid'] = iw.ssid(deviceName),
|
|
|
|
['bssid'] = iw.bssid(deviceName),
|
|
|
|
['channel'] = iw.channel(deviceName),
|
|
|
|
['mode'] = M.mapDeviceMode(iw.mode(deviceName), true),
|
|
|
|
['encryption'] = M.mapEncryptionType(encDescription),
|
|
|
|
['quality'] = iw.quality(deviceName),
|
|
|
|
['quality_max'] = iw.quality_max(deviceName),
|
|
|
|
['txpower'] = iw.txpower(deviceName),
|
|
|
|
['signal'] = iw.signal(deviceName),
|
|
|
|
['noise'] = iw.noise(deviceName)
|
2013-04-04 10:18:08 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2013-07-11 10:30:59 +02:00
|
|
|
--returns the wireless device's MAC address (as string, without colons)
|
|
|
|
--(lua numbers on openWrt seem to be 32bit so they cannot represent a MAC address as one number)
|
|
|
|
function M.getMacAddress()
|
2013-07-25 01:06:00 +02:00
|
|
|
local macText = utils.readFile('/sys/class/net/' .. deviceName .. '/address')
|
2013-07-24 18:49:07 +02:00
|
|
|
local out = ''
|
2013-07-11 10:30:59 +02:00
|
|
|
|
|
|
|
for i = 0, 5 do
|
|
|
|
local bt = string.sub(macText, i*3+1, i*3+2)
|
|
|
|
out = out .. bt
|
|
|
|
end
|
|
|
|
|
2013-07-25 01:06:00 +02:00
|
|
|
return out:upper()
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.getDeviceName()
|
|
|
|
return deviceName
|
2013-07-11 10:30:59 +02:00
|
|
|
end
|
|
|
|
|
2013-04-04 10:18:08 +02:00
|
|
|
--- Return one or all available wifi networks resulting from an iwinfo scan
|
|
|
|
-- @param ssid return data for given SSID or for all networks if SSID not given
|
|
|
|
-- @return data for all or requested network; false+error on failure or nil when requested network not found
|
|
|
|
function M.getScanInfo(ssid)
|
2013-07-25 01:06:00 +02:00
|
|
|
local iw = iwinfo[deviceApi]
|
|
|
|
local sr = iw.scanlist(deviceName)
|
2013-04-04 10:18:08 +02:00
|
|
|
local si, se
|
|
|
|
|
|
|
|
if ssid == nil then
|
|
|
|
return sr
|
|
|
|
else
|
|
|
|
if sr and #sr > 0 then
|
|
|
|
for _, se in ipairs(sr) do
|
|
|
|
if se.ssid == ssid then
|
|
|
|
return se
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false, "No scan results or scanning not possible"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Return all wireless networks configured in UCI
|
|
|
|
function M.getConfigs()
|
|
|
|
local l = {}
|
2013-07-24 18:49:07 +02:00
|
|
|
uci.foreach('wireless', 'wifi-iface', function(s) table.insert(l, s) end)
|
2013-04-04 10:18:08 +02:00
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Remove UCI config for network with given SSID
|
|
|
|
-- @return true if successfully removed, false if no such config exists
|
|
|
|
function M.removeConfig(ssid)
|
|
|
|
local rv = false
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:foreach('wireless', 'wifi-iface', function(s)
|
2013-04-04 10:18:08 +02:00
|
|
|
if s.ssid == ssid then
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:delete('wireless', s['.name'])
|
2013-04-04 10:18:08 +02:00
|
|
|
rv = true
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end)
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:commit('wireless')
|
2013-04-04 10:18:08 +02:00
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Activate wireless section for given SSID and disable all others
|
|
|
|
-- @param ssid SSID of config to enable, or nil to disable all network configs
|
|
|
|
function M.activateConfig(ssid)
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:foreach('wireless', 'wifi-iface', function(s)
|
|
|
|
local disabled = s.ssid ~= ssid and '1' or '0'
|
|
|
|
uci:set('wireless', s['.name'], 'disabled', disabled)
|
2013-04-04 10:18:08 +02:00
|
|
|
end)
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:commit('wireless')
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a new UCI network from the given iwinfo data
|
|
|
|
-- http://luci.subsignal.org/trac/browser/luci/trunk/libs/iwinfo/src/iwinfo_wext.c?rev=5645 (outdated?)
|
2013-04-09 14:37:31 +02:00
|
|
|
-- TODO: delete previous network if exists (match on MAC-address)
|
2013-04-04 10:18:08 +02:00
|
|
|
-- @param info iwinfo data to create a network from
|
|
|
|
-- @param passphrase passphrase to use (optional)
|
|
|
|
-- @param disabled immediately disable the network (optional)
|
|
|
|
function M.createConfigFromScanInfo(info, passphrase, disabled)
|
|
|
|
local mode = M.mapDeviceMode(info.mode)
|
|
|
|
|
|
|
|
local apconfig = {
|
2013-04-05 16:22:19 +02:00
|
|
|
network = M.NET,
|
2013-07-24 18:49:07 +02:00
|
|
|
device = 'radio0',
|
2013-04-04 10:18:08 +02:00
|
|
|
ssid = info.ssid,
|
|
|
|
bssid = info.bssid,
|
2013-04-09 14:37:31 +02:00
|
|
|
encryption = M.mapEncryptionType(info.encryption),
|
2013-04-04 10:18:08 +02:00
|
|
|
mode = mode,
|
|
|
|
}
|
|
|
|
if passphrase ~= nil then apconfig.key = passphrase end
|
|
|
|
apconfig.disabled = disabled ~= nil and disabled and 1 or 0
|
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:foreach('wireless', 'wifi-iface', function(s)
|
2013-04-18 11:30:48 +02:00
|
|
|
if s.bssid == info.bssid then
|
2013-07-24 18:49:07 +02:00
|
|
|
log:debug("removing old wireless config for net '" .. s.ssid .. "(bssid: " .. s.bssid .. ")'")
|
|
|
|
uci:delete('wireless', s['.name'])
|
2013-04-18 11:30:48 +02:00
|
|
|
-- return false --keep looking, just in case multiple entries with this bssid exist
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2013-07-24 18:49:07 +02:00
|
|
|
local sname = uci:add('wireless', 'wifi-iface');
|
2013-04-04 10:18:08 +02:00
|
|
|
for k, v in pairs(apconfig) do
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:set('wireless', sname, k, v)
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
2013-07-24 18:49:07 +02:00
|
|
|
uci:commit('wireless')
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Reload network config to reflect contents of config
|
|
|
|
-- @see http://wiki.openwrt.org/doc/techref/netifd)
|
|
|
|
-- * Network reload only restarts interfaces which need to be restarted so no
|
|
|
|
-- unneccesary interruptions there.
|
|
|
|
-- * ubus does not seem to work -- local c=ubus.connect();
|
2013-07-24 18:49:07 +02:00
|
|
|
-- c:call('network.interface.wlan', 'down'); c:call('network.interface.wlan', 'up'); c:close()
|
2013-04-05 16:22:19 +02:00
|
|
|
-- @param dhcpToo also reload dnsmasq if true
|
|
|
|
function M.restart(dhcpToo)
|
2013-07-24 18:49:07 +02:00
|
|
|
os.execute('/etc/init.d/network reload') --always seems to return 0
|
|
|
|
if dhcpToo ~= nil and dhcpToo then os.execute('/etc/init.d/dnsmasq reload') end
|
2013-04-05 16:22:19 +02:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2013-04-04 10:18:08 +02:00
|
|
|
return M
|