<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Do u think we could add the channel utilization in the cmd tool?<br>
      You would have to do some averaging over a time period...<br>
    </p>
    <div class="moz-cite-prefix">On 26.06.2018 21:14, Nick wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:0958d252-5d13-0cdb-0340-42b3495f900a@systemli.org">
      <pre wrap="">Here is the patch. It compiles.
How can I test this? I'm not sure if I did everything correct.
I never used lua. :/

From 50ca15c4efcc35b58bc635f6c0fbda4532a519fc Mon Sep 17 00:00:00 2001
From: PolynomialDivision <a class="moz-txt-link-rfc2396E" href="mailto:vincent@systemli.org"><vincent@systemli.org></a>
Date: Tue, 29 May 2018 00:10:01 +0200
Subject: [PATCH] iwinfo: add channel survey

Add channel survey data.

Signed-off-by: Nick Hainke <a class="moz-txt-link-rfc2396E" href="mailto:vincent@systemli.org"><vincent@systemli.org></a>
---
 include/iwinfo.h | 11 +++++++++++
 iwinfo_cli.c     | 37 +++++++++++++++++++++++++++++++++-
 iwinfo_lua.c     | 42 +++++++++++++++++++++++++++++++++++++++
 iwinfo_nl80211.c | 60
+++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 148 insertions(+), 2 deletions(-)

diff --git a/include/iwinfo.h b/include/iwinfo.h
index 929f697..c5db9b6 100644
--- a/include/iwinfo.h
+++ b/include/iwinfo.h
@@ -157,6 +157,16 @@ struct iwinfo_scanlist_entry {
     struct iwinfo_crypto_entry crypto;
 };
 
