respondd.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <string.h>
  27. static struct json_object * get_autoupdater(void) {
  28. struct uci_context *ctx = uci_alloc_context();
  29. ctx->flags &= ~UCI_FLAG_STRICT;
  30. struct uci_package *p;
  31. if (uci_load(ctx, "autoupdater", &p))
  32. goto error;
  33. struct uci_section *s = uci_lookup_section(ctx, p, "settings");
  34. if (!s)
  35. goto error;
  36. struct json_object *ret = json_object_new_object();
  37. json_object_object_add(ret, "branch", gluonutil_wrap_string(uci_lookup_option_string(ctx, s, "branch")));
  38. const char *enabled = uci_lookup_option_string(ctx, s, "enabled");
  39. json_object_object_add(ret, "enabled", json_object_new_boolean(enabled && !strcmp(enabled, "1")));
  40. uci_free_context(ctx);
  41. return ret;
  42. error:
  43. uci_free_context(ctx);
  44. return NULL;
  45. }
  46. static struct json_object * respondd_provider_nodeinfo(void) {
  47. struct json_object *ret = json_object_new_object();
  48. struct json_object *software = json_object_new_object();
  49. json_object_object_add(software, "autoupdater", get_autoupdater());
  50. json_object_object_add(ret, "software", software);
  51. return ret;
  52. }
  53. const struct respondd_provider_info respondd_providers[] = {
  54. {"nodeinfo", respondd_provider_nodeinfo},
  55. {}
  56. };