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