forked from BlissRoms-x86/platform_hardware_gps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps_huawei.c
1784 lines (1374 loc) · 45.5 KB
/
gps_huawei.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**
** Copyright 2006, The Android Open Source Project
** Copyright 2009, Michael Trimarchi <[email protected]>
** Copyright 2011, Eduardo José Tagle <[email protected]>
**
** This program is free software; you can redistribute it and/or modify it under
** the terms of the GNU General Public License as published by the Free
** Software Foundation; either version 2, or (at your option) any later
** version.
**
** This program is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
** more details.
**
** You should have received a copy of the GNU General Public License along with
** this program; if not, write to the Free Software Foundation, Inc., 59
** Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/
#include <errno.h>
#include <pthread.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <math.h>
#include <time.h>
#include <semaphore.h>
#include <signal.h>
#include <unistd.h>
#include <linux/socket.h>
#include <sys/socket.h>
#define LOG_TAG "gps_huawei"
#include <cutils/log.h>
#include <cutils/sockets.h>
#include <cutils/properties.h>
#include <hardware/gps.h>
#if GPS_DEBUG
# define D(...) ALOGD(__VA_ARGS__)
#else
# define D(...) ((void)0)
#endif
typedef void (*start_t)(void*);
/* Nmea Parser stuff */
#define NMEA_MAX_SIZE 200
enum {
STATE_QUIT = 0,
STATE_INIT = 1,
STATE_START = 2
};
typedef struct {
int pos;
int overflow;
int utc_year;
int utc_mon;
int utc_day;
int utc_diff;
GpsLocation fix;
GpsSvStatus sv_status;
int sv_status_changed;
char in[ NMEA_MAX_SIZE+1 ];
} NmeaReader;
typedef struct {
volatile int init;
int fd;
int ctrl_fd;
GpsCallbacks callbacks;
AGpsCallbacks a_callbacks;
GpsXtraCallbacks xtra_callbacks;
GpsStatus gps_status;
char nmea_buf[512];
int nmea_len;
pthread_t thread;
sem_t fix_sem;
pthread_t tmr_thread;
int control[2];
int min_interval; // in ms
NmeaReader reader;
} GpsState;
/* Since NMEA parser requires lcoks */
#define GPS_STATE_LOCK_FIX(_s) \
{ \
int ret; \
do { \
ret = sem_wait(&(_s)->fix_sem); \
} while (ret < 0 && errno == EINTR); \
}
#define GPS_STATE_UNLOCK_FIX(_s) \
sem_post(&(_s)->fix_sem)
static GpsState _gps_state[1];
static GpsState *gps_state = _gps_state;
#define GPS_POWER_IF "/sys/bus/platform/devices/shuttle-pm-gps/power_on"
#define GPS_DEV_SLOW_UPDATE_RATE (10)
#define GPS_DEV_HIGH_UPDATE_RATE (1)
static void dev_start(int fd);
static void dev_stop(int fd);
static void *gps_timer_thread( void* arg );
static void serial_write(int fd, char *msg)
{
int i, n, ret;
i = strlen(msg);
n = 0;
do {
ret = write(fd, msg + n, i - n);
if (ret < 0 && errno == EINTR) {
continue;
}
n += ret;
} while (n < i);
return;
}
static unsigned char calc_nmea_csum(char *msg)
{
unsigned char csum = 0;
int i;
for (i = 1; msg[i] != '*'; ++i) {
csum ^= msg[i];
}
return csum;
}
/*****************************************************************/
/*****************************************************************/
/***** *****/
/***** D E V I C E *****/
/***** *****/
/*****************************************************************/
/*****************************************************************/
static void dev_power(int state)
{
char prop[PROPERTY_VALUE_MAX];
int fd;
char cmd = '0';
int ret;
return ; //RvdB
if (property_get("gps.power_on",prop,GPS_POWER_IF) == 0) {
ALOGE("no gps power interface name");
return;
}
do {
fd = open( prop, O_RDWR );
} while (fd < 0 && errno == EINTR);
if (fd < 0) {
ALOGE("could not open gps power interface: %s", prop );
return;
}
if (state) {
cmd = '1';
} else {
cmd = '0';
}
do {
ret = write( fd, &cmd, 1 );
} while (ret < 0 && errno == EINTR);
close(fd);
D("gps power state = %c", cmd);
if (state)
usleep(500*1000);
return;
}
static void dev_set_nmea_message_rate(int fd,const char *msg, int rate)
{
char buff[50];
int i;
sprintf(buff, "$PUBX,40,%s,%d,%d,%d,0*", msg, rate, rate, rate);
i = strlen(buff);
sprintf((buff + i), "%02x\r\n", calc_nmea_csum(buff));
serial_write(fd, buff);
D("gps sent to device: %s", buff);
return;
}
static void dev_set_message_rate(int fd, int rate)
{
unsigned int i;
static const char *msg[] = {
"GGA", "GLL", "ZDA",
"VTG", "GSA", "GSV",
"RMC"
};
for (i = 0; i < sizeof(msg)/sizeof(msg[0]); ++i) {
dev_set_nmea_message_rate(fd, msg[i], rate);
}
return;
}
static void dev_start(int fd)
{
D("gps dev start initiated");
// Set full message rate
//RvdB dev_set_message_rate(fd, GPS_DEV_HIGH_UPDATE_RATE);
}
static void dev_stop(int fd)
{
D("gps dev stop");
// Set slow message rate
//RvdB dev_set_message_rate(fd, GPS_DEV_SLOW_UPDATE_RATE);
}
/*****************************************************************/
/*****************************************************************/
/***** *****/
/***** N M E A T O K E N I Z E R *****/
/***** *****/
/*****************************************************************/
/*****************************************************************/
typedef struct {
const char* p;
const char* end;
} Token;
#define MAX_NMEA_TOKENS 32
typedef struct {
int count;
Token tokens[ MAX_NMEA_TOKENS ];
} NmeaTokenizer;
static int nmea_tokenizer_init( NmeaTokenizer* t, const char* p, const char* end )
{
int count = 0;
char* q;
// the initial '$' is optional
if (p < end && p[0] == '$')
p += 1;
// remove trailing newline
if (end > p && end[-1] == '\n') {
end -= 1;
if (end > p && end[-1] == '\r')
end -= 1;
}
// get rid of checksum at the end of the sentecne
if (end >= p+3 && end[-3] == '*') {
end -= 3;
}
while (p < end) {
const char* q = p;
q = memchr(p, ',', end-p);
if (q == NULL)
q = end;
if (count < MAX_NMEA_TOKENS) {
t->tokens[count].p = p;
t->tokens[count].end = q;
count += 1;
}
if (q < end)
q += 1;
p = q;
}
t->count = count;
return count;
}
static Token nmea_tokenizer_get( NmeaTokenizer* t, int index )
{
Token tok;
static const char* dummy = "";
if (index < 0 || index >= t->count) {
tok.p = tok.end = dummy;
} else
tok = t->tokens[index];
return tok;
}
static int str2int( const char* p, const char* end )
{
int result = 0;
int len = end - p;
if (len == 0) {
return -1;
}
for ( ; len > 0; len--, p++ )
{
int c;
if (p >= end)
goto Fail;
c = *p - '0';
if ((unsigned)c >= 10)
goto Fail;
result = result*10 + c;
}
return result;
Fail:
return -1;
}
static double str2float( const char* p, const char* end )
{
int result = 0;
int len = end - p;
char temp[16];
if (len == 0) {
return -1.0;
}
if (len >= (int)sizeof(temp))
return 0.;
memcpy( temp, p, len );
temp[len] = 0;
return strtod( temp, NULL );
}
/** @desc Convert struct tm to time_t (time zone neutral).
*
* The one missing function in libc: It works basically like mktime, with the main difference that
* it does no time zone-related processing but interprets the members of the struct tm as UTC.
* Unlike mktime, it will not modify any fields of the tm structure; if you need this behavior, call
* mktime before this function.
*
* @param t Pointer to a struct tm containing date and time. Only the tm_year, tm_mon, tm_mday,
* tm_hour, tm_min and tm_sec members will be evaluated, all others will be ignored.
*
* @return The epoch time (seconds since 1970-01-01 00:00:00 UTC) which corresponds to t.
*
* @author Originally written by Philippe De Muyter <[email protected]> for Lynx.
* http://lynx.isc.org/current/lynx2-8-8/src/mktime.c
*/
static time_t mkgmtime(struct tm *t)
{
short month, year;
time_t result;
static const int m_to_d[12] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
month = t->tm_mon;
year = t->tm_year + month / 12 + 1900;
month %= 12;
if (month < 0) {
year -= 1;
month += 12;
}
result = (year - 1970) * 365 + m_to_d[month];
if (month <= 1)
year -= 1;
result += (year - 1968) / 4;
result -= (year - 1900) / 100;
result += (year - 1600) / 400;
result += t->tm_mday;
result -= 1;
result *= 24;
result += t->tm_hour;
result *= 60;
result += t->tm_min;
result *= 60;
result += t->tm_sec;
return (result);
}
/*****************************************************************/
/*****************************************************************/
/***** *****/
/***** N M E A P A R S E R *****/
/***** *****/
/*****************************************************************/
/*****************************************************************/
static void nmea_reader_update_utc_diff( NmeaReader* r )
{
time_t now = time(NULL);
struct tm tm_local;
struct tm tm_utc;
long time_local, time_utc;
gmtime_r( &now, &tm_utc );
localtime_r( &now, &tm_local );
time_local = mktime(&tm_local);
time_utc = mktime(&tm_utc);
r->utc_diff = time_local - time_utc;
}
static void nmea_reader_init( NmeaReader* r )
{
int i;
memset( r, 0, sizeof(*r) );
// Initialize the sizes of all the structs we use
r->fix.size = sizeof(GpsLocation);
r->sv_status.size = sizeof(GpsSvStatus);
for (i = 0; i < GPS_MAX_SVS; i++) {
r->sv_status.sv_list[i].size = sizeof(GpsSvInfo);
}
r->pos = 0;
r->overflow = 0;
r->utc_year = -1;
r->utc_mon = -1;
r->utc_day = -1;
// not sure if we still need this (this module doesn't use utc_diff)
nmea_reader_update_utc_diff( r );
}
static int nmea_reader_update_time( NmeaReader* r, Token tok )
{
int hour, minute, seconds, milliseconds;
struct tm tm;
time_t fix_time;
if (tok.p + 6 > tok.end)
return -1;
if (r->utc_year < 0) {
// no date, can't return valid timestamp (never ever make up a date, this could wreak havoc)
return -1;
}
else
{
tm.tm_year = r->utc_year - 1900;
tm.tm_mon = r->utc_mon - 1;
tm.tm_mday = r->utc_day;
}
hour = str2int(tok.p, tok.p+2);
minute = str2int(tok.p+2, tok.p+4);
seconds = str2int(tok.p+4, tok.p+6);
// parse also milliseconds (if present) for better precision
milliseconds = 0;
if (tok.end - (tok.p+7) == 2) {
milliseconds = str2int(tok.p+7, tok.end) * 10;
}
else if (tok.end - (tok.p+7) == 1) {
milliseconds = str2int(tok.p+7, tok.end) * 100;
}
else if (tok.end - (tok.p+7) >= 3) {
milliseconds = str2int(tok.p+7, tok.p+10);
}
// the following is only guaranteed to work if we have previously set a correct date, so be sure
// to always do that before
tm.tm_hour = hour;
tm.tm_min = minute;
tm.tm_sec = seconds;
fix_time = mkgmtime( &tm );
r->fix.timestamp = (long long)fix_time * 1000 + milliseconds;
return 0;
}
static int nmea_reader_update_cdate( NmeaReader* r, Token tok_d, Token tok_m, Token tok_y )
{
if ( (tok_d.p + 2 > tok_d.end) ||
(tok_m.p + 2 > tok_m.end) ||
(tok_y.p + 4 > tok_y.end) )
return -1;
r->utc_day = str2int(tok_d.p, tok_d.p+2);
r->utc_mon = str2int(tok_m.p, tok_m.p+2);
r->utc_year = str2int(tok_y.p, tok_y.p+4);
return 0;
}
static int nmea_reader_update_date( NmeaReader* r, Token date, Token time )
{
Token tok = date;
int day, mon, year;
if (tok.p + 6 != tok.end) {
D("date not properly formatted: '%.*s'", tok.end-tok.p, tok.p);
return -1;
}
day = str2int(tok.p, tok.p+2);
mon = str2int(tok.p+2, tok.p+4);
year = str2int(tok.p+4, tok.p+6) + 2000;
if ((day|mon|year) < 0) {
D("date not properly formatted: '%.*s'", tok.end-tok.p, tok.p);
return -1;
}
r->utc_year = year;
r->utc_mon = mon;
r->utc_day = day;
return nmea_reader_update_time( r, time );
}
static double convert_from_hhmm( Token tok )
{
double val = str2float(tok.p, tok.end);
int degrees = (int)(floor(val) / 100);
double minutes = val - degrees*100.;
double dcoord = degrees + minutes / 60.0;
return dcoord;
}
static int nmea_reader_update_latlong( NmeaReader* r,
Token latitude,
char latitudeHemi,
Token longitude,
char longitudeHemi )
{
double lat, lon;
Token tok;
tok = latitude;
if (tok.p + 6 > tok.end) {
D("latitude is too short: '%.*s'", tok.end-tok.p, tok.p);
return -1;
}
lat = convert_from_hhmm(tok);
if (latitudeHemi == 'S')
lat = -lat;
tok = longitude;
if (tok.p + 6 > tok.end) {
D("longitude is too short: '%.*s'", tok.end-tok.p, tok.p);
return -1;
}
lon = convert_from_hhmm(tok);
if (longitudeHemi == 'W')
lon = -lon;
r->fix.flags |= GPS_LOCATION_HAS_LAT_LONG;
r->fix.latitude = lat;
r->fix.longitude = lon;
return 0;
}
static int nmea_reader_update_altitude( NmeaReader* r,
Token altitude,
Token units )
{
double alt;
Token tok = altitude;
if (tok.p >= tok.end)
return -1;
r->fix.flags |= GPS_LOCATION_HAS_ALTITUDE;
r->fix.altitude = str2float(tok.p, tok.end);
return 0;
}
static int nmea_reader_update_accuracy( NmeaReader* r,
Token accuracy )
{
double acc;
Token tok = accuracy;
if (tok.p >= tok.end)
return -1;
r->fix.accuracy = str2float(tok.p, tok.end);
if (r->fix.accuracy == 99.99){
return 0;
}
r->fix.flags |= GPS_LOCATION_HAS_ACCURACY;
return 0;
}
static int nmea_reader_update_bearing( NmeaReader* r,
Token bearing )
{
double alt;
Token tok = bearing;
if (tok.p >= tok.end)
return -1;
r->fix.flags |= GPS_LOCATION_HAS_BEARING;
r->fix.bearing = str2float(tok.p, tok.end);
return 0;
}
static int nmea_reader_update_speed( NmeaReader* r,
Token speed )
{
double alt;
Token tok = speed;
if (tok.p >= tok.end)
return -1;
r->fix.flags |= GPS_LOCATION_HAS_SPEED;
// convert knots into m/sec (1 knot equals 1.852 km/h, 1 km/h equals 3.6 m/s)
// since 1.852 / 3.6 is an odd value (periodic), we're calculating the quotient on the fly
// to obtain maximum precision (we don't want 1.9999 instead of 2)
r->fix.speed = str2float(tok.p, tok.end) * 1.852 / 3.6;
return 0;
}
static void nmea_reader_parse( NmeaReader* r )
{
/* we received a complete sentence, now parse it to generate
* a new GPS fix...
*/
NmeaTokenizer tzer[1];
Token tok;
D("Received: '%.*s'", r->pos, r->in);
if (r->pos < 9) {
D("Too short. discarded.");
return;
}
nmea_tokenizer_init(tzer, r->in, r->in + r->pos);
#if GPS_DEBUG
{
int n;
D("Found %d tokens", tzer->count);
for (n = 0; n < tzer->count; n++) {
Token tok = nmea_tokenizer_get(tzer,n);
D("%2d: '%.*s'", n, tok.end-tok.p, tok.p);
}
}
#endif
tok = nmea_tokenizer_get(tzer, 0);
if (tok.p + 5 > tok.end) {
D("sentence id '%.*s' too short, ignored.", tok.end-tok.p, tok.p);
return;
}
// ignore first two characters.
tok.p += 2;
if ( !memcmp(tok.p, "GGA", 3) ) {
// GPS fix
Token tok_fixstaus = nmea_tokenizer_get(tzer,6);
if ((tok_fixstaus.p[0] > '0') && (r->utc_year >= 0)) {
// ignore this until we have a valid timestamp
Token tok_time = nmea_tokenizer_get(tzer,1);
Token tok_latitude = nmea_tokenizer_get(tzer,2);
Token tok_latitudeHemi = nmea_tokenizer_get(tzer,3);
Token tok_longitude = nmea_tokenizer_get(tzer,4);
Token tok_longitudeHemi = nmea_tokenizer_get(tzer,5);
Token tok_altitude = nmea_tokenizer_get(tzer,9);
Token tok_altitudeUnits = nmea_tokenizer_get(tzer,10);
// don't use this as we have no fractional seconds and no date; there are better ways to
// get a good timestamp from GPS
//nmea_reader_update_time(r, tok_time);
nmea_reader_update_latlong(r, tok_latitude,
tok_latitudeHemi.p[0],
tok_longitude,
tok_longitudeHemi.p[0]);
nmea_reader_update_altitude(r, tok_altitude, tok_altitudeUnits);
}
} else if ( !memcmp(tok.p, "GLL", 3) ) {
Token tok_fixstaus = nmea_tokenizer_get(tzer,6);
if ((tok_fixstaus.p[0] == 'A') && (r->utc_year >= 0)) {
// ignore this until we have a valid timestamp
Token tok_latitude = nmea_tokenizer_get(tzer,1);
Token tok_latitudeHemi = nmea_tokenizer_get(tzer,2);
Token tok_longitude = nmea_tokenizer_get(tzer,3);
Token tok_longitudeHemi = nmea_tokenizer_get(tzer,4);
Token tok_time = nmea_tokenizer_get(tzer,5);
// don't use this as we have no fractional seconds and no date; there are better ways to
// get a good timestamp from GPS
//nmea_reader_update_time(r, tok_time);
nmea_reader_update_latlong(r, tok_latitude,
tok_latitudeHemi.p[0],
tok_longitude,
tok_longitudeHemi.p[0]);
}
} else if ( !memcmp(tok.p, "GSA", 3) ) {
Token tok_fixStatus = nmea_tokenizer_get(tzer, 2);
int i;
if (tok_fixStatus.p[0] != '\0' && tok_fixStatus.p[0] != '1') {
Token tok_accuracy = nmea_tokenizer_get(tzer, 15);
nmea_reader_update_accuracy(r, tok_accuracy);
r->sv_status.used_in_fix_mask = 0ul;
for (i = 3; i <= 14; ++i){
Token tok_prn = nmea_tokenizer_get(tzer, i);
int prn = str2int(tok_prn.p, tok_prn.end);
if (prn > 0){
r->sv_status.used_in_fix_mask |= (1ul << (32 - prn));
r->sv_status_changed = 1;
D("%s: fix mask is %d", __FUNCTION__, r->sv_status.used_in_fix_mask);
}
}
}
} else if ( !memcmp(tok.p, "GSV", 3) ) {
Token tok_noSatellites = nmea_tokenizer_get(tzer, 3);
int noSatellites = str2int(tok_noSatellites.p, tok_noSatellites.end);
if (noSatellites > 0) {
Token tok_noSentences = nmea_tokenizer_get(tzer, 1);
Token tok_sentence = nmea_tokenizer_get(tzer, 2);
int sentence = str2int(tok_sentence.p, tok_sentence.end);
int totalSentences = str2int(tok_noSentences.p, tok_noSentences.end);
int curr;
int i;
if (sentence == 1) {
r->sv_status_changed = 0;
r->sv_status.num_svs = 0;
}
curr = r->sv_status.num_svs;
i = 0;
while (i < 4 && r->sv_status.num_svs < noSatellites){
Token tok_prn = nmea_tokenizer_get(tzer, i * 4 + 4);
Token tok_elevation = nmea_tokenizer_get(tzer, i * 4 + 5);
Token tok_azimuth = nmea_tokenizer_get(tzer, i * 4 + 6);
Token tok_snr = nmea_tokenizer_get(tzer, i * 4 + 7);
r->sv_status.sv_list[curr].prn = str2int(tok_prn.p, tok_prn.end);
r->sv_status.sv_list[curr].elevation = str2float(tok_elevation.p, tok_elevation.end);
r->sv_status.sv_list[curr].azimuth = str2float(tok_azimuth.p, tok_azimuth.end);
r->sv_status.sv_list[curr].snr = str2float(tok_snr.p, tok_snr.end);
r->sv_status.num_svs += 1;
curr += 1;
i += 1;
}
if (sentence == totalSentences) {
r->sv_status_changed = 1;
}
D("%s: GSV message with total satellites %d", __FUNCTION__, noSatellites);
}
} else if ( !memcmp(tok.p, "RMC", 3) ) {
Token tok_fixStatus = nmea_tokenizer_get(tzer,2);
if (tok_fixStatus.p[0] == 'A')
{
Token tok_time = nmea_tokenizer_get(tzer,1);
Token tok_latitude = nmea_tokenizer_get(tzer,3);
Token tok_latitudeHemi = nmea_tokenizer_get(tzer,4);
Token tok_longitude = nmea_tokenizer_get(tzer,5);
Token tok_longitudeHemi = nmea_tokenizer_get(tzer,6);
Token tok_speed = nmea_tokenizer_get(tzer,7);
Token tok_bearing = nmea_tokenizer_get(tzer,8);
Token tok_date = nmea_tokenizer_get(tzer,9);
nmea_reader_update_date( r, tok_date, tok_time );
nmea_reader_update_latlong( r, tok_latitude,
tok_latitudeHemi.p[0],
tok_longitude,
tok_longitudeHemi.p[0] );
nmea_reader_update_bearing( r, tok_bearing );
nmea_reader_update_speed ( r, tok_speed );
}
} else if ( !memcmp(tok.p, "VTG", 3) ) {
Token tok_fixStatus = nmea_tokenizer_get(tzer,9);
if (tok_fixStatus.p[0] != '\0' && tok_fixStatus.p[0] != 'N')
{
Token tok_bearing = nmea_tokenizer_get(tzer,1);
Token tok_speed = nmea_tokenizer_get(tzer,5);
nmea_reader_update_bearing( r, tok_bearing );
nmea_reader_update_speed ( r, tok_speed );
}
} else if ( !memcmp(tok.p, "ZDA", 3) ) {
Token tok_time;
Token tok_year = nmea_tokenizer_get(tzer,4);
tok_time = nmea_tokenizer_get(tzer,1);
if ((tok_year.p[0] != '\0') && (tok_time.p[0] != '\0')) {
// make sure to always set date and time together, lest bad things happen
Token tok_day = nmea_tokenizer_get(tzer,2);
Token tok_mon = nmea_tokenizer_get(tzer,3);
nmea_reader_update_cdate( r, tok_day, tok_mon, tok_year );
nmea_reader_update_time(r, tok_time);
}
} else {
tok.p -= 2;
D("unknown sentence '%.*s", tok.end-tok.p, tok.p);
}
#if GPS_DEBUG
if (r->fix.flags != 0) {
char temp[256];
char* p = temp;
char* end = p + sizeof(temp);
struct tm utc;
p += snprintf( p, end-p, "sending fix" );
if (r->fix.flags & GPS_LOCATION_HAS_LAT_LONG) {
p += snprintf(p, end-p, " lat=%g lon=%g", r->fix.latitude, r->fix.longitude);
}
if (r->fix.flags & GPS_LOCATION_HAS_ALTITUDE) {
p += snprintf(p, end-p, " altitude=%g", r->fix.altitude);
}
if (r->fix.flags & GPS_LOCATION_HAS_SPEED) {
p += snprintf(p, end-p, " speed=%g", r->fix.speed);
}
if (r->fix.flags & GPS_LOCATION_HAS_BEARING) {
p += snprintf(p, end-p, " bearing=%g", r->fix.bearing);
}
if (r->fix.flags & GPS_LOCATION_HAS_ACCURACY) {
p += snprintf(p,end-p, " accuracy=%g", r->fix.accuracy);
}
gmtime_r( (time_t*) &r->fix.timestamp, &utc );
p += snprintf(p, end-p, " time=%s", asctime( &utc ) );
ALOGE("%s\n",temp);
}
#endif
}
static void nmea_reader_addc( NmeaReader* r, int c )
{
if (r->overflow) {
r->overflow = (c != '\n');
return;
}
if (r->pos >= (int) sizeof(r->in)-1 ) {
r->overflow = 1;
r->pos = 0;
return;
}
r->in[r->pos] = (char)c;
r->pos += 1;
if (c == '\n') {
nmea_reader_parse( r );
r->pos = 0;
}
}
/*****************************************************************/
/*****************************************************************/
/***** *****/
/***** C O N N E C T I O N S T A T E *****/
/***** *****/
/*****************************************************************/
/*****************************************************************/
/* commands sent to the gps thread */
enum {
CMD_QUIT = 0,
CMD_START = 1,
CMD_STOP = 2
};
static void gps_state_start( GpsState* s )
{
char cmd = CMD_START;
int ret;
do { ret=write( s->control[0], &cmd, 1 ); }
while (ret < 0 && errno == EINTR);
if (ret != 1)
D("%s: could not send CMD_START command: ret=%d: %s",
__FUNCTION__, ret, strerror(errno));
}
static void gps_state_stop( GpsState* s )
{
char cmd = CMD_STOP;
int ret;
do { ret=write( s->control[0], &cmd, 1 ); }
while (ret < 0 && errno == EINTR);
if (ret != 1)
D("%s: could not send CMD_STOP command: ret=%d: %s",
__FUNCTION__, ret, strerror(errno));
}
static int epoll_register( int epoll_fd, int fd )
{