view test-v22/modem_func.c @ 11:2cdbd574bba6

tone-detect/Makefile: add install
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Mar 2024 12:18:49 -0800
parents 3c5734b88c20
children
line wrap: on
line source

/*
 * In this module we implement our interface to SpanDSP V.22 engine.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <spandsp.h>
#include "../include/pstn_defs.h"

extern int v22_bitrate;

#define	MAX_TEXT_LINE	80

static v22bis_state_t *modem_state;
static async_rx_state_t *async_state;
static u_char rx_line_buf[MAX_TEXT_LINE];
static unsigned rx_buf_fill;

static void
safe_print_char(c)
{
	if (c >= ' ' && c <= '~') {
		putchar(c);
		return;
	}
	switch (c) {
	case '\t':
		putchar(c);
		return;
	case '\n':
		return;
	case '\r':
		putchar('\\');
		putchar('r');
		return;
	case '\b':
		putchar('\\');
		putchar('b');
		return;
	case '\f':
		putchar('\\');
		putchar('f');
		return;
	}
	printf("\\x%02X", c);
}

static void
print_rx_line()
{
	u_char *dp, *endp;
	int c;

	fputs("MRx:\t", stdout);
	dp = rx_line_buf;
	endp = rx_line_buf + rx_buf_fill;
	while (dp < endp) {
		c = *dp++;
		safe_print_char(c);
	}
	if (c != '\n')
		putchar('\\');
	putchar('\n');
}

static void
byte_rx_func(user_data, byte)
	void *user_data;
	int byte;
{
	if (byte < 0) {
		printf("Modem state change: %s\n", signal_status_to_str(byte));
		return;
	}
	rx_line_buf[rx_buf_fill++] = byte;
	if (byte == '\n' || rx_buf_fill >= MAX_TEXT_LINE) {
		print_rx_line();
		rx_buf_fill = 0;
	}
}

static int
supply_bit()
{
	return 1;
}

void
init_modem_func()
{
	async_state = async_rx_init(NULL, 8, ASYNC_PARITY_NONE, 1, true,
				    byte_rx_func, NULL);
	if (!async_state) {
		fprintf(stderr, "error: async_rx_init() failed!\n");
		exit(1);
	}
	modem_state = v22bis_init(NULL, v22_bitrate, V22BIS_GUARD_TONE_NONE,
				  true, supply_bit, NULL, async_rx_put_bit,
				  async_state);
	if (!modem_state) {
		fprintf(stderr, "error: v22bis_init() failed!\n");
		exit(1);
	}
}

void
process_rx_frame(samples)
	int16_t *samples;
{
	v22bis_rx(modem_state, samples, FRAME_20MS);
}

void
fill_tx_frame(samples)
	int16_t *samples;
{
	v22bis_tx(modem_state, samples, FRAME_20MS);
}