61c57ab883da6e47897306885c923e2b90a23878
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>
19 #define _DEFAULT_SOURCE
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
40 #include <libubox/utils.h>
45 #define MAX_NMEA_PARAM 20
46 #define MAX_TIME_OFFSET 5
47 #define MAX_BAD_TIME 3
52 } nmea_params
[MAX_NMEA_PARAM
];
54 static int nmea_bad_time
;
55 char longitude
[33] = { 0 }, latitude
[33] = { 0 }, course
[17] = { 0 }, speed
[17] = { 0 }, elevation
[17] = { 0 };
61 char *ids
[] = { "ERROR", "WARNING", "NOTICE", };
63 if (nmea_params
[3].num
< 0 || nmea_params
[3].num
> 2)
64 nmea_params
[3].num
= 0;
66 DEBUG(3, "%s: %s\n", ids
[nmea_params
[3].num
], nmea_params
[4].str
);
75 if (*nmea_params
[2].str
!= 'A') {
77 DEBUG(4, "waiting for valid signal\n");
82 memset(&tm
, 0, sizeof(tm
));
85 if (sscanf(nmea_params
[1].str
, "%02d%02d%02d",
86 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 3) {
87 ERROR("failed to parse time '%s'\n", nmea_params
[1].str
);
89 else if (sscanf(nmea_params
[9].str
, "%02d%02d%02d",
90 &tm
.tm_mday
, &tm
.tm_mon
, &tm
.tm_year
) != 3) {
91 ERROR("failed to parse date '%s'\n", nmea_params
[9].str
);
94 tm
.tm_year
+= 100; /* year starts with 1900 */
95 tm
.tm_mon
-= 1; /* month starts with 0 */
97 strftime(tmp
, 256, "%Y-%m-%dT%H:%M:%S", &tm
);
98 DEBUG(3, "date: %s UTC\n", tmp
);
101 time_t sec
= timegm(&tm
);
104 gettimeofday(&cur
, NULL
);
106 if ((sec
< 0) || (abs(cur
.tv_sec
- sec
) > MAX_TIME_OFFSET
)) {
107 struct timeval tv
= { 0 };
109 if (++nmea_bad_time
> MAX_BAD_TIME
) {
110 LOG("system time differs from GPS time by more than %d seconds. Using %s UTC as the new time\n", MAX_TIME_OFFSET
, tmp
);
111 /* only set datetime if specified by command line argument! */
112 settimeofday(&tv
, NULL
);
120 if (strlen(nmea_params
[3].str
) < 9 || strlen(nmea_params
[5].str
) < 10) {
121 ERROR("lat/lng have invalid string length %zu<9, %zu<10\n",
122 strlen(nmea_params
[3].str
), strlen(nmea_params
[5].str
));
126 float lat
= strtof(nmea_params
[3].str
, NULL
);
127 float lon
= strtof(nmea_params
[5].str
, NULL
);
129 degrees
= floor(lat
/ 100.0);
130 minutes
= lat
- (degrees
* 100.0);
131 lat
= degrees
+ minutes
/ 60.0;
133 degrees
= floor(lon
/ 100.0);
134 minutes
= lon
- (degrees
* 100.0);
135 lon
= degrees
+ minutes
/ 60.0;
137 if (*nmea_params
[4].str
== 'S')
139 if (*nmea_params
[6].str
== 'W')
142 snprintf(latitude
, sizeof(latitude
), "%f", lat
);
143 snprintf(longitude
, sizeof(longitude
), "%f", lon
);
145 DEBUG(3, "position: %s %s\n", latitude
, longitude
);
155 strncpy(elevation
, nmea_params
[9].str
, sizeof(elevation
));
156 DEBUG(4, "height: %s\n", elevation
);
164 strncpy(course
, nmea_params
[1].str
, sizeof(course
));
165 strncpy(speed
, nmea_params
[7].str
, sizeof(speed
));
166 DEBUG(4, "course: %s\n", course
);
167 DEBUG(4, "speed: %s\n", speed
);
170 static struct nmea_msg
{
173 void (*handler
) (void);
178 .handler
= nmea_txt_cb
,
182 .handler
= nmea_rmc_cb
,
186 .handler
= nmea_gga_cb
,
190 .handler
= nmea_vtg_cb
,
195 nmea_verify_checksum(char *s
)
197 char *csum
= strrchr(s
, '*');
205 isum
= strtol(csum
, NULL
, 16);
217 nmea_tokenize(char *msg
)
220 char *tok
= strsep(&msg
, ",");
222 while (tok
&& cnt
< MAX_NMEA_PARAM
) {
223 nmea_params
[cnt
].str
= tok
;
224 nmea_params
[cnt
].num
= atoi(tok
);
226 tok
= strsep(&msg
, ",");
233 nmea_process(char *a
)
239 if (strncmp(a
, "$GP", 3))
243 csum
= strrchr(a
, '*');
247 if (nmea_verify_checksum(a
)) {
248 ERROR("nmea message has invalid checksum\n");
252 cnt
= nmea_tokenize(&a
[2]);
254 ERROR("failed to tokenize %s\n", a
);\
258 for (i
= 0; i
< ARRAY_SIZE(nmea_msgs
); i
++) {
259 if (strcmp(nmea_params
[0].str
, nmea_msgs
[i
].msg
))
261 if (nmea_msgs
[i
].cnt
<= cnt
)
262 nmea_msgs
[i
].handler();
264 ERROR("%s datagram has wrong parameter count got %d but expected %d\n", nmea_msgs
[i
].msg
, cnt
, nmea_msgs
[i
].cnt
);
270 nmea_consume(struct ustream
*s
, char **a
)
272 char *eol
= strstr(*a
, "\n");
281 ustream_consume(s
, eol
- *a
);
288 nmea_msg_cb(struct ustream
*s
, int bytes
)
291 char *a
= ustream_get_read_buf(s
, &len
);
293 while (!nmea_consume(s
, &a
))
297 static void nmea_notify_cb(struct ustream
*s
)
302 ERROR("tty error, shutting down\n");
307 nmea_open(char *dev
, struct ustream_fd
*s
, speed_t speed
)
312 tty
= open(dev
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
314 ERROR("%s: device open failed: %s\n", dev
, strerror(errno
));
318 tcgetattr(tty
, &tio
);
319 tio
.c_cflag
|= CREAD
;
321 tio
.c_iflag
|= IGNPAR
;
322 tio
.c_lflag
&= ~(ICANON
);
323 tio
.c_lflag
&= ~(ECHO
);
324 tio
.c_lflag
&= ~(ECHOE
);
325 tio
.c_lflag
&= ~(ISIG
);
328 cfsetispeed(&tio
, speed
);
329 cfsetospeed(&tio
, speed
);
330 tcsetattr(tty
, TCSANOW
, &tio
);
332 s
->stream
.string_data
= true;
333 s
->stream
.notify_read
= nmea_msg_cb
;
334 s
->stream
.notify_state
= nmea_notify_cb
;
336 ustream_fd_init(s
, tty
);
338 tcflush(tty
, TCIFLUSH
);