[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gpsd-dev] [PATCH v2 1/1] gps2udp: add a 'pack' mode
From: |
Ferry Huberts |
Subject: |
[gpsd-dev] [PATCH v2 1/1] gps2udp: add a 'pack' mode |
Date: |
Fri, 3 Jan 2014 15:37:33 +0100 |
From: Ferry Huberts <address@hidden>
-p flush-phrase Will pack all sentences that start with a '$' character
in a single packet. The packet will be sent when a sentence is
received from the gpsd daemon that starts with the flush-phrase.
Signed-off-by: Ferry Huberts <address@hidden>
---
gps2udp.c | 41 ++++++++++++++++++++++++++++++++++++++---
gps2udp.xml | 4 ++++
2 files changed, 42 insertions(+), 3 deletions(-)
diff --git a/gps2udp.c b/gps2udp.c
index ac816bc..563c01f 100644
--- a/gps2udp.c
+++ b/gps2udp.c
@@ -78,8 +78,10 @@ static bool aisonly = false;
}
/address@hidden@*/
-static int send_udp (char *nmeastring, size_t ind)
+static int send_udp (char *flush_phrase, unsigned int flush_phrase_len, char
*nmeastring, size_t ind)
{
+ static char pack_buffer[16384];
+ static unsigned int pack_buffer_len = 0;
char message [255];
char *buffer;
int channel;
@@ -105,6 +107,26 @@ static int send_udp (char *nmeastring, size_t ind)
buffer[ind] = '\r'; ind++;
buffer[ind] = '\0';
+ /*
+ * Only pack when a flush-phrase is defined and when the current sentence
+ * starts with a '$'
+ */
+ if (flush_phrase && buffer[0] == '$') {
+ strcpy(&pack_buffer[pack_buffer_len], buffer);
+ pack_buffer_len += ind;
+ if (strncmp(buffer, flush_phrase, flush_phrase_len) &&
+ (pack_buffer_len < (sizeof(pack_buffer) - sizeof(message)))) {
+ /* the current sentence doesn't start with the flush-phrase _and_
+ * there is enough space in the pack buffer for another sentence */
+ return 0;
+ }
+
+ /* send out the pack buffer */
+ buffer = pack_buffer;
+ ind = pack_buffer_len;
+ pack_buffer_len = 0;
+ }
+
/* send message on udp channel */
/address@hidden@*/
for (channel=0; channel < udpchannel; channel ++) {
@@ -182,6 +204,9 @@ static void usage(void)
"-a Select !AISDM message only.\n"
"-c [count] exit after count packets.\n"
"-b Run in background as a daemon.\n"
+ "-p flush-phrase Will pack all sentences that start with a
'$' character\n"
+ " in a single packet. The packet will be sent when a
sentence is\n"
+ " received from the gpsd daemon that starts with the
flush-phrase.\n"
"-d [0-2] 1 display sent packets, 2 ignored packets.\n"
"-v Print version and exit.\n\n"
"You must specify one, or more, of -r, -R, or -w\n"
@@ -354,12 +379,14 @@ static unsigned int AISGetInt(unsigned char *bitbytes,
unsigned int sp, unsigned
int main(int argc, char **argv)
{
bool daemonize = false;
+ char * flush_phrase = NULL;
+ unsigned int flush_phrase_len = 0;
long count = -1;
int option;
char *udphostport[MAX_UDP_DEST];
flags = WATCH_ENABLE;
- while ((option = getopt(argc, argv, "?habnjvc:l:u:d:")) != -1)
+ while ((option = getopt(argc, argv, "?habnjvc:l:u:p:d:")) != -1)
{
switch (option) {
case 'd':
@@ -397,6 +424,14 @@ int main(int argc, char **argv)
udphostport[udpchannel++] = optarg;
}
break;
+ case 'p':
+ flush_phrase = strdup(optarg);
+ flush_phrase_len = strlen(flush_phrase);
+ if (!flush_phrase_len || flush_phrase[0] != '$') {
+ (void) fprintf(stderr, "gps2udp: flush-phrase must
start with '$' character\n");
+ exit (1);
+ }
+ break;
case 'v':
(void)fprintf(stderr, "%s: %s (revision %s)\n",
argv[0], VERSION, REVISION);
@@ -483,7 +518,7 @@ int main(int argc, char **argv)
// send to all UDP destinations
if (udpchannel > 0)
- (void)send_udp(buffer, (size_t)len);
+ (void)send_udp(flush_phrase, flush_phrase_len, buffer,
(size_t)len);
// if we count messages check it now
if (count >= 0) {
diff --git a/gps2udp.xml b/gps2udp.xml
index 8f0f588..4ef8a9d 100644
--- a/gps2udp.xml
+++ b/gps2udp.xml
@@ -28,6 +28,7 @@ BSD terms apply: see the file COPYING in the distribution
root for details.
<arg choice='opt'>-a</arg>
<arg choice='opt'>-u <replaceable>hostname:udpport</replaceable></arg>
<arg choice='opt'>-c <replaceable>count</replaceable></arg>
+ <arg choice='opt'>-p <replaceable>flush-phrase</replaceable></arg>
<arg choice='opt'>-d <replaceable>1|2</replaceable></arg>
<arg choice='opt'>-v</arg>
<group>
@@ -85,6 +86,9 @@ destinations).</para>
<para>-b causes <application>gps2udp</application> to run as a daemon.</para>
<para>-c [count] causes [count] sentences to be output.
<application>gps2udp</application> will then exit gracefully.</para>
+<para>-p flush-phrase Will pack all sentences that start with a '$' character
+in a single packet. The packet will be sent when a sentence is received from
+the gpsd daemon that starts with the flush-phrase.</para>
<para>-d 1 prints sent packet on stdout.</para>
<para>-v prints the version, then exits.</para>
--
1.8.4.2
- [gpsd-dev] [PATCH v2 1/1] gps2udp: add a 'pack' mode,
Ferry Huberts <=