Skip to content

Commit

Permalink
ref_state: fixes location issue
Browse files Browse the repository at this point in the history
  • Loading branch information
javierajorge committed Jul 2, 2024
1 parent 02bdc7f commit a9224f9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
#!/usr/bin/lua

--! LibreMesh
--! Copyright (C) 2019 Gioacchino Mazzurco <[email protected]>
--! Copyright (C) 2024 Javier Jorge <[email protected]>
--!
--! This program is free software: you can redistribute it and/or modify
--! it under the terms of the GNU Affero General Public License as
--! published by the Free Software Foundation, either version 3 of the
--! License, or (at your option) any later version.
--!
--! 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 Affero General Public License for more details.
--!
--! You should have received a copy of the GNU Affero General Public License
--! along with this program. If not, see <http://www.gnu.org/licenses/>.
-- ! LibreMesh
-- ! Copyright (C) 2019 Gioacchino Mazzurco <[email protected]>
-- ! Copyright (C) 2024 Javier Jorge <[email protected]>
-- !
-- ! This program is free software: you can redistribute it and/or modify
-- ! it under the terms of the GNU Affero General Public License as
-- ! published by the Free Software Foundation, either version 3 of the
-- ! License, or (at your option) any later version.
-- !
-- ! 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 Affero General Public License for more details.
-- !
-- ! You should have received a copy of the GNU Affero General Public License
-- ! along with this program. If not, see <http://www.gnu.org/licenses/>.

local JSON = require("luci.jsonc")
local node_status = require ("lime.node_status")
local network = require ("lime.network")
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()
local links = {}
for _, station in ipairs(stations) do
macparts = network.get_mac(station.iface)
src_macaddr = string.lower(table.concat(macparts,":"))
local station_stats = node_status.get_station_stats(station)
local freq = iwinfo.nl80211.frequency(station.iface)
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,src_loc = src_loc }
end
return links
local stations = node_status.get_stations()
local links = {}
for _, station in ipairs(stations) do
macparts = network.get_mac(station.iface)
src_macaddr = string.lower(table.concat(macparts, ":"))
local station_stats = node_status.get_station_stats(station)
local freq = iwinfo.nl80211.frequency(station.iface)
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,
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
function add_dst_loc(links_info, shared_state_sample, hostname)
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]
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
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() }
local shared_state_sample = JSON.parse(io.popen("shared-state-async get wifi_links_info", "r"):read('*all'))
add_dst_loc(links_info, shared_state_sample, hostname)
local result = {[hostname] = 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
Expand Up @@ -40,7 +40,7 @@ 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()
it('a simple test to get location info', function()
stub(utils, "unsafe_shell", function (cmd)
if string.match(cmd, "wlan0") then
return iwinfo.mocks.iw_station_get_result_wlan0
Expand All @@ -64,15 +64,12 @@ it('a simple test to get links info', function()
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)
Expand Down

0 comments on commit a9224f9

Please sign in to comment.