shared_json.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /****************************************************************************
  2. NAME
  3. shared_json.c - move data between in-core and JSON structures
  4. DESCRIPTION
  5. This module uses the generic JSON parser to get data from JSON
  6. representations to gps.h structures. These functions are used in both
  7. the daemon and the client library.
  8. PERMISSIONS
  9. Written by Eric S. Raymond, 2009
  10. This file is Copyright 2009 The GPSD project
  11. SPDX-License-Identifier: BSD-2-clause
  12. ***************************************************************************/
  13. #include "../include/gpsd_config.h" /* must be before all includes */
  14. #include <math.h>
  15. #include <stdbool.h>
  16. #include "../include/gpsd.h"
  17. #ifdef SOCKET_EXPORT_ENABLE
  18. #include "../include/gps_json.h"
  19. #include "../include/strfuncs.h"
  20. #include "../include/timespec.h"
  21. int json_device_read(const char *buf,
  22. struct devconfig_t *dev,
  23. const char **endptr)
  24. {
  25. // initialized to shut up clang
  26. double d_cycle = 0.0, d_mincycle = 0.0;
  27. /* *INDENT-OFF* */
  28. const struct json_attr_t json_attrs_device[] = {
  29. {"class", t_check, .dflt.check = "DEVICE"},
  30. {"path", t_string, .addr.string = dev->path,
  31. .len = sizeof(dev->path)},
  32. // odd, device->gpsdata.online is sent, but put in dev->activated?
  33. {"activated", t_time, .addr.ts = &dev->activated,
  34. .dflt.ts = {0, 0}},
  35. {"flags", t_integer, .addr.integer = &dev->flags},
  36. {"driver", t_string, .addr.string = dev->driver,
  37. .len = sizeof(dev->driver)},
  38. {"subtype", t_string, .addr.string = dev->subtype,
  39. .len = sizeof(dev->subtype)},
  40. {"subtype1", t_string, .addr.string = dev->subtype1,
  41. .len = sizeof(dev->subtype1)},
  42. {"hexdata", t_string, .addr.string = dev->hexdata,
  43. .len = sizeof(dev->hexdata)},
  44. {"native", t_integer, .addr.integer = &dev->driver_mode,
  45. .dflt.integer = DEVDEFAULT_NATIVE},
  46. {"bps", t_uinteger, .addr.uinteger = &dev->baudrate,
  47. .dflt.uinteger = DEVDEFAULT_BPS},
  48. {"parity", t_character, .addr.character = &dev->parity,
  49. .dflt.character = DEVDEFAULT_PARITY},
  50. {"stopbits", t_uinteger, .addr.uinteger = &dev->stopbits,
  51. .dflt.uinteger = DEVDEFAULT_STOPBITS},
  52. {"cycle", t_real, .addr.real = &d_cycle,
  53. .dflt.real = NAN},
  54. {"mincycle", t_real, .addr.real = &d_mincycle,
  55. .dflt.real = NAN},
  56. // ignore unknown keys, for cross-version compatibility
  57. {"", t_ignore},
  58. {NULL},
  59. };
  60. /* *INDENT-ON* */
  61. int status;
  62. status = json_read_object(buf, json_attrs_device, endptr);
  63. if (status != 0)
  64. return status;
  65. if (0 == isfinite(d_cycle)) {
  66. dev->cycle.tv_sec = 0;
  67. dev->cycle.tv_nsec = 0;
  68. } else {
  69. DTOTS(&dev->cycle, d_cycle);
  70. }
  71. if (0 == isfinite(d_mincycle)) {
  72. dev->mincycle.tv_sec = 0;
  73. dev->mincycle.tv_nsec = 0;
  74. } else {
  75. DTOTS(&dev->mincycle, d_mincycle);
  76. }
  77. return 0;
  78. }
  79. int json_watch_read(const char *buf,
  80. struct gps_policy_t *ccp,
  81. const char **endptr)
  82. {
  83. /* *INDENT-OFF* */
  84. struct json_attr_t chanconfig_attrs[] = {
  85. {"class", t_check, .dflt.check = "WATCH"},
  86. {"device", t_string, .addr.string = ccp->devpath,
  87. .len = sizeof(ccp->devpath)},
  88. {"enable", t_boolean, .addr.boolean = &ccp->watcher,
  89. .dflt.boolean = true},
  90. {"json", t_boolean, .addr.boolean = &ccp->json,
  91. .nodefault = true},
  92. {"nmea", t_boolean, .addr.boolean = &ccp->nmea,
  93. .nodefault = true},
  94. {"pps", t_boolean, .addr.boolean = &ccp->pps},
  95. {"raw", t_integer, .addr.integer = &ccp->raw,
  96. .nodefault = true},
  97. {"remote", t_string, .addr.string = ccp->remote,
  98. .len = sizeof(ccp->remote)},
  99. {"scaled", t_boolean, .addr.boolean = &ccp->scaled},
  100. {"split24", t_boolean, .addr.boolean = &ccp->split24},
  101. {"timing", t_boolean, .addr.boolean = &ccp->timing},
  102. // ignore unknown keys, for cross-version compatibility
  103. {"", t_ignore},
  104. {NULL},
  105. };
  106. /* *INDENT-ON* */
  107. int status;
  108. status = json_read_object(buf, chanconfig_attrs, endptr);
  109. return status;
  110. }
  111. /* Translate a gps_policy_t to a WATCH string (outbuf)
  112. * return outbuf
  113. */
  114. char *json_policy_to_watch(struct gps_policy_t *ccp,
  115. char *outbuf, size_t outbuf_len)
  116. {
  117. char *str;
  118. snprintf(outbuf, outbuf_len, "?WATCH={\"device\":\"%s\"", ccp->devpath);
  119. str = ccp->watcher ? ",\"enable\":true" : ",\"enable\":false";
  120. (void)strlcat(outbuf, str, outbuf_len);
  121. str = ccp->json ? ",\"json\":true" : ",\"json\":false";
  122. (void)strlcat(outbuf, str, outbuf_len);
  123. str = ccp->nmea ? ",\"nmea\":true" : ",\"nmea\":false";
  124. (void)strlcat(outbuf, str, outbuf_len);
  125. str = ccp->pps ? ",\"pps\":true" : ",\"pps\":false";
  126. (void)strlcat(outbuf, str, outbuf_len);
  127. str_appendf(outbuf, outbuf_len, ",\"raw\":%u", ccp->raw);
  128. if ('\0' != ccp->remote[0])
  129. str_appendf(outbuf, outbuf_len, ",\"remote\":%s", ccp->remote);
  130. str = ccp->scaled ? ",\"scaled\":true" : ",\"scaled\":false";
  131. (void)strlcat(outbuf, str, outbuf_len);
  132. str = ccp->split24 ? ",\"split24\":true" : ",\"split24\":false";
  133. (void)strlcat(outbuf, str, outbuf_len);
  134. str = ccp->timing ? ",\"timing\":true}\r\n" : ",\"timing\":false}\r\n";
  135. (void)strlcat(outbuf, str, outbuf_len);
  136. return outbuf;
  137. }
  138. #endif /* SOCKET_EXPORT_ENABLE */
  139. /* shared_json.c ends here */
  140. // vim: set expandtab shiftwidth=4