view ater/activate.c @ 52:626180a15857 default tip

ater play-d144: emit E-data frames manually, osmo_trau_frame_encode() is currently broken for this frame type
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 25 Sep 2024 06:40:43 +0000
parents 40f781efdbe1
children
line wrap: on
line source

/*
 * Here we implement the operation of activating a new TRAU channel
 * on a sub-timeslot.
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <osmocom/core/select.h>
#include <osmocom/isdn/i460_mux.h>
#include <osmocom/trau/trau_frame.h>

#include "globals.h"
#include "submux.h"
#include "read_file.h"
#include "out_frame.h"

void cmd_activate(int argc, char **argv)
{
	int nr, rc;
	bool is_efr, dtxd;
	struct ater_subslot *at;
	uint8_t *init_frame;
	unsigned init_frame_count;

	if (argc < 4 || argc > 5) {
usage:		fprintf(stderr,
			"usage: %s 0|1|2|3 fr|efr initial-frame.tul [dtxd]\n",
			argv[0]);
		return;
	}
	if (argv[1][0] < '0' || argv[1][0] > '3' || argv[1][1])
		goto usage;
	nr = argv[1][0] - '0';
	if (!strcmp(argv[2], "fr"))
		is_efr = false;
	else if (!strcmp(argv[2], "efr"))
		is_efr = true;
	else
		goto usage;
	if (argv[4]) {
		if (strcmp(argv[4], "dtxd"))
			goto usage;
		dtxd = true;
	} else
		dtxd = false;

	at = &subslots[nr];
	if (at->is_active) {
		fprintf(stderr, "error: subslot %d is already active\n", nr);
		return;
	}
	rc = read_binary_file(argv[3], is_efr, &init_frame, &init_frame_count);
	if (rc < 0)
		return;		/* error msg already printed */
	if (init_frame_count != 1) {
		free(init_frame);
		fprintf(stderr, "error: %s contains more than one frame\n",
			argv[3]);
		return;
	}

	/* good to proceed now */
	at->is_active = true;
	at->is_data = false;
	at->is_data_144 = false;
	at->is_efr = is_efr;
	init_trau_ul_frame(nr);
	at->ul_frame.c_bits[16] = dtxd;
	trau_frame_from_record(init_frame, is_efr, &at->ul_frame);
	free(init_frame);
}

void cmd_activate_csd(int argc, char **argv)
{
	int nr;
	bool ir_16k, is_144, is_hr;
	struct ater_subslot *at;

	if (argc < 3 || argc > 4) {
usage:		fprintf(stderr, "usage: %s 0|1|2|3 8|16|14.4 [hr]\n", argv[0]);
		return;
	}
	if (argv[1][0] < '0' || argv[1][0] > '3' || argv[1][1])
		goto usage;
	nr = argv[1][0] - '0';
	if (!strcmp(argv[2], "8")) {
		is_144 = false;
		ir_16k = false;
	} else if (!strcmp(argv[2], "16")) {
		is_144 = false;
		ir_16k = true;
	} else if (!strcmp(argv[2], "14.4")) {
		is_144 = true;
		ir_16k = true;
	} else
		goto usage;
	if (argv[3]) {
		if (strcmp(argv[3], "hr"))
			goto usage;
		is_hr = true;
	} else
		is_hr = false;
	if (is_hr && (is_144 || ir_16k)) {
		fprintf(stderr, "error: HR above 8 kbit/s is impossible\n");
		return;
	}

	at = &subslots[nr];
	if (at->is_active) {
		fprintf(stderr, "error: subslot %d is already active\n", nr);
		return;
	}

	/* good to proceed now */
	at->is_active = true;
	at->is_data = true;
	at->is_data_144 = is_144;
	at->is_hr_data = is_hr;
	at->d144_edata = false;
	init_trau_ul_frame_csd(nr, ir_16k);
}

void cmd_deact(int argc, char **argv)
{
	int nr;
	struct ater_subslot *at;

	if (argc != 2) {
usage:		fprintf(stderr, "usage: %s 0|1|2|3\n", argv[0]);
		return;
	}
	if (argv[1][0] < '0' || argv[1][0] > '3' || argv[1][1])
		goto usage;
	nr = argv[1][0] - '0';
	at = &subslots[nr];
	if (!at->is_active) {
		fprintf(stderr, "error: subslot %d is not active\n", nr);
		return;
	}
	at->is_active = false;
	if (at->play_buffer) {
		free(at->play_buffer);
		at->play_buffer = NULL;
	}
}