neighbours-batadv.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <json-c/json.h>
  5. #include <net/if.h>
  6. #define STR(x) #x
  7. #define XSTR(x) STR(x)
  8. static json_object *neighbours(void) {
  9. struct json_object *obj = json_object_new_object();
  10. FILE *f;
  11. f = fopen("/sys/kernel/debug/batman_adv/bat0/originators" , "r");
  12. if (f == NULL) {
  13. perror("Can not open bat0/originators");
  14. exit(1);
  15. }
  16. while (!feof(f)) {
  17. char mac1[18];
  18. char mac2[18];
  19. char ifname[IF_NAMESIZE+1];
  20. int tq;
  21. double lastseen;
  22. int count = fscanf(f, "%17s%*[\t ]%lfs%*[\t (]%d) %17s%*[[ ]%" XSTR(IF_NAMESIZE) "[^]]]", mac1, &lastseen, &tq, mac2, ifname);
  23. if (count != 5)
  24. continue;
  25. if (strcmp(mac1, mac2) == 0) {
  26. struct json_object *neigh = json_object_new_object();
  27. json_object_object_add(neigh, "tq", json_object_new_int(tq));
  28. json_object_object_add(neigh, "lastseen", json_object_new_double(lastseen));
  29. json_object_object_add(neigh, "ifname", json_object_new_string(ifname));
  30. json_object_object_add(obj, mac1, neigh);
  31. }
  32. }
  33. fclose(f);
  34. return obj;
  35. }
  36. int main(void) {
  37. struct json_object *obj;
  38. printf("Access-Control-Allow-Origin: *\n");
  39. printf("Content-type: text/event-stream\n\n");
  40. while (1) {
  41. obj = neighbours();
  42. printf("data: %s\n\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  43. fflush(stdout);
  44. json_object_put(obj);
  45. sleep(1);
  46. }
  47. return 0;
  48. }