meta-sysmocom-bsp/recipes-extra/gpsd/gpsd/0001-gps2udp-Add-a-label-ti...

157 lines
5.4 KiB
Diff

From cd89f0b340764e73de76f5297217caf61b3d3283 Mon Sep 17 00:00:00 2001
From: Oliver Smith <osmith@sysmocom.de>
Date: Tue, 13 Dec 2022 16:05:00 +0100
Subject: [PATCH] gps2udp: Add a label, timestamp and mac address to each
report
Rebase of 0001-gps2udp-Add-a-label-timestamp-and-mac-address-to-eac.patch
we have been carrying:
From 19a55075d4e60e07d03d49937cd2a4d9239d00a4 Mon Sep 17 00:00:00 2001
From: Holger Hans Peter Freyther <hfreyther@sysmocom.de>
Date: Fri, 22 Aug 2014 09:55:19 +0200
Subject: [PATCH] gps2udp: Add a label, timestamp and mac address to each
report
---
clients/gps2udp.c | 61 +++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 59 insertions(+), 2 deletions(-)
diff --git a/clients/gps2udp.c b/clients/gps2udp.c
index 2d9c6033d..d27814f24 100644
--- a/clients/gps2udp.c
+++ b/clients/gps2udp.c
@@ -22,6 +22,7 @@
#include <getopt.h> // for getopt_long()
#endif
#include <netdb.h> /* for gethostbyname() */
+#include <net/if.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
@@ -29,6 +30,7 @@
// do not use strsep() it is not POSIX
#include <string.h> /* for strlcpy(), strtok(), etc. */
#include <strings.h>
+#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -60,6 +62,9 @@ static unsigned int flags;
static unsigned int debug = 0;
static bool aisonly = false;
static bool tpvonly = false;
+static const char *label;
+static const char *mac;
+static char *mac_string;
// return local time hh:mm:ss
static char* time2string(void)
@@ -82,6 +87,7 @@ static char* time2string(void)
static int send_udp(char *nmeastring, size_t ind)
{
+ char output[1024];
char message[MAX_PACKET_LENGTH];
char *buffer;
int channel;
@@ -108,6 +114,16 @@ static int send_udp(char *nmeastring, size_t ind)
buffer[ind] = '\n'; ind++;
buffer[ind] = '\0';
+ /* copy once more for the label */
+ if (label)
+ snprintf(output, 1024, "%s,%llu,%s,%s",
+ label, (unsigned long long ) time(NULL), mac_string, buffer);
+ else
+ snprintf(output, 1024, "%s", buffer);
+
+ output[1023] = '\0';
+ ind = strlen(output);
+
if (0 == (flags & WATCH_JSON) &&
'{' == buffer[0]) {
// do not send JSON when not configured to do so
@@ -129,7 +145,7 @@ static int send_udp(char *nmeastring, size_t ind)
// send message on udp channel
for (channel=0; channel < udpchannel; channel ++) {
ssize_t status = sendto(sock[channel],
- buffer,
+ output,
ind,
0,
(struct sockaddr *)&remote[channel],
@@ -225,6 +241,8 @@ static void usage(void)
" -c COUNT Exit after count packets.\n"
" -d [0-2] 1 display sent packets, "
"2 display ignored packets.\n"
+ " -l [NAME] A label to be used for the output\n"
+ " -m [IF_NAME] The interface to extract the mac from\n"
" -h Show this help.\n"
" -j Feed JSON.\n"
" -n Feed NMEA.\n"
@@ -421,7 +439,7 @@ int main(int argc, char **argv)
bool daemonize = false;
long count = -1;
char *udphostport[MAX_UDP_DEST];
- const char *optstring = "?abc:d:hjntu:V";
+ const char *optstring = "?abc:d:l:m:hjntu:V";
#ifdef HAVE_GETOPT_LONG
int option_index = 0;
static struct option long_options[] = {
@@ -478,6 +496,12 @@ int main(int argc, char **argv)
if (0 < debug)
(void)fprintf(stdout, "Debug %u selected\n", debug);
break;
+ case 'l':
+ label = optarg;
+ break;
+ case 'm':
+ mac = optarg;
+ break;
case 'j':
if (0 < debug)
(void)fprintf(stdout, "JSON selected\n");
@@ -520,6 +544,39 @@ int main(int argc, char **argv)
}
}
+ if (label && !mac) {
+ fprintf(stderr, "Need to specify the ethernet device to find the mac.\n");
+ exit(EXIT_FAILURE);
+ }
+ if (mac && strlen(mac) >= IFNAMSIZ) {
+ fprintf(stderr, "Interface name is too long.\n");
+ exit(EXIT_FAILURE);
+ } else if (mac) {
+ struct ifreq addr = { };
+ int fd, rc;
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd < 0) {
+ fprintf(stderr, "Failed to open socket.\n");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(&addr.ifr_name, mac, strlen(mac));
+ rc = ioctl(fd, SIOCGIFHWADDR, &addr);
+ close(fd);
+ if (rc < 0) {
+ fprintf(stderr, "Failed to query address.\n");
+ exit(EXIT_FAILURE);
+ }
+ mac_string = malloc(40 * sizeof(char));
+ snprintf(mac_string, 40,
+ "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
+ addr.ifr_hwaddr.sa_data[0] & 0xff,
+ addr.ifr_hwaddr.sa_data[1] & 0xff,
+ addr.ifr_hwaddr.sa_data[2] & 0xff,
+ addr.ifr_hwaddr.sa_data[3] & 0xff,
+ addr.ifr_hwaddr.sa_data[4] & 0xff,
+ addr.ifr_hwaddr.sa_data[5] & 0xff);
+ }
+
// Grok the server, port, and device.
if (optind < argc) {
gpsd_source_spec(argv[optind], &gpsd_source);
--
2.34.1