2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
24 #include <libubox/uloop.h>
31 static struct ustream_fd stream;
32 static struct ubus_auto_conn conn;
33 static struct blob_buf b;
34 static char *ubus_socket;
35 struct timespec stamp = { 0 };
36 unsigned int adjust_clock = 0;
41 clock_gettime(CLOCK_MONOTONIC, &stamp);
45 gps_info(struct ubus_context *ctx, struct ubus_object *obj,
46 struct ubus_request_data *req, const char *method,
47 struct blob_attr *msg)
51 clock_gettime(CLOCK_MONOTONIC, &now);
56 blobmsg_add_u8(&b, "signal", 0);
58 blobmsg_add_u32(&b, "age", now.tv_sec - stamp.tv_sec);
59 blobmsg_add_string(&b, "latitude", latitude);
60 blobmsg_add_string(&b, "longitude", longitude);
61 blobmsg_add_string(&b, "elevation", elevation);
62 blobmsg_add_string(&b, "course", course);
63 blobmsg_add_string(&b, "speed", speed);
65 ubus_send_reply(ctx, req, b.head);
67 return UBUS_STATUS_OK;
70 static const struct ubus_method gps_methods[] = {
71 UBUS_METHOD_NOARG("info", gps_info),
74 static struct ubus_object_type gps_object_type =
75 UBUS_OBJECT_TYPE("gps", gps_methods);
77 static struct ubus_object gps_object = {
79 .type = &gps_object_type,
80 .methods = gps_methods,
81 .n_methods = ARRAY_SIZE(gps_methods),
85 ubus_connect_handler(struct ubus_context *ctx)
89 ret = ubus_add_object(ctx, &gps_object);
91 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
95 usage(const char *prog)
97 fprintf(stderr, "Usage: %s [options] <device>\n"
99 " -a Adjust system clock from gps\n"
100 " -s <path> Path to ubus socket\n"
101 " -d <level> Enable debug messages\n"
102 " -S Print messages to stdout\n"
108 main(int argc, char ** argv)
112 char *dbglvl = getenv("DBGLVL");
113 int ulog_channels = ULOG_KMSG;
115 signal(SIGPIPE, SIG_IGN);
118 debug = atoi(dbglvl);
122 while ((ch = getopt(argc, argv, "ad:s:S")) != -1) {
128 ubus_socket = optarg;
131 debug = atoi(optarg);
134 ulog_channels = ULOG_STDIO;
137 return usage(argv[0]);
141 if (argc - optind < 1) {
142 fprintf(stderr, "ERROR: missing device parameter\n");
143 return usage(argv[0]);
146 device = argv[optind];
147 ulog_open(ulog_channels, LOG_DAEMON, "ugps");
150 conn.path = ubus_socket;
151 conn.cb = ubus_connect_handler;
152 ubus_auto_connect(&conn);
154 if (nmea_open(device, &stream, B4800) < 0)