Coverage Report

Created: 2025-11-11 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpsd/gpsd-3.26.2~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
279
{
33
34
279
    static char *stringptrs[NITEMS(rtcm2->words)];
35
279
    static char stringstore[sizeof(rtcm2->words) * 2];
36
279
    static int stringcount;
37
38
// *INDENT-OFF*
39
279
#define RTCM2_HEADER \
40
3.06k
        {"class",          t_check,    .dflt.check = "RTCM2"}, \
41
3.06k
        {"type",           t_uinteger, .addr.uinteger = &rtcm2->type}, \
42
3.06k
        {"device",         t_string,   .addr.string = path, \
43
3.06k
                                          .len = pathlen}, \
44
3.06k
        {"station_id",     t_uinteger, .addr.uinteger = &rtcm2->refstaid}, \
45
3.06k
        {"zcount",         t_real,     .addr.real = &rtcm2->zcount, \
46
3.06k
                                          .dflt.real = NAN}, \
47
3.06k
        {"seqnum",         t_uinteger, .addr.uinteger = &rtcm2->seqnum}, \
48
3.06k
        {"length",         t_uinteger, .addr.uinteger = &rtcm2->length}, \
49
3.06k
        {"station_health", t_uinteger, .addr.uinteger = &rtcm2->stathlth},
50
51
279
    int status = 0, satcount = 0;
52
53
279
    const struct json_attr_t rtcm1_satellite[] = {
54
279
        {"ident",     t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, ident)},
55
279
        {"udre",      t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, udre)},
56
279
        {"iod",       t_uinteger, STRUCTOBJECT(struct gps_rangesat_t, iod)},
57
279
        {"prc",       t_real,     STRUCTOBJECT(struct gps_rangesat_t, prc)},
58
279
        {"rrc",       t_real,     STRUCTOBJECT(struct gps_rangesat_t, rrc)},
59
279
        {NULL},
60
279
    };
61
279
    const struct json_attr_t json_rtcm1[] = {
62
279
        RTCM2_HEADER
63
279
        {"satellites", t_array, STRUCTARRAY(rtcm2->gps_ranges.sat,
64
279
                                            rtcm1_satellite, &satcount)},
65
279
        {NULL},
66
279
    };
67
68
279
    const struct json_attr_t json_rtcm3[] = {
69
279
        RTCM2_HEADER
70
279
        {"x",              t_real,    .addr.real = &rtcm2->ref_sta.x,
71
279
                                         .dflt.real = NAN},
72
279
        {"y",              t_real,    .addr.real = &rtcm2->ref_sta.y,
73
279
                                         .dflt.real = NAN},
74
279
        {"z",              t_real,    .addr.real = &rtcm2->ref_sta.z,
75
279
                                         .dflt.real = NAN},
76
279
        {NULL},
77
279
    };
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
279
    const struct json_enum_t system_table[] = {
86
279
        {"GPS", 0}, {"GLONASS", 1}, {"GALILEO", 2}, {"UNKNOWN", 3}, {NULL}
87
279
    };
88
279
    const struct json_attr_t json_rtcm4[] = {
89
279
        RTCM2_HEADER
90
279
        {"valid",          t_boolean, .addr.boolean = &rtcm2->reference.valid},
91
279
        {"system",         t_integer, .addr.integer = &rtcm2->reference.system,
92
279
                                         .map=system_table},
93
279
        {"sense",          t_integer, .addr.integer = &rtcm2->reference.sense},
94
279
        {"datum",          t_string,  .addr.string = rtcm2->reference.datum,
95
279
                                         .len = sizeof(rtcm2->reference.datum)},
96
279
        {"dx",             t_real,    .addr.real = &rtcm2->reference.dx,
97
279
                                         .dflt.real = NAN},
98
279
        {"dy",             t_real,    .addr.real = &rtcm2->reference.dy,
99
279
                                         .dflt.real = NAN},
100
279
        {"dz",             t_real,    .addr.real = &rtcm2->reference.dz,
101
279
                                         .dflt.real = NAN},
102
279
        {NULL},
103
279
    };
