diff --git a/package/gluon-status-page-api/Makefile b/package/gluon-status-page-api/Makefile index 2b32a2ea..863bbc39 100644 --- a/package/gluon-status-page-api/Makefile +++ b/package/gluon-status-page-api/Makefile @@ -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 diff --git a/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-batadv b/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-batadv deleted file mode 100755 index 3bcdf89b..00000000 --- a/package/gluon-status-page-api/files/lib/gluon/status-page/www/cgi-bin/dyn/neighbours-batadv +++ /dev/null @@ -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 diff --git a/package/gluon-status-page-api/src/Makefile b/package/gluon-status-page-api/src/Makefile new file mode 100644 index 00000000..499bb55b --- /dev/null +++ b/package/gluon-status-page-api/src/Makefile @@ -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) diff --git a/package/gluon-status-page-api/src/neighbours-batadv.c b/package/gluon-status-page-api/src/neighbours-batadv.c new file mode 100755 index 00000000..a386b5e0 --- /dev/null +++ b/package/gluon-status-page-api/src/neighbours-batadv.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +#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; +}