respondd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <respondd.h>
  23. #include <json-c/json.h>
  24. #include <libgluonutil.h>
  25. #include <uci.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. static struct uci_section * get_first_section(struct uci_package *p, const char *type) {
  29. struct uci_element *e;
  30. uci_foreach_element(&p->sections, e) {
  31. struct uci_section *s = uci_to_section(e);
  32. if (!strcmp(s->type, type))
  33. return s;
  34. }
  35. return NULL;
  36. }
  37. static const char * get_first_option(struct uci_context *ctx, struct uci_package *p, const char *type, const char *option) {
  38. struct uci_section *s = get_first_section(p, type);
  39. if (s)
  40. return uci_lookup_option_string(ctx, s, option);
  41. else
  42. return NULL;
  43. }
  44. static struct json_object * get_number(struct uci_context *ctx, struct uci_section *s, const char *name) {
  45. const char *val = uci_lookup_option_string(ctx, s, name);
  46. if (!val || !*val)
  47. return NULL;
  48. char *end;
  49. double d = strtod(val, &end);
  50. if (*end)
  51. return NULL;
  52. struct json_object *jso = json_object_new_double(d);
  53. json_object_set_serializer(jso, json_object_double_to_json_string, "%.8f", NULL);
  54. return jso;
  55. }
  56. static void maybe_add_number(struct uci_context *ctx, struct uci_section *s, const char *name, struct json_object *parent) {
  57. struct json_object *jso = get_number(ctx, s, name);
  58. if (jso)
  59. json_object_object_add(parent, name, jso);
  60. }
  61. static struct json_object * get_location(struct uci_context *ctx, struct uci_package *p) {
  62. struct uci_section *s = get_first_section(p, "location");
  63. if (!s)
  64. return NULL;
  65. const char *share = uci_lookup_option_string(ctx, s, "share_location");
  66. if (!share || strcmp(share, "1"))
  67. return NULL;
  68. struct json_object *ret = json_object_new_object();
  69. maybe_add_number(ctx, s, "latitude", ret);
  70. maybe_add_number(ctx, s, "longitude", ret);
  71. maybe_add_number(ctx, s, "altitude", ret);
  72. return ret;
  73. }
  74. static struct json_object * get_owner(struct uci_context *ctx, struct uci_package *p) {
  75. const char *contact = get_first_option(ctx, p, "owner", "contact");
  76. if (!contact || !*contact)
  77. return NULL;
  78. struct json_object *ret = json_object_new_object();
  79. json_object_object_add(ret, "contact", gluonutil_wrap_string(contact));
  80. return ret;
  81. }
  82. static struct json_object * get_system(struct uci_context *ctx, struct uci_package *p) {
  83. struct json_object *ret = json_object_new_object();
  84. const char *role = get_first_option(ctx, p, "system", "role");
  85. if (role && *role)
  86. json_object_object_add(ret, "role", gluonutil_wrap_string(role));
  87. return ret;
  88. }
  89. static struct json_object * respondd_provider_nodeinfo(void) {
  90. struct json_object *ret = json_object_new_object();
  91. struct uci_context *ctx = uci_alloc_context();
  92. ctx->flags &= ~UCI_FLAG_STRICT;
  93. struct uci_package *p;
  94. if (!uci_load(ctx, "gluon-node-info", &p)) {
  95. struct json_object *location = get_location(ctx, p);
  96. if (location)
  97. json_object_object_add(ret, "location", location);
  98. struct json_object *owner = get_owner(ctx, p);
  99. if (owner)
  100. json_object_object_add(ret, "owner", owner);
  101. json_object_object_add(ret, "system", get_system(ctx, p));
  102. }
  103. uci_free_context(ctx);
  104. return ret;
  105. }
  106. const struct respondd_provider_info respondd_providers[] = {
  107. {"nodeinfo", respondd_provider_nodeinfo},
  108. {}
  109. };