Created
September 1, 2015 21:32
-
-
Save tcatm/e397f090aa9420901ad0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <json.h> | |
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("Ca not open bat0/originators"); | |
exit(1); | |
} | |
while (!feof(f)) { | |
char mac1[18]; | |
char mac2[18]; | |
char ifname[33]; | |
int tq; | |
double lastseen; | |
int count = fscanf(f, "%17s%*[\t ]%lfs%*[\t (]%d) %17s%*[\[ ]%32[^]]]", 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment