2 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <osmocore/utils.h>
31 #include <osmocom/bb/common/osmocom_data.h>
32 #include <osmocom/bb/mobile/gps.h>
40 static struct bsc_fd gps_bfd;
41 static struct termios gps_termios, gps_old_termios;
43 static int gps_line(char *line)
45 if (!!strncmp(line, "$GPGLL", 6))
48 if (strlen(line) < 37)
51 /* ddmm.mmmm,N,dddmm.mmmm,E,hhmmss.mmm,A */
53 printf("'%s'\n", line);
58 static int nmea_checksum(char *line)
71 return (strtoul(line+1, NULL, 16) == checksum);
74 int gps_cb(struct bsc_fd *bfd, unsigned int what)
77 static char line[128];
81 len = read(bfd->fd, buff, sizeof(buff));
83 fprintf(stderr, "error reading GPS device (errno=%d)\n", errno);
95 if (!nmea_checksum(line))
96 fprintf(stderr, "NMEA checksum error\n");
101 line[lpos++] = buff[i++];
102 if (lpos == sizeof(line))
116 gps_bfd.when = BSC_FD_READ;
118 gps_bfd.fd = open(gps.device, O_RDONLY);
128 baud = B19200; break;
130 baud = B38400; break;
132 baud = B57600; break;
134 baud = B115200; break;
137 if (isatty(gps_bfd.fd))
140 tcgetattr(gps_bfd.fd, &gps_old_termios);
141 tcgetattr(gps_bfd.fd, &gps_termios);
144 gps_termios.c_cflag |= baud;
145 cfsetispeed(&gps_termios, baud);
146 cfsetospeed(&gps_termios, baud);
148 if (tcsetattr(gps_bfd.fd, TCSANOW, &gps_termios))
149 printf("Failed to set termios for GPS\n");
152 bsc_register_fd(&gps_bfd);
162 bsc_unregister_fd(&gps_bfd);
164 if (isatty(gps_bfd.fd))
165 tcsetattr(gps_bfd.fd, TCSANOW, &gps_old_termios);
168 gps_bfd.fd = -1; /* -1 or 0 indicates: 'close' */