changeset 20:5405c1573027

ater: implement file reading
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Jun 2024 08:50:29 +0000
parents 1e375472d5a5
children 2ee910aa03c3
files ater/Makefile ater/read_file.c ater/read_file.h
diffstat 3 files changed, 114 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ater/Makefile	Mon Jun 24 08:07:45 2024 +0000
+++ b/ater/Makefile	Mon Jun 24 08:50:29 2024 +0000
@@ -1,5 +1,7 @@
 PROG=	itt-ater-16
-OBJS=	main.o read_ts.o record_ctrl.o subslot_rx.o tx_func.o user_cmd.o
+OBJS=	main.o read_file.o read_ts.o record_ctrl.o subslot_rx.o tx_func.o \
+	user_cmd.o
+HDRS=	globals.h read_file.h submux.h
 LIBUTIL=../libutil/libutil.a
 
 include ../config.defs
@@ -9,7 +11,7 @@
 
 all:	${PROG}
 
-${OBJS}:	globals.h submux.h
+${OBJS}:	${HDRS}
 
 ${PROG}: ${OBJS} ${LIBUTIL}
 	${CC} -o $@ ${OBJS} ${LIBUTIL} ${OSMO_LINK}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/read_file.c	Mon Jun 24 08:50:29 2024 +0000
@@ -0,0 +1,98 @@
+/*
+ * Here we implement the function that reads binary files
+ * in our ad hoc format for TRAU testing.
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "read_file.h"
+
+static int check_magic(const uint8_t *buf, unsigned nframes, bool *got_fr,
+			bool *got_efr)
+{
+	unsigned n;
+
+	for (n = 0; n < nframes; n++) {
+		switch (*buf & 0xF0) {
+		case 0xC0:
+			*got_efr = true;
+			break;
+		case 0xD0:
+			*got_fr = true;
+			break;
+		default:
+			return -1;
+		}
+		buf += 34;
+	}
+	return 0;
+}
+
+int read_binary_file(const char *filename, bool is_efr, uint8_t **bufret,
+		     unsigned *size_ret)
+{
+	int fd, rc;
+	struct stat st;
+	uint8_t *buf;
+	unsigned nframes;
+	bool got_fr, got_efr;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		perror(filename);
+		return -1;
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		close(fd);
+		fprintf(stderr, "error: %s is not a regular file\n", filename);
+		return -1;
+	}
+	if (!st.st_size) {
+		close(fd);
+		fprintf(stderr, "error: %s is an empty file\n", filename);
+		return -1;
+	}
+	if (st.st_size % 34) {
+		close(fd);
+		fprintf(stderr,
+			"error: size of %s is not a multiple of 34 bytes\n",
+			filename);
+		return -1;
+	}
+	buf = malloc(st.st_size);
+	if (!buf) {
+		close(fd);
+		fprintf(stderr, "unable to malloc buffer for %s\n", filename);
+		return -1;
+	}
+	read(fd, buf, st.st_size);
+	close(fd);
+	nframes = st.st_size / 34;
+
+	got_fr = false;
+	got_efr = false;
+	rc = check_magic(buf, nframes, &got_fr, &got_efr);
+	if (rc < 0 || got_fr && got_efr) {
+		free(buf);
+		fprintf(stderr, "error: %s is not a valid TRAU-UL test file\n",
+			filename);
+		return -1;
+	}
+	if (is_efr != got_efr) {
+		free(buf);
+		fprintf(stderr, "error: %s is for the wrong codec\n", filename);
+		return -1;
+	}
+	*bufret = buf;
+	*size_ret = nframes;
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ater/read_file.h	Mon Jun 24 08:50:29 2024 +0000
@@ -0,0 +1,12 @@
+/*
+ * This header file defines the interface to the function that reads
+ * binary files in our ad hoc format for TRAU testing.
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+int read_binary_file(const char *filename, bool is_efr, uint8_t **bufret,
+		     unsigned *size_ret);