summaryrefslogtreecommitdiff
path: root/test_libgps.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2011-04-03 10:43:19 -0400
committerEric S. Raymond <esr@thyrsus.com>2011-04-03 10:43:19 -0400
commit7e19a51ed9675746aa2a924dd2007d30f827068f (patch)
treeb36143380f5559d9b1a32ea922cabdf76a91d657 /test_libgps.c
parent959eb915cb1f061a461f6227afc8954b067589df (diff)
downloadgpsd-7e19a51ed9675746aa2a924dd2007d30f827068f.tar.gz
Break the libgps test code our of libgps_core.c.
Diffstat (limited to 'test_libgps.c')
-rw-r--r--test_libgps.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/test_libgps.c b/test_libgps.c
new file mode 100644
index 00000000..43c72647
--- /dev/null
+++ b/test_libgps.c
@@ -0,0 +1,108 @@
+/*
+ * A simple command-line exerciser for the library.
+ * Not really useful for anything but debugging.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <stdarg.h>
+#include "gpsd.h"
+
+#ifndef S_SPLINT_S
+#include <unistd.h>
+#endif /* S_SPLINT_S */
+#include <getopt.h>
+#include <signal.h>
+
+static void onsig(int sig)
+{
+ (void)fprintf(stderr, "libgps: died with signal %d\n", sig);
+ exit(1);
+}
+
+/* must start zeroed, otherwise the unit test will try to chase garbage pointer fields. */
+static struct gps_data_t gpsdata;
+
+int main(int argc, char *argv[])
+{
+ struct gps_data_t collect;
+ char buf[BUFSIZ];
+ int option;
+ bool batchmode = false;
+ int debug = 0;
+
+ (void)signal(SIGSEGV, onsig);
+ (void)signal(SIGBUS, onsig);
+
+ while ((option = getopt(argc, argv, "bhsD:?")) != -1) {
+ switch (option) {
+ case 'b':
+ batchmode = true;
+ break;
+ case 's':
+ (void)
+ printf
+ ("Sizes: fix=%zd gpsdata=%zd rtcm2=%zd rtcm3=%zd ais=%zd compass=%zd raw=%zd devices=%zd policy=%zd version=%zd, noise=%zd\n",
+ sizeof(struct gps_fix_t),
+ sizeof(struct gps_data_t), sizeof(struct rtcm2_t),
+ sizeof(struct rtcm3_t), sizeof(struct ais_t),
+ sizeof(struct attitude_t), sizeof(struct rawdata_t),
+ sizeof(collect.devices), sizeof(struct policy_t),
+ sizeof(struct version_t), sizeof(struct gst_t));
+ exit(0);
+ case 'D':
+ debug = atoi(optarg);
+ break;
+ case '?':
+ case 'h':
+ default:
+ (void)fputs("usage: libgps [-b] [-d lvl] [-s]\n", stderr);
+ exit(1);
+ }
+ }
+
+ gps_enable_debug(debug, stdout);
+ if (batchmode) {
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ if (buf[0] == '{' || isalpha(buf[0])) {
+ gps_unpack(buf, &gpsdata);
+ libgps_dump_state(&gpsdata);
+ }
+ }
+ } else if (gps_open(NULL, 0, &collect) <= 0) {
+ (void)fputs("Daemon is not running.\n", stdout);
+ exit(1);
+ } else if (optind < argc) {
+ (void)strlcpy(buf, argv[optind], BUFSIZ);
+ (void)strlcat(buf, "\n", BUFSIZ);
+ (void)gps_send(&collect, buf);
+ (void)gps_read(&collect);
+ libgps_dump_state(&collect);
+ (void)gps_close(&collect);
+ } else {
+ int tty = isatty(0);
+
+ if (tty)
+ (void)fputs("This is the gpsd exerciser.\n", stdout);
+ for (;;) {
+ if (tty)
+ (void)fputs("> ", stdout);
+ if (fgets(buf, sizeof(buf), stdin) == NULL) {
+ if (tty)
+ putchar('\n');
+ break;
+ }
+ collect.set = 0;
+ (void)gps_send(&collect, buf);
+ (void)gps_read(&collect);
+ libgps_dump_state(&collect);
+ }
+ (void)gps_close(&collect);
+ }
+
+ return 0;
+}
+
+/*@-nullderef@*/