Show more useful error message if we can't open GPS device
[project/ugps.git] / main.c
1 /*
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.
6 *
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.
11 *
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.
15 *
16 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
17 */
18
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include <libubox/uloop.h>
25 #include <libubus.h>
26
27 #include "log.h"
28 #include "nmea.h"
29
30 unsigned int debug;
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
37 void
38 gps_timestamp(void)
39 {
40 clock_gettime(CLOCK_MONOTONIC, &stamp);
41 }
42
43 static int
44 gps_info(struct ubus_context *ctx, struct ubus_object *obj,
45 struct ubus_request_data *req, const char *method,
46 struct blob_attr *msg)
47 {
48 struct timespec now;
49
50 clock_gettime(CLOCK_MONOTONIC, &now);
51
52 blob_buf_init(&b, 0);
53
54 if (!stamp.tv_sec) {
55 blobmsg_add_u8(&b, "signal", 0);
56 } else {
57 blobmsg_add_u32(&b, "age", now.tv_sec - stamp.tv_sec);
58 blobmsg_add_string(&b, "latitude", latitude);
59 blobmsg_add_string(&b, "longitude", longitude);
60 blobmsg_add_string(&b, "elivation", elivation);
61 blobmsg_add_string(&b, "course", course);
62 blobmsg_add_string(&b, "speed", speed);
63 }
64 ubus_send_reply(ctx, req, b.head);
65
66 return UBUS_STATUS_OK;
67 }
68
69 static const struct ubus_method gps_methods[] = {
70 UBUS_METHOD_NOARG("info", gps_info),
71 };
72
73 static struct ubus_object_type gps_object_type =
74 UBUS_OBJECT_TYPE("gps", gps_methods);
75
76 static struct ubus_object gps_object = {
77 .name = "gps",
78 .type = &gps_object_type,
79 .methods = gps_methods,
80 .n_methods = ARRAY_SIZE(gps_methods),
81 };
82
83 static void
84 ubus_connect_handler(struct ubus_context *ctx)
85 {
86 int ret;
87
88 ret = ubus_add_object(ctx, &gps_object);
89 if (ret)
90 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
91 }
92
93 static int
94 usage(const char *prog)
95 {
96 fprintf(stderr, "Usage: %s [options] <device>\n"
97 "Options:\n"
98 " -s <path> Path to ubus socket\n"
99 " -d <level> Enable debug messages\n"
100 " -S Print messages to stdout\n"
101 "\n", prog);
102 return -1;
103 }
104
105 int
106 main(int argc, char ** argv)
107 {
108 int ch;
109 char *device = NULL;
110 char *dbglvl = getenv("DBGLVL");
111 int ulog_channels = ULOG_KMSG;
112
113 signal(SIGPIPE, SIG_IGN);
114
115 if (dbglvl) {
116 debug = atoi(dbglvl);
117 unsetenv("DBGLVL");
118 }
119
120 while ((ch = getopt(argc, argv, "d:D:s:S")) != -1) {
121 switch (ch) {
122 case 's':
123 ubus_socket = optarg;
124 break;
125 case 'd':
126 debug = atoi(optarg);
127 break;
128 case 'S':
129 ulog_channels = ULOG_STDIO;
130 break;
131 default:
132 return usage(argv[0]);
133 }
134 }
135
136 if (argc - optind < 1) {
137 fprintf(stderr, "ERROR: missing device parameter\n");
138 return usage(argv[0]);
139 }
140
141 device = argv[optind];
142 ulog_open(ulog_channels, LOG_DAEMON, "ugps");
143
144 uloop_init();
145 conn.path = ubus_socket;
146 conn.cb = ubus_connect_handler;
147 ubus_auto_connect(&conn);
148
149 if (nmea_open(device, &stream, B4800) < 0)
150 return -1;
151
152 uloop_run();
153 uloop_done();
154
155 return 0;
156 }