rtcm3_json.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /****************************************************************************
  2. NAME
  3. rtcm3_json.c - deserialize RTCM3 JSON
  4. DESCRIPTION
  5. This module uses the generic JSON parser to get data from RTCM3
  6. representations to libgps structures.
  7. PERMISSIONS
  8. This file is Copyright 2013 by the GPSD project
  9. SPDX-License-Identifier: BSD-2-clause
  10. ***************************************************************************/
  11. #include "gpsd_config.h" /* must be before all includes */
  12. #include <math.h>
  13. #include <stddef.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "gpsd.h"
  17. #ifdef SOCKET_EXPORT_ENABLE
  18. #include "gps_json.h"
  19. int json_rtcm3_read(const char *buf,
  20. char *path, size_t pathlen, struct rtcm3_t *rtcm3,
  21. const char **endptr)
  22. {
  23. static char *stringptrs[NITEMS(rtcm3->rtcmtypes.data)];
  24. static char stringstore[sizeof(rtcm3->rtcmtypes.data) * 2];
  25. static int stringcount;
  26. /* *INDENT-OFF* */
  27. #define RTCM3_HEADER \
  28. {"class", t_check, .dflt.check = "RTCM3"}, \
  29. {"type", t_uinteger, .addr.uinteger = &rtcm3->type}, \
  30. {"device", t_string, .addr.string = path, .len = pathlen}, \
  31. {"length", t_uinteger, .addr.uinteger = &rtcm3->length},
  32. int status = 0, satcount = 0;
  33. #define RTCM3FIELD(type, fld) STRUCTOBJECT(struct rtcm3_ ## type ## _t, fld)
  34. const struct json_attr_t rtcm1001_satellite[] = {
  35. {"ident", t_uinteger, RTCM3FIELD(1001, ident)},
  36. {"ind", t_uinteger, RTCM3FIELD(1001, L1.indicator)},
  37. {"prange", t_real, RTCM3FIELD(1001, L1.pseudorange)},
  38. {"delta", t_real, RTCM3FIELD(1001, L1.rangediff)},
  39. {"lockt", t_uinteger, RTCM3FIELD(1001, L1.locktime)},
  40. {NULL},
  41. };
  42. const struct json_attr_t rtcm1002_satellite[] = {
  43. {"ident", t_uinteger, RTCM3FIELD(1002, ident)},
  44. {"ind", t_uinteger, RTCM3FIELD(1002, L1.indicator)},
  45. {"prange", t_real, RTCM3FIELD(1002, L1.pseudorange)},
  46. {"delta", t_real, RTCM3FIELD(1002, L1.rangediff)},
  47. {"lockt", t_uinteger, RTCM3FIELD(1002, L1.locktime)},
  48. {"amb", t_uinteger, RTCM3FIELD(1002, L1.ambiguity)},
  49. {"CNR", t_real, RTCM3FIELD(1002, L1.CNR)},
  50. {NULL},
  51. };
  52. const struct json_attr_t rtcm1009_satellite[] = {
  53. {"ident", t_uinteger, RTCM3FIELD(1009, ident)},
  54. {"ind", t_uinteger, RTCM3FIELD(1009, L1.indicator)},
  55. {"channel", t_uinteger, RTCM3FIELD(1009, L1.channel)},
  56. {"prange", t_real, RTCM3FIELD(1009, L1.pseudorange)},
  57. {"delta", t_real, RTCM3FIELD(1009, L1.rangediff)},
  58. {"lockt", t_uinteger, RTCM3FIELD(1009, L1.locktime)},
  59. {NULL},
  60. };
  61. const struct json_attr_t rtcm1010_satellite[] = {
  62. {"ident", t_uinteger, RTCM3FIELD(1010, ident)},
  63. {"ind", t_uinteger, RTCM3FIELD(1010, L1.indicator)},
  64. {"channel", t_uinteger, RTCM3FIELD(1010, L1.channel)},
  65. {"prange", t_real, RTCM3FIELD(1010, L1.pseudorange)},
  66. {"delta", t_real, RTCM3FIELD(1010, L1.rangediff)},
  67. {"lockt", t_uinteger, RTCM3FIELD(1010, L1.locktime)},
  68. {"amb", t_uinteger, RTCM3FIELD(1010, L1.ambiguity)},
  69. {"CNR", t_real, RTCM3FIELD(1010, L1.CNR)},
  70. {NULL},
  71. };
  72. #undef RTCM3FIELD
  73. #define R1001 &rtcm3->rtcmtypes.rtcm3_1001.header
  74. const struct json_attr_t json_rtcm1001[] = {
  75. RTCM3_HEADER
  76. {"station_id", t_uinteger, .addr.uinteger = R1001.station_id},
  77. {"tow", t_uinteger, .addr.uinteger = (unsigned int *)R1001.tow},
  78. {"sync", t_boolean, .addr.boolean = R1001.sync},
  79. {"smoothing", t_boolean, .addr.boolean = R1001.smoothing},
  80. {"interval", t_uinteger, .addr.uinteger = R1001.interval},
  81. {"satellites", t_array,
  82. STRUCTARRAY(rtcm3->rtcmtypes.rtcm3_1001.rtk_data,
  83. rtcm1001_satellite, &satcount)},
  84. {NULL},
  85. };
  86. #undef R1001
  87. #define R1002 &rtcm3->rtcmtypes.rtcm3_1002.header
  88. const struct json_attr_t json_rtcm1002[] = {
  89. RTCM3_HEADER
  90. {"station_id", t_uinteger, .addr.uinteger = R1002.station_id},
  91. {"tow", t_uinteger, .addr.uinteger = (unsigned int *)R1002.tow},
  92. {"sync", t_boolean, .addr.boolean = R1002.sync},
  93. {"smoothing", t_boolean, .addr.boolean = R1002.smoothing},
  94. {"interval", t_uinteger, .addr.uinteger = R1002.interval},
  95. {"satellites", t_array,
  96. STRUCTARRAY(rtcm3->rtcmtypes.rtcm3_1002.rtk_data,
  97. rtcm1002_satellite, &satcount)},
  98. {NULL},
  99. };
  100. #undef R1002
  101. #define R1007 rtcm3->rtcmtypes.rtcm3_1007
  102. const struct json_attr_t json_rtcm1007[] = {
  103. RTCM3_HEADER
  104. {"station_id", t_uinteger, .addr.uinteger = &R1007.station_id},
  105. {"desc", t_string, .addr.string = R1007.descriptor,
  106. .len = sizeof(R1007.descriptor)},
  107. {"setup_id", t_uinteger, .addr.uinteger = &R1007.setup_id},
  108. {NULL},
  109. };
  110. #undef R1002
  111. #define R1008 rtcm3->rtcmtypes.rtcm3_1008
  112. const struct json_attr_t json_rtcm1008[] = {
  113. RTCM3_HEADER
  114. {"station_id", t_uinteger, .addr.uinteger = &R1008.station_id},
  115. {"desc", t_string, .addr.string = R1008.descriptor,
  116. .len = sizeof(R1008.descriptor)},
  117. {"setup_id", t_uinteger, .addr.uinteger = &R1008.setup_id},
  118. {"serial", t_string, .addr.string = R1008.serial,
  119. .len = sizeof(R1008.serial)},
  120. {NULL},
  121. };
  122. #undef R1008
  123. #define R1009 &rtcm3->rtcmtypes.rtcm3_1009.header
  124. const struct json_attr_t json_rtcm1009[] = {
  125. RTCM3_HEADER
  126. {"station_id", t_uinteger, .addr.uinteger = R1009.station_id},
  127. {"tow", t_uinteger, .addr.uinteger = (unsigned int *)R1009.tow},
  128. {"sync", t_boolean, .addr.boolean = R1009.sync},
  129. {"smoothing", t_boolean, .addr.boolean = R1009.smoothing},
  130. {"interval", t_uinteger, .addr.uinteger = R1009.interval},
  131. {"satellites", t_array,
  132. STRUCTARRAY(rtcm3->rtcmtypes.rtcm3_1009.rtk_data,
  133. rtcm1009_satellite, &satcount)},
  134. {NULL},
  135. };
  136. #undef R1010
  137. #define R1010 &rtcm3->rtcmtypes.rtcm3_1010.header
  138. const struct json_attr_t json_rtcm1010[] = {
  139. RTCM3_HEADER
  140. {"station_id", t_uinteger, .addr.uinteger = R1010.station_id},
  141. {"tow", t_uinteger, .addr.uinteger = (unsigned int *)R1010.tow},
  142. {"sync", t_boolean, .addr.boolean = R1010.sync},
  143. {"smoothing", t_boolean, .addr.boolean = R1010.smoothing},
  144. {"interval", t_uinteger, .addr.uinteger = R1010.interval},
  145. {"satellites", t_array,
  146. STRUCTARRAY(rtcm3->rtcmtypes.rtcm3_1010.rtk_data,
  147. rtcm1010_satellite, &satcount)},
  148. {NULL},
  149. };
  150. #undef R1010
  151. #define R1014 &rtcm3->rtcmtypes.rtcm3_1014
  152. const struct json_attr_t json_rtcm1014[] = {
  153. RTCM3_HEADER
  154. {"netid", t_uinteger, .addr.uinteger = R1014.network_id},
  155. {"subnetid", t_uinteger, .addr.uinteger = R1014.subnetwork_id},
  156. {"statcount", t_uinteger, .addr.uinteger = R1014.stationcount},
  157. {"master", t_uinteger, .addr.uinteger = R1014.master_id},
  158. {"aux", t_uinteger, .addr.uinteger = R1014.aux_id},
  159. {"lat", t_real, .addr.real = R1014.d_lat},
  160. {"lon", t_real, .addr.real = R1014.d_lon},
  161. {"alt", t_real, .addr.real = R1014.d_alt},
  162. {NULL},
  163. };
  164. #undef R1014
  165. #define R1033 rtcm3->rtcmtypes.rtcm3_1033
  166. const struct json_attr_t json_rtcm1033[] = {
  167. RTCM3_HEADER
  168. {"station_id", t_uinteger, .addr.uinteger = &R1033.station_id},
  169. {"desc", t_string, .addr.string = R1033.descriptor,
  170. .len = sizeof(R1033.descriptor)},
  171. {"setup_id", t_uinteger, .addr.uinteger = &R1033.setup_id},
  172. {"serial", t_string, .addr.string = R1033.serial,
  173. .len = sizeof(R1033.serial)},
  174. {"receiver", t_string, .addr.string = R1033.receiver,
  175. .len = sizeof(R1033.receiver)},
  176. {"firmware", t_string, .addr.string = R1033.firmware,
  177. .len = sizeof(R1033.firmware)},
  178. {NULL},
  179. };
  180. #undef R1033
  181. #define R1230 rtcm3->rtcmtypes.rtcm3_1230
  182. const struct json_attr_t json_rtcm1230[] = {
  183. RTCM3_HEADER
  184. {"station_id", t_uinteger, .addr.uinteger = &R1230.station_id},
  185. {"bi", t_ubyte, .addr.ubyte = &R1230.bias_indicator},
  186. {"sm", t_ubyte, .addr.ubyte = &R1230.signals_mask},
  187. {"l1ca", t_integer, .addr.integer = &R1230.l1_ca_bias,
  188. .dflt.integer = 0},
  189. {"l1p", t_integer, .addr.integer = &R1230.l1_p_bias,
  190. .dflt.integer = 0},
  191. {"l2ca", t_integer, .addr.integer = &R1230.l2_ca_bias,
  192. .dflt.integer = 0},
  193. {"l2p", t_integer, .addr.integer = &R1230.l2_p_bias,
  194. .dflt.integer = 0},
  195. {NULL},
  196. };
  197. #undef R1033
  198. const struct json_attr_t json_rtcm3_fallback[] = {
  199. RTCM3_HEADER
  200. {"data", t_array, .addr.array.element_type = t_string,
  201. .addr.array.arr.strings.ptrs = stringptrs,
  202. .addr.array.arr.strings.store = stringstore,
  203. .addr.array.arr.strings.storelen = sizeof(stringstore),
  204. .addr.array.count = &stringcount,
  205. .addr.array.maxlen = NITEMS(stringptrs)},
  206. {NULL},
  207. };
  208. #undef RTCM3_HEADER
  209. /* *INDENT-ON* */
  210. memset(rtcm3, '\0', sizeof(struct rtcm3_t));
  211. if (strstr(buf, "\"type\":1001,") != NULL) {
  212. status = json_read_object(buf, json_rtcm1001, endptr);
  213. if (status == 0)
  214. rtcm3->rtcmtypes.rtcm3_1001.header.satcount =
  215. (unsigned short)satcount;
  216. } else if (strstr(buf, "\"type\":1002,") != NULL) {
  217. status = json_read_object(buf, json_rtcm1002, endptr);
  218. if (status == 0)
  219. rtcm3->rtcmtypes.rtcm3_1002.header.satcount =
  220. (unsigned short)satcount;
  221. } else if (strstr(buf, "\"type\":1007,") != NULL) {
  222. status = json_read_object(buf, json_rtcm1007, endptr);
  223. } else if (strstr(buf, "\"type\":1008,") != NULL) {
  224. status = json_read_object(buf, json_rtcm1008, endptr);
  225. } else if (strstr(buf, "\"type\":1009,") != NULL) {
  226. status = json_read_object(buf, json_rtcm1009, endptr);
  227. } else if (strstr(buf, "\"type\":1010,") != NULL) {
  228. status = json_read_object(buf, json_rtcm1010, endptr);
  229. } else if (strstr(buf, "\"type\":1014,") != NULL) {
  230. status = json_read_object(buf, json_rtcm1014, endptr);
  231. } else if (strstr(buf, "\"type\":1033,") != NULL) {
  232. status = json_read_object(buf, json_rtcm1033, endptr);
  233. } else if (strstr(buf, "\"type\":1230,") != NULL) {
  234. status = json_read_object(buf, json_rtcm1230, endptr);
  235. } else {
  236. int n;
  237. status = json_read_object(buf, json_rtcm3_fallback, endptr);
  238. for (n = 0; n < NITEMS(rtcm3->rtcmtypes.data); n++) {
  239. if (n >= stringcount) {
  240. rtcm3->rtcmtypes.data[n] = '\0';
  241. } else {
  242. unsigned int u;
  243. int fldcount = sscanf(stringptrs[n], "0x%02x\n", &u);
  244. if (fldcount != 1)
  245. return JSON_ERR_MISC;
  246. else
  247. rtcm3->rtcmtypes.data[n] = (char)u;
  248. }
  249. }
  250. }
  251. return status;
  252. }
  253. #endif /* SOCKET_EXPORT_ENABLE */
  254. /* rtcm3_json.c ends here */
  255. // vim: set expandtab shiftwidth=4