gluon-status-page-api: rewrite neighbours-batadv in C

This commit is contained in:
Nils Schneider 2015-09-01 22:49:26 +02:00
parent 431ac81407
commit 4b8f6c9835
4 changed files with 77 additions and 39 deletions

View File

@ -12,19 +12,17 @@ define Package/gluon-status-page-api
SECTION:=gluon
CATEGORY:=Gluon
TITLE:=API for gluon-status-page
DEPENDS:=+gluon-core +uhttpd +gluon-neighbour-info +gluon-announced +libiwinfo-lua +luci-lib-jsonc
DEPENDS:=+gluon-core +uhttpd +gluon-neighbour-info +gluon-announced +libiwinfo +libjson-c
endef
define Build/Prepare
endef
define Build/Configure
endef
define Build/Compile
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/gluon-status-page-api/install
$(INSTALL_DIR) $(1)/lib/gluon/status-page/www/cgi-bin/dyn
$(INSTALL_BIN) $(PKG_BUILD_DIR)/neighbours-batadv $(1)/lib/gluon/status-page/www/cgi-bin/dyn/
$(CP) ./files/* $(1)/
endef

View File

@ -1,32 +0,0 @@
#!/usr/bin/lua
local json = require 'luci.jsonc'
local nixio = require 'nixio'
function neighbours()
local neighbours = {}
local list = io.lines("/sys/kernel/debug/batman_adv/bat0/originators")
for line in list do
local mac1, lastseen, tq, mac2, ifname =
line:match("^([0-9a-f:]+) +(%d+%.%d+)s +%( *(%d+)%) +([0-9a-f:]+) +%[ *(.-)%]")
if mac1 ~= nil and mac1 == mac2 then
neighbours[mac1] = { tq = tonumber(tq)
, lastseen = tonumber(lastseen)
, ifname = ifname
}
end
end
return neighbours
end
io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: text/event-stream\n\n")
while true do
local neighbours = json.stringify(neighbours())
io.write("data: " .. neighbours .. "\n\n")
io.flush()
nixio.nanosleep(1, 0)
end

View File

@ -0,0 +1,7 @@
CFLAGS += $(shell pkg-config --cflags json-c)
LDFLAGS += $(shell pkg-config --libs json-c)
all: neighbours-batadv
neighbours-batadv: neighbours-batadv.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)

View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <json-c/json.h>
#include <net/if.h>
#define STR(x) #x
#define XSTR(x) STR(x)
static json_object *neighbours(void) {
struct json_object *obj = json_object_new_object();
FILE *f;
f = fopen("/sys/kernel/debug/batman_adv/bat0/originators" , "r");
if (f == NULL) {
perror("Can not open bat0/originators");
exit(1);
}
while (!feof(f)) {
char mac1[18];
char mac2[18];
char ifname[IF_NAMESIZE+1];
int tq;
double lastseen;
int count = fscanf(f, "%17s%*[\t ]%lfs%*[\t (]%d) %17s%*[[ ]%" XSTR(IF_NAMESIZE) "[^]]]", mac1, &lastseen, &tq, mac2, ifname);
if (count != 5)
continue;
if (strcmp(mac1, mac2) == 0) {
struct json_object *neigh = json_object_new_object();
json_object_object_add(neigh, "tq", json_object_new_int(tq));
json_object_object_add(neigh, "lastseen", json_object_new_double(lastseen));
json_object_object_add(neigh, "ifname", json_object_new_string(ifname));
json_object_object_add(obj, mac1, neigh);
}
}
fclose(f);
return obj;
}
int main(void) {
struct json_object *obj;
printf("Access-Control-Allow-Origin: *\n");
printf("Content-type: text/event-stream\n\n");
while (1) {
obj = neighbours();
printf("data: %s\n\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
fflush(stdout);
json_object_put(obj);
sleep(1);
}
return 0;
}