view frtest/cvt-dlcap.c @ 35:3b5958f28a40

dev: efr-sid-insert helper program
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 24 Nov 2022 02:35:02 +0000
parents 7960744ba19c
children b7ea278390eb
line wrap: on
line source

/*
 * This program reads a TCH downlink capture produced with FreeCalypso tools
 * (fw version with TCH downlink sniffing feature and fc-shell tch record)
 * and converts it into our extended-libgsm binary format, to be further
 * fed to gsmfr-decode.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

static const char bfi_marker[2] = {0xBF, 0x00};

static
decode_hex_digit(ch)
{
	if (isdigit(ch))
		return(ch - '0');
	else if (isupper(ch))
		return(ch - 'A' + 10);
	else
		return(ch - 'a' + 10);
}

main(argc, argv)
	char **argv;
{
	FILE *inf, *outf;
	char linebuf[128];
	int lineno;
	char *cp;
	int i;
	u_short status_words[3];
	u_char tidsp_bytes[33], libgsm_bytes[33];

	if (argc != 3) {
		fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
		exit(1);
	}
	inf = fopen(argv[1], "r");
	if (!inf) {
		perror(argv[1]);
		exit(1);
	}
	outf = fopen(argv[2], "w");
	if (!outf) {
		perror(argv[2]);
		exit(1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
		/* grok DSP status words */
		cp = linebuf;
		for (i = 0; i < 3; i++) {
			if (!isxdigit(cp[0]) || !isxdigit(cp[1]) ||
			    !isxdigit(cp[2]) || !isxdigit(cp[3])) {
invalid:			fprintf(stderr,
				    "error: %s is not in the expected format\n",
						argv[1]);
				exit(1);
			}
			status_words[i] = (decode_hex_digit(cp[0]) << 12) |
					  (decode_hex_digit(cp[1]) << 8) |
					  (decode_hex_digit(cp[2]) << 4) |
					   decode_hex_digit(cp[3]);
			cp += 4;
			if (*cp++ != ' ')
				goto invalid;
		}
		/* read the frame bits */
		for (i = 0; i < 33; i++) {
			if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
				goto invalid;
			tidsp_bytes[i] = (decode_hex_digit(cp[0]) << 4) |
					  decode_hex_digit(cp[1]);
			cp += 2;
		}
		/* bit 2 of status word 0 is BFI */
		if (status_words[0] & 0x0004)
			fwrite(bfi_marker, 1, 2, outf);
		else {
			gsm0610_tidsp_to_libgsm(tidsp_bytes, libgsm_bytes);
			fwrite(libgsm_bytes, 1, 33, outf);
		}
	}
	exit(0);
}