view test-fsk/modem_rx.c @ 11:2cdbd574bba6

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

/*
 * In this module we implement modem Rx handling, revolving around
 * FSK demodulation via SpanDSP.
 */

#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 fsk_mode_rx;

#define	MAX_TEXT_LINE	80

static fsk_rx_state_t *fsk_rx_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;
	}
}

void
init_modem_rx()
{
	fsk_rx_state = fsk_rx_init(NULL, &preset_fsk_specs[fsk_mode_rx],
				   FSK_FRAME_MODE_FRAMED, byte_rx_func, NULL);
	if (!fsk_rx_state) {
		fprintf(stderr, "error: fsk_rx_init() failed!\n");
		exit(1);
	}
}

void
process_rx_frame(samples)
	int16_t *samples;
{
	fsk_rx(fsk_rx_state, samples, FRAME_20MS);
}