104
105
279
    const struct json_attr_t rtcm5_satellite[] = {
106
279
        {"ident",       t_uinteger, STRUCTOBJECT(struct consat_t, ident)},
107
279
        {"iodl",        t_boolean,  STRUCTOBJECT(struct consat_t, iodl)},
108
279
        {"health",      t_uinteger, STRUCTOBJECT(struct consat_t, health)},
109
279
        {"snr",         t_integer,  STRUCTOBJECT(struct consat_t, snr)},
110
279
        {"health_en",   t_boolean,  STRUCTOBJECT(struct consat_t, health_en)},
111
279
        {"new_data",    t_boolean,  STRUCTOBJECT(struct consat_t, new_data)},
112
279
        {"los_warning", t_boolean,  STRUCTOBJECT(struct consat_t, los_warning)},
113
279
        {"tou",         t_uinteger, STRUCTOBJECT(struct consat_t, tou)},
114
279
        {NULL},
115
279
    };
116
279
    const struct json_attr_t json_rtcm5[] = {
117
279
        RTCM2_HEADER
118
279
        {"satellites", t_array, STRUCTARRAY(rtcm2->conhealth.sat,
119
279
                                            rtcm5_satellite, &satcount)},
120
279
        {NULL},
121
279
    };
122
123
279
    const struct json_attr_t json_rtcm6[] = {
124
279
        RTCM2_HEADER
125
        // No-op or keepalive message
126
279
        {NULL},
127
279
    };
128
129
279
    const struct json_attr_t rtcm7_satellite[] = {
130
279
        {"lat",         t_real,     STRUCTOBJECT(struct station_t, latitude)},
131
279
        {"lon",         t_real,     STRUCTOBJECT(struct station_t, longitude)},
132
279
        {"range",       t_uinteger, STRUCTOBJECT(struct station_t, range)},
133
279
        {"frequency",   t_real,     STRUCTOBJECT(struct station_t, frequency)},
134
279
        {"health",      t_uinteger, STRUCTOBJECT(struct station_t, health)},
135
279
        {"station_id",  t_uinteger, STRUCTOBJECT(struct station_t, station_id)},
136
279
        {"bitrate",     t_uinteger, STRUCTOBJECT(struct station_t, bitrate)},
137
279
        {NULL},
138
279
    };
139
279
    const struct json_attr_t json_rtcm7[] = {
140
279
        RTCM2_HEADER
141
279
        {"satellites", t_array, STRUCTARRAY(rtcm2->almanac.station,
142
279
                                            rtcm7_satellite, &satcount)},
143
279
        {NULL},
144
279
    };
145
146
279
    const struct json_attr_t json_rtcm13[] = {
147
279
        RTCM2_HEADER
148
279
        {"status",       t_boolean,  .addr.boolean = &rtcm2->xmitter.status},
149
279
        {"rangeflag",    t_boolean,  .addr.boolean = &rtcm2->xmitter.rangeflag},
150
279
        {"lat",          t_real,     .addr.real = &rtcm2->xmitter.lat,
151
279
                                        .dflt.real = NAN},
152
279
        {"lon",          t_real,     .addr.real = &rtcm2->xmitter.lon,
153
279
                                        .dflt.real = NAN},
154
279
        {"range",        t_uinteger, .addr.uinteger = &rtcm2->xmitter.range},
155
279
        {NULL},
156
279
    };
157
158
279
    const struct json_attr_t json_rtcm14[] = {
159
279
        RTCM2_HEADER
160
279
        {"week",              t_uinteger,
161
279
                              .addr.uinteger = &rtcm2->gpstime.week},
162
279
        {"hour",              t_uinteger,
163
279
                              .addr.uinteger = &rtcm2->gpstime.hour},
164
279
        {"leapsecs",          t_uinteger,
165
279
                              .addr.uinteger = &rtcm2->gpstime.leapsecs},
166
279
        {NULL},
167
279
    };
168
169
279
    const struct json_attr_t json_rtcm16[] = {
170
279
        RTCM2_HEADER
171
279
        {"message",        t_string,  .addr.string = rtcm2->message,
172
279
                                         .len = sizeof(rtcm2->message)},
173
279
        {NULL},
174
279
    };
175
176
279
    const struct json_attr_t rtcm31_satellite[] = {
177
279
        {"ident",     t_uinteger,
178
279
                              STRUCTOBJECT(struct glonass_rangesat_t, ident)},
179
279
        {"udre",      t_uinteger,
180
279
                              STRUCTOBJECT(struct glonass_rangesat_t, udre)},
181
279
        {"change",    t_boolean,
182
279
                              STRUCTOBJECT(struct glonass_rangesat_t, change)},
183
279
        {"tod",       t_uinteger, STRUCTOBJECT(struct glonass_rangesat_t, tod)},
184
279
        {"prc",       t_real,     STRUCTOBJECT(struct glonass_rangesat_t, prc)},
185
279
        {"rrc",       t_real,     STRUCTOBJECT(struct glonass_rangesat_t, rrc)},
186
279
        {NULL},
187
279
    };
