#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2021 Tim Wilkinson
Original Perl Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("nixio")
require("aredn.http")
require("aredn.hardware")
require("aredn.utils")
local html = require("aredn.html")
local aredn_info = require("aredn.info")
local node = aredn_info.get_nvram("node")
if not node then
node = "NOCALL"
end
local wifiiface = aredn.hardware.get_iface_name("wifi")
local nf = iwinfo.nl80211.noise(wifiiface) or -95
local myfreq = tonumber(aredn_info.getFreq(aredn_info.getMeshRadioDevice()))
local board_type = aredn.hardware.get_board_type()
if board_type:match("^ubnt,") and board_type:match("ac") then
ubnt_ac = true
end
local channels = aredn.hardware.get_rfchannels(wifiiface)
local scan_list = ""
for _, channel in ipairs(channels)
do
scan_list = scan_list .. " " .. channel.frequency
end
-- scan start
local scanned = {}
local f = io.popen("iw dev " .. wifiiface .. " station dump")
if f then
local scan = {}
local myssid = aredn_info.getSSID()
for line in f:lines()
do
local m = line:match("^Station ([%da-fA-F:]+) %(on " .. wifiiface .. "%)")
if m then
scan = scanned[m]
if not scan then
scan = {
mac = m,
signal = 9999,
freq = {},
key = "",
joined = false
}
scanned[m] = scan
end
scan.mode = "Connected Ad-Hoc Station"
scan.ssid = myssid
scan.freq[myfreq] = true
end
m = line:match("signal avg:%s+([%d%-]+)")
if m then
scan.signal = tonumber(m)
end
end
f:close()
end
-- Ubiquiti AC device workaround
if ubnt_ac then
os.execute("iw dev " .. wifiiface .. " ibss leave > /dev/null 2>&1")
os.execute("wifi up > /dev/null 2>&1")
local attempt = 10
while attempt > 0
do
attempt = attempt - 1
for line in io.popen("iw dev " .. wifiiface .. " scan"):lines()
do
if line:match("^BSS ") then
attempt = 0
end
break
end
nixio.nanosleep(2, 0)
end
end
local f = io.popen("iw dev " .. wifiiface .. " scan freq" .. scan_list .. " passive")
if f then
local scan = {}
for line in f:lines()
do
local m = line:match("^BSS ([%da-fA-F:]+)")
if m then
scan = scanned[m]
if not scan then
scan = {
mac = m,
mode = "AP",
ssid = "",
signal = 9999,
freq = {},
key = "",
joined = false
}
scanned[m] = scan
elseif scan.joined then
scan = {
freq = {}
}
end
if line:match("joined") then
scan.mode = "My Ad-Hoc Network"
scan.joined = true
end
end
m = line:match("freq: (%d+)")
if m then
scan.freq[m] = true
if tonumber(m) == myfreq and scan.mode == "AP" then
scan.mode = "My Ad-Hoc Network"
scan.joined = true
end
end
m = line:match("SSID: (.+)")
if m then
scan.ssid = m
end
m = line:match("signal: ([%d%-]+)")
if m then
scan.signal = tonumber(m)
end
m = line:match("Group cipher: (.+)")
if m then
scan.key = m
end
if line:match("capability: IBSS") and scan.mode == "AP" then
scan.mode = "Foreign Ad-Hoc Network"
end
end
f:close()
end
-- scan end
-- generate page
http_header()
html.header(node .. " WiFi scan", false)
local autoscan = string.find((nixio.getenv("QUERY_STRING") or ""):lower(),"autoscan=1")
if autoscan then
html.print("")
end
html.print([[