Coverage Report

Created: 2025-07-11 06:47

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