X-Git-Url: http://git.openwrt.org/?p=project%2Fugps.git;a=blobdiff_plain;f=main.c;h=7696cbc9c26282e1a55f68e2abdeebbb6517f862;hp=841a2b6034cea082eb6ecea0672f246925389304;hb=HEAD;hpb=1c31c99edd9de9dcb403750b04041eccc751ac5e diff --git a/main.c b/main.c index 841a2b6..acd6a42 100644 --- a/main.c +++ b/main.c @@ -27,10 +27,13 @@ #include "log.h" #include "nmea.h" +unsigned int debug; static struct ustream_fd stream; static struct ubus_auto_conn conn; static struct blob_buf b; +static char *ubus_socket; struct timespec stamp = { 0 }; +unsigned int adjust_clock = 0; void gps_timestamp(void) @@ -49,15 +52,24 @@ gps_info(struct ubus_context *ctx, struct ubus_object *obj, blob_buf_init(&b, 0); - if (!stamp.tv_sec) { + if (!stamp.tv_sec || !gps_fields) { blobmsg_add_u8(&b, "signal", 0); } else { blobmsg_add_u32(&b, "age", now.tv_sec - stamp.tv_sec); - blobmsg_add_string(&b, "lattitude", lattitude); - blobmsg_add_string(&b, "longitude", longitude); - blobmsg_add_string(&b, "elivation", elivation); - blobmsg_add_string(&b, "course", course); - blobmsg_add_string(&b, "speed", speed); + if (gps_fields & GPS_FIELD_LAT) + blobmsg_add_string(&b, "latitude", latitude); + if (gps_fields & GPS_FIELD_LON) + blobmsg_add_string(&b, "longitude", longitude); + if (gps_fields & GPS_FIELD_ALT) + blobmsg_add_string(&b, "elevation", elevation); + if (gps_fields & GPS_FIELD_COG) + blobmsg_add_string(&b, "course", course); + if (gps_fields & GPS_FIELD_SPD) + blobmsg_add_string(&b, "speed", speed); + if (gps_fields & GPS_FIELD_SAT) + blobmsg_add_string(&b, "satellites", satellites); + if (gps_fields & GPS_FIELD_HDP) + blobmsg_add_string(&b, "HDOP", hdop); } ubus_send_reply(ctx, req, b.head); @@ -89,25 +101,94 @@ ubus_connect_handler(struct ubus_context *ctx) } static int -usage(void) +usage(const char *prog) { - LOG("ugps \n"); + fprintf(stderr, "Usage: %s [options] \n" + "Options:\n" + " -a Adjust system clock from gps\n" + " -s Path to ubus socket\n" + " -d Enable debug messages\n" + " -S Print messages to stdout\n" + " -b Set gps device baud rate\n" + "\n", prog); return -1; } +static speed_t get_baudrate(int baudrate) +{ + switch (baudrate) { + case 4800: + return B4800; + case 9600: + return B9600; + case 19200: + return B19200; + case 38400: + return B38400; + case 57600: + return B57600; + case 115200: + return B115200; + default: + fprintf(stderr, "ERROR: incorrect baud rate. Default 4800 baud rate has been set\n"); + return B4800; + } +} + int main(int argc, char ** argv) { + int ch; + char *device = NULL; + char *dbglvl = getenv("DBGLVL"); + int ulog_channels = ULOG_KMSG; + speed_t baudrate = B4800; signal(SIGPIPE, SIG_IGN); - if (argc != 2) - return usage(); + if (dbglvl) { + debug = atoi(dbglvl); + unsetenv("DBGLVL"); + } + + while ((ch = getopt(argc, argv, "ad:s:Sb:")) != -1) { + switch (ch) { + case 'a': + adjust_clock = -1; + break; + case 's': + ubus_socket = optarg; + break; + case 'd': + debug = atoi(optarg); + break; + case 'S': + ulog_channels = ULOG_STDIO; + break; + case 'b': + baudrate = get_baudrate(atoi(optarg)); + break; + default: + return usage(argv[0]); + } + } + + if (argc - optind < 1) { + fprintf(stderr, "ERROR: missing device parameter\n"); + return usage(argv[0]); + } + + device = argv[optind]; + ulog_open(ulog_channels, LOG_DAEMON, "ugps"); uloop_init(); + conn.path = ubus_socket; conn.cb = ubus_connect_handler; ubus_auto_connect(&conn); - nmea_open(argv[1], &stream, B4800); + + if (nmea_open(device, &stream, baudrate) < 0) + return -1; + uloop_run(); uloop_done();