merge wifi respondd modules to one wifi statistics modul

This commit is contained in:
Martin/Geno 2018-08-08 06:17:23 +02:00
parent 04f90247b1
commit ca1448f6a0
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
17 changed files with 173 additions and 226 deletions

View File

@ -1,39 +0,0 @@
#include <string.h>
#include <stdio.h>
#include <json-c/json.h>
#include <respondd.h>
#include "airtime.h"
#include "ifaces.h"
static struct json_object *respondd_provider_statistics(void) {
struct json_object *result, *wireless;
struct iface_list *ifaces;
result = json_object_new_object();
if (!result)
return NULL;
wireless = json_object_new_array();
if (!wireless) {
json_object_put(result);
return NULL;
}
ifaces = get_ifaces();
while (ifaces != NULL) {
get_airtime(wireless, ifaces->ifx);
void *freeptr = ifaces;
ifaces = ifaces->next;
free(freeptr);
}
json_object_object_add(result, "wireless", wireless);
return result;
}
const struct respondd_provider_info respondd_providers[] = {
{"statistics", respondd_provider_statistics},
{0, 0},
};

View File

@ -0,0 +1,39 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=respondd-module-wifi
PKG_VERSION:=1
PKG_RELEASE:=1
PKG_LICENSE:=BSD-2-Clause
PKG_BUILD_DEPENDS := respondd
include $(INCLUDE_DIR)/package.mk
define Package/respondd-module-wifi/Default
SECTION:=net
CATEGORY:=Network
TITLE:=Add wifi statistics to respondd
DEPENDS:=+respondd +libnl-tiny
endef
TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include/libnl-tiny
define Package/respondd-module-wifi
$(call Package/respondd-module-wifi/Default)
endef
define Package/respondd-module-gluonwifi
$(call Package/respondd-module-wifi/Default)
VARIANT:=gluon
TARGET_CFLAGS += -DGLUON
endef
define Package/respondd-module-wifi/install
$(INSTALL_DIR) $(1)/usr/lib/respondd
$(CP) $(PKG_BUILD_DIR)/respondd.so $(1)/usr/lib/respondd/wifi.so
endef
Package/respondd-module-gluonwifi/install = $(Package/respondd-module-wifi/install)
$(eval $(call BuildPackage,respondd-module-wifi))

View File

