[OpenWrt-Devel,iwinfo,2/2] Add Lua support for regrulelist
diff mbox series

Message ID 8dbf417fb12e814f32bac58bf0b0bb1a6246f4e4.1556132277.git.mschiffer@universe-factory.net
State New
Delegated to: Matthias Schiffer
Headers show
Series
  • [OpenWrt-Devel,iwinfo,1/2] Add support for regulatory rules
Related show

Commit Message

Matthias Schiffer April 24, 2019, 7:03 p.m. UTC
Example output:

    # lua -e 'print(require("jsonc").stringify(require("iwinfo").nl80211.regrulelist(), true))'
    [
      {
        "flags": [
        ],
        "max_ant_gain_mbi": 0,
        "end_freq_khz": 2483500,
        "max_eirp_mbm": 2000,
        "start_freq_khz": 2400000,
        "max_bw_khz": 40000
      },
      {
        "flags": {
          "auto_bw": true,
          "no_outdoor": true
        },
        "max_ant_gain_mbi": 0,
        "end_freq_khz": 5250000,
        "max_eirp_mbm": 2000,
        "start_freq_khz": 5150000,
        "max_bw_khz": 80000
      },
      ...
    ]

We only add flags that are set to the "flags" field to avoid bloating the
output with a large number of entries for unset flags.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
---
 iwinfo_lua.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

Patch
diff mbox series

diff --git a/iwinfo_lua.c b/iwinfo_lua.c
index bb43438d8dd5..e91b9fc81455 100644
--- a/iwinfo_lua.c
+++ b/iwinfo_lua.c
@@ -477,6 +477,82 @@  static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int
 	return 1;
 }
 
+/* Wrapper for regulatory rules */
+static int iwinfo_L_regrulelist(lua_State *L, int (*func)(const char *, char *, int *))
+{
+	int i, x, len;
+	char rv[IWINFO_BUFSIZE];
+	const char *ifname = luaL_optstring(L, 1, NULL);
+	struct iwinfo_regrule_entry *e;
+
+	lua_newtable(L);
+	memset(rv, 0, sizeof(rv));
+
+	if (!(*func)(ifname, rv, &len))
+	{
+		for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_regrule_entry), x++)
+		{
+			e = (struct iwinfo_regrule_entry *) &rv[i];
+
+			lua_newtable(L);
+
+			lua_pushinteger(L, e->start_freq_khz);
+			lua_setfield(L, -2, "start_freq_khz");
+
+			lua_pushinteger(L, e->end_freq_khz);
+			lua_setfield(L, -2, "end_freq_khz");
+
+			lua_pushinteger(L, e->max_bw_khz);
+			lua_setfield(L, -2, "max_bw_khz");
+
+			lua_pushinteger(L, e->max_ant_gain_mbi);
+			lua_setfield(L, -2, "max_ant_gain_mbi");
+
+			lua_pushinteger(L, e->max_eirp_mbm);
+			lua_setfield(L, -2, "max_eirp_mbm");
+
+			if (e->flags & IWINFO_REGRULE_DFS) {
+				lua_pushinteger(L, e->dfs_cac_time_ms);
+				lua_setfield(L, -2, "dfs_cac_time_ms");
+			}
+
+			lua_newtable(L);
+
+#define HANDLE_FLAG(flag, strflag) \
+	do { \
+		if (e->flags & IWINFO_REGRULE_##flag) { \
+			lua_pushboolean(L, true); \
+			lua_setfield(L, -2, strflag); \
+		} \
+	} while (0)
+
+			HANDLE_FLAG(NO_OFDM, "no_ofdm");
+			HANDLE_FLAG(NO_CCK, "no_cck");
+			HANDLE_FLAG(NO_INDOOR, "no_indoor");
+			HANDLE_FLAG(NO_OUTDOOR, "no_outdoor");
+			HANDLE_FLAG(DFS, "dfs");
+			HANDLE_FLAG(PTP_ONLY, "ptp_only");
+			HANDLE_FLAG(AUTO_BW, "auto_bw");
+			HANDLE_FLAG(IR_CONCURRENT, "ir_concurrent");
+			HANDLE_FLAG(NO_HT40MINUS, "no_ht40minus");
+			HANDLE_FLAG(NO_HT40PLUS, "no_ht40plus");
+			HANDLE_FLAG(NO_80MHZ, "no_80mhz");
+			HANDLE_FLAG(NO_160MHZ, "no_160mhz");
+			HANDLE_FLAG(NO_IR, "no_ir");
+			HANDLE_FLAG(PASSIVE_SCAN, "passive_scan");
+			HANDLE_FLAG(NO_IBSS, "no_ibss");
+
+#undef HANDLE_FLAG
+
+			lua_setfield(L, -2, "flags");
+
+			lua_rawseti(L, -2, x);
+		}
+	}
+
+	return 1;
+}
+
 /* Wrapper for crypto settings */
 static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *))
 {
@@ -739,6 +815,7 @@  LUA_WRAP_STRUCT_OP(nl80211,freqlist)
 LUA_WRAP_STRUCT_OP(nl80211,countrylist)
 LUA_WRAP_STRUCT_OP(nl80211,hwmodelist)
 LUA_WRAP_STRUCT_OP(nl80211,htmodelist)
+LUA_WRAP_STRUCT_OP(nl80211,regrulelist)
 LUA_WRAP_STRUCT_OP(nl80211,encryption)
 LUA_WRAP_STRUCT_OP(nl80211,mbssid_support)
 LUA_WRAP_STRUCT_OP(nl80211,hardware_id)
@@ -862,6 +939,7 @@  static const luaL_reg R_nl80211[] = {
 	LUA_REG(nl80211,countrylist),
 	LUA_REG(nl80211,hwmodelist),
 	LUA_REG(nl80211,htmodelist),
+	LUA_REG(nl80211,regrulelist),
 	LUA_REG(nl80211,encryption),
 	LUA_REG(nl80211,mbssid_support),
 	LUA_REG(nl80211,hardware_id),