diff --git a/README.md b/README.md index 6ec3e42..0152417 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,13 @@ carries the modem's sound (D-Modem-RE's `usb_line.cpp`, as with caller ID, its ST7554 or ST75951 DAA), or a [pico-serial](https://github.com/Neo-Desktop/pico-serial) Line stick (1209:0002, a Si3056 DAA: caller ID on hook, and the country's line -settings in its registers). The phone runs the modem; plugged into it +settings in its registers), or an Agere USB 2.0 soft modem (047E:2892, +047E:2828: 8000 Hz, caller ID; its ROM patched as it starts with +Agere's bulk patch). Agere's ROM patches come from D-Modem-RE's +`firmware/agere` into the APK's assets (`app/build.gradle.kts`'s +agereFirmware) and are installed in the app's files for D-Modem-RE +(`DMODEM_AGERE_FIRMWARE`); they are Agere's, under Agere's licence, not +the AGPL (its LICENSE and NOTICE.md go with them). The phone runs the modem; plugged into it (USB OTG) the stick is its line, with no SIP account or network. Settings, Line, "A phone line, through a USB modem"; Android asks once whether D-Modem may use it, and offers the app when one is plugged in. @@ -107,7 +113,7 @@ allowed and gives its descriptor. libusb is built for Android in the app renamed to `usb/usb_android.cpp`'s, which start libusb without its device discovery and give it that descriptor, wrapped, as the only device on the bus. Tested on the build host on D-Modem-RE's fake pico-serial Line -sticks and simulated exchange (`native-test/usb_line_test.py`: dialling, +sticks and Agere modems, and simulated exchange (`native-test/usb_line_test.py`, its `agere` run with the tests' own ROM patches: dialling, caller ID, answering, CONNECT 33600, data each way, hanging up, unplugged); not yet on a phone with a real stick. The phone's speaker isn't fed on this line. diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a81d67c..d226c0b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -34,6 +34,8 @@ android { getByName("main") { // proot, built for each ABI (below) jniLibs.directories.add(layout.buildDirectory.dir("proot/jniLibs").get().asFile.path) + // the Agere USB modem's ROM patches (D-Modem-RE's firmware/agere, below) + assets.directories.add(layout.buildDirectory.dir("generated/firmware-assets").get().asFile.path) } } @@ -159,3 +161,13 @@ for (abi in prootAbis) { buildProot.configure { dependsOn(t) } } tasks.named("preBuild") { dependsOn(buildProot) } + +// the Agere USB modem's ROM patches, as D-Modem-RE ships them (firmware/agere: Agere's, under Agere's +// licence, its LICENSE and NOTICE.md with them), into the APK's assets as firmware/agere; installed in +// the app's files when an Agere modem is the line (UsbModems) +val agereFirmware = tasks.register("agereFirmware") { + description = "Copies the Agere USB modem's ROM patches from D-Modem-RE into the assets" + from(rootProject.file("external/d-modem-re/firmware/agere")) + into(layout.buildDirectory.dir("generated/firmware-assets/firmware/agere")) +} +tasks.named("preBuild") { dependsOn(agereFirmware) } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index a10caf1..e567018 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -39,12 +39,15 @@ set_target_properties(dmodem_engine PROPERTIES POSITION_INDEPENDENT_CODE ON) if(ANDROID) # the USB modems (D-Modem-RE's usb_line.cpp: a SmartUSB56, a - # pico-serial Line stick) on the device Android opened for the app: + # pico-serial Line stick, an Agere USB 2.0 soft modem, usb_agere.cpp, + # its ROM patches from the app's assets, DMODEM_AGERE_FIRMWARE) on the + # device Android opened for the app: # libusb built here, and usb_line.cpp's three calls that look for the # device renamed to usb/usb_android.cpp's, which give it that one include(usb/libusb.cmake) target_sources(dmodem_engine PRIVATE usb/usb_modem.cpp usb/usb_android.cpp ${DMODEM_RE_DIR}/dmodemd/usb_line.cpp + ${DMODEM_RE_DIR}/dmodemd/usb_agere.cpp ${DMODEM_RE_DIR}/dmodemd/si3056.cpp) set_source_files_properties(${DMODEM_RE_DIR}/dmodemd/usb_line.cpp PROPERTIES COMPILE_DEFINITIONS diff --git a/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt b/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt index e694796..8091a56 100644 --- a/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt +++ b/app/src/main/java/com/neodesktop/dmodem/modem/ModemService.kt @@ -199,6 +199,10 @@ class ModemService : Service() { is UsbModems.Opened.Device -> { usbModem = o log("line: the ${o.kind.described}") + // (its ROM patches where D-Modem-RE looks for them) + if (o.kind == UsbModems.Kind.AGERE) { + runCatching { UsbModems.installAgereFirmware(this) }.onFailure { log("the Agere modem's patches: $it") } + } } } } @@ -240,7 +244,7 @@ class ModemService : Service() { sipLogLevel = settings.sipLogLevel, peer = settings.peer, usbFd = usbModem?.connection?.fileDescriptor ?: -1, - usbRate = UsbModems.RATE, + usbRate = usbModem?.kind?.rate ?: UsbModems.RATE, // (Bluetooth's way is slower: more room for the samples on it) peerLeadMs = when (settings.line) { ModemSettings.LINE_BLUETOOTH -> 200 diff --git a/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt b/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt index 426b14b..9843750 100644 --- a/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt +++ b/app/src/main/java/com/neodesktop/dmodem/modem/UsbModems.kt @@ -23,20 +23,23 @@ import kotlin.coroutines.resume * The app runs the modem itself: they carry its samples to the line. */ object UsbModems { - enum class Kind(val vendor: Int, val product: Int, val label: String) { - SMART_USB56(0x0483, 0x7554, "SmartUSB56"), - PICO_LINE(0x1209, 0x0002, "pico-serial Line"), + enum class Kind(val vendor: Int, val products: List, val label: String, val rate: Int) { + SMART_USB56(0x0483, listOf(0x7554), "SmartUSB56", 9600), + PICO_LINE(0x1209, listOf(0x0002), "pico-serial Line", 9600), + /** its ROM patched with Agere's bulk patch as it starts (D-Modem-RE ships them; [installAgereFirmware]) */ + AGERE(0x047E, listOf(0x2892, 0x2828), "Agere USB 2.0 modem", 8000), ; /** "SmartUSB56 (USB 0483:7554)" */ - val described: String get() = "$label (USB %04x:%04x)".format(vendor, product) + val described: String + get() = "$label (USB " + products.joinToString("/") { "%04x:%04x".format(vendor, it) } + ")" } - /** the modems' rate (Hz): both take 9600, the Si3056's default (dmodemd's too) */ + /** the modems' rate (Hz) but the Agere's: both take 9600, the Si3056's default (dmodemd's too); the Agere 8000 */ const val RATE = 9600 /** which of them a USB device with these IDs is, or null */ - fun kindOf(vendor: Int, product: Int): Kind? = Kind.entries.firstOrNull { it.vendor == vendor && it.product == product } + fun kindOf(vendor: Int, product: Int): Kind? = Kind.entries.firstOrNull { it.vendor == vendor && product in it.products } fun kindOf(d: UsbDevice): Kind? = kindOf(d.vendorId, d.productId) @@ -57,6 +60,22 @@ object UsbModems { class Failed(val why: String) : Opened } + /** + * The Agere modem's ROM patches (D-Modem-RE's firmware/agere: Agere's, under Agere's licence, in the + * APK's assets) in the app's files, and D-Modem-RE told where (DMODEM_AGERE_FIRMWARE): their directory + */ + fun installAgereFirmware(context: Context): java.io.File { + val dir = java.io.File(context.filesDir, "firmware/agere").apply { mkdirs() } + val assets = context.assets + for (name in assets.list("firmware/agere").orEmpty()) { + val out = java.io.File(dir, name) + val bytes = assets.open("firmware/agere/$name").use { it.readBytes() } + if (!out.exists() || !out.readBytes().contentEquals(bytes)) out.writeBytes(bytes) + } + android.system.Os.setenv("DMODEM_AGERE_FIRMWARE", dir.path, true) + return dir + } + private const val ACTION_PERMISSION = "com.neodesktop.dmodem.USB_MODEM_PERMISSION" /** @@ -66,7 +85,9 @@ object UsbModems { suspend fun open(context: Context, askMs: Long = 60_000): Opened { val m = context.getSystemService(UsbManager::class.java) ?: return Opened.Failed("this phone has no USB host") val (dev, kind) = plugged(context).firstOrNull() - ?: return Opened.Failed("no USB modem plugged in: a ${Kind.SMART_USB56.described} or a ${Kind.PICO_LINE.described}") + ?: return Opened.Failed( + "no USB modem plugged in: a ${Kind.SMART_USB56.described}, a ${Kind.PICO_LINE.described} or an ${Kind.AGERE.described}", + ) if (!m.hasPermission(dev)) { val allowed = withTimeoutOrNull(askMs) { ask(context, m, dev) } ?: false if (!allowed) return Opened.Failed("the ${kind.label} wasn't allowed: allow D-Modem to use it, then start the modem again") diff --git a/app/src/main/java/com/neodesktop/dmodem/ui/PeerLineUi.kt b/app/src/main/java/com/neodesktop/dmodem/ui/PeerLineUi.kt index a0c5258..cd69750 100644 --- a/app/src/main/java/com/neodesktop/dmodem/ui/PeerLineUi.kt +++ b/app/src/main/java/com/neodesktop/dmodem/ui/PeerLineUi.kt @@ -102,8 +102,9 @@ fun UsbLineFields() { val plugged = remember { com.neodesktop.dmodem.modem.UsbModems.describe(context) } Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { Text( - "The modem dials and answers a real phone line through a USB modem: a Smart Link SmartUSB56, or a " + - "pico-serial Line stick, both with caller ID (the stick takes the country's line settings too). The phone runs the modem; " + + "The modem dials and answers a real phone line through a USB modem: a Smart Link SmartUSB56, a " + + "pico-serial Line stick (with the country's line settings) or an Agere USB 2.0 soft modem (Agere's ROM patches " + + "come with the app), all with caller ID. The phone runs the modem; " + "the USB one is its line. No SIP account or network needed. Android asks once whether D-Modem may use it.", style = MaterialTheme.typography.bodySmall, ) diff --git a/app/src/main/res/xml/usb_serial_adapters.xml b/app/src/main/res/xml/usb_serial_adapters.xml index 6a0f4ba..53858bc 100644 --- a/app/src/main/res/xml/usb_serial_adapters.xml +++ b/app/src/main/res/xml/usb_serial_adapters.xml @@ -9,9 +9,12 @@ - + + + + diff --git a/app/src/test/java/com/neodesktop/dmodem/modem/AgereFirmwareTest.kt b/app/src/test/java/com/neodesktop/dmodem/modem/AgereFirmwareTest.kt new file mode 100644 index 0000000..defc8bf --- /dev/null +++ b/app/src/test/java/com/neodesktop/dmodem/modem/AgereFirmwareTest.kt @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package com.neodesktop.dmodem.modem + +import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File +import java.security.MessageDigest + +/** + * The Agere USB modem's ROM patches: in the APK's assets as D-Modem-RE + * ships them, installed in the app's files byte for byte (their SHA-256 + * sums D-Modem-RE's), with Agere's licence beside them. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class AgereFirmwareTest { + @Test + fun installedAsShipped() { + val ctx = ApplicationProvider.getApplicationContext() + val dir = UsbModems.installAgereFirmware(ctx) + val sums = File(dir, "SHA256SUMS").readLines().filter { it.isNotBlank() }.associate { l -> + val (sum, name) = l.trim().split(Regex("\\s+"), limit = 2) + name.removePrefix("*") to sum + } + assertEquals(4, sums.size) + for ((name, sum) in sums) { + val f = File(dir, name) + assertTrue(name, f.isFile) + val got = MessageDigest.getInstance("SHA-256").digest(f.readBytes()).joinToString("") { "%02x".format(it) } + assertEquals(name, sum, got) + } + assertTrue(File(dir, "LICENSE").isFile) + assertTrue(File(dir, "NOTICE.md").isFile) + // (again: nothing changes) + assertEquals(dir, UsbModems.installAgereFirmware(ctx)) + } +} diff --git a/app/src/test/java/com/neodesktop/dmodem/modem/UsbModemsTest.kt b/app/src/test/java/com/neodesktop/dmodem/modem/UsbModemsTest.kt index 300d150..bb2998a 100644 --- a/app/src/test/java/com/neodesktop/dmodem/modem/UsbModemsTest.kt +++ b/app/src/test/java/com/neodesktop/dmodem/modem/UsbModemsTest.kt @@ -12,6 +12,9 @@ class UsbModemsTest { fun theTwoModemsByTheirIds() { assertEquals(UsbModems.Kind.SMART_USB56, UsbModems.kindOf(0x0483, 0x7554)) assertEquals(UsbModems.Kind.PICO_LINE, UsbModems.kindOf(0x1209, 0x0002)) + assertEquals(UsbModems.Kind.AGERE, UsbModems.kindOf(0x047E, 0x2892)) + assertEquals(UsbModems.Kind.AGERE, UsbModems.kindOf(0x047E, 0x2828)) + assertNull(UsbModems.kindOf(0x047E, 0x1234)) // (the pico-serial board, an STM32's other devices: not modems) assertNull(UsbModems.kindOf(0x1209, 0x0001)) assertNull(UsbModems.kindOf(0x0483, 0x5740)) @@ -21,5 +24,9 @@ class UsbModemsTest { fun describedWithTheirIds() { assertEquals("SmartUSB56 (USB 0483:7554)", UsbModems.Kind.SMART_USB56.described) assertEquals("pico-serial Line (USB 1209:0002)", UsbModems.Kind.PICO_LINE.described) + assertEquals("Agere USB 2.0 modem (USB 047e:2892/047e:2828)", UsbModems.Kind.AGERE.described) + // (the Agere runs at 8000 Hz, the others at 9600) + assertEquals(8000, UsbModems.Kind.AGERE.rate) + assertEquals(9600, UsbModems.Kind.PICO_LINE.rate) } } diff --git a/external/d-modem-re b/external/d-modem-re index 79e00fa..a30aa39 160000 --- a/external/d-modem-re +++ b/external/d-modem-re @@ -1 +1 @@ -Subproject commit 79e00fa2aaf8785e37bb7dc6f5c39b0427af1e75 +Subproject commit a30aa39dd589de4ed6c6574d596880114a711ad2 diff --git a/native-test/CMakeLists.txt b/native-test/CMakeLists.txt index ee61778..cbed38e 100644 --- a/native-test/CMakeLists.txt +++ b/native-test/CMakeLists.txt @@ -92,7 +92,7 @@ set(_app ${CMAKE_CURRENT_SOURCE_DIR}/../app/src/main/cpp) set(_dmd ${DMODEM_RE_DIR}/dmodemd) add_library(dmodem_engine_usb STATIC ${_app}/engine.cpp ${_app}/peer_modem.cpp ${_app}/dmodem_engine.cpp - ${_app}/usb/usb_modem.cpp ${_dmd}/usb_line.cpp ${_dmd}/si3056.cpp + ${_app}/usb/usb_modem.cpp ${_dmd}/usb_line.cpp ${_dmd}/usb_agere.cpp ${_dmd}/si3056.cpp usb_fake_device.cpp ${_dmd}/test/fake_pico_line.cpp) target_include_directories(dmodem_engine_usb PUBLIC ${_app}) target_include_directories(dmodem_engine_usb SYSTEM PUBLIC @@ -112,3 +112,25 @@ add_test(NAME usb_line COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/usb_line_test.py $ $) set_tests_properties(usb_line PROPERTIES TIMEOUT 300) + +# the same on D-Modem-RE's fake Agere USB 2.0 soft modems +# (test/fake_agere.cpp): their ROM patches the tests' own, in a firmware +# directory as the app installs Agere's (DMODEM_AGERE_FIRMWARE) +add_library(dmodem_engine_agere STATIC ${_app}/engine.cpp + ${_app}/peer_modem.cpp ${_app}/dmodem_engine.cpp + ${_app}/usb/usb_modem.cpp ${_dmd}/usb_line.cpp ${_dmd}/usb_agere.cpp + ${_dmd}/si3056.cpp usb_fake_device.cpp ${_dmd}/test/fake_agere.cpp) +target_include_directories(dmodem_engine_agere PUBLIC ${_app}) +target_include_directories(dmodem_engine_agere SYSTEM PUBLIC + ${DMODEM_LIBUSB_SRC}/libusb) +target_compile_definitions(dmodem_engine_agere PUBLIC DMODEM_ENGINE_USB) +target_link_libraries(dmodem_engine_agere PUBLIC dmodem_sip) + +add_executable(agere_host usb_host.cpp) +target_link_libraries(agere_host PRIVATE dmodem_engine_agere Threads::Threads) +target_compile_options(agere_host PRIVATE -Wall -Wextra) + +add_test(NAME agere_line COMMAND python3 + ${CMAKE_CURRENT_SOURCE_DIR}/usb_line_test.py $ + $ agere) +set_tests_properties(agere_line PROPERTIES TIMEOUT 300) diff --git a/native-test/usb_fake_device.cpp b/native-test/usb_fake_device.cpp index 3291e87..31d99ae 100644 --- a/native-test/usb_fake_device.cpp +++ b/native-test/usb_fake_device.cpp @@ -15,12 +15,18 @@ void usb_use_fd(int) {} } // namespace dmodem_android -/* what the fake leaves to the real libusb (upstream's tests preload the +/* (weak: a fake that has its own wins) what the fake leaves to the real libusb (upstream's tests preload the fake in front of it; here it is linked in alone): the device's ports, for wake on ring's sysfs switch (UsbLine::arm_wake(), with set_wake() alone), unknown */ -extern "C" int LIBUSB_CALL libusb_get_port_numbers(libusb_device *, +extern "C" __attribute__((weak)) int LIBUSB_CALL libusb_get_port_numbers(libusb_device *, uint8_t *, int) { return LIBUSB_ERROR_NOT_SUPPORTED; } + +/* (the Agere modem's speed check: the fake stick isn't one) */ +extern "C" __attribute__((weak)) int LIBUSB_CALL libusb_get_device_speed(libusb_device *) +{ + return LIBUSB_SPEED_HIGH; +} diff --git a/native-test/usb_line_test.py b/native-test/usb_line_test.py index d92cc48..515e083 100644 --- a/native-test/usb_line_test.py +++ b/native-test/usb_line_test.py @@ -2,12 +2,15 @@ # SPDX-License-Identifier: AGPL-3.0-only # Copyright (C) 2026 Neo-Desktop # -# usb_line_test.py : the app's engine on a USB -# modem's line (usb_modem.h), two of them (usb_host) on D-Modem-RE's fake -# pico-serial Line sticks whose lines are the simulated exchange's, 1001 -# and 1002 (as D-Modem-RE's pico_line_test.py runs dmodemd --usb): +# usb_line_test.py [agere]: the app's engine on +# a USB modem's line (usb_modem.h), two of them (usb_host) on D-Modem-RE's +# fake pico-serial Line sticks (or, agere, its fake Agere USB 2.0 soft +# modems at 8000 Hz, their ROM patches the tests' own in a firmware +# directory, DMODEM_AGERE_FIRMWARE, as the app installs Agere's) whose +# lines are the simulated exchange's, 1001 and 1002 (as D-Modem-RE's +# pico_line_test.py and agere_test.py run dmodemd --usb): # -# - each says it's on a pico-serial Line (ATI3); +# - each says what it's on (ATI3); # - 1001 dials 1002 (ATX4: after the dial tone, in DTMF); 1002 rings, # shows the caller ID (AT#CID=1), answers at the second ring (ATS0=2); # both CONNECT, and the engine's status says online; a line of text @@ -17,8 +20,19 @@ import os, re, shutil, subprocess, sys, tempfile, threading, time host, exchange = sys.argv[1], sys.argv[2] -rate = "9600" +agere = len(sys.argv) > 3 and sys.argv[3] == "agere" +rate = "8000" if agere else "9600" tmp = tempfile.mkdtemp(prefix="dmodem-usb-line-") +device = b"Agere USB 2.0 \\(Mercury\\)" if agere else b"pico-serial Line \\(Si3056\\)" +firmware = os.path.join(tmp, "firmware") +if agere: + # the tests' patches (D-Modem-RE's agere_patch.h test_patch(), as its + # agere_test.py makes them), a file each, as Agere's are installed + os.makedirs(firmware) + LENS = (748, 748, 1156, 508) + for i, n in enumerate(LENS): + with open(os.path.join(firmware, "patch%d.bin" % i), "wb") as f: + f.write(bytes((k * 31 + i * 77 + 5 + (k >> 8)) & 0xff for k in range(n))) procs = [] events = [] @@ -35,7 +49,12 @@ class Modem: """the engine (usb_host) on line 's fake stick: its DTE, its log""" def __init__(self, n): self.n = n - env = dict(os.environ, FAKE_PICO_LINE_SOCKET=os.path.join(tmp, n + ".sock")) + sock = os.path.join(tmp, n + ".sock") + if agere: + env = dict(os.environ, FAKE_AGERE_SOCKET=sock, DMODEM_AGERE_FIRMWARE=firmware, + DMODEM_AGERE_TEST_PATCHES="1") + else: + env = dict(os.environ, FAKE_PICO_LINE_SOCKET=sock) self.p = subprocess.Popen([host, rate], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) procs.append(self.p) @@ -97,8 +116,8 @@ try: b = Modem("1002") for m in (a, b): check("%s ATE0" % m.n, m.cmd(b"ATE0", t=15)) - check("%s ATI3: pico-serial Line" % m.n, - m.cmd(b"ATI3", rb"pico-serial Line \(Si3056\)\r\n(\r\n)?OK\r\n")) + check("%s ATI3: %s" % (m.n, device.decode().replace("\\", "")), + m.cmd(b"ATI3", device + rb"\r\n(\r\n)?OK\r\n")) check("1002 answers at the 2nd ring", b.cmd(b"ATS0=2")) check("1002 caller ID on", b.cmd(b"AT#CID=1")) check("1001 waits for the dial tone", a.cmd(b"ATX4"))