Skip to content

Commit 4176d0a

Browse files
author
=
committedFeb 19, 2025
clean up
1 parent a849727 commit 4176d0a

File tree

1 file changed

+115
-75
lines changed

1 file changed

+115
-75
lines changed
 

‎hcxnmealog.c

+115-75
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
static int fd_socket_rx = 0;
3838
static int fd_gps = 0;
3939
static int fd_timer = 0;
40-
static int ifaktindex = 0;
4140
static int timerwaitnd = TIMER_EPWAITND;
4241
static float latitude = 0;
4342
static float longitude = 0;
@@ -63,13 +62,26 @@ static u8 *ieee82011ptr = NULL;
6362
static u8 *payloadptr = NULL;
6463
static ieee80211_mac_t *macfrx = NULL;
6564
static rth_t *rth = NULL;
65+
static struct tpacket_stats lStats = { 0 };
66+
static socklen_t lStatsLength = sizeof(lStats);
6667
static struct sock_fprog bpf = { 0 };
6768
static struct timespec tspecnmea = { 0 };
6869
static struct timespec tspecakt = { 0 };
6970
static char nmeabuffer[NMEA_SIZE] = { 0 };
7071
static u8 rx[PCAPNG_SNAPLEN * 2] = { 0 };
7172
static u8 rxbuffer[PCAPNG_SNAPLEN * 2] = { 0 };
7273
/*===========================================================================*/
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+
/*===========================================================================*/
7385
static size_t chop(char *buffer, size_t len)
7486
{
7587
char *ptr = NULL;
@@ -147,7 +159,25 @@ if(bpf.len == 0) return false;
147159
return true;
148160
}
149161
/*===========================================================================*/
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)
151181
{
152182
static size_t c = 10;
153183
static struct sockaddr_ll saddr;
@@ -206,7 +236,6 @@ while((!wanteventflag) || (c != 0))
206236
if(packetlen == -1) break;
207237
c--;
208238
}
209-
packetptr = &rxbuffer[PCAPNG_SNAPLEN * 2];
210239
return true;
211240
}
212241
/*---------------------------------------------------------------------------*/
@@ -253,6 +282,53 @@ tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soo
253282
tty.c_cc[VMIN] = 0;
254283
cfsetspeed(&tty, (speed_t)baudrate);
255284
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+
256332
return true;
257333
}
258334
/*===========================================================================*/
@@ -287,6 +363,32 @@ tval.it_value.tv_nsec = TIMER_VALUE_NSEC;
287363
tval.it_interval.tv_sec = TIMER_INTERVAL_SEC;
288364
tval.it_interval.tv_nsec = TIMER_INTERVAL_NSEC;
289365
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+
290392
return true;
291393
}
292394
/*===========================================================================*/
@@ -504,7 +606,7 @@ fprintf(stdout, "\033[?25l");
504606
if(nmeaoutname != NULL)
505607
{
506608
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);
508610
}
509611
while(!wanteventflag)
510612
{
@@ -530,7 +632,7 @@ while(!wanteventflag)
530632
{
531633
if(nmeaoutname != NULL)
532634
{
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);
534636
}
535637
}
536638
}
@@ -590,14 +692,11 @@ int main(int argc, char *argv[])
590692
{
591693
static int auswahl;
592694
static int index;
695+
static int ifaktindex;
593696
static int baudrate;
594697
static char *gpsdevice;
595698
static char *nmeaoutname;
596699
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);
601700

602701
static const char *short_options = "o:d:b:i:hv";
603702
static const struct option long_options[] =
@@ -613,6 +712,7 @@ index = 0;
613712
optind = 1;
614713
optopt = 0;
615714
baudrate = 9600;
715+
ifaktindex = 0;
616716
gpsdevice = NULL;
617717
nmeaoutname = NULL;
618718
bpfname = NULL;
@@ -665,64 +765,9 @@ if(argc < 2)
665765
}
666766
setbuf(stdout, NULL);
667767

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;
726771

727772
if(gps_loop(basename(argv[0]), nmeaoutname) == false)
728773
{
@@ -731,14 +776,9 @@ if(gps_loop(basename(argv[0]), nmeaoutname) == false)
731776
}
732777

733778
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();
742782
if(nmeaoutname != NULL)
743783
{
744784
fprintf(stdout, "\nSummary:\n"

0 commit comments

Comments
 (0)