+struct iwinfo_survey_entry {
+    uint32_t frequency;
+    int8_t noise;
+    uint64_t channel_time;
+    uint64_t channel_time_busy;
+    uint64_t channel_time_ext_busy;
+    uint64_t channel_time_rx;
+    uint64_t channel_time_tx;
+};
+
 struct iwinfo_country_entry {
     uint16_t iso3166;
     char ccode[4];
@@ -203,6 +213,7 @@ struct iwinfo_ops {
     int (*bitrate)(const char *, int *);
     int (*signal)(const char *, int *);
     int (*noise)(const char *, int *);
+    int (*survey)(const char *, struct iwinfo_survey_entry *);
     int (*quality)(const char *, int *);
     int (*quality_max)(const char *, int *);
     int (*mbssid_support)(const char *, int *);
diff --git a/iwinfo_cli.c b/iwinfo_cli.c
index 49c9035..2d30fdd 100644
--- a/iwinfo_cli.c
+++ b/iwinfo_cli.c
@@ -116,6 +116,18 @@ static char * format_signal(int sig)
     return buf;
 }
 
+static char * format_channel_time(uint64_t time)
+{
+    static char buf[30];
+
+    if (!time)
+        snprintf(buf, sizeof(buf), "unknown");
+    else
+        snprintf(buf, sizeof(buf), "%llu ms", time);
+
+    return buf;
+}
+
 static char * format_noise(int noise)
 {
     static char buf[10];
@@ -531,6 +543,25 @@ static char * print_phyname(const struct iwinfo_ops
*iw, const char *ifname)
     return "?";
 }
 
+static void print_survey(const struct iwinfo_ops *iw, const char *ifname)
+{
+    struct iwinfo_survey_entry entry;
+    iw->survey(ifname, &entry);
+
+    if(iw->survey == NULL){
+        printf("No survey information available\n");
+        return;
+    }
+
+    printf("%s\tESSID:\t\t\t\t%s\n", ifname, print_ssid(iw, ifname));
+    printf("\tChannel:\t\t\t%s (%s)\n", print_channel(iw, ifname),
format_frequency(entry.frequency));
+    printf("\tNoise:\t\t\t\t%s\n", format_noise(entry.noise));
+    printf("\tchannel Active Time:\t\t%s\n",
format_channel_time(entry.channel_time));
+    printf("\tChannel Busy
Time:\t\t%s\n",format_channel_time(entry.channel_time_busy));
+    printf("\tExtension Channel Busy
Time:\t%s\n",format_channel_time(entry.channel_time_ext_busy));
+    printf("\tChannel Receive
Time:\t\t%s\n",format_channel_time(entry.channel_time_rx));
+    printf("\tChannel Transmit
Time:\t\t%s\n",format_channel_time(entry.channel_time_tx));
+}
 
 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
 {
@@ -805,6 +836,7 @@ int main(int argc, char **argv)
             "Usage:\n"
             "    iwinfo <device> info\n"
             "    iwinfo <device> scan\n"
+            "    iwinfo <device> survey\n"
             "    iwinfo <device> txpowerlist\n"
             "    iwinfo <device> freqlist\n"
             "    iwinfo <device> assoclist\n<a class="moz-txt-link-rfc2396E" href="mailto:@@-883,7+915,10@@intmain%28intargc,char**argv%29%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0break;%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0case%27s%27:-%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0print_scanlist%28iw,argv[1]%29;+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0if%28argv[i][1]==%27c%27%29+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0print_scanlist%28iw,argv[1]%29;+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0else+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0print_survey%28iw,argv[1]%29;%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0break;%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0case%27t%27:diff--gita/iwinfo_lua.cb/iwinfo_lua.cindexeebab8e..6ed6ffd100644---a/iwinfo_lua.c+++b/iwinfo_lua.c@@-439,6+439,46@@staticintiwinfo_L_scanlist%28lua_State*L,int%28*func%29%28constchar*,char*,int%C2%A0%C2%A0%C2%A0%C2%A0return1;%C2%A0%7D%C2%A0+/*Wrapperforsurveyinfo*/+staticintiwinfo_L_survey%28lua_State*L,int%28*func%29%28constchar*,structiwinfo_survey_entry*%29%29+%7B+%C2%A0%C2%A0%C2%A0constchar*ifname=luaL_checkstring%28L,1%29;+%C2%A0%C2%A0%C2%A0structiwinfo_survey_entrye;++%C2%A0%C2%A0%C2%A0if%28%21%28*func%29%28ifname,&e%29%29+%C2%A0%C2%A0%C2%A0%7B+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0lua_newtable%28L%29;++%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0/*Channel*/+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0lua_pushinteger%28L,e.frequency%29;+%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0lua_setfield%28L,-2,">"
@@ -883,7 +915,10 @@ int main(int argc, char **argv)
                     break;
 
                 case 's':
-                    print_scanlist(iw, argv[1]);
+                    if(argv[i][1] == 'c')
+                        print_scanlist(iw, argv[1]);
+                    else
+                        print_survey(iw, argv[1]);
                     break;
 
                 case 't':
diff --git a/iwinfo_lua.c b/iwinfo_lua.c
index eebab8e..6ed6ffd 100644
--- a/iwinfo_lua.c
+++ b/iwinfo_lua.c
@@ -439,6 +439,46 @@ static int iwinfo_L_scanlist(lua_State *L, int
(*func)(const char *, char *, int
     return 1;
 }
 
+/* Wrapper for survey info */
+static int iwinfo_L_survey(lua_State *L, int (*func)(const char *,
struct iwinfo_survey_entry *))
+{
+    const char *ifname = luaL_checkstring(L, 1);
+    struct iwinfo_survey_entry e;
+
+    if (!(*func)(ifname, &e))
+    {
+        lua_newtable(L);
+
+        /* Channel */
+        lua_pushinteger(L, e.frequency);
+        lua_setfield(L, -2, "</a>frequency");
+
+        /* Quality, Signal */
+        lua_pushinteger(L, e.noise);
+        lua_setfield(L, -2, "noise");
+
+        lua_pushnumber(L, e.channel_time);
+        lua_setfield(L, -2, "channel_time");
+
+        lua_pushnumber(L, e.channel_time_busy);
+        lua_setfield(L, -2, "channel_time_busy");
+
+        lua_pushnumber(L, e.channel_time_ext_busy);
+        lua_setfield(L, -2, "channel_time_ext_busy");
+
+        lua_pushnumber(L, e.channel_time_rx);
+        lua_setfield(L, -2, "channel_time_rx");
+
+        lua_pushnumber(L, e.channel_time_tx);
+        lua_setfield(L, -2, "channel_time_tx");
+    } else
+    {
+        lua_pushnil(L);
+    }
+
+    return 1;
+}
+
 /* Wrapper for frequency list */
 static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *,
char *, int *))
 {
@@ -732,6 +772,7 @@ LUA_WRAP_STRUCT_OP(nl80211,mode)
 LUA_WRAP_STRUCT_OP(nl80211,assoclist)
 LUA_WRAP_STRUCT_OP(nl80211,txpwrlist)
 LUA_WRAP_STRUCT_OP(nl80211,scanlist)
+LUA_WRAP_STRUCT_OP(nl80211,survey)
 LUA_WRAP_STRUCT_OP(nl80211,freqlist)
 LUA_WRAP_STRUCT_OP(nl80211,countrylist)
 LUA_WRAP_STRUCT_OP(nl80211,hwmodelist)
@@ -855,6 +896,7 @@ static const luaL_reg R_nl80211[] = {
     LUA_REG(nl80211,assoclist),
     LUA_REG(nl80211,txpwrlist),
     LUA_REG(nl80211,scanlist),
+    LUA_REG(nl80211,survey),
     LUA_REG(nl80211,freqlist),
     LUA_REG(nl80211,countrylist),
     LUA_REG(nl80211,hwmodelist),
diff --git a/iwinfo_nl80211.c b/iwinfo_nl80211.c
index ecd2d6a..c58ff1b 100644
--- a/iwinfo_nl80211.c
+++ b/iwinfo_nl80211.c
@@ -1357,6 +1357,64 @@ static int nl80211_get_signal(const char *ifname,
int *buf)
     return -1;
 }
 
+static int nl80211_get_channel_survey_cb(struct nl_msg *msg, void *arg)
+{
+    struct iwinfo_survey_entry *entry = arg;
+    struct nlattr *tb[NL80211_ATTR_MAX + 1];
+    struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+    struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
+
+    static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
+            [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
+            [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
+    };
+
+    nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+              genlmsg_attrlen(gnlh, 0), NULL);
+
+    if (!tb[NL80211_ATTR_SURVEY_INFO])
+        return NL_SKIP;
+
+    if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
+                         tb[NL80211_ATTR_SURVEY_INFO], sp))
+        return NL_SKIP;
+
+    if (si[NL80211_SURVEY_INFO_IN_USE])
+    {
+        if(si[NL80211_SURVEY_INFO_FREQUENCY])
+            entry->frequency =
nla_get_u32(si[NL80211_SURVEY_INFO_FREQUENCY]);
+
+        if(si[NL80211_SURVEY_INFO_NOISE])
+            entry->noise =
(int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
+
+        if(si[NL80211_SURVEY_INFO_CHANNEL_TIME])
+            entry->channel_time =
nla_get_u64(si[NL80211_SURVEY_INFO_CHANNEL_TIME]);
+
+        if(si[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY])
+            entry->channel_time_busy =
nla_get_u64(si[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
+
+        if(si[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
+            entry->channel_time_ext_busy =
nla_get_u64(si[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
+
+        if(si[NL80211_SURVEY_INFO_TIME_RX])
+            entry->channel_time_rx =
nla_get_u64(si[NL80211_SURVEY_INFO_TIME_RX]);
+
+        if(si[NL80211_SURVEY_INFO_TIME_TX])
+            entry->channel_time_tx =
nla_get_u64(si[NL80211_SURVEY_INFO_TIME_TX]);
+    }
+
+    return NL_SKIP;
+}
+
+static int nl80211_get_channel_survey(const char *ifname, struct
iwinfo_survey_entry *entry)
+{
+    if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP,
+                        nl80211_get_channel_survey_cb, entry))
+        return -1;
+
+    return 0;
+}
+
 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
 {
     int8_t *noise = arg;
@@ -1384,7 +1442,6 @@ static int nl80211_get_noise_cb(struct nl_msg
*msg, void *arg)
     return NL_SKIP;
 }
 
-
 static int nl80211_get_noise(const char *ifname, int *buf)
 {
     int8_t noise = 0;
@@ -2832,6 +2889,7 @@ const struct iwinfo_ops nl80211_ops = {
     .bitrate          = nl80211_get_bitrate,
     .signal           = nl80211_get_signal,
     .noise            = nl80211_get_noise,
+    .survey           = nl80211_get_channel_survey,
     .quality          = nl80211_get_quality,
     .quality_max      = nl80211_get_quality_max,
     .mbssid_support   = nl80211_get_mbssid_support,
</pre>
    </blockquote>
    <br>
  </body>
</html>