Files
gui/core/test/dmodemctl_test.c
T
Claude 9b2206e29a Add voice calls and a local answering machine
libdmodemctl now drives dmodemd's V.253 voice mode: placing and
answering voice calls, the DLE stream and its events, G.711 mu-law,
DTMF, and an answering machine (greeting, beep, recording) that takes
messages as WAV files. It is off until the user turns it on.

Each app gains a Phone page: Linux (PulseAudio), Windows (waveOut and
waveIn, Windows 95 and later) and macOS (AVAudioEngine).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRTvbHMpudRfQWK4c4H
2026-11-11 12:15:46 +00:00

768 lines
23 KiB
C

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* dmodemctl_test.c - libdmodemctl's tests: a session against a scripted
* modem, AT!M and AT#UD as dmodemd prints them, and the terminal screen
*
* dmodemctl_test <the directory of model.txt>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dmodemctl.h"
static int failures, checks;
#define CHECK(c) check((c), #c, __FILE__, __LINE__)
#define CHECK_STR(a, b) check_str((a), (b), #a, __FILE__, __LINE__)
static void check(int ok, const char *what, const char *file, int line)
{
checks++;
if (!ok) {
failures++;
fprintf(stderr, "%s:%d: failed: %s\n", file, line, what);
}
}
static void check_str(const char *a, const char *b, const char *what,
const char *file, int line)
{
checks++;
if (strcmp(a ? a : "(null)", b ? b : "(null)")) {
failures++;
fprintf(stderr, "%s:%d: %s is \"%s\", not \"%s\"\n", file, line,
what, a ? a : "(null)", b);
}
}
/* what the session wrote to the modem, as text */
static const char *out(dmc_session *s)
{
static char b[4096];
int n = dmc_output(s, b, sizeof(b) - 1);
b[n] = '\0';
return b;
}
static const char *term(dmc_session *s)
{
static char b[8192];
int n = dmc_terminal(s, b, sizeof(b) - 1);
b[n] = '\0';
return b;
}
static void feed(dmc_session *s, const char *t)
{
dmc_feed(s, t, (int)strlen(t));
}
/* the next event of type, skipping the others; 0 if none */
static int next_of(dmc_session *s, int type, dmc_event *ev)
{
while (dmc_next_event(s, ev))
if (ev->type == type)
return 1;
return 0;
}
static char *read_file(const char *dir, const char *name)
{
char path[1024];
snprintf(path, sizeof(path), "%s/%s", dir, name);
FILE *f = fopen(path, "rb");
if (!f) {
fprintf(stderr, "can't open %s\n", path);
exit(2);
}
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
char *b = malloc((size_t)n + 1);
if (!b || fread(b, 1, (size_t)n, f) != (size_t)n)
exit(2);
b[n] = '\0';
fclose(f);
return b;
}
/* a command, its echo, its response and OK */
static void test_command(void)
{
dmc_session *s = dmc_session_new();
dmc_event ev;
CHECK(dmc_state(s) == DMC_CLOSED);
CHECK(dmc_command(s, "ATI3", 0, 0) == -1);
dmc_open(s, 0);
CHECK(dmc_state(s) == DMC_IDLE);
int tag = dmc_command(s, "I3", 0, 0);
CHECK(tag > 0);
CHECK_STR(out(s), "ATI3\r");
/* a second waits for the first */
int tag2 = dmc_command(s, "ATI0", DMC_QUIET, 0);
CHECK_STR(out(s), "");
feed(s, "ATI3\r\r\nD-Modem 1.0.0\r\n\r\nOK\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag);
CHECK(ev.ok);
CHECK_STR(ev.text, "D-Modem 1.0.0");
CHECK_STR(ev.result, "OK");
CHECK_STR(term(s), "ATI3\r\r\nD-Modem 1.0.0\r\n\r\nOK\r\n");
/* the quiet one: written now, kept off the screen */
CHECK_STR(out(s), "ATI0\r");
feed(s, "ATI0\r\r\n56000\r\nRING\r\nOK\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag2);
CHECK_STR(ev.text, "56000");
CHECK_STR(term(s), "\r\nRING\r\n");
CHECK(dmc_state(s) == DMC_RINGING);
/* ERROR */
tag = dmc_command(s, "ATX9", 0, 0);
out(s);
feed(s, "ATX9\r\r\nERROR\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && !ev.ok);
CHECK_STR(ev.result, "ERROR");
/* a timeout */
dmc_poll(s, 1000);
tag = dmc_command(s, "ATI4", 0, 0);
out(s);
CHECK(dmc_poll(s, 1000) > 0);
dmc_poll(s, 7000);
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && !ev.ok);
CHECK_STR(ev.result, "timeout");
dmc_close(s);
CHECK(dmc_state(s) == DMC_CLOSED);
dmc_session_free(s);
}
/* a call: dial, connect, online data, hang up through the escape */
static void test_call(void)
{
dmc_session *s = dmc_session_new();
dmc_event ev;
dmc_open(s, 0);
int tag = dmc_dial(s, "555-1234");
CHECK_STR(out(s), "ATD555-1234\r");
CHECK(dmc_state(s) == DMC_DIALING);
/* no other command while dialling */
int later = dmc_command(s, "ATI11", DMC_QUIET, 0);
CHECK_STR(out(s), "");
feed(s, "ATD555-1234\r\r\nCONNECT 33600/ARQ/V34/LAPM/V42BIS\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && ev.ok);
CHECK(dmc_state(s) == DMC_ONLINE);
CHECK(dmc_connect_rate(s) == 33600);
CHECK_STR(dmc_connect_text(s), "CONNECT 33600/ARQ/V34/LAPM/V42BIS");
/* online: data, not results; no commands */
feed(s, "OK\r\nERROR\r\nhello\r\n");
CHECK(dmc_state(s) == DMC_ONLINE);
CHECK(dmc_command(s, "ATI0", 0, 0) == -1);
/* hang up: the guard time, +++, OK, ATH */
dmc_poll(s, 100);
dmc_hangup(s);
CHECK(dmc_state(s) == DMC_HANGING_UP);
CHECK_STR(out(s), "");
dmc_poll(s, 1300);
CHECK_STR(out(s), "+++");
feed(s, "\r\nOK\r\n");
dmc_poll(s, 1400);
CHECK_STR(out(s), "ATH\r");
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* the command that waited goes now */
CHECK_STR(out(s), "ATI11\r");
feed(s, "ATI11\r\r\nLast call: 33600\r\nOK\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == later && ev.ok);
dmc_session_free(s);
}
/* the far end hangs up; a call comes in with caller ID and is
answered; a dial stopped */
static void test_unsolicited(void)
{
dmc_session *s = dmc_session_new();
dmc_event ev;
dmc_open(s, 0);
dmc_poll(s, 10);
feed(s, "\r\nRING\r\n\r\nDATE = 1106\r\nTIME = 1316\r\n"
"NMBR = 5551234\r\nNAME = D-Modem BBS\r\n\r\nRING\r\n");
CHECK(dmc_state(s) == DMC_RINGING);
CHECK_STR(dmc_caller_number(s), "5551234");
CHECK_STR(dmc_caller_name(s), "D-Modem BBS");
CHECK(next_of(s, DMC_EV_CALLER, &ev));
CHECK_STR(ev.text, "DATE=1106");
/* no more rings: idle again */
dmc_poll(s, 9000);
CHECK(dmc_state(s) == DMC_IDLE);
feed(s, "\r\nRING\r\n");
int tag = dmc_answer(s);
CHECK_STR(out(s), "ATA\r");
CHECK(dmc_state(s) == DMC_ANSWERING);
feed(s, "ATA\r\r\nCONNECT 28800\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag);
CHECK(dmc_state(s) == DMC_ONLINE);
feed(s, "data\r\nNO CARRIER\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* an automatic answer (S0) */
feed(s, "\r\nRING\r\n\r\nCONNECT 9600\r\n");
CHECK(dmc_state(s) == DMC_ONLINE);
feed(s, "\r\nNO CARRIER\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* a dial stopped by a key */
dmc_dial(s, "1");
out(s);
dmc_hangup(s);
CHECK_STR(out(s), "\r");
feed(s, "ATD1\r\r\nNO CARRIER\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
dmc_session_free(s);
}
/* what the user types: ATD, the configurator */
static void test_typed(void)
{
dmc_session *s = dmc_session_new();
dmc_open(s, 0);
dmc_type(s, "atdt12", 6);
dmc_type(s, "3\r", 2);
CHECK_STR(out(s), "atdt123\r");
CHECK(dmc_state(s) == DMC_DIALING);
feed(s, "atdt123\r\r\nBUSY\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
dmc_type(s, "AT!CF\r", 6);
out(s);
CHECK(dmc_state(s) == DMC_CONFIGURATOR);
feed(s, "AT!CF\r\x1b[6n\x1b[0m\x1b[2J\x1b[1;1HD-Modem setup\r\n");
CHECK(dmc_state(s) == DMC_CONFIGURATOR);
feed(s, "\x1b[0m\x1b[2J\x1b[H\x1b[?25h\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* its screens as one line longer than a line is kept, the OK at the
end of it (as AT!CF's Alt-X sends it) */
dmc_type(s, "AT!CF\r", 6);
out(s);
CHECK(dmc_state(s) == DMC_CONFIGURATOR);
for (int i = 0; i < 1000; i++)
feed(s, "\x1b[0;30;47m\xe2\x96\x91");
CHECK(dmc_state(s) == DMC_CONFIGURATOR);
feed(s, "\x1b[0m\x1b[2J\x1b[H\x1b[?25hOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* refused (off hook) */
dmc_type(s, "AT!CT\r", 6);
CHECK(dmc_state(s) == DMC_CONFIGURATOR);
feed(s, "AT!CT\r\r\nERROR\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* the configurator from a command */
dmc_event ev;
int tag = dmc_command(s, "AT!CT", 0, 60000);
out(s);
CHECK(dmc_state(s) == DMC_CONFIGURATOR);
feed(s, "AT!CT\r\r\ndmodem#exit\r\n\r\nOK\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && ev.ok);
CHECK(dmc_state(s) == DMC_IDLE);
dmc_session_free(s);
}
static void test_model(const char *dir)
{
char *text = read_file(dir, "model.txt");
/* the response as the session gives it: no echo, no OK, LF */
dmc_session *s = dmc_session_new();
dmc_event ev;
dmc_open(s, 0);
dmc_command(s, "AT!M", DMC_QUIET, 0);
out(s);
feed(s, text);
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.ok);
CHECK_STR(term(s), "");
dmc_model *m = dmc_model_parse(ev.text);
CHECK(m != NULL);
CHECK(dmc_model_count(m) == 73);
int c = dmc_model_find(m, "country");
CHECK(c == 0);
CHECK(dmc_setting_kind(m, c) == DMC_PICK);
CHECK_STR(dmc_setting_value(m, c), "B5");
CHECK_STR(dmc_setting_group(m, c), "Line");
CHECK(dmc_setting_choices(m, c) > 40);
int de = -1;
for (int i = 0; i < dmc_setting_choices(m, c); i++)
if (!strcmp(dmc_choice_name(m, c, i), "germany"))
de = i;
CHECK(de >= 0);
CHECK_STR(dmc_setting_command(m, c, dmc_choice_value(m, c, de)),
"AT+GCI=42");
CHECK_STR(dmc_setting_check(m, c, "ZZ"), "Not one of the choices");
CHECK(dmc_setting_choice_of(m, c, "b5") >= 0);
int a = dmc_model_find(m, "auto-answer");
CHECK(a >= 0);
CHECK(dmc_setting_kind(m, a) == DMC_NUMBER);
CHECK(dmc_setting_lo(m, a) == 0 && dmc_setting_hi(m, a) == 255);
CHECK_STR(dmc_setting_check(m, a, "2"), "");
CHECK_STR(dmc_setting_check(m, a, "256"), "From 0 to 255");
CHECK_STR(dmc_setting_check(m, a, "x"), "Enter a number");
CHECK_STR(dmc_setting_command(m, a, "2"), "ATS0=2");
CHECK_STR(dmc_setting_command(m, a, "300"), "");
dmc_setting_set(m, a, "2");
CHECK_STR(dmc_setting_value(m, a), "2");
CHECK(dmc_model_groups(m) >= 4);
CHECK_STR(dmc_model_group(m, 0), "Line");
int text_kind = 0;
for (int i = 0; i < dmc_model_count(m); i++)
if (dmc_setting_kind(m, i) == DMC_TEXT) {
text_kind = 1;
CHECK_STR(dmc_setting_check(m, i, "1,0,1"), "");
CHECK(*dmc_setting_check(m, i, "a"));
}
CHECK(text_kind);
dmc_model_free(m);
CHECK(dmc_model_parse("OK") == NULL);
CHECK(dmc_model_parse("model\t2\t0") == NULL);
free(text);
dmc_session_free(s);
}
static void test_diag(const char *dir)
{
char *text = read_file(dir, "diag-none.txt");
dmc_diag *d = dmc_diag_parse(text);
CHECK(dmc_diag_count(d) == 3);
CHECK(!dmc_diag_has_call(d));
CHECK_STR(dmc_diag_label(d, 0), "Report version");
CHECK_STR(dmc_diag_value(d, 0), "1.0");
CHECK_STR(dmc_diag_value(d, 2), "No previous call");
dmc_diag_free(d);
free(text);
d = dmc_diag_parse(
"DIAG <2A4D3263 0=10>\nDIAG <2A4D3263 1=7>\n"
"DIAG <2A4D3263 2=0 3=0>\n"
"DIAG <2A4D3263 4=\"E0C14513\">\n"
"DIAG <2A4D3263 10=0C>\nDIAG <2A4D3263 20=C>\n"
"DIAG <2A4D3263 22=0D05>\n"
"DIAG <2A4D3263 26=8340>\nDIAG <2A4D3263 34=7080>\n"
"DIAG <2A4D3263 40=1>\nDIAG <2A4D3263 44=81>\n"
"DIAG <2A4D3263 52=0001E240>\nDIAG <2A4D3263 60=41>\n"
"DIAG <2A4D3263 9A=1>\n");
CHECK(dmc_diag_has_call(d));
CHECK(dmc_diag_count(d) == 15);
const char *want[][2] = {
{ "Call setup", "Data answer tone heard" },
{ "Mode", "Data" },
{ "DTE interface", "Asynchronous" },
{ "V.8 CM", "E0 C1 45 13" },
{ "Received level", "-12 dBm" },
{ "Transmit modulation", "V.34" },
{ "Transmit symbol rate", "3333 baud" },
{ "First transmit rate", "33,600 bit/s" },
{ "Last transmit rate", "28,800 bit/s" },
{ "Error control", "V.42 LAPM" },
{ "Compression", "V.44" },
{ "Characters sent", "123,456" },
{ "Why the call ended", "The other modem hung up" },
{ "Key 9A", "1" },
};
for (size_t w = 0; w < sizeof(want) / sizeof(want[0]); w++) {
int found = 0;
for (int i = 0; i < dmc_diag_count(d); i++)
if (!strcmp(dmc_diag_label(d, i), want[w][0])) {
CHECK_STR(dmc_diag_value(d, i), want[w][1]);
found = 1;
}
if (!found)
fprintf(stderr, "no row %s\n", want[w][0]);
CHECK(found);
}
CHECK_STR(dmc_diag_raw(d, 0x60), "41");
CHECK(dmc_diag_raw(d, 0x61) == NULL);
dmc_diag_free(d);
}
static void test_vt(void)
{
dmc_vt *t = dmc_vt_new(20, 5, 100);
const char *s = "ATI3\r\r\nD-Modem\r\n\r\nOK\r\n";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK(dmc_vt_changed(t));
CHECK(!dmc_vt_changed(t));
CHECK_STR(dmc_vt_text(t, 0), "ATI3");
CHECK_STR(dmc_vt_text(t, 1), "D-Modem");
CHECK_STR(dmc_vt_text(t, 3), "OK");
CHECK(dmc_vt_cursor_row(t) == 4 && dmc_vt_cursor_col(t) == 0);
/* scrolling into the scrollback */
s = "a\r\nb\r\nc\r\n";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK(dmc_vt_history(t) == 3);
CHECK_STR(dmc_vt_text(t, -3), "ATI3");
CHECK_STR(dmc_vt_text(t, -1), "");
CHECK_STR(dmc_vt_text(t, 0), "OK");
/* the configurator's way: clear, move, colours, UTF-8 */
s = "\x1b[0m\x1b[?25l\x1b[2J\x1b[2;3H\x1b[0;1;37;44m\xe2\x94\x8c"
"Line\x1b[0;30;47m\xc3\xa9";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK(!dmc_vt_cursor_visible(t));
CHECK(dmc_vt_char(t, 1, 2) == 0x250C);
CHECK(dmc_vt_fg(t, 1, 2) == 7 && dmc_vt_bg(t, 1, 2) == 4);
CHECK(dmc_vt_attr(t, 1, 2) == DMC_BOLD);
CHECK(dmc_vt_char(t, 1, 7) == 0xE9);
CHECK(dmc_vt_fg(t, 1, 7) == 0 && dmc_vt_bg(t, 1, 7) == 7);
CHECK_STR(dmc_vt_text(t, 1), " \xe2\x94\x8cLine\xc3\xa9");
/* the size probe: to the far corner and asked where */
char r[32];
s = "\x1b[999;999H\x1b[6n";
dmc_vt_feed(t, s, (int)strlen(s));
int n = dmc_vt_reply(t, r, sizeof(r) - 1);
r[n] = '\0';
CHECK_STR(r, "\x1b[5;20R");
/* and the UTF-8 probe: CR, é, asked again: column 2 */
s = "\r\xc3\xa9\x1b[6n";
dmc_vt_feed(t, s, (int)strlen(s));
n = dmc_vt_reply(t, r, sizeof(r) - 1);
r[n] = '\0';
CHECK_STR(r, "\x1b[5;2R");
/* erase to the end of the line, insert, delete */
s = "\x1b[3;1Habcdef\x1b[3;3H\x1b[K";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK_STR(dmc_vt_text(t, 2), "ab");
s = "\x1b[3;1H\x1b[2@";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK_STR(dmc_vt_text(t, 2), " ab");
s = "\x1b[3P";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK_STR(dmc_vt_text(t, 2), "b");
/* a wrapped line */
s = "\x1b[4;1H01234567890123456789X";
dmc_vt_feed(t, s, (int)strlen(s));
CHECK_STR(dmc_vt_text(t, 3), "01234567890123456789");
CHECK_STR(dmc_vt_text(t, 4), "X");
/* keys */
n = dmc_vt_key(t, DMC_KEY_F2, r, sizeof(r));
CHECK(n == 3 && !memcmp(r, "\x1bOQ", 3));
n = dmc_vt_key(t, DMC_KEY_UP, r, sizeof(r));
CHECK(n == 3 && !memcmp(r, "\x1b[A", 3));
/* resized */
dmc_vt_resize(t, 80, 24);
CHECK(dmc_vt_cols(t) == 80 && dmc_vt_rows(t) == 24);
CHECK_STR(dmc_vt_text(t, 3), "01234567890123456789");
CHECK(dmc_vt_palette(4) == 0x0000AA);
dmc_vt_free(t);
}
static void test_daemon(void)
{
char b[256];
CHECK(dmc_daemon_ready("Use `/dev/ttyDM0' as modem device, Ctrl+C "
"for termination.", b, sizeof(b)));
CHECK_STR(b, "/dev/ttyDM0");
CHECK(dmc_daemon_ready("Use `/dev/pts/3' as modem device, Ctrl+C for "
"termination.", b, sizeof(b)));
CHECK_STR(b, "/dev/pts/3");
CHECK(dmc_daemon_ready("Use TCP 127.0.0.1:2323 as the modem, Ctrl+C "
"for termination.", b, sizeof(b)));
CHECK_STR(b, "TCP 127.0.0.1:2323");
CHECK(!dmc_daemon_ready("modem `dmodem0' created. TTY is `/dev/pts/3'",
b, sizeof(b)));
CHECK(dmc_daemon_error("error: cannot create PTY."));
CHECK(!dmc_daemon_error("13:21:22.774 sip: SIP on 127.0.0.1:5060"));
}
/* the session's bytes to the modem, binary: how many into b */
static int outb(dmc_session *s, unsigned char *b, int max)
{
return dmc_output(s, (char *)b, max);
}
static void feedb(dmc_session *s, const unsigned char *b, int n)
{
dmc_feed(s, (const char *)b, n);
}
/* the setup of a voice call: voice mode, mu-law, then cmd and its
result; the stream started */
static void voice_setup(dmc_session *s, const char *cmd, int state)
{
CHECK_STR(out(s), "AT+FCLASS=8\r");
feed(s, "AT+FCLASS=8\r\r\nOK\r\n");
CHECK_STR(out(s), "AT+VSM=4\r");
feed(s, "AT+VSM=4\r\r\nOK\r\n");
char want[64];
snprintf(want, sizeof(want), "%s\r", cmd);
CHECK_STR(out(s), want);
snprintf(want, sizeof(want), "%s\r\r\nOK\r\n", cmd);
feed(s, want);
CHECK(dmc_state(s) == state);
CHECK_STR(out(s), "AT+VTR\r");
term(s);
feed(s, "AT+VTR\r\r\nCONNECT\r\n");
/* (none of it on the screen) */
CHECK_STR(term(s), "");
}
/* a voice call dialled: the samples both ways, DTMF, hanging up */
static void test_voice(void)
{
dmc_session *s = dmc_session_new();
dmc_event ev;
static unsigned char b[65536];
int16_t pcm[256];
dmc_open(s, 0);
int tag = dmc_voice_dial(s, "555-1000");
CHECK(tag > 0);
CHECK(dmc_voice_dial(s, "1") == -1);
CHECK(dmc_command(s, "ATI3", 0, 0) == -1);
CHECK_STR(out(s), "AT+FCLASS=8\r");
feed(s, "AT+FCLASS=8\r\r\nOK\r\n");
CHECK_STR(out(s), "AT+VSM=4\r");
feed(s, "AT+VSM=4\r\r\nOK\r\n");
CHECK_STR(out(s), "ATD555-1000\r");
CHECK(dmc_state(s) == DMC_DIALING);
feed(s, "ATD555-1000\r\r\nOK\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && ev.ok);
CHECK(dmc_state(s) == DMC_VOICE);
CHECK_STR(dmc_state_name(DMC_VOICE), "Voice call");
CHECK_STR(out(s), "AT+VTR\r");
term(s);
/* the stream: samples (a DLE among them doubled), a digit */
unsigned char in[] = { 'C', 'O', 'N', 'N', 'E', 'C', 'T', '\r', '\n',
0xff, 0x80, 0x10, 0x10, 0x00, 0x10, '5',
0x10, '~', 0x7f };
feedb(s, in, sizeof(in));
CHECK_STR(term(s), "");
int n = dmc_voice_read(s, pcm, 256);
CHECK(n == 5);
CHECK(pcm[0] == 0);
CHECK(pcm[1] == 32124);
CHECK(pcm[3] == -32124);
CHECK(pcm[4] == 0);
CHECK(next_of(s, DMC_EV_VOICE, &ev));
CHECK_STR(ev.text, "5");
CHECK_STR(ev.result, "digit");
CHECK(!next_of(s, DMC_EV_VOICE, &ev));
/* ours: silence, 60 ms ahead, then at the line's pace */
CHECK(dmc_poll(s, 0) == 20);
n = outb(s, b, sizeof(b));
CHECK(n == 480);
CHECK(b[0] == 0xff && b[479] == 0xff);
for (int i = 0; i < 160; i++)
pcm[i] = 8000;
dmc_voice_write(s, pcm, 160);
dmc_poll(s, 20);
n = outb(s, b, sizeof(b));
CHECK(n == 160);
CHECK(b[0] != 0xff && b[0] == b[159]);
/* a sample that is a DLE: doubled */
pcm[0] = -15996;
dmc_voice_write(s, pcm, 1);
dmc_poll(s, 21);
n = outb(s, b, sizeof(b));
CHECK(n == 9 && b[0] == 0x10 && b[1] == 0x10);
/* a DTMF digit: its tone in our samples */
CHECK(dmc_voice_dtmf(s, '!') == -1);
CHECK(dmc_voice_dtmf(s, '1') == 0);
dmc_poll(s, 41);
n = outb(s, b, sizeof(b));
int tone = 0;
for (int i = 0; i < n; i++)
if (b[i] != 0xff)
tone++;
CHECK(n == 160 && tone > 100);
/* hung up here: <DLE>^, the modem's <DLE><ETX> and OK, ATH */
dmc_hangup(s);
n = outb(s, b, sizeof(b));
CHECK(n == 2 && b[0] == 0x10 && b[1] == '^');
CHECK(dmc_state(s) == DMC_HANGING_UP);
unsigned char end[] = { 0xff, 0x10, 0x03, '\r', '\n', 'O', 'K', '\r',
'\n' };
feedb(s, end, sizeof(end));
CHECK_STR(out(s), "ATH\r");
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
CHECK(dmc_poll(s, 100) == -1);
/* the far end hangs up: <DLE>l, then the stream ended as before */
tag = dmc_voice_dial(s, "1");
CHECK_STR(out(s), "AT+FCLASS=8\r");
feed(s, "AT+FCLASS=8\r\r\nOK\r\nAT+VSM=4\r\r\nOK\r\n");
CHECK_STR(out(s), "AT+VSM=4\rATD1\r");
feed(s, "ATD1\r\r\nOK\r\nAT+VTR\r\r\nCONNECT\r\n");
CHECK(dmc_state(s) == DMC_VOICE);
unsigned char lci[] = { 0x10, 'l' };
feedb(s, lci, 2);
CHECK(next_of(s, DMC_EV_VOICE, &ev));
CHECK_STR(ev.result, "hangup");
n = outb(s, b, sizeof(b));
CHECK(n >= 2 && b[n - 2] == 0x10 && b[n - 1] == '^');
feedb(s, end + 1, sizeof(end) - 1);
CHECK_STR(out(s), "ATH\r");
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* busy: the call given up, the modem on hook in data mode */
tag = dmc_voice_dial(s, "2");
out(s);
feed(s, "AT+FCLASS=8\r\r\nOK\r\nAT+VSM=4\r\r\nOK\r\n");
out(s);
feed(s, "ATD2\r\r\nBUSY\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && !ev.ok);
CHECK_STR(ev.result, "BUSY");
CHECK(dmc_state(s) == DMC_IDLE);
CHECK_STR(out(s), "ATH\r");
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* a modem with no voice mode: the call refused */
tag = dmc_voice_dial(s, "3");
out(s);
feed(s, "AT+FCLASS=8\r\r\nERROR\r\n");
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
CHECK(ev.tag == tag && !ev.ok);
CHECK_STR(out(s), "ATH\r");
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_command(s, "ATI3", 0, 0) > 0);
CHECK_STR(out(s), "ATI3\r");
feed(s, "ATI3\r\r\nOK\r\n");
/* answered: RING, AT+VLS=1 */
CHECK(dmc_voice_answer(s) == -1);
feed(s, "RING\r\n");
CHECK(dmc_ring_count(s) == 1);
tag = dmc_voice_answer(s);
CHECK(tag > 0);
voice_setup(s, "AT+VLS=1", DMC_VOICE);
dmc_close(s);
CHECK(dmc_state(s) == DMC_CLOSED);
dmc_session_free(s);
}
/* the answering machine: answers after two RINGs, the greeting, the
beep, the caller's message until they are quiet */
static void test_voicemail(void)
{
dmc_session *s = dmc_session_new();
dmc_event ev;
static unsigned char b[65536];
static int16_t greeting[800];
for (int i = 0; i < 800; i++)
greeting[i] = (int16_t)(i & 1 ? 6000 : -6000);
dmc_voicemail_set(s, greeting, 800, 10, 1);
dmc_voicemail_auto(s, 2);
dmc_open(s, 0);
feed(s, "RING\r\n\r\nNMBR = 5551234\r\n");
CHECK_STR(out(s), "");
feed(s, "RING\r\n");
voice_setup(s, "AT+VLS=1", DMC_VOICEMAIL);
CHECK_STR(dmc_state_name(DMC_VOICEMAIL), "Taking a message");
/* the greeting, then the beep: 0.1 s and 0.5 s */
dmc_poll(s, 0);
dmc_poll(s, 1000);
int n = outb(s, b, sizeof(b));
CHECK(n == 8480);
CHECK(b[0] == b[2] && b[0] != 0xff && b[1] != b[0]);
CHECK(b[902] != 0xff);
CHECK(b[4900] == 0xff);
/* the caller speaks for 2 s, then is quiet for 1 s */
static unsigned char talk[24000];
for (int i = 0; i < 16000; i++)
talk[i] = (unsigned char)(i & 1 ? 0x90 : 0x10 + 0x80);
memset(talk + 16000, 0xff, 8000);
feedb(s, talk, 24000);
CHECK(next_of(s, DMC_EV_MESSAGE, &ev));
CHECK(ev.ok);
CHECK_STR(ev.text, "5551234");
const int16_t *msg;
n = dmc_voice_message(s, &msg);
CHECK(n == 16000 + 4000);
CHECK(msg[0] != 0 && msg[n - 1] == 0);
n = outb(s, b, sizeof(b));
CHECK(n >= 2 && b[n - 2] == 0x10 && b[n - 1] == '^');
unsigned char end[] = { 0x10, 0x03, '\r', '\n', 'O', 'K', '\r', '\n' };
feedb(s, end, sizeof(end));
CHECK_STR(out(s), "ATH\r");
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
/* a caller who hangs up in the greeting: no message */
dmc_voicemail_auto(s, 0);
feed(s, "RING\r\n");
CHECK_STR(out(s), "");
CHECK(dmc_voicemail_answer(s) > 0);
voice_setup(s, "AT+VLS=1", DMC_VOICEMAIL);
unsigned char lci[] = { 0x10, 'l' };
feedb(s, lci, 2);
feedb(s, end, sizeof(end));
out(s);
feed(s, "ATH\r\r\nOK\r\n");
CHECK(next_of(s, DMC_EV_MESSAGE, &ev));
CHECK(!ev.ok);
CHECK(dmc_state(s) == DMC_IDLE);
/* picked up while it plays the greeting: a voice call */
feed(s, "RING\r\n");
CHECK(dmc_voicemail_answer(s) > 0);
voice_setup(s, "AT+VLS=1", DMC_VOICEMAIL);
CHECK(dmc_voice_pickup(s) == 0);
CHECK(dmc_state(s) == DMC_VOICE);
CHECK(dmc_voice_pickup(s) == -1);
dmc_hangup(s);
feedb(s, end, sizeof(end));
out(s);
feed(s, "ATH\r\r\nOK\r\n");
CHECK(dmc_state(s) == DMC_IDLE);
CHECK(!next_of(s, DMC_EV_MESSAGE, &ev));
dmc_session_free(s);
}
/* WAV files: ours read back; 16 kHz stereo made 8 kHz mono */
static void test_wav(void)
{
char h[44 + 8];
int16_t *pcm;
CHECK(dmc_wav_header(h, 4) == 44);
CHECK(!memcmp(h, "RIFF", 4) && !memcmp(h + 36, "data", 4));
CHECK((unsigned char)h[40] == 8);
memcpy(h + 44, "\x10\x00\xf0\xff\x00\x40\x00\x80", 8);
CHECK(dmc_wav_decode(h, sizeof(h), &pcm) == 4);
CHECK(pcm[0] == 16 && pcm[1] == -16 && pcm[2] == 16384 &&
pcm[3] == -32768);
dmc_free(pcm);
/* 16 kHz, two channels, 8 frames: 4 samples */
char w[44 + 32];
dmc_wav_header(w, 16);
w[22] = 2; /* channels */
w[24] = (char)0x80; w[25] = 0x3e; /* 16000 */
memset(w + 44, 0, 32);
w[44] = 0x00; w[45] = 0x10; /* frame 0: 4096 and 0 */
CHECK(dmc_wav_decode(w, sizeof(w), &pcm) == 4);
CHECK(pcm[0] == 2048 && pcm[1] == 0);
dmc_free(pcm);
CHECK(dmc_wav_decode("RIFF", 4, &pcm) == -1 && !pcm);
}
int main(int argc, char **argv)
{
const char *dir = argc > 1 ? argv[1] : ".";
CHECK(dmc_version() == DMC_VERSION);
test_command();
test_call();
test_unsolicited();
test_typed();
test_model(dir);
test_diag(dir);
test_vt();
test_daemon();
test_voice();
test_voicemail();
test_wav();
printf("%d checks, %d failed\n", checks, failures);
return failures ? 1 : 0;
}