mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
Toward iOS: the engine's C API, an XCFramework build for Apple's systems, its Swift tests
The engine for a program in C or Swift (dmodem_engine.h): started on SIP or a peer line, its DTE a socket, the log, status, exit, peer line and speaker as callbacks; native-test's c_api_test drives it (AT commands, a dial handing the program its line, BUSY, the end). ios/build-native.sh builds the engine with D-Modem-RE, libre and Mbed TLS for iOS (devices, simulator) and macOS into DModemEngine.xcframework; ios/DModemSmoke's Swift tests run it on macOS through the C API. CI (apple.yml) does both on a macOS runner; android.yml's native job runs the engine's tests with Apple's fallbacks on Linux too. D-Modem-RE to its claude/apple-portability branch (portable.h: Apple's ways where Linux has its own), on top of the speaker hook. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
15 files changed
+623
-5
No files matched your search
@@ -78,6 +78,14 @@ jobs:
|
||||
- name: Engine test
|
||||
run: ctest --test-dir build-host --output-on-failure
|
||||
|
||||
# (Apple's ways on Linux: the portable fallbacks the iOS build has)
|
||||
- name: Engine test, Apple's fallbacks
|
||||
run: |
|
||||
cmake -S native-test -B build-fallback -G Ninja \
|
||||
-DCMAKE_CXX_FLAGS=-DDMODEM_PORTABLE_FALLBACKS -DCMAKE_C_FLAGS=-DDMODEM_PORTABLE_FALLBACKS
|
||||
cmake --build build-fallback
|
||||
ctest --test-dir build-fallback --output-on-failure
|
||||
|
||||
device:
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# The modem for Apple's systems: the engine with D-Modem-RE, libre and
|
||||
# Mbed TLS built for iOS (devices and simulator) and macOS as an
|
||||
# XCFramework (ios/build-native.sh), and its Swift tests run on macOS
|
||||
# (ios/DModemSmoke): the modem through its C API, as the iOS app has it.
|
||||
#
|
||||
# D-Modem-RE is a private submodule: the DMODEM_RE_TOKEN secret (as
|
||||
# android.yml's).
|
||||
name: Apple
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
engine:
|
||||
runs-on: macos-15
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
token: ${{ secrets.DMODEM_RE_TOKEN || github.token }}
|
||||
|
||||
- name: Ninja
|
||||
run: brew install ninja
|
||||
|
||||
- name: Build the XCFramework
|
||||
run: ios/build-native.sh
|
||||
|
||||
- name: Swift tests (macOS)
|
||||
run: swift test --package-path ios/DModemSmoke
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: DModemEngine.xcframework
|
||||
path: build-apple/DModemEngine.xcframework
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: failure()
|
||||
with:
|
||||
name: build-logs
|
||||
path: build-apple/**/_deps/*.log
|
||||
@@ -10,3 +10,5 @@ captures/
|
||||
# host builds of native-test
|
||||
/build-host*/
|
||||
.kotlin/
|
||||
/build-apple/
|
||||
/build-fallback/
|
||||
@@ -116,6 +116,21 @@ The host test needs OpenSSL's headers (libssl-dev):
|
||||
CI (`.github/workflows/android.yml`) runs all three; it needs a
|
||||
`DMODEM_RE_TOKEN` secret that can read the private D-Modem-RE submodule.
|
||||
|
||||
## Apple's systems (the iOS app to come)
|
||||
|
||||
The native modem builds for iOS and macOS too: the engine's C API,
|
||||
`app/src/main/cpp/dmodem_engine.h` (what Swift calls in place of
|
||||
Android's JNI), with D-Modem-RE, libre and Mbed TLS as one XCFramework:
|
||||
|
||||
ios/build-native.sh # macOS with Xcode, CMake, Ninja
|
||||
swift test --package-path ios/DModemSmoke
|
||||
|
||||
D-Modem-RE's `portable.h` has Apple's ways where Linux has its own
|
||||
(close-on-exec sockets, SIGPIPE, a monotonic sleep, a peer's hang-up);
|
||||
the Linux build tests them too with `-DDMODEM_PORTABLE_FALLBACKS`. CI
|
||||
(`.github/workflows/apple.yml`) builds the XCFramework and runs the Swift
|
||||
tests on macOS.
|
||||
|
||||
## License
|
||||
|
||||
GNU Affero General Public License version 3 only ([COPYING](COPYING)), as
|
||||
|
||||
@@ -27,7 +27,7 @@ set(DMODEM_ARI OFF CACHE BOOL "" FORCE)
|
||||
add_subdirectory(${DMODEM_RE_DIR} d-modem-re)
|
||||
|
||||
# the engine on its own (the host's tests build it too)
|
||||
add_library(dmodem_engine STATIC engine.cpp peer_modem.cpp)
|
||||
add_library(dmodem_engine STATIC engine.cpp peer_modem.cpp dmodem_engine.cpp)
|
||||
target_include_directories(dmodem_engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(dmodem_engine PUBLIC dmodem_sip)
|
||||
target_compile_options(dmodem_engine PRIVATE -Wall -Wextra)
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*
|
||||
* dmodem_engine.cpp - the engine for C (dmodem_engine.h)
|
||||
*
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include "dmodem_engine.h"
|
||||
#include "engine.h"
|
||||
|
||||
using dmodem_android::Config;
|
||||
using dmodem_android::Engine;
|
||||
using dmodem_android::Status;
|
||||
|
||||
namespace {
|
||||
|
||||
std::mutex mu;
|
||||
dmodem_callbacks cbs;
|
||||
|
||||
dmodem_callbacks callbacks()
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu);
|
||||
return cbs;
|
||||
}
|
||||
|
||||
const char *str(const char *s)
|
||||
{
|
||||
return s ? s : "";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" void dmodem_config_init(dmodem_config *c)
|
||||
{
|
||||
*c = dmodem_config();
|
||||
c->sip_port = 5060;
|
||||
c->t38 = 1;
|
||||
c->sip_log_level = 1;
|
||||
c->peer_lead_ms = 100;
|
||||
}
|
||||
|
||||
extern "C" void dmodem_set_callbacks(const dmodem_callbacks *cb)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu);
|
||||
cbs = cb ? *cb : dmodem_callbacks();
|
||||
}
|
||||
dmodem_callbacks c = callbacks();
|
||||
Engine &e = Engine::instance();
|
||||
if (c.log)
|
||||
e.set_log([c](const char *line) { c.log(c.ctx, line); });
|
||||
else
|
||||
e.set_log(nullptr);
|
||||
e.set_speaker(
|
||||
c.speaker ? Engine::SpeakerFn([c](int level) {
|
||||
c.speaker(c.ctx, level);
|
||||
}) : nullptr,
|
||||
c.sound ? Engine::SoundFn([c](const int16_t *pcm, int n) {
|
||||
c.sound(c.ctx, pcm, n);
|
||||
}) : nullptr);
|
||||
}
|
||||
|
||||
extern "C" int dmodem_start(const dmodem_config *c)
|
||||
{
|
||||
Config cfg;
|
||||
dmodem_callbacks cb = callbacks();
|
||||
|
||||
cfg.user = str(c->user);
|
||||
cfg.password = str(c->password);
|
||||
cfg.domain = str(c->domain);
|
||||
cfg.sip_port = c->sip_port;
|
||||
cfg.listen = c->listen;
|
||||
cfg.t38 = c->t38;
|
||||
cfg.dns = str(c->dns);
|
||||
cfg.country = str(c->country);
|
||||
cfg.data_file = str(c->data_file);
|
||||
cfg.log_level = c->log_level;
|
||||
cfg.sip_log_level = c->sip_log_level;
|
||||
cfg.peer = c->peer;
|
||||
cfg.peer_lead_ms = c->peer_lead_ms;
|
||||
return Engine::instance().start(cfg,
|
||||
[cb](const Status &s) {
|
||||
if (cb.status)
|
||||
cb.status(cb.ctx, (int)s.link, s.offhook,
|
||||
s.tx_rate, s.rx_rate);
|
||||
},
|
||||
[cb](int r) {
|
||||
if (cb.exited)
|
||||
cb.exited(cb.ctx, r);
|
||||
},
|
||||
[cb](bool caller, const char *dial, int fd) {
|
||||
if (cb.line)
|
||||
cb.line(cb.ctx, caller, dial, fd);
|
||||
else
|
||||
close(fd);
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" void dmodem_stop(void)
|
||||
{
|
||||
Engine::instance().stop();
|
||||
}
|
||||
|
||||
extern "C" int dmodem_running(void)
|
||||
{
|
||||
return Engine::instance().running();
|
||||
}
|
||||
|
||||
extern "C" void dmodem_peer_event(const char *line)
|
||||
{
|
||||
Engine::instance().peer_event(str(line));
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*
|
||||
* dmodem_engine.h - the app's modem (engine.h) for a program in C, or
|
||||
* one that calls C (Swift, on iOS): the modem on a
|
||||
* SIP line, or on a line to another phone through
|
||||
* the program (peer_modem.h), in a thread of its
|
||||
* own, its DTE a socket
|
||||
*
|
||||
* One modem in the process. The callbacks come on the modem's and
|
||||
* SIP's threads.
|
||||
*
|
||||
*/
|
||||
#ifndef DMODEM_ENGINE_H
|
||||
#define DMODEM_ENGINE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct dmodem_config {
|
||||
/* the SIP account: user, password, the registrar's or proxy's
|
||||
host[:port]; the local SIP port (0: 5060) */
|
||||
const char *user, *password, *domain;
|
||||
int sip_port;
|
||||
/* register and take calls in (or, peer: take the other phone's) */
|
||||
int listen;
|
||||
/* fax calls may go over to T.38 */
|
||||
int t38;
|
||||
/* DNS servers for SRV and NAPTR, apart by commas (NULL: none) */
|
||||
const char *dns;
|
||||
/* the modem's country by name (NULL or "": USA) */
|
||||
const char *country;
|
||||
/* where the modem keeps what it learns of the line (NULL: nowhere) */
|
||||
const char *data_file;
|
||||
/* the modem's log level (0: errors), SIP's (0 none .. 4) */
|
||||
int log_level, sip_log_level;
|
||||
/* the line another phone's through the program (line callback),
|
||||
not SIP; its lead silence (ms) */
|
||||
int peer, peer_lead_ms;
|
||||
} dmodem_config;
|
||||
|
||||
/* the link's states (status) */
|
||||
enum { DMODEM_IDLE, DMODEM_CONNECTING, DMODEM_ONLINE, DMODEM_HANGING_UP };
|
||||
|
||||
typedef struct dmodem_callbacks {
|
||||
void *ctx;
|
||||
/* a line of the log */
|
||||
void (*log)(void *ctx, const char *line);
|
||||
/* the call's state as it changes */
|
||||
void (*status)(void *ctx, int link, int offhook, unsigned tx_rate,
|
||||
unsigned rx_rate);
|
||||
/* the modem stopped: 0 by dmodem_stop() or the DTE closed, -1 on
|
||||
an error */
|
||||
void (*exited)(void *ctx, int result);
|
||||
/* a peer line's modem off hook: dialling (caller) dial, or
|
||||
answering; fd its samples (16 bit, 8000 Hz), the program's to
|
||||
close */
|
||||
void (*line)(void *ctx, int caller, const char *dial, int fd);
|
||||
/* the modem's speaker (ATM, ATL): 0 off, 1..3; while on, the
|
||||
line's sound */
|
||||
void (*speaker)(void *ctx, int level);
|
||||
void (*sound)(void *ctx, const int16_t *pcm, int n);
|
||||
} dmodem_callbacks;
|
||||
|
||||
/* *c with the defaults (SIP port 5060, T.38 on, SIP's log level 1,
|
||||
lead 100 ms) */
|
||||
void dmodem_config_init(dmodem_config *c);
|
||||
|
||||
/* the callbacks (copied), before dmodem_start() */
|
||||
void dmodem_set_callbacks(const dmodem_callbacks *cb);
|
||||
|
||||
/* the modem started: the program's end of its DTE (the program's to
|
||||
close: closing it stops the modem), or -1 if it runs already or
|
||||
can't start */
|
||||
int dmodem_start(const dmodem_config *c);
|
||||
|
||||
/* the modem stopped (its call hung up, SIP gone); waits for it */
|
||||
void dmodem_stop(void);
|
||||
|
||||
/* the modem runs */
|
||||
int dmodem_running(void);
|
||||
|
||||
/* a peer line's event to the modem: RING, CANCEL, ANSWERED, FAILED
|
||||
<SIP status>, DROPPED */
|
||||
void dmodem_peer_event(const char *line);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <dcore.h>
|
||||
#include <dsplibs.h>
|
||||
#include <log.h>
|
||||
#include <portable.h>
|
||||
#include <sip.h>
|
||||
#include <sip_modem.h>
|
||||
|
||||
@@ -85,7 +86,7 @@ int Engine::start(const Config &cfg, StatusFn status, ExitFn done,
|
||||
/* (it ended by itself) */
|
||||
thread_.join();
|
||||
}
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0)
|
||||
if (dmd::socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0)
|
||||
return -1;
|
||||
active_ = true;
|
||||
stopping_ = false;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <log.h>
|
||||
#include <portable.h>
|
||||
#include <run_loop.h>
|
||||
|
||||
#include "peer_modem.h"
|
||||
@@ -57,7 +58,7 @@ bool send_all(int fd, const void *buf, size_t n)
|
||||
{
|
||||
const char *p = static_cast<const char *>(buf);
|
||||
while (n > 0) {
|
||||
ssize_t w = send(fd, p, n, MSG_NOSIGNAL);
|
||||
ssize_t w = send(fd, p, n, dmd::SEND_NOSIGNAL);
|
||||
if (w < 0 && errno == EINTR)
|
||||
continue;
|
||||
if (w <= 0)
|
||||
@@ -138,7 +139,7 @@ int PeerLine::start(dcore::Modem &m)
|
||||
ERR("peer: no line\n");
|
||||
return -1;
|
||||
}
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
|
||||
if (dmd::socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
|
||||
ERR("socketpair: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
Vendored
+1
-1
Submodule external/d-modem-re updated: 5bba5df0b9...ed4156e6bd.
@@ -0,0 +1,20 @@
|
||||
// swift-tools-version:5.9
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
//
|
||||
// The modem from Swift: the engine's XCFramework (ios/build-native.sh)
|
||||
// and tests that run it on macOS through its C API, as the iOS app will.
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "DModemSmoke",
|
||||
platforms: [.macOS(.v12), .iOS(.v15)],
|
||||
targets: [
|
||||
.binaryTarget(name: "DModemEngine", path: "../../build-apple/DModemEngine.xcframework"),
|
||||
.testTarget(
|
||||
name: "DModemSmokeTests",
|
||||
dependencies: ["DModemEngine"],
|
||||
linkerSettings: [.linkedLibrary("c++")]
|
||||
),
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,94 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
import DModemEngine
|
||||
import Foundation
|
||||
import XCTest
|
||||
|
||||
/// The modem through its C API from Swift, on an Apple system: started on
|
||||
/// a peer line, AT commands answered, a dial handing its line to Swift,
|
||||
/// BUSY from Swift, the modem ended by closing its DTE.
|
||||
final class EngineTests: XCTestCase {
|
||||
/// what the callbacks saw (from the modem's threads)
|
||||
final class Seen {
|
||||
let lock = NSLock()
|
||||
var lineFd: Int32 = -1
|
||||
var dial = ""
|
||||
var exits = 0
|
||||
var speakerOn = false
|
||||
}
|
||||
|
||||
/// text from fd until it holds want, or secs
|
||||
func expect(_ fd: Int32, _ want: String, secs: Double = 5) -> Bool {
|
||||
var text = ""
|
||||
let end = Date().addingTimeInterval(secs)
|
||||
var buf = [UInt8](repeating: 0, count: 4096)
|
||||
while Date() < end {
|
||||
var p = pollfd(fd: fd, events: Int16(POLLIN), revents: 0)
|
||||
if poll(&p, 1, 100) > 0 {
|
||||
let n = read(fd, &buf, buf.count)
|
||||
if n <= 0 { break }
|
||||
text += String(decoding: buf[0..<n], as: UTF8.self)
|
||||
if text.contains(want) { return true }
|
||||
}
|
||||
}
|
||||
print("waited for \(want), got \(text)")
|
||||
return false
|
||||
}
|
||||
|
||||
func put(_ fd: Int32, _ s: String) {
|
||||
_ = s.withCString { write(fd, $0, strlen($0)) }
|
||||
}
|
||||
|
||||
func testThePeerLine() throws {
|
||||
signal(SIGPIPE, SIG_IGN)
|
||||
let seen = Seen()
|
||||
var cb = dmodem_callbacks()
|
||||
cb.ctx = Unmanaged.passRetained(seen).toOpaque()
|
||||
cb.line = { ctx, caller, dial, fd in
|
||||
let s = Unmanaged<Seen>.fromOpaque(ctx!).takeUnretainedValue()
|
||||
s.lock.lock(); s.lineFd = fd; s.dial = String(cString: dial!); s.lock.unlock()
|
||||
}
|
||||
cb.exited = { ctx, _ in
|
||||
let s = Unmanaged<Seen>.fromOpaque(ctx!).takeUnretainedValue()
|
||||
s.lock.lock(); s.exits += 1; s.lock.unlock()
|
||||
}
|
||||
cb.speaker = { ctx, level in
|
||||
let s = Unmanaged<Seen>.fromOpaque(ctx!).takeUnretainedValue()
|
||||
if level > 0 { s.lock.lock(); s.speakerOn = true; s.lock.unlock() }
|
||||
}
|
||||
dmodem_set_callbacks(&cb)
|
||||
|
||||
var c = dmodem_config()
|
||||
dmodem_config_init(&c)
|
||||
c.peer = 1
|
||||
let dte = dmodem_start(&c)
|
||||
XCTAssertGreaterThanOrEqual(dte, 0)
|
||||
XCTAssertEqual(dmodem_running(), 1)
|
||||
|
||||
put(dte, "ATE0\r")
|
||||
XCTAssertTrue(expect(dte, "OK"))
|
||||
put(dte, "ATI3\r")
|
||||
XCTAssertTrue(expect(dte, "peer"))
|
||||
|
||||
// a dial: its line to us; we say busy
|
||||
put(dte, "ATX3D5551234\r")
|
||||
var fd: Int32 = -1
|
||||
for _ in 0..<50 where fd < 0 {
|
||||
usleep(100_000)
|
||||
seen.lock.lock(); fd = seen.lineFd; seen.lock.unlock()
|
||||
}
|
||||
XCTAssertGreaterThanOrEqual(fd, 0)
|
||||
XCTAssertTrue(seen.dial.contains("5551234"))
|
||||
XCTAssertTrue(seen.speakerOn)
|
||||
dmodem_peer_event("FAILED 486")
|
||||
close(fd)
|
||||
XCTAssertTrue(expect(dte, "BUSY", secs: 10))
|
||||
|
||||
// the DTE closed: the modem ends
|
||||
close(dte)
|
||||
for _ in 0..<50 where dmodem_running() != 0 { usleep(100_000) }
|
||||
XCTAssertEqual(dmodem_running(), 0)
|
||||
seen.lock.lock(); XCTAssertEqual(seen.exits, 1); seen.lock.unlock()
|
||||
dmodem_stop()
|
||||
}
|
||||
}
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/bin/sh
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# Copyright (C) 2026 Neo-Desktop
|
||||
#
|
||||
# The modem for Apple's systems: the app's engine (app/src/main/cpp) with
|
||||
# D-Modem-RE, libre and Mbed TLS, built for iOS (devices), the iOS
|
||||
# simulator and macOS, each as one static library, together as
|
||||
# build-apple/DModemEngine.xcframework (its header dmodem_engine.h, its
|
||||
# module DModemEngine: Swift imports it).
|
||||
#
|
||||
# ios/build-native.sh (macOS with Xcode, CMake and Ninja)
|
||||
#
|
||||
# DEPLOYMENT: the oldest iOS (15.0) and macOS (12.0) it runs on.
|
||||
set -eu
|
||||
|
||||
ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
||||
OUT=$ROOT/build-apple
|
||||
IOS_MIN=${IOS_MIN:-15.0}
|
||||
MACOS_MIN=${MACOS_MIN:-12.0}
|
||||
mkdir -p "$OUT"
|
||||
|
||||
# build <name> <system> <sdk> <archs> <min>: the engine's static libraries
|
||||
# merged into $OUT/<name>/libDModemEngine.a
|
||||
build() {
|
||||
name=$1 system=$2 sdk=$3 archs=$4 min=$5
|
||||
dir=$OUT/$name
|
||||
echo "== $name ($sdk, $archs)"
|
||||
set -- -S "$ROOT/app/src/main/cpp" -B "$dir/build" -G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_OSX_SYSROOT="$sdk" \
|
||||
-DCMAKE_OSX_ARCHITECTURES="$archs" \
|
||||
-DCMAKE_OSX_DEPLOYMENT_TARGET="$min" \
|
||||
-DDMODEM_CRYPTO=mbedtls
|
||||
if [ "$system" = iOS ]; then
|
||||
set -- "$@" -DCMAKE_SYSTEM_NAME=iOS
|
||||
fi
|
||||
cmake "$@"
|
||||
cmake --build "$dir/build" --target dmodem_engine
|
||||
# the engine, the modem (libdmodem_sip) and what it needs (libre,
|
||||
# Mbed TLS's crypto): one library
|
||||
libs="$(find "$dir/build" -name libdmodem_engine.a -o \
|
||||
-name libdmodem_sip.a | sort) \
|
||||
$(find "$dir/build" -path '*libre-install/lib/*' \( \
|
||||
-name libre.a -o -name libmbedcrypto.a \) | sort)"
|
||||
echo "$libs"
|
||||
libtool -static -o "$dir/libDModemEngine.a" $libs
|
||||
}
|
||||
|
||||
build ios iOS iphoneos arm64 "$IOS_MIN"
|
||||
build ios-simulator iOS iphonesimulator "arm64;x86_64" "$IOS_MIN"
|
||||
build macos Darwin macosx "arm64;x86_64" "$MACOS_MIN"
|
||||
|
||||
# the header and its module
|
||||
inc=$OUT/include
|
||||
rm -rf "$inc"
|
||||
mkdir -p "$inc"
|
||||
cp "$ROOT/app/src/main/cpp/dmodem_engine.h" "$inc/"
|
||||
cat > "$inc/module.modulemap" <<MAP
|
||||
module DModemEngine {
|
||||
header "dmodem_engine.h"
|
||||
link "c++"
|
||||
export *
|
||||
}
|
||||
MAP
|
||||
|
||||
rm -rf "$OUT/DModemEngine.xcframework"
|
||||
xcodebuild -create-xcframework \
|
||||
-library "$OUT/ios/libDModemEngine.a" -headers "$inc" \
|
||||
-library "$OUT/ios-simulator/libDModemEngine.a" -headers "$inc" \
|
||||
-library "$OUT/macos/libDModemEngine.a" -headers "$inc" \
|
||||
-output "$OUT/DModemEngine.xcframework"
|
||||
echo "built $OUT/DModemEngine.xcframework"
|
||||
@@ -53,3 +53,12 @@ if(AMRNB_LIB)
|
||||
target_compile_definitions(codec_line PRIVATE HAVE_AMRNB)
|
||||
target_link_libraries(codec_line PRIVATE ${AMRNB_LIB})
|
||||
endif()
|
||||
|
||||
# c_api_test: the engine through its C API (dmodem_engine.h), as iOS's
|
||||
# Swift has it
|
||||
add_executable(c_api_test c_api_test.c)
|
||||
target_link_libraries(c_api_test PRIVATE dmodem_engine Threads::Threads)
|
||||
target_compile_options(c_api_test PRIVATE -Wall -Wextra)
|
||||
set_target_properties(c_api_test PROPERTIES LINKER_LANGUAGE CXX)
|
||||
add_test(NAME c_api COMMAND c_api_test)
|
||||
set_tests_properties(c_api PROPERTIES TIMEOUT 120)
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*
|
||||
* c_api_test.c - the engine through its C API (dmodem_engine.h), as a
|
||||
* program in C or Swift (iOS) has it: on a peer line,
|
||||
* AT commands answered, a dial handing the program
|
||||
* its line, the program's BUSY, the status and the
|
||||
* speaker reported, the modem ended by closing its
|
||||
* DTE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dmodem_engine.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond, ...) do { \
|
||||
if (cond) { printf("ok: " __VA_ARGS__); printf("\n"); } \
|
||||
else { printf("FAIL (%s:%d): ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); printf("\n"); failures++; } } while (0)
|
||||
|
||||
static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
|
||||
static int line_fd = -1, line_caller = -1, exits = 0, exit_result = 99;
|
||||
static int statuses, speaker_on, logs;
|
||||
static char line_dial[64];
|
||||
|
||||
static void on_log(void *ctx, const char *l) { (void)ctx; (void)l; __sync_fetch_and_add(&logs, 1); }
|
||||
static void on_status(void *ctx, int link, int offhook, unsigned tx, unsigned rx)
|
||||
{
|
||||
(void)ctx; (void)link; (void)offhook; (void)tx; (void)rx;
|
||||
__sync_fetch_and_add(&statuses, 1);
|
||||
}
|
||||
static void on_exit_(void *ctx, int r)
|
||||
{
|
||||
(void)ctx;
|
||||
pthread_mutex_lock(&mu);
|
||||
exits++;
|
||||
exit_result = r;
|
||||
pthread_mutex_unlock(&mu);
|
||||
}
|
||||
static void on_line(void *ctx, int caller, const char *dial, int fd)
|
||||
{
|
||||
(void)ctx;
|
||||
pthread_mutex_lock(&mu);
|
||||
line_fd = fd;
|
||||
line_caller = caller;
|
||||
snprintf(line_dial, sizeof(line_dial), "%s", dial);
|
||||
pthread_mutex_unlock(&mu);
|
||||
}
|
||||
static void on_speaker(void *ctx, int level) { (void)ctx; if (level > 0) speaker_on = 1; }
|
||||
|
||||
/* text from fd until want (or secs) */
|
||||
static int expect(int fd, const char *want, int secs)
|
||||
{
|
||||
char buf[4096];
|
||||
size_t have = 0;
|
||||
for (int i = 0; i < secs * 10; i++) {
|
||||
struct pollfd p = { fd, POLLIN, 0 };
|
||||
if (poll(&p, 1, 100) > 0) {
|
||||
ssize_t n = read(fd, buf + have, sizeof(buf) - 1 - have);
|
||||
if (n <= 0)
|
||||
break;
|
||||
have += (size_t)n;
|
||||
buf[have] = 0;
|
||||
if (strstr(buf, want))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
buf[have] = 0;
|
||||
printf(" (waited for \"%s\", got \"%s\")\n", want, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void put(int fd, const char *s)
|
||||
{
|
||||
if (write(fd, s, strlen(s)) < 0)
|
||||
perror("write");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
dmodem_config c;
|
||||
dmodem_callbacks cb = { 0 };
|
||||
int dte, fd = -1;
|
||||
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
cb.log = on_log;
|
||||
cb.status = on_status;
|
||||
cb.exited = on_exit_;
|
||||
cb.line = on_line;
|
||||
cb.speaker = on_speaker;
|
||||
dmodem_set_callbacks(&cb);
|
||||
dmodem_config_init(&c);
|
||||
CHECK(c.sip_port == 5060 && c.t38 && c.peer_lead_ms == 100, "the defaults");
|
||||
c.peer = 1;
|
||||
c.log_level = 1;
|
||||
dte = dmodem_start(&c);
|
||||
CHECK(dte >= 0 && dmodem_running(), "started on a peer line");
|
||||
CHECK(dmodem_start(&c) < 0, "a second start refused");
|
||||
|
||||
put(dte, "ATE0\r");
|
||||
CHECK(expect(dte, "OK", 5), "ATE0: OK");
|
||||
put(dte, "ATI3\r");
|
||||
CHECK(expect(dte, "peer", 5), "ATI3: the peer line's driver");
|
||||
|
||||
/* a dial: the line handed over; the program says BUSY */
|
||||
put(dte, "ATX3D5551234\r");
|
||||
for (int i = 0; i < 50 && fd < 0; i++) {
|
||||
usleep(100000);
|
||||
pthread_mutex_lock(&mu);
|
||||
fd = line_fd;
|
||||
pthread_mutex_unlock(&mu);
|
||||
}
|
||||
CHECK(fd >= 0 && line_caller == 1 && strstr(line_dial, "5551234"),
|
||||
"ATD: the line to the program (dialling %s)", line_dial);
|
||||
CHECK(speaker_on, "the speaker on as it dials (ATM1)");
|
||||
dmodem_peer_event("FAILED 486");
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
CHECK(expect(dte, "BUSY", 10), "the program's FAILED 486: BUSY");
|
||||
CHECK(statuses > 0 && logs > 0, "status (%d) and log (%d) reported", statuses, logs);
|
||||
|
||||
/* the DTE closed: the modem ends */
|
||||
close(dte);
|
||||
for (int i = 0; i < 50 && dmodem_running(); i++)
|
||||
usleep(100000);
|
||||
pthread_mutex_lock(&mu);
|
||||
CHECK(!dmodem_running() && exits == 1 && exit_result == 0,
|
||||
"the DTE closed: the modem ended (%d)", exit_result);
|
||||
pthread_mutex_unlock(&mu);
|
||||
dmodem_stop();
|
||||
printf(failures ? "FAIL: %d\n" : "PASS\n", failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Reference in new issue
Block a user