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