From 04f90247b14fdc8f40653cc0c0f45c861a6f4d6c Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 1 Jul 2018 16:49:55 +0200 Subject: [PATCH] add respondd-module-wifisettings --- net/respondd-module-wifisettings/Makefile | 25 ++++ net/respondd-module-wifisettings/README.md | 13 +++ net/respondd-module-wifisettings/src/Makefile | 9 ++ .../src/respondd.c | 109 ++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 net/respondd-module-wifisettings/Makefile create mode 100644 net/respondd-module-wifisettings/README.md create mode 100644 net/respondd-module-wifisettings/src/Makefile create mode 100644 net/respondd-module-wifisettings/src/respondd.c diff --git a/net/respondd-module-wifisettings/Makefile b/net/respondd-module-wifisettings/Makefile new file mode 100644 index 0000000..54739aa --- /dev/null +++ b/net/respondd-module-wifisettings/Makefile @@ -0,0 +1,25 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=respondd-module-wifisettings +PKG_VERSION:=1 +PKG_RELEASE:=1 + +PKG_LICENSE:=BSD-2-Clause + +PKG_BUILD_DEPENDS := respondd + +include $(INCLUDE_DIR)/package.mk + +define Package/respondd-module-wifisettings + SECTION:=net + CATEGORY:=Network + TITLE:=Add wifisettings to respondd + DEPENDS:=+respondd +libuci +endef + +define Package/respondd-module-wifisettings/install + $(INSTALL_DIR) $(1)/usr/lib/respondd + $(CP) $(PKG_BUILD_DIR)/respondd.so $(1)/usr/lib/respondd/wifi-settings.so +endef + +$(eval $(call BuildPackage,respondd-module-wifisettings)) diff --git a/net/respondd-module-wifisettings/README.md b/net/respondd-module-wifisettings/README.md new file mode 100644 index 0000000..3ed26ac --- /dev/null +++ b/net/respondd-module-wifisettings/README.md @@ -0,0 +1,13 @@ +This module adds a respondd wifisettings usage nodeinfo provider. +The format is the following: + +```json +{ + "nodeinfo": { + "channel24": 11, + "txpower24": 22, + "channel5": 44, + "txpower5": 18, + } +} +``` diff --git a/net/respondd-module-wifisettings/src/Makefile b/net/respondd-module-wifisettings/src/Makefile new file mode 100644 index 0000000..e7b64b1 --- /dev/null +++ b/net/respondd-module-wifisettings/src/Makefile @@ -0,0 +1,9 @@ +all: respondd.so + +CFLAGS += -Wall + +respondd.so: respondd.c + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -luci + +clean: + rm -rf *.so diff --git a/net/respondd-module-wifisettings/src/respondd.c b/net/respondd-module-wifisettings/src/respondd.c new file mode 100644 index 0000000..3a44f45 --- /dev/null +++ b/net/respondd-module-wifisettings/src/respondd.c @@ -0,0 +1,109 @@ +#include +#include +#include + +#include + +#include +#include + +#include + +const unsigned int INVALID_CHANNEL = 0; +const unsigned int INVALID_TXPOWER = 0; + +static inline unsigned char parse_option(const char *s, unsigned char invalid) { + char *endptr = NULL; + long int result; + + if (!s) + return invalid; + + result = strtol(s, &endptr, 10); + + if (!endptr) + return invalid; + if ('\0' != *endptr) + return invalid; + if (result > UCHAR_MAX) + return invalid; + if (result < 0) + return invalid; + + return (unsigned char)(result % UCHAR_MAX); +} + +static struct json_object *respondd_provider_nodeinfo(void) { + struct uci_context *ctx = NULL; + struct uci_package *p = NULL; + struct uci_section *s; + struct uci_element *e; + struct json_object *ret = NULL, *wireless = NULL, *v; + unsigned char tmp; + + ctx = uci_alloc_context(); + if (!ctx) + goto end; + ctx->flags &= ~UCI_FLAG_STRICT; + + wireless = json_object_new_object(); + if (!wireless) + goto end; + + ret = json_object_new_object(); + if (!ret) + goto end; + + if (uci_load(ctx, "wireless", &p)) + goto end; + + uci_foreach_element(&p->sections, e) { + s = uci_to_section(e); + + if(!strncmp(s->type,"wifi-device",11)){ + tmp = parse_option(uci_lookup_option_string(ctx, s, "channel"), INVALID_CHANNEL); + if (tmp != INVALID_CHANNEL) { + v = json_object_new_int64(tmp); + if (!v) + goto end; + if (tmp >= 1 && tmp <= 14){ + json_object_object_add(wireless, "channel24", v); + tmp = parse_option(uci_lookup_option_string(ctx, s, "txpower"), INVALID_TXPOWER); + if (tmp != INVALID_TXPOWER) { + v = json_object_new_int64(tmp); + if (!v) + goto end; + json_object_object_add(wireless, "txpower24", v); + } + // FIXME lowes is 7, but i was able to differ between 2.4 Ghz and 5 Ghz by iwinfo_ops->frequency + // In EU and US it is 36, so it would be okay for the moment (https://en.wikipedia.org/wiki/List_of_WLAN_channels) + } else if (tmp >= 36 && tmp < 196){ + json_object_object_add(wireless, "channel5", v); + tmp = parse_option(uci_lookup_option_string(ctx, s, "txpower"), INVALID_TXPOWER); + if (tmp != INVALID_TXPOWER) { + v = json_object_new_int64(tmp); + if (!v) + goto end; + json_object_object_add(wireless, "txpower5", v); + } + } else + json_object_object_add(wireless, "ErrorChannel", v); + } + } + } + + json_object_object_add(ret, "wireless", wireless); +end: + if (ctx) { + if (p) + uci_unload(ctx, p); + uci_free_context(ctx); + } + return ret; + +} + +const struct respondd_provider_info respondd_providers[] = { + {"nodeinfo", respondd_provider_nodeinfo}, + {0,0}, +};