188
279
    const struct json_attr_t json_rtcm31[] = {
189
279
        RTCM2_HEADER
190
279
        {"satellites", t_array, STRUCTARRAY(rtcm2->glonass_ranges.sat,
191
279
                                            rtcm31_satellite, &satcount)},
192
279
        {NULL},
193
279
    };
194
195
279
    const struct json_attr_t json_rtcm2_fallback[] = {
196
279
        RTCM2_HEADER
197
279
        {"data",         t_array, .addr.array.element_type = t_string,
198
279
                         .addr.array.arr.strings.ptrs = stringptrs,
199
279
                         .addr.array.arr.strings.store = stringstore,
200
279
                         .addr.array.arr.strings.storelen = sizeof(stringstore),
201
279
                         .addr.array.count = &stringcount,
202
279
                         .addr.array.maxlen = NITEMS(stringptrs)},
203
279
        {NULL},
204
279
    };
205
206
279
#undef RTCM2_HEADER
207
/* *INDENT-ON* */
208
209
279
    memset(rtcm2, '\0', sizeof(struct rtcm2_t));
210
211
279
    if (strstr(buf, "\"type\":1,") != NULL
212
276
        || strstr(buf, "\"type\":9,") != NULL) {
213
5
        status = json_read_object(buf, json_rtcm1, endptr);
214
5
        if (status == 0)
215
1
            rtcm2->gps_ranges.nentries = (unsigned)satcount;
216
274
    } else if (strstr(buf, "\"type\":3,") != NULL) {
217
66
        status = json_read_object(buf, json_rtcm3, endptr);
218
66
        if (status == 0) {
219
61
            rtcm2->ref_sta.valid = (isfinite(rtcm2->ref_sta.x) != 0)
220
33
                && (isfinite(rtcm2->ref_sta.y) != 0)
221
19
                && (isfinite(rtcm2->ref_sta.z) != 0);
222
61
        }
223
208
    } else if (strstr(buf, "\"type\":4,") != NULL) {
224
87
        status = json_read_object(buf, json_rtcm4, endptr);
225
87
        if (status == 0)
226
4
            rtcm2->reference.valid = (isfinite(rtcm2->reference.dx) != 0)
227
2
                && (isfinite(rtcm2->reference.dy) != 0)
228
1
                && (isfinite(rtcm2->reference.dz) != 0);
229
121
    } else if (strstr(buf, "\"type\":5,") != NULL) {
230
4
        status = json_read_object(buf, json_rtcm5, endptr);
231
4
        if (status == 0)
232
1
            rtcm2->conhealth.nentries = (unsigned)satcount;
233
117
    } else if (strstr(buf, "\"type\":6,") != NULL) {
234
1
        status = json_read_object(buf, json_rtcm6, endptr);
235
116
    } 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
111
    } else if (strstr(buf, "\"type\":13,") != NULL) {
240
1
        status = json_read_object(buf, json_rtcm13, endptr);
241
110
    } else if (strstr(buf, "\"type\":14,") != NULL) {
242
1
        status = json_read_object(buf, json_rtcm14, endptr);
243
109
    } else if (strstr(buf, "\"type\":16,") != NULL) {
244
2
        status = json_read_object(buf, json_rtcm16, endptr);
245
107
    } 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
103
    } else {
250
103
        int n;
251
103
        status = json_read_object(buf, json_rtcm2_fallback, endptr);
252
1.42k
        for (n = 0; n < NITEMS(rtcm2->words); n++) {
253
1.38k
            if (n >= stringcount) {
254
1.14k
                rtcm2->words[n] = 0;
255
1.14k
            } else {
256
240
                unsigned int u;
257
240
                int fldcount = sscanf(stringptrs[n], "0x%08x\n", &u);
258
240
                if (fldcount != 1)
259
61
                    return JSON_ERR_MISC;
260
179
                else
261
179
                    rtcm2->words[n] = (isgps30bits_t) u;
262
240
            }
263
1.38k
        }
264
103
    }
265
218
    return status;
266
279
}
267
268
// vim: set expandtab shiftwidth=4