summaryrefslogtreecommitdiff
path: root/libgps_json.c
blob: c15750f3121648b2ecee29c81f43d992b1e03379 (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
/****************************************************************************

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 <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;
    const struct json_attr_t json_attrs_1[] = {
	{"class",  check,   .dflt.check = "TPV"},
	{"device", string,  .addr.string.ptr = gpsdata->gps_device,
			    .addr.string.len = sizeof(gpsdata->gps_device)},
	{"tag",    string,  .addr.string.ptr = gpsdata->tag,
			    .addr.string.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},
	{"eph",    real,    .addr.real = &gpsdata->fix.eph,
			    .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},
    };

    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.eph)==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];
    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.ptr  = gpsdata->gps_device,
				.addr.string.len = PATH_MAX},
	{"tag",	       string,  .addr.string.ptr  = gpsdata->tag,
				.addr.string.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.subtype = json_attrs_2_1,
				.addr.array.maxlen = MAXCHANNELS},
	{NULL},
    };
    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_device_read(const char *buf, 
			     struct device_t *dev, const char **endptr)
{
    const struct json_enum_t datatype_map[] = {
	{"GPS", 	DEV_GPS},
	{"RTCM2",	DEV_RTCM2},
	{"RTCM3",	DEV_RTCM3},
	{"AIS", 	DEV_AIS},
    };
    const struct json_attr_t json_attrs_device[] = {
	{"class",      check,      .dflt.check = "DEVICE"},
	{"path",       string,     .addr.string.ptr  = dev->path,
	                           .addr.string.len = sizeof(dev->path)},
	{"activated",  real,       .addr.real    = &dev->activated},
	{"type",       array,  	   .addr.array.element_type = flags,
	                           .addr.array.arr.flags.map = datatype_map,
                                   .addr.array.arr.flags.bits=&dev->datatypes},
	{"driver",     string,     .addr.string.ptr  = dev->driver,
	                           .addr.string.len = sizeof(dev->driver)},
	{"subtype",    string,     .addr.string.ptr  = dev->subtype,
	                           .addr.string.len = sizeof(dev->subtype)},
	{NULL},
    };
    int status;

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

    return 0;
}

#ifdef __UNUSED_
static int json_devicelist_read(const char *buf, 
			     struct gps_data_t *gpsdata, const char **endptr)
{
    char names[GPS_JSON_DEVICES_MAX][PATH_MAX];
    const struct json_attr_t json_attrs_subdevices[] = {
	// FIXME: Parse device records, too.
	{"name",   string,  .addr.string.ptr = gpsdata->gps_device,
			    .addr.string.len = sizeof(gpsdata->gps_device)},
	{NULL},
    };
    const struct json_attr_t json_attrs_devices[] = {
        {"class",      check,   .dflt.check = "DEVICES"},
	{"devices",    array,   .addr.array.element_type = object,
				.addr.array.arr.subtype = json_attrs_subdevices,
				.addr.array.maxlen = MAXDEVICES_PER_USER},
	{NULL},
    };
    int status;

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

    gpsdata->devices.time = timestamp();
    gpsdata->set |= DEVICELIST_SET;
    gpsdata->devices.ndevices = *json_attrs_devices[0].addr.array.count;
    return 0;
}
#endif

int libgps_json_unpack(const char *buf, struct gps_data_t *gpsdata)
/* the only entry point - unpack a JSON object into C structs */
{
    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\":\"DEVICE\"") != 0) {
	int status = json_device_read(buf, &gpsdata->devices.list[0], NULL);
	if (status == 0)
	    gpsdata->set |= DEVICE_SET;
	return status;
    } else
	return -1;
}

/* libgps_json.c ends here */