Coverage Report

Created: 2024-02-25 06:36

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