summaryrefslogtreecommitdiff
path: root/rtcm2_json.c
blob: 878fc024e5c90765d79c9196384fa7e899778656 (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
/****************************************************************************

NAME
   rtcm2_json.c - deserialize RTCM2 JSON

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

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

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

#include "gpsd_config.h"
#include "gpsd.h"
#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;

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

    int status, satcount;

    const struct json_attr_t rtcm1_satellite[] = {
	{"ident",     uinteger, STRUCTOBJECT(struct rangesat_t, ident)},
	{"udre",      uinteger, STRUCTOBJECT(struct rangesat_t, udre)},
	{"issuedata", uinteger, STRUCTOBJECT(struct rangesat_t, issuedata)},
	{"rangerr",   real,     STRUCTOBJECT(struct rangesat_t, rangerr)},
	{"rangerate", real,     STRUCTOBJECT(struct rangesat_t, rangerate)},
	{NULL},
    };
    const struct json_attr_t json_rtcm1[] = {
	RTCM2_HEADER
        {"satellites", array,	STRUCTARRAY(rtcm2->ranges.sat, 
					    rtcm1_satellite, &satcount)},
	{NULL},
    };

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

    /*
     * Beware! Needs to stay synchronized with a corresponding
     * nam,e 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", 2}, {NULL}
    };
    const struct json_attr_t json_rtcm4[] = {
	RTCM2_HEADER
        {"valid",          boolean, .addr.boolean = &rtcm2->reference.valid},
	{"system",         integer, .addr.integer = &rtcm2->reference.system,
	                            .map=system_table},
	{"sense",          integer, .addr.integer = &rtcm2->reference.sense},
	{"datum",          string,  .addr.string = rtcm2->reference.datum,
	                            .len = sizeof(rtcm2->reference.datum)},
	{"dx",             real,    .addr.real = &rtcm2->reference.dx,
			            .dflt.real = NAN},
	{"dy",             real,    .addr.real = &rtcm2->reference.dy,
			            .dflt.real = NAN},
	{"dz",             real,    .addr.real = &rtcm2->reference.dz,
			            .dflt.real = NAN},
	{NULL},
    };

    const struct json_attr_t rtcm5_satellite[] = {
	{"ident",       uinteger, STRUCTOBJECT(struct consat_t, ident)},
	{"iodl",        boolean,  STRUCTOBJECT(struct consat_t, iodl)},
	{"health",      uinteger, STRUCTOBJECT(struct consat_t, health)},
	{"snr",         integer,  STRUCTOBJECT(struct consat_t, snr)},
	{"health_en",   boolean,  STRUCTOBJECT(struct consat_t, health_en)},
	{"new_data",    boolean,  STRUCTOBJECT(struct consat_t, new_data)},
	{"los_warning", boolean,  STRUCTOBJECT(struct consat_t, los_warning)},
	{"tou",         uinteger, STRUCTOBJECT(struct consat_t, tou)},
	{NULL},
    };
    const struct json_attr_t json_rtcm5[] = {
	RTCM2_HEADER
        {"satellites", 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[] = {
	{"latitude",    real,     STRUCTOBJECT(struct station_t, latitude)},
	{"longitude",   real,     STRUCTOBJECT(struct station_t, longitude)},
	{"range",       uinteger, STRUCTOBJECT(struct station_t, range)},
	{"frequency",   real,     STRUCTOBJECT(struct station_t, frequency)},
	{"health",      uinteger, STRUCTOBJECT(struct station_t, health)},
	{"station_id",  uinteger, STRUCTOBJECT(struct station_t, station_id)},
	{"bitrate",     uinteger, STRUCTOBJECT(struct station_t, bitrate)},
	{NULL},
    };
    const struct json_attr_t json_rtcm7[] = {
	RTCM2_HEADER
        {"satellites", array,	STRUCTARRAY(rtcm2->almanac.station, 
					    rtcm7_satellite, &satcount)},
	{NULL},
    };

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

    const struct json_attr_t json_rtcm2_fallback[] = {
	RTCM2_HEADER
	{"data",         array, .addr.array.element_type = 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 STRUCTARRAY
#undef STRUCTOBJECT
#undef RTCM2_HEADER

    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->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 = (isnan(rtcm2->ecef.x)==0)&&(isnan(rtcm2->ecef.y)==0)&&(isnan(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 = (isnan(rtcm2->reference.dx)==0)&&(isnan(rtcm2->reference.dy)==0)&&(isnan(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\":16,") != NULL) {
	status = json_read_object(buf, json_rtcm16, endptr);
    } 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;
}

/* rtcm2_json.c ends here */