diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2009-08-18 15:30:49 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2009-08-18 15:30:49 +0000 |
commit | 6885b22471926d3387be8fc491ea016c78917c13 (patch) | |
tree | bb8e2ad915055b4789e99192c7fe7c22b4338a1d | |
parent | 22f2dbd5af5ecfcdcd3ce71cb2b6d714887d17cd (diff) | |
download | gpsd-6885b22471926d3387be8fc491ea016c78917c13.tar.gz |
More steps towards ?DEVICES decoding - refactor code.
-rw-r--r-- | gps_json.h | 2 | ||||
-rw-r--r-- | gpsd.c | 72 | ||||
-rw-r--r-- | gpsd.h-tail | 11 | ||||
-rw-r--r-- | gpsd_json.c | 37 | ||||
-rw-r--r-- | libgps_json.c | 5 |
5 files changed, 73 insertions, 54 deletions
@@ -4,6 +4,7 @@ #define GPS_JSON_COMMAND_MAX 80 #define GPS_JSON_RESPONSE_MAX 1024 +#define GPS_JSON_DEVICES_MAX 4 struct devconfig_t { char device[PATH_MAX]; @@ -15,6 +16,7 @@ struct devconfig_t { }; void json_tpv_dump(struct gps_data_t *, struct gps_fix_t *, char *, size_t); void json_sky_dump(struct gps_data_t *, char *, size_t); +void json_device_dump(struct gps_device_t *, char *, size_t); int json_watch_read(struct policy_t *, const char *, const char **); void json_watch_dump(struct policy_t *, char *, size_t); int json_configdev_read(struct devconfig_t *, const char *, const char **); @@ -110,11 +110,7 @@ * Manifest names for the gnss_type enum - must be kept synced with it. * Also, masks so we can tell what packet types correspond to each class. */ -struct classmap_t { - char *name; - int mask; -}; -static struct classmap_t classmap[] = { +struct classmap_t classmap[CLASSMAP_NITEMS] = { {"ANY", 0}, {"GPS", GPS_TYPEMASK}, {"RTCM2", PACKET_TYPEMASK(RTCM2_PACKET)}, @@ -1562,10 +1558,28 @@ static bool handle_oldstyle(struct subscriber_t *sub, char *buf, #endif /* OLDSTYLE_ENABLE */ #ifdef GPSDNG_ENABLE +static void json_devicelist_dump(char *reply, size_t replylen) +{ + struct gps_device_t *devp; + (void)strlcpy(reply, + "{\"class\"=\"DEVICES\",\"devices\":[", replylen); + for (devp = devices; devp < devices + MAXDEVICES; devp++) + if (allocated_device(devp) && strlen(reply)+strlen(devp->gpsdata.gps_device)+3 < replylen-1) { + json_device_dump(devp, + reply+strlen(reply), replylen-strlen(reply)); + (void)strlcat(reply, ",", replylen); + } + + if (reply[strlen(reply)-1] == ',') + reply[strlen(reply)-1] = '\0'; + (void)strlcat(reply, "]}", replylen); +} + static void handle_newstyle_request(struct subscriber_t *sub, const char *buf, const char **after, char *reply, size_t replylen) { + struct gps_device_t *devp; struct channel_t *channel; const char *end; @@ -1577,6 +1591,7 @@ static void handle_newstyle_request(struct subscriber_t *sub, * Still to be implemented: equivalents of Z $ */ if (strncmp(buf, "TPV;", 4) == 0) { + buf += 4; if ((channel=assign_channel(sub, GPS, NULL))!= NULL && have_fix(channel)) { json_tpv_dump(&channel->device->gpsdata, &channel->fixbuffer, reply, replylen); @@ -1584,61 +1599,18 @@ static void handle_newstyle_request(struct subscriber_t *sub, (void)strlcpy(reply, "{\"class\":\"TPV\"}", replylen); } - buf += 4; } else if (strncmp(buf, "SKY;", 4) == 0) { + buf += 4; if ((channel=assign_channel(sub, GPS, NULL))!= NULL && channel->device->gpsdata.satellites > 0) { json_sky_dump(&channel->device->gpsdata, reply, replylen); } else { (void)strlcpy(reply, "{\"class\":\"SKY\"}", replylen); } - buf += 4; } else if (strncmp(buf, "DEVICES;", 8) == 0) { - int i; - (void)strlcpy(reply, - "{\"class\"=\"DEVICES\",\"devices\":[", replylen); - for (i = 0; i < MAXDEVICES; i++) { - if (allocated_device(&devices[i]) && strlen(reply)+strlen(devices[i].gpsdata.gps_device)+3 < replylen-1) { - struct classmap_t *cmp; - (void)strlcat(reply, "{\"class\":\"DEVICE\",\"name\":\"", replylen); - (void)strlcat(reply, devices[i].gpsdata.gps_device, replylen); - (void)strlcat(reply, "\",", replylen); - if (devices[i].observed != 0) { - (void)strlcat(reply, "\"type\":[", replylen); - for (cmp = classmap; cmp < classmap+NITEMS(classmap); cmp++) - if ((devices[i].observed & cmp->mask) != 0) { - (void)strlcat(reply, "\"", replylen); - (void)strlcat(reply, cmp->name, replylen); - (void)strlcat(reply, "\",", replylen); - } - if (reply[strlen(reply)-1] == ',') - reply[strlen(reply)-1] = '\0'; - (void)strlcat(reply, "],", replylen); - } - if (devices[i].device_type != NULL) { - (void)strlcat(reply, "\"driver\":\"", replylen); - (void)strlcat(reply, - devices[i].device_type->type_name, - replylen); - (void)strlcat(reply, "\",", replylen); - } - if (devices[i].subtype[0] != '\0') { - (void)strlcat(reply, "\",\"subtype\":\"", replylen); - (void)strlcat(reply, - devices[i].subtype, - replylen); - } - if (reply[strlen(reply)-1] == ',') - reply[strlen(reply)-1] = '\0'; - (void)strlcat(reply, "},", replylen); - } - } - if (reply[strlen(reply)-1] == ',') - reply[strlen(reply)-1] = '\0'; - (void)strlcat(reply, "]}", replylen); buf += 8; + json_devicelist_dump(reply, replylen); } else if (strncmp(buf, "WATCH", 5) == 0 && (buf[5] == ';' || buf[5] == '=')) { - struct gps_device_t *devp; buf += 5; if (*buf == ';') { ++buf; diff --git a/gpsd.h-tail b/gpsd.h-tail index 8c39a8eb..ca36f45e 100644 --- a/gpsd.h-tail +++ b/gpsd.h-tail @@ -147,6 +147,14 @@ extern size_t oncore_payload_cksum_length(unsigned char id1,unsigned char id2); /* this is where we choose the confidence level to use in reports */ #define GPSD_CONFIDENCE CEP95_SIGMA +/* the map of device class names */ +struct classmap_t { + char *name; + int mask; +}; +#define CLASSMAP_NITEMS 5; +struct classmap_t classmap[CLASSMAP_NITEMS]; + #define NTPSHMSEGS 4 /* number of NTP SHM segments */ struct gps_context_t { @@ -487,9 +495,6 @@ extern int srec_hdr(unsigned int, unsigned char *, unsigned char *); extern int srec_fin(unsigned int, unsigned char *); extern unsigned char hc(unsigned char); -/* exported bits for the GPS flasher */ -bool sirf_write(int fd, unsigned char *msg); - /* application interface */ extern void gpsd_init(struct gps_device_t *, struct gps_context_t *, diff --git a/gpsd_json.c b/gpsd_json.c index aa99786d..8797366f 100644 --- a/gpsd_json.c +++ b/gpsd_json.c @@ -156,6 +156,43 @@ void json_sky_dump(struct gps_data_t *datap, char *reply, size_t replylen) 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\",\"name\":\"", replylen); + (void)strlcat(reply, device->gpsdata.gps_device, replylen); + (void)strlcat(reply, "\",", replylen); + if (device->observed != 0) { + (void)strlcat(reply, "\"type\":[", replylen); + for (cmp = classmap; cmp < classmap+NITEMS(classmap); cmp++) + if ((device->observed & cmp->mask) != 0) { + (void)strlcat(reply, "\"", replylen); + (void)strlcat(reply, cmp->name, replylen); + (void)strlcat(reply, "\",", replylen); + } + if (reply[strlen(reply)-1] == ',') + reply[strlen(reply)-1] = '\0'; + (void)strlcat(reply, "],", replylen); + } + 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); + } + 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) { diff --git a/libgps_json.c b/libgps_json.c index 39a82025..5732af56 100644 --- a/libgps_json.c +++ b/libgps_json.c @@ -141,15 +141,18 @@ static int json_sky_read(const char *buf, static int json_devices_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}, }; // FIXME: Can we abolish the hard limit on the number of devices? const struct json_attr_t json_attrs_devices[] = { {"devices", array, .addr.array.element_type = object, .addr.array.arr.subtype = json_attrs_subdevices, - .addr.array.maxlen = 4}, + .addr.array.maxlen = GPS_JSON_DEVICES_MAX}, {NULL}, }; int status; |