Files
app/native-test/usb_host.cpp
T
Claude d353ae2b02 USB modems as the modem's line: SmartUSB56 and pico-serial Line
A new line, "A phone line, through a USB modem": the phone runs the
modem, a Smart Link SmartUSB56 (0483:7554) or a pico-serial Line stick
(1209:0002) plugged into it carries its samples to a real phone line,
with D-Modem-RE's usb_line.cpp as dmodemd --usb drives them (the
stick's caller ID and the country's DAA settings too).

- libusb 1.0.27 (D-Modem-RE's pinned release) built for Android
  (usb/libusb.cmake); usb_line.cpp built with libusb_init,
  libusb_get_device_list and libusb_open renamed to usb_android.cpp's,
  which start libusb without device discovery and give it the
  descriptor Android opened for the app, wrapped, as the only device.
- usb_modem.cpp: the core on that line, the run loop of dmodemd's
  run_line() with a stop pipe and the engine's status; Engine::Config
  usb_fd and usb_rate, through JNI.
- UsbModems: the devices by their IDs, Android's permission asked when
  the modem starts, the connection held while it runs; ModemSettings
  LINE_USB (no SIP account, not a peer line); the settings, Home and the
  notifications say so; Android offers the app when one is plugged in.
- native-test: usb_host (the engine on D-Modem-RE's fake stick, linked
  in as libusb) and usb_line_test.py: two of them through its simulated
  exchange: caller ID, CONNECT 33600, data each way, hang up, unplugged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
2026-10-22 19:16:22 +00:00

88 lines
2.1 KiB
C++

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 Neo-Desktop
*
* usb_host.cpp - the app's engine on a USB modem's line (engine.h,
* usb_modem.h) as a program, for usb_line_test.py: the
* device D-Modem-RE's fake pico-serial Line stick
* (test/fake_pico_line.cpp, linked in as libusb), its
* line the simulated exchange's at $FAKE_PICO_LINE_SOCKET
*
* The modem's DTE on stdin and stdout; on stderr the log, and
* STATUS <link> <offhook> <tx> <rx> and EXIT <result> lines.
*
* usb_host [rate]
*
*/
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <atomic>
#include "engine.h"
using namespace dmodem_android;
namespace {
std::atomic<bool> exited{ false };
/* what came on from to to, false at from's end (or to's) */
bool pass(int from, int to)
{
char buf[4096];
ssize_t n = read(from, buf, sizeof(buf));
if (n <= 0)
return false;
for (ssize_t o = 0; o < n;) {
ssize_t w = write(to, buf + o, n - o);
if (w <= 0)
return false;
o += w;
}
return true;
}
} // namespace
int main(int argc, char *argv[])
{
Config cfg;
Engine &e = Engine::instance();
signal(SIGPIPE, SIG_IGN);
setvbuf(stderr, nullptr, _IOLBF, 0);
cfg.usb_fd = 0; /* (the fake has its own: any will do) */
cfg.usb_rate = argc > 1 ? atoi(argv[1]) : 9600;
cfg.log_level = getenv("USB_LOG") ? atoi(getenv("USB_LOG")) : 1;
cfg.country = getenv("USB_COUNTRY") ? getenv("USB_COUNTRY") : "";
int dte = e.start(cfg,
[](const Status &s) {
fprintf(stderr, "STATUS %d %d %u %u\n", (int)s.link,
s.offhook ? 1 : 0, s.tx_rate, s.rx_rate);
},
[](int result) {
fprintf(stderr, "EXIT %d\n", result);
exited = true;
});
if (dte < 0) {
fprintf(stderr, "EXIT -1\n");
return 1;
}
struct pollfd p[2] = { { 0, POLLIN, 0 }, { dte, POLLIN, 0 } };
while (!exited) {
if (poll(p, 2, 200) < 0)
break;
if ((p[0].revents & (POLLIN | POLLHUP)) && !pass(0, dte))
break;
if ((p[1].revents & (POLLIN | POLLHUP)) && !pass(dte, 1))
break;
}
e.stop();
close(dte);
return 0;
}