summaryrefslogtreecommitdiff
path: root/libgps_json.c
blob: f57e72db74a525ea0adb264fb9bbd9ac096c6854 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/****************************************************************************

NAME
   libgps_json.c - deserialize gpsd data coming from the server

DESCRIPTION
   This module uses the generic JSON parser to get data from JSON
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"

static int json_tpv_read(const char *buf, 
			 struct gps_data_t *gpsdata, const char **endptr)
{
    int status;
    /*@ -fullinitblock @*/
    const struct json_attr_t json_attrs_1[] = {
	{"class",  check,   .dflt.check = "TPV"},
	{"device", string,  .addr.string = gpsdata->dev.path,
			    .len = sizeof(gpsdata->dev.path)},
	{"tag",    string,  .addr.string = gpsdata->tag,
			    .len = sizeof(gpsdata->tag)},
	{"time",   real,    .addr.real = &gpsdata->fix.time,
			    .dflt.real = NAN},
	{"ept",    real,    .addr.real = &gpsdata->fix.ept,
			    .dflt.real = NAN},
	{"lon",    real,    .addr.real = &gpsdata->fix.longitude,
			    .dflt.real = NAN},
	{"lat",    real,    .addr.real = &gpsdata->fix.latitude,
			    .dflt.real = NAN},
	{"alt",    real,    .addr.real = &gpsdata->fix.altitude,
			    .dflt.real = NAN},
	{"epx",    real,    .addr.real = &gpsdata->fix.epx,
			    .dflt.real = NAN},
	{"epy",    real,    .addr.real = &gpsdata->fix.epy,
			    .dflt.real = NAN},
	{"epv",    real,    .addr.real = &gpsdata->fix.epv,
			    .dflt.real = NAN},
	{"track",   real,   .addr.real = &gpsdata->fix.track,
			    .dflt.real = NAN},
	{"speed",   real,   .addr.real = &gpsdata->fix.speed,
			    .dflt.real = NAN},
	{"climb",   real,   .addr.real = &gpsdata->fix.climb,
			    .dflt.real = NAN},
	{"epd",    real,    .addr.real = &gpsdata->fix.epd,
			    .dflt.real = NAN},
	{"eps",    real,    .addr.real = &gpsdata->fix.eps,
			    .dflt.real = NAN},
	{"epc",    real,    .addr.real = &gpsdata->fix.epc,
			    .dflt.real = NAN},
	{"mode",   integer, .addr.integer = &gpsdata->fix.mode,
			    .dflt.integer = MODE_NOT_SEEN},
	{NULL},
    };
    /*@ +fullinitblock @*/

    status = json_read_object(buf, json_attrs_1, endptr);

    if (status == 0) {
	gpsdata->status = STATUS_FIX;
	gpsdata->set = STATUS_SET;
	if (isnan(gpsdata->fix.time) == 0)
	    gpsdata->set |= TIME_SET;
	if (isnan(gpsdata->fix.ept) == 0)
	    gpsdata->set |= TIMERR_SET;
	if (isnan(gpsdata->fix.longitude) == 0)
	    gpsdata->set |= LATLON_SET;
	if (isnan(gpsdata->fix.altitude) == 0)
	    gpsdata->set |= ALTITUDE_SET;
	if (isnan(gpsdata->fix.epx)==0 && isnan(gpsdata->fix.epy)==0)
	    gpsdata->set |= HERR_SET;
	if (isnan(gpsdata->fix.epv)==0)
	    gpsdata->set |= VERR_SET;
	if (isnan(gpsdata->fix.track)==0)
	    gpsdata->set |= TRACK_SET;
	if (isnan(gpsdata->fix.speed)==0)
	    gpsdata->set |= SPEED_SET;
	if (isnan(gpsdata->fix.climb)==0)
	    gpsdata->set |= CLIMB_SET;
	if (isnan(gpsdata->fix.epd)==0)
	    gpsdata->set |= TRACKERR_SET;
	if (isnan(gpsdata->fix.eps)==0)
	    gpsdata->set |= SPEEDERR_SET;
	if (isnan(gpsdata->fix.epc)==0)
	    gpsdata->set |= CLIMBERR_SET;
	if (isnan(gpsdata->fix.epc)==0)
	    gpsdata->set |= CLIMBERR_SET;
	if (gpsdata->fix.mode != MODE_NOT_SEEN)
	    gpsdata->set |= MODE_SET;
    }
    return status;
}

