Coverage Report

Created: 2025-03-14 06:43

/src/gpsd/gpsd-3.25.1~dev/libgps/rtcm2_json.c
Line
Count
Source
1
/****************************************************************************
2
3
NAME
4
   rtcm2_json.c - deserialize RTCM2 JSON
5
6
DESCRIPTION
7
   This module uses the generic JSON parser to get data from RTCM2
8
representations to libgps structures.
9
10
PERMISSIONS
11
   This file is Copyright 2010 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
25
#include "../include/gps_json.h"
26
27
// common fields in every RTCM2 message
28
29
int json_rtcm2_read(const char *buf,
30
                    char *path, size_t pathlen, struct rtcm2_t *rtcm2,
31
                    const char **endptr)
32
287
{
33
34
287
    static char *stringptrs[NITEMS(rtcm2->words)];
35
287
    static char stringstore[sizeof(rtcm2->words) * 2];
36
287
    static int stringcount;
37
38
// *INDENT-OFF*
39
287
#define RTCM2_HEADER \
40
3.15k
        {"class",          t_check,    .dflt.check = "RTCM2"}, \
41
3.15k
        {"type",           t_uinteger, .addr.uinteger = &rtcm2->type}, \
42
3.15k
        {"device",         t_string,   .addr.string = path, \
43
3.15k
                                          .len = pathlen}, \
44
3.15k
        {"station_id",     t_uinteger, .addr.uinteger = &rtcm2->refstaid}, \
45
3.15k
        {"zcount",         t_real,     .addr.real = &rtcm2->zcount, \
46
3.15k
                                          .dflt.real = NAN}, \
47
3.15k
        {"seqnum",         t_uinteger, .addr.uinteger = &rtcm2->seqnum}, \
48
3.15k
        {"length",         t_uinteger, .addr.uinteger = &rtcm2->length}, \
49
3.15k
        {"station_health", t_uinteger, .addr.uinteger = &rtcm2->stathlth},
50
51
287
    int status = 0, satcount = 0;
52
53
287
    const struct json_attr_t rtcm1_satellite[] = {
54
287
        {"ident",     t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, ident)},
55
287
        {"udre",      t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, udre)},
56
287
        {"iod",       t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, iod)},
57
287
        {"prc",       t_real,     STRUCTOBJECT(struct gps_rangesat_t, prc)},
58
287
        {"rrc",       t_real,     STRUCTOBJECT(struct gps_rangesat_t, rrc)},
59
287
        {NULL},
60
287
    };
61
287
    const struct json_attr_t json_rtcm1[] = {
62
287
        RTCM2_HEADER
63
287
        {"satellites", t_array, STRUCTARRAY(rtcm2->gps_ranges.sat,
64
287
                                            rtcm1_satellite, &satcount)},
65
287
        {NULL},
66
287
    };
67
68
287
    const struct json_attr_t json_rtcm3[] = {
69
287
        RTCM2_HEADER
70
287
        {"x",              t_real,    .addr.real = &rtcm2->ref_sta.x,
71
287
                                         .dflt.real = NAN},
72
287
        {"y",              t_real,    .addr.real = &rtcm2->ref_sta.y,
73
287
                                         .dflt.real = NAN},
74
287
        {"z",              t_real,    .addr.real = &rtcm2->ref_sta.z,
75
287
                                         .dflt.real = NAN},
76
287
        {NULL},
77
287
    };
78
79
    /*
80
     * Beware! Needs to stay synchronized with a corresponding
81
     * name array in the RTCM2 JSON dump code. This interpretation of
82
     * NAVSYSTEM_GALILEO is assumed from RTCM3, it's not actually
83
     * documented in RTCM 2.1.
84
     */
85
287
    const struct json_enum_t system_table[] = {
86
287
        {"GPS", 0}, {"GLONASS", 1}, {"GALILEO", 2}, {"UNKNOWN", 3}, {NULL}
87
287
    };
88
287
    const struct json_attr_t json_rtcm4[] = {
89
287
        RTCM2_HEADER
90
287
        {"valid",          t_boolean, .addr.boolean = &rtcm2->reference.valid},
91
287
        {"system",         t_integer, .addr.integer = &rtcm2->reference.system,
92
287
                                         .map=system_table},
93
287
        {"sense",          t_integer, .addr.integer = &rtcm2->reference.sense},
94
287
        {"datum",          t_string,  .addr.string = rtcm2->reference.datum,
95
287
                                         .len = sizeof(rtcm2->reference.datum)},
96
287
        {"dx",             t_real,    .addr.real = &rtcm2->reference.dx,
97
287
                                         .dflt.real = NAN},
98
287
        {"dy",             t_real,    .addr.real = &rtcm2->reference.dy,
99
287
                                         .dflt.real = NAN},
100
287
        {"dz",             t_real,    .addr.real = &rtcm2->reference.dz,
101
287
                                         .dflt.real = NAN},
102
287
        {NULL},
103
287
    };
