view d144/atrau-parse.c @ 63:4a906c2075bc

top Makefile: d144 depends on utils
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 25 Sep 2024 18:49:39 +0000
parents 323a4b75a32d
children
line wrap: on
line source

/*
 * This program reads a binary file containing a capture of 64 kbit/s output
 * of a TRAU in 14.4 kbit/s data mode.  Per GSM specs this output is supposed
 * to contain A-TRAU frames of TS 48.020 chapter 11 (RAA' function);
 * this program looks for such frames and parses them.
 */

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

static void
decode_ra2(ts_bytes, frame_bits)
	uint8_t *ts_bytes, *frame_bits;
{
	uint8_t *ip, *op;
	unsigned n;

	ip = ts_bytes;
	op = frame_bits;
	for (n = 0; n < 160; n++) {
		*op++ = (*ip & 0x80) >> 7;
		*op++ = (*ip & 0x40) >> 6;
		ip++;
	}
}

static int
check_sync_zeros(frame_bits)
	uint8_t *frame_bits;
{
	int i;

	for (i = 0; i < 16; i++) {
		if (frame_bits[i])
			return 0;
	}
	return 1;
}

static unsigned
gather_nibble(bits)
	uint8_t *bits;
{
	unsigned n, acc;

	acc = 0;
	for (n = 0; n < 4; n++) {
		acc <<= 1;
		acc |= *bits++;
	}
	return acc;
}

static void
dump_subframe(bits)
	uint8_t *bits;
{
	unsigned n, d;

	printf("  %u ", *bits++);
	for (n = 0; n < 9; n++) {
		d = gather_nibble(bits);
		printf("%x", d);
		bits += 4;
	}
	putchar('\n');
}

static void
dump_frame(frame_bits)
	uint8_t *frame_bits;
{
	unsigned n;

	if (!check_sync_zeros(frame_bits) || !frame_bits[16])
		printf("  Bad sync bits!\n");
	printf("  C1-C5: %u%u%u%u%u\n", frame_bits[17], frame_bits[18],
		frame_bits[19], frame_bits[20], frame_bits[21]);
	printf("  M1=%u M2=%u\n", frame_bits[22], frame_bits[23]);
	for (n = 0; n < 8; n++)
		dump_subframe(frame_bits + 24 + n * 37);
}

main(argc, argv)
	char **argv;
{
	FILE *binf;
	uint8_t frame_in[160], frame_bits[320];
	u_long file_offset;
	int cc;

	if (argc != 2) {
		fprintf(stderr, "usage: %s capture.bin\n", argv[0]);
		exit(1);
	}
	binf = fopen(argv[1], "r");
	if (!binf) {
		perror(argv[1]);
		exit(1);
	}
	for (file_offset = 0; ; file_offset += 160) {
		cc = fread(frame_in, 1, 160, binf);
		if (cc <= 0)
			break;
		if (cc != 160) {
			fprintf(stderr,
				"error: %s is not a multiple of 160 bytes\n",
				argv[1]);
			exit(1);
		}
		printf("Frame at 0x%lx:\n", file_offset);
		decode_ra2(frame_in, frame_bits);
		dump_frame(frame_bits);
		putchar('\n');
	}
	exit(0);
}