Skip to content

Commit

Permalink
feat(nl80211): add method get_noise
Browse files Browse the repository at this point in the history
This method is used to obtain the noise of the interface.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Oct 29, 2023
1 parent c12de65 commit 9db2016
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions nl80211.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ int luaopen_eco_core_nl80211(lua_State *L)
lua_add_constant(L, "CMD_NEW_SCAN_RESULTS", NL80211_CMD_NEW_SCAN_RESULTS);
lua_add_constant(L, "CMD_SCAN_ABORTED", NL80211_CMD_SCAN_ABORTED);

lua_add_constant(L, "CMD_GET_SURVEY", NL80211_CMD_GET_SURVEY);

lua_add_constant(L, "CMD_RADAR_DETECT", NL80211_CMD_RADAR_DETECT);
lua_add_constant(L, "CMD_GET_PROTOCOL_FEATURES", NL80211_CMD_GET_PROTOCOL_FEATURES);
lua_add_constant(L, "CMD_CH_SWITCH_STARTED_NOTIFY", NL80211_CMD_CH_SWITCH_STARTED_NOTIFY);
Expand Down Expand Up @@ -431,6 +433,9 @@ int luaopen_eco_core_nl80211(lua_State *L)
lua_add_constant(L, "RATE_INFO_EHT_NSS", NL80211_RATE_INFO_EHT_NSS);
lua_add_constant(L, "RATE_INFO_EHT_GI", NL80211_RATE_INFO_EHT_GI);

lua_add_constant(L, "SURVEY_INFO_NOISE", NL80211_SURVEY_INFO_NOISE);
lua_add_constant(L, "SURVEY_INFO_IN_USE", NL80211_SURVEY_INFO_IN_USE);

lua_add_constant(L, "PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP", NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP);

lua_add_constant(L, "BAND_2GHZ", NL80211_BAND_2GHZ);
Expand Down
66 changes: 65 additions & 1 deletion nl80211.lua
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,64 @@ function M.wait_event(grp_name, timeout, cb, data)
end
end

function M.get_noise(ifname)
if type(ifname) ~= 'string' then
error('invalid ifname')
end

local ifidx = socket.if_nametoindex(ifname)
if not ifidx then
return nil, 'no dev'
end

local sock, msg = prepare_send_cmd(nl80211.CMD_GET_SURVEY, nl.NLM_F_DUMP)
if not sock then
return nil, msg
end

msg:put_attr_u32(nl80211.ATTR_IFINDEX, ifidx)

local ok, err = sock:send(msg)
if not ok then
return nil, err
end

local noise = 0

while true do
msg, err = sock:recv()
if not msg then
return nil, err
end

while true do
local nlh = msg:next()
if not nlh then
break
end

if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end

if nlh.type == nl.NLMSG_DONE then
return noise
end

local attrs = msg:parse_attr(genl.GENLMSGHDR_SIZE)
if attrs[nl80211.ATTR_SURVEY_INFO] then
local si = nl.parse_attr_nested(attrs[nl80211.ATTR_SURVEY_INFO])
if si[nl80211.SURVEY_INFO_NOISE] then
if noise == 0 or si[nl80211.SURVEY_INFO_IN_USE] then
noise = nl.attr_get_s8(si[nl80211.SURVEY_INFO_NOISE])
end
end
end
end
end
end

local function parse_bitrate(attrs)
local r = {}
local rate = 0
Expand Down Expand Up @@ -967,7 +1025,10 @@ function M.get_station(ifname, mac)
end

local sinfo = nl.parse_attr_nested(attrs[nl80211.ATTR_STA_INFO])
return parse_station(attrs, sinfo)
local res = parse_station(attrs, sinfo)
res.noise = M.get_noise(ifname) or 0

return res
end

function M.get_stations(ifname)
Expand All @@ -992,6 +1053,8 @@ function M.get_stations(ifname)
return nil, err
end

local noise = M.get_noise(ifname) or 0

local stations = {}

while true do
Expand Down Expand Up @@ -1020,6 +1083,7 @@ function M.get_stations(ifname)
local sinfo = nl.parse_attr_nested(attrs[nl80211.ATTR_STA_INFO])
local res = parse_station(attrs, sinfo)
if res then
res.noise = noise
stations[#stations + 1] = res
end
end
Expand Down

0 comments on commit 9db2016

Please sign in to comment.