1/*
2Copyright (c) 2016, Joo Aun Saw
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include <stdio.h>
29#include <math.h>
30#include <time.h>
31#include <string.h>
32#include <dlfcn.h>
33
34#include "libgps_loader.h"
35
36const char *LIBGPS_FILE = LIBGPS_SO_VERSION;
37
38static int libgps_load_sym(void **func, void *handle, char *symbol)
39{
40 char *sym_error;
41
42 *func = dlsym(handle, symbol);
43 if ((sym_error = dlerror()) != NULL)
44 {
45 fprintf(stderr, "%s\n", sym_error);
46 return -1;
47 }
48 return 0;
49}
50
51int libgps_load(gpsd_info *gpsd)
52{
53 int err = 0;
54
55 gpsd->libgps_handle = dlopen(LIBGPS_FILE, RTLD_LAZY);
56 if (!gpsd->libgps_handle)
57 {
58 fprintf(stderr, "%s\n", dlerror());
59 return -1;
60 }
61
62 err |= libgps_load_sym((void **)(&gpsd->gps_read), gpsd->libgps_handle, "gps_read");
63 err |= libgps_load_sym((void **)(&gpsd->gps_waiting), gpsd->libgps_handle, "gps_waiting");
64 err |= libgps_load_sym((void **)(&gpsd->gps_open), gpsd->libgps_handle, "gps_open");
65 err |= libgps_load_sym((void **)(&gpsd->gps_close), gpsd->libgps_handle, "gps_close");
66 err |= libgps_load_sym((void **)(&gpsd->gps_errstr), gpsd->libgps_handle, "gps_errstr");
67 err |= libgps_load_sym((void **)(&gpsd->gps_stream), gpsd->libgps_handle, "gps_stream");
68 if (err)
69 return -1;
70
71 return 0;
72}
73
74void libgps_unload(gpsd_info *gpsd)
75{
76 if (gpsd->libgps_handle)
77 {
78 disconnect_gpsd(gpsd);
79 dlclose(gpsd->libgps_handle);
80 gpsd->libgps_handle = NULL;
81 }
82}
83
84void gpsd_init(gpsd_info *gpsd)
85{
86 memset(gpsd, 0, sizeof(gpsd_info));
87 gpsd->server = "localhost";
88 gpsd->port = DEFAULT_GPSD_PORT;
89}
90
91int connect_gpsd(gpsd_info *gpsd)
92{
93 if (gpsd->libgps_handle == NULL)
94 return -1;
95 if (!gpsd->gpsd_connected)
96 {
97 if (gpsd->gps_open(gpsd->server, gpsd->port, &gpsd->gpsdata) != 0)
98 return -1;
99 gpsd->gpsd_connected = 1;
100 gpsd->gps_stream(&gpsd->gpsdata, WATCH_ENABLE, NULL);
101 }
102 return 0;
103}
104
105int disconnect_gpsd(gpsd_info *gpsd)
106{
107 if (gpsd->libgps_handle == NULL)
108 return -1;
109 if (gpsd->gpsd_connected)
110 {
111 gpsd->gps_stream(&gpsd->gpsdata, WATCH_DISABLE, NULL);
112 gpsd->gps_close(&gpsd->gpsdata);
113 gpsd->gpsd_connected = 0;
114 }
115 return 0;
116}
117
118int wait_gps_time(gpsd_info *gpsd, int timeout_s)
119{
120 if (gpsd->libgps_handle == NULL)
121 return -1;
122 if (gpsd->gpsd_connected)
123 {
124 gps_mask_t mask = TIME_SET;
125 time_t start = time(NULL);
126 while ((time(NULL) - start < timeout_s) &&
127 ((!gpsd->gpsdata.online) || ((gpsd->gpsdata.set & mask) == 0)))
128 {
129 if (gpsd->gps_waiting(&gpsd->gpsdata, 200))
130 read_gps_data_once(gpsd);
131 }
132 if ((gpsd->gpsdata.online) && ((gpsd->gpsdata.set & mask) != 0))
133 return 0;
134 }
135 return -1;
136}
137
138int read_gps_data_once(gpsd_info *gpsd)
139{
140 if (gpsd->libgps_handle == NULL)
141 return -1;
142 if (gpsd->gpsd_connected)
143 {
144 if (gpsd->gps_waiting(&gpsd->gpsdata, 200))
145 {
146 int ret = gpsd->gps_read(&gpsd->gpsdata);
147 if (ret < 0)
148 {
149 gpsd->gps_close(&gpsd->gpsdata);
150 gpsd->gpsd_connected = 0;
151 ret = 0;
152 }
153 return ret;
154 }
155 }
156 return 0;
157}
158
159int deg_to_str(double f, char *buf, int buf_size)
160{
161 double fsec, fdeg, fmin;
162
163 if (buf_size <= 0)
164 return -1;
165 *buf = 0;
166 if (f < 0 || f > 360)
167 return -1;
168
169 fmin = modf(f, &fdeg);
170 fsec = modf(fmin * 60, &fmin);
171 fsec *= 60;
172 snprintf(buf, buf_size, "%03d/1,%02d/1,%05d/1000", (int)fdeg, (int)fmin, (int)(fsec*1000));
173
174 return 0;
175}
176