static int json_sky_read(const char *buf, 
			 struct gps_data_t *gpsdata, const char **endptr)
{
    bool usedflags[MAXCHANNELS];
    /*@ -fullinitblock @*/
    const struct json_attr_t json_attrs_2_1[] = {
	{"PRN",	   integer, .addr.integer = gpsdata->PRN},
	{"el",	   integer, .addr.integer = gpsdata->elevation},
	{"az",	   integer, .addr.integer = gpsdata->azimuth},
	{"ss",	   real,    .addr.real = gpsdata->ss},
	{"used",   boolean, .addr.boolean = usedflags},
	{NULL},
    };
    const struct json_attr_t json_attrs_2[] = {
	{"class",      check,   .dflt.check = "SKY"},
	{"device",     string,  .addr.string  = gpsdata->dev.path,
				.len = PATH_MAX},
	{"tag",	       string,  .addr.string  = gpsdata->tag,
				.len = MAXTAGLEN},
	{"time",       real,    .addr.real    = &gpsdata->fix.time},
	{"reported",   integer, .addr.integer = &gpsdata->satellites_used},
	{"satellites", array,   .addr.array.element_type = object,
				.addr.array.arr.objects.subtype=json_attrs_2_1,
	                        .addr.array.maxlen = MAXCHANNELS,
	                        .addr.array.count = &gpsdata->satellites},
	{NULL},
    };
    /*@ +fullinitblock @*/
    int status, i, j;

    for (i = 0; i < MAXCHANNELS; i++)
	usedflags[i] = false;

    status = json_read_object(buf, json_attrs_2, endptr);
    if (status != 0)
	return status;

    for (i = j = 0; i < MAXCHANNELS; i++) {
	if (usedflags[i]) {
	    gpsdata->used[j++] = gpsdata->PRN[i];
	}
    }

    gpsdata->set |= SATELLITE_SET;
    return 0;
}

static int json_devicelist_read(const char *buf, 
			     struct gps_data_t *gpsdata, const char **endptr)
{
    /*@ -fullinitblock @*/
    const struct json_attr_t json_attrs_subdevices[] = {
	{"class",      check,      .dflt.check = "DEVICE"},
	{"path",       string,     STRUCTOBJECT(struct devconfig_t, path),
	                           .len = sizeof(gpsdata->devices.list[0].path)},
	{"activated",  real,       STRUCTOBJECT(struct devconfig_t, activated)},
	{"flags",      integer,	   STRUCTOBJECT(struct devconfig_t, flags)},
	{"driver",     string,     STRUCTOBJECT(struct devconfig_t, driver),
	                           .len = sizeof(gpsdata->devices.list[0].driver)},
	{"subtype",    string,     STRUCTOBJECT(struct devconfig_t, subtype),
	                           .len = sizeof(gpsdata->devices.list[0].subtype)},
	{"native",     integer,    STRUCTOBJECT(struct devconfig_t, driver_mode),
				   .dflt.integer = -1},
	{"bps",	       integer,    STRUCTOBJECT(struct devconfig_t, baudrate),
				   .dflt.integer = -1},
	{"parity",     character,  STRUCTOBJECT(struct devconfig_t, parity),
	                           .dflt.character = 'N'},
	{"stopbits",   integer,    STRUCTOBJECT(struct devconfig_t, stopbits),
				   .dflt.integer = -1},
	{"cycle",      real,       STRUCTOBJECT(struct devconfig_t, cycle),
				   .dflt.real = NAN},
	{"mincycle",   real,       STRUCTOBJECT(struct devconfig_t, mincycle),
				   .dflt.real = NAN},
	{NULL},
    };
    const struct json_attr_t json_attrs_devices[] = {
        {"class",   check,  .dflt.check = "DEVICES"},
        {"devices", array,  STRUCTARRAY(gpsdata->devices.list,
					json_attrs_subdevices,
					&gpsdata->devices.ndevices)},
	{NULL},
    };
    /*@ +fullinitblock @*/
    int status;

