typo fix
[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 static struct ustream_fd stream;
31 static struct ubus_auto_conn conn;
32 static struct blob_buf b;
33 struct timespec stamp = { 0 };
34
35 void
36 gps_timestamp(void)
37 {
38         clock_gettime(CLOCK_MONOTONIC, &stamp);
39 }
40
41 static int
42 gps_info(struct ubus_context *ctx, struct ubus_object *obj,
43         struct ubus_request_data *req, const char *method,
44         struct blob_attr *msg)
45 {
46         struct timespec now;
47
48         clock_gettime(CLOCK_MONOTONIC, &now);
49
50         blob_buf_init(&b, 0);
51
52         if (!stamp.tv_sec) {
53                 blobmsg_add_u8(&b, "signal", 0);
54         } else {
55                 blobmsg_add_u32(&b, "age", now.tv_sec - stamp.tv_sec);
56                 blobmsg_add_string(&b, "latitude", latitude);
57                 blobmsg_add_string(&b, "longitude", longitude);
58                 blobmsg_add_string(&b, "elivation", elivation);
59                 blobmsg_add_string(&b, "course", course);
60                 blobmsg_add_string(&b, "speed", speed);
61         }
62         ubus_send_reply(ctx, req, b.head);
63
64         return UBUS_STATUS_OK;
65 }
66
67 static const struct ubus_method gps_methods[] = {
68         UBUS_METHOD_NOARG("info", gps_info),
69 };
70
71 static struct ubus_object_type gps_object_type =
72         UBUS_OBJECT_TYPE("gps", gps_methods);
73
74 static struct ubus_object gps_object = {
75         .name = "gps",
76         .type = &gps_object_type,
77         .methods = gps_methods,
78         .n_methods = ARRAY_SIZE(gps_methods),
79 };
80
81 static void
82 ubus_connect_handler(struct ubus_context *ctx)
83 {
84         int ret;
85
86         ret = ubus_add_object(ctx, &gps_object);
87         if (ret)
88                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
89 }
90
91 static int
92 usage(void)
93 {
94         LOG("ugps <device>\n");
95         return -1;
96 }
97
98 int
99 main(int argc, char ** argv)
100 {
101
102         signal(SIGPIPE, SIG_IGN);
103
104         if (argc != 2)
105                 return usage();
106
107         uloop_init();
108         conn.cb = ubus_connect_handler;
109         ubus_auto_connect(&conn);
110         nmea_open(argv[1], &stream, B4800);
111         uloop_run();
112         uloop_done();
113
114         return 0;
115 }