view ater8/tx_func.c @ 42:ff94d7fc5891

new program itt-ater-8
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 30 Aug 2024 19:02:42 +0000
parents ater/tx_func.c@2742dbea95f1
children 3cc26391d24d
line wrap: on
line source

/*
 * Here we are going to implement Tx on Ater toward the TRAU.
 */

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

#include <osmocom/core/bits.h>
#include <osmocom/core/msgb.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 "out_frame.h"

void init_trau_ul_frame(int nr)
{
	struct ater_subslot *at = &subslots[nr];
	struct osmo_trau_frame *fr = &at->ul_frame;

	fr->type = OSMO_TRAU8_SPEECH;
	fr->dir = OSMO_TRAU_DIR_UL;
	memset(fr->c_bits + 5, 1, 3);
	fr->xc_bits[0] = 0;
	memset(fr->t_bits, 1, 2);
}

static void handle_play(struct ater_subslot *at)
{
	if (at->play_wait_align) {
		if (at->mfrm_count)
			return;
		at->play_wait_align = false;
	}
	trau_frame_from_record(at->play_buffer + at->play_buf_ptr * 22,
				&at->ul_frame, &at->frame_has_taf);
	at->play_buf_ptr++;
	if (at->play_buf_ptr < at->play_buf_total)
		return;
	free(at->play_buffer);
	at->play_buffer = NULL;
	printf("file play finished\n");
}

/* compute the odd parity bit of the given input bit sequence */
static ubit_t compute_odd_parity(const ubit_t *in, unsigned int num_bits)
{
	int i;
	unsigned int sum = 0;

	for (i = 0; i < num_bits; i++) {
		if (in[i])
			sum++;
	}

	if (sum & 1)
		return 0;
	else
		return 1;
}

static void tx_service_subslot(int nr)
{
	struct ater_subslot *at = &subslots[nr];
	struct osmo_trau_frame *fr = &at->ul_frame;
	ubit_t taf;
	struct msgb *msg;
	int len;

	if (!at->is_active)
		return;
	if (at->play_buffer)
		handle_play(at);
	at->mfrm_count++;
	if (at->mfrm_count >= 12) {
		at->mfrm_count = 0;
		taf = 1;
	} else {
		taf = 0;
	}
	if (at->frame_has_taf)
		fr->xc_bits[3] = taf;
	fr->xc_bits[5] = compute_odd_parity(fr->xc_bits, 5);

	msg = msgb_alloc_c(g_ctx, 320, "TRAU-UL-frame");
	if (!msg)
		return;
	len = osmo_trau_frame_encode(msg->tail, msgb_tailroom(msg), fr);
	if (len <= 0) {
		msgb_free(msg);
		return;
	}
	msgb_put(msg, len);
	osmo_i460_mux_enqueue(at->schan, msg);
}

void transmit_e1_ts(void)
{
	uint8_t buf[160];
	int nr;

	for (nr = 0; nr < ATER_SUBSLOTS; nr++)
		tx_service_subslot(nr);
	osmo_i460_mux_out(&i460_ts, buf, 160);
	write(ts_fd, buf, 160);
}