-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtracker_Linux.c
835 lines (726 loc) · 27.8 KB
/
tracker_Linux.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
/*
Copyright (C) 2003-2011 Egon Wanke <[email protected]> 1
This program sends the output of the evaluation boards as udp packets
to the server of blitzortung.org. It supports all firmware version.
If there are any problem with this tracker program, contact me by
email to a [email protected].
Name this file "tracker_Linux.c" and compile it by
> g++ -Wall -lm -o tracker_Linux tracker_Linux.c
and try
> ./tracker_Linux -h
*/
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <math.h>
#include <syslog.h>
long double fabsl (long double x); // this is only necessary for the OpenWrt
// but do not disturb other systems
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
#define VERSION "LT 23" // version string send to server
#define SERVER_ADDR "rechenserver.de" // server address
#define SERVER_PORT 8308 // server port
#define STRING_BUFFER_SIZE 2048 // maximal buffer size for the strings we use
#define RING_BUFFER_SIZE 20 // ring buffer size for averaged computation of counter difference
#define SMOOTH_FACTOR 3600 //
#define POS_PRECISION 0.001000l // position precision in degree to reach before sending data
#define TIME_PRECISION 0.000001l // time precision to reach before sending data
#define MAX_STR_SEC 12 // maximal number of strikes per second
#define MAX_NONZERO_SEC 6 // maximal number of seconds with strikes
#define MIN_SATS 3 // Warn if less than or equal to this
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
// if verbose flag is true, the program outputs read data and log file entries
// on standard output
bool verbose_flag;
// if logfile flag is true, the program outputs information in a logfile
bool logfile_flag= false;
FILE *log_fd;
// if syslog flag is true, the program uses the system logger
bool syslog_flag= false;
// all these global last and last_last variables store the information of the
// last BLSEC sentence
long long last_counter= -1, last_last_counter= -1;
int last_year, last_mon, last_day, last_hour, last_min, last_sec, last_last_sec;
int last_sat, last_last_sat;
int last_channels;
int last_values;
int last_bits;
long double last_lat, last_last_lat;
long double last_lon, last_last_lon;
long double last_alt, last_last_alt;
long long last_nsec;
char last_status= '-', last_last_status;
bool last_pos_ok= false, last_last_pos_ok= false;
bool last_accuracy_ok= false, last_last_accuracy_ok= false;
bool last_sec_ok= false, last_last_sec_ok= false;
bool last_imode= false, last_last_imode= false;
char last_data[STRING_BUFFER_SIZE];
char last_firmware[STRING_BUFFER_SIZE];
long long last_transmition_time= 0ll;
bool last_checksum_error= false;
long long now_time= 0ll;
long long dif_sum= 1;
long double lat_sum;
long double lon_sum;
long double alt_sum;
long double dif_average;
long double lat_average;
long double lon_average;
long double alt_average;
long double lat_average_x;
long double lon_average_x;
long double alt_average_x;
long double dif_precision;
long double lat_precision;
long double lon_precision;
long double alt_precision;
long double time_precision;
int ring_buffer_index= 0;
// the ring buffer is used to compute the averred position and time difference
// between two BLSEC sentences
struct ring_buffer_type {
long long dif;
long double lat;
long double lon;
long double alt; } ring_buffer[RING_BUFFER_SIZE];
int str_sec= 0;
int last_str_sec= 0;
int nonzero_sec= 0;
int baudrate;
char *gps_type;
/******************************************************************************/
/***** initialization string for the gps devices ******************************/
/******************************************************************************/
// initialization for San Jose Navigation moduls, 4800 baud
char init_gps_SANAV[]="\
$PFEC,GPint,GGA01,GLL00,GSA00,GSV00,RMC01,DTM00,VTG00,ZDA00*00\n";
// initialization for Garmin moduls
char init_gps_Garmin[]= "\
$PGRMO,GPGGA,1*00\n\
$PGRMO,GPGSA,0*00\n\
$PGRMO,GPGSV,0*00\n\
$PGRMO,GPRMC,1*00\n\
$PGRMO,GPVTG,0*00\n\
$PGRMO,PGRMM,0*00\n\
$PGRMO,PGRMT,0*00\n\
$PGRMO,PGRME,0*00\n\
$PGRMO,PGRMB,0*00\n\
$PGRMCE*00\n";
char init_gps_Garmin_SBAS_on[]= "$PGRMC1,1,,,,,,,W,,,,,*00\n";
char init_gps_Garmin_SBAS_off[]= "$PGRMC1,1,,,,,,,N,,,,,*00\n";
char init_gps_Garmin_4800[]= "$PGRMC,,,,,,,,,,3,,2,4,*00\n";
char init_gps_Garmin_9600[]= "$PGRMC,,,,,,,,,,4,,2,4,*00\n";
char init_gps_Garmin_19200[]= "$PGRMC,,,,,,,,,,5,,2,4,*00\n";
char init_gps_Garmin_38400[]= "$PGRMC,,,,,,,,,,8,,2,4,*00\n";
// initialization for SiRF moduls
char init_gps_SiRF[]= "\
$PSRF103,00,00,01,01*00\n\
$PSRF103,01,00,00,01*00\n\
$PSRF103,02,00,00,01*00\n\
$PSRF103,03,00,00,01*00\n\
$PSRF103,04,00,01,01*00\n\
$PSRF103,05,00,00,01*00\n\
$PSRF103,06,00,00,01*00\n\
$PSRF103,08,00,00,01*00\n";
char init_gps_SiRF_SBAS_on[]= "$PSRF151,01*00\n";
char init_gps_SiRF_SBAS_off[]= "$PSRF151,00*00\n";
char init_gps_SiRF_4800[]= "$PSRF100,1,4800,8,1,0*00\n";
char init_gps_SiRF_9600[]= "$PSRF100,1,9600,8,1,0*00\n";
char init_gps_SiRF_19200[]= "$PSRF100,1,19200,8,1,0*00\n";
char init_gps_SiRF_38400[]= "$PSRF100,1,38400,8,1,0*00\n";
/******************************************************************************/
/***** time functions *********************************************************/
/******************************************************************************/
// convert utc calender time to epoche nanoseconds
long long utc_ctime_to_ensec (int year, int mon, int day, int hour, int min, long double sec_nsec)
{
int sec= (int)sec_nsec;
long long nsec= (long long)(sec_nsec*1000000000ll)-sec*1000000000ll;
struct tm t;
t.tm_year= year-1900;
t.tm_mon= mon-1;
t.tm_mday= day;
t.tm_hour= hour;
t.tm_min= min;
t.tm_sec= (int)sec;
time_t esec= timegm(&t);
return ((long long)esec*1000000000ll+nsec);
}
// convert epoche nanoseconds to utc calender time
void ensec_to_utc_ctime (long long ensec, int *year, int *mon, int *day, int *hour, int *min, long double *sec_nsec)
{
time_t esec= ensec/1000000000ll;
struct tm *t = gmtime (&esec);
*year= t->tm_year+1900;
*mon= t->tm_mon+1;
*day= t->tm_mday;
*hour= t->tm_hour;
*min= t->tm_min;
*sec_nsec= t->tm_sec+(ensec%1000000000ll)/1000000000.0l;
}
// return epoche nanoseconds
long long ensec_time ()
{
struct timeval t;
gettimeofday (&t,(struct timezone *)0);
return ((long long)(t.tv_sec)*1000000000ll+(long long)t.tv_usec*1000ll);
}
/******************************************************************************/
/***** write to log file or syslog ********************************************/
/******************************************************************************/
void write_to_log (const char *text)
{
if (logfile_flag) {
int year, mon, day, min, hour;
long double sec_nsec;
char buf [STRING_BUFFER_SIZE];
ensec_to_utc_ctime (ensec_time(), &year, &mon, &day, &hour, &min, &sec_nsec);
sprintf (buf, "%04d-%02d-%02d %02d:%02d:%02.0Lf, PID: %d,", year, mon, day, hour, min, sec_nsec, (int)getpid());
fprintf (log_fd, "%s %s", buf, text);
fflush (log_fd);
/*if (verbose_flag) {
printf ("%s %s", buf, text);
fflush (stdout); } */
}
if (syslog_flag) {
openlog ("blitzortung", LOG_CONS|LOG_PID, LOG_USER);
syslog (LOG_INFO,"%s",text);
closelog (); }
}
/******************************************************************************/
/***** initialization *********************************************************/
/******************************************************************************/
//
char int_to_hex (int b)
{
b&= 0x0F;
if (b > 9) {
return (b+'A'-10); }
else {
return (b+'0'); }
}
//
char hex_to_int (char c)
{
if ((c >= '0')&&(c <= '9')) {
return (c-'0'); }
else {
return (c-'A'+10); }
}
// computes NMEA checksums between every $ and *
void fill_checksum (char *buf)
{
unsigned int i;
int c= 0;
for (i=0; i < strlen (buf); i++) {
if (buf[i] == '$') {
c= 0; }
else if (buf[i] == '*') {
i++;
buf [i]= int_to_hex (c>>4);
i++;
buf [i]= int_to_hex (c); }
else {
c^=buf[i]; } }
}
int compute_checksum (const char *buf)
{
unsigned int i;
int c= 0;
for (i=0; i < strlen (buf); i++) {
if (buf[i] == '$') {
c= 0; }
else if (buf[i] == '*') {
return (c); }
else {
c^=buf[i]; } }
return (c);
}
// set baudrate of open tty
void set_baudrate (int f, int baudrate)
{
struct termios tio;
tio.c_iflag= IGNBRK | IGNPAR ;
tio.c_oflag= OPOST | ONLCR ;
if (baudrate == 300) {
tio.c_cflag= B300 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 600) {
tio.c_cflag= B600 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 1200) {
tio.c_cflag= B1200 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 2400) {
tio.c_cflag= B2400 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 4800) {
tio.c_cflag= B4800 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 9600) {
tio.c_cflag= B9600 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 19200) {
tio.c_cflag= B19200 | CS8 | CLOCAL | CREAD ; }
else if (baudrate == 38400) {
tio.c_cflag= B38400 | CS8 | CLOCAL | CREAD ; }
else {
printf ("Do not know how to initialize the tty with %d baud!\n", baudrate);
exit (-1); }
tio.c_lflag= 0;
tio.c_cc[VTIME]= 0;
tio.c_cc[VMIN]= 1;
tcsetattr (f, TCSANOW, &tio);
}
// initialize the GPS modul
void init_gps (const char *serial_device, char *gps_type, int baudrate, bool SBAS_flag)
{
char init_string [STRING_BUFFER_SIZE];
char buf [STRING_BUFFER_SIZE];
if (strcmp (gps_type, "SANAV") == 0) {
strcpy (init_string, init_gps_SANAV);
if (baudrate != 4800) {
printf ("Do not know how to initialize GPS device SANAV with %d baud!\n", baudrate);
exit (-1); }
if (SBAS_flag == 1) {
printf ("Do not know how to initialize GPS device SANAV with SBAS support!\n");
exit (-1); } }
else if (strcmp (gps_type, "Garmin") == 0) {
strcpy (init_string, init_gps_Garmin);
if (baudrate == 4800) {
strcat (init_string, init_gps_Garmin_4800); }
else if (baudrate == 9600) {
strcat (init_string, init_gps_Garmin_9600); }
else if (baudrate == 19200) {
strcat (init_string, init_gps_Garmin_19200); }
else if (baudrate == 38400) {
strcat (init_string, init_gps_Garmin_38400); }
else {
printf ("Do not know how to initialize GPS device Garmin with %d baud!\n", baudrate);
exit (-1); }
if (SBAS_flag) {
strcat (init_string, init_gps_Garmin_SBAS_on); }
else {
strcat (init_string, init_gps_Garmin_SBAS_off); } }
else if (strcmp (gps_type, "SiRF") == 0) {
strcpy (init_string, init_gps_SiRF);
if (baudrate == 4800) {
strcat (init_string, init_gps_SiRF_4800); }
else if (baudrate == 9600) {
strcat (init_string, init_gps_SiRF_9600); }
else if (baudrate == 19200) {
strcat (init_string, init_gps_SiRF_19200); }
else if (baudrate == 38400) {
strcat (init_string, init_gps_SiRF_38400); }
else {
printf ("Do not know how to initialize GPS device SiRF with %d baud!\n", baudrate);
exit (-1); }
if (SBAS_flag) {
strcat (init_string, init_gps_SiRF_SBAS_on); }
else {
strcat (init_string, init_gps_SiRF_SBAS_off); } }
else if (strcmp (gps_type, "-") == 0) {
return; }
fill_checksum (init_string);
if (verbose_flag) {
printf ("%s",init_string);
fflush (stdout); }
int br= 4800;
for (int b=0; b<4; b++) {
int f= open (serial_device, O_RDWR | O_NOCTTY );
if (f == -1) {
perror ("open()");
exit (-1); }
set_baudrate (f, br);
sprintf (buf, "initialize GPS with %s, %d baud, using %d baud\n", gps_type, baudrate, br);
write_to_log (buf);
br+= br;
write (f, "55555\n55555\n55555\n", 18); // only for synchronization
write (f, init_string, strlen(init_string));
close (f); }
}
/******************************************************************************/
/***** send_strike ************************************************************/
/******************************************************************************/
void send_strike (int sock_id, struct sockaddr *serv_addr, const char *username, const char *password)
{
char buf [STRING_BUFFER_SIZE];
sprintf (buf, "%04d-%02d-%02d %02d:%02d:%02d.%09lld %.6Lf %.6Lf %.0Lf %s %s %c %d %d %d %d %s %s %s %d %s\n",
last_year, last_mon, last_day, last_hour, last_min, last_sec, last_nsec, lat_average_x, lon_average_x, alt_average_x, username, password, last_status, last_sat, last_channels, last_values, last_bits, last_data, VERSION, last_firmware, baudrate, gps_type);
if (sendto (sock_id, buf, strlen (buf), 0, serv_addr, sizeof (sockaddr)) == -1) {
write_to_log ("error: sendto ()\n");
return; }
if (verbose_flag) {
printf ("%s", buf);
fflush (stdout); }
if (syslog_flag) {
write_to_log ("Strike sent\n"); }
}
/******************************************************************************/
/***** evaluate ***************************************************************/
/******************************************************************************/
// write status to log file
void log_status ()
{
if ((last_last_imode)&&(last_imode))
return;
char buf [STRING_BUFFER_SIZE];
if (last_checksum_error) {
write_to_log ("Checksum error\n"); }
if ((!(last_last_sec_ok))&&(last_sec_ok)) {
sprintf (buf, "GPS seconds running, %d %d\n", last_last_sec, last_sec);
write_to_log (buf); }
if ((last_last_sec_ok)&&(!(last_sec_ok))) {
sprintf (buf, "GPS seconds stopped, %d %d\n", last_last_sec, last_sec);
write_to_log (buf); }
if (last_status != last_last_status) {
sprintf (buf, "GPS status changed from '%c' to '%c'\n", last_last_status, last_status);
write_to_log (buf); }
if ((!(last_last_pos_ok))&&(last_pos_ok)) {
sprintf (buf, "Position fixed, lat: %+.6Lf, lon: %+.6Lf, alt: %+.1Lf\n", lat_average, lon_average, alt_average);
write_to_log (buf); }
if ((last_last_pos_ok)&&(!(last_pos_ok))) {
sprintf (buf, "Position lost, lat: %+.6Lf, lon: %+.6Lf, alt: %+.1Lf\n", last_lat, last_lon, last_alt);
write_to_log (buf); }
// We only want to warn about Satellite counts when under threshold
if ((last_last_sat != last_sat)&&(last_sat <= MIN_SATS)) {
sprintf (buf, "Number of satellites changed from %d to %d\n", last_last_sat, last_sat);
write_to_log (buf); }
if ((!(last_last_accuracy_ok))&&(last_accuracy_ok)) {
sprintf (buf, "1PPS signal accuracy ok, counter: %06llX %06llX, %+.0Lf nsec\n", last_last_counter, last_counter, time_precision*1000000000.0l);
write_to_log (buf); }
if ((last_last_accuracy_ok)&&(!(last_accuracy_ok))) {
sprintf (buf, "1PPS signal inaccurate, counter: %06llX %06llX, accuracy: %+.0LF nsec\n", last_last_counter, last_counter, time_precision*1000000000.0l);
write_to_log (buf); }
if ((!(last_last_imode))&&(last_imode)) {
sprintf (buf, "started interference mode, %d strikes per second, %d seconds nonzero\n", last_str_sec, nonzero_sec);
write_to_log (buf); }
if ((last_last_imode)&&(!(last_imode))) {
sprintf (buf, "stopped interference mode\n");
write_to_log (buf); }
}
void evaluate (const char *line, int sock_id, struct sockaddr *serv_addr, const char *username, const char *password)
{
int year, mon, day, min, hour, sec= 0, lat_deg, lon_deg, sat= 0, A, B, n;
long double lat, lat_min, lon, lon_min, alt= 0.0l;
char ns, we, status;
long long counter, dif;
bool BLSIG_found= false;
char buf[STRING_BUFFER_SIZE];
char firmware[3];
firmware [0]= '-';
firmware [1]= '-';
firmware [2]= '-';
int checksum;
// BLSEC sentences
if ((((n= sscanf (line, "$BS,%llX,%c,%2d%2d%2d,%2d%2d%2d,%2d%Lf,%c,%3d%Lf,%c,%Lf,%d,%c%c%c*%X",
&counter, &status, &hour, &min, &sec, &day, &mon, &year, &lat_deg, &lat_min, &ns, &lon_deg, &lon_min, &we, &alt, &sat, &firmware[0], &firmware[1], &firmware[2], &checksum)) == 20) ||
((n= sscanf (line, "$BLSEC,%llX,%c,%2d%2d%2d,%2d%2d%2d,%2d%Lf,%c,%3d%Lf,%c,%Lf,M,%d*%X",
&counter, &status, &hour, &min, &sec, &day, &mon, &year, &lat_deg, &lat_min, &ns, &lon_deg, &lon_min, &we, &alt, &sat, &checksum)) == 17) ||
((n= sscanf (line, "$BLSEC,%2d%2d%2d,%2d%2d%2d,%c,%2d%Lf,%c,%3d%Lf,%c,%llX*%X",
&hour, &min, &sec, &day, &mon, &year, &status, &lat_deg, &lat_min, &ns, &lon_deg, &lon_min, &we, &counter, &checksum)) == 15))&&
(!(last_checksum_error= (checksum != compute_checksum(line))))) {
lat= lat_deg+lat_min/60.0;
if (ns == 'S') {
lat= -lat; }
lon= lon_deg+lon_min/60.0;
if (ns == 'W') {
lon= -lon; }
if (last_counter == -1) {
last_lat= lat;
last_lon= lon;
last_alt= alt;
last_counter= counter; }
else {
last_year= year+2000;
last_mon= mon;
last_day= day;
last_min= min;
last_hour= hour;
last_last_sec_ok= last_sec_ok;
last_sec_ok= (sec == (last_sec+1)%60);
last_last_sec= last_sec;
last_sec= sec;
last_last_status= last_status;
last_status= status;
last_last_sat= last_sat;
last_sat= sat;
last_last_lat= last_lat;
last_lat= lat;
last_last_lon= last_lon;
last_lon= lon;
last_last_alt= last_alt;
last_alt= alt;
if (counter > last_counter) {
dif= counter-last_counter; }
else {
dif= (counter+0x1000000ll)-last_counter; }
last_last_counter= last_counter;
last_counter= counter;
// firmware
if (n == 20) {
last_firmware[0]= firmware[0];
last_firmware[1]= firmware[1];
last_firmware[2]= firmware[2];
last_firmware[3]= 0; }
// fill the ring buffer
ring_buffer [ring_buffer_index].lat= lat;
ring_buffer [ring_buffer_index].lon= lon;
ring_buffer [ring_buffer_index].alt= alt;
ring_buffer [ring_buffer_index].dif= dif;
ring_buffer_index= (ring_buffer_index+1)%RING_BUFFER_SIZE;
// compute average dif, lat lon, alt for buffer 1
dif_sum= 0;
lat_sum= 0.0;
lon_sum= 0.0;
alt_sum= 0.0;
for (int i=0; i<RING_BUFFER_SIZE; i++) {
dif_sum+= ring_buffer [i].dif;
lat_sum+= ring_buffer [i].lat;
lon_sum+= ring_buffer [i].lon;
alt_sum+= ring_buffer [i].alt; }
dif_average= (long double)dif_sum/RING_BUFFER_SIZE;
lat_average= lat_sum/RING_BUFFER_SIZE;
lon_average= lon_sum/RING_BUFFER_SIZE;
alt_average= alt_sum/RING_BUFFER_SIZE;
dif_precision= (dif_average-dif);
lat_precision= (lat_average-lat);
lon_precision= (lon_average-lon);
alt_precision= (alt_average-alt);
time_precision= (dif_average-dif)/dif_average;
last_last_accuracy_ok= last_accuracy_ok;
last_accuracy_ok= (fabsl (time_precision) < TIME_PRECISION);
last_last_pos_ok= last_pos_ok;
last_pos_ok= (fabsl (lat_precision) + fabsl (lon_precision) < POS_PRECISION);
if ((!(last_last_pos_ok))&&(last_pos_ok)) {
lat_average_x= lat_average;
lon_average_x= lon_average;
alt_average_x= alt_average; }
else {
lat_average_x= (lat_average_x*(SMOOTH_FACTOR-1)+lat_average)/SMOOTH_FACTOR;
lon_average_x= (lon_average_x*(SMOOTH_FACTOR-1)+lon_average)/SMOOTH_FACTOR;
alt_average_x= (alt_average_x*(SMOOTH_FACTOR-1)+alt_average)/SMOOTH_FACTOR; }
if (str_sec > 0) {
nonzero_sec++; }
last_last_imode= last_imode;
if ((str_sec >= MAX_STR_SEC)||(nonzero_sec >= MAX_NONZERO_SEC)) {
last_imode= true; }
if (str_sec == 0) {
nonzero_sec= 0;
last_imode= false; }
last_str_sec= str_sec;
str_sec= 0;
if (verbose_flag) {
printf ("lat: %+.6Lf, lon: %+.6Lf, alt: %+.1Lf, time precision: %+.0Lf nsec\n", lat_average_x, lon_average_x, alt_average_x, time_precision*1000000000.0l);
fflush (stdout); }
now_time= ensec_time ();
if ((last_accuracy_ok)&&(last_pos_ok)&&(last_sec_ok)&&
(now_time > last_transmition_time + 600000000000ll)) {
last_channels= 0;
last_values= 0;
last_bits= nonzero_sec;
strcpy (last_data, "-");
send_strike (sock_id, serv_addr, username, password);
last_transmition_time= now_time; }
log_status(); } }
// BLSIG sentence type 1
else if ((sscanf (line, "$BLSIG,%6llx,%2x,%2x%c", &counter, &A, &B, &status) == 4)&&(status == '*')) {
sprintf (last_data, "%02x%02x",A,B);
last_channels= 2;
last_values= 1;
last_bits= 8;
BLSIG_found= true; }
// BLSIG sentence type 2
else if ((sscanf (line, "$BLSIG,%6llx,%3x,%3x%c", &counter, &A, &B, &status) == 4)&&(status == '*')) {
sprintf (last_data, "%03x%03x",A,B);
last_channels= 2;
last_values= 1;
last_bits= 12;
BLSIG_found= true; }
// BLSIG sentence type 3
else if ((sscanf (line, "$BLSEQ,%6llx,%256s%c", &counter, last_data, &status) == 3)&&(status == '*')) {
last_channels= 2;
last_values= 64;
last_bits= 8;
BLSIG_found= true; }
// BD sentence type 1
else if ((sscanf (line, "$BD,%6llx,%256s%c", &counter, last_data, &status) == 3)&&(status == '*')) {
last_channels= 2;
last_values= 64;
last_bits= 8;
BLSIG_found= true; }
// BM sentence type 1
else if ((sscanf (line, "$BM,%6llx,%256s%c", &counter, last_data, &status) == 3)&&(status == '*')) {
last_channels= 1;
last_values= 128;
last_bits= 8;
BLSIG_found= true; }
// Firmware sentence
else if (sscanf (line, "Firmware Version: %s", firmware) == 1) {
strcpy(last_firmware, firmware);
write_to_log (line); }
// unknown sentence
else {
sprintf (buf, "unknown sentence: %s", line);
write_to_log (buf); }
if (BLSIG_found) {
if (counter > last_counter) {
dif= counter-last_counter; }
else {
dif= (counter+0x1000000ll)-last_counter; }
last_nsec= (long long)(dif*RING_BUFFER_SIZE*1000000000ll)/dif_sum;
str_sec++;
// send only if precision is ok
if ((last_accuracy_ok)&&(last_pos_ok)&&(last_sec_ok)&&(!(last_imode))) {
send_strike (sock_id, serv_addr, username, password);
last_transmition_time= now_time; } }
}
/******************************************************************************/
/***** main *******************************************************************/
/******************************************************************************/
int main (int argc, char **argv)
{
char program_name [STRING_BUFFER_SIZE];
program_name[0]= 0;
if (argc >0) {
strcpy (program_name,argv[0]);
argc--;
argv++; }
char *logfile= 0;
char *serial_device_in= 0;
char *serial_device_out= 0;
bool serial_device_out_flag= false;
char *username;
char *password;
verbose_flag= false;
bool SBAS_flag= false;
bool help_flag= false;
bool flag_found;
do {
flag_found= false;
if ((argc > 0) && ((strcmp (argv[0], "-l") == 0)|(strcmp (argv[0],"--log") == 0))) {
flag_found= true;
logfile_flag= true;
logfile= argv[1];
argc-=2;
argv+=2; }
if ((argc > 0) && ((strcmp (argv[0], "-e") == 0)||(strcmp (argv[0],"--echo") == 0))) {
flag_found= true;
serial_device_out_flag= true;
serial_device_out= argv[1];
argc-=2;
argv+=2; }
if ((argc > 0) && ((strcmp (argv[0], "-s") == 0)||(strcmp (argv[0],"--SBAS") == 0))) {
flag_found= true;
SBAS_flag= true;
argc-=1;
argv+=1; }
if ((argc > 0) && ((strcmp (argv[0],"-L") == 0)||(strcmp (argv[0],"--syslog") == 0))) {
flag_found= true;
syslog_flag= true;
argc-=1;
argv+=1; }
if ((argc > 0) && ((strcmp (argv[0],"-v") == 0)||(strcmp (argv[0],"--verbose") == 0))) {
flag_found= true;
verbose_flag= true;
argc-=1;
argv+=1; }
if ((argc > 0) && ((strcmp (argv[0],"-h") == 0)||(strcmp (argv[0], "--help") == 0))) {
flag_found= true;
help_flag= true;
argc--;
argv++; } }
while (flag_found);
if ((help_flag)||(argc != 5)) {
printf ("%s: [-v] [-h] [-l logfile] [-e serial_device] [-s] gps_type baudrate serial_device username password\n",program_name);
printf ("gps_type : gps type (SANAV, Garmin, or SiRF ('-' for no initialization)\n");
printf ("baudrate : baudrate (4800, 9600, 19200, or 38400)\n");
printf ("serial_device : serial device (example: /dev/ttyS0)\n");
printf ("username : username (example: PeterPim) \n");
printf ("password : password (example: xxxxxxxx)\n");
printf ("-l logfile : log tracker information\n");
printf ("-L : send info to syslog\n");
printf ("-e serial_device : serial device for input echo\n");
printf ("-s : activate SBAS (WAAS/EGNOS/MSAS) support\n");
printf ("-v : verbose mode\n");
printf ("-h : print this help text\n");
exit (-1); }
gps_type= argv[0];
baudrate= atoi (argv[1]);
serial_device_in= argv[2];
strcpy(last_firmware, "-");
username= argv[3];
password= argv[4];
if (logfile_flag) {
log_fd= fopen(logfile, "w"); }
write_to_log ("tracker started\n");
init_gps (serial_device_in, gps_type, baudrate, SBAS_flag);
int f= open (serial_device_in, O_RDWR | O_NOCTTY );
if (f < 0) {
perror ("open ()");
exit (-1); }
set_baudrate (f, baudrate);
int e= 0;
if (serial_device_out_flag) {
e= open (serial_device_out, O_RDWR | O_NOCTTY );
if (e < 0) {
perror ("open ()");
exit (-1); }
set_baudrate (e, baudrate); }
int sock_id= socket (AF_INET, SOCK_DGRAM, 0);
if (sock_id == -1) {
perror ("socket ()");
exit (-1); }
struct sockaddr_in serv_addr;
serv_addr.sin_family= AF_INET;
serv_addr.sin_port= htons (SERVER_PORT);
serv_addr.sin_addr.s_addr= inet_addr (SERVER_ADDR);
if (serv_addr.sin_addr.s_addr == INADDR_NONE) {
/* host not given by IP but by name */
struct hostent *host_info= gethostbyname (SERVER_ADDR);
if (host_info == NULL) {
write_to_log ("gethostbyname () failed, try again in 10 seconds\n");
sleep (10);
hostent *host_info= gethostbyname (SERVER_ADDR);
if (host_info == NULL) {
write_to_log ("gethostbyname () failed, try again in 60 seconds\n");
sleep (60);
hostent *host_info= gethostbyname (SERVER_ADDR);
if (host_info == NULL) {
write_to_log ("gethostbyname () failed, give up, check internet connection\n");
perror ("gethostbyname ()");
close (sock_id);
exit (-1); } } }
memcpy((char*) &serv_addr.sin_addr.s_addr, host_info->h_addr, host_info->h_length); }
char c;
int i=0;
char buf[STRING_BUFFER_SIZE];
while (true) {
if (read (f, &c, 1) == 1) {
if (serial_device_out_flag) {
write (e, &c, 1); }
buf [i]= c;
if (verbose_flag) {
putchar (c); }
if (i < STRING_BUFFER_SIZE-2) {
i++; }
if (c == '\n') {
buf [i]= 0;
evaluate (buf, sock_id, (struct sockaddr *)&serv_addr, username, password);
i= 0; } } }
}