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

NAME
   gpsd_json.c - move data between in-core and JSON structures

DESCRIPTION
   This module uses the generic JSON parser to get data from JSON
representations to gpsd core strctures, and vice_versa.

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

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

#include "gpsd_config.h"
#include "gpsd.h"
#include "gps_json.h"

void json_tpv_dump(struct gps_data_t *gpsdata, struct gps_fix_t *fixp, 
		   char *reply, size_t replylen)
{
    assert(replylen > 2);
    (void)strlcpy(reply, "{\"class\":\"TPV\",", replylen);
    (void)snprintf(reply+strlen(reply),
		   replylen-strlen(reply),
		   "\"tag\":\"%s\",",
		   gpsdata->tag[0]!='\0' ? gpsdata->tag : "-");
    (void)snprintf(reply+strlen(reply),
		   replylen-strlen(reply),
		   "\"device\":\"%s\",",
		   gpsdata->gps_device);
    if (isnan(fixp->time)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"time\":%.3f,",
		       fixp->time);
    if (isnan(fixp->ept)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"ept\":%.3f,",
		       fixp->ept);
    if (isnan(fixp->latitude)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"lat\":%.9f,",
		       fixp->latitude);
    if (isnan(fixp->longitude)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"lon\":%.9f,",
		       fixp->longitude);
    if (isnan(fixp->altitude)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"alt\":%.3f,",
		       fixp->altitude);
    if (isnan(fixp->eph)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"eph\":%.3f,",
		       fixp->eph);
    if (isnan(fixp->epv)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"epv\":%.3f,",
		       fixp->epv);
    if (isnan(fixp->track)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"track\":%.4f,",
		       fixp->track);
    if (isnan(fixp->speed)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"speed\":%.3f,",
		       fixp->speed);
    if (isnan(fixp->climb)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"climb\":%.3f,",
		       fixp->climb);
    if (isnan(fixp->epd)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"epd\":%.4f,",
		       fixp->epd);
    if (isnan(fixp->eps)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"eps\":%.2f,", fixp->eps);
    if (isnan(fixp->epc)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"epc\":%.2f,", fixp->epc);
    if (fixp->mode > 0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"mode\":%d,", fixp->mode);
    if (reply[strlen(reply)-1] == ',')
	reply[strlen(reply)-1] = '\0';	/* trim trailing comma */
    (void)strlcat(reply, "}", sizeof(reply)-strlen(reply));
}

void json_sky_dump(struct gps_data_t *datap, char *reply, size_t replylen)
{
    int i, j, used, reported = 0;
    assert(replylen > 2);
    (void)strlcpy(reply, "{\"class\":\"SKY\",", replylen);
    (void)snprintf(reply+strlen(reply),
		   replylen- strlen(reply),
		   "\"tag\":\"%s\",",
		   datap->tag[0]!='\0' ? datap->tag : "-");
    (void)snprintf(reply+strlen(reply),
		   replylen-strlen(reply),
		   "\"device\":\"%s\",",
		   datap->gps_device);
    if (isnan(datap->sentence_time)==0)
	(void)snprintf(reply+strlen(reply),
		       replylen-strlen(reply),
		       "\"time\":%.3f ",
		       datap->sentence_time);
    /* insurance against flaky drivers */
    for (i = 0; i < datap->satellites; i++)
	if (datap->PRN[i])
	    reported++;
    (void)snprintf(reply+strlen(reply),
		   replylen-strlen(reply),
		   "\"reported\":%d,", reported);
    if (reported) {
	(void)strlcat(reply, "\"satellites\":[", replylen);
	for (i = 0; i < reported; i++) {
	    used = 0;
	    for (j = 0; j < datap->satellites_used; j++)
		if (datap->used[j] == datap->PRN[i]) {
		    used = 1;
		    break;
		}
	    if (datap->PRN[i]) {
		(void)snprintf(reply+strlen(reply),
			       replylen-strlen(reply),
			       "{\"PRN\":%d,\"el\":%d,\"az\":%d,\"ss\":%.0f,\"used\":%s},",
			       datap->PRN[i],
			       datap->elevation[i],datap->azimuth[i],
			       datap->ss[i],
			       used ? "true" : "false");
	    }
	}
	reply[strlen(reply)-1] = '\0';	/* trim trailing comma */
	(void)strlcat(reply, "]", replylen-strlen(reply));
    }
    (void)strlcat(reply, "}", replylen-strlen(reply));
    if (datap->satellites != reported)
	gpsd_report(LOG_WARN,"Satellite count %d != PRN count %d\n",
		    datap->satellites, reported);
}

