Skip to content

Commit

Permalink
ref_state: adds wifi links location
Browse files Browse the repository at this point in the history
  • Loading branch information
javierajorge committed Jun 28, 2024
1 parent 5312ac4 commit 02bdc7f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local JSON = require("luci.jsonc")
local node_status = require ("lime.node_status")
local network = require ("lime.network")
local iwinfo = require "iwinfo"

local location = require 'lime.location'

function get_wifi_links_info()
local stations = node_status.get_stations()
Expand All @@ -34,13 +34,32 @@ function get_wifi_links_info()
local chanenel = iwinfo.nl80211.channel(station.iface)
local key_table = {string.lower(string.gsub(src_macaddr,":","")),string.lower(string.gsub(station.station_mac,":",""))}
table.sort(key_table)
local src_loc = location.get_node() or location.get_community() or {lat="FIXME", long="FIXME"}
links[table.concat(key_table)]= {src_mac=src_macaddr ,dst_mac=string.lower(station.station_mac),
signal=station_stats.signal,chains=station_stats.chains,iface=station.iface,
rx_rate=station_stats.rx_rate,tx_rate=station_stats.tx_rate,freq=freq, channel = chanenel }
rx_rate=station_stats.rx_rate,tx_rate=station_stats.tx_rate,freq=freq, channel = chanenel,src_loc = src_loc }
end
return links
end

function add_dst_loc(links_info,shared_state_sample)
for link, l_data in pairs(links_info) do
for node, data in pairs(shared_state_sample) do
if node ~= hostname then
local link_data= data[link]
if link_data ~= nil then
if link_data.src_mac == l_data.dst_mac then
l_data.dst_loc = link_data.src_loc
end
end
end
end
end
end

local hostname = io.input("/proc/sys/kernel/hostname"):read("*line")
local links_info = get_wifi_links_info()
local shared_state_sample = JSON.parse(io.popen("shared-state-async insert wifi_links_info", "r"):read('*all'))
add_dst_loc(links_info,shared_state_sample)
local result = { [hostname] = get_wifi_links_info() }
io.popen("shared-state-async insert wifi_links_info", "w"):write(JSON.stringify(result))
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
local utils = require "lime.utils"
local node_status = require 'lime.node_status'
local iwinfo = require('iwinfo')
local JSON = require("luci.jsonc")


