From 82a389246f849cd747c0275756b99d2c93e99e55 Mon Sep 17 00:00:00 2001 From: Javier Jorge Date: Fri, 22 Sep 2023 16:32:31 -0300 Subject: [PATCH 01/17] intial module structure --- packages/shared-state-bat_info/Makefile | 23 ++++++++++ .../publishers/shared-state-publish_bat_info | 1 + .../uci-defaults/shared-state_bat_info_cron | 9 ++++ .../usr/bin/shared-state-publish_bat_info | 46 +++++++++++++++++++ .../tests/test_shared-state-bat_info.lua | 33 +++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 packages/shared-state-bat_info/Makefile create mode 120000 packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info create mode 100755 packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron create mode 100755 packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info create mode 100644 packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua diff --git a/packages/shared-state-bat_info/Makefile b/packages/shared-state-bat_info/Makefile new file mode 100644 index 000000000..5b894b674 --- /dev/null +++ b/packages/shared-state-bat_info/Makefile @@ -0,0 +1,23 @@ +# +# Copyright (C) 2023 Javier Jorge +# Copyright (C) 2023 Asociación Civil Altermundi +# This is free software, licensed under the GNU Affero General Public License v3. +# + +include ../../libremesh.mk + +define Package/$(PKG_NAME) + SECTION:=lime + CATEGORY:=LibreMesh + TITLE:=Node information module for shared-state + MAINTAINER:= Javier + DEPENDS:=+lua +luci-lib-jsonc +ubus-lime-utils \ + +lime-system +ubus-lime-location shared-state + PKGARCH:=all +endef + +define Package/$(PKG_NAME)/description + Syncronize node information beween nodes. +endef + +$(eval $(call BuildPackage,$(PKG_NAME))) diff --git a/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info b/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info new file mode 120000 index 000000000..bac98ef4f --- /dev/null +++ b/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info @@ -0,0 +1 @@ +../../../usr/bin/shared-state-publish_bat_info \ No newline at end of file diff --git a/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron b/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron new file mode 100755 index 000000000..27132a824 --- /dev/null +++ b/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron @@ -0,0 +1,9 @@ +#!/bin/sh +unique_append() +{ + grep -qF "$1" "$2" || echo "$1" >> "$2" +} + +unique_append \ + '*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync node_info &> /dev/null)&)'\ + /etc/crontabs/root diff --git a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info new file mode 100755 index 000000000..309037c6c --- /dev/null +++ b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info @@ -0,0 +1,46 @@ +#!/usr/bin/lua + +--! LibreMesh +--! Copyright (C) 2023 Javier Jorge +--! Copyright (C) 2023 Asociación Civil Altermundi +--! +--! 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 . + +local JSON = require("luci.jsonc") +local network = require ("lime.network") +local location = require("lime.location") +local utils = require('lime.utils') +local config = require("lime.config") + +local hostname = utils.hostname() +function get_node_info() + local uci = config.get_uci_cursor() + local coords = location.get_node() or location.get_community() or + {lat="FIXME", long="FIXME"} + local fw_version = "no version" + pcall(function () fw_version = utils.release_info()['DISTRIB_RELEASE'] end) + local board = "no board" + pcall(function () board = utils.current_board() end) + local ipv4 = uci:get("network", "lan", "ipaddr") + local ipv6 = uci:get("network", "lan", "ip6addr") + if ipv6 then ipv6 = ipv6:gsub("/.*$", "") end + local uptime = utils.uptime_s() + local macs = network.get_own_macs("*") + return {hostname=hostname, firmware_version=fw_version, board=board, + ipv4=ipv4, ipv6=ipv6,coordinates=coords,macs= macs,uptime = uptime, + device = "Router" } +end + +local result = { [hostname] = get_node_info() } +io.popen("shared-state insert node_info", "w"):write(JSON.stringify(result)) diff --git a/packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua b/packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua new file mode 100644 index 000000000..498c419f6 --- /dev/null +++ b/packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua @@ -0,0 +1,33 @@ +local JSON = require("luci.jsonc") +local test_utils = require('tests.utils') +local utils = require('lime.utils') + +local uci = nil + +package.path = package.path .. ";packages/shared-state-node_info/files/usr/bin/?;;" +require ("shared-state-publish_node_info") + +describe('Tests network_nodes #network_nodes', function () + before_each('', function() + uci = test_utils.setup_test_uci() + stub(utils, "release_info", function () return {DISTRIB_RELEASE='2021.1'} end) + stub(utils, "current_board", function () return 'devboard' end) + end) + + after_each('', function() + test_utils.teardown_test_uci(uci) + end) + + it('a simple test to get node info and assert requiered fields are present', function() + uci = config.get_uci_cursor() + uci:set('network', 'lan', 'interface') + uci:set('network', 'lan', 'ipaddr', '10.5.0.5') + uci:set('network', 'lan', 'ip6addr', 'fd0d:fe46:8ce8::ab:cd00/64') + uci:commit('network') + nodeinfo = get_node_info() + assert.are.equal('devboard', nodeinfo.board) + assert.are.equal('2021.1', nodeinfo.firmware_version) + assert.are.equal('10.5.0.5', nodeinfo.ipv4) + assert.are.equal('fd0d:fe46:8ce8::ab:cd00', nodeinfo.ipv6) + end) +end) \ No newline at end of file From 2fdda9588b92879da443b2150333338a2dde967c Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 11:52:52 -0300 Subject: [PATCH 02/17] added batman link information publisher --- .../files/usr/lib/lua/lime/iwinfo_lua.c | 932 ++++++++++++++++++ packages/shared-state-bat_info/Makefile | 2 +- .../usr/bin/shared-state-publish_bat_info | 39 +- 3 files changed, 951 insertions(+), 22 deletions(-) create mode 100644 packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c diff --git a/packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c b/packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c new file mode 100644 index 000000000..1a9100123 --- /dev/null +++ b/packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c @@ -0,0 +1,932 @@ +/* + * iwinfo - Wireless Information Library - Lua Bindings + * + * Copyright (C) 2009 Jo-Philipp Wich + * + * The iwinfo library is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * The iwinfo library 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with the iwinfo library. If not, see http://www.gnu.org/licenses/. + */ + +#include "iwinfo/lua.h" + + +/* Determine type */ +static int iwinfo_L_type(lua_State *L) +{ + const char *ifname = luaL_checkstring(L, 1); + const char *type = iwinfo_type(ifname); + + if (type) + lua_pushstring(L, type); + else + lua_pushnil(L); + + return 1; +} + +/* Shutdown backends */ +static int iwinfo_L__gc(lua_State *L) +{ + iwinfo_finish(); + return 0; +} + +/* + * Build a short textual description of the crypto info + */ + +static char * iwinfo_crypto_print_ciphers(int ciphers) +{ + static char str[128] = { 0 }; + char *pos = str; + + if (ciphers & IWINFO_CIPHER_WEP40) + pos += sprintf(pos, "WEP-40, "); + + if (ciphers & IWINFO_CIPHER_WEP104) + pos += sprintf(pos, "WEP-104, "); + + if (ciphers & IWINFO_CIPHER_TKIP) + pos += sprintf(pos, "TKIP, "); + + if (ciphers & IWINFO_CIPHER_CCMP) + pos += sprintf(pos, "CCMP, "); + + if (ciphers & IWINFO_CIPHER_WRAP) + pos += sprintf(pos, "WRAP, "); + + if (ciphers & IWINFO_CIPHER_AESOCB) + pos += sprintf(pos, "AES-OCB, "); + + if (ciphers & IWINFO_CIPHER_CKIP) + pos += sprintf(pos, "CKIP, "); + + if (!ciphers || (ciphers & IWINFO_CIPHER_NONE)) + pos += sprintf(pos, "NONE, "); + + *(pos - 2) = 0; + + return str; +} + +static char * iwinfo_crypto_print_suites(int suites) +{ + static char str[64] = { 0 }; + char *pos = str; + + if (suites & IWINFO_KMGMT_PSK) + pos += sprintf(pos, "PSK/"); + + if (suites & IWINFO_KMGMT_8021x) + pos += sprintf(pos, "802.1X/"); + + if (!suites || (suites & IWINFO_KMGMT_NONE)) + pos += sprintf(pos, "NONE/"); + + *(pos - 1) = 0; + + return str; +} + +static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry *c) +{ + static char desc[512] = { 0 }; + + if (c) + { + if (c->enabled) + { + /* WEP */ + if (c->auth_algs && !c->wpa_version) + { + if ((c->auth_algs & IWINFO_AUTH_OPEN) && + (c->auth_algs & IWINFO_AUTH_SHARED)) + { + sprintf(desc, "WEP Open/Shared (%s)", + iwinfo_crypto_print_ciphers(c->pair_ciphers)); + } + else if (c->auth_algs & IWINFO_AUTH_OPEN) + { + sprintf(desc, "WEP Open System (%s)", + iwinfo_crypto_print_ciphers(c->pair_ciphers)); + } + else if (c->auth_algs & IWINFO_AUTH_SHARED) + { + sprintf(desc, "WEP Shared Auth (%s)", + iwinfo_crypto_print_ciphers(c->pair_ciphers)); + } + } + + /* WPA */ + else if (c->wpa_version) + { + switch (c->wpa_version) { + case 3: + sprintf(desc, "mixed WPA/WPA2 %s (%s)", + iwinfo_crypto_print_suites(c->auth_suites), + iwinfo_crypto_print_ciphers( + c->pair_ciphers & c->group_ciphers)); + break; + + case 2: + sprintf(desc, "WPA2 %s (%s)", + iwinfo_crypto_print_suites(c->auth_suites), + iwinfo_crypto_print_ciphers( + c->pair_ciphers & c->group_ciphers)); + break; + + case 1: + sprintf(desc, "WPA %s (%s)", + iwinfo_crypto_print_suites(c->auth_suites), + iwinfo_crypto_print_ciphers( + c->pair_ciphers & c->group_ciphers)); + break; + } + } + else + { + sprintf(desc, "None"); + } + } + else + { + sprintf(desc, "None"); + } + } + else + { + sprintf(desc, "Unknown"); + } + + return desc; +} + +/* Build Lua table from crypto data */ +static void iwinfo_L_cryptotable(lua_State *L, struct iwinfo_crypto_entry *c) +{ + int i, j; + + lua_newtable(L); + + lua_pushboolean(L, c->enabled); + lua_setfield(L, -2, "enabled"); + + lua_pushstring(L, iwinfo_crypto_desc(c)); + lua_setfield(L, -2, "description"); + + lua_pushboolean(L, (c->enabled && !c->wpa_version)); + lua_setfield(L, -2, "wep"); + + lua_pushinteger(L, c->wpa_version); + lua_setfield(L, -2, "wpa"); + + lua_newtable(L); + for (i = 0, j = 1; i < 8; i++) + { + if (c->pair_ciphers & (1 << i)) + { + lua_pushstring(L, IWINFO_CIPHER_NAMES[i]); + lua_rawseti(L, -2, j++); + } + } + lua_setfield(L, -2, "pair_ciphers"); + + lua_newtable(L); + for (i = 0, j = 1; i < 8; i++) + { + if (c->group_ciphers & (1 << i)) + { + lua_pushstring(L, IWINFO_CIPHER_NAMES[i]); + lua_rawseti(L, -2, j++); + } + } + lua_setfield(L, -2, "group_ciphers"); + + lua_newtable(L); + for (i = 0, j = 1; i < 8; i++) + { + if (c->auth_suites & (1 << i)) + { + lua_pushstring(L, IWINFO_KMGMT_NAMES[i]); + lua_rawseti(L, -2, j++); + } + } + lua_setfield(L, -2, "auth_suites"); + + lua_newtable(L); + for (i = 0, j = 1; i < 8; i++) + { + if (c->auth_algs & (1 << i)) + { + lua_pushstring(L, IWINFO_AUTH_NAMES[i]); + lua_rawseti(L, -2, j++); + } + } + lua_setfield(L, -2, "auth_algs"); +} + + +/* Wrapper for mode */ +static int iwinfo_L_mode(lua_State *L, int (*func)(const char *, int *)) +{ + int mode; + const char *ifname = luaL_checkstring(L, 1); + + if ((*func)(ifname, &mode)) + mode = IWINFO_OPMODE_UNKNOWN; + + lua_pushstring(L, IWINFO_OPMODE_NAMES[mode]); + return 1; +} + +/* Wrapper for assoclist */ +static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *)) +{ + int i, len; + char rv[IWINFO_BUFSIZE]; + char macstr[18]; + const char *ifname = luaL_checkstring(L, 1); + struct iwinfo_assoclist_entry *e; + + lua_newtable(L); + memset(rv, 0, sizeof(rv)); + + if (!(*func)(ifname, rv, &len)) + { + for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry)) + { + e = (struct iwinfo_assoclist_entry *) &rv[i]; + + sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", + e->mac[0], e->mac[1], e->mac[2], + e->mac[3], e->mac[4], e->mac[5]); + + lua_newtable(L); + + lua_pushnumber(L, e->signal); + lua_setfield(L, -2, "signal"); + + lua_pushnumber(L, e->noise); + lua_setfield(L, -2, "noise"); + + lua_pushnumber(L, e->inactive); + lua_setfield(L, -2, "inactive"); + + lua_pushnumber(L, e->rx_packets); + lua_setfield(L, -2, "rx_packets"); + + lua_pushnumber(L, e->tx_packets); + lua_setfield(L, -2, "tx_packets"); + + lua_pushnumber(L, e->rx_rate.rate); + lua_setfield(L, -2, "rx_rate"); + + lua_pushnumber(L, e->tx_rate.rate); + lua_setfield(L, -2, "tx_rate"); + + if (e->rx_rate.mcs >= 0) + { + lua_pushnumber(L, e->rx_rate.mcs); + lua_setfield(L, -2, "rx_mcs"); + + lua_pushboolean(L, e->rx_rate.is_40mhz); + lua_setfield(L, -2, "rx_40mhz"); + + lua_pushboolean(L, e->rx_rate.is_short_gi); + lua_setfield(L, -2, "rx_short_gi"); + } + + if (e->tx_rate.mcs >= 0) + { + lua_pushnumber(L, e->tx_rate.mcs); + lua_setfield(L, -2, "tx_mcs"); + + lua_pushboolean(L, e->tx_rate.is_40mhz); + lua_setfield(L, -2, "tx_40mhz"); + + lua_pushboolean(L, e->tx_rate.is_short_gi); + lua_setfield(L, -2, "tx_short_gi"); + } + + lua_setfield(L, -2, macstr); + } + } + + return 1; +} + +/* Wrapper for tx power list */ +static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *)) +{ + int i, x, len; + char rv[IWINFO_BUFSIZE]; + const char *ifname = luaL_checkstring(L, 1); + struct iwinfo_txpwrlist_entry *e; + + memset(rv, 0, sizeof(rv)); + + if (!(*func)(ifname, rv, &len)) + { + lua_newtable(L); + + for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++) + { + e = (struct iwinfo_txpwrlist_entry *) &rv[i]; + + lua_newtable(L); + + lua_pushnumber(L, e->mw); + lua_setfield(L, -2, "mw"); + + lua_pushnumber(L, e->dbm); + lua_setfield(L, -2, "dbm"); + + lua_rawseti(L, -2, x); + } + + return 1; + } + + return 0; +} + +/* Wrapper for scan list */ +static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *)) +{ + int i, x, len = 0; + char rv[IWINFO_BUFSIZE]; + char macstr[18]; + const char *ifname = luaL_checkstring(L, 1); + struct iwinfo_scanlist_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_scanlist_entry), x++) + { + e = (struct iwinfo_scanlist_entry *) &rv[i]; + + lua_newtable(L); + + /* BSSID */ + sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", + e->mac[0], e->mac[1], e->mac[2], + e->mac[3], e->mac[4], e->mac[5]); + + lua_pushstring(L, macstr); + lua_setfield(L, -2, "bssid"); + + /* ESSID */ + if (e->ssid[0]) + { + lua_pushstring(L, (char *) e->ssid); + lua_setfield(L, -2, "ssid"); + } + + /* Channel */ + lua_pushinteger(L, e->channel); + lua_setfield(L, -2, "channel"); + + /* Mode */ + lua_pushstring(L, IWINFO_OPMODE_NAMES[e->mode]); + lua_setfield(L, -2, "mode"); + + /* Quality, Signal */ + lua_pushinteger(L, e->quality); + lua_setfield(L, -2, "quality"); + + lua_pushinteger(L, e->quality_max); + lua_setfield(L, -2, "quality_max"); + + lua_pushnumber(L, (e->signal - 0x100)); + lua_setfield(L, -2, "signal"); + + /* Crypto */ + iwinfo_L_cryptotable(L, &e->crypto); + lua_setfield(L, -2, "encryption"); + + lua_rawseti(L, -2, x); + } + } + + return 1; +} + +/* Wrapper for frequency list */ +static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *)) +{ + int i, x, len; + char rv[IWINFO_BUFSIZE]; + const char *ifname = luaL_checkstring(L, 1); + struct iwinfo_freqlist_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_freqlist_entry), x++) + { + e = (struct iwinfo_freqlist_entry *) &rv[i]; + + lua_newtable(L); + + /* MHz */ + lua_pushinteger(L, e->mhz); + lua_setfield(L, -2, "mhz"); + + /* Channel */ + lua_pushinteger(L, e->channel); + lua_setfield(L, -2, "channel"); + + /* Restricted (DFS/TPC/Radar) */ + lua_pushboolean(L, e->restricted); + lua_setfield(L, -2, "restricted"); + + lua_rawseti(L, -2, x); + } + } + + return 1; +} + +/* Wrapper for crypto settings */ +static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *)) +{ + const char *ifname = luaL_checkstring(L, 1); + struct iwinfo_crypto_entry c = { 0 }; + + if (!(*func)(ifname, (char *)&c)) + { + iwinfo_L_cryptotable(L, &c); + return 1; + } + + lua_pushnil(L); + return 1; +} + +/* Wrapper for hwmode list */ +static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *)) +{ + const char *ifname = luaL_checkstring(L, 1); + int hwmodes = 0; + + if (!(*func)(ifname, &hwmodes)) + { + lua_newtable(L); + + lua_pushboolean(L, hwmodes & IWINFO_80211_A); + lua_setfield(L, -2, "a"); + + lua_pushboolean(L, hwmodes & IWINFO_80211_B); + lua_setfield(L, -2, "b"); + + lua_pushboolean(L, hwmodes & IWINFO_80211_G); + lua_setfield(L, -2, "g"); + + lua_pushboolean(L, hwmodes & IWINFO_80211_N); + lua_setfield(L, -2, "n"); + + lua_pushboolean(L, hwmodes & IWINFO_80211_AC); + lua_setfield(L, -2, "ac"); + + return 1; + } + + lua_pushnil(L); + return 1; +} + +/* Wrapper for htmode list */ +static int iwinfo_L_htmodelist(lua_State *L, int (*func)(const char *, int *)) +{ + const char *ifname = luaL_checkstring(L, 1); + int i, htmodes = 0; + + if (!(*func)(ifname, &htmodes)) + { + lua_newtable(L); + + for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++) + { + lua_pushboolean(L, htmodes & (1 << i)); + lua_setfield(L, -2, IWINFO_HTMODE_NAMES[i]); + } + + return 1; + } + + lua_pushnil(L); + return 1; +} + +/* Wrapper for mbssid_support */ +static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *)) +{ + const char *ifname = luaL_checkstring(L, 1); + int support = 0; + + if (!(*func)(ifname, &support)) + { + lua_pushboolean(L, support); + return 1; + } + + lua_pushnil(L); + return 1; +} + +/* Wrapper for hardware_id */ +static int iwinfo_L_hardware_id(lua_State *L, int (*func)(const char *, char *)) +{ + const char *ifname = luaL_checkstring(L, 1); + struct iwinfo_hardware_id ids; + + if (!(*func)(ifname, (char *)&ids)) + { + lua_newtable(L); + + lua_pushnumber(L, ids.vendor_id); + lua_setfield(L, -2, "vendor_id"); + + lua_pushnumber(L, ids.device_id); + lua_setfield(L, -2, "device_id"); + + lua_pushnumber(L, ids.subsystem_vendor_id); + lua_setfield(L, -2, "subsystem_vendor_id"); + + lua_pushnumber(L, ids.subsystem_device_id); + lua_setfield(L, -2, "subsystem_device_id"); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +/* Wrapper for country list */ +static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166) +{ + int i; + struct iwinfo_country_entry *c; + + for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry)) + { + c = (struct iwinfo_country_entry *) &buf[i]; + + if (c->iso3166 == iso3166) + return c->ccode; + } + + return NULL; +} + +static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *)) +{ + int len, i; + char rv[IWINFO_BUFSIZE], alpha2[3]; + char *ccode; + const char *ifname = luaL_checkstring(L, 1); + const struct iwinfo_iso3166_label *l; + + lua_newtable(L); + memset(rv, 0, sizeof(rv)); + + if (!(*func)(ifname, rv, &len)) + { + for (l = IWINFO_ISO3166_NAMES, i = 1; l->iso3166; l++) + { + if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL) + { + sprintf(alpha2, "%c%c", + (l->iso3166 / 256), (l->iso3166 % 256)); + + lua_newtable(L); + + lua_pushstring(L, alpha2); + lua_setfield(L, -2, "alpha2"); + + lua_pushstring(L, ccode); + lua_setfield(L, -2, "ccode"); + + lua_pushstring(L, l->name); + lua_setfield(L, -2, "name"); + + lua_rawseti(L, -2, i++); + } + } + } + + return 1; +} + + +#ifdef USE_WL +/* Broadcom */ +LUA_WRAP_INT_OP(wl,channel) +LUA_WRAP_INT_OP(wl,frequency) +LUA_WRAP_INT_OP(wl,frequency_offset) +LUA_WRAP_INT_OP(wl,txpower) +LUA_WRAP_INT_OP(wl,txpower_offset) +LUA_WRAP_INT_OP(wl,bitrate) +LUA_WRAP_INT_OP(wl,signal) +LUA_WRAP_INT_OP(wl,noise) +LUA_WRAP_INT_OP(wl,quality) +LUA_WRAP_INT_OP(wl,quality_max) +LUA_WRAP_STRING_OP(wl,ssid) +LUA_WRAP_STRING_OP(wl,bssid) +LUA_WRAP_STRING_OP(wl,country) +LUA_WRAP_STRING_OP(wl,hardware_name) +LUA_WRAP_STRING_OP(wl,phyname) +LUA_WRAP_STRUCT_OP(wl,mode) +LUA_WRAP_STRUCT_OP(wl,assoclist) +LUA_WRAP_STRUCT_OP(wl,txpwrlist) +LUA_WRAP_STRUCT_OP(wl,scanlist) +LUA_WRAP_STRUCT_OP(wl,freqlist) +LUA_WRAP_STRUCT_OP(wl,countrylist) +LUA_WRAP_STRUCT_OP(wl,hwmodelist) +LUA_WRAP_STRUCT_OP(wl,htmodelist) +LUA_WRAP_STRUCT_OP(wl,encryption) +LUA_WRAP_STRUCT_OP(wl,mbssid_support) +LUA_WRAP_STRUCT_OP(wl,hardware_id) +#endif + +#ifdef USE_MADWIFI +/* Madwifi */ +LUA_WRAP_INT_OP(madwifi,channel) +LUA_WRAP_INT_OP(madwifi,frequency) +LUA_WRAP_INT_OP(madwifi,frequency_offset) +LUA_WRAP_INT_OP(madwifi,txpower) +LUA_WRAP_INT_OP(madwifi,txpower_offset) +LUA_WRAP_INT_OP(madwifi,bitrate) +LUA_WRAP_INT_OP(madwifi,signal) +LUA_WRAP_INT_OP(madwifi,noise) +LUA_WRAP_INT_OP(madwifi,quality) +LUA_WRAP_INT_OP(madwifi,quality_max) +LUA_WRAP_STRING_OP(madwifi,ssid) +LUA_WRAP_STRING_OP(madwifi,bssid) +LUA_WRAP_STRING_OP(madwifi,country) +LUA_WRAP_STRING_OP(madwifi,hardware_name) +LUA_WRAP_STRING_OP(madwifi,phyname) +LUA_WRAP_STRUCT_OP(madwifi,mode) +LUA_WRAP_STRUCT_OP(madwifi,assoclist) +LUA_WRAP_STRUCT_OP(madwifi,txpwrlist) +LUA_WRAP_STRUCT_OP(madwifi,scanlist) +LUA_WRAP_STRUCT_OP(madwifi,freqlist) +LUA_WRAP_STRUCT_OP(madwifi,countrylist) +LUA_WRAP_STRUCT_OP(madwifi,hwmodelist) +LUA_WRAP_STRUCT_OP(madwifi,htmodelist) +LUA_WRAP_STRUCT_OP(madwifi,encryption) +LUA_WRAP_STRUCT_OP(madwifi,mbssid_support) +LUA_WRAP_STRUCT_OP(madwifi,hardware_id) +#endif + +#ifdef USE_NL80211 +/* NL80211 */ +LUA_WRAP_INT_OP(nl80211,channel) +LUA_WRAP_INT_OP(nl80211,frequency) +LUA_WRAP_INT_OP(nl80211,frequency_offset) +LUA_WRAP_INT_OP(nl80211,txpower) +LUA_WRAP_INT_OP(nl80211,txpower_offset) +LUA_WRAP_INT_OP(nl80211,bitrate) +LUA_WRAP_INT_OP(nl80211,signal) +LUA_WRAP_INT_OP(nl80211,noise) +LUA_WRAP_INT_OP(nl80211,quality) +LUA_WRAP_INT_OP(nl80211,quality_max) +LUA_WRAP_STRING_OP(nl80211,ssid) +LUA_WRAP_STRING_OP(nl80211,bssid) +LUA_WRAP_STRING_OP(nl80211,country) +LUA_WRAP_STRING_OP(nl80211,hardware_name) +LUA_WRAP_STRING_OP(nl80211,phyname) +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,freqlist) +LUA_WRAP_STRUCT_OP(nl80211,countrylist) +LUA_WRAP_STRUCT_OP(nl80211,hwmodelist) +LUA_WRAP_STRUCT_OP(nl80211,htmodelist) +LUA_WRAP_STRUCT_OP(nl80211,encryption) +LUA_WRAP_STRUCT_OP(nl80211,mbssid_support) +LUA_WRAP_STRUCT_OP(nl80211,hardware_id) +#endif + +/* Wext */ +LUA_WRAP_INT_OP(wext,channel) +LUA_WRAP_INT_OP(wext,frequency) +LUA_WRAP_INT_OP(wext,frequency_offset) +LUA_WRAP_INT_OP(wext,txpower) +LUA_WRAP_INT_OP(wext,txpower_offset) +LUA_WRAP_INT_OP(wext,bitrate) +LUA_WRAP_INT_OP(wext,signal) +LUA_WRAP_INT_OP(wext,noise) +LUA_WRAP_INT_OP(wext,quality) +LUA_WRAP_INT_OP(wext,quality_max) +LUA_WRAP_STRING_OP(wext,ssid) +LUA_WRAP_STRING_OP(wext,bssid) +LUA_WRAP_STRING_OP(wext,country) +LUA_WRAP_STRING_OP(wext,hardware_name) +LUA_WRAP_STRING_OP(wext,phyname) +LUA_WRAP_STRUCT_OP(wext,mode) +LUA_WRAP_STRUCT_OP(wext,assoclist) +LUA_WRAP_STRUCT_OP(wext,txpwrlist) +LUA_WRAP_STRUCT_OP(wext,scanlist) +LUA_WRAP_STRUCT_OP(wext,freqlist) +LUA_WRAP_STRUCT_OP(wext,countrylist) +LUA_WRAP_STRUCT_OP(wext,hwmodelist) +LUA_WRAP_STRUCT_OP(wext,htmodelist) +LUA_WRAP_STRUCT_OP(wext,encryption) +LUA_WRAP_STRUCT_OP(wext,mbssid_support) +LUA_WRAP_STRUCT_OP(wext,hardware_id) + +#ifdef USE_WL +/* Broadcom table */ +static const luaL_reg R_wl[] = { + LUA_REG(wl,channel), + LUA_REG(wl,frequency), + LUA_REG(wl,frequency_offset), + LUA_REG(wl,txpower), + LUA_REG(wl,txpower_offset), + LUA_REG(wl,bitrate), + LUA_REG(wl,signal), + LUA_REG(wl,noise), + LUA_REG(wl,quality), + LUA_REG(wl,quality_max), + LUA_REG(wl,mode), + LUA_REG(wl,ssid), + LUA_REG(wl,bssid), + LUA_REG(wl,country), + LUA_REG(wl,assoclist), + LUA_REG(wl,txpwrlist), + LUA_REG(wl,scanlist), + LUA_REG(wl,freqlist), + LUA_REG(wl,countrylist), + LUA_REG(wl,hwmodelist), + LUA_REG(wl,htmodelist), + LUA_REG(wl,encryption), + LUA_REG(wl,mbssid_support), + LUA_REG(wl,hardware_id), + LUA_REG(wl,hardware_name), + LUA_REG(wl,phyname), + { NULL, NULL } +}; +#endif + +#ifdef USE_MADWIFI +/* Madwifi table */ +static const luaL_reg R_madwifi[] = { + LUA_REG(madwifi,channel), + LUA_REG(madwifi,frequency), + LUA_REG(madwifi,frequency_offset), + LUA_REG(madwifi,txpower), + LUA_REG(madwifi,txpower_offset), + LUA_REG(madwifi,bitrate), + LUA_REG(madwifi,signal), + LUA_REG(madwifi,noise), + LUA_REG(madwifi,quality), + LUA_REG(madwifi,quality_max), + LUA_REG(madwifi,mode), + LUA_REG(madwifi,ssid), + LUA_REG(madwifi,bssid), + LUA_REG(madwifi,country), + LUA_REG(madwifi,assoclist), + LUA_REG(madwifi,txpwrlist), + LUA_REG(madwifi,scanlist), + LUA_REG(madwifi,freqlist), + LUA_REG(madwifi,countrylist), + LUA_REG(madwifi,hwmodelist), + LUA_REG(madwifi,htmodelist), + LUA_REG(madwifi,encryption), + LUA_REG(madwifi,mbssid_support), + LUA_REG(madwifi,hardware_id), + LUA_REG(madwifi,hardware_name), + LUA_REG(madwifi,phyname), + { NULL, NULL } +}; +#endif + +#ifdef USE_NL80211 +/* NL80211 table */ +static const luaL_reg R_nl80211[] = { + LUA_REG(nl80211,channel), + LUA_REG(nl80211,frequency), + LUA_REG(nl80211,frequency_offset), + LUA_REG(nl80211,txpower), + LUA_REG(nl80211,txpower_offset), + LUA_REG(nl80211,bitrate), + LUA_REG(nl80211,signal), + LUA_REG(nl80211,noise), + LUA_REG(nl80211,quality), + LUA_REG(nl80211,quality_max), + LUA_REG(nl80211,mode), + LUA_REG(nl80211,ssid), + LUA_REG(nl80211,bssid), + LUA_REG(nl80211,country), + LUA_REG(nl80211,assoclist), + LUA_REG(nl80211,txpwrlist), + LUA_REG(nl80211,scanlist), + LUA_REG(nl80211,freqlist), + LUA_REG(nl80211,countrylist), + LUA_REG(nl80211,hwmodelist), + LUA_REG(nl80211,htmodelist), + LUA_REG(nl80211,encryption), + LUA_REG(nl80211,mbssid_support), + LUA_REG(nl80211,hardware_id), + LUA_REG(nl80211,hardware_name), + LUA_REG(nl80211,phyname), + { NULL, NULL } +}; +#endif + +/* Wext table */ +static const luaL_reg R_wext[] = { + LUA_REG(wext,channel), + LUA_REG(wext,frequency), + LUA_REG(wext,frequency_offset), + LUA_REG(wext,txpower), + LUA_REG(wext,txpower_offset), + LUA_REG(wext,bitrate), + LUA_REG(wext,signal), + LUA_REG(wext,noise), + LUA_REG(wext,quality), + LUA_REG(wext,quality_max), + LUA_REG(wext,mode), + LUA_REG(wext,ssid), + LUA_REG(wext,bssid), + LUA_REG(wext,country), + LUA_REG(wext,assoclist), + LUA_REG(wext,txpwrlist), + LUA_REG(wext,scanlist), + LUA_REG(wext,freqlist), + LUA_REG(wext,countrylist), + LUA_REG(wext,hwmodelist), + LUA_REG(wext,htmodelist), + LUA_REG(wext,encryption), + LUA_REG(wext,mbssid_support), + LUA_REG(wext,hardware_id), + LUA_REG(wext,hardware_name), + LUA_REG(wext,phyname), + { NULL, NULL } +}; + +/* Common */ +static const luaL_reg R_common[] = { + { "type", iwinfo_L_type }, + { "__gc", iwinfo_L__gc }, + { NULL, NULL } +}; + + +LUALIB_API int luaopen_iwinfo(lua_State *L) { + luaL_register(L, IWINFO_META, R_common); + +#ifdef USE_WL + luaL_newmetatable(L, IWINFO_WL_META); + luaL_register(L, NULL, R_common); + luaL_register(L, NULL, R_wl); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_setfield(L, -2, "wl"); +#endif + +#ifdef USE_MADWIFI + luaL_newmetatable(L, IWINFO_MADWIFI_META); + luaL_register(L, NULL, R_common); + luaL_register(L, NULL, R_madwifi); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_setfield(L, -2, "madwifi"); +#endif + +#ifdef USE_NL80211 + luaL_newmetatable(L, IWINFO_NL80211_META); + luaL_register(L, NULL, R_common); + luaL_register(L, NULL, R_nl80211); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_setfield(L, -2, "nl80211"); +#endif + + luaL_newmetatable(L, IWINFO_WEXT_META); + luaL_register(L, NULL, R_common); + luaL_register(L, NULL, R_wext); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + lua_setfield(L, -2, "wext"); + + return 1; +} diff --git a/packages/shared-state-bat_info/Makefile b/packages/shared-state-bat_info/Makefile index 5b894b674..03ce4a825 100644 --- a/packages/shared-state-bat_info/Makefile +++ b/packages/shared-state-bat_info/Makefile @@ -17,7 +17,7 @@ define Package/$(PKG_NAME) endef define Package/$(PKG_NAME)/description - Syncronize node information beween nodes. + Syncronize batman protocol links information beween nodes. endef $(eval $(call BuildPackage,$(PKG_NAME))) diff --git a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info index 309037c6c..cac76065a 100755 --- a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info +++ b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info @@ -18,29 +18,26 @@ --! along with this program. If not, see . local JSON = require("luci.jsonc") -local network = require ("lime.network") -local location = require("lime.location") local utils = require('lime.utils') -local config = require("lime.config") +local network = require ("lime.network") + local hostname = utils.hostname() -function get_node_info() - local uci = config.get_uci_cursor() - local coords = location.get_node() or location.get_community() or - {lat="FIXME", long="FIXME"} - local fw_version = "no version" - pcall(function () fw_version = utils.release_info()['DISTRIB_RELEASE'] end) - local board = "no board" - pcall(function () board = utils.current_board() end) - local ipv4 = uci:get("network", "lan", "ipaddr") - local ipv6 = uci:get("network", "lan", "ip6addr") - if ipv6 then ipv6 = ipv6:gsub("/.*$", "") end - local uptime = utils.uptime_s() - local macs = network.get_own_macs("*") - return {hostname=hostname, firmware_version=fw_version, board=board, - ipv4=ipv4, ipv6=ipv6,coordinates=coords,macs= macs,uptime = uptime, - device = "Router" } +function get_bat_links_info() + + batneights = utils.unsafe_shell("batctl nj") + batneights = string.gsub(batneights,"neigh_address","dst_mac") + batneights = string.gsub(batneights,"hard_ifname","iface") + batneights_obj = JSON.parse(batneights) + for key,value in pairs (batneights_obj) do + macparts = network.get_mac(value.iface) + src_macaddr = table.concat(macparts,":") + value.src_mac=src_macaddr + utils.printJson(value) + end + utils.printJson(batneights_obj) + return batneights_obj end -local result = { [hostname] = get_node_info() } -io.popen("shared-state insert node_info", "w"):write(JSON.stringify(result)) +local result = { [hostname] = get_bat_links_info() } +io.popen("shared-state insert bat_links_info", "w"):write(JSON.stringify(result)) From 1aed507fed27a5306b54f639da9d10730cb9b8c0 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 12:16:13 -0300 Subject: [PATCH 03/17] removed debug prints --- .../files/usr/bin/shared-state-publish_bat_info | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info index cac76065a..b11f6f444 100755 --- a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info +++ b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info @@ -33,9 +33,7 @@ function get_bat_links_info() macparts = network.get_mac(value.iface) src_macaddr = table.concat(macparts,":") value.src_mac=src_macaddr - utils.printJson(value) end - utils.printJson(batneights_obj) return batneights_obj end From db4a0fd7d6af9899e02ed61b75147cebd0750605 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 12:23:05 -0300 Subject: [PATCH 04/17] consistent renaming --- .../etc/shared-state/publishers/shared-state-publish_bat_info | 1 - .../shared-state/publishers/shared-state-publish_bat_links_info | 1 + ...red-state_bat_info_cron => shared-state_bat_links_info_cron} | 2 +- ...ate-publish_bat_info => shared-state-publish_bat_links_info} | 0 4 files changed, 2 insertions(+), 2 deletions(-) delete mode 120000 packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info create mode 120000 packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info rename packages/shared-state-bat_info/files/etc/uci-defaults/{shared-state_bat_info_cron => shared-state_bat_links_info_cron} (53%) rename packages/shared-state-bat_info/files/usr/bin/{shared-state-publish_bat_info => shared-state-publish_bat_links_info} (100%) diff --git a/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info b/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info deleted file mode 120000 index bac98ef4f..000000000 --- a/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_info +++ /dev/null @@ -1 +0,0 @@ -../../../usr/bin/shared-state-publish_bat_info \ No newline at end of file diff --git a/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info b/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info new file mode 120000 index 000000000..895385186 --- /dev/null +++ b/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info @@ -0,0 +1 @@ +../../../usr/bin/shared-state-publish_bat_links_info \ No newline at end of file diff --git a/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron b/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_links_info_cron similarity index 53% rename from packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron rename to packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_links_info_cron index 27132a824..f540eb89a 100755 --- a/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_info_cron +++ b/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_links_info_cron @@ -5,5 +5,5 @@ unique_append() } unique_append \ - '*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync node_info &> /dev/null)&)'\ + '*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync bat_links_info &> /dev/null)&)'\ /etc/crontabs/root diff --git a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info b/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_links_info similarity index 100% rename from packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_info rename to packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_links_info From 9ee99220b0a7007a64005a1b71771f4daa242087 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 13:31:06 -0300 Subject: [PATCH 05/17] fixed folder name --- .../tests/test_shared-state-bat_info.lua | 33 ------------------- .../Makefile | 0 .../shared-state-publish_bat_links_info | 0 .../shared-state_bat_links_info_cron | 0 .../bin/shared-state-publish_bat_links_info | 4 +-- .../test_shared-state-bat_links_info.lua | 28 ++++++++++++++++ 6 files changed, 30 insertions(+), 35 deletions(-) delete mode 100644 packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua rename packages/{shared-state-bat_info => shared-state-bat_links_info}/Makefile (100%) rename packages/{shared-state-bat_info => shared-state-bat_links_info}/files/etc/shared-state/publishers/shared-state-publish_bat_links_info (100%) rename packages/{shared-state-bat_info => shared-state-bat_links_info}/files/etc/uci-defaults/shared-state_bat_links_info_cron (100%) rename packages/{shared-state-bat_info => shared-state-bat_links_info}/files/usr/bin/shared-state-publish_bat_links_info (97%) create mode 100644 packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua diff --git a/packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua b/packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua deleted file mode 100644 index 498c419f6..000000000 --- a/packages/shared-state-bat_info/tests/test_shared-state-bat_info.lua +++ /dev/null @@ -1,33 +0,0 @@ -local JSON = require("luci.jsonc") -local test_utils = require('tests.utils') -local utils = require('lime.utils') - -local uci = nil - -package.path = package.path .. ";packages/shared-state-node_info/files/usr/bin/?;;" -require ("shared-state-publish_node_info") - -describe('Tests network_nodes #network_nodes', function () - before_each('', function() - uci = test_utils.setup_test_uci() - stub(utils, "release_info", function () return {DISTRIB_RELEASE='2021.1'} end) - stub(utils, "current_board", function () return 'devboard' end) - end) - - after_each('', function() - test_utils.teardown_test_uci(uci) - end) - - it('a simple test to get node info and assert requiered fields are present', function() - uci = config.get_uci_cursor() - uci:set('network', 'lan', 'interface') - uci:set('network', 'lan', 'ipaddr', '10.5.0.5') - uci:set('network', 'lan', 'ip6addr', 'fd0d:fe46:8ce8::ab:cd00/64') - uci:commit('network') - nodeinfo = get_node_info() - assert.are.equal('devboard', nodeinfo.board) - assert.are.equal('2021.1', nodeinfo.firmware_version) - assert.are.equal('10.5.0.5', nodeinfo.ipv4) - assert.are.equal('fd0d:fe46:8ce8::ab:cd00', nodeinfo.ipv6) - end) -end) \ No newline at end of file diff --git a/packages/shared-state-bat_info/Makefile b/packages/shared-state-bat_links_info/Makefile similarity index 100% rename from packages/shared-state-bat_info/Makefile rename to packages/shared-state-bat_links_info/Makefile diff --git a/packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info similarity index 100% rename from packages/shared-state-bat_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info rename to packages/shared-state-bat_links_info/files/etc/shared-state/publishers/shared-state-publish_bat_links_info diff --git a/packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_links_info_cron b/packages/shared-state-bat_links_info/files/etc/uci-defaults/shared-state_bat_links_info_cron similarity index 100% rename from packages/shared-state-bat_info/files/etc/uci-defaults/shared-state_bat_links_info_cron rename to packages/shared-state-bat_links_info/files/etc/uci-defaults/shared-state_bat_links_info_cron diff --git a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info similarity index 97% rename from packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_links_info rename to packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index b11f6f444..e27cd3c09 100755 --- a/packages/shared-state-bat_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -21,11 +21,11 @@ local JSON = require("luci.jsonc") local utils = require('lime.utils') local network = require ("lime.network") - local hostname = utils.hostname() function get_bat_links_info() - + batneights_obj={} batneights = utils.unsafe_shell("batctl nj") + print (batneights) batneights = string.gsub(batneights,"neigh_address","dst_mac") batneights = string.gsub(batneights,"hard_ifname","iface") batneights_obj = JSON.parse(batneights) diff --git a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua new file mode 100644 index 000000000..46e3e5630 --- /dev/null +++ b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua @@ -0,0 +1,28 @@ +local utils = require "lime.utils" + +package.path = package.path .. ";packages/shared-state-bat_links_info/files/usr/bin/?;;" +require ("shared-state-publish_bat_links_info") + +local sample_batman_nj = + +describe('Tests bat_links_info #bat_links_info', function () + it('a simple test to get node info and assert requiered fields are present', function() + utils.log("presss") + + stub(utils, "unsafe_shell", function (cmd) + if cmd == "batctl nj" then + utils.log(" tubbbbbbbbbbbbbbbbbbbb") + else + utils.log("sssssss") + end + return '[{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1990,"neigh_address":"02:58:47:da:4e:aa"},{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1690,"neigh_address":"02:58:47:46:28:95"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":1510,"neigh_address":"02:ab:46:da:4e:aa"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":970,"neigh_address":"02:ab:46:46:28:95"}]' + end) + + local links_info = {} + + links_info = get_bat_links_info() + assert.are.equal('"02:58:47:da:4e:aa"', links_info[0].dst_mac) + utils.printJson(links_info) + + end) +end) \ No newline at end of file From 79e7dba4316e44b790758d0ff212fd3a27c4a309 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 13:56:31 -0300 Subject: [PATCH 06/17] Added test case --- .../bin/shared-state-publish_bat_links_info | 5 ++- .../test_shared-state-bat_links_info.lua | 35 +++++++++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index e27cd3c09..83d36bd7b 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -23,9 +23,8 @@ local network = require ("lime.network") local hostname = utils.hostname() function get_bat_links_info() - batneights_obj={} - batneights = utils.unsafe_shell("batctl nj") - print (batneights) + local batneights_obj={} + local batneights = utils.unsafe_shell("batctl nj") batneights = string.gsub(batneights,"neigh_address","dst_mac") batneights = string.gsub(batneights,"hard_ifname","iface") batneights_obj = JSON.parse(batneights) diff --git a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua index 46e3e5630..94982926e 100644 --- a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua +++ b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua @@ -1,28 +1,27 @@ local utils = require "lime.utils" +local network = require ("lime.network") +local iwinfo = require "iwinfo" -package.path = package.path .. ";packages/shared-state-bat_links_info/files/usr/bin/?;;" -require ("shared-state-publish_bat_links_info") -local sample_batman_nj = describe('Tests bat_links_info #bat_links_info', function () + stub(utils, "unsafe_shell", function (cmd) + return '[{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1990,"neigh_address":"02:58:47:da:4e:aa"},{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1690,"neigh_address":"02:58:47:46:28:95"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":1510,"neigh_address":"02:ab:46:da:4e:aa"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":970,"neigh_address":"02:ab:46:46:28:95"}]' + end) + stub(network, "get_mac", function (iface) + return iwinfo.mocks.wlan1_mesh_mac + end) + + package.path = package.path .. ";packages/shared-state-bat_links_info/files/usr/bin/?;;" + require ("shared-state-publish_bat_links_info") + it('a simple test to get node info and assert requiered fields are present', function() - utils.log("presss") - - stub(utils, "unsafe_shell", function (cmd) - if cmd == "batctl nj" then - utils.log(" tubbbbbbbbbbbbbbbbbbbb") - else - utils.log("sssssss") - end - return '[{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1990,"neigh_address":"02:58:47:da:4e:aa"},{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1690,"neigh_address":"02:58:47:46:28:95"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":1510,"neigh_address":"02:ab:46:da:4e:aa"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":970,"neigh_address":"02:ab:46:46:28:95"}]' - end) - local links_info = {} - links_info = get_bat_links_info() - assert.are.equal('"02:58:47:da:4e:aa"', links_info[0].dst_mac) - utils.printJson(links_info) - + assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[1].src_mac) + assert.are.equal('02:58:47:da:4e:aa', links_info[1].dst_mac) + assert.are.equal(26, links_info[1].hard_ifindex) + assert.are.equal(1990, links_info[1].last_seen_msecs) + assert.are.equal("wlan0-mesh_250", links_info[1].iface) end) end) \ No newline at end of file From ad077f4d29ce745793a48b2838e906943e5e0b35 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 14:16:47 -0300 Subject: [PATCH 07/17] removed unused file --- .../files/usr/lib/lua/lime/iwinfo_lua.c | 932 ------------------ 1 file changed, 932 deletions(-) delete mode 100644 packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c diff --git a/packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c b/packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c deleted file mode 100644 index 1a9100123..000000000 --- a/packages/lime-system/files/usr/lib/lua/lime/iwinfo_lua.c +++ /dev/null @@ -1,932 +0,0 @@ -/* - * iwinfo - Wireless Information Library - Lua Bindings - * - * Copyright (C) 2009 Jo-Philipp Wich - * - * The iwinfo library is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License version 2 - * as published by the Free Software Foundation. - * - * The iwinfo library 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with the iwinfo library. If not, see http://www.gnu.org/licenses/. - */ - -#include "iwinfo/lua.h" - - -/* Determine type */ -static int iwinfo_L_type(lua_State *L) -{ - const char *ifname = luaL_checkstring(L, 1); - const char *type = iwinfo_type(ifname); - - if (type) - lua_pushstring(L, type); - else - lua_pushnil(L); - - return 1; -} - -/* Shutdown backends */ -static int iwinfo_L__gc(lua_State *L) -{ - iwinfo_finish(); - return 0; -} - -/* - * Build a short textual description of the crypto info - */ - -static char * iwinfo_crypto_print_ciphers(int ciphers) -{ - static char str[128] = { 0 }; - char *pos = str; - - if (ciphers & IWINFO_CIPHER_WEP40) - pos += sprintf(pos, "WEP-40, "); - - if (ciphers & IWINFO_CIPHER_WEP104) - pos += sprintf(pos, "WEP-104, "); - - if (ciphers & IWINFO_CIPHER_TKIP) - pos += sprintf(pos, "TKIP, "); - - if (ciphers & IWINFO_CIPHER_CCMP) - pos += sprintf(pos, "CCMP, "); - - if (ciphers & IWINFO_CIPHER_WRAP) - pos += sprintf(pos, "WRAP, "); - - if (ciphers & IWINFO_CIPHER_AESOCB) - pos += sprintf(pos, "AES-OCB, "); - - if (ciphers & IWINFO_CIPHER_CKIP) - pos += sprintf(pos, "CKIP, "); - - if (!ciphers || (ciphers & IWINFO_CIPHER_NONE)) - pos += sprintf(pos, "NONE, "); - - *(pos - 2) = 0; - - return str; -} - -static char * iwinfo_crypto_print_suites(int suites) -{ - static char str[64] = { 0 }; - char *pos = str; - - if (suites & IWINFO_KMGMT_PSK) - pos += sprintf(pos, "PSK/"); - - if (suites & IWINFO_KMGMT_8021x) - pos += sprintf(pos, "802.1X/"); - - if (!suites || (suites & IWINFO_KMGMT_NONE)) - pos += sprintf(pos, "NONE/"); - - *(pos - 1) = 0; - - return str; -} - -static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry *c) -{ - static char desc[512] = { 0 }; - - if (c) - { - if (c->enabled) - { - /* WEP */ - if (c->auth_algs && !c->wpa_version) - { - if ((c->auth_algs & IWINFO_AUTH_OPEN) && - (c->auth_algs & IWINFO_AUTH_SHARED)) - { - sprintf(desc, "WEP Open/Shared (%s)", - iwinfo_crypto_print_ciphers(c->pair_ciphers)); - } - else if (c->auth_algs & IWINFO_AUTH_OPEN) - { - sprintf(desc, "WEP Open System (%s)", - iwinfo_crypto_print_ciphers(c->pair_ciphers)); - } - else if (c->auth_algs & IWINFO_AUTH_SHARED) - { - sprintf(desc, "WEP Shared Auth (%s)", - iwinfo_crypto_print_ciphers(c->pair_ciphers)); - } - } - - /* WPA */ - else if (c->wpa_version) - { - switch (c->wpa_version) { - case 3: - sprintf(desc, "mixed WPA/WPA2 %s (%s)", - iwinfo_crypto_print_suites(c->auth_suites), - iwinfo_crypto_print_ciphers( - c->pair_ciphers & c->group_ciphers)); - break; - - case 2: - sprintf(desc, "WPA2 %s (%s)", - iwinfo_crypto_print_suites(c->auth_suites), - iwinfo_crypto_print_ciphers( - c->pair_ciphers & c->group_ciphers)); - break; - - case 1: - sprintf(desc, "WPA %s (%s)", - iwinfo_crypto_print_suites(c->auth_suites), - iwinfo_crypto_print_ciphers( - c->pair_ciphers & c->group_ciphers)); - break; - } - } - else - { - sprintf(desc, "None"); - } - } - else - { - sprintf(desc, "None"); - } - } - else - { - sprintf(desc, "Unknown"); - } - - return desc; -} - -/* Build Lua table from crypto data */ -static void iwinfo_L_cryptotable(lua_State *L, struct iwinfo_crypto_entry *c) -{ - int i, j; - - lua_newtable(L); - - lua_pushboolean(L, c->enabled); - lua_setfield(L, -2, "enabled"); - - lua_pushstring(L, iwinfo_crypto_desc(c)); - lua_setfield(L, -2, "description"); - - lua_pushboolean(L, (c->enabled && !c->wpa_version)); - lua_setfield(L, -2, "wep"); - - lua_pushinteger(L, c->wpa_version); - lua_setfield(L, -2, "wpa"); - - lua_newtable(L); - for (i = 0, j = 1; i < 8; i++) - { - if (c->pair_ciphers & (1 << i)) - { - lua_pushstring(L, IWINFO_CIPHER_NAMES[i]); - lua_rawseti(L, -2, j++); - } - } - lua_setfield(L, -2, "pair_ciphers"); - - lua_newtable(L); - for (i = 0, j = 1; i < 8; i++) - { - if (c->group_ciphers & (1 << i)) - { - lua_pushstring(L, IWINFO_CIPHER_NAMES[i]); - lua_rawseti(L, -2, j++); - } - } - lua_setfield(L, -2, "group_ciphers"); - - lua_newtable(L); - for (i = 0, j = 1; i < 8; i++) - { - if (c->auth_suites & (1 << i)) - { - lua_pushstring(L, IWINFO_KMGMT_NAMES[i]); - lua_rawseti(L, -2, j++); - } - } - lua_setfield(L, -2, "auth_suites"); - - lua_newtable(L); - for (i = 0, j = 1; i < 8; i++) - { - if (c->auth_algs & (1 << i)) - { - lua_pushstring(L, IWINFO_AUTH_NAMES[i]); - lua_rawseti(L, -2, j++); - } - } - lua_setfield(L, -2, "auth_algs"); -} - - -/* Wrapper for mode */ -static int iwinfo_L_mode(lua_State *L, int (*func)(const char *, int *)) -{ - int mode; - const char *ifname = luaL_checkstring(L, 1); - - if ((*func)(ifname, &mode)) - mode = IWINFO_OPMODE_UNKNOWN; - - lua_pushstring(L, IWINFO_OPMODE_NAMES[mode]); - return 1; -} - -/* Wrapper for assoclist */ -static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *)) -{ - int i, len; - char rv[IWINFO_BUFSIZE]; - char macstr[18]; - const char *ifname = luaL_checkstring(L, 1); - struct iwinfo_assoclist_entry *e; - - lua_newtable(L); - memset(rv, 0, sizeof(rv)); - - if (!(*func)(ifname, rv, &len)) - { - for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry)) - { - e = (struct iwinfo_assoclist_entry *) &rv[i]; - - sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", - e->mac[0], e->mac[1], e->mac[2], - e->mac[3], e->mac[4], e->mac[5]); - - lua_newtable(L); - - lua_pushnumber(L, e->signal); - lua_setfield(L, -2, "signal"); - - lua_pushnumber(L, e->noise); - lua_setfield(L, -2, "noise"); - - lua_pushnumber(L, e->inactive); - lua_setfield(L, -2, "inactive"); - - lua_pushnumber(L, e->rx_packets); - lua_setfield(L, -2, "rx_packets"); - - lua_pushnumber(L, e->tx_packets); - lua_setfield(L, -2, "tx_packets"); - - lua_pushnumber(L, e->rx_rate.rate); - lua_setfield(L, -2, "rx_rate"); - - lua_pushnumber(L, e->tx_rate.rate); - lua_setfield(L, -2, "tx_rate"); - - if (e->rx_rate.mcs >= 0) - { - lua_pushnumber(L, e->rx_rate.mcs); - lua_setfield(L, -2, "rx_mcs"); - - lua_pushboolean(L, e->rx_rate.is_40mhz); - lua_setfield(L, -2, "rx_40mhz"); - - lua_pushboolean(L, e->rx_rate.is_short_gi); - lua_setfield(L, -2, "rx_short_gi"); - } - - if (e->tx_rate.mcs >= 0) - { - lua_pushnumber(L, e->tx_rate.mcs); - lua_setfield(L, -2, "tx_mcs"); - - lua_pushboolean(L, e->tx_rate.is_40mhz); - lua_setfield(L, -2, "tx_40mhz"); - - lua_pushboolean(L, e->tx_rate.is_short_gi); - lua_setfield(L, -2, "tx_short_gi"); - } - - lua_setfield(L, -2, macstr); - } - } - - return 1; -} - -/* Wrapper for tx power list */ -static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *)) -{ - int i, x, len; - char rv[IWINFO_BUFSIZE]; - const char *ifname = luaL_checkstring(L, 1); - struct iwinfo_txpwrlist_entry *e; - - memset(rv, 0, sizeof(rv)); - - if (!(*func)(ifname, rv, &len)) - { - lua_newtable(L); - - for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++) - { - e = (struct iwinfo_txpwrlist_entry *) &rv[i]; - - lua_newtable(L); - - lua_pushnumber(L, e->mw); - lua_setfield(L, -2, "mw"); - - lua_pushnumber(L, e->dbm); - lua_setfield(L, -2, "dbm"); - - lua_rawseti(L, -2, x); - } - - return 1; - } - - return 0; -} - -/* Wrapper for scan list */ -static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *)) -{ - int i, x, len = 0; - char rv[IWINFO_BUFSIZE]; - char macstr[18]; - const char *ifname = luaL_checkstring(L, 1); - struct iwinfo_scanlist_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_scanlist_entry), x++) - { - e = (struct iwinfo_scanlist_entry *) &rv[i]; - - lua_newtable(L); - - /* BSSID */ - sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", - e->mac[0], e->mac[1], e->mac[2], - e->mac[3], e->mac[4], e->mac[5]); - - lua_pushstring(L, macstr); - lua_setfield(L, -2, "bssid"); - - /* ESSID */ - if (e->ssid[0]) - { - lua_pushstring(L, (char *) e->ssid); - lua_setfield(L, -2, "ssid"); - } - - /* Channel */ - lua_pushinteger(L, e->channel); - lua_setfield(L, -2, "channel"); - - /* Mode */ - lua_pushstring(L, IWINFO_OPMODE_NAMES[e->mode]); - lua_setfield(L, -2, "mode"); - - /* Quality, Signal */ - lua_pushinteger(L, e->quality); - lua_setfield(L, -2, "quality"); - - lua_pushinteger(L, e->quality_max); - lua_setfield(L, -2, "quality_max"); - - lua_pushnumber(L, (e->signal - 0x100)); - lua_setfield(L, -2, "signal"); - - /* Crypto */ - iwinfo_L_cryptotable(L, &e->crypto); - lua_setfield(L, -2, "encryption"); - - lua_rawseti(L, -2, x); - } - } - - return 1; -} - -/* Wrapper for frequency list */ -static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *)) -{ - int i, x, len; - char rv[IWINFO_BUFSIZE]; - const char *ifname = luaL_checkstring(L, 1); - struct iwinfo_freqlist_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_freqlist_entry), x++) - { - e = (struct iwinfo_freqlist_entry *) &rv[i]; - - lua_newtable(L); - - /* MHz */ - lua_pushinteger(L, e->mhz); - lua_setfield(L, -2, "mhz"); - - /* Channel */ - lua_pushinteger(L, e->channel); - lua_setfield(L, -2, "channel"); - - /* Restricted (DFS/TPC/Radar) */ - lua_pushboolean(L, e->restricted); - lua_setfield(L, -2, "restricted"); - - lua_rawseti(L, -2, x); - } - } - - return 1; -} - -/* Wrapper for crypto settings */ -static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *)) -{ - const char *ifname = luaL_checkstring(L, 1); - struct iwinfo_crypto_entry c = { 0 }; - - if (!(*func)(ifname, (char *)&c)) - { - iwinfo_L_cryptotable(L, &c); - return 1; - } - - lua_pushnil(L); - return 1; -} - -/* Wrapper for hwmode list */ -static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *)) -{ - const char *ifname = luaL_checkstring(L, 1); - int hwmodes = 0; - - if (!(*func)(ifname, &hwmodes)) - { - lua_newtable(L); - - lua_pushboolean(L, hwmodes & IWINFO_80211_A); - lua_setfield(L, -2, "a"); - - lua_pushboolean(L, hwmodes & IWINFO_80211_B); - lua_setfield(L, -2, "b"); - - lua_pushboolean(L, hwmodes & IWINFO_80211_G); - lua_setfield(L, -2, "g"); - - lua_pushboolean(L, hwmodes & IWINFO_80211_N); - lua_setfield(L, -2, "n"); - - lua_pushboolean(L, hwmodes & IWINFO_80211_AC); - lua_setfield(L, -2, "ac"); - - return 1; - } - - lua_pushnil(L); - return 1; -} - -/* Wrapper for htmode list */ -static int iwinfo_L_htmodelist(lua_State *L, int (*func)(const char *, int *)) -{ - const char *ifname = luaL_checkstring(L, 1); - int i, htmodes = 0; - - if (!(*func)(ifname, &htmodes)) - { - lua_newtable(L); - - for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++) - { - lua_pushboolean(L, htmodes & (1 << i)); - lua_setfield(L, -2, IWINFO_HTMODE_NAMES[i]); - } - - return 1; - } - - lua_pushnil(L); - return 1; -} - -/* Wrapper for mbssid_support */ -static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *)) -{ - const char *ifname = luaL_checkstring(L, 1); - int support = 0; - - if (!(*func)(ifname, &support)) - { - lua_pushboolean(L, support); - return 1; - } - - lua_pushnil(L); - return 1; -} - -/* Wrapper for hardware_id */ -static int iwinfo_L_hardware_id(lua_State *L, int (*func)(const char *, char *)) -{ - const char *ifname = luaL_checkstring(L, 1); - struct iwinfo_hardware_id ids; - - if (!(*func)(ifname, (char *)&ids)) - { - lua_newtable(L); - - lua_pushnumber(L, ids.vendor_id); - lua_setfield(L, -2, "vendor_id"); - - lua_pushnumber(L, ids.device_id); - lua_setfield(L, -2, "device_id"); - - lua_pushnumber(L, ids.subsystem_vendor_id); - lua_setfield(L, -2, "subsystem_vendor_id"); - - lua_pushnumber(L, ids.subsystem_device_id); - lua_setfield(L, -2, "subsystem_device_id"); - } - else - { - lua_pushnil(L); - } - - return 1; -} - -/* Wrapper for country list */ -static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166) -{ - int i; - struct iwinfo_country_entry *c; - - for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry)) - { - c = (struct iwinfo_country_entry *) &buf[i]; - - if (c->iso3166 == iso3166) - return c->ccode; - } - - return NULL; -} - -static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *)) -{ - int len, i; - char rv[IWINFO_BUFSIZE], alpha2[3]; - char *ccode; - const char *ifname = luaL_checkstring(L, 1); - const struct iwinfo_iso3166_label *l; - - lua_newtable(L); - memset(rv, 0, sizeof(rv)); - - if (!(*func)(ifname, rv, &len)) - { - for (l = IWINFO_ISO3166_NAMES, i = 1; l->iso3166; l++) - { - if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL) - { - sprintf(alpha2, "%c%c", - (l->iso3166 / 256), (l->iso3166 % 256)); - - lua_newtable(L); - - lua_pushstring(L, alpha2); - lua_setfield(L, -2, "alpha2"); - - lua_pushstring(L, ccode); - lua_setfield(L, -2, "ccode"); - - lua_pushstring(L, l->name); - lua_setfield(L, -2, "name"); - - lua_rawseti(L, -2, i++); - } - } - } - - return 1; -} - - -#ifdef USE_WL -/* Broadcom */ -LUA_WRAP_INT_OP(wl,channel) -LUA_WRAP_INT_OP(wl,frequency) -LUA_WRAP_INT_OP(wl,frequency_offset) -LUA_WRAP_INT_OP(wl,txpower) -LUA_WRAP_INT_OP(wl,txpower_offset) -LUA_WRAP_INT_OP(wl,bitrate) -LUA_WRAP_INT_OP(wl,signal) -LUA_WRAP_INT_OP(wl,noise) -LUA_WRAP_INT_OP(wl,quality) -LUA_WRAP_INT_OP(wl,quality_max) -LUA_WRAP_STRING_OP(wl,ssid) -LUA_WRAP_STRING_OP(wl,bssid) -LUA_WRAP_STRING_OP(wl,country) -LUA_WRAP_STRING_OP(wl,hardware_name) -LUA_WRAP_STRING_OP(wl,phyname) -LUA_WRAP_STRUCT_OP(wl,mode) -LUA_WRAP_STRUCT_OP(wl,assoclist) -LUA_WRAP_STRUCT_OP(wl,txpwrlist) -LUA_WRAP_STRUCT_OP(wl,scanlist) -LUA_WRAP_STRUCT_OP(wl,freqlist) -LUA_WRAP_STRUCT_OP(wl,countrylist) -LUA_WRAP_STRUCT_OP(wl,hwmodelist) -LUA_WRAP_STRUCT_OP(wl,htmodelist) -LUA_WRAP_STRUCT_OP(wl,encryption) -LUA_WRAP_STRUCT_OP(wl,mbssid_support) -LUA_WRAP_STRUCT_OP(wl,hardware_id) -#endif - -#ifdef USE_MADWIFI -/* Madwifi */ -LUA_WRAP_INT_OP(madwifi,channel) -LUA_WRAP_INT_OP(madwifi,frequency) -LUA_WRAP_INT_OP(madwifi,frequency_offset) -LUA_WRAP_INT_OP(madwifi,txpower) -LUA_WRAP_INT_OP(madwifi,txpower_offset) -LUA_WRAP_INT_OP(madwifi,bitrate) -LUA_WRAP_INT_OP(madwifi,signal) -LUA_WRAP_INT_OP(madwifi,noise) -LUA_WRAP_INT_OP(madwifi,quality) -LUA_WRAP_INT_OP(madwifi,quality_max) -LUA_WRAP_STRING_OP(madwifi,ssid) -LUA_WRAP_STRING_OP(madwifi,bssid) -LUA_WRAP_STRING_OP(madwifi,country) -LUA_WRAP_STRING_OP(madwifi,hardware_name) -LUA_WRAP_STRING_OP(madwifi,phyname) -LUA_WRAP_STRUCT_OP(madwifi,mode) -LUA_WRAP_STRUCT_OP(madwifi,assoclist) -LUA_WRAP_STRUCT_OP(madwifi,txpwrlist) -LUA_WRAP_STRUCT_OP(madwifi,scanlist) -LUA_WRAP_STRUCT_OP(madwifi,freqlist) -LUA_WRAP_STRUCT_OP(madwifi,countrylist) -LUA_WRAP_STRUCT_OP(madwifi,hwmodelist) -LUA_WRAP_STRUCT_OP(madwifi,htmodelist) -LUA_WRAP_STRUCT_OP(madwifi,encryption) -LUA_WRAP_STRUCT_OP(madwifi,mbssid_support) -LUA_WRAP_STRUCT_OP(madwifi,hardware_id) -#endif - -#ifdef USE_NL80211 -/* NL80211 */ -LUA_WRAP_INT_OP(nl80211,channel) -LUA_WRAP_INT_OP(nl80211,frequency) -LUA_WRAP_INT_OP(nl80211,frequency_offset) -LUA_WRAP_INT_OP(nl80211,txpower) -LUA_WRAP_INT_OP(nl80211,txpower_offset) -LUA_WRAP_INT_OP(nl80211,bitrate) -LUA_WRAP_INT_OP(nl80211,signal) -LUA_WRAP_INT_OP(nl80211,noise) -LUA_WRAP_INT_OP(nl80211,quality) -LUA_WRAP_INT_OP(nl80211,quality_max) -LUA_WRAP_STRING_OP(nl80211,ssid) -LUA_WRAP_STRING_OP(nl80211,bssid) -LUA_WRAP_STRING_OP(nl80211,country) -LUA_WRAP_STRING_OP(nl80211,hardware_name) -LUA_WRAP_STRING_OP(nl80211,phyname) -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,freqlist) -LUA_WRAP_STRUCT_OP(nl80211,countrylist) -LUA_WRAP_STRUCT_OP(nl80211,hwmodelist) -LUA_WRAP_STRUCT_OP(nl80211,htmodelist) -LUA_WRAP_STRUCT_OP(nl80211,encryption) -LUA_WRAP_STRUCT_OP(nl80211,mbssid_support) -LUA_WRAP_STRUCT_OP(nl80211,hardware_id) -#endif - -/* Wext */ -LUA_WRAP_INT_OP(wext,channel) -LUA_WRAP_INT_OP(wext,frequency) -LUA_WRAP_INT_OP(wext,frequency_offset) -LUA_WRAP_INT_OP(wext,txpower) -LUA_WRAP_INT_OP(wext,txpower_offset) -LUA_WRAP_INT_OP(wext,bitrate) -LUA_WRAP_INT_OP(wext,signal) -LUA_WRAP_INT_OP(wext,noise) -LUA_WRAP_INT_OP(wext,quality) -LUA_WRAP_INT_OP(wext,quality_max) -LUA_WRAP_STRING_OP(wext,ssid) -LUA_WRAP_STRING_OP(wext,bssid) -LUA_WRAP_STRING_OP(wext,country) -LUA_WRAP_STRING_OP(wext,hardware_name) -LUA_WRAP_STRING_OP(wext,phyname) -LUA_WRAP_STRUCT_OP(wext,mode) -LUA_WRAP_STRUCT_OP(wext,assoclist) -LUA_WRAP_STRUCT_OP(wext,txpwrlist) -LUA_WRAP_STRUCT_OP(wext,scanlist) -LUA_WRAP_STRUCT_OP(wext,freqlist) -LUA_WRAP_STRUCT_OP(wext,countrylist) -LUA_WRAP_STRUCT_OP(wext,hwmodelist) -LUA_WRAP_STRUCT_OP(wext,htmodelist) -LUA_WRAP_STRUCT_OP(wext,encryption) -LUA_WRAP_STRUCT_OP(wext,mbssid_support) -LUA_WRAP_STRUCT_OP(wext,hardware_id) - -#ifdef USE_WL -/* Broadcom table */ -static const luaL_reg R_wl[] = { - LUA_REG(wl,channel), - LUA_REG(wl,frequency), - LUA_REG(wl,frequency_offset), - LUA_REG(wl,txpower), - LUA_REG(wl,txpower_offset), - LUA_REG(wl,bitrate), - LUA_REG(wl,signal), - LUA_REG(wl,noise), - LUA_REG(wl,quality), - LUA_REG(wl,quality_max), - LUA_REG(wl,mode), - LUA_REG(wl,ssid), - LUA_REG(wl,bssid), - LUA_REG(wl,country), - LUA_REG(wl,assoclist), - LUA_REG(wl,txpwrlist), - LUA_REG(wl,scanlist), - LUA_REG(wl,freqlist), - LUA_REG(wl,countrylist), - LUA_REG(wl,hwmodelist), - LUA_REG(wl,htmodelist), - LUA_REG(wl,encryption), - LUA_REG(wl,mbssid_support), - LUA_REG(wl,hardware_id), - LUA_REG(wl,hardware_name), - LUA_REG(wl,phyname), - { NULL, NULL } -}; -#endif - -#ifdef USE_MADWIFI -/* Madwifi table */ -static const luaL_reg R_madwifi[] = { - LUA_REG(madwifi,channel), - LUA_REG(madwifi,frequency), - LUA_REG(madwifi,frequency_offset), - LUA_REG(madwifi,txpower), - LUA_REG(madwifi,txpower_offset), - LUA_REG(madwifi,bitrate), - LUA_REG(madwifi,signal), - LUA_REG(madwifi,noise), - LUA_REG(madwifi,quality), - LUA_REG(madwifi,quality_max), - LUA_REG(madwifi,mode), - LUA_REG(madwifi,ssid), - LUA_REG(madwifi,bssid), - LUA_REG(madwifi,country), - LUA_REG(madwifi,assoclist), - LUA_REG(madwifi,txpwrlist), - LUA_REG(madwifi,scanlist), - LUA_REG(madwifi,freqlist), - LUA_REG(madwifi,countrylist), - LUA_REG(madwifi,hwmodelist), - LUA_REG(madwifi,htmodelist), - LUA_REG(madwifi,encryption), - LUA_REG(madwifi,mbssid_support), - LUA_REG(madwifi,hardware_id), - LUA_REG(madwifi,hardware_name), - LUA_REG(madwifi,phyname), - { NULL, NULL } -}; -#endif - -#ifdef USE_NL80211 -/* NL80211 table */ -static const luaL_reg R_nl80211[] = { - LUA_REG(nl80211,channel), - LUA_REG(nl80211,frequency), - LUA_REG(nl80211,frequency_offset), - LUA_REG(nl80211,txpower), - LUA_REG(nl80211,txpower_offset), - LUA_REG(nl80211,bitrate), - LUA_REG(nl80211,signal), - LUA_REG(nl80211,noise), - LUA_REG(nl80211,quality), - LUA_REG(nl80211,quality_max), - LUA_REG(nl80211,mode), - LUA_REG(nl80211,ssid), - LUA_REG(nl80211,bssid), - LUA_REG(nl80211,country), - LUA_REG(nl80211,assoclist), - LUA_REG(nl80211,txpwrlist), - LUA_REG(nl80211,scanlist), - LUA_REG(nl80211,freqlist), - LUA_REG(nl80211,countrylist), - LUA_REG(nl80211,hwmodelist), - LUA_REG(nl80211,htmodelist), - LUA_REG(nl80211,encryption), - LUA_REG(nl80211,mbssid_support), - LUA_REG(nl80211,hardware_id), - LUA_REG(nl80211,hardware_name), - LUA_REG(nl80211,phyname), - { NULL, NULL } -}; -#endif - -/* Wext table */ -static const luaL_reg R_wext[] = { - LUA_REG(wext,channel), - LUA_REG(wext,frequency), - LUA_REG(wext,frequency_offset), - LUA_REG(wext,txpower), - LUA_REG(wext,txpower_offset), - LUA_REG(wext,bitrate), - LUA_REG(wext,signal), - LUA_REG(wext,noise), - LUA_REG(wext,quality), - LUA_REG(wext,quality_max), - LUA_REG(wext,mode), - LUA_REG(wext,ssid), - LUA_REG(wext,bssid), - LUA_REG(wext,country), - LUA_REG(wext,assoclist), - LUA_REG(wext,txpwrlist), - LUA_REG(wext,scanlist), - LUA_REG(wext,freqlist), - LUA_REG(wext,countrylist), - LUA_REG(wext,hwmodelist), - LUA_REG(wext,htmodelist), - LUA_REG(wext,encryption), - LUA_REG(wext,mbssid_support), - LUA_REG(wext,hardware_id), - LUA_REG(wext,hardware_name), - LUA_REG(wext,phyname), - { NULL, NULL } -}; - -/* Common */ -static const luaL_reg R_common[] = { - { "type", iwinfo_L_type }, - { "__gc", iwinfo_L__gc }, - { NULL, NULL } -}; - - -LUALIB_API int luaopen_iwinfo(lua_State *L) { - luaL_register(L, IWINFO_META, R_common); - -#ifdef USE_WL - luaL_newmetatable(L, IWINFO_WL_META); - luaL_register(L, NULL, R_common); - luaL_register(L, NULL, R_wl); - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - lua_setfield(L, -2, "wl"); -#endif - -#ifdef USE_MADWIFI - luaL_newmetatable(L, IWINFO_MADWIFI_META); - luaL_register(L, NULL, R_common); - luaL_register(L, NULL, R_madwifi); - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - lua_setfield(L, -2, "madwifi"); -#endif - -#ifdef USE_NL80211 - luaL_newmetatable(L, IWINFO_NL80211_META); - luaL_register(L, NULL, R_common); - luaL_register(L, NULL, R_nl80211); - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - lua_setfield(L, -2, "nl80211"); -#endif - - luaL_newmetatable(L, IWINFO_WEXT_META); - luaL_register(L, NULL, R_common); - luaL_register(L, NULL, R_wext); - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - lua_setfield(L, -2, "wext"); - - return 1; -} From 947e962822ca278fc4a6fe2e1fbe0b8246ddc01e Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 26 Sep 2023 14:45:59 -0300 Subject: [PATCH 08/17] Add batctl dependency --- packages/shared-state-bat_links_info/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/shared-state-bat_links_info/Makefile b/packages/shared-state-bat_links_info/Makefile index 03ce4a825..6cd7faf5d 100644 --- a/packages/shared-state-bat_links_info/Makefile +++ b/packages/shared-state-bat_links_info/Makefile @@ -1,5 +1,6 @@ # # Copyright (C) 2023 Javier Jorge +# Copyright (c) 2023 Instituto Nacional de Tecnología Industrial # Copyright (C) 2023 Asociación Civil Altermundi # This is free software, licensed under the GNU Affero General Public License v3. # @@ -12,7 +13,7 @@ define Package/$(PKG_NAME) TITLE:=Node information module for shared-state MAINTAINER:= Javier DEPENDS:=+lua +luci-lib-jsonc +ubus-lime-utils \ - +lime-system +ubus-lime-location shared-state + +lime-system +batctl-default shared-state PKGARCH:=all endef From f89fbd2145862d1bb070b79e2c7217d784f0ec28 Mon Sep 17 00:00:00 2001 From: Javier Jorge Date: Wed, 27 Sep 2023 14:43:29 -0300 Subject: [PATCH 09/17] Fixed makefile title --- packages/shared-state-bat_links_info/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared-state-bat_links_info/Makefile b/packages/shared-state-bat_links_info/Makefile index 6cd7faf5d..3d6aa03de 100644 --- a/packages/shared-state-bat_links_info/Makefile +++ b/packages/shared-state-bat_links_info/Makefile @@ -10,7 +10,7 @@ include ../../libremesh.mk define Package/$(PKG_NAME) SECTION:=lime CATEGORY:=LibreMesh - TITLE:=Node information module for shared-state + TITLE:=Batman protocol links information module for shared-state MAINTAINER:= Javier DEPENDS:=+lua +luci-lib-jsonc +ubus-lime-utils \ +lime-system +batctl-default shared-state From 3d862031aaf341d2b566e967eb4eaf8324df7909 Mon Sep 17 00:00:00 2001 From: javierbrk Date: Wed, 4 Oct 2023 09:13:47 -0300 Subject: [PATCH 10/17] removed hard_ifindex and added new line at the end of file --- .../files/usr/bin/shared-state-publish_bat_links_info | 2 ++ .../tests/test_shared-state-bat_links_info.lua | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index 83d36bd7b..7c4b3468d 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -31,8 +31,10 @@ function get_bat_links_info() for key,value in pairs (batneights_obj) do macparts = network.get_mac(value.iface) src_macaddr = table.concat(macparts,":") + value.hard_ifindex=nil value.src_mac=src_macaddr end + utils.printJson(batneights_obj) return batneights_obj end diff --git a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua index 94982926e..5a91ff026 100644 --- a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua +++ b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua @@ -20,8 +20,7 @@ describe('Tests bat_links_info #bat_links_info', function () links_info = get_bat_links_info() assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[1].src_mac) assert.are.equal('02:58:47:da:4e:aa', links_info[1].dst_mac) - assert.are.equal(26, links_info[1].hard_ifindex) assert.are.equal(1990, links_info[1].last_seen_msecs) assert.are.equal("wlan0-mesh_250", links_info[1].iface) end) -end) \ No newline at end of file +end) From 18d70c74abd4ff332150a200d2df26ef5fada18e Mon Sep 17 00:00:00 2001 From: javierbrk Date: Wed, 4 Oct 2023 09:14:58 -0300 Subject: [PATCH 11/17] remove debug print --- .../files/usr/bin/shared-state-publish_bat_links_info | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index 7c4b3468d..d8e286691 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -34,7 +34,6 @@ function get_bat_links_info() value.hard_ifindex=nil value.src_mac=src_macaddr end - utils.printJson(batneights_obj) return batneights_obj end From 6817c8ca1740d5f4fe242e8d5b2eab2e28a82b6b Mon Sep 17 00:00:00 2001 From: javierbrk Date: Wed, 4 Oct 2023 16:26:58 -0300 Subject: [PATCH 12/17] add link tq parameter and optional best indicator --- .../bin/shared-state-publish_bat_links_info | 6 +-- .../test_shared-state-bat_links_info.lua | 37 ++++++++++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index d8e286691..c037d6ebd 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -24,15 +24,13 @@ local network = require ("lime.network") local hostname = utils.hostname() function get_bat_links_info() local batneights_obj={} - local batneights = utils.unsafe_shell("batctl nj") + local batneights = utils.unsafe_shell("batctl oj") batneights = string.gsub(batneights,"neigh_address","dst_mac") + batneights = string.gsub(batneights,"orig_address","src_mac") batneights = string.gsub(batneights,"hard_ifname","iface") batneights_obj = JSON.parse(batneights) for key,value in pairs (batneights_obj) do - macparts = network.get_mac(value.iface) - src_macaddr = table.concat(macparts,":") value.hard_ifindex=nil - value.src_mac=src_macaddr end return batneights_obj end diff --git a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua index 5a91ff026..60137de50 100644 --- a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua +++ b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua @@ -5,8 +5,33 @@ local iwinfo = require "iwinfo" describe('Tests bat_links_info #bat_links_info', function () + + oj_output = [[ + [{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","orig_address": + "02:95:39:46:28:95","last_seen_msecs":20,"neigh_address": + "02:ab:46:da:4e:aa","tq":222},{"hard_ifindex":26,"hard_ifname": + "wlan0-mesh_250","orig_address":"02:95:39:46:28:95","last_seen_msecs": + 20,"neigh_address":"02:58:47:da:4e:aa","tq":0},{"hard_ifindex":28, + "hard_ifname":"wlan1-mesh_250","orig_address":"02:95:39:46:28:95", + "best":true,"last_seen_msecs":20,"neigh_address":"02:ab:46:46:28:95", + "tq":251},{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","orig_address" + :"02:95:39:46:28:95","last_seen_msecs":20,"neigh_address": + "02:58:47:46:28:95","tq":239},{"hard_ifindex":26,"hard_ifname": + "wlan0-mesh_250","orig_address":"02:58:47:da:4e:aa","last_seen_msecs": + 1260,"neigh_address":"02:58:47:46:28:95","tq":179},{"hard_ifindex":26, + "hard_ifname":"wlan0-mesh_250","orig_address":"02:58:47:da:4e:aa","best" + :true,"last_seen_msecs":1260,"neigh_address":"02:58:47:da:4e:aa","tq": + 255},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","orig_address": + "02:95:39:da:4e:aa","last_seen_msecs":1100,"neigh_address": + "02:ab:46:46:28:95","tq":214},{"hard_ifindex":26,"hard_ifname": + "wlan0-mesh_250","orig_address":"02:95:39:da:4e:aa","last_seen_msecs": + 1100,"neigh_address":"02:58:47:46:28:95","tq":0},{"hard_ifindex":28, + "hard_ifname":"wlan1-mesh_250","orig_address":"02:95:39:da:4e:aa","best": + true,"last_seen_msecs":1100,"neigh_address":"02:ab:46:da:4e:aa","tq":255}] + ]] + stub(utils, "unsafe_shell", function (cmd) - return '[{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1990,"neigh_address":"02:58:47:da:4e:aa"},{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","last_seen_msecs":1690,"neigh_address":"02:58:47:46:28:95"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":1510,"neigh_address":"02:ab:46:da:4e:aa"},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","last_seen_msecs":970,"neigh_address":"02:ab:46:46:28:95"}]' + return oj_output end) stub(network, "get_mac", function (iface) return iwinfo.mocks.wlan1_mesh_mac @@ -18,9 +43,11 @@ describe('Tests bat_links_info #bat_links_info', function () it('a simple test to get node info and assert requiered fields are present', function() local links_info = {} links_info = get_bat_links_info() - assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[1].src_mac) - assert.are.equal('02:58:47:da:4e:aa', links_info[1].dst_mac) - assert.are.equal(1990, links_info[1].last_seen_msecs) - assert.are.equal("wlan0-mesh_250", links_info[1].iface) + assert.are.equal('02:95:39:46:28:95', links_info[1].src_mac) + assert.are.equal('02:ab:46:da:4e:aa', links_info[1].dst_mac) + assert.are.equal(20, links_info[1].last_seen_msecs) + assert.are.equal("wlan1-mesh_250", links_info[1].iface) + assert.are.equal(222, links_info[1].tq) + end) end) From f6aa83f5569cf89f49000adc90f3dafe20fca78b Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 10 Oct 2023 16:17:00 -0300 Subject: [PATCH 13/17] package refactoring to include tq index --- .../bin/shared-state-publish_bat_links_info | 26 ++- .../test_shared-state-bat_links_info.lua | 209 +++++++++++++++--- 2 files changed, 201 insertions(+), 34 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index c037d6ebd..6fffcd1ec 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -22,16 +22,34 @@ local utils = require('lime.utils') local network = require ("lime.network") local hostname = utils.hostname() + function get_bat_links_info() local batneights_obj={} - local batneights = utils.unsafe_shell("batctl oj") + local batlinks_obj={} + local batlinks = utils.unsafe_shell("batctl oj") + batlinks_obj = JSON.parse(batlinks) + + + local batneights = utils.unsafe_shell("batctl nj") batneights = string.gsub(batneights,"neigh_address","dst_mac") - batneights = string.gsub(batneights,"orig_address","src_mac") + batlinks = string.gsub(batlinks,"orig_address","src_mac") batneights = string.gsub(batneights,"hard_ifname","iface") batneights_obj = JSON.parse(batneights) - for key,value in pairs (batneights_obj) do - value.hard_ifindex=nil + + for key,neight_value in pairs (batneights_obj) do + macparts = network.get_mac(neight_value.iface) + src_macaddr = table.concat(macparts,":") + neight_value.hard_ifindex=nil + neight_value.src_mac=src_macaddr + for key,link_value in pairs (batlinks_obj) do + if link_value.hard_ifname == neight_value.iface and + link_value.neigh_address== link_value.orig_address and + link_value.neigh_address== neight_value.dst_mac then + neight_value.tq = link_value.tq + end + end end + utils.printJson(batneights_obj) return batneights_obj end diff --git a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua index 60137de50..5b97e86e6 100644 --- a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua +++ b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua @@ -6,32 +6,179 @@ local iwinfo = require "iwinfo" describe('Tests bat_links_info #bat_links_info', function () - oj_output = [[ - [{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","orig_address": - "02:95:39:46:28:95","last_seen_msecs":20,"neigh_address": - "02:ab:46:da:4e:aa","tq":222},{"hard_ifindex":26,"hard_ifname": - "wlan0-mesh_250","orig_address":"02:95:39:46:28:95","last_seen_msecs": - 20,"neigh_address":"02:58:47:da:4e:aa","tq":0},{"hard_ifindex":28, - "hard_ifname":"wlan1-mesh_250","orig_address":"02:95:39:46:28:95", - "best":true,"last_seen_msecs":20,"neigh_address":"02:ab:46:46:28:95", - "tq":251},{"hard_ifindex":26,"hard_ifname":"wlan0-mesh_250","orig_address" - :"02:95:39:46:28:95","last_seen_msecs":20,"neigh_address": - "02:58:47:46:28:95","tq":239},{"hard_ifindex":26,"hard_ifname": - "wlan0-mesh_250","orig_address":"02:58:47:da:4e:aa","last_seen_msecs": - 1260,"neigh_address":"02:58:47:46:28:95","tq":179},{"hard_ifindex":26, - "hard_ifname":"wlan0-mesh_250","orig_address":"02:58:47:da:4e:aa","best" - :true,"last_seen_msecs":1260,"neigh_address":"02:58:47:da:4e:aa","tq": - 255},{"hard_ifindex":28,"hard_ifname":"wlan1-mesh_250","orig_address": - "02:95:39:da:4e:aa","last_seen_msecs":1100,"neigh_address": - "02:ab:46:46:28:95","tq":214},{"hard_ifindex":26,"hard_ifname": - "wlan0-mesh_250","orig_address":"02:95:39:da:4e:aa","last_seen_msecs": - 1100,"neigh_address":"02:58:47:46:28:95","tq":0},{"hard_ifindex":28, - "hard_ifname":"wlan1-mesh_250","orig_address":"02:95:39:da:4e:aa","best": - true,"last_seen_msecs":1100,"neigh_address":"02:ab:46:da:4e:aa","tq":255}] + local oj_output = [[ + [ + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:95:39:46:28:95", + "last_seen_msecs": 1820, + "neigh_address": "02:ab:46:da:4e:aa", + "tq": 225 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:95:39:46:28:95", + "last_seen_msecs": 1820, + "neigh_address": "02:58:47:da:4e:aa", + "tq": 0 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:95:39:46:28:95", + "best": true, + "last_seen_msecs": 1820, + "neigh_address": "02:ab:46:46:28:95", + "tq": 255 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:95:39:46:28:95", + "last_seen_msecs": 1820, + "neigh_address": "02:58:47:46:28:95", + "tq": 253 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:58:47:da:4e:aa", + "last_seen_msecs": 0, + "neigh_address": "02:58:47:46:28:95", + "tq": 193 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:58:47:da:4e:aa", + "best": true, + "last_seen_msecs": 0, + "neigh_address": "02:58:47:da:4e:aa", + "tq": 111 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:95:39:da:4e:aa", + "last_seen_msecs": 10, + "neigh_address": "02:ab:46:46:28:95", + "tq": 221 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:95:39:da:4e:aa", + "last_seen_msecs": 10, + "neigh_address": "02:58:47:46:28:95", + "tq": 0 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:95:39:da:4e:aa", + "best": true, + "last_seen_msecs": 10, + "neigh_address": "02:ab:46:da:4e:aa", + "tq": 255 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:95:39:da:4e:aa", + "last_seen_msecs": 10, + "neigh_address": "02:58:47:da:4e:aa", + "tq": 254 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:58:47:46:28:95", + "last_seen_msecs": 140, + "neigh_address": "02:58:47:da:4e:aa", + "tq": 198 + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "orig_address": "02:58:47:46:28:95", + "best": true, + "last_seen_msecs": 140, + "neigh_address": "02:58:47:46:28:95", + "tq": 222 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:ab:46:46:28:95", + "last_seen_msecs": 1420, + "neigh_address": "02:ab:46:da:4e:aa", + "tq": 198 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:ab:46:46:28:95", + "best": true, + "last_seen_msecs": 1420, + "neigh_address": "02:ab:46:46:28:95", + "tq": 444 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:ab:46:da:4e:aa", + "last_seen_msecs": 1680, + "neigh_address": "02:ab:46:46:28:95", + "tq": 195 + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "orig_address": "02:ab:46:da:4e:aa", + "best": true, + "last_seen_msecs": 1680, + "neigh_address": "02:ab:46:da:4e:aa", + "tq": 333 + } + ] ]] - + local nj_output = [[ + [ + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "last_seen_msecs": 1040, + "neigh_address": "02:58:47:da:4e:aa" + }, + { + "hard_ifindex": 26, + "hard_ifname": "wlan0-mesh_250", + "last_seen_msecs": 1250, + "neigh_address": "02:58:47:46:28:95" + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "last_seen_msecs": 640, + "neigh_address": "02:ab:46:da:4e:aa" + }, + { + "hard_ifindex": 28, + "hard_ifname": "wlan1-mesh_250", + "last_seen_msecs": 450, + "neigh_address": "02:ab:46:46:28:95" + } + ] + ]] stub(utils, "unsafe_shell", function (cmd) - return oj_output + if cmd == "batctl nj" then + return nj_output + elseif cmd == "batctl oj" then + return oj_output + end + return "" end) stub(network, "get_mac", function (iface) return iwinfo.mocks.wlan1_mesh_mac @@ -43,11 +190,13 @@ describe('Tests bat_links_info #bat_links_info', function () it('a simple test to get node info and assert requiered fields are present', function() local links_info = {} links_info = get_bat_links_info() - assert.are.equal('02:95:39:46:28:95', links_info[1].src_mac) - assert.are.equal('02:ab:46:da:4e:aa', links_info[1].dst_mac) - assert.are.equal(20, links_info[1].last_seen_msecs) - assert.are.equal("wlan1-mesh_250", links_info[1].iface) - assert.are.equal(222, links_info[1].tq) - + assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[1].src_mac) + assert.are.equal('02:58:47:da:4e:aa', links_info[1].dst_mac) + assert.are.equal(1040, links_info[1].last_seen_msecs) + assert.are.equal("wlan0-mesh_250", links_info[1].iface) + assert.are.equal(111, links_info[1].tq) + assert.are.equal(222, links_info[2].tq) + assert.are.equal(333, links_info[3].tq) + assert.are.equal(444, links_info[4].tq) end) end) From 6d4c6091bf347b5e576ad2c028d889458f4c3b05 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 10 Oct 2023 16:27:50 -0300 Subject: [PATCH 14/17] include mac verification in tests --- .../files/usr/bin/shared-state-publish_bat_links_info | 5 ++--- .../tests/test_shared-state-bat_links_info.lua | 10 ++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index 6fffcd1ec..1ff1b03bf 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -37,8 +37,8 @@ function get_bat_links_info() batneights_obj = JSON.parse(batneights) for key,neight_value in pairs (batneights_obj) do - macparts = network.get_mac(neight_value.iface) - src_macaddr = table.concat(macparts,":") + local macparts = network.get_mac(neight_value.iface) + local src_macaddr = table.concat(macparts,":") neight_value.hard_ifindex=nil neight_value.src_mac=src_macaddr for key,link_value in pairs (batlinks_obj) do @@ -49,7 +49,6 @@ function get_bat_links_info() end end end - utils.printJson(batneights_obj) return batneights_obj end diff --git a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua index 5b97e86e6..2dc3c7e42 100644 --- a/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua +++ b/packages/shared-state-bat_links_info/tests/test_shared-state-bat_links_info.lua @@ -172,6 +172,7 @@ describe('Tests bat_links_info #bat_links_info', function () } ] ]] + stub(utils, "unsafe_shell", function (cmd) if cmd == "batctl nj" then return nj_output @@ -180,17 +181,22 @@ describe('Tests bat_links_info #bat_links_info', function () end return "" 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) - + package.path = package.path .. ";packages/shared-state-bat_links_info/files/usr/bin/?;;" require ("shared-state-publish_bat_links_info") it('a simple test to get node info and assert requiered fields are present', function() local links_info = {} links_info = get_bat_links_info() - assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[1].src_mac) + assert.are.equal(table.concat(iwinfo.mocks.wlan0_mesh_mac,":"), links_info[1].src_mac) + assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[4].src_mac) assert.are.equal('02:58:47:da:4e:aa', links_info[1].dst_mac) assert.are.equal(1040, links_info[1].last_seen_msecs) assert.are.equal("wlan0-mesh_250", links_info[1].iface) From 3d63187e92d76265278592db2c2a1be193c89031 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Thu, 12 Oct 2023 09:01:36 -0300 Subject: [PATCH 15/17] name refactoring and comments --- .../bin/shared-state-publish_bat_links_info | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index 1ff1b03bf..ac6566491 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -24,32 +24,34 @@ local network = require ("lime.network") local hostname = utils.hostname() function get_bat_links_info() - local batneights_obj={} - local batlinks_obj={} - local batlinks = utils.unsafe_shell("batctl oj") - batlinks_obj = JSON.parse(batlinks) + local bat_neighbors_obj={} + local bat_originators_obj={} + local bat_originators = utils.unsafe_shell("batctl oj") + bat_originators_obj = JSON.parse(bat_originators) - local batneights = utils.unsafe_shell("batctl nj") - batneights = string.gsub(batneights,"neigh_address","dst_mac") - batlinks = string.gsub(batlinks,"orig_address","src_mac") - batneights = string.gsub(batneights,"hard_ifname","iface") - batneights_obj = JSON.parse(batneights) + local bat_neighbors = utils.unsafe_shell("batctl nj") + bat_neighbors = string.gsub(bat_neighbors,"neigh_address","dst_mac") + bat_neighbors = string.gsub(bat_neighbors,"hard_ifname","iface") + bat_neighbors_obj = JSON.parse(bat_neighbors) - for key,neight_value in pairs (batneights_obj) do + for key,neight_value in pairs (bat_neighbors_obj) do local macparts = network.get_mac(neight_value.iface) local src_macaddr = table.concat(macparts,":") neight_value.hard_ifindex=nil neight_value.src_mac=src_macaddr - for key,link_value in pairs (batlinks_obj) do - if link_value.hard_ifname == neight_value.iface and - link_value.neigh_address== link_value.orig_address and - link_value.neigh_address== neight_value.dst_mac then - neight_value.tq = link_value.tq + for key,originator_value in pairs (bat_originators_obj) do + if originator_value.hard_ifname == neight_value.iface and + originator_value.neigh_address== originator_value.orig_address and + originator_value.neigh_address== neight_value.dst_mac then + -- Batman "transmit link quality" (tq) is a byte that describes + -- the probability of a successful transmission towards a + -- neighbor node + neight_value.tq = originator_value.tq end end end - return batneights_obj + return bat_neighbors_obj end local result = { [hostname] = get_bat_links_info() } From 1e1f3793a60451bb6d67e93a4e7893fc8daf05e7 Mon Sep 17 00:00:00 2001 From: "javier.jorge" Date: Tue, 27 Feb 2024 13:32:31 -0300 Subject: [PATCH 16/17] shared-state-async: ads bat links info --- .../etc/uci-defaults/shared-state_bat_links_info_cron | 10 +++++++++- .../files/usr/bin/shared-state-publish_bat_links_info | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/shared-state-bat_links_info/files/etc/uci-defaults/shared-state_bat_links_info_cron b/packages/shared-state-bat_links_info/files/etc/uci-defaults/shared-state_bat_links_info_cron index f540eb89a..c725f7168 100755 --- a/packages/shared-state-bat_links_info/files/etc/uci-defaults/shared-state_bat_links_info_cron +++ b/packages/shared-state-bat_links_info/files/etc/uci-defaults/shared-state_bat_links_info_cron @@ -1,9 +1,17 @@ #!/bin/sh + unique_append() { grep -qF "$1" "$2" || echo "$1" >> "$2" } +uci set shared-state.bat_links_info=dataType +uci set shared-state.bat_links_info.name='bat_links_info' +uci set shared-state.bat_links_info.scope='community' +uci set shared-state.bat_links_info.ttl='2400' +uci set shared-state.bat_links_info.update_interval='30' +uci commit shared-state + unique_append \ - '*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync bat_links_info &> /dev/null)&)'\ + '*/30 * * * * ((sleep $(($RANDOM % 120)); shared-state-publish_bat_links_info &> /dev/null)&)' \ /etc/crontabs/root diff --git a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info index ac6566491..3d7837353 100755 --- a/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info +++ b/packages/shared-state-bat_links_info/files/usr/bin/shared-state-publish_bat_links_info @@ -55,4 +55,4 @@ function get_bat_links_info() end local result = { [hostname] = get_bat_links_info() } -io.popen("shared-state insert bat_links_info", "w"):write(JSON.stringify(result)) +io.popen("shared-state-async insert bat_links_info", "w"):write(JSON.stringify(result)) From 30298c78974001cde006288949c3ff5bc0f15462 Mon Sep 17 00:00:00 2001 From: javierbrk Date: Fri, 1 Mar 2024 10:37:42 -0300 Subject: [PATCH 17/17] adds shared state async as aa dependency --- packages/shared-state-bat_links_info/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared-state-bat_links_info/Makefile b/packages/shared-state-bat_links_info/Makefile index 3d6aa03de..b1615b6f5 100644 --- a/packages/shared-state-bat_links_info/Makefile +++ b/packages/shared-state-bat_links_info/Makefile @@ -13,7 +13,7 @@ define Package/$(PKG_NAME) TITLE:=Batman protocol links information module for shared-state MAINTAINER:= Javier DEPENDS:=+lua +luci-lib-jsonc +ubus-lime-utils \ - +lime-system +batctl-default shared-state + +lime-system +batctl-default shared-state-async PKGARCH:=all endef