modemu2k 0.2.5
Library that provides telnet capability to a comm program
Loading...
Searching...
No Matches
modemu2k.h
Go to the documentation of this file.
1#pragma once
2
47
48#include <poll.h> /* struct pollfd, used by m2k_get_pollfds()/m2k_step() below */
49#include <stddef.h> /* size_t */
50
51#include "modemu2k_version.h" /* M2K_VERSION_MAJOR / _MINOR / _PATCH / M2K_VERSION */
52
66#ifndef M2K_API
67# if defined(_WIN32)
68# if defined(M2K_BUILDING_DLL)
69# define M2K_API __declspec(dllexport)
70# else
71# define M2K_API __declspec(dllimport)
72# endif
73# elif defined(__GNUC__) && __GNUC__ >= 4
74# define M2K_API __attribute__((visibility("default")))
75# else
76# define M2K_API
77# endif
78#endif
79
80/* Deprecation marker. Consumers see a -Wdeprecated-declarations
81 * warning at the call site. modemu2k's own build defines
82 * M2K_SUPPRESS_DEPRECATED so the in-tree tests that still exercise
83 * the deprecated path build clean. */
84#if defined(M2K_SUPPRESS_DEPRECATED)
85# define M2K_DEPRECATED(msg)
86#elif defined(__GNUC__) || defined(__clang__)
87# define M2K_DEPRECATED(msg) __attribute__((deprecated(msg)))
88#elif defined(_MSC_VER)
89# define M2K_DEPRECATED(msg) __declspec(deprecated(msg))
90#else
91# define M2K_DEPRECATED(msg)
92#endif
93
94#ifdef __cplusplus
95extern "C" {
96#endif
97
99typedef struct m2k_s m2k_t;
100
112M2K_API const char *m2k_version(void);
113
119typedef void (*m2k_log_fn)(const char *msg, void *userdata);
120
140
162
168
174
189M2K_API void m2k_set_log_fn(m2k_t *ctx, m2k_log_fn fn, void *userdata);
190
192#define M2K_ERROR_BUFFER_SIZE 256
193
218M2K_API void m2k_set_error_buffer(m2k_t *ctx, char *buf, size_t size);
219
246M2K_API m2k_err_t m2k_atcmd(m2k_t *ctx, const char *cmd);
247
265M2K_API M2K_DEPRECATED("use m2k_run() or the step API instead")
266m2k_err_t m2k_dial(m2k_t *ctx, const char *host, const char *port);
267
281M2K_API M2K_DEPRECATED("use m2k_run() or the step API instead")
282m2k_err_t m2k_online(m2k_t *ctx);
283
297
316
324
334
350M2K_API m2k_err_t m2k_setup_pty(m2k_t *ctx, const char **slave_out);
351
365
373M2K_API m2k_err_t m2k_setup_dev(m2k_t *ctx, const char *dev);
374
395M2K_API m2k_err_t m2k_setup_listen(m2k_t *ctx, const char *port);
396
413
435M2K_API m2k_err_t m2k_write_from_app(m2k_t *ctx, const void *buf, size_t len,
436 size_t *consumed);
437
469M2K_API m2k_err_t m2k_read_to_app(m2k_t *ctx, void *buf, size_t max, size_t *len_out);
470
485
504
549
564
581
604M2K_API m2k_err_t m2k_setup_answer(m2k_t *ctx, const char *port);
605
618
645
646/* ── Steppable event-loop API ───────────────────────────────────────
647 *
648 * The functions below let a host application integrate modemu2k into
649 * its own event loop (poll/epoll/select/etc.) instead of handing the
650 * fds to the blocking m2k_run() above.
651 *
652 * Usage:
653 * while (!m2k_run_done(ctx)) {
654 * struct pollfd fds[M2K_MAX_POLLFDS]; // caller-provided buffer
655 * size_t nfds = M2K_MAX_POLLFDS;
656 * int timeout_ms;
657 * m2k_get_pollfds(ctx, fds, &nfds, &timeout_ms);
658 * // Splice fds into your own pollset, call poll()/epoll_wait().
659 * // On return, write back the .revents the OS reported into the
660 * // corresponding entries of `fds`.
661 * m2k_step(ctx, fds, nfds);
662 * }
663 *
664 * Dialing (ATD) is non-blocking: m2k_step() transitions the context
665 * into an internal DIAL state on ATD, returns control immediately,
666 * and resumes the connect() across subsequent m2k_step() calls. The
667 * host event loop is not held up by connect(). m2k_get_pollfds()
668 * publishes the in-progress socket fd while DIAL is active, so the
669 * host pollset stays accurate without special-casing.
670 *
671 * Answering (ATA, or S0 auto-answer) works the same way: the context
672 * parks in an internal ANSWER state while waiting for a caller within
673 * the S7 window, and m2k_get_pollfds() publishes the answer listener
674 * fd for the duration.
675 */
676
678#define M2K_MAX_POLLFDS 3
679
700M2K_API m2k_err_t m2k_get_pollfds(m2k_t *ctx, struct pollfd *fds,
701 size_t *nfds_inout, int *timeout_ms);
702
718M2K_API m2k_err_t m2k_step(m2k_t *ctx, struct pollfd *fds, size_t nfds);
719
726M2K_API int m2k_run_done(const m2k_t *ctx);
727
740
764M2K_API int m2k_describe_state(const m2k_t *ctx, char *buf, size_t cap);
765
778
810
824M2K_API void m2k_set_dtr(m2k_t *ctx, int on);
825
837M2K_API void m2k_set_rts(m2k_t *ctx, int on);
838
840M2K_API int m2k_get_dtr(const m2k_t *ctx);
841
843M2K_API int m2k_get_rts(const m2k_t *ctx);
844
862
866
893
897
898#ifdef __cplusplus
899}
900#endif
M2K_API void m2k_set_force_verbose(m2k_t *ctx, int on)
Bypass the ATV verbose category mask.
M2K_API int m2k_get_ring_count(const m2k_t *ctx)
Report how many RINGs the current inbound call has produced.
M2K_API m2k_err_t m2k_setup_app_io(m2k_t *ctx)
Embed mode: no real TTY fd; the host application supplies the bytes a TTY would normally produce,...
void(* m2k_log_fn)(const char *msg, void *userdata)
Log callback type.
Definition modemu2k.h:119
M2K_API int m2k_is_online(const m2k_t *ctx)
Test whether the modem is currently in online mode.
M2K_API int m2k_get_dtr(const m2k_t *ctx)
M2K_API m2k_err_t m2k_listen_accept(m2k_t *ctx)
Accept a single incoming connection on the listening socket opened by m2k_setup_listen() and adopt it...
M2K_API size_t m2k_pending_to_line(const m2k_t *ctx)
Count bytes taken from the DTE that have not reached the line.
M2K_API m2k_err_t m2k_run(m2k_t *ctx)
Run the modem command/online loop until the PTY closes.
M2K_API void m2k_set_error_buffer(m2k_t *ctx, char *buf, size_t size)
Install a buffer that receives a detailed message for the most recent error.
M2K_API void m2k_free(m2k_t *ctx)
Release all resources held by ctx.
M2K_API void m2k_set_log_level(m2k_t *ctx, m2k_log_level_t level)
Set the severity threshold for log delivery.
M2K_API m2k_err_t m2k_setup_comm_program(m2k_t *ctx, const char *cmd)
Allocate a PTY and fork/exec a comm program on the slave.
M2K_API int m2k_run_done(const m2k_t *ctx)
Test whether the session has ended.
M2K_API m2k_err_t m2k_setup_stdin(m2k_t *ctx)
Use stdin/stdout as the TTY (standalone mode).
M2K_API void m2k_set_log_fn(m2k_t *ctx, m2k_log_fn fn, void *userdata)
Install a log callback.
M2K_API m2k_err_t m2k_setup_dev(m2k_t *ctx, const char *dev)
Open an existing PTY device as the TTY.
M2K_API m2k_err_t m2k_escape(m2k_t *ctx)
Request an immediate return to command mode from online mode.
M2K_API const char * m2k_version(void)
Runtime version string of the linked libmodemu2k.
struct m2k_s m2k_t
Opaque modem emulator context. Create with m2k_new(), destroy with m2k_free().
Definition modemu2k.h:99
m2k_log_level_t
Log severity levels, ordered from most to least severe.
Definition modemu2k.h:133
@ M2K_LOG_WARN
Definition modemu2k.h:135
@ M2K_LOG_ERROR
Definition modemu2k.h:134
@ M2K_LOG_TRACE
Definition modemu2k.h:138
@ M2K_LOG_DEBUG
Definition modemu2k.h:137
@ M2K_LOG_INFO
Definition modemu2k.h:136
M2K_API int m2k_describe_state(const m2k_t *ctx, char *buf, size_t cap)
Write a human-readable snapshot of the modem's state into buf.
M2K_API size_t m2k_pending_to_dte(const m2k_t *ctx)
Count bytes queued for the DTE that it has not taken yet.
M2K_API m2k_err_t m2k_atcmd(m2k_t *ctx, const char *cmd)
Feed a Hayes AT command string to the modem.
M2K_API m2k_err_t m2k_hangup(m2k_t *ctx)
Hang up: abandon whatever connection activity is in progress.
M2K_API m2k_t * m2k_new(void)
Allocate and initialise a new modem context.
M2K_API m2k_err_t m2k_step(m2k_t *ctx, struct pollfd *fds, size_t nfds)
Run one non-blocking iteration of the state machine.
M2K_API int m2k_has_pending_output(const m2k_t *ctx)
Test whether the modem has TTY-bound bytes still buffered.
M2K_API int m2k_get_rts(const m2k_t *ctx)
m2k_err_t
Return codes used by all m2k_* functions.
Definition modemu2k.h:147
@ M2K_ERR_PTY
Definition modemu2k.h:150
@ M2K_ERR_NOMEM
Definition modemu2k.h:149
@ M2K_ERR_WOULDBLOCK
Definition modemu2k.h:157
@ M2K_ERR_TIMEOUT
Definition modemu2k.h:152
@ M2K_OK
Definition modemu2k.h:148
@ M2K_ERR_CANCELED
Definition modemu2k.h:155
@ M2K_ERR_BUG
Definition modemu2k.h:156
@ M2K_ERR_SOCKET
Definition modemu2k.h:151
@ M2K_ERR_AT
Definition modemu2k.h:160
M2K_API m2k_err_t m2k_write_from_app(m2k_t *ctx, const void *buf, size_t len, size_t *consumed)
Push bytes into the modem as if they had been read from the TTY.
M2K_API m2k_err_t m2k_setup_pty(m2k_t *ctx, const char **slave_out)
Allocate a PTY master and return the slave device path.
M2K_API m2k_err_t m2k_read_to_app(m2k_t *ctx, void *buf, size_t max, size_t *len_out)
Drain bytes from the modem that would normally have been written to the TTY.
M2K_API m2k_err_t m2k_setup_answer(m2k_t *ctx, const char *port)
Bind the line-side "answer" listener on port.
M2K_API int m2k_has_carrier(const m2k_t *ctx)
Test whether the modem has an active carrier (live TCP socket).
M2K_API m2k_err_t m2k_get_pollfds(m2k_t *ctx, struct pollfd *fds, size_t *nfds_inout, int *timeout_ms)
Describe the fds and timeout the caller's event loop should watch.
M2K_API const char * m2k_strerror(m2k_err_t err)
Return a human-readable string for err.
M2K_API void m2k_set_rts(m2k_t *ctx, int on)
Set the host's RTS (Request to Send) signal state.
M2K_API int m2k_get_answer_fd(const m2k_t *ctx)
Expose the answer listener's fd (after m2k_setup_answer()).
M2K_API int m2k_get_force_verbose(const m2k_t *ctx)
M2K_API m2k_err_t m2k_setup_listen(m2k_t *ctx, const char *port)
Bind a TCP listening socket on port.
M2K_API void m2k_set_dtr(m2k_t *ctx, int on)
Set the host's DTR (Data Terminal Ready) signal state.
M2K_API m2k_log_level_t m2k_get_log_level(const m2k_t *ctx)
#define M2K_API
Public-symbol visibility/export marker.
Definition modemu2k.h:76
M2K_API int m2k_get_listen_fd(const m2k_t *ctx)
Expose the listening socket's fd (after m2k_setup_listen, before m2k_listen_accept).