2 ** Copyright 2006, The Android Open Source Project
3 ** Copyright 2009, Michael Trimarchi <michael@panicking.kicks-ass.org>
4 ** Copyright 2015, Keith Conger <keith.conger@gmail.com>
6 ** This program is free software; you can redistribute it and/or modify it under
7 ** the terms of the GNU General Public License as published by the Free
8 ** Software Foundation; either version 2, or (at your option) any later
11 ** This program is distributed in the hope that it will be useful, but WITHOUT
12 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 ** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 ** You should have received a copy of the GNU General Public License along with
17 ** this program; if not, write to the Free Software Foundation, Inc., 59
18 ** Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <sys/epoll.h>
31 #define LOG_TAG "gps_serial"
33 #include <cutils/log.h>
34 #include <cutils/sockets.h>
35 #include <cutils/properties.h>
36 #include <hardware/gps.h>
38 /* this is the state of our connection to the qemu_gpsd daemon */
42 GpsCallbacks *callbacks;
48 static GpsState _gps_state[1];
49 static int id_in_fixed[12];
51 #define DFR(...) ALOGD(__VA_ARGS__)
54 # define D(...) ALOGD(__VA_ARGS__)
56 # define D(...) ((void)0)
59 #define GPS_DEV_SLOW_UPDATE_RATE (10)
60 #define GPS_DEV_HIGH_UPDATE_RATE (1)
62 static void gps_dev_init(int fd);
63 static void gps_dev_deinit(int fd);
64 static void gps_dev_start(int fd);
65 static void gps_dev_stop(int fd);
67 /*****************************************************************/
68 /*****************************************************************/
70 /***** N M E A T O K E N I Z E R *****/
72 /*****************************************************************/
73 /*****************************************************************/
75 #define MAX_NMEA_TOKENS 32
85 Token tokens[ MAX_NMEA_TOKENS ];
90 nmea_tokenizer_init( NmeaTokenizer* t, const char* p, const char* end )
95 // the initial '$' is optional
96 if (p < end && p[0] == '$')
99 // remove trailing newline
100 if (end > p && end[-1] == '\n') {
102 if (end > p && end[-1] == '\r')
106 // get rid of checksum at the end of the sentence
107 if (end >= p+3 && end[-3] == '*') {
114 q = memchr(p, ',', end-p);
118 if (count < MAX_NMEA_TOKENS) {
119 t->tokens[count].p = p;
120 t->tokens[count].end = q;
135 nmea_tokenizer_get( NmeaTokenizer* t, int index )
139 if (index < 0 || index >= t->count || index >= MAX_NMEA_TOKENS)
140 tok.p = tok.end = "";
142 tok = t->tokens[index];
149 str2int( const char* p, const char* end )
154 for ( ; len > 0; len--, p++ ) {
161 if ((unsigned)c >= 10)
164 result = result*10 + c;
174 str2float( const char* p, const char* end )
176 size_t len = end - p;
179 if (len >= sizeof(temp))
182 memcpy( temp, p, len );
184 return strtod( temp, NULL );
188 /*****************************************************************/
189 /*****************************************************************/
191 /***** N M E A P A R S E R *****/
193 /*****************************************************************/
194 /*****************************************************************/
196 #define NMEA_MAX_SIZE 255
206 GpsSvStatus sv_status;
207 gps_location_callback callback;
208 char in[ NMEA_MAX_SIZE+1 ];
212 void update_gps_status(GpsStatusValue val)
214 GpsState* state = _gps_state;
215 //Should be made thread safe...
216 state->status.status=val;
217 if (state->callbacks->status_cb)
218 state->callbacks->status_cb(&state->status);
222 void update_gps_svstatus(GpsSvStatus *val)
224 GpsState* state = _gps_state;
225 //Should be made thread safe...
226 if (state->callbacks->sv_status_cb)
227 state->callbacks->sv_status_cb(val);
231 void update_gps_location(GpsLocation *fix)
233 GpsState* state = _gps_state;
234 //Should be made thread safe...
235 if (state->callbacks->location_cb)
236 state->callbacks->location_cb(fix);
241 nmea_reader_update_utc_diff( NmeaReader* r )
243 time_t now = time(NULL);
246 long time_local, time_utc;
248 gmtime_r( &now, &tm_utc );
249 localtime_r( &now, &tm_local );
251 time_local = tm_local.tm_sec +
252 60*(tm_local.tm_min +
253 60*(tm_local.tm_hour +
254 24*(tm_local.tm_yday +
255 365*tm_local.tm_year)));
257 time_utc = tm_utc.tm_sec +
261 365*tm_utc.tm_year)));
263 r->utc_diff = time_utc - time_local;
268 nmea_reader_init( NmeaReader* r )
270 memset( r, 0, sizeof(*r) );
278 r->fix.size = sizeof(r->fix);
280 nmea_reader_update_utc_diff( r );
285 nmea_reader_set_callback( NmeaReader* r, gps_location_callback cb )
288 if (cb != NULL && r->fix.flags != 0) {
289 D("%s: sending latest fix to new callback", __FUNCTION__);
290 r->callback( &r->fix );
297 nmea_reader_update_time( NmeaReader* r, Token tok )
304 if (tok.p + 6 > tok.end)
307 if (r->utc_year < 0) {
308 // no date yet, get current one
309 time_t now = time(NULL);
310 gmtime_r( &now, &tm );
311 r->utc_year = tm.tm_year + 1900;
312 r->utc_mon = tm.tm_mon + 1;
313 r->utc_day = tm.tm_mday;
316 hour = str2int(tok.p, tok.p+2);
317 minute = str2int(tok.p+2, tok.p+4);
318 seconds = str2float(tok.p+4, tok.end);
322 tm.tm_sec = (int) seconds;
323 tm.tm_year = r->utc_year - 1900;
324 tm.tm_mon = r->utc_mon - 1;
325 tm.tm_mday = r->utc_day;
328 fix_time = mktime( &tm ) + r->utc_diff;
329 r->fix.timestamp = (long long)fix_time * 1000;
335 nmea_reader_update_date( NmeaReader* r, Token date, Token time )
340 if (tok.p + 6 != tok.end) {
341 D("Date not properly formatted: '%.*s'", tok.end-tok.p, tok.p);
344 day = str2int(tok.p, tok.p+2);
345 mon = str2int(tok.p+2, tok.p+4);
346 year = str2int(tok.p+4, tok.p+6) + 2000;
348 if ((day|mon|year) < 0) {
349 D("Date not properly formatted: '%.*s'", tok.end-tok.p, tok.p);
357 return nmea_reader_update_time( r, time );
362 convert_from_hhmm( Token tok )
364 double val = str2float(tok.p, tok.end);
365 int degrees = (int)(floor(val) / 100);
366 double minutes = val - degrees*100.;
367 double dcoord = degrees + minutes / 60.0;
373 nmea_reader_update_latlong( NmeaReader* r,
383 if (tok.p + 6 > tok.end) {
384 D("Latitude is too short: '%.*s'", tok.end-tok.p, tok.p);
387 lat = convert_from_hhmm(tok);
388 if (latitudeHemi == 'S')
392 if (tok.p + 6 > tok.end) {
393 D("Longitude is too short: '%.*s'", tok.end-tok.p, tok.p);
396 lon = convert_from_hhmm(tok);
397 if (longitudeHemi == 'W')
400 r->fix.flags |= GPS_LOCATION_HAS_LAT_LONG;
401 r->fix.latitude = lat;
402 r->fix.longitude = lon;
408 nmea_reader_update_altitude( NmeaReader* r,
413 Token tok = altitude;
415 if (tok.p >= tok.end)
418 r->fix.flags |= GPS_LOCATION_HAS_ALTITUDE;
419 r->fix.altitude = str2float(tok.p, tok.end);
424 static int nmea_reader_update_accuracy( NmeaReader* r,
428 Token tok = accuracy;
430 if (tok.p >= tok.end)
433 r->fix.accuracy = str2float(tok.p, tok.end);
434 if (r->fix.accuracy == 99.99){
438 r->fix.flags |= GPS_LOCATION_HAS_ACCURACY;
444 nmea_reader_update_bearing( NmeaReader* r,
450 if (tok.p >= tok.end)
453 r->fix.flags |= GPS_LOCATION_HAS_BEARING;
454 r->fix.bearing = str2float(tok.p, tok.end);
460 nmea_reader_update_speed( NmeaReader* r,
466 if (tok.p >= tok.end)
469 r->fix.flags |= GPS_LOCATION_HAS_SPEED;
470 r->fix.speed = str2float(tok.p, tok.end) * 1.852 / 3.6;
476 nmea_reader_update_svs( NmeaReader* r, int inview, int num, int i, Token prn, Token elevation, Token azimuth, Token snr )
482 r->sv_status.sv_list[i].prn=str2int(prn.p,prn.end);
483 r->sv_status.sv_list[i].elevation=str2int(elevation.p,elevation.end);
484 r->sv_status.sv_list[i].azimuth=str2int(azimuth.p,azimuth.end);
485 r->sv_status.sv_list[i].snr=str2int(snr.p,snr.end);
487 if (id_in_fixed[o]==str2int(prn.p,prn.end)){
488 prnid = str2int(prn.p, prn.end);
489 r->sv_status.used_in_fix_mask |= (1ul << (prnid-1));
498 nmea_reader_parse( NmeaReader* r )
500 /* we received a complete sentence, now parse it to generate
503 NmeaTokenizer tzer[1];
507 D("Received: '%.*s'", r->pos, r->in);
509 D("Too short. discarded.");
513 gettimeofday(&tv, NULL);
514 if (_gps_state->init)
515 _gps_state->callbacks->nmea_cb(tv.tv_sec*1000+tv.tv_usec/1000, r->in, r->pos);
517 nmea_tokenizer_init(tzer, r->in, r->in + r->pos);
521 D("Found %d tokens", tzer->count);
522 for (n = 0; n < tzer->count; n++) {
523 Token tok = nmea_tokenizer_get(tzer,n);
524 D("%2d: '%.*s'", n, tok.end-tok.p, tok.p);
529 tok = nmea_tokenizer_get(tzer, 0);
530 if (tok.p + 5 > tok.end) {
531 D("Sentence id '%.*s' too short, ignored.", tok.end-tok.p, tok.p);
534 // ignore first two characters.
536 if ( !memcmp(tok.p, "GGA", 3) ) {
538 Token tok_time = nmea_tokenizer_get(tzer,1);
539 Token tok_latitude = nmea_tokenizer_get(tzer,2);
540 Token tok_latitudeHemi = nmea_tokenizer_get(tzer,3);
541 Token tok_longitude = nmea_tokenizer_get(tzer,4);
542 Token tok_longitudeHemi = nmea_tokenizer_get(tzer,5);
543 Token tok_accuracy = nmea_tokenizer_get(tzer,8);
544 Token tok_altitude = nmea_tokenizer_get(tzer,9);
545 Token tok_altitudeUnits = nmea_tokenizer_get(tzer,10);
547 nmea_reader_update_time(r, tok_time);
548 nmea_reader_update_latlong(r, tok_latitude,
549 tok_latitudeHemi.p[0],
551 tok_longitudeHemi.p[0]);
552 nmea_reader_update_altitude(r, tok_altitude, tok_altitudeUnits);
554 nmea_reader_update_accuracy(r, tok_accuracy);
556 } else if ( !memcmp(tok.p, "GSA", 3) ) {
559 M=Manual, forced to operate in 2D or 3D
565 3-14 = IDs of SVs used in position fix (null for unused fields)
570 Token tok_mode = nmea_tokenizer_get(tzer,1);
571 Token tok_fix = nmea_tokenizer_get(tzer,2);
572 Token tok_id = nmea_tokenizer_get(tzer,3);
573 Token tok_pdop = nmea_tokenizer_get(tzer,15);
574 Token tok_hdop = nmea_tokenizer_get(tzer,16);
575 Token tok_vdop = nmea_tokenizer_get(tzer,17);
577 nmea_reader_update_accuracy(r, tok_hdop);
580 for ( i=0; i<12; i++ ) {
581 Token tok_id = nmea_tokenizer_get(tzer,3+i);
582 if ( tok_id.end > tok_id.p ){
583 id_in_fixed[i]=str2int(tok_id.p,tok_id.end);
584 D("Satellite used '%.*s'", tok_id.end-tok_id.p, tok_id.p);
587 } else if ( !memcmp(tok.p, "GSV", 3) ) {
589 1 = Total number of messages of this type in this cycle
591 3 = Total number of SVs in view
593 5 = Elevation in degrees, 90 maximum
594 6 = Azimuth, degrees from true north, 000 to 359
595 7 = SNR, 00-99 dB (null when not tracking)
596 8-11 = Information about second SV, same as field 4-7
597 12-15= Information about third SV, same as field 4-7
598 16-19= Information about fourth SV, same as field 4-7
601 //Satellites are handled by RPC-side code.
602 Token tok_num_messages = nmea_tokenizer_get(tzer,1);
603 Token tok_msg_number = nmea_tokenizer_get(tzer,2);
604 Token tok_svs_inview = nmea_tokenizer_get(tzer,3);
605 Token tok_sv1_prn_num = nmea_tokenizer_get(tzer,4);
606 Token tok_sv1_elevation = nmea_tokenizer_get(tzer,5);
607 Token tok_sv1_azimuth = nmea_tokenizer_get(tzer,6);
608 Token tok_sv1_snr = nmea_tokenizer_get(tzer,7);
609 Token tok_sv2_prn_num = nmea_tokenizer_get(tzer,8);
610 Token tok_sv2_elevation = nmea_tokenizer_get(tzer,9);
611 Token tok_sv2_azimuth = nmea_tokenizer_get(tzer,10);
612 Token tok_sv2_snr = nmea_tokenizer_get(tzer,11);
613 Token tok_sv3_prn_num = nmea_tokenizer_get(tzer,12);
614 Token tok_sv3_elevation = nmea_tokenizer_get(tzer,13);
615 Token tok_sv3_azimuth = nmea_tokenizer_get(tzer,14);
616 Token tok_sv3_snr = nmea_tokenizer_get(tzer,15);
617 Token tok_sv4_prn_num = nmea_tokenizer_get(tzer,16);
618 Token tok_sv4_elevation = nmea_tokenizer_get(tzer,17);
619 Token tok_sv4_azimuth = nmea_tokenizer_get(tzer,18);
620 Token tok_sv4_snr = nmea_tokenizer_get(tzer,19);
621 int num_messages = str2int(tok_num_messages.p,tok_num_messages.end);
622 int msg_number = str2int(tok_msg_number.p,tok_msg_number.end);
623 int svs_inview = str2int(tok_svs_inview.p,tok_svs_inview.end);
624 D("GSV %d %d %d", num_messages, msg_number, svs_inview );
626 r->sv_status.used_in_fix_mask = 0ul;
629 nmea_reader_update_svs( r, svs_inview, msg_number, 0, tok_sv1_prn_num, tok_sv1_elevation, tok_sv1_azimuth, tok_sv1_snr );
630 nmea_reader_update_svs( r, svs_inview, msg_number, 1, tok_sv2_prn_num, tok_sv2_elevation, tok_sv2_azimuth, tok_sv2_snr );
631 nmea_reader_update_svs( r, svs_inview, msg_number, 2, tok_sv3_prn_num, tok_sv3_elevation, tok_sv3_azimuth, tok_sv3_snr );
632 nmea_reader_update_svs( r, svs_inview, msg_number, 3, tok_sv4_prn_num, tok_sv4_elevation, tok_sv4_azimuth, tok_sv4_snr );
633 r->sv_status.num_svs=svs_inview;
635 if (num_messages==msg_number)
636 update_gps_svstatus(&r->sv_status);
638 } else if ( !memcmp(tok.p, "RMC", 3) ) {
639 Token tok_time = nmea_tokenizer_get(tzer,1);
640 Token tok_fixStatus = nmea_tokenizer_get(tzer,2);
641 Token tok_latitude = nmea_tokenizer_get(tzer,3);
642 Token tok_latitudeHemi = nmea_tokenizer_get(tzer,4);
643 Token tok_longitude = nmea_tokenizer_get(tzer,5);
644 Token tok_longitudeHemi = nmea_tokenizer_get(tzer,6);
645 Token tok_speed = nmea_tokenizer_get(tzer,7);
646 Token tok_bearing = nmea_tokenizer_get(tzer,8);
647 Token tok_date = nmea_tokenizer_get(tzer,9);
649 D("in RMC, fixStatus=%c", tok_fixStatus.p[0]);
650 if (tok_fixStatus.p[0] == 'A') {
651 nmea_reader_update_date( r, tok_date, tok_time );
653 nmea_reader_update_latlong( r, tok_latitude,
654 tok_latitudeHemi.p[0],
656 tok_longitudeHemi.p[0] );
658 nmea_reader_update_bearing( r, tok_bearing );
659 nmea_reader_update_speed ( r, tok_speed );
661 } else if ( !memcmp(tok.p, "VTG", 3) ) {
662 Token tok_fixStatus = nmea_tokenizer_get(tzer,9);
664 if (tok_fixStatus.p[0] != '\0' && tok_fixStatus.p[0] != 'N') {
665 Token tok_bearing = nmea_tokenizer_get(tzer,1);
666 Token tok_speed = nmea_tokenizer_get(tzer,5);
668 nmea_reader_update_bearing( r, tok_bearing );
669 nmea_reader_update_speed ( r, tok_speed );
673 D("Unknown sentence '%.*s", tok.end-tok.p, tok.p);
681 char* end = p + sizeof(temp);
684 p += snprintf( p, end-p, "Sending fix" );
685 if (r->fix.flags & GPS_LOCATION_HAS_LAT_LONG) {
686 p += snprintf(p, end-p, " lat=%g lon=%g", r->fix.latitude, r->fix.longitude);
688 if (r->fix.flags & GPS_LOCATION_HAS_ALTITUDE) {
689 p += snprintf(p, end-p, " altitude=%g", r->fix.altitude);
691 if (r->fix.flags & GPS_LOCATION_HAS_SPEED) {
692 p += snprintf(p, end-p, " speed=%g", r->fix.speed);
694 if (r->fix.flags & GPS_LOCATION_HAS_BEARING) {
695 p += snprintf(p, end-p, " bearing=%g", r->fix.bearing);
697 if (r->fix.flags & GPS_LOCATION_HAS_ACCURACY) {
698 p += snprintf(p,end-p, " accuracy=%g", r->fix.accuracy);
700 gmtime_r( (time_t*) &r->fix.timestamp, &utc );
701 p += snprintf(p, end-p, " time=%s", asctime( &utc ) );
705 if (r->fix.flags & GPS_LOCATION_HAS_ACCURACY) {
706 if (_gps_state->callbacks->location_cb) {
707 _gps_state->callbacks->location_cb( &r->fix );
710 D("No callback, keeping data until needed !");
717 nmea_reader_addc( NmeaReader* r, int c )
720 r->overflow = (c != '\n');
724 if (r->pos >= (int) sizeof(r->in)-1 ) {
730 r->in[r->pos] = (char)c;
734 nmea_reader_parse( r );
740 /*****************************************************************/
741 /*****************************************************************/
743 /***** C O N N E C T I O N S T A T E *****/
745 /*****************************************************************/
746 /*****************************************************************/
748 /* commands sent to the gps thread */
757 gps_state_done( GpsState* s )
759 // tell the thread to quit, and wait for it
762 write( s->control[0], &cmd, 1 );
763 pthread_join(s->thread, &dummy);
765 // close the control socket pair
766 close( s->control[0] ); s->control[0] = -1;
767 close( s->control[1] ); s->control[1] = -1;
769 // close connection to the QEMU GPS daemon
770 close( s->fd ); s->fd = -1;
776 gps_state_start( GpsState* s )
778 char cmd = CMD_START;
782 ret = write( s->control[0], &cmd, 1 );
783 } while (ret < 0 && errno == EINTR);
786 D("%s: could not send CMD_START command: ret=%d: %s",
787 __FUNCTION__, ret, strerror(errno));
792 gps_state_stop( GpsState* s )
797 do { ret=write( s->control[0], &cmd, 1 ); }
798 while (ret < 0 && errno == EINTR);
801 D("%s: could not send CMD_STOP command: ret=%d: %s",
802 __FUNCTION__, ret, strerror(errno));
807 epoll_register( int epoll_fd, int fd )
809 struct epoll_event ev;
812 /* important: make the fd non-blocking */
813 flags = fcntl(fd, F_GETFL);
814 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
819 ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev );
820 } while (ret < 0 && errno == EINTR);
826 epoll_deregister( int epoll_fd, int fd )
830 ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NULL );
831 } while (ret < 0 && errno == EINTR);
836 /* this is the main thread, it waits for commands from gps_state_start/stop and,
837 * when started, messages from the QEMU GPS daemon. these are simple NMEA sentences
838 * that must be parsed to be converted into GPS fixes sent to the framework
841 gps_state_thread( void* arg )
843 GpsState* state = (GpsState*) arg;
844 NmeaReader reader[1];
845 int epoll_fd = epoll_create(2);
847 int gps_fd = state->fd;
848 int control_fd = state->control[1];
850 nmea_reader_init( reader );
852 // register control file descriptors for polling
853 epoll_register( epoll_fd, control_fd );
854 epoll_register( epoll_fd, gps_fd );
856 D("GPS thread running");
860 struct epoll_event events[2];
863 nevents = epoll_wait( epoll_fd, events, 2, -1 );
866 ALOGE("epoll_wait() unexpected error: %s", strerror(errno));
869 for (ne = 0; ne < nevents; ne++) {
870 if ((events[ne].events & (EPOLLERR|EPOLLHUP)) != 0) {
871 ALOGE("EPOLLERR or EPOLLHUP after epoll_wait() !?");
874 if ((events[ne].events & EPOLLIN) != 0) {
875 int fd = events[ne].data.fd;
877 if (fd == control_fd) {
880 D("GPS control fd event");
882 ret = read( fd, &cmd, 1 );
883 } while (ret < 0 && errno == EINTR);
885 if (cmd == CMD_QUIT) {
886 D("GPS thread quitting on demand");
888 } else if (cmd == CMD_START) {
890 D("GPS thread starting location_cb=%p", state->callbacks->location_cb);
892 update_gps_status(GPS_STATUS_SESSION_BEGIN);
894 } else if (cmd == CMD_STOP) {
896 D("GPS thread stopping");
898 update_gps_status(GPS_STATUS_SESSION_END);
901 } else if (fd == gps_fd) {
906 ret = read( fd, buff, sizeof(buff) );
910 if (errno != EWOULDBLOCK)
911 ALOGE("Error while reading from GPS daemon socket: %s:", strerror(errno));
914 for (nn = 0; nn < ret; nn++)
915 nmea_reader_addc( reader, buff[nn] );
918 ALOGE("epoll_wait() returned unkown fd %d ?", fd);
927 gps_state_init( GpsState* state, GpsCallbacks* callbacks )
929 char prop[PROPERTY_VALUE_MAX];
930 char baud[PROPERTY_VALUE_MAX];
935 struct sigevent tmr_event;
938 state->control[0] = -1;
939 state->control[1] = -1;
941 state->callbacks = callbacks;
944 // Look for a kernel-provided device name
945 if (property_get("ro.kernel.android.gps",prop,"") == 0) {
946 D("no kernel-provided gps device name");
950 snprintf(device, sizeof(device), "/dev/%s",prop);
952 state->fd = open( device, O_RDWR );
953 } while (state->fd < 0 && errno == EINTR);
956 ALOGE("could not open gps serial device %s: %s", device, strerror(errno) );
960 D("GPS will read from %s", device);
962 // Disable echo on serial lines
963 if ( isatty( state->fd ) ) {
965 tcgetattr( state->fd, &ios );
966 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
967 ios.c_oflag &= (~ONLCR); /* Stop \n -> \r\n translation on output */
968 ios.c_iflag &= (~(ICRNL | INLCR)); /* Stop \r -> \n & \n -> \r translation on input */
969 ios.c_iflag |= (IGNCR | IXOFF); /* Ignore \r & XON/XOFF on input */
970 // Set baud rate and other flags
971 property_get("ro.kernel.android.gpsttybaud",baud,"9600");
972 if (strcmp(baud, "4800") == 0) {
973 ALOGE("Setting gps baud rate to 4800");
974 ios.c_cflag = B4800 | CRTSCTS | CS8 | CLOCAL | CREAD;
975 } else if (strcmp(baud, "9600") == 0) {
976 ALOGE("Setting gps baud rate to 9600");
977 ios.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
978 } else if (strcmp(baud, "19200") == 0) {
979 ALOGE("Setting gps baud rate to 19200");
980 ios.c_cflag = B19200 | CRTSCTS | CS8 | CLOCAL | CREAD;
981 } else if (strcmp(baud, "38400") == 0) {
982 ALOGE("Setting gps baud rate to 38400");
983 ios.c_cflag = B38400 | CRTSCTS | CS8 | CLOCAL | CREAD;
984 } else if (strcmp(baud, "57600") == 0) {
985 ALOGE("Setting gps baud rate to 57600");
986 ios.c_cflag = B57600 | CRTSCTS | CS8 | CLOCAL | CREAD;
987 } else if (strcmp(baud, "115200") == 0) {
988 ALOGE("Setting gps baud rate to 115200");
989 ios.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
991 ALOGE("GPS baud rate unknown: '%s'", baud);
995 tcsetattr( state->fd, TCSANOW, &ios );
998 if ( socketpair( AF_LOCAL, SOCK_STREAM, 0, state->control ) < 0 ) {
999 ALOGE("Could not create thread control socket pair: %s", strerror(errno));
1003 state->thread = callbacks->create_thread_cb( "gps_state_thread", gps_state_thread, state );
1005 if ( !state->thread ) {
1006 ALOGE("Could not create GPS thread: %s", strerror(errno));
1010 D("GPS state initialized");
1015 gps_state_done( state );
1019 /*****************************************************************/
1020 /*****************************************************************/
1022 /***** I N T E R F A C E *****/
1024 /*****************************************************************/
1025 /*****************************************************************/
1028 serial_gps_init(GpsCallbacks* callbacks)
1030 D("serial_gps_init");
1031 GpsState* s = _gps_state;
1034 gps_state_init(s, callbacks);
1044 serial_gps_cleanup(void)
1046 GpsState* s = _gps_state;
1056 GpsState* s = _gps_state;
1059 DFR("%s: called with uninitialized state !!", __FUNCTION__);
1063 D("%s: called", __FUNCTION__);
1072 GpsState* s = _gps_state;
1075 DFR("%s: called with uninitialized state !!", __FUNCTION__);
1079 D("%s: called", __FUNCTION__);
1086 serial_gps_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty)
1093 serial_gps_inject_location(double latitude, double longitude, float accuracy)
1099 serial_gps_delete_aiding_data(GpsAidingData flags)
1103 static int serial_gps_set_position_mode(GpsPositionMode mode, GpsPositionRecurrence recurrence,
1104 uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time)
1106 GpsState* s = _gps_state;
1109 D("%s: called with uninitialized state !!", __FUNCTION__);
1113 D("set_position_mode: mode=%d recurrence=%d min_interval=%d preferred_accuracy=%d preferred_time=%d",
1114 mode, recurrence, min_interval, preferred_accuracy, preferred_time);
1121 serial_gps_get_extension(const char* name)
1127 static const GpsInterface serialGpsInterface = {
1128 sizeof(GpsInterface),
1133 serial_gps_inject_time,
1134 serial_gps_inject_location,
1135 serial_gps_delete_aiding_data,
1136 serial_gps_set_position_mode,
1137 serial_gps_get_extension,
1141 const GpsInterface* gps_get_hardware_interface()
1143 D("GPS dev get_hardware_interface");
1144 return &serialGpsInterface;
1148 /*****************************************************************/
1149 /*****************************************************************/
1151 /***** D E V I C E *****/
1153 /*****************************************************************/
1154 /*****************************************************************/
1156 static void gps_dev_power(int state)
1162 static void gps_dev_send(int fd, char *msg)
1172 ret = write(fd, msg + n, i - n);
1174 if (ret < 0 && errno == EINTR) {
1184 static unsigned char gps_dev_calc_nmea_csum(char *msg)
1186 unsigned char csum = 0;
1189 for (i = 1; msg[i] != '*'; ++i) {
1197 static void gps_dev_set_nmea_message_rate(int fd, char *msg, int rate)
1202 sprintf(buff, "$PUBX,40,%s,%d,%d,%d,0*", msg, rate, rate, rate);
1206 sprintf((buff + i), "%02x\r\n", gps_dev_calc_nmea_csum(buff));
1208 gps_dev_send(fd, buff);
1210 D("GPS sent to device: %s", buff);
1214 static void gps_dev_set_baud_rate(int fd, int baud)
1219 for (u = 0; u < 3; ++u) {
1221 sprintf(buff, "$PUBX,41,%d,0003,0003,%d,0*", u, baud);
1225 sprintf((buff + i), "%02x\r\n", gps_dev_calc_nmea_csum(buff));
1227 gps_dev_send(fd, buff);
1229 D("Sent to device: %s", buff);
1235 static void gps_dev_set_message_rate(int fd, int rate)
1241 "GGA", "GLL", "VTG",
1245 for (i = 0; i < sizeof(msg)/sizeof(msg[0]); ++i) {
1246 gps_dev_set_nmea_message_rate(fd, msg[i], rate);
1253 static void gps_dev_init(int fd)
1261 static void gps_dev_deinit(int fd)
1267 static void gps_dev_start(int fd)
1269 // Set full message rate
1270 gps_dev_set_message_rate(fd, GPS_DEV_HIGH_UPDATE_RATE);
1272 D("GPS dev start initiated");
1276 static void gps_dev_stop(int fd)
1278 // Set slow message rate
1279 gps_dev_set_message_rate(fd, GPS_DEV_SLOW_UPDATE_RATE);
1281 D("GPS dev stop initiated");
1285 static int open_gps(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
1287 D("GPS dev open_gps");
1288 struct gps_device_t *dev = malloc(sizeof(struct gps_device_t));
1289 memset(dev, 0, sizeof(*dev));
1291 dev->common.tag = HARDWARE_DEVICE_TAG;
1292 dev->common.version = 0;
1293 dev->common.module = (struct hw_module_t*)module;
1294 dev->get_gps_interface = gps_get_hardware_interface;
1296 *device = &dev->common;
1301 static struct hw_module_methods_t gps_module_methods = {
1306 struct hw_module_t HAL_MODULE_INFO_SYM = {
1307 .tag = HARDWARE_MODULE_TAG,
1310 .id = GPS_HARDWARE_MODULE_ID,
1311 .name = "Serial GPS Module",
1312 .author = "Keith Conger",
1313 .methods = &gps_module_methods,