summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-08-19 03:14:12 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-08-19 03:14:12 +0000
commit6098283d8394544b97c70f473a9743d0c4527d2f (patch)
tree2b4415bf7a7749087fe8b6777cd08a1ff9adb59d
parent747088bbe5dc34c0902c0bddc492bc4815f4a7c3 (diff)
downloadgpsd-6098283d8394544b97c70f473a9743d0c4527d2f.tar.gz
Library parse code, and unit test, for DEVICE responses.
-rw-r--r--gps.h7
-rw-r--r--libgps_json.c28
-rw-r--r--test_json.c24
3 files changed, 46 insertions, 13 deletions
diff --git a/gps.h b/gps.h
index 4c3a7b13..9453cb83 100644
--- a/gps.h
+++ b/gps.h
@@ -774,9 +774,16 @@ struct ais_t
#define MAXDEVICES_PER_USER 4
#define GPS_PATH_MAX 64 /* dev files usually have short names */
+#define TYPES_PER_DEVICE 4
struct device_t {
char path[GPS_PATH_MAX];
+ int ndatatypes;
+ int datatypes[TYPES_PER_DEVICE];
+#define DEV_GPS 1
+#define DEV_RTCM2 2
+#define DEV_RTCM3 3
+#define DEV_AIS 4
char driver[64];
char subtype[64];
double activated;
diff --git a/libgps_json.c b/libgps_json.c
index de620ca0..a48a6851 100644
--- a/libgps_json.c
+++ b/libgps_json.c
@@ -143,16 +143,26 @@ static int json_sky_read(const char *buf,
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 (list)
- {"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)},
+ {"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 = enumerated,
+ .addr.array.arr.enumerated.map = datatype_map,
+ .addr.array.arr.enumerated.store = dev->datatypes,
+ .addr.array.maxlen = sizeof(dev->datatypes)/sizeof(dev->datatypes[0]),
+ .addr.array.count = &dev->ndatatypes},
+ {"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;
diff --git a/test_json.c b/test_json.c
index 728c1afe..f200a179 100644
--- a/test_json.c
+++ b/test_json.c
@@ -132,6 +132,14 @@ const struct json_array_t json_array_5 = {
.count = &enumcount,
};
+/* Case 6: test DEVICE parsing */
+
+const char *json_str6 = "{\"class\":\"DEVICE\",\
+ \"path\":\"/dev/ttyUSB0\",\
+ \"type\":[\"GPS\",\"AIS\"],\
+ \"driver\":\"Foonly\",\"subtype\":\"Foonly Frob\"\
+ }";
+
int main(int argc UNUSED, char *argv[] UNUSED)
{
int status;
@@ -178,10 +186,18 @@ int main(int argc UNUSED, char *argv[] UNUSED)
status = json_read_array(json_str5, &json_array_5, NULL);
ASSERT_CASE(5, status);
- assert(enumcount == 3);
- assert(enumstore[0] == 3);
- assert(enumstore[1] == 6);
- assert(enumstore[2] == 14);
+ ASSERT_INTEGER("enumcount", enumcount, 3);
+ ASSERT_INTEGER("enumstore[0]", enumstore[0], 3);
+ ASSERT_INTEGER("enumstore[1]", enumstore[1], 6);
+ ASSERT_INTEGER("enumstore[2]", enumstore[2], 14);
+
+ status = libgps_json_unpack(json_str6, &gpsdata);
+ ASSERT_CASE(6, status);
+ ASSERT_STRING("path", gpsdata.devices.list[0].path, "/dev/ttyUSB0");
+ ASSERT_INTEGER("ndatatypes", gpsdata.devices.list[0].ndatatypes, 2);
+ ASSERT_INTEGER("datatypes[0]",gpsdata.devices.list[0].datatypes[0],DEV_GPS);
+ ASSERT_INTEGER("datatypes[1]",gpsdata.devices.list[0].datatypes[1],DEV_AIS);
+ ASSERT_STRING("driver", gpsdata.devices.list[0].driver, "Foonly");
(void)fprintf(stderr, "succeeded.\n");