view frtest/cvt-dlcap.c @ 137:b7ea278390eb

gsmfr-cvt-dlcap: support new FC TCH DL recording format
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 13 Dec 2022 04:37:45 +0000
parents 7960744ba19c
children be57e06bed84
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>
#include <string.h>
#include <strings.h>

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

static
parse_classic_part(line, status_words, tidsp_bytes)
	char *line;
	u_short *status_words;
	u_char *tidsp_bytes;
{
	char *cp;
	int i;

	/* grok DSP status words */
	cp = line;
	for (i = 0; i < 3; i++) {
		if (!isxdigit(cp[0]) || !isxdigit(cp[1]) ||
		    !isxdigit(cp[2]) || !isxdigit(cp[3]))
			return -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++ != ' ')
			return -1;
	}
	/* read the frame bits */
	for (i = 0; i < 33; i++) {
		if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
			return -1;
		tidsp_bytes[i] = (decode_hex_digit(cp[0]) << 4) |
				  decode_hex_digit(cp[1]);
		cp += 2;
	}
	return 0;
}

main(argc, argv)
	char **argv;
{
	FILE *inf, *outf;
	char linebuf[128];
	int lineno, rc;
	u_short status_words[3];
	u_char tidsp_bytes[33], libgsm_bytes[33], bfi[2];
	unsigned fn_mod_104;

	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++) {
		/* support both old and new formats */
		if (isxdigit(linebuf[0]) && isxdigit(linebuf[1]) &&
		    isxdigit(linebuf[2]) && isxdigit(linebuf[3])) {
			rc = parse_classic_part(linebuf, status_words,
						tidsp_bytes);
			if (rc < 0) {
invalid:			fprintf(stderr,
				"error: %s is not in the expected format\n",
					argv[1]);
				exit(1);
			}
			fn_mod_104 = 0;		/* won't have TAF */
		} else if (!strncmp(linebuf, "FR ", 3)) {
			rc = parse_classic_part(linebuf + 3, status_words,
						tidsp_bytes);
			if (rc < 0)
				goto invalid;
			if (linebuf[84] != ' ')
				goto invalid;
			if (!isdigit(linebuf[85]))
				goto invalid;
			fn_mod_104 = strtoul(linebuf + 85, 0, 10);
		} else
			goto invalid;
		/*
		 * Bit 15 of status word 0 is buffer validity flag,
		 * bit 2 is BFI.
		 */
		if (!(status_words[0] & 0x8000) || (status_words[0] & 0x0004)) {
			bfi[0] = 0xBF;
			bfi[1] = fn_mod_104 == 60;
			fwrite(bfi, 1, 2, outf);
		} else {
			gsm0610_tidsp_to_libgsm(tidsp_bytes, libgsm_bytes);
			fwrite(libgsm_bytes, 1, 33, outf);
		}
	}
	exit(0);
}