@ -1,25 +1,43 @@
This module adds a respondd airtime usage statistics provider.
This module adds a respondd wifi usage statistics provider.
The format is the following:
```json
{
"statistics": {
"clients":{
"wifi24": 3,
"wifi5": 7
},
"wireless": [
{
"frequency": 5220,
"txpower": 1700,
"active": 366561161,
"busy": 46496566,
"rx": 808415,
"tx": 41711344,
"noise": 162
"noise": 162,
"clients": 5
},
{
"frequency": 5220,
"txpower": 1700,
"active": 366561161,
"busy": 46496566,
"rx": 808415,
"tx": 41711344,
"noise": 162,
"clients": 2
},
{
"frequency": 2437,
"txpower": 2000,
"active": 366649704,
"busy": 205221222,
"rx": 108121446,
"tx": 85453679,
"noise": 161
"noise": 161,
"clients": 3
}
]
}

View File

@ -9,7 +9,7 @@ all: respondd.so
%.c: %.h
respondd.so: netlink.c airtime.c ifaces.c respondd.c
respondd.so: netlink.c clients.c airtime.c ifaces.c respondd.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -lnl-tiny -o $@ $^ $(LDLIBS)
clean:

View File

@ -57,8 +57,7 @@
* @__NL80211_SURVEY_INFO_AFTER_LAST: internal use
*/
static const char const* msg_names[NL80211_SURVEY_INFO_MAX + 1] = {
[NL80211_SURVEY_INFO_FREQUENCY] = "frequency",
static const char * msg_names[NL80211_SURVEY_INFO_MAX + 1] = {
[NL80211_SURVEY_INFO_CHANNEL_TIME] = "active",
[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY] = "busy",
[NL80211_SURVEY_INFO_CHANNEL_TIME_RX] = "rx",
@ -67,19 +66,13 @@ static const char const* msg_names[NL80211_SURVEY_INFO_MAX + 1] = {
};
static int survey_airtime_handler(struct nl_msg *msg, void *arg) {
struct json_object *parent_json = (struct json_object *) arg;
struct json_object *json = (struct json_object *) arg;
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *survey_info = nla_find(genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NL80211_ATTR_SURVEY_INFO);
if (!survey_info) {
fprintf(stderr, "respondd-module-airtime: survey data missing in netlink message\n");
goto abort;
}
struct json_object *freq_json = json_object_new_object();
if (!freq_json) {
fprintf(stderr, "respondd-module-airtime: failed allocating JSON object\n");
fputs("respondd-module-wifi: survey data missing in netlink message\n", stderr);
goto abort;
}
@ -99,7 +92,6 @@ static int survey_airtime_handler(struct nl_msg *msg, void *arg) {
switch (type) {
// these are the required fields
case NL80211_SURVEY_INFO_IN_USE:
case NL80211_SURVEY_INFO_FREQUENCY:
case NL80211_SURVEY_INFO_CHANNEL_TIME:
req_fields++;
}
@ -122,18 +114,13 @@ static int survey_airtime_handler(struct nl_msg *msg, void *arg) {
data_json = json_object_new_int(nla_get_u8(nla));
break;
default:
fprintf(stderr, "respondd-module-airtime: Unexpected NL attribute length: %d\n", nla_len(nla));
fprintf(stderr, "respondd-module-wifi: Unexpected NL attribute length: %d\n", nla_len(nla));
}
if (data_json)
json_object_object_add(freq_json, msg_names[type], data_json);
json_object_object_add(json, msg_names[type], data_json);
}
if (req_fields == 3)
json_object_array_add(parent_json, freq_json);
else
json_object_put(freq_json);
abort:
return NL_SKIP;
}

View File

@ -0,0 +1,16 @@
#include <linux/nl80211.h>
#include <netlink/genl/genl.h>
#include "netlink.h"
#include "clients.h"
static int station_client_handler(struct nl_msg *msg, void *arg) {
int *count = (int *) arg;
(*count)++;
return NL_SKIP;
}
bool get_client_counts(int *count, int ifx) {
return nl_send_dump(station_client_handler, count, NL80211_CMD_GET_STATION, ifx);
}

View File

@ -0,0 +1,6 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
__attribute__((visibility("hidden"))) bool get_client_counts(int *count, int ifx);

View File

@ -7,7 +7,6 @@
static int iface_dump_handler(struct nl_msg *msg, void *arg) {
struct nlattr *tb[NL80211_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
int wiphy;
struct iface_list **last_next;
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
@ -15,17 +14,22 @@ static int iface_dump_handler(struct nl_msg *msg, void *arg) {
if (!tb[NL80211_ATTR_WIPHY] || !tb[NL80211_ATTR_IFINDEX])
goto skip;
wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
for (last_next = arg; *last_next != NULL; last_next = &(*last_next)->next) {
if ((*last_next)->wiphy == wiphy)
goto skip;
}
#ifdef GLUON
if(nla_strcmp(tb[NL80211_ATTR_IFNAME], "client") == -1)
goto skip;
#endif
// TODO fix add to head list - instatt find last item
for (last_next = arg; *last_next != NULL; last_next = &(*last_next)->next) {}
*last_next = malloc(sizeof(**last_next));
if (!*last_next)
goto skip;
(*last_next)->next = NULL;
(*last_next)->wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
(*last_next)->ifx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
(*last_next)->wiphy = wiphy;
(*last_next)->frequency = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
(*last_next)->txpower = nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]);
skip:
return NL_SKIP;

View File

@ -1,8 +1,10 @@
#pragma once
struct iface_list {
int ifx;
int wiphy;
int ifx;
int frequency;
int txpower;
struct iface_list *next;
};

View File

@ -6,6 +6,7 @@
#include "netlink.h"
bool nl_send_dump(nl_recvmsg_msg_cb_t cb, void *cb_arg, int cmd, uint32_t cmd_arg) {
bool ok = false;
int ret;
@ -14,7 +15,7 @@ bool nl_send_dump(nl_recvmsg_msg_cb_t cb, void *cb_arg, int cmd, uint32_t cmd_ar
struct nl_msg *msg = NULL;
#define ERR(...) { fprintf(stderr, "respondd-module-airtime: " __VA_ARGS__); goto out; }
#define ERR(...) { fprintf(stderr, "respondd-module-wifi: " __VA_ARGS__); goto out; }
sk = nl_socket_alloc();
if (!sk)

View File

@ -0,0 +1,69 @@
#include <string.h>
#include <stdio.h>
#include <json-c/json.h>
#include <respondd.h>
#include "airtime.h"
#include "clients.h"
#include "ifaces.h"
static struct json_object *respondd_provider_statistics(void) {
struct json_object *result, *wireless, *clients, *interface;
struct iface_list *ifaces;
int wifi24 = 0, wifi5 = 0, clients_count;
result = json_object_new_object();
if (!result)
return NULL;
wireless = json_object_new_array();
if (!wireless) {
//TODO why needed? : json_object_put(result);
return NULL;
}
clients = json_object_new_object();
if (!clients) {
return NULL;
}
ifaces = get_ifaces();
while (ifaces != NULL) {
clients_count = 0;
get_client_counts(&clients_count, ifaces->ifx);
if (ifaces->frequency < 5000)
wifi24 += clients_count;
if (ifaces->frequency > 5000)
wifi5 += clients_count;
//TODO wiphy only one radio added? (no necessary on gluon - only one ap-ssid at radio)
interface = json_object_new_object();
if (!interface) {
continue;
}
json_object_object_add(interface, "frequency", json_object_new_int(ifaces->frequency));
json_object_object_add(interface, "txpower", json_object_new_int(ifaces->txpower));
get_airtime(interface, ifaces->ifx);
//TODO remove at merge radios (one wiphy radio)
json_object_object_add(interface, "clients", json_object_new_int(clients_count));
json_object_array_add(wireless, interface);
void *freeptr = ifaces;
ifaces = ifaces->next;
free(freeptr);
}
//TODO maybe skip: if (wifi24 > 0 || wifi5 > 0) {
json_object_object_add(clients, "wifi24", json_object_new_int(wifi24));
json_object_object_add(clients, "wifi5", json_object_new_int(wifi5));
json_object_object_add(result, "clients", clients);
//}
json_object_object_add(result, "wireless", wireless);
return result;
}
const struct respondd_provider_info respondd_providers[] = {
{"statistics", respondd_provider_statistics},
{0, 0},
};

View File

@ -1,25 +0,0 @@
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))

View File

@ -1,13 +0,0 @@
This module adds a respondd wifisettings usage nodeinfo provider.
The format is the following:
```json
{
"nodeinfo": {
"channel24": 11,
"txpower24": 22,
"channel5": 44,
"txpower5": 18,
}
}
```

View File

@ -1,9 +0,0 @@
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

View File

@ -1,109 +0,0 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <json-c/json.h>
#include <uci.h>
#include <respondd.h>
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},
};