37
37
static int fd_socket_rx = 0 ;
38
38
static int fd_gps = 0 ;
39
39
static int fd_timer = 0 ;
40
- static int ifaktindex = 0 ;
41
40
static int timerwaitnd = TIMER_EPWAITND ;
42
41
static float latitude = 0 ;
43
42
static float longitude = 0 ;
@@ -63,13 +62,26 @@ static u8 *ieee82011ptr = NULL;
63
62
static u8 * payloadptr = NULL ;
64
63
static ieee80211_mac_t * macfrx = NULL ;
65
64
static rth_t * rth = NULL ;
65
+ static struct tpacket_stats lStats = { 0 };
66
+ static socklen_t lStatsLength = sizeof (lStats );
66
67
static struct sock_fprog bpf = { 0 };
67
68
static struct timespec tspecnmea = { 0 };
68
69
static struct timespec tspecakt = { 0 };
69
70
static char nmeabuffer [NMEA_SIZE ] = { 0 };
70
71
static u8 rx [PCAPNG_SNAPLEN * 2 ] = { 0 };
71
72
static u8 rxbuffer [PCAPNG_SNAPLEN * 2 ] = { 0 };
72
73
/*===========================================================================*/
74
+ static void close_devices ()
75
+ {
76
+ if (fd_gps != 0 ) close (fd_gps );
77
+ if (fd_socket_rx != 0 )
78
+ {
79
+ if (getsockopt (fd_socket_rx , SOL_PACKET , PACKET_STATISTICS , & lStats , & lStatsLength ) != 0 ) fprintf (stdout , "PACKET_STATISTICS failed\n" );
80
+ close (fd_socket_rx );
81
+ }
82
+ return ;
83
+ }
84
+ /*===========================================================================*/
73
85
static size_t chop (char * buffer , size_t len )
74
86
{
75
87
char * ptr = NULL ;
@@ -147,7 +159,25 @@ if(bpf.len == 0) return false;
147
159
return true;
148
160
}
149
161
/*===========================================================================*/
150
- static bool open_socket_rx (char * bpfname )
162
+ static void close_files (void )
163
+ {
164
+ if (fh_nmea != NULL )fclose (fh_nmea );
165
+ return ;
166
+ }
167
+ /*---------------------------------------------------------------------------*/
168
+ static bool open_files (char * nmeaoutname )
169
+ {
170
+ if (nmeaoutname == NULL ) fh_nmea = stdout ;
171
+ else if ((fh_nmea = fopen (nmeaoutname , "a" )) == NULL )
172
+ {
173
+ errorcount ++ ;
174
+ fprintf (stderr , "failed to open nmea file\n" );
175
+ return false;
176
+ }
177
+ return true;
178
+ }
179
+ /*===========================================================================*/
180
+ static bool open_socket_rx (int ifaktindex , char * bpfname )
151
181
{
152
182
static size_t c = 10 ;
153
183
static struct sockaddr_ll saddr ;
@@ -206,7 +236,6 @@ while((!wanteventflag) || (c != 0))
206
236
if (packetlen == -1 ) break ;
207
237
c -- ;
208
238
}
209
- packetptr = & rxbuffer [PCAPNG_SNAPLEN * 2 ];
210
239
return true;
211
240
}
212
241
/*---------------------------------------------------------------------------*/
@@ -253,6 +282,53 @@ tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soo
253
282
tty .c_cc [VMIN ] = 0 ;
254
283
cfsetspeed (& tty , (speed_t )baudrate );
255
284
if (tcsetattr (fd_gps , TCSANOW , & tty ) < 0 ) return false;
285
+ return true;
286
+ }
287
+ /*---------------------------------------------------------------------------*/
288
+ static bool open_devices (char * hcxnmealogname , int ifaktindex , char * bpfname , char * gpsdevice , int baudrate )
289
+ {
290
+ static char * gpsdname = "gpsd" ;
291
+ static char * devicename = "/dev" ;
292
+
293
+ if (ifaktindex != 0 )
294
+ {
295
+ if (getuid () != 0 )
296
+ {
297
+ errorcount ++ ;
298
+ fprintf (stderr , "%s must be run as root\n" , hcxnmealogname );
299
+ return false;
300
+ }
301
+ if (open_socket_rx (ifaktindex , bpfname ) == false)
302
+ {
303
+ errorcount ++ ;
304
+ fprintf (stderr , "failed to open raw packet socket\n" );
305
+ return false;
306
+ }
307
+ }
308
+ if (strncmp (gpsdname , gpsdevice , 4 ) == 0 )
309
+ {
310
+ if (open_socket_gpsd () == false)
311
+ {
312
+ fprintf (stderr , "failed to connect to GPSD\n" );
313
+ return EXIT_SUCCESS ;
314
+ }
315
+ }
316
+ else if (strncmp (devicename , gpsdevice , 4 ) == 0 )
317
+ {
318
+ if (open_device_gps (gpsdevice , baudrate ) == false)
319
+ {
320
+ fprintf (stderr , "failed to open GPS device\n" );
321
+ return EXIT_SUCCESS ;
322
+ }
323
+ }
324
+ else
325
+ {
326
+ fprintf (stderr , "no GPS device selected\n" );
327
+ return EXIT_SUCCESS ;
328
+ }
329
+
330
+
331
+
256
332
return true;
257
333
}
258
334
/*===========================================================================*/
@@ -287,6 +363,32 @@ tval.it_value.tv_nsec = TIMER_VALUE_NSEC;
287
363
tval .it_interval .tv_sec = TIMER_INTERVAL_SEC ;
288
364
tval .it_interval .tv_nsec = TIMER_INTERVAL_NSEC ;
289
365
if (timerfd_settime (fd_timer , 0 , & tval , NULL ) == -1 ) return false;
366
+ return true;
367
+ }
368
+ /*===========================================================================*/
369
+ static void global_deinit ()
370
+ {
371
+ if (fd_timer != 0 ) close (fd_timer );
372
+ return ;
373
+ }
374
+ /*---------------------------------------------------------------------------*/
375
+ static bool global_init (void )
376
+ {
377
+ packetptr = & rxbuffer [PCAPNG_SNAPLEN * 2 ];
378
+ if (set_signal_handler () == false)
379
+ {
380
+ errorcount ++ ;
381
+ fprintf (stderr , "failed to initialize signal handler\n" );
382
+ return false;
383
+ }
384
+
385
+ if (set_timer () == false)
386
+ {
387
+ errorcount ++ ;
388
+ fprintf (stderr , "failed to initialize timer\n" );
389
+ return false;
390
+ }
391
+
290
392
return true;
291
393
}
292
394
/*===========================================================================*/
@@ -504,7 +606,7 @@ fprintf(stdout, "\033[?25l");
504
606
if (nmeaoutname != NULL )
505
607
{
506
608
fprintf (stdout , "%s %s logging NMEA 0183 track to %s\n" , basename , VERSION_TAG , nmeaoutname );
507
- fprintf (stdout , "\rNMEA 0183 sentences: %" PRIu64 " (lat:%f lon:%f alt:%f ) | 802.11 packets: %" PRIu64 , nmeapacketcount , latitude , longitude , altitude , packetcount );
609
+ fprintf (stdout , "\rNMEA 0183 sentences: %" PRIu64 " (lat:%.1f lon:%.1f alt:%.1f ) | 802.11 packets: %" PRIu64 , nmeapacketcount , latitude , longitude , altitude , packetcount );
508
610
}
509
611
while (!wanteventflag )
510
612
{
@@ -530,7 +632,7 @@ while(!wanteventflag)
530
632
{
531
633
if (nmeaoutname != NULL )
532
634
{
533
- fprintf (stdout , "\rNMEA 0183 sentences: %" PRIu64 " (lat:%f lon:%f alt:%f ) | 802.11 packets: %" PRIu64 , nmeapacketcount , latitude , longitude , altitude , packetcount );
635
+ fprintf (stdout , "\rNMEA 0183 sentences: %" PRIu64 " (lat:%f lon:%f alt:%.1f ) | 802.11 packets: %" PRIu64 , nmeapacketcount , latitude , longitude , altitude , packetcount );
534
636
}
535
637
}
536
638
}
@@ -590,14 +692,11 @@ int main(int argc, char *argv[])
590
692
{
591
693
static int auswahl ;
592
694
static int index ;
695
+ static int ifaktindex ;
593
696
static int baudrate ;
594
697
static char * gpsdevice ;
595
698
static char * nmeaoutname ;
596
699
static char * bpfname ;
597
- static char * gpsdname = "gpsd" ;
598
- static char * devicename = "/dev" ;
599
- static struct tpacket_stats lStats = { 0 };
600
- static socklen_t lStatsLength = sizeof (lStats );
601
700
602
701
static const char * short_options = "o:d:b:i:hv" ;
603
702
static const struct option long_options [] =
@@ -613,6 +712,7 @@ index = 0;
613
712
optind = 1 ;
614
713
optopt = 0 ;
615
714
baudrate = 9600 ;
715
+ ifaktindex = 0 ;
616
716
gpsdevice = NULL ;
617
717
nmeaoutname = NULL ;
618
718
bpfname = NULL ;
@@ -665,64 +765,9 @@ if(argc < 2)
665
765
}
666
766
setbuf (stdout , NULL );
667
767
668
- if (ifaktindex != 0 )
669
- {
670
- if (getuid () != 0 )
671
- {
672
- errorcount ++ ;
673
- fprintf (stderr , "%s must be run as root\n" , basename (argv [0 ]));
674
- goto byebye ;
675
- }
676
- if (open_socket_rx (bpfname ) == false)
677
- {
678
- errorcount ++ ;
679
- fprintf (stderr , "failed to open raw packet socket\n" );
680
- goto byebye ;
681
- }
682
- }
683
- if (strncmp (gpsdname , gpsdevice , 4 ) == 0 )
684
- {
685
- if (open_socket_gpsd () == false)
686
- {
687
- fprintf (stderr , "failed to connect to GPSD\n" );
688
- return EXIT_SUCCESS ;
689
- }
690
- }
691
- else if (strncmp (devicename , gpsdevice , 4 ) == 0 )
692
- {
693
- if (open_device_gps (gpsdevice , baudrate ) == false)
694
- {
695
- fprintf (stderr , "failed to open GPS device\n" );
696
- return EXIT_SUCCESS ;
697
- }
698
- }
699
- else
700
- {
701
- fprintf (stderr , "no GPS device selected\n" );
702
- return EXIT_SUCCESS ;
703
- }
704
-
705
- if (nmeaoutname == NULL ) fh_nmea = stdout ;
706
- else if ((fh_nmea = fopen (nmeaoutname , "a" )) == NULL )
707
- {
708
- errorcount ++ ;
709
- fprintf (stderr , "failed to open nmea file\n" );
710
- goto byebye ;
711
- }
712
-
713
- if (set_signal_handler () == false)
714
- {
715
- errorcount ++ ;
716
- fprintf (stderr , "failed to initialize signal handler\n" );
717
- goto byebye ;
718
- }
719
-
720
- if (set_timer () == false)
721
- {
722
- errorcount ++ ;
723
- fprintf (stderr , "failed to initialize timer\n" );
724
- goto byebye ;
725
- }
768
+ if (open_devices (basename (argv [0 ]), ifaktindex , bpfname , gpsdevice , baudrate ) == false) goto byebye ;
769
+ if (open_files (nmeaoutname ) == false) goto byebye ;
770
+ if (global_init () == false) goto byebye ;
726
771
727
772
if (gps_loop (basename (argv [0 ]), nmeaoutname ) == false)
728
773
{
@@ -731,14 +776,9 @@ if(gps_loop(basename(argv[0]), nmeaoutname) == false)
731
776
}
732
777
733
778
byebye :
734
- if (fd_timer != 0 ) close (fd_timer );
735
- if (fd_gps != 0 ) close (fd_gps );
736
- if (fh_nmea != NULL )fclose (fh_nmea );
737
- if (fd_socket_rx != 0 )
738
- {
739
- if (getsockopt (fd_socket_rx , SOL_PACKET , PACKET_STATISTICS , & lStats , & lStatsLength ) != 0 ) fprintf (stdout , "PACKET_STATISTICS failed\n" );
740
- close (fd_socket_rx );
741
- }
779
+ close_devices ();
780
+ close_files ();
781
+ global_deinit ();
742
782
if (nmeaoutname != NULL )
743
783
{
744
784
fprintf (stdout , "\nSummary:\n"
0 commit comments