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];
52 #define DFR(...) ALOGD(__VA_ARGS__)
55 # define D(...) ALOGD(__VA_ARGS__)
57 # define D(...) ((void)0)
60 #define GPS_DEV_SLOW_UPDATE_RATE (10)
61 #define GPS_DEV_HIGH_UPDATE_RATE (1)
63 #define GPS_DEV_LOW_BAUD (B9600)
64 #define GPS_DEV_HIGH_BAUD (B115200)
65 static void gps_dev_init(int fd);
66 static void gps_dev_deinit(int fd);
67 static void gps_dev_start(int fd);
68 static void gps_dev_stop(int fd);
70 /*****************************************************************/
71 /*****************************************************************/
73 /***** N M E A T O K E N I Z E R *****/
75 /*****************************************************************/
76 /*****************************************************************/
83 #define MAX_NMEA_TOKENS 16
87 Token tokens[ MAX_NMEA_TOKENS ];
91 nmea_tokenizer_init( NmeaTokenizer* t, const char* p, const char* end )
96 // the initial '$' is optional
97 if (p < end && p[0] == '$')
100 // remove trailing newline
101 if (end > p && end[-1] == '\n') {
103 if (end > p && end[-1] == '\r')
107 // get rid of checksum at the end of the sentence
108 if (end >= p+3 && end[-3] == '*') {
115 q = memchr(p, ',', end-p);
120 if (count < MAX_NMEA_TOKENS) {
121 t->tokens[count].p = p;
122 t->tokens[count].end = q;
137 nmea_tokenizer_get( NmeaTokenizer* t, int index )
141 if (index < 0 || index >= t->count || index >= MAX_NMEA_TOKENS)
142 tok.p = tok.end = "";
144 tok = t->tokens[index];
150 str2int( const char* p, const char* end )
155 for ( ; len > 0; len--, p++ ) {
162 if ((unsigned)c >= 10)
165 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 );
187 /*****************************************************************/
188 /*****************************************************************/
190 /***** N M E A P A R S E R *****/
192 /*****************************************************************/
193 /*****************************************************************/
195 #define NMEA_MAX_SIZE 255
205 GpsSvStatus sv_status;
206 gps_location_callback callback;
207 char in[ NMEA_MAX_SIZE+1 ];
210 void update_gps_status(GpsStatusValue val)
212 GpsState* state = _gps_state;
213 //Should be made thread safe...
214 state->status.status=val;
215 if (state->callbacks->status_cb)
216 state->callbacks->status_cb(&state->status);
219 void update_gps_svstatus(GpsSvStatus *val)
221 GpsState* state = _gps_state;
222 //Should be made thread safe...
223 if (state->callbacks->sv_status_cb)
224 state->callbacks->sv_status_cb(val);
227 void update_gps_location(GpsLocation *fix)
229 GpsState* state = _gps_state;
230 //Should be made thread safe...
231 if (state->callbacks->location_cb)
232 state->callbacks->location_cb(fix);
236 nmea_reader_update_utc_diff( NmeaReader* r )
238 time_t now = time(NULL);
241 long time_local, time_utc;
243 gmtime_r( &now, &tm_utc );
244 localtime_r( &now, &tm_local );
246 time_local = tm_local.tm_sec +
247 60*(tm_local.tm_min +
248 60*(tm_local.tm_hour +
249 24*(tm_local.tm_yday +
250 365*tm_local.tm_year)));
252 time_utc = tm_utc.tm_sec +
256 365*tm_utc.tm_year)));
258 r->utc_diff = time_utc - time_local;
263 nmea_reader_init( NmeaReader* r )
265 memset( r, 0, sizeof(*r) );
273 r->fix.size = sizeof(r->fix);
275 nmea_reader_update_utc_diff( r );
280 nmea_reader_set_callback( NmeaReader* r, gps_location_callback cb )
283 if (cb != NULL && r->fix.flags != 0) {
284 D("%s: sending latest fix to new callback", __FUNCTION__);
285 r->callback( &r->fix );
292 nmea_reader_update_time( NmeaReader* r, Token tok )
299 if (tok.p + 6 > tok.end)
302 if (r->utc_year < 0) {
303 // no date yet, get current one
304 time_t now = time(NULL);
305 gmtime_r( &now, &tm );
306 r->utc_year = tm.tm_year + 1900;
307 r->utc_mon = tm.tm_mon + 1;
308 r->utc_day = tm.tm_mday;
311 hour = str2int(tok.p, tok.p+2);
312 minute = str2int(tok.p+2, tok.p+4);
313 seconds = str2float(tok.p+4, tok.end);
317 tm.tm_sec = (int) seconds;
318 tm.tm_year = r->utc_year - 1900;
319 tm.tm_mon = r->utc_mon - 1;
320 tm.tm_mday = r->utc_day;
323 fix_time = mktime( &tm ) + r->utc_diff;
324 r->fix.timestamp = (long long)fix_time * 1000;
329 nmea_reader_update_date( NmeaReader* r, Token date, Token time )
334 if (tok.p + 6 != tok.end) {
335 D("date not properly formatted: '%.*s'", tok.end-tok.p, tok.p);
338 day = str2int(tok.p, tok.p+2);
339 mon = str2int(tok.p+2, tok.p+4);
340 year = str2int(tok.p+4, tok.p+6) + 2000;
342 if ((day|mon|year) < 0) {
343 D("date not properly formatted: '%.*s'", tok.end-tok.p, tok.p);
351 return nmea_reader_update_time( r, time );
356 convert_from_hhmm( Token tok )
358 double val = str2float(tok.p, tok.end);
359 int degrees = (int)(floor(val) / 100);
360 double minutes = val - degrees*100.;
361 double dcoord = degrees + minutes / 60.0;
367 nmea_reader_update_latlong( NmeaReader* r,
377 if (tok.p + 6 > tok.end) {
378 D("latitude is too short: '%.*s'", tok.end-tok.p, tok.p);
381 lat = convert_from_hhmm(tok);
382 if (latitudeHemi == 'S')
386 if (tok.p + 6 > tok.end) {
387 D("longitude is too short: '%.*s'", tok.end-tok.p, tok.p);
390 lon = convert_from_hhmm(tok);
391 if (longitudeHemi == 'W')
394 r->fix.flags |= GPS_LOCATION_HAS_LAT_LONG;
395 r->fix.latitude = lat;
396 r->fix.longitude = lon;
402 nmea_reader_update_altitude( NmeaReader* r,
407 Token tok = altitude;
409 if (tok.p >= tok.end)
412 r->fix.flags |= GPS_LOCATION_HAS_ALTITUDE;
413 r->fix.altitude = str2float(tok.p, tok.end);
418 static int nmea_reader_update_accuracy( NmeaReader* r,
422 Token tok = accuracy;
424 if (tok.p >= tok.end)
427 r->fix.accuracy = str2float(tok.p, tok.end);
428 if (r->fix.accuracy == 99.99){
432 r->fix.flags |= GPS_LOCATION_HAS_ACCURACY;
438 nmea_reader_update_bearing( NmeaReader* r,
444 if (tok.p >= tok.end)
447 r->fix.flags |= GPS_LOCATION_HAS_BEARING;
448 r->fix.bearing = str2float(tok.p, tok.end);
454 nmea_reader_update_speed( NmeaReader* r,
460 if (tok.p >= tok.end)
463 r->fix.flags |= GPS_LOCATION_HAS_SPEED;
464 r->fix.speed = str2float(tok.p, tok.end);
469 nmea_reader_update_svs( NmeaReader* r, int inview, int num, int i, Token prn, Token elevation, Token azimuth, Token snr )
474 r->sv_status.sv_list[i].prn=str2int(prn.p,prn.end);
475 r->sv_status.sv_list[i].elevation=str2int(elevation.p,elevation.end);
476 r->sv_status.sv_list[i].azimuth=str2int(azimuth.p,azimuth.end);
477 r->sv_status.sv_list[i].snr=str2int(snr.p,snr.end);
479 if (id_in_fixed[o]==str2int(prn.p,prn.end))
480 r->sv_status.used_in_fix_mask |= 1<i ;
487 nmea_reader_parse( NmeaReader* r )
489 /* we received a complete sentence, now parse it to generate
492 NmeaTokenizer tzer[1];
496 D("Received: '%.*s'", r->pos, r->in);
498 D("Too short. discarded.");
502 gettimeofday(&tv, NULL);
503 if (_gps_state->init)
504 _gps_state->callbacks->nmea_cb(tv.tv_sec*1000+tv.tv_usec/1000, r->in, r->pos);
506 nmea_tokenizer_init(tzer, r->in, r->in + r->pos);
510 D("Found %d tokens", tzer->count);
511 for (n = 0; n < tzer->count; n++) {
512 Token tok = nmea_tokenizer_get(tzer,n);
513 D("%2d: '%.*s'", n, tok.end-tok.p, tok.p);
518 tok = nmea_tokenizer_get(tzer, 0);
519 if (tok.p + 5 > tok.end) {
520 D("sentence id '%.*s' too short, ignored.", tok.end-tok.p, tok.p);
523 // ignore first two characters.
525 if ( !memcmp(tok.p, "GGA", 3) ) {
527 Token tok_time = nmea_tokenizer_get(tzer,1);
528 Token tok_latitude = nmea_tokenizer_get(tzer,2);
529 Token tok_latitudeHemi = nmea_tokenizer_get(tzer,3);
530 Token tok_longitude = nmea_tokenizer_get(tzer,4);
531 Token tok_longitudeHemi = nmea_tokenizer_get(tzer,5);
532 Token tok_accuracy = nmea_tokenizer_get(tzer,8);
533 Token tok_altitude = nmea_tokenizer_get(tzer,9);
534 Token tok_altitudeUnits = nmea_tokenizer_get(tzer,10);
536 nmea_reader_update_time(r, tok_time);
537 nmea_reader_update_latlong(r, tok_latitude,
538 tok_latitudeHemi.p[0],
540 tok_longitudeHemi.p[0]);
541 nmea_reader_update_altitude(r, tok_altitude, tok_altitudeUnits);
543 nmea_reader_update_accuracy(r, tok_accuracy);
545 } else if ( !memcmp(tok.p, "GSA", 3) ) {
546 //Satellites are handled by RPC-side code.
549 M=Manual, forced to operate in 2D or 3D
555 3-14 = IDs of SVs used in position fix (null for unused fields)
560 Token tok_mode = nmea_tokenizer_get(tzer,1);
561 Token tok_fix = nmea_tokenizer_get(tzer,2);
562 Token tok_id = nmea_tokenizer_get(tzer,3);
563 Token tok_pdop = nmea_tokenizer_get(tzer,15);
564 Token tok_hdop = nmea_tokenizer_get(tzer,16);
565 Token tok_vdop = nmea_tokenizer_get(tzer,17);
568 for ( i=0; i<12; i++ ) {
569 Token tok_id = nmea_tokenizer_get(tzer,3+i);
570 if ( tok_id.end > tok_id.p )
571 id_in_fixed[i]=str2int(tok_id.end, tok_id.p);
573 } else if ( !memcmp(tok.p, "GSV", 3) ) {
574 //Satellites are handled by RPC-side code.
575 Token tok_num_messages = nmea_tokenizer_get(tzer,1);
576 Token tok_msg_number = nmea_tokenizer_get(tzer,2);
577 Token tok_svs_inview = nmea_tokenizer_get(tzer,3);
578 Token tok_sv1_prn_num = nmea_tokenizer_get(tzer,4);
579 Token tok_sv1_elevation = nmea_tokenizer_get(tzer,5);
580 Token tok_sv1_azimuth = nmea_tokenizer_get(tzer,6);
581 Token tok_sv1_snr = nmea_tokenizer_get(tzer,7);
582 Token tok_sv2_prn_num = nmea_tokenizer_get(tzer,8);
583 Token tok_sv2_elevation = nmea_tokenizer_get(tzer,9);
584 Token tok_sv2_azimuth = nmea_tokenizer_get(tzer,10);
585 Token tok_sv2_snr = nmea_tokenizer_get(tzer,11);
586 Token tok_sv3_prn_num = nmea_tokenizer_get(tzer,12);
587 Token tok_sv3_elevation = nmea_tokenizer_get(tzer,13);
588 Token tok_sv3_azimuth = nmea_tokenizer_get(tzer,14);
589 Token tok_sv3_snr = nmea_tokenizer_get(tzer,15);
590 Token tok_sv4_prn_num = nmea_tokenizer_get(tzer,16);
591 Token tok_sv4_elevation = nmea_tokenizer_get(tzer,17);
592 Token tok_sv4_azimuth = nmea_tokenizer_get(tzer,18);
593 Token tok_sv4_snr = nmea_tokenizer_get(tzer,19);
594 int num_messages = str2int(tok_num_messages.p,tok_num_messages.end);
595 int msg_number = str2int(tok_msg_number.p,tok_msg_number.end);
596 int svs_inview = str2int(tok_svs_inview.p,tok_svs_inview.end);
597 D("GSV %d %d %d", num_messages, msg_number, svs_inview );
599 r->sv_status.used_in_fix_mask=0;
600 nmea_reader_update_svs( r, svs_inview, msg_number, 0, tok_sv1_prn_num, tok_sv1_elevation, tok_sv1_azimuth, tok_sv1_snr );
601 nmea_reader_update_svs( r, svs_inview, msg_number, 1, tok_sv2_prn_num, tok_sv2_elevation, tok_sv2_azimuth, tok_sv2_snr );
602 nmea_reader_update_svs( r, svs_inview, msg_number, 2, tok_sv3_prn_num, tok_sv3_elevation, tok_sv3_azimuth, tok_sv3_snr );
603 nmea_reader_update_svs( r, svs_inview, msg_number, 3, tok_sv4_prn_num, tok_sv4_elevation, tok_sv4_azimuth, tok_sv4_snr );
604 r->sv_status.num_svs=svs_inview;
606 if (num_messages==msg_number)
607 update_gps_svstatus(&r->sv_status);
609 1 = Total number of messages of this type in this cycle
611 3 = Total number of SVs in view
613 5 = Elevation in degrees, 90 maximum
614 6 = Azimuth, degrees from true north, 000 to 359
615 7 = SNR, 00-99 dB (null when not tracking)
616 8-11 = Information about second SV, same as field 4-7
617 12-15= Information about third SV, same as field 4-7
618 16-19= Information about fourth SV, same as field 4-7
622 _gps_state.sv_status.num_svs=ntohl(data[82]) & 0x1F;
623 for(i=0;i<ret.num_svs;++i) {
624 _gps_state.sv_status.sv_list[i].prn=ntohl(data[83+3*i]);
625 _gps_state.sv_status.sv_list[i].elevation=ntohl(data[83+3*i+1]);
626 _gps_state.sv_status.sv_list[i].azimuth=ntohl(data[83+3*i+2])/100;
627 _gps_state.sv_status.sv_list[i].snr=ntohl(data[83+3*i+2])%100;
629 _gps_state.sv_status.used_in_fix_mask=ntohl(data[77]);
630 update_gps_svstatus(&_gps_state.
633 } else if ( !memcmp(tok.p, "RMC", 3) ) {
634 Token tok_time = nmea_tokenizer_get(tzer,1);
635 Token tok_fixStatus = nmea_tokenizer_get(tzer,2);
636 Token tok_latitude = nmea_tokenizer_get(tzer,3);
637 Token tok_latitudeHemi = nmea_tokenizer_get(tzer,4);
638 Token tok_longitude = nmea_tokenizer_get(tzer,5);
639 Token tok_longitudeHemi = nmea_tokenizer_get(tzer,6);
640 Token tok_speed = nmea_tokenizer_get(tzer,7);
641 Token tok_bearing = nmea_tokenizer_get(tzer,8);
642 Token tok_date = nmea_tokenizer_get(tzer,9);
644 D("in RMC, fixStatus=%c", tok_fixStatus.p[0]);
645 if (tok_fixStatus.p[0] == 'A') {
646 nmea_reader_update_date( r, tok_date, tok_time );
648 nmea_reader_update_latlong( r, tok_latitude,
649 tok_latitudeHemi.p[0],
651 tok_longitudeHemi.p[0] );
653 nmea_reader_update_bearing( r, tok_bearing );
654 nmea_reader_update_speed ( r, tok_speed );
656 } else if ( !memcmp(tok.p, "VTG", 3) ) {
657 Token tok_fixStatus = nmea_tokenizer_get(tzer,9);
659 if (tok_fixStatus.p[0] != '\0' && tok_fixStatus.p[0] != 'N') {
660 Token tok_bearing = nmea_tokenizer_get(tzer,1);
661 Token tok_speed = nmea_tokenizer_get(tzer,5);
663 nmea_reader_update_bearing( r, tok_bearing );
664 nmea_reader_update_speed ( r, tok_speed );
668 D("unknown sentence '%.*s", tok.end-tok.p, tok.p);
677 char* end = p + sizeof(temp);
680 p += snprintf( p, end-p, "sending fix" );
681 if (r->fix.flags & GPS_LOCATION_HAS_LAT_LONG) {
682 p += snprintf(p, end-p, " lat=%g lon=%g", r->fix.latitude, r->fix.longitude);
684 if (r->fix.flags & GPS_LOCATION_HAS_ALTITUDE) {
685 p += snprintf(p, end-p, " altitude=%g", r->fix.altitude);
687 if (r->fix.flags & GPS_LOCATION_HAS_SPEED) {
688 p += snprintf(p, end-p, " speed=%g", r->fix.speed);
690 if (r->fix.flags & GPS_LOCATION_HAS_BEARING) {
691 p += snprintf(p, end-p, " bearing=%g", r->fix.bearing);
693 if (r->fix.flags & GPS_LOCATION_HAS_ACCURACY) {
694 p += snprintf(p,end-p, " accuracy=%g", r->fix.accuracy);
696 gmtime_r( (time_t*) &r->fix.timestamp, &utc );
697 p += snprintf(p, end-p, " time=%s", asctime( &utc ) );
701 if (r->fix.flags & GPS_LOCATION_HAS_ACCURACY) {
702 if (_gps_state->callbacks->location_cb) {
703 _gps_state->callbacks->location_cb( &r->fix );
706 D("no callback, keeping data until needed !");
713 nmea_reader_addc( NmeaReader* r, int c )
716 r->overflow = (c != '\n');
720 if (r->pos >= (int) sizeof(r->in)-1 ) {
726 r->in[r->pos] = (char)c;
730 nmea_reader_parse( r );
736 /*****************************************************************/
737 /*****************************************************************/
739 /***** C O N N E C T I O N S T A T E *****/
741 /*****************************************************************/
742 /*****************************************************************/
744 /* commands sent to the gps thread */
752 gps_state_done( GpsState* s )
754 // tell the thread to quit, and wait for it
757 write( s->control[0], &cmd, 1 );
758 pthread_join(s->thread, &dummy);
760 // close the control socket pair
761 close( s->control[0] ); s->control[0] = -1;
762 close( s->control[1] ); s->control[1] = -1;
764 // close connection to the QEMU GPS daemon
765 close( s->fd ); s->fd = -1;
771 gps_state_start( GpsState* s )
773 char cmd = CMD_START;
777 ret = write( s->control[0], &cmd, 1 );
778 } while (ret < 0 && errno == EINTR);
781 D("%s: could not send CMD_START command: ret=%d: %s",
782 __FUNCTION__, ret, strerror(errno));
787 gps_state_stop( GpsState* s )
792 do { ret=write( s->control[0], &cmd, 1 ); }
793 while (ret < 0 && errno == EINTR);
796 D("%s: could not send CMD_STOP command: ret=%d: %s",
797 __FUNCTION__, ret, strerror(errno));
802 epoll_register( int epoll_fd, int fd )
804 struct epoll_event ev;
807 /* important: make the fd non-blocking */
808 flags = fcntl(fd, F_GETFL);
809 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
814 ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev );
815 } while (ret < 0 && errno == EINTR);
821 epoll_deregister( int epoll_fd, int fd )
825 ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NULL );
826 } while (ret < 0 && errno == EINTR);
830 /* this is the main thread, it waits for commands from gps_state_start/stop and,
831 * when started, messages from the QEMU GPS daemon. these are simple NMEA sentences
832 * that must be parsed to be converted into GPS fixes sent to the framework
835 gps_state_thread( void* arg )
837 GpsState* state = (GpsState*) arg;
838 NmeaReader reader[1];
839 int epoll_fd = epoll_create(2);
841 int gps_fd = state->fd;
842 int control_fd = state->control[1];
844 nmea_reader_init( reader );
846 // register control file descriptors for polling
847 epoll_register( epoll_fd, control_fd );
848 epoll_register( epoll_fd, gps_fd );
850 D("gps thread running");
854 struct epoll_event events[2];
857 nevents = epoll_wait( epoll_fd, events, 2, -1 );
860 ALOGE("epoll_wait() unexpected error: %s", strerror(errno));
863 for (ne = 0; ne < nevents; ne++) {
864 if ((events[ne].events & (EPOLLERR|EPOLLHUP)) != 0) {
865 ALOGE("EPOLLERR or EPOLLHUP after epoll_wait() !?");
868 if ((events[ne].events & EPOLLIN) != 0) {
869 int fd = events[ne].data.fd;
871 if (fd == control_fd) {
874 D("gps control fd event");
876 ret = read( fd, &cmd, 1 );
877 } while (ret < 0 && errno == EINTR);
879 if (cmd == CMD_QUIT) {
880 D("gps thread quitting on demand");
882 } else if (cmd == CMD_START) {
884 D("gps thread starting location_cb=%p", state->callbacks->location_cb);
886 //nmea_reader_set_callback( reader, state->callbacks.location_cb );
887 update_gps_status(GPS_STATUS_SESSION_BEGIN);
889 } else if (cmd == CMD_STOP) {
891 D("gps thread stopping");
893 //nmea_reader_set_callback( reader, NULL );
894 update_gps_status(GPS_STATUS_SESSION_END);
897 } else if (fd == gps_fd) {
902 ret = read( fd, buff, sizeof(buff) );
906 if (errno != EWOULDBLOCK)
907 ALOGE("error while reading from gps daemon socket: %s:", strerror(errno));
910 for (nn = 0; nn < ret; nn++)
911 nmea_reader_addc( reader, buff[nn] );
914 ALOGE("epoll_wait() returned unkown fd %d ?", fd);
923 gps_state_init( GpsState* state, GpsCallbacks* callbacks )
925 char prop[PROPERTY_VALUE_MAX];
926 char baud[PROPERTY_VALUE_MAX];
931 struct sigevent tmr_event;
934 state->control[0] = -1;
935 state->control[1] = -1;
937 state->callbacks = callbacks;
940 // look for a kernel-provided device name
942 if (property_get("ro.kernel.android.gps",prop,"") == 0) {
943 D("no kernel-provided gps device name");
947 snprintf(device, sizeof(device), "/dev/%s",prop);
949 state->fd = open( device, O_RDWR );
950 } while (state->fd < 0 && errno == EINTR);
953 ALOGE("could not open gps serial device %s: %s", device, strerror(errno) );
957 D("gps will read from %s", device);
959 // disable echo on serial lines
960 if ( isatty( state->fd ) ) {
962 tcgetattr( state->fd, &ios );
963 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
964 ios.c_oflag &= (~ONLCR); /* Stop \n -> \r\n translation on output */
965 ios.c_iflag &= (~(ICRNL | INLCR)); /* Stop \r -> \n & \n -> \r translation on input */
966 ios.c_iflag |= (IGNCR | IXOFF); /* Ignore \r & XON/XOFF on input */
967 // set baud rate and other flags
968 property_get("ro.kernel.android.gpsttybaud",baud,"9600");
969 if (strcmp(baud, "9600") == 0) {
970 ALOGE("setting gps baud rate to 9600");
971 ios.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
972 } else if (strcmp(baud, "19200") == 0) {
973 ALOGE("setting gps baud rate to 19200");
974 ios.c_cflag = B19200 | CRTSCTS | CS8 | CLOCAL | CREAD;
975 } else if (strcmp(baud, "38400") == 0) {
976 ALOGE("setting gps baud rate to 38400");
977 ios.c_cflag = B38400 | CRTSCTS | CS8 | CLOCAL | CREAD;
978 } else if (strcmp(baud, "57600") == 0) {
979 ALOGE("setting gps baud rate to 57600");
980 ios.c_cflag = B57600 | CRTSCTS | CS8 | CLOCAL | CREAD;
981 } else if (strcmp(baud, "115200") == 0) {
982 ALOGE("setting gps baud rate to 115200");
983 ios.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
985 ALOGE("gps baud rate unknown: '%s'", baud);
989 tcsetattr( state->fd, TCSANOW, &ios );
992 if ( socketpair( AF_LOCAL, SOCK_STREAM, 0, state->control ) < 0 ) {
993 ALOGE("could not create thread control socket pair: %s", strerror(errno));
997 state->thread = callbacks->create_thread_cb( "gps_state_thread", gps_state_thread, state );
999 if ( !state->thread ) {
1000 ALOGE("could not create gps thread: %s", strerror(errno));
1004 D("gps state initialized");
1009 gps_state_done( state );
1013 /*****************************************************************/
1014 /*****************************************************************/
1016 /***** I N T E R F A C E *****/
1018 /*****************************************************************/
1019 /*****************************************************************/
1023 serial_gps_init(GpsCallbacks* callbacks)
1025 D("serial_gps_init");
1026 GpsState* s = _gps_state;
1029 gps_state_init(s, callbacks);
1038 serial_gps_cleanup(void)
1040 GpsState* s = _gps_state;
1050 GpsState* s = _gps_state;
1053 DFR("%s: called with uninitialized state !!", __FUNCTION__);
1057 D("%s: called", __FUNCTION__);
1066 GpsState* s = _gps_state;
1069 DFR("%s: called with uninitialized state !!", __FUNCTION__);
1073 D("%s: called", __FUNCTION__);
1080 serial_gps_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty)
1087 serial_gps_inject_location(double latitude, double longitude, float accuracy)
1093 serial_gps_delete_aiding_data(GpsAidingData flags)
1097 static int serial_gps_set_position_mode(GpsPositionMode mode, GpsPositionRecurrence recurrence,
1098 uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time)
1100 GpsState* s = _gps_state;
1103 D("%s: called with uninitialized state !!", __FUNCTION__);
1107 D("set_position_mode: mode=%d recurrence=%d min_interval=%d preferred_accuracy=%d preferred_time=%d",
1108 mode, recurrence, min_interval, preferred_accuracy, preferred_time);
1114 serial_gps_get_extension(const char* name)
1119 static const GpsInterface serialGpsInterface = {
1120 sizeof(GpsInterface),
1125 serial_gps_inject_time,
1126 serial_gps_inject_location,
1127 serial_gps_delete_aiding_data,
1128 serial_gps_set_position_mode,
1129 serial_gps_get_extension,
1132 const GpsInterface* gps_get_hardware_interface()
1134 D("gps dev get_hardware_interface");
1135 return &serialGpsInterface;
1140 /*****************************************************************/
1141 /*****************************************************************/
1143 /***** D E V I C E *****/
1145 /*****************************************************************/
1146 /*****************************************************************/
1148 static void gps_dev_power(int state)
1150 char prop[PROPERTY_VALUE_MAX];
1155 if (property_get("gps.power_on",prop,GPS_POWER_IF) == 0) {
1156 ALOGE("no gps power interface name");
1161 fd = open( prop, O_RDWR );
1162 } while (fd < 0 && errno == EINTR);
1165 ALOGE("could not open gps power interfcae: %s", prop );
1176 ret = write( fd, &cmd, 1 );
1177 } while (ret < 0 && errno == EINTR);
1181 DFR("gps power state = %c", cmd);
1186 static void gps_dev_send(int fd, char *msg)
1196 ret = write(fd, msg + n, i - n);
1198 if (ret < 0 && errno == EINTR) {
1207 static unsigned char gps_dev_calc_nmea_csum(char *msg)
1209 unsigned char csum = 0;
1212 for (i = 1; msg[i] != '*'; ++i) {
1219 static void gps_dev_set_nmea_message_rate(int fd, char *msg, int rate)
1224 sprintf(buff, "$PUBX,40,%s,%d,%d,%d,0*", msg, rate, rate, rate);
1228 sprintf((buff + i), "%02x\r\n", gps_dev_calc_nmea_csum(buff));
1230 gps_dev_send(fd, buff);
1232 D("gps sent to device: %s", buff);
1235 static void gps_dev_set_baud_rate(int fd, int baud)
1240 for (u = 0; u < 3; ++u) {
1242 sprintf(buff, "$PUBX,41,%d,0003,0003,%d,0*", u, baud);
1246 sprintf((buff + i), "%02x\r\n", gps_dev_calc_nmea_csum(buff));
1248 gps_dev_send(fd, buff);
1250 D("gps sent to device: %s", buff);
1255 static void gps_dev_set_message_rate(int fd, int rate)
1261 "GGA", "GLL", "VTG",
1265 for (i = 0; i < sizeof(msg)/sizeof(msg[0]); ++i) {
1266 gps_dev_set_nmea_message_rate(fd, msg[i], rate);
1272 static void gps_dev_init(int fd)
1276 //usleep(1000*1000);
1278 // To set to STOP state
1284 static void gps_dev_deinit(int fd)
1289 static void gps_dev_start(int fd)
1291 // Set full message rate
1292 gps_dev_set_message_rate(fd, GPS_DEV_HIGH_UPDATE_RATE);
1294 D("gps dev start initiated");
1297 static void gps_dev_stop(int fd)
1299 // Set slow message rate
1300 gps_dev_set_message_rate(fd, GPS_DEV_SLOW_UPDATE_RATE);
1302 D("gps dev stop initiated");
1305 static int open_gps(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
1307 D("gps dev open_gps");
1308 struct gps_device_t *dev = malloc(sizeof(struct gps_device_t));
1309 memset(dev, 0, sizeof(*dev));
1311 dev->common.tag = HARDWARE_DEVICE_TAG;
1312 dev->common.version = 0;
1313 dev->common.module = (struct hw_module_t*)module;
1314 // dev->common.close = (int (*)(struct hw_device_t*))close_lights;
1315 dev->get_gps_interface = gps_get_hardware_interface;
1317 *device = &dev->common;
1322 static struct hw_module_methods_t gps_module_methods = {
1326 struct hw_module_t HAL_MODULE_INFO_SYM = {
1327 .tag = HARDWARE_MODULE_TAG,
1330 .id = GPS_HARDWARE_MODULE_ID,
1331 .name = "Serial GPS Module",
1332 .author = "The Android-x86 Open Source Project",
1333 .methods = &gps_module_methods,