changeset 142:d41edd329670

fc-cal2text utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 Feb 2017 03:37:11 +0000
parents 6b01d4ef85c3
children daba926cc38a
files .hgignore ffstools/cal2text/Makefile ffstools/cal2text/dircheck.c ffstools/cal2text/main.c ffstools/cal2text/mkdir.c ffstools/cal2text/smallconv.c
diffstat 6 files changed, 345 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Feb 26 23:37:56 2017 +0000
+++ b/.hgignore	Mon Feb 27 03:37:11 2017 +0000
@@ -5,6 +5,7 @@
 \.bin$
 \.srec$
 
+^ffstools/cal2text/fc-cal2text$
 ^ffstools/tiffs-rd/tiffs$
 ^ffstools/tiffs-wrappers/mokoffs$
 ^ffstools/tiffs-wrappers/pirffs$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ffstools/cal2text/Makefile	Mon Feb 27 03:37:11 2017 +0000
@@ -0,0 +1,20 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	fc-cal2text
+OBJS=	dircheck.o main.o mkdir.o rftablewr.o smallconv.o
+INSTBIN=/opt/freecalypso/bin
+
+all:	${PROG}
+
+${PROG}:	${OBJS}
+	${CC} -o $@ ${OBJS}
+
+rftablewr.o:	../../rvinterf/tmsh/rftablewr.c
+	${CC} ${CFLAGS} -c -o $@ $<
+
+install:	${PROG}
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f ${PROG} *.o *.out *errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ffstools/cal2text/dircheck.c	Mon Feb 27 03:37:11 2017 +0000
@@ -0,0 +1,26 @@
+/*
+ * Both arguments to fc-cal2text must name directories that already exist.
+ * Here we implement the check for this requirement.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+check_directory_exists(pathname)
+	char *pathname;
+{
+	struct stat st;
+
+	if (stat(pathname, &st) < 0) {
+		perror(pathname);
+		exit(1);
+	}
+	if (!S_ISDIR(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a directory\n", pathname);
+		exit(1);
+	}
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ffstools/cal2text/main.c	Mon Feb 27 03:37:11 2017 +0000
@@ -0,0 +1,231 @@
+/*
+ * The main() function for fc-cal2text lives here.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+char *input_dir_base, *output_dir_base;
+
+extern void write_afcdac_ascii();
+extern void write_stdmap_ascii();
+extern void write_afcparams_table();
+extern void write_agcwords_table();
+extern void write_agcglobals_table();
+extern void write_il2agc_table();
+extern void write_tx_levels_table();
+extern void write_tx_calchan_table();
+extern void write_tx_caltemp_table();
+extern void write_rx_calchan_table();
+extern void write_rx_caltemp_table();
+extern void write_rx_agcparams_table();
+extern void write_tx_ramp();
+
+struct output_chunk {
+	unsigned	offset;
+	char		*outfile;
+	void		(*conv_func)();
+};
+
+static struct output_chunk afcdac_handling = {
+	0, "afcdac", write_afcdac_ascii
+};
+
+static struct output_chunk stdmap_handling = {
+	0, "stdmap", write_stdmap_ascii
+};
+
+static struct output_chunk afcparams_handling = {
+	0, "afcparams", write_afcparams_table
+};
+
+static struct output_chunk agcglobals_handling = {
+	0, "agcglobals", write_agcglobals_table
+};
+
+static struct output_chunk il2agc_handling[3] = {
+	{0,	"il2agc-pwr",	write_il2agc_table},
+	{121,	"il2agc-max",	write_il2agc_table},
+	{242,	"il2agc-av",	write_il2agc_table},
+};
+
+static struct output_chunk agcwords_handling = {
+	0, "agcwords", write_agcwords_table
+};
+
+static struct output_chunk tx_ramps_handling[16] = {
+	{0x000, "ramp00", write_tx_ramp},
+	{0x020, "ramp01", write_tx_ramp},
+	{0x040, "ramp02", write_tx_ramp},
+	{0x060, "ramp03", write_tx_ramp},
+	{0x080, "ramp04", write_tx_ramp},
+	{0x0A0, "ramp05", write_tx_ramp},
+	{0x0C0, "ramp06", write_tx_ramp},
+	{0x0E0, "ramp07", write_tx_ramp},
+	{0x100, "ramp08", write_tx_ramp},
+	{0x120, "ramp09", write_tx_ramp},
+	{0x140, "ramp10", write_tx_ramp},
+	{0x160, "ramp11", write_tx_ramp},
+	{0x180, "ramp12", write_tx_ramp},
+	{0x1A0, "ramp13", write_tx_ramp},
+	{0x1C0, "ramp14", write_tx_ramp},
+	{0x1E0, "ramp15", write_tx_ramp},
+};
+
+static struct output_chunk tx_levels_handling = {
+	0, "levels", write_tx_levels_table
+};
+
+static struct output_chunk tx_calchan_handling = {
+	0, "calchan", write_tx_calchan_table
+};
+ 
+static struct output_chunk tx_caltemp_handling = {
+	0, "caltemp", write_tx_caltemp_table
+};
+ 
+static struct output_chunk rx_calchan_handling = {
+	0, "calchan", write_rx_calchan_table
+};
+ 
+static struct output_chunk rx_caltemp_handling = {
+	0, "caltemp", write_rx_caltemp_table
+};
+
+static struct output_chunk rx_agcparams_handling = {
+	0, "agcparams", write_rx_agcparams_table
+};
+
+static struct input_file {
+	char		*filename;
+	unsigned	expect_size;
+	char		*outdir;
+	struct output_chunk *handling;
+	unsigned	nchunks;
+} input_file_list[] = {
+	{"afcdac",            2,   "global",  &afcdac_handling,       1},
+	{"stdmap",            2,   "global",  &stdmap_handling,       1},
+	{"afcparams",         24,  "global",  &afcparams_handling,    1},
+	{"rx/agcglobals",     8,   "global",  &agcglobals_handling,   1},
+	{"rx/il2agc",         363, "global",   il2agc_handling,       3},
+	{"rx/agcwords",	      40,  "global",  &agcwords_handling,     1},
+
+	{"tx/ramps.850",      512, "tx-850",   tx_ramps_handling,     16},
+	{"tx/levels.850",     128, "tx-850",  &tx_levels_handling,    1},
+	{"tx/calchan.850",    128, "tx-850",  &tx_calchan_handling,   1},
+	{"tx/caltemp.850",    40,  "tx-850",  &tx_caltemp_handling,   1},
+	{"rx/calchan.850",    40,  "rx-850",  &rx_calchan_handling,   1},
+	{"rx/caltemp.850",    44,  "rx-850",  &rx_caltemp_handling,   1},
+	{"rx/agcparams.850",  8,   "rx-850",  &rx_agcparams_handling, 1},
+
+	{"tx/ramps.900",      512, "tx-900",   tx_ramps_handling,     16},
+	{"tx/levels.900",     128, "tx-900",  &tx_levels_handling,    1},
+	{"tx/calchan.900",    128, "tx-900",  &tx_calchan_handling,   1},
+	{"tx/caltemp.900",    40,  "tx-900",  &tx_caltemp_handling,   1},
+	{"rx/calchan.900",    40,  "rx-900",  &rx_calchan_handling,   1},
+	{"rx/caltemp.900",    44,  "rx-900",  &rx_caltemp_handling,   1},
+	{"rx/agcparams.900",  8,   "rx-900",  &rx_agcparams_handling, 1},
+
+	{"tx/ramps.1800",     512, "tx-1800",  tx_ramps_handling,     16},
+	{"tx/levels.1800",    128, "tx-1800", &tx_levels_handling,    1},
+	{"tx/calchan.1800",   128, "tx-1800", &tx_calchan_handling,   1},
+	{"tx/caltemp.1800",   40,  "tx-1800", &tx_caltemp_handling,   1},
+	{"rx/calchan.1800",   40,  "rx-1800", &rx_calchan_handling,   1},
+	{"rx/caltemp.1800",   44,  "rx-1800", &rx_caltemp_handling,   1},
+	{"rx/agcparams.1800", 8,   "rx-1800", &rx_agcparams_handling, 1},
+
+	{"tx/ramps.1900",     512, "tx-1900",  tx_ramps_handling,     16},
+	{"tx/levels.1900",    128, "tx-1900", &tx_levels_handling,    1},
+	{"tx/calchan.1900",   128, "tx-1900", &tx_calchan_handling,   1},
+	{"tx/caltemp.1900",   40,  "tx-1900", &tx_caltemp_handling,   1},
+	{"rx/calchan.1900",   40,  "rx-1900", &rx_calchan_handling,   1},
+	{"rx/caltemp.1900",   44,  "rx-1900", &rx_caltemp_handling,   1},
+	{"rx/agcparams.1900", 8,   "rx-1900", &rx_agcparams_handling, 1},
+
+	{0,                   0,   0,         0,                      0}
+};
+
+static
+try_read_input_file(ip, buf)
+	struct input_file *ip;
+	u_char *buf;
+{
+	char pathname[MAXPATHLEN];
+	int fd;
+	struct stat st;
+
+	sprintf(pathname, "%s/%s", input_dir_base, ip->filename);
+	fd = open(pathname, O_RDONLY);
+	if (fd < 0)
+		return(0);
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		close(fd);
+		fprintf(stderr, "%s is not a regular file, skipping\n",
+			pathname);
+		return(0);
+	}
+	if (st.st_size != ip->expect_size) {
+		close(fd);
+		fprintf(stderr,
+		"%s has the wrong length (expected %u bytes), skipping\n",
+			pathname, ip->expect_size);
+		return(0);
+	}
+	read(fd, buf, ip->expect_size);
+	close(fd);
+	return(1);
+}
+
+static void
+write_outputs(ip, buf)
+	struct input_file *ip;
+	u_char *buf;
+{
+	char pathname[MAXPATHLEN];
+	struct output_chunk *op;
+	unsigned nc;
+	FILE *of;
+
+	sprintf(pathname, "%s/%s", output_dir_base, ip->outdir);
+	mkdir_existok(pathname);
+	op = ip->handling;
+	for (nc = 0; nc < ip->nchunks; op++, nc++) {
+		sprintf(pathname, "%s/%s/%s", output_dir_base, ip->outdir,
+			op->outfile);
+		of = fopen(pathname, "w");
+		if (!of) {
+			perror(pathname);
+			exit(1);
+		}
+		op->conv_func(buf + op->offset, of);
+		fclose(of);
+	}
+}
+ 
+main(argc, argv)
+	char **argv;
+{
+	struct input_file *ip;
+	u_char buf[512];
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s input-dir output-dir\n", argv[0]);
+		exit(1);
+	}
+	check_directory_exists(argv[1]);
+	input_dir_base = argv[1];
+	check_directory_exists(argv[2]);
+	output_dir_base = argv[2];
+
+	for (ip = input_file_list; ip->filename; ip++) {
+		if (try_read_input_file(ip, buf))
+			write_outputs(ip, buf);
+	}
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ffstools/cal2text/mkdir.c	Mon Feb 27 03:37:11 2017 +0000
@@ -0,0 +1,31 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+mkdir_existok(pathname)
+	char *pathname;
+{
+	int rc;
+	struct stat st;
+
+	rc = stat(pathname, &st);
+	if (rc < 0) {
+		rc = mkdir(pathname, 0777);
+		if (rc < 0) {
+			perror(pathname);
+			exit(1);
+		}
+		return(0);
+	} else {
+		if (S_ISDIR(st.st_mode))
+			return(0);
+		else {
+			fprintf(stderr,
+			"error: %s already exists and is not a directory\n",
+				pathname);
+			exit(1);
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ffstools/cal2text/smallconv.c	Mon Feb 27 03:37:11 2017 +0000
@@ -0,0 +1,36 @@
+/*
+ * /gsm/rf/afcdac and /gsm/rf/stdmap each store a single 16-bit value,
+ * and are not tables in the rftw/rftr sense, hence the code in rftablewr.c
+ * does not handle these two.  However, in fc-cal2text we would like to
+ * handle their conversion from binary to ASCII the same way as the bigger
+ * tables, hence the two functions in this module.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <endian.h>
+
+void
+write_afcdac_ascii(bin, outf)
+	uint16_t *bin;
+	FILE *outf;
+{
+	int i;
+
+	i = le16toh(*bin);
+	if (i >= 32768)
+		i -= 65536;
+	fprintf(outf, "%d\n", i);
+}
+
+void
+write_stdmap_ascii(bin, outf)
+	uint16_t *bin;
+	FILE *outf;
+{
+	int i;
+
+	i = le16toh(*bin);
+	fprintf(outf, "0x%04X\n", i);
+}