changeset 60:932de83078d4

reframe: a utility to upgrade juf files to the current format Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 15 Jan 2020 10:31:19 -0500
parents f00ab2e1d9d7
children 49e8cd3755f9
files .hgignore CMakeLists.txt reframe.c
diffstat 3 files changed, 162 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Jan 15 10:31:01 2020 -0500
+++ b/.hgignore	Wed Jan 15 10:31:19 2020 -0500
@@ -14,3 +14,4 @@
 dump-ecef
 dump-ubx
 print-state
+reframe
--- a/CMakeLists.txt	Wed Jan 15 10:31:01 2020 -0500
+++ b/CMakeLists.txt	Wed Jan 15 10:31:19 2020 -0500
@@ -108,3 +108,11 @@
 	gnss
 	ublox8
 )
+
+add_executable(reframe
+	reframe.c
+)
+
+target_link_libraries(reframe
+	${JEFFPC_LIBRARY}
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/reframe.c	Wed Jan 15 10:31:19 2020 -0500
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <jeffpc/error.h>
+#include <jeffpc/io.h>
+#include <jeffpc/rand.h>
+
+#include "frame.h"
+
+static const char *prog;
+
+static void usage(void)
+{
+	fprintf(stderr, "Usage: %s\n", prog);
+	fprintf(stderr, "\n");
+	fprintf(stderr, "  <stdin>        input framed log\n");
+	fprintf(stderr, "  <stdout>       output framed log (%x)\n",
+		FRAME_MAGIC);
+	exit(1);
+}
+
+int main(int argc, char **argv)
+{
+	uint32_t session0;
+	uint32_t seq;
+	int ret;
+
+	prog = argv[0];
+
+	if (argc != 1)
+		usage();
+
+	session0 = rand32();
+	seq = 0;
+
+	for (;;) {
+		union {
+			struct frame0 v0;
+			struct frame cur;
+			uint8_t raw[sizeof(uint32_t)];
+		} u;
+		uint8_t buf[(1u << 16) + 8];
+		struct frame new;
+		uint32_t magic;
+		size_t len;
+
+		/*
+		 * read in & convert frame header
+		 */
+
+		ret = xread(STDIN_FILENO, &magic, sizeof(magic));
+		if (ret) {
+			fprintf(stderr, "Error: failed to read frame magic: "
+				"%s\n", xstrerror(ret));
+			break;
+		}
+
+		magic = be32_to_cpu(magic);
+
+		switch (magic) {
+			case FRAME_MAGIC:
+				len = sizeof(struct frame);
+				break;
+			case FRAME_MAGIC0:
+				len = sizeof(struct frame0);
+				break;
+			default:
+				fprintf(stderr, "Error: Unknown frame magic "
+					"%x\n", magic);
+				ret = 1;
+				goto err;
+		}
+
+		ret = xread(STDIN_FILENO, &u.raw[sizeof(magic)],
+			    len - sizeof(magic));
+		if (ret) {
+			fprintf(stderr, "Error: failed to read frame header: "
+				"%s\n", xstrerror(ret));
+			break;
+		}
+
+		switch (magic) {
+			case FRAME_MAGIC:
+				new = u.cur;
+				break;
+			case FRAME_MAGIC0:
+				/* juf0 had bad session numbers */
+				new.session = cpu32_to_be(session0);
+				new.tick    = u.v0.tick;
+				new.time    = 0;
+				new.len     = u.v0.len;
+				new.seq     = cpu32_to_be(seq++);
+				break;
+		}
+
+		new.magic = cpu32_to_be(FRAME_MAGIC);
+		new._pad = 0;
+
+		/*
+		 * read in payload
+		 */
+		len = be32_to_cpu(new.len);
+
+		ASSERT3U(len, <=, sizeof(buf));
+
+		ret = xread(STDIN_FILENO, buf, len);
+		if (ret) {
+			fprintf(stderr, "Error: failed to read in payload: "
+				"%s\n", xstrerror(ret));
+			break;
+		}
+
+		/*
+		 * write out new frame & data
+		 */
+
+		ret = xwrite(STDOUT_FILENO, &new, sizeof(struct frame));
+		if (ret) {
+			fprintf(stderr, "Error: failed to write frame header: "
+				"%s\n", xstrerror(ret));
+			break;
+		}
+
+		ret = xwrite(STDOUT_FILENO, buf, len);
+		if (ret) {
+			fprintf(stderr, "Error: failed to write payload: %s\n",
+				xstrerror(ret));
+			break;
+		}
+	}
+
+err:
+	return ret;
+}