104
105
287
    const struct json_attr_t rtcm5_satellite[] = {
106
287
        {"ident",       t_uinteger, STRUCTOBJECT(struct consat_t, ident)},
107
287
        {"iodl",        t_boolean,  STRUCTOBJECT(struct consat_t, iodl)},
108
287
        {"health",      t_uinteger, STRUCTOBJECT(struct consat_t, health)},
109
287
        {"snr",         t_integer,  STRUCTOBJECT(struct consat_t, snr)},
110
287
        {"health_en",   t_boolean,  STRUCTOBJECT(struct consat_t, health_en)},
111
287
        {"new_data",    t_boolean,  STRUCTOBJECT(struct consat_t, new_data)},
112
287
        {"los_warning", t_boolean,  STRUCTOBJECT(struct consat_t, los_warning)},
113
287
        {"tou",         t_uinteger, STRUCTOBJECT(struct consat_t, tou)},
114
287
        {NULL},
115
287
    };
116
287
    const struct json_attr_t json_rtcm5[] = {
117
287
        RTCM2_HEADER
118
287
        {"satellites", t_array, STRUCTARRAY(rtcm2->conhealth.sat,
119
287
                                            rtcm5_satellite, &satcount)},
120
287
        {NULL},
121
287
    };
122
123
287
    const struct json_attr_t json_rtcm6[] = {
124
287
        RTCM2_HEADER
125
        // No-op or keepalive message
126
287
        {NULL},
127
287
    };
128
129
287
    const struct json_attr_t rtcm7_satellite[] = {
130
287
        {"lat",         t_real,     STRUCTOBJECT(struct station_t, latitude)},
131
287
        {"lon",         t_real,     STRUCTOBJECT(struct station_t, longitude)},
132
287
        {"range",       t_uinteger, STRUCTOBJECT(struct station_t, range)},
133
287
        {"frequency",   t_real,     STRUCTOBJECT(struct station_t, frequency)},
134
287
        {"health",      t_uinteger, STRUCTOBJECT(struct station_t, health)},
135
287
        {"station_id",  t_uinteger, STRUCTOBJECT(struct station_t, station_id)},
136
287
        {"bitrate",     t_uinteger, STRUCTOBJECT(struct station_t, bitrate)},
137
287
        {NULL},
138
287
    };
139
287
    const struct json_attr_t json_rtcm7[] = {
140
287
        RTCM2_HEADER
141
287
        {"satellites", t_array, STRUCTARRAY(rtcm2->almanac.station,
142
287
                                            rtcm7_satellite, &satcount)},
143
287
        {NULL},
144
287
    };
145
146
287
    const struct json_attr_t json_rtcm13[] = {
147
287
        RTCM2_HEADER
148
287
        {"status",       t_boolean,  .addr.boolean = &rtcm2->xmitter.status},
149
287
        {"rangeflag",    t_boolean,  .addr.boolean = &rtcm2->xmitter.rangeflag},
150
287
        {"lat",          t_real,     .addr.real = &rtcm2->xmitter.lat,
151
287
                                        .dflt.real = NAN},
152
287
        {"lon",          t_real,     .addr.real = &rtcm2->xmitter.lon,
153
287
                                        .dflt.real = NAN},
154
287
        {"range",        t_uinteger, .addr.uinteger = &rtcm2->xmitter.range},
155
287
        {NULL},
156
287
    };
157
158
287
    const struct json_attr_t json_rtcm14[] = {
159
287
        RTCM2_HEADER
160
287
        {"week",              t_uinteger,
161
287
                              .addr.uinteger = &rtcm2->gpstime.week},
162
287
        {"hour",              t_uinteger,
163
287
                              .addr.uinteger = &rtcm2->gpstime.hour},
164
287
        {"leapsecs",          t_uinteger,
165
287
                              .addr.uinteger = &rtcm2->gpstime.leapsecs},
166
287
        {NULL},
167
287
    };
168
169
287
    const struct json_attr_t json_rtcm16[] = {
170
287
        RTCM2_HEADER
171
287
        {"message",        t_string,  .addr.string = rtcm2->message,
172
287
                                         .len = sizeof(rtcm2->message)},
173
287
        {NULL},
174
287
    };
175
176
287
    const struct json_attr_t rtcm31_satellite[] = {
177
287
        {"ident",     t_uinteger,
178
287
                              STRUCTOBJECT(struct glonass_rangesat_t, ident)},
179
287
        {"udre",      t_uinteger,
180
287
                              STRUCTOBJECT(struct glonass_rangesat_t, udre)},
181
287
        {"change",    t_boolean,
182
287
                              STRUCTOBJECT(struct glonass_rangesat_t, change)},
183
287
        {"tod",       t_uinteger, STRUCTOBJECT(struct glonass_rangesat_t, tod)},
184
287
        {"prc",       t_real,     STRUCTOBJECT(struct glonass_rangesat_t, prc)},
185
287
        {"rrc",       t_real,     STRUCTOBJECT(struct glonass_rangesat_t, rrc)},
186
287
        {NULL},
187
287
    };
