summaryrefslogtreecommitdiff
path: root/rtcm2_json.c
blob: a0936ebd36dba8127761bcbfc7866a1102bb55a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/****************************************************************************

NAME
   rtcm2_json.c - deserialize RTCM2 JSON

DESCRIPTION
   This module uses the generic JSON parser to get data from RTCM2
representations to libgps structures.

PERMISSIONS
   This file is Copyright (c) 2010-2018 by the GPSD project
   SPDX-License-Identifier: BSD-2-clause

***************************************************************************/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stddef.h>

#include "gpsd.h"

#ifdef SOCKET_EXPORT_ENABLE
#include "gps_json.h"

/* common fields in every RTCM2 message */

int json_rtcm2_read(const char *buf,
		    char *path, size_t pathlen, struct rtcm2_t *rtcm2,
		    const char **endptr)
{

    static char *stringptrs[NITEMS(rtcm2->words)];
    static char stringstore[sizeof(rtcm2->words) * 2];
    static int stringcount;

/* *INDENT-OFF* */
#define RTCM2_HEADER \
	{"class",          t_check,    .dflt.check = "RTCM2"}, \
	{"type",           t_uinteger, .addr.uinteger = &rtcm2->type}, \
	{"device",         t_string,   .addr.string = path, \
	                                  .len = pathlen}, \
	{"station_id",     t_uinteger, .addr.uinteger = &rtcm2->refstaid}, \
	{"zcount",         t_real,     .addr.real = &rtcm2->zcount, \
			                  .dflt.real = NAN}, \
	{"seqnum",         t_uinteger, .addr.uinteger = &rtcm2->seqnum}, \
	{"length",         t_uinteger, .addr.uinteger = &rtcm2->length}, \
	{"station_health", t_uinteger, .addr.uinteger = &rtcm2->stathlth},

    int status = 0, satcount = 0;

    const struct json_attr_t rtcm1_satellite[] = {
	{"ident",     t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, ident)},
	{"udre",      t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, udre)},
	{"iod",       t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, iod)},
	{"prc",       t_real,     STRUCTOBJECT(struct gps_rangesat_t, prc)},
	{"rrc",       t_real,     STRUCTOBJECT(struct gps_rangesat_t, rrc)},
	{NULL},
    };
    const struct json_attr_t json_rtcm1[] = {
	RTCM2_HEADER
        {"satellites", t_array,	STRUCTARRAY(rtcm2->gps_ranges.sat,
					    rtcm1_satellite, &satcount)},
	{NULL},
    };

    const struct json_attr_t json_rtcm3[] = {
	RTCM2_HEADER
	{"x",              t_real,    .addr.real = &rtcm2->ecef.x,
			                 .dflt.real = NAN},
	{"y",              t_real,    .addr.real = &rtcm2->ecef.y,
			                 .dflt.real = NAN},
	{"z",              t_real,    .addr.real = &rtcm2->ecef.z,
			                 .dflt.real = NAN},
	{NULL},
    };

    /*
     * Beware! Needs to stay synchronized with a corresponding
     * name array in the RTCM2 JSON dump code. This interpretation of
     * NAVSYSTEM_GALILEO is assumed from RTCM3, it's not actually
     * documented in RTCM 2.1.
     */
    const struct json_enum_t system_table[] = {
	{"GPS", 0}, {"GLONASS", 1}, {"GALILEO", 2}, {"UNKNOWN", 3}, {NULL}
    };
    const struct json_attr_t json_rtcm4[] = {
	RTCM2_HEADER
        {"valid",          t_boolean, .addr.boolean = &rtcm2->reference.valid},
	{"system",         t_integer, .addr.integer = &rtcm2->reference.system,
	                                 .map=system_table},
	{"sense",          t_integer, .addr.integer = &rtcm2->reference.sense},
	{"datum",          t_string,  .addr.string = rtcm2->reference.datum,
	                                 .len = sizeof(rtcm2->reference.datum)},
	{"dx",             t_real,    .addr.real = &rtcm2->reference.dx,
			                 .dflt.real = NAN},
	{"dy",             t_real,    .addr.real = &rtcm2->reference.dy,
			                 .dflt.real = NAN},
	{"dz",             t_real,    .addr.real = &rtcm2->reference.dz,
			                 .dflt.real = NAN},
	{NULL},
    };

    const struct json_attr_t rtcm5_satellite[] = {
	{"ident",       t_uinteger, STRUCTOBJECT(struct consat_t, ident)},
	{"iodl",        t_boolean,  STRUCTOBJECT(struct consat_t, iodl)},
	{"health",      t_uinteger, STRUCTOBJECT(struct consat_t, health)},
	{"snr",         t_integer,  STRUCTOBJECT(struct consat_t, snr)},
	{"health_en",   t_boolean,  STRUCTOBJECT(struct consat_t, health_en)},
	{"new_data",    t_boolean,  STRUCTOBJECT(struct consat_t, new_data)},
	{"los_warning", t_boolean,  STRUCTOBJECT(struct consat_t, los_warning)},
	{"tou",         t_uinteger, STRUCTOBJECT(struct consat_t, tou)},
	{NULL},
    };
    const struct json_attr_t json_rtcm5[] = {
	RTCM2_HEADER
        {"satellites", t_array,	STRUCTARRAY(rtcm2->conhealth.sat,
					    rtcm5_satellite, &satcount)},
	{NULL},
    };

    const struct json_attr_t json_rtcm6[] = {
	RTCM2_HEADER
	// No-op or keepalive message
	{NULL},
    };

    const struct json_attr_t rtcm7_satellite[] = {
	{"lat",         t_real,     STRUCTOBJECT(struct station_t, latitude)},
	{"lon",         t_real,     STRUCTOBJECT(struct station_t, longitude)},
	{"range",       t_uinteger, STRUCTOBJECT(struct station_t, range)},
	{"frequency",   t_real,     STRUCTOBJECT(struct station_t, frequency)},
	{"health",      t_uinteger, STRUCTOBJECT(struct station_t, health)},
	{"station_id",  t_uinteger, STRUCTOBJECT(struct station_t, station_id)},
	{"bitrate",     t_uinteger, STRUCTOBJECT(struct station_t, bitrate)},
	{NULL},
    };
    const struct json_attr_t json_rtcm7[] = {
	RTCM2_HEADER
        {"satellites", t_array,	STRUCTARRAY(rtcm2->almanac.station,
					    rtcm7_satellite, &satcount)},
	{NULL},
    };

    const struct json_attr_t json_rtcm13[] = {
	RTCM2_HEADER
        {"status",       t_boolean,  .addr.boolean = &rtcm2->xmitter.status},
        {"rangeflag",    t_boolean,  .addr.boolean = &rtcm2->xmitter.rangeflag},
	{"lat",          t_real,     .addr.real = &rtcm2->xmitter.lat,
			                .dflt.real = NAN},
	{"lon",          t_real,     .addr.real = &rtcm2->xmitter.lon,
			                .dflt.real = NAN},
	{"range",        t_uinteger, .addr.uinteger = &rtcm2->xmitter.range},
	{NULL},
    };

    const struct json_attr_t json_rtcm14[] = {
	RTCM2_HEADER
	{"week",              t_uinteger, .addr.uinteger = &rtcm2->gpstime.week},
	{"hour",              t_uinteger, .addr.uinteger = &rtcm2->gpstime.hour},
	{"leapsecs",          t_uinteger, .addr.uinteger = &rtcm2->gpstime.leapsecs},
	{NULL},
    };

    const struct json_attr_t json_rtcm16[] = {
	RTCM2_HEADER
	{"message",        t_string,  .addr.string = rtcm2->message,
	                                 .len = sizeof(rtcm2->message)},
	{NULL},
    };

    const struct json_attr_t rtcm31_satellite[] = {
	{"ident",     t_uinteger, STRUCTOBJECT(struct glonass_rangesat_t, ident)},
	{"udre",      t_uinteger, STRUCTOBJECT(struct glonass_rangesat_t, udre)},
	{"change",    t_boolean,  STRUCTOBJECT(struct glonass_rangesat_t, change)},
	{"tod",       t_uinteger, STRUCTOBJECT(struct glonass_rangesat_t, tod)},
	{"prc",       t_real,     STRUCTOBJECT(struct glonass_rangesat_t, prc)},
	{"rrc",       t_real,     STRUCTOBJECT(struct glonass_rangesat_t, rrc)},
	{NULL},
    };
    const struct json_attr_t json_rtcm31[] = {
	RTCM2_HEADER
        {"satellites", t_array,	STRUCTARRAY(rtcm2->glonass_ranges.sat,
					    rtcm31_satellite, &satcount)},
	{NULL},
    };

    const struct json_attr_t json_rtcm2_fallback[] = {
	RTCM2_HEADER
	{"data",         t_array, .addr.array.element_type = t_string,
	                             .addr.array.arr.strings.ptrs = stringptrs,
	                             .addr.array.arr.strings.store = stringstore,
	                             .addr.array.arr.strings.storelen = sizeof(stringstore),
	                             .addr.array.count = &stringcount,
	                             .addr.array.maxlen = NITEMS(stringptrs)},
	{NULL},
    };