local shared_state_output_text = [[
{"LiMe-462895":{"c00000010101c04a00be7b0a":{"src_loc":{"long":"FYI","lat":"FYI"},"freq":5240,"iface":"wlan1-mesh","tx_rate":300000,"dst_mac":"ae:40:41:1d:f9:35","channel":48,"chains":[-46,-43],"signal":-42,"rx_rate":300000,"src_mac":"c0:4a:00:be:7b:0a"},"a6f3c1462897ae40411c8516":{"freq":5240,"iface":"wlan1-mesh","tx_rate":240000,"dst_mac":"ae:40:41:1c:85:16","channel":48,"chains":[-54,-54],"signal":-51,"rx_rate":300000,"src_mac":"a6:f3:c1:46:28:97"},"a6f3c1462897c64a00fc3abf":{"freq":5240,"iface":"wlan1-mesh","tx_rate":180000,"dst_mac":"c6:4a:00:fc:3a:bf","channel":48,"chains":[-65,-63],"signal":-61,"rx_rate":270000,"src_mac":"a6:f3:c1:46:28:97"},"a6f3c1462896c64a00fc3abe":{"freq":2462,"iface":"wlan0-mesh","tx_rate":144400,"dst_mac":"c6:4a:00:fc:3a:be","channel":11,"chains":[-51,-40],"signal":-40,"rx_rate":144400,"src_mac":"a6:f3:c1:46:28:96"}},"LiMe-b713f7":{"a6f3c1462897ae40411df935":{"freq":5240,"iface":"wlan1-mesh","tx_rate":300000,"dst_mac":"a6:f3:c1:46:28:97","channel":48,"chains":[-53,-53],"signal":-50,"rx_rate":300000,"src_mac":"ae:40:41:1d:f9:35"},"ae40411c85c3ae40411df934":{"freq":5785,"iface":"wlan2-mesh","tx_rate":150000,"dst_mac":"ae:40:41:1c:85:c3","channel":157,"chains":[-51,-36],"signal":-36,"rx_rate":300000,"src_mac":"ae:40:41:1d:f9:34"},"ae40411df935c64a00fc3abf":{"freq":5240,"iface":"wlan1-mesh","tx_rate":300000,"dst_mac":"c6:4a:00:fc:3a:bf","channel":48,"chains":[-58,-63],"signal":-57,"rx_rate":240000,"src_mac":"ae:40:41:1d:f9:35"},"ae40411c8516ae40411df935":{"freq":5240,"iface":"wlan1-mesh","tx_rate":300000,"dst_mac":"ae:40:41:1c:85:16","channel":48,"chains":[-58,-70],"signal":-57,"rx_rate":300000,"src_mac":"ae:40:41:1d:f9:35"}},"LiMe-fc3abd":{"ae40411df935c64a00fc3abf":{"freq":5240,"iface":"wlan1-mesh","tx_rate":240000,"dst_mac":"ae:40:41:1d:f9:35","channel":48,"chains":[-48,-46],"signal":-44,"rx_rate":300000,"src_mac":"c6:4a:00:fc:3a:bf"},"ae40411c8516c64a00fc3abf":{"freq":5240,"iface":"wlan1-mesh","tx_rate":150000,"dst_mac":"ae:40:41:1c:85:16","channel":48,"chains":[-67,-63],"signal":-62,"rx_rate":270000,"src_mac":"c6:4a:00:fc:3a:bf"},"a6f3c1462897c64a00fc3abf":{"freq":5240,"iface":"wlan1-mesh","tx_rate":300000,"dst_mac":"a6:f3:c1:46:28:97","channel":48,"chains":[-64,-67],"signal":-63,"rx_rate":180000,"src_mac":"c6:4a:00:fc:3a:bf"},"a6f3c1462896c64a00fc3abe":{"freq":2462,"iface":"wlan0-mesh","tx_rate":144400,"dst_mac":"a6:f3:c1:46:28:96","channel":11,"chains":[-53,-45],"signal":-44,"rx_rate":130000,"src_mac":"c6:4a:00:fc:3a:be"}},"cheche":{"ae40411c8516ae40411df935":{"freq":5240,"iface":"wlan1-mesh","tx_rate":240000,"dst_mac":"ae:40:41:1d:f9:35","channel":48,"chains":[-55,-59],"signal":-54,"rx_rate":243000,"src_mac":"ae:40:41:1c:85:16"},"ae40411c85c3ae40411df934":{"freq":5785,"iface":"wlan2-mesh","tx_rate":270000,"dst_mac":"ae:40:41:1d:f9:34","channel":157,"chains":[-40,-31],"signal":-30,"rx_rate":150000,"src_mac":"ae:40:41:1c:85:c3"},"a6f3c1462897ae40411c8516":{"freq":5240,"iface":"wlan1-mesh","tx_rate":300000,"dst_mac":"a6:f3:c1:46:28:97","channel":48,"chains":[-56,-58],"signal":-54,"rx_rate":180000,"src_mac":"ae:40:41:1c:85:16"},"ae40411c8516c64a00fc3abf":{"freq":5240,"iface":"wlan1-mesh","tx_rate":270000,"dst_mac":"c6:4a:00:fc:3a:bf","channel":48,"chains":[-73,-75],"signal":-71,"rx_rate":90000,"src_mac":"ae:40:41:1c:85:16"}}}
]]

package.path = package.path .. ";packages/shared-state-wifi_links_info/files/usr/share/shared-state/publishers/?;;"
require ("shared-state-publish_wifi_links_info")
Expand Down Expand Up @@ -34,3 +40,42 @@ it('a simple test to get links info and assert requiered fields are present', fu
assert.is.equal("c0:00:00:00:00:00", links_info["c00000000000c04a00be7b09"].src_mac)
end)

it('a simple test to get links info', function()
stub(utils, "unsafe_shell", function (cmd)
if string.match(cmd, "wlan0") then
return iwinfo.mocks.iw_station_get_result_wlan0
end
return iwinfo.mocks.iw_station_get_result_wlan1
end)
stub(node_status, "get_stations", function () return iwinfo.mocks.get_stations end)
stub(node_status, "get_stations", function () return iwinfo.mocks.get_stations end)
stub(iwinfo.nl80211,"frequency",function (iface) return 2400 end)
stub(network, "get_mac", function (iface)
if string.match(iface, "wlan0") then
return iwinfo.mocks.wlan0_mesh_mac
end
return iwinfo.mocks.wlan1_mesh_mac
end)
local links_info = {}

links_info = get_wifi_links_info()
local hostname = io.input("/proc/sys/kernel/hostname"):read("*line")
local shared_state_sample = JSON.parse(shared_state_output_text)
assert.is.equal(nil, links_info["c00000010101c04a00be7b0a"].dst_loc)
add_dst_loc(links_info,shared_state_sample)
assert.is.equal("FYI", links_info["c00000010101c04a00be7b0a"].dst_loc.lat)
utils.printJson(links_info)

local links_info = {}

links_info = get_wifi_links_info()
local hostname = io.input("/proc/sys/kernel/hostname"):read("*line")
--asume shared state has just initialized
local shared_state_sample = JSON.parse("{}")
utils.printJson()
assert.is.equal(nil, links_info["c00000010101c04a00be7b0a"].dst_loc)
add_dst_loc(links_info,shared_state_sample)
assert.is.equal(nil, links_info["c00000010101c04a00be7b0a"].dst_loc)
assert.is.equal("c0:00:00:00:00:00", links_info["c00000000000c04a00be7b09"].src_mac)

end)

0 comments on commit 02bdc7f

Please sign in to comment.