view miscutil/tw5a-dump.c @ 523:86d4ec69b36c

miscutil: new program tw5a-dump
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 19 Sep 2024 04:13:28 +0000
parents miscutil/gsmrec-dump.c@b5f8ea41362b
children
line wrap: on
line source

/*
 * This program reads a TW-TS-005 Annex A hexadecimal file
 * and dumps all frames in human-readable form.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../libgsmfr2/tw_gsmfr.h"
#include "../libgsmefr/gsm_efr.h"
#include "../libtest/tw5reader.h"

static void
dump_fr_frame(frame)
	uint8_t *frame;
{
	int16_t params[GSMFR_NUM_PARAMS];
	int i, j, n;

	printf("  SID=%d LARc", gsmfr_preproc_sid_classify(frame));
	gsmfr_unpack_to_array(frame, params);
	n = 0;
	for (i = 0; i < 8; i++)
		printf(" %d", params[n++]);
	if (!bcmp(frame, gsmfr_decoder_homing_frame, GSMFR_RTP_FRAME_LEN))
		fputs(" (DHF)", stdout);
	putchar('\n');
	for (i = 0; i < 4; i++) {
		putchar(' ');
		for (j = 0; j < 17; j++)
			printf(" %d", params[n++]);
		putchar('\n');
	}
}

static void
dump_efr_frame(frame)
	uint8_t *frame;
{
	int16_t params[EFR_NUM_PARAMS];
	int i, j, n;

	printf("  SID=%d LPC", EFR_sid_classify(frame));
	EFR_frame2params(frame, params);
	n = 0;
	for (i = 0; i < 5; i++)
		printf(" %d", params[n++]);
	if (!bcmp(frame, EFR_decoder_homing_frame, EFR_RTP_FRAME_LEN))
		fputs(" (DHF)", stdout);
	putchar('\n');
	for (i = 0; i < 4; i++) {
		putchar(' ');
		for (j = 0; j < 13; j++)
			printf(" %d", params[n++]);
		putchar('\n');
	}
}

main(argc, argv)
	char **argv;
{
	FILE *hexf;
	unsigned lineno;
	uint8_t frame[TWTS005_MAX_FRAME];
	unsigned frame_len, bfi;
	int rc;

	if (argc != 2) {
		fprintf(stderr, "usage: %s hex-file\n", argv[0]);
		exit(1);
	}
	hexf = fopen(argv[1], "r");
	if (!hexf) {
		perror(argv[1]);
		exit(1);
	}
	lineno = 0;
	for (;;) {
		rc = twts005_read_frame(hexf, &lineno, frame, &frame_len);
		if (rc < 0) {
			fprintf(stderr, "%s line %u: not valid TW-TS-005\n",
				argv[1], lineno);
			exit(1);
		}
		if (!rc)
			break;
		switch (frame_len) {
		case 0:
			printf("line %u: NULL frame\n", lineno);
			break;
		case 1:
			if ((frame[0] & 0xF6) != 0xE6)
				goto invalid;
			printf("line %u: No_Data frame\n", lineno);
			printf("  TEH=%02X (DTXd=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, frame[0] & 1);
			break;
		case GSMFR_RTP_FRAME_LEN:
			if ((frame[0] & 0xF0) != 0xD0)
				goto invalid;
			printf("line %u: FR basic frame\n", lineno);
			dump_fr_frame(frame);
			break;
		case GSMFR_RTP_FRAME_LEN+1:
			if ((frame[0] & 0xF4) != 0xE0)
				goto invalid;
			if ((frame[1] & 0xF0) != 0xD0)
				goto invalid;
			bfi = (frame[0] >> 1) & 1;
			printf("line %u: FR %s frame\n", lineno,
				bfi ? "bad" : "extended");
			printf("  TEH=%02X (DTXd=%u BFI=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, bfi, frame[0] & 1);
			dump_fr_frame(frame + 1);
			break;
		case EFR_RTP_FRAME_LEN:
			if ((frame[0] & 0xF0) != 0xC0)
				goto invalid;
			printf("line %u: EFR basic frame\n", lineno);
			dump_efr_frame(frame);
			break;
		case EFR_RTP_FRAME_LEN+1:
			if ((frame[0] & 0xF4) != 0xE0)
				goto invalid;
			if ((frame[1] & 0xF0) != 0xC0)
				goto invalid;
			bfi = (frame[0] >> 1) & 1;
			printf("line %u: EFR %s frame\n", lineno,
				bfi ? "bad" : "extended");
			printf("  TEH=%02X (DTXd=%u BFI=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, bfi, frame[0] & 1);
			dump_efr_frame(frame + 1);
			break;
		default:
		invalid:
			fprintf(stderr,
				"%s line %u: not a valid FR or EFR frame\n",
				argv[1], lineno);
			exit(1);
		}
	}
	exit(0);
}