mirror of
https://github.com/D-Modem/gui.git
synced 2026-11-28 14:19:58 -08:00
libdmodemctl: the core the desktop apps share
A plain C library with no I/O of its own, so the Windows, macOS and Linux apps can each be only views: the AT session (commands, their responses, the call's state from the result codes, hanging up through the escape), the settings model from AT!M, the call statistics from AT#UD, a small ANSI terminal screen for the configurator, and reading dmodemd's start-up lines. Tested against dmodemd's own output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRVTPinTtLUajudobBC
This commit is contained in:
15 files changed
+3619
No files matched your search
+11
@@ -0,0 +1,11 @@
|
||||
build/
|
||||
build-*/
|
||||
.vs/
|
||||
bin/
|
||||
obj/
|
||||
*.user
|
||||
.build/
|
||||
.swiftpm/
|
||||
*.xcodeproj/xcuserdata/
|
||||
DerivedData/
|
||||
.flatpak-builder/
|
||||
@@ -0,0 +1,27 @@
|
||||
# D-Modem's desktop apps: the shared C core (libdmodemctl), and the
|
||||
# Linux app (GTK 4 and libadwaita) when its libraries are there. The
|
||||
# Windows app (windows/) and the macOS app (macos/) build with their own
|
||||
# tools and take the core from here.
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dmodem-gui VERSION 1.0.0 LANGUAGES C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
option(DMODEM_GUI_LINUX "Build the GTK 4 app (linux/)" ON)
|
||||
|
||||
add_subdirectory(core)
|
||||
enable_testing()
|
||||
add_subdirectory(core/test)
|
||||
|
||||
if(DMODEM_GUI_LINUX AND UNIX AND NOT APPLE)
|
||||
find_package(PkgConfig)
|
||||
if(PkgConfig_FOUND)
|
||||
pkg_check_modules(GTK IMPORTED_TARGET gtk4>=4.10 libadwaita-1>=1.4 vte-2.91-gtk4)
|
||||
endif()
|
||||
if(GTK_FOUND)
|
||||
add_subdirectory(linux)
|
||||
else()
|
||||
message(STATUS "gtk4, libadwaita-1 or vte-2.91-gtk4 not found: the Linux app is not built")
|
||||
endif()
|
||||
endif()
|
||||
@@ -0,0 +1,26 @@
|
||||
# libdmodemctl: what the desktop apps share, in plain C with no
|
||||
# dependencies. Static for the Linux app; a DLL for the Windows app's
|
||||
# P/Invoke (-DDMODEMCTL_SHARED=ON).
|
||||
option(DMODEMCTL_SHARED "Build libdmodemctl as a shared library" OFF)
|
||||
|
||||
set(DMODEMCTL_SOURCES
|
||||
src/session.c
|
||||
src/model.c
|
||||
src/diag.c
|
||||
src/vt.c
|
||||
src/daemon.c
|
||||
src/util.c)
|
||||
|
||||
if(DMODEMCTL_SHARED)
|
||||
add_library(dmodemctl SHARED ${DMODEMCTL_SOURCES})
|
||||
target_compile_definitions(dmodemctl PRIVATE DMC_BUILD_DLL)
|
||||
set_target_properties(dmodemctl PROPERTIES C_VISIBILITY_PRESET hidden)
|
||||
else()
|
||||
add_library(dmodemctl STATIC ${DMODEMCTL_SOURCES})
|
||||
endif()
|
||||
target_include_directories(dmodemctl PUBLIC include)
|
||||
if(MSVC)
|
||||
target_compile_options(dmodemctl PRIVATE /W4 /wd4996)
|
||||
else()
|
||||
target_compile_options(dmodemctl PRIVATE -Wall -Wextra -Wshadow)
|
||||
endif()
|
||||
@@ -0,0 +1,295 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* dmodemctl.h - the part of D-Modem's desktop apps that is not drawing.
|
||||
*
|
||||
* The Windows, macOS and Linux apps are thin native shells over this
|
||||
* small C library, so that each of them is only views. It does no I/O
|
||||
* of its own and starts no threads: each app opens the modem's DTE (a
|
||||
* pty, a TCP socket) in its own way, hands the bytes it reads to
|
||||
* dmc_feed(), writes what dmc_output() gives it, and calls dmc_poll()
|
||||
* from a timer. That keeps it simple to call from C (GTK), C#
|
||||
* (P/Invoke) and Swift.
|
||||
*
|
||||
* Its parts:
|
||||
* - dmc_session: the AT conversation with dmodemd: commands and their
|
||||
* responses, the call's state from the result codes (RING, CONNECT,
|
||||
* NO CARRIER), the escape to command mode and hanging up, and the
|
||||
* stream for the app's terminal view.
|
||||
* - dmc_model: the modem's settings, from AT!M, for a native settings
|
||||
* page.
|
||||
* - dmc_diag: the last call's statistics, from AT#UD (Microsoft's
|
||||
* Unimodem diagnostics), as labelled rows.
|
||||
* - dmc_vt: a small ANSI terminal screen, for the apps whose toolkit
|
||||
* has no terminal widget (Windows and macOS): the AT console and the
|
||||
* full-screen configurator (AT!CF).
|
||||
* - dmc_daemon_*: starting dmodemd and reading what it prints.
|
||||
*
|
||||
* Strings are UTF-8. A const char * the library returns stays valid
|
||||
* until the next call on the same object.
|
||||
*/
|
||||
#ifndef DMODEMCTL_H
|
||||
#define DMODEMCTL_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) && defined(DMC_BUILD_DLL)
|
||||
#define DMC_API __declspec(dllexport)
|
||||
#elif defined(_WIN32) && defined(DMC_USE_DLL)
|
||||
#define DMC_API __declspec(dllimport)
|
||||
#elif defined(__GNUC__)
|
||||
#define DMC_API __attribute__((visibility("default")))
|
||||
#else
|
||||
#define DMC_API
|
||||
#endif
|
||||
|
||||
/* the library's version: major * 10000 + minor * 100 + patch */
|
||||
#define DMC_VERSION 10000
|
||||
DMC_API int dmc_version(void);
|
||||
|
||||
/*
|
||||
* The session
|
||||
*/
|
||||
|
||||
typedef struct dmc_session dmc_session;
|
||||
|
||||
/* the call's state, as the result codes tell it */
|
||||
enum dmc_state {
|
||||
DMC_CLOSED = 0, /* no DTE open */
|
||||
DMC_IDLE, /* command mode, on hook */
|
||||
DMC_DIALING, /* ATD sent, no result yet */
|
||||
DMC_RINGING, /* RING heard, not answered */
|
||||
DMC_ANSWERING, /* ATA sent, no result yet */
|
||||
DMC_ONLINE, /* CONNECT: data mode */
|
||||
DMC_ONLINE_COMMAND, /* escaped (+++) during a call */
|
||||
DMC_HANGING_UP, /* going back on hook */
|
||||
DMC_CONFIGURATOR /* AT!C, AT!CF or AT!CT runs */
|
||||
};
|
||||
|
||||
/* what dmc_next_event() reports */
|
||||
enum dmc_event_type {
|
||||
DMC_EV_NONE = 0,
|
||||
/* a command of dmc_command()'s is done: tag, ok, text (its lines,
|
||||
apart by '\n', without the echo and the final result), result
|
||||
(the final result: OK, ERROR, CONNECT 33600..., or "timeout") */
|
||||
DMC_EV_RESPONSE,
|
||||
/* the state changed: state */
|
||||
DMC_EV_STATE,
|
||||
/* a result code nobody waited for (RING, NO CARRIER...): result */
|
||||
DMC_EV_RESULT,
|
||||
/* caller ID after a RING: text "NMBR=5551234" and the like */
|
||||
DMC_EV_CALLER
|
||||
};
|
||||
|
||||
typedef struct dmc_event {
|
||||
int type;
|
||||
int tag;
|
||||
int ok;
|
||||
int state;
|
||||
const char *text;
|
||||
const char *result;
|
||||
} dmc_event;
|
||||
|
||||
/* flags of dmc_command() */
|
||||
enum {
|
||||
/* its echo and response kept out of the terminal stream (the app's
|
||||
own queries: AT!M, AT#UD) */
|
||||
DMC_QUIET = 1
|
||||
};
|
||||
|
||||
DMC_API dmc_session *dmc_session_new(void);
|
||||
DMC_API void dmc_session_free(dmc_session *s);
|
||||
|
||||
/* the DTE opened (state IDLE) or closed (state CLOSED); either clears
|
||||
what was queued */
|
||||
DMC_API void dmc_open(dmc_session *s, int64_t now_ms);
|
||||
DMC_API void dmc_close(dmc_session *s);
|
||||
|
||||
/* bytes the modem sent */
|
||||
DMC_API void dmc_feed(dmc_session *s, const char *data, int n);
|
||||
/* bytes the user typed in the terminal view, sent as they are (the
|
||||
session reads the command lines in them, to follow ATD, ATA, ATH and
|
||||
AT!C typed by hand) */
|
||||
DMC_API void dmc_type(dmc_session *s, const char *data, int n);
|
||||
/* the time now: runs the timers (a command's timeout, the escape's
|
||||
guard times); returns the ms until it next needs to be called, -1 if
|
||||
only when something happens */
|
||||
DMC_API int dmc_poll(dmc_session *s, int64_t now_ms);
|
||||
|
||||
/* bytes to write to the modem: up to max into buf, how many */
|
||||
DMC_API int dmc_output(dmc_session *s, char *buf, int max);
|
||||
/* bytes for the terminal view: up to max into buf, how many (what the
|
||||
modem sent, less the DMC_QUIET commands') */
|
||||
DMC_API int dmc_terminal(dmc_session *s, char *buf, int max);
|
||||
/* the next event into *ev: 1, or 0 for none */
|
||||
DMC_API int dmc_next_event(dmc_session *s, dmc_event *ev);
|
||||
|
||||
/* an AT command (with or without the AT, no CR), queued behind any
|
||||
other; timeout_ms 0 for the usual (5 s, ATD and ATA two minutes);
|
||||
returns its tag (above 0) for the DMC_EV_RESPONSE, or -1 (closed,
|
||||
or online) */
|
||||
DMC_API int dmc_command(dmc_session *s, const char *cmd, int flags,
|
||||
int timeout_ms);
|
||||
/* dial a number (ATD), answer (ATA): a tag as dmc_command()'s */
|
||||
DMC_API int dmc_dial(dmc_session *s, const char *number);
|
||||
DMC_API int dmc_answer(dmc_session *s);
|
||||
/* hang up whatever the state: online, the escape (+++, with its guard
|
||||
times) and ATH; dialling, a key to stop it; else ATH */
|
||||
DMC_API void dmc_hangup(dmc_session *s);
|
||||
|
||||
DMC_API int dmc_state(const dmc_session *s);
|
||||
/* the state's name for people: "Idle", "Dialling", "Online" */
|
||||
DMC_API const char *dmc_state_name(int state);
|
||||
/* the last CONNECT's text ("CONNECT 33600/ARQ/V34/LAPM/V42BIS"), its
|
||||
rate (0 none), the caller ID's number and name of the last RING */
|
||||
DMC_API const char *dmc_connect_text(const dmc_session *s);
|
||||
DMC_API int dmc_connect_rate(const dmc_session *s);
|
||||
DMC_API const char *dmc_caller_number(const dmc_session *s);
|
||||
DMC_API const char *dmc_caller_name(const dmc_session *s);
|
||||
|
||||
/*
|
||||
* The settings model (AT!M)
|
||||
*/
|
||||
|
||||
typedef struct dmc_model dmc_model;
|
||||
|
||||
enum dmc_kind { DMC_PICK = 0, DMC_NUMBER, DMC_TEXT };
|
||||
|
||||
/* the model from AT!M's response text (dmc_event.text); NULL if it is
|
||||
not one */
|
||||
DMC_API dmc_model *dmc_model_parse(const char *text);
|
||||
DMC_API void dmc_model_free(dmc_model *m);
|
||||
|
||||
DMC_API int dmc_model_count(const dmc_model *m);
|
||||
/* setting i's fields */
|
||||
DMC_API const char *dmc_setting_key(const dmc_model *m, int i);
|
||||
DMC_API int dmc_setting_kind(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_setting_value(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_setting_default(const dmc_model *m, int i);
|
||||
DMC_API long dmc_setting_lo(const dmc_model *m, int i);
|
||||
DMC_API long dmc_setting_hi(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_setting_form(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_setting_group(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_setting_label(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_setting_help(const dmc_model *m, int i);
|
||||
DMC_API int dmc_setting_choices(const dmc_model *m, int i);
|
||||
DMC_API const char *dmc_choice_value(const dmc_model *m, int i, int c);
|
||||
DMC_API const char *dmc_choice_name(const dmc_model *m, int i, int c);
|
||||
DMC_API const char *dmc_choice_label(const dmc_model *m, int i, int c);
|
||||
/* the choice of setting i whose value is v, -1 none */
|
||||
DMC_API int dmc_setting_choice_of(const dmc_model *m, int i,
|
||||
const char *v);
|
||||
/* the setting whose key is key, -1 none */
|
||||
DMC_API int dmc_model_find(const dmc_model *m, const char *key);
|
||||
|
||||
/* the groups, in the order the settings list them */
|
||||
DMC_API int dmc_model_groups(const dmc_model *m);
|
||||
DMC_API const char *dmc_model_group(const dmc_model *m, int g);
|
||||
|
||||
/* "" if setting i takes value, else why not */
|
||||
DMC_API const char *dmc_setting_check(const dmc_model *m, int i,
|
||||
const char *value);
|
||||
/* the command that sets setting i to value ("AT" and its form, the
|
||||
value in it), "" if it does not take it */
|
||||
DMC_API const char *dmc_setting_command(const dmc_model *m, int i,
|
||||
const char *value);
|
||||
/* setting i's value now value (after its command's OK) */
|
||||
DMC_API void dmc_setting_set(dmc_model *m, int i, const char *value);
|
||||
|
||||
/*
|
||||
* The last call's statistics (AT#UD)
|
||||
*/
|
||||
|
||||
typedef struct dmc_diag dmc_diag;
|
||||
|
||||
/* the statistics from AT#UD's response text; never NULL */
|
||||
DMC_API dmc_diag *dmc_diag_parse(const char *text);
|
||||
DMC_API void dmc_diag_free(dmc_diag *d);
|
||||
/* how many rows, each its key (the specification's, hexadecimal), its
|
||||
label and its value for people ("33600 bit/s", "V.34") */
|
||||
DMC_API int dmc_diag_count(const dmc_diag *d);
|
||||
DMC_API int dmc_diag_key(const dmc_diag *d, int i);
|
||||
DMC_API const char *dmc_diag_label(const dmc_diag *d, int i);
|
||||
DMC_API const char *dmc_diag_value(const dmc_diag *d, int i);
|
||||
/* the raw value of key, NULL if it was not reported */
|
||||
DMC_API const char *dmc_diag_raw(const dmc_diag *d, int key);
|
||||
/* there was a call to report (key 1 not "no previous call") */
|
||||
DMC_API int dmc_diag_has_call(const dmc_diag *d);
|
||||
|
||||
/*
|
||||
* The terminal screen
|
||||
*/
|
||||
|
||||
typedef struct dmc_vt dmc_vt;
|
||||
|
||||
/* a cell's attributes */
|
||||
enum {
|
||||
DMC_BOLD = 1,
|
||||
DMC_UNDERLINE = 2,
|
||||
DMC_REVERSE = 4
|
||||
};
|
||||
|
||||
/* the keys dmc_vt_key() encodes */
|
||||
enum dmc_key {
|
||||
DMC_KEY_UP = 1, DMC_KEY_DOWN, DMC_KEY_RIGHT, DMC_KEY_LEFT,
|
||||
DMC_KEY_HOME, DMC_KEY_END, DMC_KEY_PGUP, DMC_KEY_PGDN,
|
||||
DMC_KEY_INSERT, DMC_KEY_DELETE, DMC_KEY_BACKTAB,
|
||||
DMC_KEY_F1, DMC_KEY_F2, DMC_KEY_F3, DMC_KEY_F4, DMC_KEY_F5,
|
||||
DMC_KEY_F6, DMC_KEY_F7, DMC_KEY_F8, DMC_KEY_F9, DMC_KEY_F10,
|
||||
DMC_KEY_ENTER, DMC_KEY_BACKSPACE, DMC_KEY_TAB, DMC_KEY_ESCAPE
|
||||
};
|
||||
|
||||
/* a screen of cols by rows, with scrollback lines kept above it */
|
||||
DMC_API dmc_vt *dmc_vt_new(int cols, int rows, int scrollback);
|
||||
DMC_API void dmc_vt_free(dmc_vt *t);
|
||||
DMC_API void dmc_vt_resize(dmc_vt *t, int cols, int rows);
|
||||
DMC_API void dmc_vt_reset(dmc_vt *t);
|
||||
DMC_API int dmc_vt_cols(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_rows(const dmc_vt *t);
|
||||
/* bytes from the modem (dmc_terminal()'s) */
|
||||
DMC_API void dmc_vt_feed(dmc_vt *t, const char *data, int n);
|
||||
/* the screen's answers to the modem's questions (where the cursor is:
|
||||
ESC [6n), to write to the modem as if typed */
|
||||
DMC_API int dmc_vt_reply(dmc_vt *t, char *buf, int max);
|
||||
/* how many lines of scrollback there are now */
|
||||
DMC_API int dmc_vt_history(const dmc_vt *t);
|
||||
/* a cell: row 0 the screen's top, -1 the newest scrollback line and so
|
||||
on; its character (a Unicode code point, 32 for none), colours (0 to
|
||||
15 the ANSI palette, -1 the default) and attributes */
|
||||
DMC_API uint32_t dmc_vt_char(const dmc_vt *t, int row, int col);
|
||||
DMC_API int dmc_vt_fg(const dmc_vt *t, int row, int col);
|
||||
DMC_API int dmc_vt_bg(const dmc_vt *t, int row, int col);
|
||||
DMC_API int dmc_vt_attr(const dmc_vt *t, int row, int col);
|
||||
/* a row as UTF-8 text, trailing blanks dropped */
|
||||
DMC_API const char *dmc_vt_text(dmc_vt *t, int row);
|
||||
DMC_API int dmc_vt_cursor_row(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_cursor_col(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_cursor_visible(const dmc_vt *t);
|
||||
/* the screen changed since the last call (and clears the mark) */
|
||||
DMC_API int dmc_vt_changed(dmc_vt *t);
|
||||
/* the bytes a key sends, into buf: how many */
|
||||
DMC_API int dmc_vt_key(const dmc_vt *t, int key, char *buf, int max);
|
||||
/* the RGB of palette colour c (0 to 15), as 0xRRGGBB */
|
||||
DMC_API uint32_t dmc_vt_palette(int c);
|
||||
|
||||
/*
|
||||
* dmodemd
|
||||
*/
|
||||
|
||||
/* a line dmodemd printed when it started: 1 and its DTE (a pty's
|
||||
path, "TCP 127.0.0.1:2323", "pipe \\.\pipe\dmodem") into buf if it is
|
||||
the line that says the modem is ready, else 0 */
|
||||
DMC_API int dmc_daemon_ready(const char *line, char *buf, int max);
|
||||
/* a line that is an error of dmodemd's ("error: ..."): 1 */
|
||||
DMC_API int dmc_daemon_error(const char *line);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* daemon.c - reading what dmodemd prints as it starts (dmodemctl.h)
|
||||
*
|
||||
* On Linux, macOS, BSD and Haiku it says
|
||||
* Use `/dev/ttyDM0' as modem device, Ctrl+C for termination.
|
||||
* (the pty's own path, /dev/pts/3, when it may not make the link), and
|
||||
* on Windows
|
||||
* Use TCP 127.0.0.1:2323 as the modem, Ctrl+C for termination.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
|
||||
static int copy(const char *from, size_t n, char *buf, int max)
|
||||
{
|
||||
if (!buf || max <= 0)
|
||||
return 1;
|
||||
if (n >= (size_t)max)
|
||||
n = (size_t)max - 1;
|
||||
memcpy(buf, from, n);
|
||||
buf[n] = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dmc_daemon_ready(const char *line, char *buf, int max)
|
||||
{
|
||||
if (!line)
|
||||
return 0;
|
||||
const char *p = strstr(line, "Use `");
|
||||
if (p) {
|
||||
p += 5;
|
||||
const char *e = strchr(p, '\'');
|
||||
if (e && strstr(e, "as modem device"))
|
||||
return copy(p, (size_t)(e - p), buf, max);
|
||||
}
|
||||
p = strstr(line, "Use ");
|
||||
if (p) {
|
||||
p += 4;
|
||||
const char *e = strstr(p, " as the modem");
|
||||
if (e)
|
||||
return copy(p, (size_t)(e - p), buf, max);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dmc_daemon_error(const char *line)
|
||||
{
|
||||
return line && !strncmp(line, "error:", 6);
|
||||
}
|
||||
+435
@@ -0,0 +1,435 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* diag.c - the last call's statistics from AT#UD (dmodemctl.h)
|
||||
*
|
||||
* AT#UD follows Microsoft's Unimodem Diagnostics Command Reference
|
||||
* Specification, version 1.0 (D-Modem's docs/specs/umud10.rtf): lines
|
||||
* "DIAG <2A4D3263 key=value ...>", the keys and values in hexadecimal
|
||||
* and the code tables V.58's. Each key becomes a row with its label
|
||||
* and its value for people.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
/* how a key's value reads */
|
||||
enum fmt {
|
||||
F_HEX, /* as it is */
|
||||
F_NUM, /* a count */
|
||||
F_BPS, /* bit/s */
|
||||
F_DBM, /* -dBm */
|
||||
F_DB, /* dB */
|
||||
F_MS, /* ms */
|
||||
F_HZ, /* Hz */
|
||||
F_BAUD, /* symbols a second */
|
||||
F_BYTES, /* bytes */
|
||||
F_TABLE, /* one of the code tables */
|
||||
F_STRING, /* quoted octets */
|
||||
F_VERSION /* digit.digit */
|
||||
};
|
||||
|
||||
struct code {
|
||||
unsigned v;
|
||||
const char *label;
|
||||
};
|
||||
|
||||
static const struct code setup[] = {
|
||||
{ 0x0, "No previous call" },
|
||||
{ 0x1, "No dial tone" },
|
||||
{ 0x2, "Network busy (reorder)" },
|
||||
{ 0x3, "Busy" },
|
||||
{ 0x4, "No signal recognised" },
|
||||
{ 0x5, "Voice" },
|
||||
{ 0x6, "Text telephone" },
|
||||
{ 0x7, "Data answer tone heard" },
|
||||
{ 0x8, "Data calling tone heard" },
|
||||
{ 0x9, "Fax answer tone heard" },
|
||||
{ 0xA, "Fax calling tone heard" },
|
||||
{ 0xB, "V.8bis" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static const struct code media[] = {
|
||||
{ 0x0, "Data" },
|
||||
{ 0x1, "Fax" },
|
||||
{ 0x2, "Voice" },
|
||||
{ 0x3, "VoiceView" },
|
||||
{ 0x4, "ASVD (V.61)" },
|
||||
{ 0x5, "ASVD (V.34Q)" },
|
||||
{ 0x6, "DSVD (Multi-Tech)" },
|
||||
{ 0x7, "DSVD 1.2" },
|
||||
{ 0x8, "DSVD (V.70)" },
|
||||
{ 0x9, "Video telephony (H.324)" },
|
||||
{ 0xA, "Other V.80 call" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static const struct code dte_mode[] = {
|
||||
{ 0x0, "Asynchronous" },
|
||||
{ 0x1, "V.80 transparent synchronous" },
|
||||
{ 0x2, "V.80 framed synchronous" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
/* V.58's gstnModulationSchemeActive, and the manufacturers' */
|
||||
static const struct code modulation[] = {
|
||||
{ 0x0, "V.17" }, { 0x1, "V.21" }, { 0x2, "V.22" },
|
||||
{ 0x3, "V.22bis" }, { 0x4, "V.23" }, { 0x5, "V.23 (half duplex)" },
|
||||
{ 0x6, "V.26bis" }, { 0x7, "V.26ter" }, { 0x8, "V.27ter" },
|
||||
{ 0x9, "V.29" }, { 0xA, "V.32" }, { 0xB, "V.32bis" },
|
||||
{ 0xC, "V.34" }, { 0xD, "V.34 half duplex" },
|
||||
{ 0xE, "V.90 (PCM)" }, { 0xF, "V.91 (PCM both ways)" },
|
||||
{ 0x80, "X2" }, { 0x81, "K56flex" }, { 0x82, "V.FC" },
|
||||
{ 0x83, "V.32terbo" }, { 0x84, "Bell 212A" }, { 0x85, "Bell 103" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static const struct code error_control[] = {
|
||||
{ 0x0, "None" },
|
||||
{ 0x1, "V.42 LAPM" },
|
||||
{ 0x2, "MNP" },
|
||||
{ 0x80, "MNP 10" },
|
||||
{ 0x81, "ECP" },
|
||||
{ 0x82, "ETC" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static const struct code compression[] = {
|
||||
{ 0x0, "None" },
|
||||
{ 0x1, "V.42bis" },
|
||||
{ 0x80, "MNP 5" },
|
||||
{ 0x81, "V.44" },
|
||||
{ 0x82, "zstd" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static const struct code flow[] = {
|
||||
{ 0x0, "Off" },
|
||||
{ 0x1, "XON/XOFF" },
|
||||
{ 0x2, "RTS/CTS" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
/* V.58's callCleared */
|
||||
static const struct code cleared[] = {
|
||||
{ 0x0, "Cause unknown" },
|
||||
{ 0x1, "No previous call" },
|
||||
{ 0x2, "Call in progress" },
|
||||
{ 0x3, "Call waiting signal" },
|
||||
{ 0x4, "Delayed" },
|
||||
{ 0x19, "Inactivity timer" },
|
||||
{ 0x1F, "DTR off stops the dial" },
|
||||
{ 0x20, "DTR turned off" },
|
||||
{ 0x28, "No number" },
|
||||
{ 0x29, "Blacklisted number" },
|
||||
{ 0x2A, "Too many attempts" },
|
||||
{ 0x2B, "Extension off hook" },
|
||||
{ 0x2C, "No answer in time (S7)" },
|
||||
{ 0x2D, "Incoming call while dialling" },
|
||||
{ 0x2E, "Loop current lost" },
|
||||
{ 0x2F, "No dial tone" },
|
||||
{ 0x30, "Voice" },
|
||||
{ 0x31, "Reorder tone" },
|
||||
{ 0x32, "SIT tone" },
|
||||
{ 0x33, "Busy" },
|
||||
{ 0x34, "Long space" },
|
||||
{ 0x3C, "Carrier lost" },
|
||||
{ 0x3D, "Training failed" },
|
||||
{ 0x3E, "No modulation in common" },
|
||||
{ 0x3F, "Retrain failed" },
|
||||
{ 0x40, "Too many retrains" },
|
||||
{ 0x41, "The other modem hung up" },
|
||||
{ 0x42, "Fax detected" },
|
||||
{ 0x50, "Stopped by a key" },
|
||||
{ 0x51, "Hung up (ATH)" },
|
||||
{ 0x52, "Reset (ATZ)" },
|
||||
{ 0x5A, "Frame reject" },
|
||||
{ 0x5B, "No error control" },
|
||||
{ 0x5C, "Protocol violation" },
|
||||
{ 0x5D, "Too many retransmissions" },
|
||||
{ 0x5E, "Negotiation failed" },
|
||||
{ 0x5F, "Disconnect frame received" },
|
||||
{ 0x60, "SABME frame received" },
|
||||
{ 0x64, "Lost synchronisation" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
struct keyinfo {
|
||||
int key;
|
||||
const char *label;
|
||||
int fmt;
|
||||
const struct code *table;
|
||||
};
|
||||
|
||||
static const struct keyinfo keys[] = {
|
||||
{ 0x00, "Report version", F_VERSION, NULL },
|
||||
{ 0x01, "Call setup", F_TABLE, setup },
|
||||
{ 0x02, "Mode", F_TABLE, media },
|
||||
{ 0x03, "DTE interface", F_TABLE, dte_mode },
|
||||
{ 0x04, "V.8 CM", F_STRING, NULL },
|
||||
{ 0x05, "V.8 JM", F_STRING, NULL },
|
||||
{ 0x10, "Received level", F_DBM, NULL },
|
||||
{ 0x11, "Transmit level", F_DBM, NULL },
|
||||
{ 0x12, "Noise level", F_DBM, NULL },
|
||||
{ 0x13, "Mean squared error", F_NUM, NULL },
|
||||
{ 0x14, "Near echo loss", F_DB, NULL },
|
||||
{ 0x15, "Far echo loss", F_DB, NULL },
|
||||
{ 0x16, "Far echo delay", F_MS, NULL },
|
||||
{ 0x17, "Round trip delay", F_MS, NULL },
|
||||
{ 0x18, "V.34 INFO bits", F_HEX, NULL },
|
||||
{ 0x20, "Transmit modulation", F_TABLE, modulation },
|
||||
{ 0x21, "Receive modulation", F_TABLE, modulation },
|
||||
{ 0x22, "Transmit symbol rate", F_BAUD, NULL },
|
||||
{ 0x23, "Receive symbol rate", F_BAUD, NULL },
|
||||
{ 0x24, "Transmit carrier", F_HZ, NULL },
|
||||
{ 0x25, "Receive carrier", F_HZ, NULL },
|
||||
{ 0x26, "First transmit rate", F_BPS, NULL },
|
||||
{ 0x27, "First receive rate", F_BPS, NULL },
|
||||
{ 0x30, "Carrier losses", F_NUM, NULL },
|
||||
{ 0x31, "Rate renegotiations", F_NUM, NULL },
|
||||
{ 0x32, "Retrains asked for", F_NUM, NULL },
|
||||
{ 0x33, "Retrains granted", F_NUM, NULL },
|
||||
{ 0x34, "Last transmit rate", F_BPS, NULL },
|
||||
{ 0x35, "Last receive rate", F_BPS, NULL },
|
||||
{ 0x40, "Error control", F_TABLE, error_control },
|
||||
{ 0x41, "Frame size", F_BYTES, NULL },
|
||||
{ 0x42, "Link timeouts", F_NUM, NULL },
|
||||
{ 0x43, "NAKs received", F_NUM, NULL },
|
||||
{ 0x44, "Compression", F_TABLE, compression },
|
||||
{ 0x45, "Dictionary size", F_BYTES, NULL },
|
||||
{ 0x50, "Transmit flow control", F_TABLE, flow },
|
||||
{ 0x51, "Receive flow control", F_TABLE, flow },
|
||||
{ 0x52, "Characters sent", F_NUM, NULL },
|
||||
{ 0x53, "Characters received", F_NUM, NULL },
|
||||
{ 0x54, "Characters lost sending", F_NUM, NULL },
|
||||
{ 0x55, "Characters lost receiving", F_NUM, NULL },
|
||||
{ 0x56, "Frames sent", F_NUM, NULL },
|
||||
{ 0x57, "Frames received", F_NUM, NULL },
|
||||
{ 0x58, "Frames sent again", F_NUM, NULL },
|
||||
{ 0x59, "Frames received in error", F_NUM, NULL },
|
||||
{ 0x60, "Why the call ended", F_TABLE, cleared },
|
||||
{ 0x61, "Call waiting events", F_NUM, NULL },
|
||||
{ -1, NULL, 0, NULL }
|
||||
};
|
||||
|
||||
struct row {
|
||||
int key;
|
||||
char *raw, *label, *value;
|
||||
};
|
||||
|
||||
struct dmc_diag {
|
||||
struct row *rows;
|
||||
int n;
|
||||
};
|
||||
|
||||
static const struct keyinfo *info(int key)
|
||||
{
|
||||
for (const struct keyinfo *k = keys; k->label; k++)
|
||||
if (k->key == key)
|
||||
return k;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *code_label(const struct code *t, unsigned v)
|
||||
{
|
||||
for (; t->label; t++)
|
||||
if (t->v == v)
|
||||
return t->label;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* a number with thousands apart by commas */
|
||||
static void grouped(char *out, size_t n, unsigned long v)
|
||||
{
|
||||
char digits[32];
|
||||
snprintf(digits, sizeof(digits), "%lu", v);
|
||||
size_t len = strlen(digits), o = 0;
|
||||
for (size_t i = 0; i < len && o + 2 < n; i++) {
|
||||
if (i && (len - i) % 3 == 0)
|
||||
out[o++] = ',';
|
||||
out[o++] = digits[i];
|
||||
}
|
||||
out[o] = '\0';
|
||||
}
|
||||
|
||||
static char *shown(const struct keyinfo *k, const char *raw)
|
||||
{
|
||||
char out[160];
|
||||
unsigned long v = strtoul(raw, NULL, 16);
|
||||
char num[40];
|
||||
grouped(num, sizeof(num), v);
|
||||
switch (k ? k->fmt : F_HEX) {
|
||||
case F_NUM:
|
||||
snprintf(out, sizeof(out), "%s", num);
|
||||
break;
|
||||
case F_BPS:
|
||||
snprintf(out, sizeof(out), "%s bit/s", num);
|
||||
break;
|
||||
case F_DBM:
|
||||
snprintf(out, sizeof(out), "%s%lu dBm", v ? "-" : "", v);
|
||||
break;
|
||||
case F_DB:
|
||||
snprintf(out, sizeof(out), "%lu dB", v);
|
||||
break;
|
||||
case F_MS:
|
||||
snprintf(out, sizeof(out), "%lu ms", v);
|
||||
break;
|
||||
case F_HZ:
|
||||
snprintf(out, sizeof(out), "%lu Hz", v);
|
||||
break;
|
||||
case F_BAUD:
|
||||
snprintf(out, sizeof(out), "%lu baud", v);
|
||||
break;
|
||||
case F_BYTES:
|
||||
snprintf(out, sizeof(out), "%s bytes", num);
|
||||
break;
|
||||
case F_TABLE: {
|
||||
const char *l = code_label(k->table, (unsigned)v);
|
||||
if (l)
|
||||
snprintf(out, sizeof(out), "%s", l);
|
||||
else
|
||||
snprintf(out, sizeof(out), "Code %lX", v);
|
||||
break;
|
||||
}
|
||||
case F_VERSION:
|
||||
if (strlen(raw) == 2)
|
||||
snprintf(out, sizeof(out), "%c.%c", raw[0], raw[1]);
|
||||
else
|
||||
snprintf(out, sizeof(out), "%s", raw);
|
||||
break;
|
||||
case F_STRING: {
|
||||
/* the octets, apart by spaces */
|
||||
size_t o = 0;
|
||||
for (const char *p = raw; *p && o + 4 < sizeof(out); p++) {
|
||||
if (*p == '"')
|
||||
continue;
|
||||
if (o && (o + 1) % 3 == 0)
|
||||
out[o++] = ' ';
|
||||
out[o++] = *p;
|
||||
}
|
||||
out[o] = '\0';
|
||||
break;
|
||||
}
|
||||
default:
|
||||
snprintf(out, sizeof(out), "%s", raw);
|
||||
break;
|
||||
}
|
||||
return dup_str(out);
|
||||
}
|
||||
|
||||
static void add(dmc_diag *d, int key, const char *raw, size_t n)
|
||||
{
|
||||
/* a key again: its last value */
|
||||
for (int i = 0; i < d->n; i++)
|
||||
if (d->rows[i].key == key) {
|
||||
free(d->rows[i].raw);
|
||||
free(d->rows[i].value);
|
||||
d->rows[i].raw = dup_n(raw, n);
|
||||
d->rows[i].value = shown(info(key), d->rows[i].raw);
|
||||
return;
|
||||
}
|
||||
struct row *r = realloc(d->rows, sizeof(*r) * (size_t)(d->n + 1));
|
||||
if (!r)
|
||||
abort();
|
||||
d->rows = r;
|
||||
r = &d->rows[d->n++];
|
||||
const struct keyinfo *k = info(key);
|
||||
char label[32];
|
||||
if (!k)
|
||||
snprintf(label, sizeof(label), "Key %X", key);
|
||||
r->key = key;
|
||||
r->raw = dup_n(raw, n);
|
||||
r->label = dup_str(k ? k->label : label);
|
||||
r->value = shown(k, r->raw);
|
||||
}
|
||||
|
||||
dmc_diag *dmc_diag_parse(const char *text)
|
||||
{
|
||||
dmc_diag *d = calloc(1, sizeof(*d));
|
||||
if (!d)
|
||||
abort();
|
||||
const char *p = text ? text : "";
|
||||
while ((p = strstr(p, "DIAG <")) != NULL) {
|
||||
p += 6;
|
||||
const char *end = strchr(p, '>');
|
||||
if (!end)
|
||||
break;
|
||||
/* the tag (2A4D3263), then key=value pairs */
|
||||
while (p < end && *p != ' ')
|
||||
p++;
|
||||
while (p < end) {
|
||||
while (p < end && *p == ' ')
|
||||
p++;
|
||||
const char *eq = p;
|
||||
while (eq < end && *eq != '=')
|
||||
eq++;
|
||||
if (eq >= end)
|
||||
break;
|
||||
int key = (int)strtol(p, NULL, 16);
|
||||
const char *v = eq + 1, *ve = v;
|
||||
if (*v == '"') {
|
||||
ve = strchr(v + 1, '"');
|
||||
ve = ve && ve < end ? ve + 1 : end;
|
||||
} else {
|
||||
while (ve < end && *ve != ' ')
|
||||
ve++;
|
||||
}
|
||||
add(d, key, v, (size_t)(ve - v));
|
||||
p = ve;
|
||||
}
|
||||
p = end + 1;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
void dmc_diag_free(dmc_diag *d)
|
||||
{
|
||||
if (!d)
|
||||
return;
|
||||
for (int i = 0; i < d->n; i++) {
|
||||
free(d->rows[i].raw);
|
||||
free(d->rows[i].label);
|
||||
free(d->rows[i].value);
|
||||
}
|
||||
free(d->rows);
|
||||
free(d);
|
||||
}
|
||||
|
||||
int dmc_diag_count(const dmc_diag *d)
|
||||
{
|
||||
return d ? d->n : 0;
|
||||
}
|
||||
|
||||
int dmc_diag_key(const dmc_diag *d, int i)
|
||||
{
|
||||
return d && i >= 0 && i < d->n ? d->rows[i].key : -1;
|
||||
}
|
||||
|
||||
const char *dmc_diag_label(const dmc_diag *d, int i)
|
||||
{
|
||||
return d && i >= 0 && i < d->n ? d->rows[i].label : "";
|
||||
}
|
||||
|
||||
const char *dmc_diag_value(const dmc_diag *d, int i)
|
||||
{
|
||||
return d && i >= 0 && i < d->n ? d->rows[i].value : "";
|
||||
}
|
||||
|
||||
const char *dmc_diag_raw(const dmc_diag *d, int key)
|
||||
{
|
||||
for (int i = 0; d && i < d->n; i++)
|
||||
if (d->rows[i].key == key)
|
||||
return d->rows[i].raw;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int dmc_diag_has_call(const dmc_diag *d)
|
||||
{
|
||||
const char *v = dmc_diag_raw(d, 1);
|
||||
return v && strtoul(v, NULL, 16) != 0;
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* model.c - the modem's settings from AT!M (dmodemctl.h)
|
||||
*
|
||||
* AT!M's lines, their fields apart by tabs:
|
||||
*
|
||||
* model 1 <settings>
|
||||
* setting <key> <kind> <value> <default> <lo> <hi> <form> <group>
|
||||
* <label> <help>
|
||||
* choice <value> <name> <label> (a pick's, after it)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
struct choice {
|
||||
char *value, *name, *label;
|
||||
};
|
||||
|
||||
struct setting {
|
||||
char *key, *value, *def, *form, *group, *label, *help;
|
||||
int kind;
|
||||
long lo, hi;
|
||||
struct choice *choices;
|
||||
int n_choices;
|
||||
};
|
||||
|
||||
struct dmc_model {
|
||||
struct setting *s;
|
||||
int n;
|
||||
char **groups;
|
||||
int n_groups;
|
||||
dmc_buf scratch;
|
||||
char why[96];
|
||||
};
|
||||
|
||||
/* the line's fields: up to max, how many (the line cut in place) */
|
||||
static int fields(char *line, char **f, int max)
|
||||
{
|
||||
int n = 0;
|
||||
char *p = line;
|
||||
while (n < max) {
|
||||
f[n++] = p;
|
||||
char *t = strchr(p, '\t');
|
||||
if (!t)
|
||||
break;
|
||||
*t = '\0';
|
||||
p = t + 1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static void add_group(dmc_model *m, const char *g)
|
||||
{
|
||||
for (int i = 0; i < m->n_groups; i++)
|
||||
if (!strcmp(m->groups[i], g))
|
||||
return;
|
||||
char **p = realloc(m->groups, sizeof(*p) * (size_t)(m->n_groups + 1));
|
||||
if (!p)
|
||||
abort();
|
||||
m->groups = p;
|
||||
m->groups[m->n_groups++] = dup_str(g);
|
||||
}
|
||||
|
||||
dmc_model *dmc_model_parse(const char *text)
|
||||
{
|
||||
if (!text)
|
||||
return NULL;
|
||||
char *copy = dup_str(text);
|
||||
dmc_model *m = calloc(1, sizeof(*m));
|
||||
if (!m)
|
||||
abort();
|
||||
int seen_model = 0, declared = -1;
|
||||
char *save = copy;
|
||||
while (save) {
|
||||
char *line = save;
|
||||
char *nl = strchr(line, '\n');
|
||||
if (nl) {
|
||||
*nl = '\0';
|
||||
save = nl + 1;
|
||||
} else {
|
||||
save = NULL;
|
||||
}
|
||||
size_t len = strlen(line);
|
||||
if (len && line[len - 1] == '\r')
|
||||
line[len - 1] = '\0';
|
||||
char *f[12];
|
||||
int n = fields(line, f, 12);
|
||||
if (!strcmp(f[0], "model") && n >= 3) {
|
||||
if (atoi(f[1]) != 1)
|
||||
break; /* a format we do not know */
|
||||
seen_model = 1;
|
||||
declared = atoi(f[2]);
|
||||
} else if (!strcmp(f[0], "setting") && n >= 11 && seen_model) {
|
||||
struct setting *p = realloc(m->s, sizeof(*p) *
|
||||
(size_t)(m->n + 1));
|
||||
if (!p)
|
||||
abort();
|
||||
m->s = p;
|
||||
struct setting *s = &m->s[m->n++];
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->key = dup_str(f[1]);
|
||||
s->kind = !strcmp(f[2], "pick") ? DMC_PICK :
|
||||
!strcmp(f[2], "number") ? DMC_NUMBER : DMC_TEXT;
|
||||
s->value = dup_str(f[3]);
|
||||
s->def = dup_str(f[4]);
|
||||
s->lo = strtol(f[5], NULL, 10);
|
||||
s->hi = strtol(f[6], NULL, 10);
|
||||
s->form = dup_str(f[7]);
|
||||
s->group = dup_str(f[8]);
|
||||
s->label = dup_str(f[9]);
|
||||
s->help = dup_str(f[10]);
|
||||
add_group(m, s->group);
|
||||
} else if (!strcmp(f[0], "choice") && n >= 4 && m->n) {
|
||||
struct setting *s = &m->s[m->n - 1];
|
||||
struct choice *c = realloc(s->choices, sizeof(*c) *
|
||||
(size_t)(s->n_choices + 1));
|
||||
if (!c)
|
||||
abort();
|
||||
s->choices = c;
|
||||
c = &s->choices[s->n_choices++];
|
||||
c->value = dup_str(f[1]);
|
||||
c->name = dup_str(f[2]);
|
||||
c->label = dup_str(f[3]);
|
||||
}
|
||||
}
|
||||
free(copy);
|
||||
if (!seen_model || (declared >= 0 && declared != m->n)) {
|
||||
dmc_model_free(m);
|
||||
return NULL;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
void dmc_model_free(dmc_model *m)
|
||||
{
|
||||
if (!m)
|
||||
return;
|
||||
for (int i = 0; i < m->n; i++) {
|
||||
struct setting *s = &m->s[i];
|
||||
free(s->key);
|
||||
free(s->value);
|
||||
free(s->def);
|
||||
free(s->form);
|
||||
free(s->group);
|
||||
free(s->label);
|
||||
free(s->help);
|
||||
for (int j = 0; j < s->n_choices; j++) {
|
||||
free(s->choices[j].value);
|
||||
free(s->choices[j].name);
|
||||
free(s->choices[j].label);
|
||||
}
|
||||
free(s->choices);
|
||||
}
|
||||
free(m->s);
|
||||
for (int i = 0; i < m->n_groups; i++)
|
||||
free(m->groups[i]);
|
||||
free(m->groups);
|
||||
buf_free(&m->scratch);
|
||||
free(m);
|
||||
}
|
||||
|
||||
int dmc_model_count(const dmc_model *m)
|
||||
{
|
||||
return m ? m->n : 0;
|
||||
}
|
||||
|
||||
static const struct setting *at(const dmc_model *m, int i)
|
||||
{
|
||||
static const struct setting none = { "", "", "", "", "", "", "", 0,
|
||||
0, 0, NULL, 0 };
|
||||
return m && i >= 0 && i < m->n ? &m->s[i] : &none;
|
||||
}
|
||||
|
||||
const char *dmc_setting_key(const dmc_model *m, int i) { return at(m, i)->key; }
|
||||
int dmc_setting_kind(const dmc_model *m, int i) { return at(m, i)->kind; }
|
||||
const char *dmc_setting_value(const dmc_model *m, int i) { return at(m, i)->value; }
|
||||
const char *dmc_setting_default(const dmc_model *m, int i) { return at(m, i)->def; }
|
||||
long dmc_setting_lo(const dmc_model *m, int i) { return at(m, i)->lo; }
|
||||
long dmc_setting_hi(const dmc_model *m, int i) { return at(m, i)->hi; }
|
||||
const char *dmc_setting_form(const dmc_model *m, int i) { return at(m, i)->form; }
|
||||
const char *dmc_setting_group(const dmc_model *m, int i) { return at(m, i)->group; }
|
||||
const char *dmc_setting_label(const dmc_model *m, int i) { return at(m, i)->label; }
|
||||
const char *dmc_setting_help(const dmc_model *m, int i) { return at(m, i)->help; }
|
||||
int dmc_setting_choices(const dmc_model *m, int i) { return at(m, i)->n_choices; }
|
||||
|
||||
static const struct choice *choice_at(const dmc_model *m, int i, int c)
|
||||
{
|
||||
static const struct choice none = { "", "", "" };
|
||||
const struct setting *s = at(m, i);
|
||||
return c >= 0 && c < s->n_choices ? &s->choices[c] : &none;
|
||||
}
|
||||
|
||||
const char *dmc_choice_value(const dmc_model *m, int i, int c)
|
||||
{
|
||||
return choice_at(m, i, c)->value;
|
||||
}
|
||||
|
||||
const char *dmc_choice_name(const dmc_model *m, int i, int c)
|
||||
{
|
||||
return choice_at(m, i, c)->name;
|
||||
}
|
||||
|
||||
const char *dmc_choice_label(const dmc_model *m, int i, int c)
|
||||
{
|
||||
return choice_at(m, i, c)->label;
|
||||
}
|
||||
|
||||
int dmc_setting_choice_of(const dmc_model *m, int i, const char *v)
|
||||
{
|
||||
const struct setting *s = at(m, i);
|
||||
for (int c = 0; c < s->n_choices; c++)
|
||||
if (same_ci(s->choices[c].value, v ? v : ""))
|
||||
return c;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int dmc_model_find(const dmc_model *m, const char *key)
|
||||
{
|
||||
for (int i = 0; m && i < m->n; i++)
|
||||
if (!strcmp(m->s[i].key, key ? key : ""))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int dmc_model_groups(const dmc_model *m)
|
||||
{
|
||||
return m ? m->n_groups : 0;
|
||||
}
|
||||
|
||||
const char *dmc_model_group(const dmc_model *m, int g)
|
||||
{
|
||||
return m && g >= 0 && g < m->n_groups ? m->groups[g] : "";
|
||||
}
|
||||
|
||||
/* the value a number: digits only, within lo and hi */
|
||||
static int number_ok(const struct setting *s, const char *v, char *why,
|
||||
size_t n)
|
||||
{
|
||||
if (!*v) {
|
||||
snprintf(why, n, "Enter a number");
|
||||
return 0;
|
||||
}
|
||||
for (const char *p = v; *p; p++)
|
||||
if (*p < '0' || *p > '9') {
|
||||
snprintf(why, n, "Enter a number");
|
||||
return 0;
|
||||
}
|
||||
if (strlen(v) > 10) {
|
||||
snprintf(why, n, "From %ld to %ld", s->lo, s->hi);
|
||||
return 0;
|
||||
}
|
||||
long x = strtol(v, NULL, 10);
|
||||
if (x < s->lo || x > s->hi) {
|
||||
snprintf(why, n, "From %ld to %ld", s->lo, s->hi);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *dmc_setting_check(const dmc_model *m, int i, const char *value)
|
||||
{
|
||||
static char none[] = "No such setting";
|
||||
if (!m || i < 0 || i >= m->n)
|
||||
return none;
|
||||
char *why = ((dmc_model *)m)->why;
|
||||
const size_t size = sizeof(m->why);
|
||||
const struct setting *s = at(m, i);
|
||||
const char *v = value ? value : "";
|
||||
why[0] = '\0';
|
||||
switch (s->kind) {
|
||||
case DMC_PICK:
|
||||
if (dmc_setting_choice_of(m, i, v) < 0)
|
||||
snprintf(why, size, "Not one of the choices");
|
||||
break;
|
||||
case DMC_NUMBER:
|
||||
number_ok(s, v, why, size);
|
||||
break;
|
||||
default:
|
||||
/* a list of numbers, apart by commas (some may be left
|
||||
out, as AT takes them) */
|
||||
if (!*v)
|
||||
snprintf(why, size, "Enter the values");
|
||||
for (const char *p = v; *p; p++)
|
||||
if ((*p < '0' || *p > '9') && *p != ',') {
|
||||
snprintf(why, size,
|
||||
"Numbers apart by commas");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return why;
|
||||
}
|
||||
|
||||
const char *dmc_setting_command(const dmc_model *m, int i, const char *value)
|
||||
{
|
||||
dmc_model *mm = (dmc_model *)m;
|
||||
if (!m)
|
||||
return "";
|
||||
buf_clear(&mm->scratch);
|
||||
if (*dmc_setting_check(m, i, value))
|
||||
return "";
|
||||
const struct setting *s = at(m, i);
|
||||
const char *v = value;
|
||||
/* (a pick takes its value as the modem lists it) */
|
||||
if (s->kind == DMC_PICK)
|
||||
v = s->choices[dmc_setting_choice_of(m, i, value)].value;
|
||||
buf_adds(&mm->scratch, "AT");
|
||||
for (const char *p = s->form; *p; p++) {
|
||||
if (p[0] == '%' && p[1]) {
|
||||
p++;
|
||||
if (*p == 's')
|
||||
buf_adds(&mm->scratch, v);
|
||||
else
|
||||
buf_addc(&mm->scratch, *p);
|
||||
} else {
|
||||
buf_addc(&mm->scratch, *p);
|
||||
}
|
||||
}
|
||||
return buf_str(&mm->scratch);
|
||||
}
|
||||
|
||||
void dmc_setting_set(dmc_model *m, int i, const char *value)
|
||||
{
|
||||
if (!m || i < 0 || i >= m->n)
|
||||
return;
|
||||
set_str(&m->s[i].value, value);
|
||||
}
|
||||
@@ -0,0 +1,761 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* session.c - the AT conversation with dmodemd (dmodemctl.h)
|
||||
*
|
||||
* The modem's bytes are read a line at a time (the ends of lines CR LF,
|
||||
* the CRs dropped). A command of dmc_command()'s is written when the
|
||||
* one before it has its final result; the lines between its echo and
|
||||
* that result are its response. Result codes nobody waits for (RING,
|
||||
* NO CARRIER, a CONNECT after an automatic answer) and the command
|
||||
* lines the user types in the terminal move the call's state too.
|
||||
*
|
||||
* Online, the bytes are the call's data and only NO CARRIER is looked
|
||||
* for; while a configurator runs, only the OK that ends it (or the
|
||||
* ERROR that refuses it). Results need the modem's verbose result
|
||||
* codes (V1, its default).
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
/* the usual time a command may take, and dialling's or answering's */
|
||||
#define COMMAND_MS 5000
|
||||
#define CALL_MS 120000
|
||||
/* the escape's guard time (S12's default, and a margin) */
|
||||
#define GUARD_MS 1100
|
||||
/* how long after the escape its OK may take */
|
||||
#define ESCAPE_OK_MS 3000
|
||||
/* RINGs are this far apart at most (a ring cadence is 6 s) */
|
||||
#define RING_GAP_MS 8000
|
||||
/* a line longer than this is cut (a configurator's screen has none) */
|
||||
#define LINE_MAX 4096
|
||||
|
||||
struct command {
|
||||
int tag; /* 0: the session's own */
|
||||
char *cmd; /* with its AT */
|
||||
int flags, timeout_ms;
|
||||
struct command *next;
|
||||
};
|
||||
|
||||
struct event {
|
||||
dmc_event ev;
|
||||
char *text, *result;
|
||||
struct event *next;
|
||||
};
|
||||
|
||||
/* the steps of a hang-up from online */
|
||||
enum { HANG_NONE, HANG_GUARD, HANG_ESCAPED };
|
||||
|
||||
struct dmc_session {
|
||||
int state;
|
||||
int64_t now;
|
||||
dmc_buf out, term, line, typed;
|
||||
/* the queue, and the one written that waits for its result */
|
||||
struct command *queue, *active;
|
||||
int64_t active_until;
|
||||
int echo_seen;
|
||||
dmc_buf response;
|
||||
int next_tag;
|
||||
/* the events, and the one dmc_next_event() last gave */
|
||||
struct event *events, *events_tail, *current;
|
||||
/* a hang-up from online: its step and when the next is due */
|
||||
int hang;
|
||||
int64_t hang_at;
|
||||
/* the user typed +++ online */
|
||||
int user_escape;
|
||||
int64_t ring_until;
|
||||
char *connect_text, *caller_number, *caller_name;
|
||||
int connect_rate;
|
||||
};
|
||||
|
||||
static void command_free(struct command *c)
|
||||
{
|
||||
if (!c)
|
||||
return;
|
||||
free(c->cmd);
|
||||
free(c);
|
||||
}
|
||||
|
||||
static void event_free(struct event *e)
|
||||
{
|
||||
if (!e)
|
||||
return;
|
||||
free(e->text);
|
||||
free(e->result);
|
||||
free(e);
|
||||
}
|
||||
|
||||
static void push_event(dmc_session *s, int type, int tag, int ok,
|
||||
const char *text, const char *result)
|
||||
{
|
||||
struct event *e = calloc(1, sizeof(*e));
|
||||
if (!e)
|
||||
abort();
|
||||
e->text = dup_str(text);
|
||||
e->result = dup_str(result);
|
||||
e->ev.type = type;
|
||||
e->ev.tag = tag;
|
||||
e->ev.ok = ok;
|
||||
e->ev.state = s->state;
|
||||
e->ev.text = e->text;
|
||||
e->ev.result = e->result;
|
||||
if (s->events_tail)
|
||||
s->events_tail->next = e;
|
||||
else
|
||||
s->events = e;
|
||||
s->events_tail = e;
|
||||
}
|
||||
|
||||
static void set_state(dmc_session *s, int state)
|
||||
{
|
||||
if (s->state == state)
|
||||
return;
|
||||
s->state = state;
|
||||
if (state != DMC_RINGING) {
|
||||
s->ring_until = 0;
|
||||
}
|
||||
if (state == DMC_IDLE || state == DMC_CLOSED) {
|
||||
s->hang = HANG_NONE;
|
||||
s->user_escape = 0;
|
||||
}
|
||||
push_event(s, DMC_EV_STATE, 0, 1, "", "");
|
||||
}
|
||||
|
||||
static void clear_queue(dmc_session *s)
|
||||
{
|
||||
while (s->queue) {
|
||||
struct command *c = s->queue;
|
||||
s->queue = c->next;
|
||||
command_free(c);
|
||||
}
|
||||
command_free(s->active);
|
||||
s->active = NULL;
|
||||
buf_clear(&s->response);
|
||||
}
|
||||
|
||||
dmc_session *dmc_session_new(void)
|
||||
{
|
||||
dmc_session *s = calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
abort();
|
||||
s->next_tag = 1;
|
||||
s->connect_text = dup_str("");
|
||||
s->caller_number = dup_str("");
|
||||
s->caller_name = dup_str("");
|
||||
return s;
|
||||
}
|
||||
|
||||
void dmc_session_free(dmc_session *s)
|
||||
{
|
||||
if (!s)
|
||||
return;
|
||||
clear_queue(s);
|
||||
while (s->events) {
|
||||
struct event *e = s->events;
|
||||
s->events = e->next;
|
||||
event_free(e);
|
||||
}
|
||||
event_free(s->current);
|
||||
buf_free(&s->out);
|
||||
buf_free(&s->term);
|
||||
buf_free(&s->line);
|
||||
buf_free(&s->typed);
|
||||
buf_free(&s->response);
|
||||
free(s->connect_text);
|
||||
free(s->caller_number);
|
||||
free(s->caller_name);
|
||||
free(s);
|
||||
}
|
||||
|
||||
void dmc_open(dmc_session *s, int64_t now_ms)
|
||||
{
|
||||
clear_queue(s);
|
||||
buf_clear(&s->out);
|
||||
buf_clear(&s->line);
|
||||
buf_clear(&s->typed);
|
||||
s->now = now_ms;
|
||||
s->hang = HANG_NONE;
|
||||
set_state(s, DMC_IDLE);
|
||||
}
|
||||
|
||||
void dmc_close(dmc_session *s)
|
||||
{
|
||||
struct command *a = s->active;
|
||||
s->active = NULL;
|
||||
if (a && a->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, a->tag, 0, "", "closed");
|
||||
command_free(a);
|
||||
while (s->queue) {
|
||||
struct command *c = s->queue;
|
||||
s->queue = c->next;
|
||||
if (c->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, c->tag, 0, "", "closed");
|
||||
command_free(c);
|
||||
}
|
||||
buf_clear(&s->out);
|
||||
buf_clear(&s->line);
|
||||
buf_clear(&s->typed);
|
||||
buf_clear(&s->response);
|
||||
set_state(s, DMC_CLOSED);
|
||||
}
|
||||
|
||||
/* the command's letter after AT and what comes before it, skipping
|
||||
spaces: its first command (D, A, H, O, or '!' for AT!) */
|
||||
static char first_command(const char *cmd)
|
||||
{
|
||||
const char *p = cmd;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
if (!starts_ci(p, "AT"))
|
||||
return 0;
|
||||
p += 2;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
return (char)toupper((unsigned char)*p);
|
||||
}
|
||||
|
||||
/* the command starts a configurator: AT!C, AT!CF, AT!CT */
|
||||
static int is_configurator(const char *cmd)
|
||||
{
|
||||
const char *p = cmd;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
if (!starts_ci(p, "AT"))
|
||||
return 0;
|
||||
p += 2;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
return p[0] == '!' && toupper((unsigned char)p[1]) == 'C';
|
||||
}
|
||||
|
||||
/* the state a command written puts the call in */
|
||||
static void command_sent(dmc_session *s, const char *cmd)
|
||||
{
|
||||
char c = first_command(cmd);
|
||||
if (is_configurator(cmd)) {
|
||||
if (s->state == DMC_IDLE)
|
||||
set_state(s, DMC_CONFIGURATOR);
|
||||
} else if (c == 'D') {
|
||||
if (s->state == DMC_IDLE || s->state == DMC_ONLINE_COMMAND)
|
||||
set_state(s, DMC_DIALING);
|
||||
} else if (c == 'A') {
|
||||
set_state(s, DMC_ANSWERING);
|
||||
} else if (c == 'H' && s->state == DMC_ONLINE_COMMAND) {
|
||||
set_state(s, DMC_HANGING_UP);
|
||||
}
|
||||
}
|
||||
|
||||
/* the next command written, if none waits */
|
||||
static void pump(dmc_session *s)
|
||||
{
|
||||
if (s->active || !s->queue || s->state == DMC_CLOSED ||
|
||||
s->state == DMC_ONLINE || s->state == DMC_CONFIGURATOR ||
|
||||
s->state == DMC_DIALING || s->state == DMC_ANSWERING)
|
||||
return;
|
||||
if (s->hang == HANG_GUARD || s->hang == HANG_ESCAPED)
|
||||
return;
|
||||
struct command *c = s->queue;
|
||||
s->queue = c->next;
|
||||
c->next = NULL;
|
||||
s->active = c;
|
||||
s->active_until = s->now + c->timeout_ms;
|
||||
s->echo_seen = 0;
|
||||
buf_clear(&s->response);
|
||||
buf_adds(&s->out, c->cmd);
|
||||
buf_addc(&s->out, '\r');
|
||||
command_sent(s, c->cmd);
|
||||
}
|
||||
|
||||
static int enqueue(dmc_session *s, const char *cmd, int flags,
|
||||
int timeout_ms, int tag, int front)
|
||||
{
|
||||
struct command *c = calloc(1, sizeof(*c));
|
||||
if (!c)
|
||||
abort();
|
||||
const char *p = cmd;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
if (starts_ci(p, "AT")) {
|
||||
c->cmd = dup_str(p);
|
||||
} else {
|
||||
size_t n = strlen(p);
|
||||
c->cmd = malloc(n + 3);
|
||||
if (!c->cmd)
|
||||
abort();
|
||||
memcpy(c->cmd, "AT", 2);
|
||||
memcpy(c->cmd + 2, p, n + 1);
|
||||
}
|
||||
/* (no CR or LF in it: one command line) */
|
||||
for (char *q = c->cmd; *q; q++)
|
||||
if (*q == '\r' || *q == '\n')
|
||||
*q = ' ';
|
||||
char first = first_command(c->cmd);
|
||||
c->tag = tag;
|
||||
c->flags = flags;
|
||||
c->timeout_ms = timeout_ms > 0 ? timeout_ms :
|
||||
(first == 'D' || first == 'A') ? CALL_MS : COMMAND_MS;
|
||||
if (front) {
|
||||
c->next = s->queue;
|
||||
s->queue = c;
|
||||
} else {
|
||||
struct command **q = &s->queue;
|
||||
while (*q)
|
||||
q = &(*q)->next;
|
||||
*q = c;
|
||||
}
|
||||
pump(s);
|
||||
return tag;
|
||||
}
|
||||
|
||||
int dmc_command(dmc_session *s, const char *cmd, int flags, int timeout_ms)
|
||||
{
|
||||
if (!cmd || s->state == DMC_CLOSED || s->state == DMC_ONLINE ||
|
||||
s->state == DMC_CONFIGURATOR)
|
||||
return -1;
|
||||
int tag = s->next_tag++;
|
||||
if (s->next_tag <= 0)
|
||||
s->next_tag = 1;
|
||||
return enqueue(s, cmd, flags, timeout_ms, tag, 0);
|
||||
}
|
||||
|
||||
int dmc_dial(dmc_session *s, const char *number)
|
||||
{
|
||||
dmc_buf b = { 0 };
|
||||
buf_adds(&b, "ATD");
|
||||
for (const char *p = number ? number : ""; *p; p++)
|
||||
if (*p != '\r' && *p != '\n' && *p != ';')
|
||||
buf_addc(&b, *p);
|
||||
int tag = dmc_command(s, buf_str(&b), 0, 0);
|
||||
buf_free(&b);
|
||||
return tag;
|
||||
}
|
||||
|
||||
int dmc_answer(dmc_session *s)
|
||||
{
|
||||
return dmc_command(s, "ATA", 0, 0);
|
||||
}
|
||||
|
||||
void dmc_hangup(dmc_session *s)
|
||||
{
|
||||
switch (s->state) {
|
||||
case DMC_ONLINE:
|
||||
if (s->hang == HANG_NONE) {
|
||||
s->hang = HANG_GUARD;
|
||||
s->hang_at = s->now + GUARD_MS;
|
||||
set_state(s, DMC_HANGING_UP);
|
||||
}
|
||||
break;
|
||||
case DMC_DIALING:
|
||||
case DMC_ANSWERING:
|
||||
/* any character stops it, with NO CARRIER */
|
||||
buf_addc(&s->out, '\r');
|
||||
break;
|
||||
case DMC_IDLE:
|
||||
case DMC_RINGING:
|
||||
case DMC_ONLINE_COMMAND:
|
||||
enqueue(s, "ATH", 0, 0, 0, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* the line is a final result code: its kind (0 none) */
|
||||
enum { R_NONE, R_OK, R_ERROR, R_CONNECT, R_NO_CARRIER, R_FAIL };
|
||||
|
||||
static int result_kind(const char *l)
|
||||
{
|
||||
if (!strcmp(l, "OK"))
|
||||
return R_OK;
|
||||
if (!strcmp(l, "ERROR") || !strcmp(l, "+FCERROR"))
|
||||
return R_ERROR;
|
||||
if (!strncmp(l, "CONNECT", 7) && (!l[7] || l[7] == ' '))
|
||||
return R_CONNECT;
|
||||
if (!strcmp(l, "NO CARRIER"))
|
||||
return R_NO_CARRIER;
|
||||
if (!strcmp(l, "BUSY") || !strcmp(l, "NO DIALTONE") ||
|
||||
!strcmp(l, "NO DIAL TONE") || !strcmp(l, "NO ANSWER") ||
|
||||
!strcmp(l, "DELAYED") || !strcmp(l, "BLACKLISTED") ||
|
||||
!strcmp(l, "NO LINE"))
|
||||
return R_FAIL;
|
||||
return R_NONE;
|
||||
}
|
||||
|
||||
static void connected(dmc_session *s, const char *l)
|
||||
{
|
||||
set_str(&s->connect_text, l);
|
||||
s->connect_rate = atoi(l + 7);
|
||||
s->hang = HANG_NONE;
|
||||
set_state(s, DMC_ONLINE);
|
||||
}
|
||||
|
||||
/* the caller ID's lines after a RING: "NMBR = 5551234" */
|
||||
static int caller_line(dmc_session *s, const char *l)
|
||||
{
|
||||
static const char *const names[] = { "DATE", "TIME", "NMBR", "NAME",
|
||||
"MESG", "DDN_NMBR", NULL };
|
||||
for (int i = 0; names[i]; i++) {
|
||||
size_t n = strlen(names[i]);
|
||||
if (strncmp(l, names[i], n))
|
||||
continue;
|
||||
const char *p = l + n;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
if (*p != '=')
|
||||
continue;
|
||||
p++;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
if (!strcmp(names[i], "NMBR"))
|
||||
set_str(&s->caller_number, p);
|
||||
else if (!strcmp(names[i], "NAME"))
|
||||
set_str(&s->caller_name, p);
|
||||
dmc_buf b = { 0 };
|
||||
buf_adds(&b, names[i]);
|
||||
buf_addc(&b, '=');
|
||||
buf_adds(&b, p);
|
||||
push_event(s, DMC_EV_CALLER, 0, 1, buf_str(&b), "");
|
||||
buf_free(&b);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* a result nobody's command waited for */
|
||||
static void unsolicited(dmc_session *s, const char *l, int kind)
|
||||
{
|
||||
if (!strcmp(l, "RING")) {
|
||||
if (s->state == DMC_IDLE || s->state == DMC_RINGING) {
|
||||
if (s->state == DMC_IDLE) {
|
||||
set_str(&s->caller_number, "");
|
||||
set_str(&s->caller_name, "");
|
||||
}
|
||||
set_state(s, DMC_RINGING);
|
||||
s->ring_until = s->now + RING_GAP_MS;
|
||||
}
|
||||
push_event(s, DMC_EV_RESULT, 0, 1, "", l);
|
||||
return;
|
||||
}
|
||||
if (caller_line(s, l))
|
||||
return;
|
||||
switch (kind) {
|
||||
case R_CONNECT:
|
||||
connected(s, l);
|
||||
break;
|
||||
case R_NO_CARRIER:
|
||||
case R_FAIL:
|
||||
set_state(s, DMC_IDLE);
|
||||
break;
|
||||
case R_OK:
|
||||
if (s->state == DMC_HANGING_UP)
|
||||
set_state(s, DMC_IDLE);
|
||||
else if (s->state == DMC_DIALING || s->state == DMC_ANSWERING)
|
||||
set_state(s, DMC_IDLE); /* ATD...; and the like */
|
||||
return;
|
||||
case R_ERROR:
|
||||
if (s->state == DMC_DIALING || s->state == DMC_ANSWERING ||
|
||||
s->state == DMC_CONFIGURATOR)
|
||||
set_state(s, DMC_IDLE);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
push_event(s, DMC_EV_RESULT, 0, kind != R_FAIL && kind != R_NO_CARRIER,
|
||||
"", l);
|
||||
}
|
||||
|
||||
/* the active command's final result */
|
||||
static void finish(dmc_session *s, const char *result, int kind)
|
||||
{
|
||||
struct command *c = s->active;
|
||||
s->active = NULL;
|
||||
char first = first_command(c->cmd);
|
||||
switch (kind) {
|
||||
case R_CONNECT:
|
||||
connected(s, result);
|
||||
break;
|
||||
case R_NO_CARRIER:
|
||||
case R_FAIL:
|
||||
set_state(s, DMC_IDLE);
|
||||
break;
|
||||
case R_OK:
|
||||
if (s->state == DMC_HANGING_UP || first == 'H' ||
|
||||
(first == 'Z' && s->state != DMC_CONFIGURATOR))
|
||||
set_state(s, DMC_IDLE);
|
||||
else if (s->state == DMC_DIALING || s->state == DMC_ANSWERING ||
|
||||
s->state == DMC_CONFIGURATOR)
|
||||
set_state(s, DMC_IDLE);
|
||||
break;
|
||||
case R_ERROR:
|
||||
if (s->state != DMC_ONLINE_COMMAND && s->state != DMC_IDLE)
|
||||
set_state(s, DMC_IDLE);
|
||||
break;
|
||||
}
|
||||
if (c->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, c->tag,
|
||||
kind == R_OK || kind == R_CONNECT,
|
||||
buf_str(&s->response), result);
|
||||
command_free(c);
|
||||
buf_clear(&s->response);
|
||||
pump(s);
|
||||
}
|
||||
|
||||
/* a whole line from the modem (no CR or LF) */
|
||||
static void line_in(dmc_session *s, const char *l)
|
||||
{
|
||||
/* online: the call's data, but for its end */
|
||||
if (s->state == DMC_ONLINE ||
|
||||
(s->state == DMC_HANGING_UP && s->hang == HANG_GUARD)) {
|
||||
if (!strcmp(l, "NO CARRIER")) {
|
||||
set_state(s, DMC_IDLE);
|
||||
push_event(s, DMC_EV_RESULT, 0, 0, "", l);
|
||||
} else if (!strcmp(l, "OK") && s->user_escape) {
|
||||
s->user_escape = 0;
|
||||
set_state(s, DMC_ONLINE_COMMAND);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (s->state == DMC_HANGING_UP && s->hang == HANG_ESCAPED) {
|
||||
if (!strcmp(l, "OK")) {
|
||||
s->hang = HANG_NONE;
|
||||
enqueue(s, "ATH", 0, 0, 0, 1);
|
||||
} else if (!strcmp(l, "NO CARRIER")) {
|
||||
set_state(s, DMC_IDLE);
|
||||
push_event(s, DMC_EV_RESULT, 0, 0, "", l);
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* a configurator: only its end */
|
||||
if (s->state == DMC_CONFIGURATOR) {
|
||||
const char *p = l;
|
||||
/* (its last screen may come before the OK on that line) */
|
||||
size_t n = strlen(p);
|
||||
int ok = n >= 2 && !strcmp(p + n - 2, "OK");
|
||||
int err = !strcmp(p, "ERROR");
|
||||
if (!ok && !err && strcmp(p, "RING"))
|
||||
return;
|
||||
if (s->active && (ok || err)) {
|
||||
finish(s, ok ? "OK" : "ERROR", ok ? R_OK : R_ERROR);
|
||||
return;
|
||||
}
|
||||
if (!strcmp(p, "RING")) {
|
||||
/* a RING ends it, with no OK */
|
||||
set_state(s, DMC_IDLE);
|
||||
unsolicited(s, p, R_NONE);
|
||||
return;
|
||||
}
|
||||
set_state(s, DMC_IDLE);
|
||||
return;
|
||||
}
|
||||
int kind = result_kind(l);
|
||||
if (s->active) {
|
||||
if (!s->echo_seen && same_ci(l, s->active->cmd)) {
|
||||
s->echo_seen = 1;
|
||||
return;
|
||||
}
|
||||
if (!*l && !s->response.n)
|
||||
return;
|
||||
if (!strcmp(l, "RING")) {
|
||||
/* a RING in a quiet response still reaches the
|
||||
screen */
|
||||
if (s->active->flags & DMC_QUIET)
|
||||
buf_adds(&s->term, "\r\nRING\r\n");
|
||||
unsolicited(s, l, R_NONE);
|
||||
return;
|
||||
}
|
||||
if (kind != R_NONE) {
|
||||
/* (the response without its blank lines at the end) */
|
||||
while (s->response.n &&
|
||||
s->response.p[s->response.n - 1] == '\n')
|
||||
buf_pop(&s->response);
|
||||
finish(s, l, kind);
|
||||
return;
|
||||
}
|
||||
if (s->response.n)
|
||||
buf_addc(&s->response, '\n');
|
||||
buf_adds(&s->response, l);
|
||||
return;
|
||||
}
|
||||
if (!*l)
|
||||
return;
|
||||
unsolicited(s, l, kind);
|
||||
}
|
||||
|
||||
void dmc_feed(dmc_session *s, const char *data, int n)
|
||||
{
|
||||
if (s->state == DMC_CLOSED)
|
||||
return;
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = data[i];
|
||||
int quiet = s->active && (s->active->flags & DMC_QUIET);
|
||||
if (!quiet)
|
||||
buf_addc(&s->term, c);
|
||||
if (c == '\n') {
|
||||
line_in(s, buf_str(&s->line));
|
||||
buf_clear(&s->line);
|
||||
continue;
|
||||
}
|
||||
if (c == '\r')
|
||||
continue;
|
||||
if (s->line.n < LINE_MAX)
|
||||
buf_addc(&s->line, c);
|
||||
}
|
||||
}
|
||||
|
||||
void dmc_type(dmc_session *s, const char *data, int n)
|
||||
{
|
||||
if (s->state == DMC_CLOSED)
|
||||
return;
|
||||
buf_add(&s->out, data, n);
|
||||
if (s->state == DMC_ONLINE) {
|
||||
/* the escape typed by hand: its OK is command mode */
|
||||
for (int i = 0; i < n; i++)
|
||||
if (data[i] == '+')
|
||||
s->user_escape = 1;
|
||||
return;
|
||||
}
|
||||
if (s->state == DMC_CONFIGURATOR)
|
||||
return;
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = data[i];
|
||||
if (c == '\r' || c == '\n') {
|
||||
if (s->typed.n)
|
||||
command_sent(s, buf_str(&s->typed));
|
||||
buf_clear(&s->typed);
|
||||
} else if (c == '\b' || c == 0x7f) {
|
||||
buf_pop(&s->typed);
|
||||
} else if (s->typed.n < LINE_MAX) {
|
||||
buf_addc(&s->typed, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int dmc_poll(dmc_session *s, int64_t now_ms)
|
||||
{
|
||||
s->now = now_ms;
|
||||
int64_t next = -1;
|
||||
if (s->state == DMC_CLOSED)
|
||||
return -1;
|
||||
if (s->hang == HANG_GUARD && now_ms >= s->hang_at) {
|
||||
buf_adds(&s->out, "+++");
|
||||
s->hang = HANG_ESCAPED;
|
||||
s->hang_at = now_ms + GUARD_MS + ESCAPE_OK_MS;
|
||||
} else if (s->hang == HANG_ESCAPED && now_ms >= s->hang_at) {
|
||||
/* no OK: ATH all the same */
|
||||
s->hang = HANG_NONE;
|
||||
enqueue(s, "ATH", 0, 0, 0, 1);
|
||||
}
|
||||
if (s->hang != HANG_NONE)
|
||||
next = s->hang_at - now_ms;
|
||||
if (s->state == DMC_RINGING && s->ring_until && now_ms >= s->ring_until)
|
||||
set_state(s, DMC_IDLE);
|
||||
if (s->state == DMC_RINGING && s->ring_until) {
|
||||
int64_t d = s->ring_until - now_ms;
|
||||
if (next < 0 || d < next)
|
||||
next = d;
|
||||
}
|
||||
if (s->active) {
|
||||
if (now_ms >= s->active_until) {
|
||||
struct command *c = s->active;
|
||||
s->active = NULL;
|
||||
if (s->state == DMC_DIALING || s->state == DMC_ANSWERING) {
|
||||
buf_addc(&s->out, '\r');
|
||||
set_state(s, DMC_IDLE);
|
||||
}
|
||||
if (c->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, c->tag, 0,
|
||||
buf_str(&s->response), "timeout");
|
||||
command_free(c);
|
||||
buf_clear(&s->response);
|
||||
} else {
|
||||
int64_t d = s->active_until - now_ms;
|
||||
if (next < 0 || d < next)
|
||||
next = d;
|
||||
}
|
||||
}
|
||||
pump(s);
|
||||
if (s->active && next < 0)
|
||||
next = s->active_until - now_ms;
|
||||
if (next > 0x7fffffff)
|
||||
next = 0x7fffffff;
|
||||
return (int)next;
|
||||
}
|
||||
|
||||
int dmc_output(dmc_session *s, char *buf, int max)
|
||||
{
|
||||
return buf_take(&s->out, buf, max);
|
||||
}
|
||||
|
||||
int dmc_terminal(dmc_session *s, char *buf, int max)
|
||||
{
|
||||
return buf_take(&s->term, buf, max);
|
||||
}
|
||||
|
||||
int dmc_next_event(dmc_session *s, dmc_event *ev)
|
||||
{
|
||||
event_free(s->current);
|
||||
s->current = NULL;
|
||||
struct event *e = s->events;
|
||||
if (!e) {
|
||||
memset(ev, 0, sizeof(*ev));
|
||||
ev->text = ev->result = "";
|
||||
return 0;
|
||||
}
|
||||
s->events = e->next;
|
||||
if (!s->events)
|
||||
s->events_tail = NULL;
|
||||
s->current = e;
|
||||
*ev = e->ev;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dmc_state(const dmc_session *s)
|
||||
{
|
||||
return s->state;
|
||||
}
|
||||
|
||||
const char *dmc_state_name(int state)
|
||||
{
|
||||
switch (state) {
|
||||
case DMC_CLOSED: return "Not connected";
|
||||
case DMC_IDLE: return "Idle";
|
||||
case DMC_DIALING: return "Dialling";
|
||||
case DMC_RINGING: return "Ringing";
|
||||
case DMC_ANSWERING: return "Answering";
|
||||
case DMC_ONLINE: return "Online";
|
||||
case DMC_ONLINE_COMMAND: return "Online (command mode)";
|
||||
case DMC_HANGING_UP: return "Hanging up";
|
||||
case DMC_CONFIGURATOR: return "Configurator";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
const char *dmc_connect_text(const dmc_session *s)
|
||||
{
|
||||
return s->connect_text;
|
||||
}
|
||||
|
||||
int dmc_connect_rate(const dmc_session *s)
|
||||
{
|
||||
return s->connect_rate;
|
||||
}
|
||||
|
||||
const char *dmc_caller_number(const dmc_session *s)
|
||||
{
|
||||
return s->caller_number;
|
||||
}
|
||||
|
||||
const char *dmc_caller_name(const dmc_session *s)
|
||||
{
|
||||
return s->caller_name;
|
||||
}
|
||||
|
||||
int dmc_version(void)
|
||||
{
|
||||
return DMC_VERSION;
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* util.c - growable byte buffers and string helpers
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void grow(dmc_buf *b, int need)
|
||||
{
|
||||
if (b->n + need + 1 <= b->cap)
|
||||
return;
|
||||
int cap = b->cap ? b->cap : 64;
|
||||
while (cap < b->n + need + 1)
|
||||
cap *= 2;
|
||||
char *p = realloc(b->p, (size_t)cap);
|
||||
if (!p)
|
||||
abort();
|
||||
b->p = p;
|
||||
b->cap = cap;
|
||||
}
|
||||
|
||||
void buf_add(dmc_buf *b, const char *s, int n)
|
||||
{
|
||||
if (n <= 0)
|
||||
return;
|
||||
grow(b, n);
|
||||
memcpy(b->p + b->n, s, (size_t)n);
|
||||
b->n += n;
|
||||
b->p[b->n] = '\0';
|
||||
}
|
||||
|
||||
void buf_adds(dmc_buf *b, const char *s)
|
||||
{
|
||||
buf_add(b, s, (int)strlen(s));
|
||||
}
|
||||
|
||||
void buf_addc(dmc_buf *b, char c)
|
||||
{
|
||||
buf_add(b, &c, 1);
|
||||
}
|
||||
|
||||
void buf_drop(dmc_buf *b, int n)
|
||||
{
|
||||
if (n >= b->n) {
|
||||
buf_clear(b);
|
||||
return;
|
||||
}
|
||||
memmove(b->p, b->p + n, (size_t)(b->n - n));
|
||||
b->n -= n;
|
||||
b->p[b->n] = '\0';
|
||||
}
|
||||
|
||||
void buf_pop(dmc_buf *b)
|
||||
{
|
||||
if (b->n > 0)
|
||||
b->p[--b->n] = '\0';
|
||||
}
|
||||
|
||||
void buf_clear(dmc_buf *b)
|
||||
{
|
||||
b->n = 0;
|
||||
if (b->p)
|
||||
b->p[0] = '\0';
|
||||
}
|
||||
|
||||
void buf_free(dmc_buf *b)
|
||||
{
|
||||
free(b->p);
|
||||
b->p = NULL;
|
||||
b->n = b->cap = 0;
|
||||
}
|
||||
|
||||
int buf_take(dmc_buf *b, char *out, int max)
|
||||
{
|
||||
int n = b->n < max ? b->n : max;
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
memcpy(out, b->p, (size_t)n);
|
||||
buf_drop(b, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
const char *buf_str(dmc_buf *b)
|
||||
{
|
||||
return b->p ? b->p : "";
|
||||
}
|
||||
|
||||
char *dup_n(const char *s, size_t n)
|
||||
{
|
||||
char *p = malloc(n + 1);
|
||||
if (!p)
|
||||
abort();
|
||||
memcpy(p, s, n);
|
||||
p[n] = '\0';
|
||||
return p;
|
||||
}
|
||||
|
||||
char *dup_str(const char *s)
|
||||
{
|
||||
return dup_n(s ? s : "", strlen(s ? s : ""));
|
||||
}
|
||||
|
||||
static char lower(char c)
|
||||
{
|
||||
return c >= 'A' && c <= 'Z' ? (char)(c - 'A' + 'a') : c;
|
||||
}
|
||||
|
||||
int same_ci(const char *s, const char *t)
|
||||
{
|
||||
while (*s && *t && lower(*s) == lower(*t)) {
|
||||
s++;
|
||||
t++;
|
||||
}
|
||||
return !*s && !*t;
|
||||
}
|
||||
|
||||
int starts_ci(const char *s, const char *p)
|
||||
{
|
||||
while (*p) {
|
||||
if (lower(*s) != lower(*p))
|
||||
return 0;
|
||||
s++;
|
||||
p++;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void set_str(char **p, const char *s)
|
||||
{
|
||||
free(*p);
|
||||
*p = dup_str(s);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* util.h - growable byte buffers and string helpers shared by the
|
||||
* library's parts
|
||||
*/
|
||||
#ifndef DMC_UTIL_H
|
||||
#define DMC_UTIL_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* a growable run of bytes, always ended by a '\0' past n */
|
||||
typedef struct dmc_buf {
|
||||
char *p;
|
||||
int n, cap;
|
||||
} dmc_buf;
|
||||
|
||||
void buf_add(dmc_buf *b, const char *s, int n);
|
||||
void buf_adds(dmc_buf *b, const char *s);
|
||||
void buf_addc(dmc_buf *b, char c);
|
||||
/* the last byte taken off (none: nothing) */
|
||||
void buf_pop(dmc_buf *b);
|
||||
/* the first n bytes taken out */
|
||||
void buf_drop(dmc_buf *b, int n);
|
||||
void buf_clear(dmc_buf *b);
|
||||
void buf_free(dmc_buf *b);
|
||||
/* up to max bytes moved into out: how many */
|
||||
int buf_take(dmc_buf *b, char *out, int max);
|
||||
/* its text ("" when empty) */
|
||||
const char *buf_str(dmc_buf *b);
|
||||
|
||||
/* a copy of s (or of its first n bytes) */
|
||||
char *dup_str(const char *s);
|
||||
char *dup_n(const char *s, size_t n);
|
||||
/* s and t the same, ignoring ASCII case */
|
||||
int same_ci(const char *s, const char *t);
|
||||
/* s begins with p, ignoring ASCII case */
|
||||
int starts_ci(const char *s, const char *p);
|
||||
/* *p set to a copy of s (the old one freed) */
|
||||
void set_str(char **p, const char *s);
|
||||
|
||||
#endif
|
||||
+756
@@ -0,0 +1,756 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* vt.c - a small ANSI terminal screen (dmodemctl.h)
|
||||
*
|
||||
* Enough of a VT100 and ANSI X3.64 for the modem's command mode and its
|
||||
* full-screen configurator (AT!CF): UTF-8 text, CR, LF, BS, TAB, the
|
||||
* cursor moves (CUP, CUU, CUD, CUF, CUB, CHA, VPA, ESC 7 and 8), erasing
|
||||
* (ED, EL, ECH), inserting and deleting (ICH, DCH, IL, DL), the scroll
|
||||
* region (DECSTBM), SGR's colours and bold, underline and reverse, the
|
||||
* cursor shown or hidden (?25), and the cursor position report the
|
||||
* modem asks for (DSR 6) to learn the screen's size. What it does not
|
||||
* know it skips.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
struct cell {
|
||||
uint32_t ch;
|
||||
int8_t fg, bg;
|
||||
uint8_t attr;
|
||||
};
|
||||
|
||||
struct line {
|
||||
struct cell *c;
|
||||
int n;
|
||||
};
|
||||
|
||||
enum { GROUND, ESCAPE, CSI, OSC, OSC_ESC, CHARSET };
|
||||
|
||||
#define MAX_PARAMS 16
|
||||
|
||||
struct dmc_vt {
|
||||
int cols, rows;
|
||||
struct cell *screen; /* rows * cols */
|
||||
/* the scrollback, newest last */
|
||||
struct line *hist;
|
||||
int hist_n, hist_max;
|
||||
int cx, cy; /* the cursor */
|
||||
int wrap; /* the next character wraps */
|
||||
int top, bottom; /* the scroll region, 0-based */
|
||||
int sx, sy; /* ESC 7's */
|
||||
int8_t fg, bg;
|
||||
uint8_t attr;
|
||||
int cursor_on;
|
||||
int changed;
|
||||
/* the parser */
|
||||
int state;
|
||||
int params[MAX_PARAMS], n_params;
|
||||
int private_mark;
|
||||
/* UTF-8 being read */
|
||||
uint32_t u;
|
||||
int u_left;
|
||||
dmc_buf reply, text;
|
||||
};
|
||||
|
||||
static struct cell blank(const dmc_vt *t)
|
||||
{
|
||||
struct cell c = { ' ', -1, t ? t->bg : -1, 0 };
|
||||
return c;
|
||||
}
|
||||
|
||||
static struct cell *cell(dmc_vt *t, int y, int x)
|
||||
{
|
||||
return &t->screen[y * t->cols + x];
|
||||
}
|
||||
|
||||
static void clear_cells(dmc_vt *t, int y, int x0, int x1)
|
||||
{
|
||||
struct cell b = blank(t);
|
||||
for (int x = x0; x < x1 && x < t->cols; x++)
|
||||
*cell(t, y, x) = b;
|
||||
}
|
||||
|
||||
dmc_vt *dmc_vt_new(int cols, int rows, int scrollback)
|
||||
{
|
||||
dmc_vt *t = calloc(1, sizeof(*t));
|
||||
if (!t)
|
||||
abort();
|
||||
t->hist_max = scrollback > 0 ? scrollback : 0;
|
||||
t->cols = cols > 0 ? cols : 80;
|
||||
t->rows = rows > 0 ? rows : 24;
|
||||
t->screen = calloc((size_t)(t->cols * t->rows), sizeof(struct cell));
|
||||
if (!t->screen)
|
||||
abort();
|
||||
dmc_vt_reset(t);
|
||||
return t;
|
||||
}
|
||||
|
||||
static void free_hist(dmc_vt *t)
|
||||
{
|
||||
for (int i = 0; i < t->hist_n; i++)
|
||||
free(t->hist[i].c);
|
||||
free(t->hist);
|
||||
t->hist = NULL;
|
||||
t->hist_n = 0;
|
||||
}
|
||||
|
||||
void dmc_vt_free(dmc_vt *t)
|
||||
{
|
||||
if (!t)
|
||||
return;
|
||||
free_hist(t);
|
||||
free(t->screen);
|
||||
buf_free(&t->reply);
|
||||
buf_free(&t->text);
|
||||
free(t);
|
||||
}
|
||||
|
||||
void dmc_vt_reset(dmc_vt *t)
|
||||
{
|
||||
t->fg = t->bg = -1;
|
||||
t->attr = 0;
|
||||
for (int y = 0; y < t->rows; y++)
|
||||
clear_cells(t, y, 0, t->cols);
|
||||
t->cx = t->cy = t->sx = t->sy = 0;
|
||||
t->wrap = 0;
|
||||
t->top = 0;
|
||||
t->bottom = t->rows - 1;
|
||||
t->cursor_on = 1;
|
||||
t->state = GROUND;
|
||||
t->u_left = 0;
|
||||
t->changed = 1;
|
||||
}
|
||||
|
||||
void dmc_vt_resize(dmc_vt *t, int cols, int rows)
|
||||
{
|
||||
if (cols < 2 || rows < 2 || (cols == t->cols && rows == t->rows))
|
||||
return;
|
||||
struct cell *s = calloc((size_t)(cols * rows), sizeof(struct cell));
|
||||
if (!s)
|
||||
abort();
|
||||
struct cell b = { ' ', -1, -1, 0 };
|
||||
for (int i = 0; i < cols * rows; i++)
|
||||
s[i] = b;
|
||||
/* the bottom of the old screen at the bottom of the new, where
|
||||
the cursor's line would otherwise go */
|
||||
int shift = t->cy >= rows ? t->cy - rows + 1 : 0;
|
||||
for (int y = 0; y < rows && y + shift < t->rows; y++)
|
||||
for (int x = 0; x < cols && x < t->cols; x++)
|
||||
s[y * cols + x] = *cell(t, y + shift, x);
|
||||
free(t->screen);
|
||||
t->screen = s;
|
||||
t->cols = cols;
|
||||
t->rows = rows;
|
||||
t->cy -= shift;
|
||||
if (t->cx >= cols)
|
||||
t->cx = cols - 1;
|
||||
if (t->cy >= rows)
|
||||
t->cy = rows - 1;
|
||||
t->top = 0;
|
||||
t->bottom = rows - 1;
|
||||
t->wrap = 0;
|
||||
t->changed = 1;
|
||||
}
|
||||
|
||||
int dmc_vt_cols(const dmc_vt *t) { return t->cols; }
|
||||
int dmc_vt_rows(const dmc_vt *t) { return t->rows; }
|
||||
|
||||
/* the top line of the screen to the scrollback */
|
||||
static void keep_line(dmc_vt *t, int y)
|
||||
{
|
||||
if (!t->hist_max)
|
||||
return;
|
||||
if (t->hist_n == t->hist_max) {
|
||||
free(t->hist[0].c);
|
||||
memmove(t->hist, t->hist + 1,
|
||||
sizeof(*t->hist) * (size_t)(t->hist_n - 1));
|
||||
t->hist_n--;
|
||||
} else {
|
||||
struct line *h = realloc(t->hist, sizeof(*h) *
|
||||
(size_t)(t->hist_n + 1));
|
||||
if (!h)
|
||||
abort();
|
||||
t->hist = h;
|
||||
}
|
||||
struct line *l = &t->hist[t->hist_n++];
|
||||
l->n = t->cols;
|
||||
l->c = malloc(sizeof(struct cell) * (size_t)t->cols);
|
||||
if (!l->c)
|
||||
abort();
|
||||
memcpy(l->c, cell(t, y, 0), sizeof(struct cell) * (size_t)t->cols);
|
||||
}
|
||||
|
||||
/* the region from top to bottom up n lines */
|
||||
static void scroll_up(dmc_vt *t, int top, int bottom, int n)
|
||||
{
|
||||
for (int k = 0; k < n; k++) {
|
||||
if (top == 0 && bottom == t->rows - 1)
|
||||
keep_line(t, 0);
|
||||
memmove(cell(t, top, 0), cell(t, top + 1, 0),
|
||||
sizeof(struct cell) * (size_t)(t->cols * (bottom - top)));
|
||||
clear_cells(t, bottom, 0, t->cols);
|
||||
}
|
||||
}
|
||||
|
||||
static void scroll_down(dmc_vt *t, int top, int bottom, int n)
|
||||
{
|
||||
for (int k = 0; k < n; k++) {
|
||||
memmove(cell(t, top + 1, 0), cell(t, top, 0),
|
||||
sizeof(struct cell) * (size_t)(t->cols * (bottom - top)));
|
||||
clear_cells(t, top, 0, t->cols);
|
||||
}
|
||||
}
|
||||
|
||||
static void line_feed(dmc_vt *t)
|
||||
{
|
||||
if (t->cy == t->bottom)
|
||||
scroll_up(t, t->top, t->bottom, 1);
|
||||
else if (t->cy < t->rows - 1)
|
||||
t->cy++;
|
||||
}
|
||||
|
||||
static void put(dmc_vt *t, uint32_t ch)
|
||||
{
|
||||
if (t->wrap) {
|
||||
t->cx = 0;
|
||||
line_feed(t);
|
||||
t->wrap = 0;
|
||||
}
|
||||
struct cell *c = cell(t, t->cy, t->cx);
|
||||
c->ch = ch;
|
||||
c->fg = t->fg;
|
||||
c->bg = t->bg;
|
||||
c->attr = t->attr;
|
||||
if (t->cx == t->cols - 1)
|
||||
t->wrap = 1;
|
||||
else
|
||||
t->cx++;
|
||||
}
|
||||
|
||||
static int param(const dmc_vt *t, int i, int def)
|
||||
{
|
||||
return i < t->n_params && t->params[i] > 0 ? t->params[i] : def;
|
||||
}
|
||||
|
||||
static void clamp_cursor(dmc_vt *t)
|
||||
{
|
||||
if (t->cx < 0)
|
||||
t->cx = 0;
|
||||
if (t->cx >= t->cols)
|
||||
t->cx = t->cols - 1;
|
||||
if (t->cy < 0)
|
||||
t->cy = 0;
|
||||
if (t->cy >= t->rows)
|
||||
t->cy = t->rows - 1;
|
||||
t->wrap = 0;
|
||||
}
|
||||
|
||||
static void sgr(dmc_vt *t)
|
||||
{
|
||||
if (!t->n_params) {
|
||||
t->fg = t->bg = -1;
|
||||
t->attr = 0;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < t->n_params; i++) {
|
||||
int p = t->params[i] < 0 ? 0 : t->params[i];
|
||||
if (p == 0) {
|
||||
t->fg = t->bg = -1;
|
||||
t->attr = 0;
|
||||
} else if (p == 1) {
|
||||
t->attr |= DMC_BOLD;
|
||||
} else if (p == 4) {
|
||||
t->attr |= DMC_UNDERLINE;
|
||||
} else if (p == 7) {
|
||||
t->attr |= DMC_REVERSE;
|
||||
} else if (p == 22) {
|
||||
t->attr &= (uint8_t)~DMC_BOLD;
|
||||
} else if (p == 24) {
|
||||
t->attr &= (uint8_t)~DMC_UNDERLINE;
|
||||
} else if (p == 27) {
|
||||
t->attr &= (uint8_t)~DMC_REVERSE;
|
||||
} else if (p >= 30 && p <= 37) {
|
||||
t->fg = (int8_t)(p - 30);
|
||||
} else if (p == 39) {
|
||||
t->fg = -1;
|
||||
} else if (p >= 40 && p <= 47) {
|
||||
t->bg = (int8_t)(p - 40);
|
||||
} else if (p == 49) {
|
||||
t->bg = -1;
|
||||
} else if (p >= 90 && p <= 97) {
|
||||
t->fg = (int8_t)(p - 90 + 8);
|
||||
} else if (p >= 100 && p <= 107) {
|
||||
t->bg = (int8_t)(p - 100 + 8);
|
||||
} else if ((p == 38 || p == 48) && i + 1 < t->n_params) {
|
||||
/* 256 colours: the first 16 kept, the rest skipped */
|
||||
if (t->params[i + 1] == 5 && i + 2 < t->n_params) {
|
||||
int c = t->params[i + 2];
|
||||
if (c >= 0 && c < 16) {
|
||||
if (p == 38)
|
||||
t->fg = (int8_t)c;
|
||||
else
|
||||
t->bg = (int8_t)c;
|
||||
}
|
||||
i += 2;
|
||||
} else if (t->params[i + 1] == 2) {
|
||||
i += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void csi(dmc_vt *t, char final)
|
||||
{
|
||||
int n = param(t, 0, 1);
|
||||
switch (final) {
|
||||
case 'A':
|
||||
t->cy -= n;
|
||||
if (t->cy < t->top && t->cy + n >= t->top)
|
||||
t->cy = t->top;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'B':
|
||||
t->cy += n;
|
||||
if (t->cy > t->bottom && t->cy - n <= t->bottom)
|
||||
t->cy = t->bottom;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'C':
|
||||
t->cx += n;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'D':
|
||||
t->cx -= n;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'E':
|
||||
t->cy += n;
|
||||
t->cx = 0;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'F':
|
||||
t->cy -= n;
|
||||
t->cx = 0;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'G':
|
||||
case '`':
|
||||
t->cx = n - 1;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'd':
|
||||
t->cy = n - 1;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'H':
|
||||
case 'f':
|
||||
t->cy = param(t, 0, 1) - 1;
|
||||
t->cx = param(t, 1, 1) - 1;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'J': {
|
||||
int p = t->n_params ? t->params[0] : 0;
|
||||
if (p == 0) {
|
||||
clear_cells(t, t->cy, t->cx, t->cols);
|
||||
for (int y = t->cy + 1; y < t->rows; y++)
|
||||
clear_cells(t, y, 0, t->cols);
|
||||
} else if (p == 1) {
|
||||
for (int y = 0; y < t->cy; y++)
|
||||
clear_cells(t, y, 0, t->cols);
|
||||
clear_cells(t, t->cy, 0, t->cx + 1);
|
||||
} else if (p == 2 || p == 3) {
|
||||
for (int y = 0; y < t->rows; y++)
|
||||
clear_cells(t, y, 0, t->cols);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'K': {
|
||||
int p = t->n_params ? t->params[0] : 0;
|
||||
if (p == 0)
|
||||
clear_cells(t, t->cy, t->cx, t->cols);
|
||||
else if (p == 1)
|
||||
clear_cells(t, t->cy, 0, t->cx + 1);
|
||||
else if (p == 2)
|
||||
clear_cells(t, t->cy, 0, t->cols);
|
||||
break;
|
||||
}
|
||||
case 'X':
|
||||
clear_cells(t, t->cy, t->cx, t->cx + n);
|
||||
break;
|
||||
case '@': {
|
||||
if (n > t->cols - t->cx)
|
||||
n = t->cols - t->cx;
|
||||
memmove(cell(t, t->cy, t->cx + n), cell(t, t->cy, t->cx),
|
||||
sizeof(struct cell) * (size_t)(t->cols - t->cx - n));
|
||||
clear_cells(t, t->cy, t->cx, t->cx + n);
|
||||
break;
|
||||
}
|
||||
case 'P': {
|
||||
if (n > t->cols - t->cx)
|
||||
n = t->cols - t->cx;
|
||||
memmove(cell(t, t->cy, t->cx), cell(t, t->cy, t->cx + n),
|
||||
sizeof(struct cell) * (size_t)(t->cols - t->cx - n));
|
||||
clear_cells(t, t->cy, t->cols - n, t->cols);
|
||||
break;
|
||||
}
|
||||
case 'L':
|
||||
if (t->cy >= t->top && t->cy <= t->bottom) {
|
||||
if (n > t->bottom - t->cy + 1)
|
||||
n = t->bottom - t->cy + 1;
|
||||
scroll_down(t, t->cy, t->bottom, n);
|
||||
}
|
||||
break;
|
||||
case 'M':
|
||||
if (t->cy >= t->top && t->cy <= t->bottom) {
|
||||
if (n > t->bottom - t->cy + 1)
|
||||
n = t->bottom - t->cy + 1;
|
||||
for (int k = 0; k < n; k++) {
|
||||
memmove(cell(t, t->cy, 0), cell(t, t->cy + 1, 0),
|
||||
sizeof(struct cell) *
|
||||
(size_t)(t->cols * (t->bottom - t->cy)));
|
||||
clear_cells(t, t->bottom, 0, t->cols);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'S':
|
||||
scroll_up(t, t->top, t->bottom, n);
|
||||
break;
|
||||
case 'T':
|
||||
scroll_down(t, t->top, t->bottom, n);
|
||||
break;
|
||||
case 'm':
|
||||
sgr(t);
|
||||
break;
|
||||
case 'r': {
|
||||
int top = param(t, 0, 1) - 1, bottom = param(t, 1, t->rows) - 1;
|
||||
if (bottom >= t->rows)
|
||||
bottom = t->rows - 1;
|
||||
if (top < bottom) {
|
||||
t->top = top;
|
||||
t->bottom = bottom;
|
||||
t->cx = t->cy = 0;
|
||||
t->wrap = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 's':
|
||||
t->sx = t->cx;
|
||||
t->sy = t->cy;
|
||||
break;
|
||||
case 'u':
|
||||
t->cx = t->sx;
|
||||
t->cy = t->sy;
|
||||
clamp_cursor(t);
|
||||
break;
|
||||
case 'n':
|
||||
if (t->n_params && t->params[0] == 6) {
|
||||
char r[32];
|
||||
snprintf(r, sizeof(r), "\x1b[%d;%dR", t->cy + 1, t->cx + 1);
|
||||
buf_adds(&t->reply, r);
|
||||
} else if (t->n_params && t->params[0] == 5) {
|
||||
buf_adds(&t->reply, "\x1b[0n");
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
if (!t->private_mark)
|
||||
buf_adds(&t->reply, "\x1b[?1;2c");
|
||||
break;
|
||||
case 'h':
|
||||
case 'l':
|
||||
if (t->private_mark == '?') {
|
||||
for (int i = 0; i < t->n_params; i++) {
|
||||
if (t->params[i] == 25)
|
||||
t->cursor_on = final == 'h';
|
||||
else if (t->params[i] == 1049 ||
|
||||
t->params[i] == 47 ||
|
||||
t->params[i] == 1047) {
|
||||
for (int y = 0; y < t->rows; y++)
|
||||
clear_cells(t, y, 0, t->cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void control(dmc_vt *t, uint32_t c)
|
||||
{
|
||||
switch (c) {
|
||||
case '\r':
|
||||
t->cx = 0;
|
||||
t->wrap = 0;
|
||||
break;
|
||||
case '\n':
|
||||
case '\v':
|
||||
case '\f':
|
||||
line_feed(t);
|
||||
t->wrap = 0;
|
||||
break;
|
||||
case '\b':
|
||||
if (t->cx > 0)
|
||||
t->cx--;
|
||||
t->wrap = 0;
|
||||
break;
|
||||
case '\t':
|
||||
t->cx = (t->cx / 8 + 1) * 8;
|
||||
if (t->cx >= t->cols)
|
||||
t->cx = t->cols - 1;
|
||||
t->wrap = 0;
|
||||
break;
|
||||
case 0x1b:
|
||||
t->state = ESCAPE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void byte(dmc_vt *t, unsigned char b)
|
||||
{
|
||||
switch (t->state) {
|
||||
case ESCAPE:
|
||||
t->state = GROUND;
|
||||
if (b == '[') {
|
||||
t->state = CSI;
|
||||
t->n_params = 0;
|
||||
t->private_mark = 0;
|
||||
memset(t->params, -1, sizeof(t->params));
|
||||
} else if (b == ']') {
|
||||
t->state = OSC;
|
||||
} else if (b == '(' || b == ')' || b == '*' || b == '+') {
|
||||
t->state = CHARSET;
|
||||
} else if (b == '7') {
|
||||
t->sx = t->cx;
|
||||
t->sy = t->cy;
|
||||
} else if (b == '8') {
|
||||
t->cx = t->sx;
|
||||
t->cy = t->sy;
|
||||
clamp_cursor(t);
|
||||
} else if (b == 'D') {
|
||||
line_feed(t);
|
||||
} else if (b == 'E') {
|
||||
t->cx = 0;
|
||||
line_feed(t);
|
||||
} else if (b == 'M') {
|
||||
if (t->cy == t->top)
|
||||
scroll_down(t, t->top, t->bottom, 1);
|
||||
else if (t->cy > 0)
|
||||
t->cy--;
|
||||
} else if (b == 'c') {
|
||||
dmc_vt_reset(t);
|
||||
}
|
||||
return;
|
||||
case CHARSET:
|
||||
t->state = GROUND;
|
||||
return;
|
||||
case OSC:
|
||||
if (b == 7)
|
||||
t->state = GROUND;
|
||||
else if (b == 0x1b)
|
||||
t->state = OSC_ESC;
|
||||
return;
|
||||
case OSC_ESC:
|
||||
t->state = b == '\\' ? GROUND : OSC;
|
||||
return;
|
||||
case CSI:
|
||||
if (b >= '0' && b <= '9') {
|
||||
if (!t->n_params)
|
||||
t->n_params = 1;
|
||||
int *p = &t->params[t->n_params - 1];
|
||||
if (*p < 0)
|
||||
*p = 0;
|
||||
if (*p < 100000)
|
||||
*p = *p * 10 + (b - '0');
|
||||
} else if (b == ';' || b == ':') {
|
||||
if (!t->n_params)
|
||||
t->n_params = 1;
|
||||
if (t->n_params < MAX_PARAMS)
|
||||
t->n_params++;
|
||||
} else if (b == '?' || b == '>' || b == '=' || b == '<') {
|
||||
t->private_mark = b;
|
||||
} else if (b >= 0x40 && b <= 0x7e) {
|
||||
t->state = GROUND;
|
||||
csi(t, (char)b);
|
||||
} else if (b == 0x1b) {
|
||||
t->state = ESCAPE;
|
||||
} else if (b < 0x20) {
|
||||
control(t, b);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
/* the ground state: UTF-8 */
|
||||
if (t->u_left) {
|
||||
if ((b & 0xC0) == 0x80) {
|
||||
t->u = (t->u << 6) | (b & 0x3F);
|
||||
if (--t->u_left == 0)
|
||||
put(t, t->u);
|
||||
return;
|
||||
}
|
||||
t->u_left = 0;
|
||||
put(t, 0xFFFD);
|
||||
}
|
||||
if (b < 0x20 || b == 0x7f) {
|
||||
control(t, b);
|
||||
} else if (b < 0x80) {
|
||||
put(t, b);
|
||||
} else if ((b & 0xE0) == 0xC0) {
|
||||
t->u = b & 0x1F;
|
||||
t->u_left = 1;
|
||||
} else if ((b & 0xF0) == 0xE0) {
|
||||
t->u = b & 0x0F;
|
||||
t->u_left = 2;
|
||||
} else if ((b & 0xF8) == 0xF0) {
|
||||
t->u = b & 0x07;
|
||||
t->u_left = 3;
|
||||
} else {
|
||||
put(t, 0xFFFD);
|
||||
}
|
||||
}
|
||||
|
||||
void dmc_vt_feed(dmc_vt *t, const char *data, int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
byte(t, (unsigned char)data[i]);
|
||||
if (n > 0)
|
||||
t->changed = 1;
|
||||
}
|
||||
|
||||
int dmc_vt_reply(dmc_vt *t, char *buf, int max)
|
||||
{
|
||||
return buf_take(&t->reply, buf, max);
|
||||
}
|
||||
|
||||
int dmc_vt_history(const dmc_vt *t)
|
||||
{
|
||||
return t->hist_n;
|
||||
}
|
||||
|
||||
static const struct cell *get(const dmc_vt *t, int row, int col)
|
||||
{
|
||||
static const struct cell none = { ' ', -1, -1, 0 };
|
||||
if (row >= 0) {
|
||||
if (row >= t->rows || col < 0 || col >= t->cols)
|
||||
return &none;
|
||||
return &t->screen[row * t->cols + col];
|
||||
}
|
||||
int h = t->hist_n + row;
|
||||
if (h < 0 || col < 0 || col >= t->hist[h].n)
|
||||
return &none;
|
||||
return &t->hist[h].c[col];
|
||||
}
|
||||
|
||||
uint32_t dmc_vt_char(const dmc_vt *t, int row, int col)
|
||||
{
|
||||
return get(t, row, col)->ch;
|
||||
}
|
||||
|
||||
int dmc_vt_fg(const dmc_vt *t, int row, int col)
|
||||
{
|
||||
return get(t, row, col)->fg;
|
||||
}
|
||||
|
||||
int dmc_vt_bg(const dmc_vt *t, int row, int col)
|
||||
{
|
||||
return get(t, row, col)->bg;
|
||||
}
|
||||
|
||||
int dmc_vt_attr(const dmc_vt *t, int row, int col)
|
||||
{
|
||||
return get(t, row, col)->attr;
|
||||
}
|
||||
|
||||
static void utf8(dmc_buf *b, uint32_t c)
|
||||
{
|
||||
char o[4];
|
||||
if (c < 0x80) {
|
||||
buf_addc(b, (char)c);
|
||||
} else if (c < 0x800) {
|
||||
o[0] = (char)(0xC0 | (c >> 6));
|
||||
o[1] = (char)(0x80 | (c & 0x3F));
|
||||
buf_add(b, o, 2);
|
||||
} else if (c < 0x10000) {
|
||||
o[0] = (char)(0xE0 | (c >> 12));
|
||||
o[1] = (char)(0x80 | ((c >> 6) & 0x3F));
|
||||
o[2] = (char)(0x80 | (c & 0x3F));
|
||||
buf_add(b, o, 3);
|
||||
} else {
|
||||
o[0] = (char)(0xF0 | (c >> 18));
|
||||
o[1] = (char)(0x80 | ((c >> 12) & 0x3F));
|
||||
o[2] = (char)(0x80 | ((c >> 6) & 0x3F));
|
||||
o[3] = (char)(0x80 | (c & 0x3F));
|
||||
buf_add(b, o, 4);
|
||||
}
|
||||
}
|
||||
|
||||
const char *dmc_vt_text(dmc_vt *t, int row)
|
||||
{
|
||||
buf_clear(&t->text);
|
||||
int n = row >= 0 ? t->cols : (t->hist_n + row >= 0 ?
|
||||
t->hist[t->hist_n + row].n : 0);
|
||||
while (n > 0 && get(t, row, n - 1)->ch == ' ')
|
||||
n--;
|
||||
for (int x = 0; x < n; x++)
|
||||
utf8(&t->text, get(t, row, x)->ch);
|
||||
return buf_str(&t->text);
|
||||
}
|
||||
|
||||
int dmc_vt_cursor_row(const dmc_vt *t) { return t->cy; }
|
||||
int dmc_vt_cursor_col(const dmc_vt *t) { return t->cx; }
|
||||
int dmc_vt_cursor_visible(const dmc_vt *t) { return t->cursor_on; }
|
||||
|
||||
int dmc_vt_changed(dmc_vt *t)
|
||||
{
|
||||
int c = t->changed;
|
||||
t->changed = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
int dmc_vt_key(const dmc_vt *t, int key, char *buf, int max)
|
||||
{
|
||||
static const char *const seq[] = {
|
||||
[DMC_KEY_UP] = "\x1b[A", [DMC_KEY_DOWN] = "\x1b[B",
|
||||
[DMC_KEY_RIGHT] = "\x1b[C", [DMC_KEY_LEFT] = "\x1b[D",
|
||||
[DMC_KEY_HOME] = "\x1b[H", [DMC_KEY_END] = "\x1b[F",
|
||||
[DMC_KEY_PGUP] = "\x1b[5~", [DMC_KEY_PGDN] = "\x1b[6~",
|
||||
[DMC_KEY_INSERT] = "\x1b[2~", [DMC_KEY_DELETE] = "\x1b[3~",
|
||||
[DMC_KEY_BACKTAB] = "\x1b[Z",
|
||||
[DMC_KEY_F1] = "\x1bOP", [DMC_KEY_F2] = "\x1bOQ",
|
||||
[DMC_KEY_F3] = "\x1bOR", [DMC_KEY_F4] = "\x1bOS",
|
||||
[DMC_KEY_F5] = "\x1b[15~", [DMC_KEY_F6] = "\x1b[17~",
|
||||
[DMC_KEY_F7] = "\x1b[18~", [DMC_KEY_F8] = "\x1b[19~",
|
||||
[DMC_KEY_F9] = "\x1b[20~", [DMC_KEY_F10] = "\x1b[21~",
|
||||
[DMC_KEY_ENTER] = "\r", [DMC_KEY_BACKSPACE] = "\x7f",
|
||||
[DMC_KEY_TAB] = "\t", [DMC_KEY_ESCAPE] = "\x1b",
|
||||
};
|
||||
(void)t;
|
||||
if (key <= 0 || key > DMC_KEY_ESCAPE || !seq[key])
|
||||
return 0;
|
||||
int n = (int)strlen(seq[key]);
|
||||
if (n > max)
|
||||
return 0;
|
||||
memcpy(buf, seq[key], (size_t)n);
|
||||
return n;
|
||||
}
|
||||
|
||||
uint32_t dmc_vt_palette(int c)
|
||||
{
|
||||
/* the CGA's colours, as the configurator was drawn for them */
|
||||
static const uint32_t pal[16] = {
|
||||
0x000000, 0xAA0000, 0x00AA00, 0xAA5500,
|
||||
0x0000AA, 0xAA00AA, 0x00AAAA, 0xAAAAAA,
|
||||
0x555555, 0xFF5555, 0x55FF55, 0xFFFF55,
|
||||
0x5555FF, 0xFF55FF, 0x55FFFF, 0xFFFFFF,
|
||||
};
|
||||
return c >= 0 && c < 16 ? pal[c] : 0;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
add_executable(dmodemctl_test dmodemctl_test.c)
|
||||
target_link_libraries(dmodemctl_test PRIVATE dmodemctl)
|
||||
target_include_directories(dmodemctl_test PRIVATE ../src)
|
||||
add_test(NAME dmodemctl COMMAND dmodemctl_test ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
@@ -0,0 +1,5 @@
|
||||
AT#UD␍
|
||||
DIAG <2A4D3263 0=10>
|
||||
DIAG <2A4D3263 1=0>
|
||||
DIAG <2A4D3263 60=1>
|
||||
OK
|
||||
@@ -0,0 +1,480 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* 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);
|
||||
/* 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"));
|
||||
}
|
||||
|
||||
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();
|
||||
printf("%d checks, %d failed\n", checks, failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
AT!M␍
|
||||
model 1 73
|
||||
setting country pick B5 B5 +GCI=%s Line Country +GCI: the line's settings and tones
|
||||
choice 02 algeria ALGERIA
|
||||
choice 07 argentina ARGENTINA
|
||||
choice 09 australia AUSTRALIA
|
||||
choice 0A austria AUSTRIA
|
||||
choice 0C bahrein BAHREIN
|
||||
choice 0F belgium BELGIUM
|
||||
choice 16 brazil BRAZIL
|
||||
choice 1A brunei BRUNEI
|
||||
choice 1B bulgaria BULGARIA
|
||||
choice 20 canada CANADA
|
||||
choice 25 chile CHILE
|
||||
choice 26 china CHINA
|
||||
choice 100 ctr21europe CTR21EUROPE
|
||||
choice 2D cyprus CYPRUS
|
||||
choice 2E czech-republic CZECH_REPUBLIC
|
||||
choice 31 denmark DENMARK
|
||||
choice 36 egypt EGYPT
|
||||
choice 101 estonia ESTONIA
|
||||
choice 3C finland FINLAND
|
||||
choice 3D france FRANCE
|
||||
choice 42 germany GERMANY
|
||||
choice 46 greece GREECE
|
||||
choice 50 hong-kong HONG_KONG
|
||||
choice 51 hungary HUNGARY
|
||||
choice 52 iceland ICELAND
|
||||
choice 53 india INDIA
|
||||
choice 54 indonesia INDONESIA
|
||||
choice 57 ireland IRELAND
|
||||
choice 58 israel ISRAEL
|
||||
choice 59 italy ITALY
|
||||
choice 00 japan JAPAN
|
||||
choice 5E jordan JORDAN
|
||||
choice 61 korea KOREA
|
||||
choice 62 kuwait KUWAIT
|
||||
choice 103 latvia LATVIA
|
||||
choice 64 lebanon LEBANON
|
||||
choice 104 lithuania LITHUANIA
|
||||
choice 69 luxembourg LUXEMBOURG
|
||||
choice 6C malaysia MALAYSIA
|
||||
choice 70 malta MALTA
|
||||
choice 73 mexico MEXICO
|
||||
choice 77 morocco MOROCCO
|
||||
choice 7B netherlands NETHERLANDS
|
||||
choice 7E new-zealand NEW_ZEALAND
|
||||
choice 82 norway NORWAY
|
||||
choice 83 oman OMAN
|
||||
choice 84 pakistan PAKISTAN
|
||||
choice 88 peru PERU
|
||||
choice 89 philippines PHILIPPINES
|
||||
choice 8A poland POLAND
|
||||
choice 8B portugal PORTUGAL
|
||||
choice 8E romania ROMANIA
|
||||
choice B8 russia RUSSIA
|
||||
choice 98 saudiarabia SAUDIARABIA
|
||||
choice 9C singapore SINGAPORE
|
||||
choice C5 slovakia SLOVAKIA
|
||||
choice C6 slovenia SLOVENIA
|
||||
choice 9F southafrica SOUTHAFRICA
|
||||
choice 107 southkorea SOUTHKOREA
|
||||
choice A0 spain SPAIN
|
||||
choice A1 srilanka SRILANKA
|
||||
choice A5 sweden SWEDEN
|
||||
choice A6 switzerland SWITZERLAND
|
||||
choice 108 taiwan TAIWAN
|
||||
choice A9 thailand THAILAND
|
||||
choice AD tunisia TUNISIA
|
||||
choice AE turkey TURKEY
|
||||
choice B3 uae UAE
|
||||
choice B4 uk UK
|
||||
choice B7 uruguay URUGUAY
|
||||
choice B5 usa USA
|
||||
choice BC vietnam VIETNAM
|
||||
setting dial-mode pick T T %s Line Dialling T tone, P pulse
|
||||
choice T tone Tone
|
||||
choice P pulse Pulse
|
||||
setting pulse-ratio pick 0 0 &P%s Line Pulse make/break &P (S28): kept; the country sets the pulses
|
||||
choice 0 0 &P0
|
||||
choice 1 1 &P1
|
||||
choice 2 2 &P2
|
||||
choice 3 3 &P3
|
||||
setting line-type pick 0 0 &L%s Line Line type &L (S47): a dialled line, or a leased one
|
||||
choice 0 dialup Dial-up
|
||||
choice 1 leased-2wire Leased 2-wire
|
||||
choice 2 leased-4wire Leased 4-wire
|
||||
setting call-progress pick 4 4 X%s Line Result codes and tones X (S56): CONNECT's rate, dial tone, busy
|
||||
choice 0 basic X0 basic
|
||||
choice 1 rate X1 rate
|
||||
choice 2 dial-tone X2 dial tone
|
||||
choice 3 busy X3 busy
|
||||
choice 4 full X4 all
|
||||
setting modulation pick 92 92 +MS=%s Modulation Modulation +MS: the highest modulation tried
|
||||
choice 21 v21 V21
|
||||
choice 22 v22 V22
|
||||
choice 23 v23 V23
|
||||
choice 26 v26 V26
|
||||
choice 126 v26ter V26ter
|
||||
choice 226 v26bis V26bis
|
||||
choice 27 v27ter V27ter
|
||||
choice 29 v29 V29
|
||||
choice 229 v29fastpos V29FastPOS
|
||||
choice 122 v22bis V22bis
|
||||
choice 32 v32 V32
|
||||
choice 132 v32bis V32bis
|
||||
choice 232 v32terbo V32terbo
|
||||
choice 168 hst HST
|
||||
choice 128 vfc VFC
|
||||
choice 61 v61 V61
|
||||
choice 33 v33 V33
|
||||
choice 34 v34 V34
|
||||
choice 103 bell103 Bell103
|
||||
choice 18 v18 V18
|
||||
choice 45 baudot Baudot
|
||||
choice 110 edt EDT
|
||||
choice 118 dtmf DTMF
|
||||
choice 121 v21text V21text
|
||||
choice 123 v23text V23text
|
||||
choice 203 bell103text Bell103text
|
||||
choice 5 contactid ContactID
|
||||
choice 12 sms SMS
|
||||
choice 212 bell212 Bell212
|
||||
choice 201 bell201 Bell201
|
||||
choice 202 bell202 Bell202
|
||||
choice 208 bell208 Bell208
|
||||
choice 56 k56flex K56Flex
|
||||
choice 90 v90 V90
|
||||
choice 91 v91 V91
|
||||
choice 92 v92 V92
|
||||
choice 150 vcvm VCVM
|
||||
choice 200 vwb VWB
|
||||
choice 255 pep PEP
|
||||
choice 64 clear CLEAR
|
||||
setting automode pick 1 1 +MS=,%s Modulation Automode +MS, N: fall back to slower modulations
|
||||
choice 0 off Off
|
||||
choice 1 on On
|
||||
setting min-rate number 300 300 300 56000 +MS=,,%s Modulation Lowest rate (bit/s) +MS: hang up rather than go below it
|
||||
setting max-rate number 56000 56000 300 56000 +MS=,,,%s Modulation Highest rate (bit/s) +MS: the fastest the modem tries
|
||||
setting error-control pick 3 3 \N%s Error control Error correction \N (S103): V.42, MNP or none
|
||||
choice 0 normal Normal (none)
|
||||
choice 1 direct Direct (none)
|
||||
choice 2 reliable Reliable
|
||||
choice 3 auto-reliable Auto-reliable
|
||||
choice 4 lapm V.42 LAPM only
|
||||
choice 5 mnp MNP only
|
||||
setting compression pick 3 3 %%C%s Error control Compression %C (S104): needs error correction
|
||||
choice 0 off None
|
||||
choice 1 mnp5 MNP 5
|
||||
choice 2 v42bis-v44 V.42bis or V.44
|
||||
choice 3 auto Either
|
||||
setting mnp-block pick 3 3 \A%s Error control MNP block size \A (S105): MNP's longest block
|
||||
choice 0 64 64 octets
|
||||
choice 1 128 128 octets
|
||||
choice 2 192 192 octets
|
||||
choice 3 256 256 octets
|
||||
setting line-quality pick 0 0 %%E%s Error control Line quality monitor %E (S39): kept
|
||||
choice 0 0 %E0
|
||||
choice 1 1 %E1
|
||||
choice 2 2 %E2
|
||||
setting echo pick 1 1 E%s Terminal Command echo E (S13): the modem echoes what you type
|
||||
choice 0 off Off
|
||||
choice 1 on On
|
||||
setting quiet pick 0 0 Q%s Terminal Result codes Q (S14): OK, CONNECT and the others shown or not
|
||||
choice 0 shown Shown
|
||||
choice 1 hidden Hidden
|
||||
setting verbose pick 1 1 V%s Terminal Result code form V (S15): words or numbers
|
||||
choice 0 numeric Numbers
|
||||
choice 1 words Words
|
||||
setting connect-lines number 0 0 0 7 &A%s Terminal Lines before CONNECT &A (S70): 1 modulation, 2 protocol, 4 rate
|
||||
setting connect-protocol pick 0 0 \V%s Terminal Protocol in CONNECT \V (S106): CONNECT 33600/LAPM
|
||||
choice 0 off Off
|
||||
choice 1 on On
|
||||
choice 2 on2 On (\V2)
|
||||
setting connect-rate pick 0 0 &E%s Terminal CONNECT rate source &E (S71): kept
|
||||
choice 0 0 &E0
|
||||
choice 1 1 &E1
|
||||
setting dcd pick 0 0 &C%s Terminal DCD &C (S60): kept
|
||||
choice 0 always-on Always on
|
||||
choice 1 carrier Follows carrier
|
||||
setting flow-control pick 0 0 &H%s Terminal Flow control &H (S62): as #UD reports it
|
||||
choice 0 none None
|
||||
choice 1 rts-cts RTS/CTS
|
||||
choice 2 xon-xoff XON/XOFF
|
||||
choice 3 both Both
|
||||
setting y number 0 0 0 3 Y%s Terminal Y setting Y (S161): kept
|
||||
setting speaker pick 1 1 M%s Speaker Speaker M (S29)
|
||||
choice 0 off Off
|
||||
choice 1 until-connect On until CONNECT
|
||||
choice 2 always Always on
|
||||
choice 3 answer-only On after dialling until CONNECT
|
||||
setting volume pick 3 3 L%s Speaker Speaker volume L (S30)
|
||||
choice 0 lowest Lowest
|
||||
choice 1 low Low
|
||||
choice 2 medium Medium
|
||||
choice 3 high High
|
||||
setting mode pick 0 0 +FCLASS=%s Fax and voice Mode +FCLASS: data, fax or voice
|
||||
choice 0 data Data
|
||||
choice 1 fax1 Fax class 1
|
||||
choice 1.0 fax1.0 Fax class 1.0
|
||||
choice 8 voice Voice
|
||||
setting caller-id pick 0 0 #CID=%s Fax and voice Caller ID #CID: shown after the first RING
|
||||
choice 0 off Off
|
||||
choice 1 formatted Formatted
|
||||
choice 2 raw Unformatted
|
||||
setting modem-on-hold pick 0 0 +PMH=%s Fax and voice V.92 modem-on-hold +PMH: granted or refused
|
||||
choice 0 on Granted
|
||||
choice 1 off Refused
|
||||
setting modem-on-hold-time number 5 5 0 13 +PMHT=%s Fax and voice Modem-on-hold time code +PMHT: the longest hold granted (V.92's code)
|
||||
setting auto-answer number 0 0 0 255 S0=%s Timers Rings before answering S0: 0 never answers by itself
|
||||
setting dial-tone-wait number 2 2 0 255 S6=%s Timers Dial tone wait (s) S6: seconds to wait for a dial tone
|
||||
setting carrier-wait number 60 60 0 255 S7=%s Timers Carrier wait (s) S7: seconds to wait for the other modem
|
||||
setting comma-pause number 2 2 0 255 S8=%s Timers Comma pause (s) S8: a ',' in the number waits this long
|
||||
setting carrier-detect-time number 6 6 0 255 S9=%s Timers Carrier detect (0.1 s) S9: the carrier must last this long
|
||||
setting carrier-loss-time number 7 7 0 255 S10=%s Timers Carrier loss (0.1 s) S10: hang up after the carrier is gone this long
|
||||
setting dtmf-duration number 100 100 0 255 S11=%s Timers DTMF tone length (ms) S11: each digit's tone and the gap after it
|
||||
setting flash-time number 50 50 0 255 S24=%s Timers Hook flash (10 ms) S24: how long a hook flash lasts
|
||||
setting answer-delay number 2 2 0 255 S92=%s Timers Answer delay (s) S92: from ATA to the answer tone
|
||||
setting test-timer number 0 0 0 255 S18=%s Timers Test timer (s) S18: how long an AT&T test runs, 0 until AT&T0
|
||||
setting simulated-carrier number 0 0 0 1 S48=%s Timers Simulated carrier (V.13) S48: 1 sends V.13's simulated carrier
|
||||
setting fast-connect number 0 0 0 3 S49=%s Timers Fast connect (bits) S49: 1 the short V.22 handshake, 2 with HDLC flags
|
||||
setting fast-answer-tone number 0 0 0 255 S50=%s Timers Fast answer tone (10 ms) S50: the fast connect answer tone, 0 for 400 ms
|
||||
setting escape-char number 43 43 0 255 S2=%s Characters Escape character S2: the +++ escape's character (43 is '+')
|
||||
setting escape-guard number 50 50 0 255 S12=%s Characters Escape guard time (20 ms) S12: the quiet before and after +++
|
||||
setting cr-char number 13 13 0 127 S3=%s Characters Line end character S3: ends a command line (13 is CR)
|
||||
setting lf-char number 10 10 0 127 S4=%s Characters Line feed character S4: follows S3 in responses (10 is LF)
|
||||
setting bs-char number 8 8 0 127 S5=%s Characters Backspace character S5: erases a character (8 is BS)
|
||||
setting ifc text 2,2 2,2 +IFC=%s Extended DTE flow control +IFC=(0-3),(0-2)
|
||||
setting icf text 3,3 3,3 +ICF=%s Extended DTE character framing +ICF=(1-6),(0-3)
|
||||
setting ipr text 0 0 +IPR=%s Extended DTE rate +IPR=(0,300,1200,2400,4800,9600,19200,38400,57600,115200,230400,460800,921600)
|
||||
setting ilrr text 0 0 +ILRR=%s Extended Local rate reporting +ILRR=(0,1)
|
||||
setting idsr text 0 0 +IDSR=%s Extended DSR control +IDSR=(0,1)
|
||||
setting ilsd text 0 0 +ILSD=%s Extended Line signal detect +ILSD=(0,1)
|
||||
setting mr text 0 0 +MR=%s Extended Modulation reporting +MR=(0,1)
|
||||
setting er text 0 0 +ER=%s Extended Error control reporting +ER=(0,1)
|
||||
setting dr text 0 0 +DR=%s Extended Compression reporting +DR=(0,1)
|
||||
setting es text 3,0,2 3,0,2 +ES=%s Extended Error control (V.250) +ES=(0-4,6),(0-4),(0-6,8)
|
||||
setting esa text 0,0,0,0,0,0,255,255 0,0,0,0,0,0,255,255 +ESA=%s Extended V.80 synchronous access +ESA=(0-2),(0,1),(0,1),(0),(0-2),(0),(0-255),(0-255)
|
||||
setting itf text 383,128,0 383,128,0 +ITF=%s Extended V.80 transmit buffer +ITF=(1-4095),(0-4094),(0-500)
|
||||
setting a8e text 1,1,0,0 1,1,0,0 +A8E=%s Extended V.8 and V.8 bis +A8E=(1),(1),(0),(0,1)
|
||||
setting dsvd text 0 0 +DSVD=%s Extended DSVD (V.70) offered +DSVD=(0,1)
|
||||
setting asvd text 0 0 +ASVD=%s Extended ASVD (V.61) offered +ASVD=(0,1)
|
||||
setting ds text 3,0,2048,34 3,0,2048,34 +DS=%s Extended V.42bis compression +DS=(0-3),(0,1),(512-65535),(6-250)
|
||||
setting ds44 text 3,0,0,4096,4096,255,255,12288,12288 3,0,0,4096,4096,255,255,12288,12288 +DS44=%s Extended V.44 compression +DS44=(0-3),(0,1),(0,1),(256-65535),(256-65535),(32-255),(32-255),(512-65535),(512-65535)
|
||||
setting dsz text 1,1 1,1 +DSZ=%s Extended zstd compression +DSZ=(0,1),(0,1)
|
||||
setting b2 text 0 0 +B2=%s Extended Clear channel link layer +B2=(0,5,7,9)
|
||||
setting cbst text 0,0,0 0,0,0 +CBST=%s Extended Bearer service +CBST=(0,65,66,68,70,71,75,79,80,81,83),(0,1),(0,1)
|
||||
setting crc text 0 0 +CRC=%s Extended Cellular result codes +CRC=(0,1)
|
||||
setting pcw text 2 2 +PCW=%s Extended V.92 call waiting +PCW=(0-2)
|
||||
setting pig text 1 1 +PIG=%s Extended V.92 PCM upstream +PIG=(0,1)
|
||||
setting faa text 0 0 +FAA=%s Extended Adaptive answer +FAA=(0,1)
|
||||
setting fpr text 0 0 +FPR=%s Extended Fax DTE rate +FPR=(0,1,2,4,8,10,18)
|
||||
setting sec text 0 0 -SEC=%s Extended MNP 10EC -SEC=(0,1),(0-30)
|
||||
setting cvm text 63,8000,0 63,8000,0 +CVM=%s Extended V.cvm +CVM=(0-63),(8000,16000),(0,1)
|
||||
OK
|
||||
Reference in new issue
Block a user