188
287
    const struct json_attr_t json_rtcm31[] = {
189
287
        RTCM2_HEADER
190
287
        {"satellites", t_array, STRUCTARRAY(rtcm2->glonass_ranges.sat,
191
287
                                            rtcm31_satellite, &satcount)},
192
287
        {NULL},
193
287
    };
194
195
287
    const struct json_attr_t json_rtcm2_fallback[] = {
196
287
        RTCM2_HEADER
197
287
        {"data",         t_array, .addr.array.element_type = t_string,
198
287
                         .addr.array.arr.strings.ptrs = stringptrs,
199
287
                         .addr.array.arr.strings.store = stringstore,
200
287
                         .addr.array.arr.strings.storelen = sizeof(stringstore),
201
287
                         .addr.array.count = &stringcount,
202
287
                         .addr.array.maxlen = NITEMS(stringptrs)},
203
287
        {NULL},
204
287
    };
205
206
287
#undef RTCM2_HEADER
207
/* *INDENT-ON* */
208
209
287
    memset(rtcm2, '\0', sizeof(struct rtcm2_t));
210
211
287
    if (strstr(buf, "\"type\":1,") != NULL
212
287
        || strstr(buf, "\"type\":9,") != NULL) {
213
7
        status = json_read_object(buf, json_rtcm1, endptr);
214
7
        if (status == 0)
215
1
            rtcm2->gps_ranges.nentries = (unsigned)satcount;
216
280
    } else if (strstr(buf, "\"type\":3,") != NULL) {
217
71
        status = json_read_object(buf, json_rtcm3, endptr);
218
71
        if (status == 0) {
219
67
            rtcm2->ref_sta.valid = (isfinite(rtcm2->ref_sta.x) != 0)
220
67
                && (isfinite(rtcm2->ref_sta.y) != 0)
221
67
                && (isfinite(rtcm2->ref_sta.z) != 0);
222
67
        }
223
209
    } else if (strstr(buf, "\"type\":4,") != NULL) {
224
88
        status = json_read_object(buf, json_rtcm4, endptr);
225
88
        if (status == 0)
226
3
            rtcm2->reference.valid = (isfinite(rtcm2->reference.dx) != 0)
227
3
                && (isfinite(rtcm2->reference.dy) != 0)
228
3
                && (isfinite(rtcm2->reference.dz) != 0);
229
121
    } else if (strstr(buf, "\"type\":5,") != NULL) {
230
5
        status = json_read_object(buf, json_rtcm5, endptr);
231
5
        if (status == 0)
232
1
            rtcm2->conhealth.nentries = (unsigned)satcount;
233
116
    } else if (strstr(buf, "\"type\":6,") != NULL) {
234
1
        status = json_read_object(buf, json_rtcm6, endptr);
235
115
    } else if (strstr(buf, "\"type\":7,") != NULL) {
236
5
        status = json_read_object(buf, json_rtcm7, endptr);
237
5
        if (status == 0)
238
1
            rtcm2->almanac.nentries = (unsigned)satcount;
239
110
    } else if (strstr(buf, "\"type\":13,") != NULL) {
240
1
        status = json_read_object(buf, json_rtcm13, endptr);
241
109
    } else if (strstr(buf, "\"type\":14,") != NULL) {
242
1
        status = json_read_object(buf, json_rtcm14, endptr);
243
108
    } else if (strstr(buf, "\"type\":16,") != NULL) {
244
2
        status = json_read_object(buf, json_rtcm16, endptr);
245
106
    } else if (strstr(buf, "\"type\":31,") != NULL) {
246
4
        status = json_read_object(buf, json_rtcm31, endptr);
247
4
        if (status == 0)
248
1
            rtcm2->glonass_ranges.nentries = (unsigned)satcount;
249
102
    } else {
250
102
        int n;
251
102
        status = json_read_object(buf, json_rtcm2_fallback, endptr);
252
1.67k
        for (n = 0; n < NITEMS(rtcm2->words); n++) {
253
1.62k
            if (n >= stringcount) {
254
1.40k
                rtcm2->words[n] = 0;
255
1.40k
            } else {
256
224
                unsigned int u;
257
224
                int fldcount = sscanf(stringptrs[n], "0x%08x\n", &u);
258
224
                if (fldcount != 1)
259
52
                    return JSON_ERR_MISC;
260
172
                else
261
172
                    rtcm2->words[n] = (isgps30bits_t) u;
262
224
            }
263
1.62k
        }
264
102
    }
265
235
    return status;
266
287
}
267
268
// vim: set expandtab shiftwidth=4