    memset(&gpsdata->devices, '\0', sizeof(gpsdata->devices));
    status = json_read_object(buf, json_attrs_devices, endptr);
    if (status != 0)
	return status;

    gpsdata->devices.time = timestamp();
    gpsdata->set |= DEVICELIST_SET;
    return 0;
}

static int json_version_read(const char *buf, 
			     struct gps_data_t *gpsdata, const char **endptr)
{
    /*@ -fullinitblock @*/
    const struct json_attr_t json_attrs_version[] = {
        {"class",     check,   .dflt.check = "VERSION"},
	{"release",   string,  .addr.string  = gpsdata->version.release,
	                       .len = sizeof(gpsdata->version.release)},
	{"rev",       string,  .addr.string  = gpsdata->version.rev,
	                       .len = sizeof(gpsdata->version.rev)},
	{"api_major", integer, .addr.integer = &gpsdata->version.api_major},
	{"api_minor", integer, .addr.integer = &gpsdata->version.api_minor},
	{NULL},
    };
    /*@ +fullinitblock @*/
    int status;

    memset(&gpsdata->version, '\0', sizeof(gpsdata->version));
    status = json_read_object(buf, json_attrs_version, endptr);
    if (status != 0)
	return status;

    gpsdata->set |= VERSION_SET;
    return 0;
}

static int json_error_read(const char *buf, 
			     struct gps_data_t *gpsdata, const char **endptr)
{
    /*@ -fullinitblock @*/
    const struct json_attr_t json_attrs_error[] = {
        {"class",     check,   .dflt.check = "ERROR"},
	{"message",   string,  .addr.string  = gpsdata->error,
	                       .len = sizeof(gpsdata->error)},
	{NULL},
    };
    /*@ +fullinitblock @*/
    int status;

    memset(&gpsdata->error, '\0', sizeof(gpsdata->error));
    status = json_read_object(buf, json_attrs_error, endptr);
    if (status != 0)
	return status;

    gpsdata->set |= ERR_SET;
    return 0;
}

int libgps_json_unpack(const char *buf, struct gps_data_t *gpsdata)
/* the only entry point - unpack a JSON object into gpsdata_t substructures */
{
    int status;
    if (strstr(buf, "\"class\":\"TPV\"") != 0) {
	return json_tpv_read(buf, gpsdata, NULL);
    } else if (strstr(buf, "\"class\":\"SKY\"") != 0) {
	return json_sky_read(buf, gpsdata, NULL);
    } else if (strstr(buf, "\"class\":\"DEVICES\"") != 0) {
	return json_devicelist_read(buf, gpsdata, NULL);
    } else if (strstr(buf, "\"class\":\"DEVICE\"") != 0) {
	status = json_device_read(buf, &gpsdata->devices.list[0], NULL);
	if (status == 0)
	    gpsdata->set |= DEVICE_SET;
	return status;
    } else if (strstr(buf, "\"class\":\"WATCH\"") != 0) {
	status = json_watch_read(buf, &gpsdata->policy, NULL);
	if (status == 0)
	    gpsdata->set |= POLICY_SET;
	return status;
    } else if (strstr(buf, "\"class\":\"VERSION\"") != 0) {
	return json_version_read(buf, gpsdata, NULL);
#ifdef RTCM104V2_ENABLE
    } else if (strstr(buf, "\"class\":\"RTCM2\"") != 0) {
	status = json_rtcm2_read(buf, 
				 gpsdata->dev.path, sizeof(gpsdata->dev.path), 
				 &gpsdata->rtcm2, NULL);
	if (status == 0)
	    gpsdata->set |= RTCM2_SET;
	return status;
#endif /* RTCM104V2_ENABLE */
#ifdef AIVDM_ENABLE
    } else if (strstr(buf, "\"class\":\"AIS\"") != 0) {
	status = json_ais_read(buf, 
				 gpsdata->dev.path, sizeof(gpsdata->dev.path), 
				 &gpsdata->ais, NULL);
	if (status == 0)
	    gpsdata->set |= AIS_SET;
	return status;
#endif /* AIVDM_ENABLE */
    } else if (strstr(buf, "\"class\":\"ERROR\"") != 0) {
	return json_error_read(buf, gpsdata, NULL);
    } else
	return -1;
}

/* libgps_json.c ends here */