7793097f105f04530406916b2dcb028b7ccb699c
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
23 #include <sys/types.h>
25 #include <sys/ioctl.h>
39 #include <libubox/utils.h>
44 #define MAX_NMEA_PARAM 20
45 #define MAX_TIME_OFFSET 5
46 #define MAX_BAD_TIME 3
51 } nmea_params
[MAX_NMEA_PARAM
];
53 static int nmea_bad_time
;
54 char longitude
[33] = { 0 }, latitude
[33] = { 0 }, course
[17] = { 0 }, speed
[17] = { 0 }, elevation
[17] = { 0 };
60 char *ids
[] = { "ERROR", "WARNING", "NOTICE", };
62 if (nmea_params
[3].num
< 0 || nmea_params
[3].num
> 2)
63 nmea_params
[3].num
= 0;
65 DEBUG(3, "%s: %s\n", ids
[nmea_params
[3].num
], nmea_params
[4].str
);
74 if (*nmea_params
[2].str
!= 'A') {
76 DEBUG(4, "waiting for valid signal\n");
81 memset(&tm
, 0, sizeof(tm
));
84 if (sscanf(nmea_params
[1].str
, "%02d%02d%02d",
85 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 3) {
86 ERROR("failed to parse time '%s'\n", nmea_params
[1].str
);
88 else if (sscanf(nmea_params
[9].str
, "%02d%02d%02d",
89 &tm
.tm_mday
, &tm
.tm_mon
, &tm
.tm_year
) != 3) {
90 ERROR("failed to parse date '%s'\n", nmea_params
[9].str
);
93 tm
.tm_year
+= 100; /* year starts with 1900 */
94 tm
.tm_mon
-= 1; /* month starts with 0 */
96 strftime(tmp
, 256, "%Y-%m-%dT%H:%M:%S", &tm
);
97 DEBUG(3, "date: %s UTC\n", tmp
);
100 time_t sec
= timegm(&tm
);
101 struct timeval tv
= { 0 };
104 gettimeofday(&cur
, NULL
);
106 if ((sec
< 0) || (abs(cur
.tv_sec
- tv
.tv_sec
) > MAX_TIME_OFFSET
)) {
108 if (++nmea_bad_time
> MAX_BAD_TIME
) {
109 LOG("system time differs from GPS time by more than %d seconds. Using %s UTC as the new time\n", MAX_TIME_OFFSET
, tmp
);
110 /* only set datetime if specified by command line argument! */
111 settimeofday(&tv
, NULL
);
119 if (strlen(nmea_params
[3].str
) < 9 || strlen(nmea_params
[5].str
) < 10) {
120 ERROR("lat/lng have invalid string length %d<9, %d<10\n",
121 strlen(nmea_params
[3].str
), strlen(nmea_params
[5].str
));
125 float lat
= strtof(nmea_params
[3].str
, NULL
);
126 float lon
= strtof(nmea_params
[5].str
, NULL
);
128 degrees
= floor(lat
/ 100.0);
129 minutes
= lat
- (degrees
* 100.0);
130 lat
= degrees
+ minutes
/ 60.0;
132 degrees
= floor(lon
/ 100.0);
133 minutes
= lon
- (degrees
* 100.0);
134 lon
= degrees
+ minutes
/ 60.0;
136 if (*nmea_params
[4].str
== 'S')
138 if (*nmea_params
[6].str
== 'W')
141 snprintf(latitude
, sizeof(latitude
), "%f", lat
);
142 snprintf(longitude
, sizeof(longitude
), "%f", lon
);
144 DEBUG(3, "position: %s %s\n", latitude
, longitude
);
154 strncpy(elevation
, nmea_params
[9].str
, sizeof(elevation
));
155 DEBUG(4, "height: %s\n", elevation
);
163 strncpy(course
, nmea_params
[1].str
, sizeof(course
));
164 strncpy(speed
, nmea_params
[7].str
, sizeof(speed
));
165 DEBUG(4, "course: %s\n", course
);
166 DEBUG(4, "speed: %s\n", speed
);
169 static struct nmea_msg
{
172 void (*handler
) (void);
177 .handler
= nmea_txt_cb
,
181 .handler
= nmea_rmc_cb
,
185 .handler
= nmea_gga_cb
,
189 .handler
= nmea_vtg_cb
,
194 nmea_verify_checksum(char *s
)
196 char *csum
= strrchr(s
, '*');
204 isum
= strtol(csum
, NULL
, 16);
216 nmea_tokenize(char *msg
)
219 char *tok
= strsep(&msg
, ",");
221 while (tok
&& cnt
< MAX_NMEA_PARAM
) {
222 nmea_params
[cnt
].str
= tok
;
223 nmea_params
[cnt
].num
= atoi(tok
);
225 tok
= strsep(&msg
, ",");
232 nmea_process(char *a
)
237 if (strncmp(a
, "$GP", 3))
241 csum
= strrchr(a
, '*');
245 if (nmea_verify_checksum(a
)) {
246 ERROR("nmea message has invalid checksum\n");
250 cnt
= nmea_tokenize(&a
[2]);
252 ERROR("failed to tokenize %s\n", a
);\
256 for (i
= 0; i
< ARRAY_SIZE(nmea_msgs
); i
++) {
257 if (strcmp(nmea_params
[0].str
, nmea_msgs
[i
].msg
))
259 if (nmea_msgs
[i
].cnt
<= cnt
)
260 nmea_msgs
[i
].handler();
262 ERROR("%s datagram has wrong parameter count got %d but expected %d\n", nmea_msgs
[i
].msg
, cnt
, nmea_msgs
[i
].cnt
);
268 nmea_consume(struct ustream
*s
, char **a
)
270 char *eol
= strstr(*a
, "\n");
279 ustream_consume(s
, eol
- *a
);
286 nmea_msg_cb(struct ustream
*s
, int bytes
)
289 char *a
= ustream_get_read_buf(s
, &len
);
291 while (!nmea_consume(s
, &a
))
295 static void nmea_notify_cb(struct ustream
*s
)
300 ERROR("tty error, shutting down\n");
305 nmea_open(char *dev
, struct ustream_fd
*s
, speed_t speed
)
310 tty
= open(dev
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
312 ERROR("%s: device open failed: %s\n", dev
, strerror(errno
));
316 tcgetattr(tty
, &tio
);
317 tio
.c_cflag
|= CREAD
;
319 tio
.c_iflag
|= IGNPAR
;
320 tio
.c_lflag
&= ~(ICANON
);
321 tio
.c_lflag
&= ~(ECHO
);
322 tio
.c_lflag
&= ~(ECHOE
);
323 tio
.c_lflag
&= ~(ISIG
);
326 cfsetispeed(&tio
, speed
);
327 cfsetospeed(&tio
, speed
);
328 tcsetattr(tty
, TCSANOW
, &tio
);
330 s
->stream
.string_data
= true;
331 s
->stream
.notify_read
= nmea_msg_cb
;
332 s
->stream
.notify_state
= nmea_notify_cb
;
334 ustream_fd_init(s
, tty
);
336 tcflush(tty
, TCIFLUSH
);