#undef RTCM2_HEADER
/* *INDENT-ON* */

    memset(rtcm2, '\0', sizeof(struct rtcm2_t));

    if (strstr(buf, "\"type\":1,") != NULL
	|| strstr(buf, "\"type\":9,") != NULL) {
	status = json_read_object(buf, json_rtcm1, endptr);
	if (status == 0)
	    rtcm2->gps_ranges.nentries = (unsigned)satcount;
    } else if (strstr(buf, "\"type\":3,") != NULL) {
	status = json_read_object(buf, json_rtcm3, endptr);
	if (status == 0) {
	    rtcm2->ecef.valid = (isfinite(rtcm2->ecef.x) != 0)
		&& (isfinite(rtcm2->ecef.y) != 0)
		&& (isfinite(rtcm2->ecef.z) != 0);
	}
    } else if (strstr(buf, "\"type\":4,") != NULL) {
	status = json_read_object(buf, json_rtcm4, endptr);
	if (status == 0)
	    rtcm2->reference.valid = (isfinite(rtcm2->reference.dx) != 0)
		&& (isfinite(rtcm2->reference.dy) != 0)
		&& (isfinite(rtcm2->reference.dz) != 0);
    } else if (strstr(buf, "\"type\":5,") != NULL) {
	status = json_read_object(buf, json_rtcm5, endptr);
	if (status == 0)
	    rtcm2->conhealth.nentries = (unsigned)satcount;
    } else if (strstr(buf, "\"type\":6,") != NULL) {
	status = json_read_object(buf, json_rtcm6, endptr);
    } else if (strstr(buf, "\"type\":7,") != NULL) {
	status = json_read_object(buf, json_rtcm7, endptr);
	if (status == 0)
	    rtcm2->almanac.nentries = (unsigned)satcount;
    } else if (strstr(buf, "\"type\":13,") != NULL) {
	status = json_read_object(buf, json_rtcm13, endptr);
    } else if (strstr(buf, "\"type\":14,") != NULL) {
	status = json_read_object(buf, json_rtcm14, endptr);
    } else if (strstr(buf, "\"type\":16,") != NULL) {
	status = json_read_object(buf, json_rtcm16, endptr);
    } else if (strstr(buf, "\"type\":31,") != NULL) {
	status = json_read_object(buf, json_rtcm31, endptr);
	if (status == 0)
	    rtcm2->glonass_ranges.nentries = (unsigned)satcount;
    } else {
	int n;
	status = json_read_object(buf, json_rtcm2_fallback, endptr);
	for (n = 0; n < NITEMS(rtcm2->words); n++) {
	    if (n >= stringcount) {
		rtcm2->words[n] = 0;
	    } else {
		unsigned int u;
		int fldcount = sscanf(stringptrs[n], "0x%08x\n", &u);
		if (fldcount != 1)
		    return JSON_ERR_MISC;
		else
		    rtcm2->words[n] = (isgps30bits_t) u;
	    }
	}
    }
    return status;
}
#endif /* SOCKET_EXPORT_ENABLE */

/* rtcm2_json.c ends here */