changeset 39:d135288b7f53

dump-*: switch to using framed input logs Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Tue, 14 Jan 2020 23:43:51 -0500
parents 6c53105716c0
children 222b6e81d8d2
files dump-common.c dump-common.h dump-ecef.c ubx.c ubx.h
diffstat 5 files changed, 59 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/dump-common.c	Tue Jan 14 23:28:02 2020 -0500
+++ b/dump-common.c	Tue Jan 14 23:43:51 2020 -0500
@@ -23,14 +23,16 @@
 #include <jeffpc/error.h>
 
 #include "dump-common.h"
+#include "xstdio.h"
+#include "frame.h"
 
 static const char *prog;
 
 static void usage(void)
 {
-	fprintf(stderr, "Usage: %s <rawlog>\n", prog);
+	fprintf(stderr, "Usage: %s <log>\n", prog);
 	fprintf(stderr, "\n");
-	fprintf(stderr, "  <rawlog>       path with raw UBX messages\n");
+	fprintf(stderr, "  <log>          path with framed UBX messages\n");
 	exit(1);
 }
 
@@ -54,9 +56,41 @@
 		return 9;
 	}
 
-	for (;;)
-		if (!read_and_process_ubx_message(ifile, process_ubx_message))
+	for (;;) {
+		struct frame frame;
+		uint8_t *buf;
+		int ret;
+
+		ret = xfread(ifile, &frame, sizeof(frame));
+		if (ret) {
+			fprintf(stderr, "Error: failed to read frame header\n");
 			break;
+		}
+
+		frame.magic   = be32_to_cpu(frame.magic);
+		frame.session = be32_to_cpu(frame.session);
+		frame.tick    = be32_to_cpu(frame.tick);
+		frame.len     = be32_to_cpu(frame.len);
+
+		if (frame.magic != FRAME_MAGIC) {
+			fprintf(stderr, "Error: frame magic mismatch "
+				"(got %08x exp %08x)\n",
+				frame.magic, FRAME_MAGIC);
+			break;
+		}
+
+		buf = alloca(frame.len);
+
+		ret = xfread(ifile, buf, frame.len);
+		if (ret) {
+			fprintf(stderr, "Error: failed to read frame data\n");
+			break;
+		}
+
+		if (!parse_and_process_ubx_message(buf, frame.len, frame.tick,
+						   process_ubx_message))
+			break;
+	}
 
 	fclose(ifile);
 
--- a/dump-common.h	Tue Jan 14 23:28:02 2020 -0500
+++ b/dump-common.h	Tue Jan 14 23:43:51 2020 -0500
@@ -30,7 +30,7 @@
 #include "ubx.h"
 
 extern void process_ubx_message(const struct ubx_header *header,
-				const uint8_t *raw,
-				size_t len);
+				const uint8_t *raw, size_t len,
+				uint64_t tick);
 
 #endif
--- a/dump-ecef.c	Tue Jan 14 23:28:02 2020 -0500
+++ b/dump-ecef.c	Tue Jan 14 23:43:51 2020 -0500
@@ -23,8 +23,8 @@
 #include "dump-common.h"
 
 void process_ubx_message(const struct ubx_header *header,
-			 const uint8_t *raw,
-			 size_t len)
+			 const uint8_t *raw, size_t len,
+			 uint64_t tick)
 {
 	struct ubx_nav_posecef msg;
 
--- a/ubx.c	Tue Jan 14 23:28:02 2020 -0500
+++ b/ubx.c	Tue Jan 14 23:43:51 2020 -0500
@@ -133,20 +133,16 @@
 	return send_ubx_with_ack(file, UBX_CFG_MSG, raw, sizeof(raw));
 }
 
-bool read_and_process_ubx_message(FILE *file, void(*f)(const struct ubx_header *,
-						       const uint8_t *,
-						       size_t))
+bool parse_and_process_ubx_message(const uint8_t *buf, size_t len,
+				   const uint64_t tick,
+				   void(*f)(const struct ubx_header *,
+					    const uint8_t *, size_t,
+					    uint64_t))
 {
 	struct ubx_header hdr;
-	uint16_t len;
-	int ret;
+	uint16_t ubx_len;
 
-	/* read in header */
-	ret = xfread(file, &hdr, sizeof(hdr));
-	if (ret) {
-		fprintf(stderr, "Failed to read UBX header\n");
-		return false;
-	}
+	memcpy(&hdr, buf, sizeof(hdr));
 
 	if ((hdr.sync[0] != UBX_SYNC_BYTE_1) ||
 	    (hdr.sync[1] != UBX_SYNC_BYTE_2)) {
@@ -155,19 +151,16 @@
 		return false;
         }
 
-	len = le16_to_cpu(hdr.len);
-
-	uint8_t raw[len + 2];
+	ubx_len = le16_to_cpu(hdr.len);
 
-	/* read in payload + checksum */
-	ret = xfread(file, raw, sizeof(raw));
-	if (ret) {
-		fprintf(stderr, "Failed to read UBX payload & checksum\n");
+	if (ubx_len != (len - sizeof(hdr) - 2)) {
+		fprintf(stderr, "UBX message length differs (got %u exp %lu)\n",
+			ubx_len, len - sizeof(hdr) - 2);
 		return false;
 	}
 
 	if (f)
-		f(&hdr, raw, len);
+		f(&hdr, &buf[sizeof(hdr)], ubx_len, tick);
 
 	return true;
 }
--- a/ubx.h	Tue Jan 14 23:28:02 2020 -0500
+++ b/ubx.h	Tue Jan 14 23:43:51 2020 -0500
@@ -253,8 +253,10 @@
 			     size_t len);
 extern int enable_ubx_msg(FILE *file, enum ubx_msg_id id, int port, int rate);
 
-extern bool read_and_process_ubx_message(FILE *file, void(*f)(const struct ubx_header *,
-							      const uint8_t *,
-							      size_t));
+extern bool parse_and_process_ubx_message(const uint8_t *buf, size_t len,
+					  const uint64_t tick,
+					  void(*f)(const struct ubx_header *,
+						   const uint8_t *, size_t,
+						   uint64_t));
 
 #endif