void json_device_dump(struct gps_device_t *device,
		     char *reply, size_t replylen)
{
    struct classmap_t *cmp;
    (void)strlcpy(reply, "{\"class\":\"DEVICE\",\"path\":\"", replylen);
    (void)strlcat(reply, device->gpsdata.gps_device, replylen);
    (void)strlcat(reply, "\",", replylen);
    (void)snprintf(reply+strlen(reply), replylen-strlen(reply),
		   "\"activated\":%2.2f,", device->gpsdata.online);
    if (device->observed != 0) {
	int mask = 0;
	for (cmp = classmap; cmp < classmap+NITEMS(classmap); cmp++)
	    if ((device->observed & cmp->packetmask) != 0) 
		mask |= cmp->typemask;
	if (mask != 0)
	    (void)snprintf(reply+strlen(reply), replylen-strlen(reply),
			   "\"flags\":%d,", mask);
    }
    if (device->device_type != NULL) {
	(void)strlcat(reply, "\"driver\":\"", replylen);
	(void)strlcat(reply, 
		      device->device_type->type_name,
		      replylen);
	(void)strlcat(reply, "\",", replylen);
    }
    if (device->subtype[0] != '\0') {
	(void)strlcat(reply, "\"subtype\":\"", replylen);
	(void)strlcat(reply, 
		      device->subtype,
		      replylen);
	(void)strlcat(reply, "\",", replylen);
    }
    if (reply[strlen(reply)-1] == ',')
	reply[strlen(reply)-1] = '\0';
    (void)strlcat(reply, "}", replylen);
}

int json_watch_read(struct policy_t *ccp,
			 const char *buf, const char **endptr)
{
    int intcasoc;
    struct json_attr_t chanconfig_attrs[] = {
	{"raw",	           integer,  .addr.integer = &ccp->raw,
				     .dflt.integer = -1},
	{"buffer_policy",  integer,  .addr.integer = &intcasoc,
				     .dflt.integer = -1},
	{"scaled",         boolean,  .addr.boolean = &ccp->scaled},
	{NULL},
    };
    int status;

    status = json_read_object(buf, chanconfig_attrs, endptr);
    if (status == 0) {
	if (intcasoc != -1)
	    ccp->buffer_policy = intcasoc;
    }
    return status;
}

void json_watch_dump(struct policy_t *ccp, char *reply, size_t replylen)
{
    (void)snprintf(reply+strlen(reply), replylen-strlen(reply),
		   "{\"class\":\"WATCH\",\"raw\":%d,\"buffer_policy\":%d,\"scaled\":%s}",
		   ccp->raw, 
		   ccp->buffer_policy,
		   ccp->scaled ? "true" : "false");
}

int json_configdev_read(struct devconfig_t *cdp, const char *buf, const char **endptr)
{
    struct json_attr_t devconfig_attrs[] = {
	{"device",	string,   .addr.string.ptr=cdp->device,
				  .addr.string.len=sizeof(cdp->device)},
	{"native",	integer,  .addr.integer = &cdp->native,
				  .dflt.integer = -1},
	{"bps",		integer,  .addr.integer = &cdp->bps,
				  .dflt.integer = -1},
	{"serialmode",	string,	  .addr.string.ptr=cdp->serialmode,
				  .addr.string.len=sizeof(cdp->serialmode)},
	{"cycle",	real,     .addr.real = &cdp->cycle,
				  .dflt.real = NAN},
	{"mincycle",	real,     .addr.real = &cdp->mincycle,
				  .dflt.real = NAN},
	{NULL},
    };

    return json_read_object(buf, devconfig_attrs, endptr);
}

void json_configdev_dump(struct gps_device_t *devp, char *reply, size_t replylen)
{
    (void)snprintf(reply, replylen,
		   "{\"class\":\"CONFIGDEV\",\"device\":\"%s\",\"native\":%d,\"bps\":%d,\"serialmode\":\"%u%c%u\",\"cycle\":%2.2f",
		   devp->gpsdata.gps_device,
		   devp->gpsdata.driver_mode,
		   (int)gpsd_get_speed(&devp->ttyset),
		   9 - devp->gpsdata.stopbits,
		   (int)devp->gpsdata.parity,
		   devp->gpsdata.stopbits,
		   devp->gpsdata.cycle);
    if (devp->device_type->rate_switcher != NULL)
	(void)snprintf(reply+strlen(reply), replylen-strlen(reply),
		       ",\"mincycle\":%2.2f",
		       devp->device_type->min_cycle);
    (void)strlcat(reply, "}", replylen);
}

/* gpsd_json.c ends here */