shared_json.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 (c) 2009-2018 The GPSD project
  11. SPDX-License-Identifier: BSD-2-clause
  12. ***************************************************************************/
  13. #include "gpsd_config.h" /* must be before all includes */
  14. #include <math.h>
  15. #include <stdbool.h>
  16. #include "gpsd.h"
  17. #ifdef SOCKET_EXPORT_ENABLE
  18. #include "gps_json.h"
  19. #include "timespec.h"
  20. int json_device_read(const char *buf,
  21. struct devconfig_t *dev,
  22. const char **endptr)
  23. {
  24. double d_cycle, d_mincycle;
  25. /* *INDENT-OFF* */
  26. const struct json_attr_t json_attrs_device[] = {
  27. {"class", t_check, .dflt.check = "DEVICE"},
  28. {"path", t_string, .addr.string = dev->path,
  29. .len = sizeof(dev->path)},
  30. // odd, device->gpsdata.online is sent, but put in dev->activated?
  31. {"activated", t_time, .addr.ts = &dev->activated,
  32. .dflt.ts = {0, 0}},
  33. {"flags", t_integer, .addr.integer = &dev->flags},
  34. {"driver", t_string, .addr.string = dev->driver,
  35. .len = sizeof(dev->driver)},
  36. {"subtype", t_string, .addr.string = dev->subtype,
  37. .len = sizeof(dev->subtype)},
  38. {"subtype1", t_string, .addr.string = dev->subtype1,
  39. .len = sizeof(dev->subtype1)},
  40. {"hexdata", t_string, .addr.string = dev->hexdata,
  41. .len = sizeof(dev->hexdata)},
  42. {"native", t_integer, .addr.integer = &dev->driver_mode,
  43. .dflt.integer = DEVDEFAULT_NATIVE},
  44. {"bps", t_uinteger, .addr.uinteger = &dev->baudrate,
  45. .dflt.uinteger = DEVDEFAULT_BPS},
  46. {"parity", t_character, .addr.character = &dev->parity,
  47. .dflt.character = DEVDEFAULT_PARITY},
  48. {"stopbits", t_uinteger, .addr.uinteger = &dev->stopbits,
  49. .dflt.uinteger = DEVDEFAULT_STOPBITS},
  50. {"cycle", t_real, .addr.real = &d_cycle,
  51. .dflt.real = NAN},
  52. {"mincycle", t_real, .addr.real = &d_mincycle,
  53. .dflt.real = NAN},
  54. {NULL},
  55. };
  56. /* *INDENT-ON* */
  57. int status;
  58. status = json_read_object(buf, json_attrs_device, endptr);
  59. if (status != 0)
  60. return status;
  61. if (0 == isfinite(d_cycle)) {
  62. dev->cycle.tv_sec = 0;
  63. dev->cycle.tv_nsec = 0;
  64. } else {
  65. DTOTS(&dev->cycle, d_cycle);
  66. }
  67. if (0 == isfinite(d_mincycle)) {
  68. dev->mincycle.tv_sec = 0;
  69. dev->mincycle.tv_nsec = 0;
  70. } else {
  71. DTOTS(&dev->cycle, d_mincycle);
  72. }
  73. return 0;
  74. }
  75. int json_watch_read(const char *buf,
  76. struct gps_policy_t *ccp,
  77. const char **endptr)
  78. {
  79. bool dummy_pps_flag;
  80. /* *INDENT-OFF* */
  81. struct json_attr_t chanconfig_attrs[] = {
  82. {"class", t_check, .dflt.check = "WATCH"},
  83. {"enable", t_boolean, .addr.boolean = &ccp->watcher,
  84. .dflt.boolean = true},
  85. {"json", t_boolean, .addr.boolean = &ccp->json,
  86. .nodefault = true},
  87. {"raw", t_integer, .addr.integer = &ccp->raw,
  88. .nodefault = true},
  89. {"nmea", t_boolean, .addr.boolean = &ccp->nmea,
  90. .nodefault = true},
  91. {"scaled", t_boolean, .addr.boolean = &ccp->scaled},
  92. {"timing", t_boolean, .addr.boolean = &ccp->timing},
  93. {"split24", t_boolean, .addr.boolean = &ccp->split24},
  94. {"pps", t_boolean, .addr.boolean = &ccp->pps},
  95. {"device", t_string, .addr.string = ccp->devpath,
  96. .len = sizeof(ccp->devpath)},
  97. {"remote", t_string, .addr.string = ccp->remote,
  98. .len = sizeof(ccp->remote)},
  99. {"pps", t_boolean, .addr.boolean = &dummy_pps_flag,
  100. .nodefault = false},
  101. {NULL},
  102. };
  103. /* *INDENT-ON* */
  104. int status;
  105. status = json_read_object(buf, chanconfig_attrs, endptr);
  106. return status;
  107. }
  108. #endif /* SOCKET_EXPORT_ENABLE */
  109. /* shared_json.c ends here */