USB modems: each transfer counted both ways, in the log; the first IN ones' heads

The Agere's samples don't come on the device (its clock stays at 0):
whether its IN transfers never come back, come back failed or come
back empty is now in the log, with the clock every 10 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
Claude committed 2026-10-26 04:50:35 +00:00
1 parent b040b4a6cb
commit b0119dbe5e
8 files changed
+162 -10

No files matched your search

+9 -3
View File
@@ -43,15 +43,21 @@ if(ANDROID)
# 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
# device renamed to usb/usb_android.cpp's, which give it that one;
# libusb_submit_transfer() renamed too, the transfers counted for the
# log (usb/usb_transfers.cpp)
include(usb/libusb.cmake)
target_sources(dmodem_engine PRIVATE usb/usb_modem.cpp
usb/usb_android.cpp ${DMODEM_RE_DIR}/dmodemd/usb_line.cpp
usb/usb_android.cpp usb/usb_transfers.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
"libusb_init=dmodem_libusb_init;libusb_get_device_list=dmodem_libusb_get_device_list;libusb_open=dmodem_libusb_open")
"libusb_init=dmodem_libusb_init;libusb_get_device_list=dmodem_libusb_get_device_list;libusb_open=dmodem_libusb_open;libusb_submit_transfer=dmodem_libusb_submit_transfer")
set_source_files_properties(${DMODEM_RE_DIR}/dmodemd/usb_agere.cpp
PROPERTIES COMPILE_DEFINITIONS
"libusb_submit_transfer=dmodem_libusb_submit_transfer")
target_compile_definitions(dmodem_engine PUBLIC DMODEM_ENGINE_USB)
target_link_libraries(dmodem_engine PUBLIC dmodem_android_libusb)
+3
View File
@@ -23,6 +23,8 @@ namespace dmodem_android {
void usb_use_fd(int fd)
{
fd_ = fd;
if (fd >= 0)
usb_transfers_reset();
}
} // namespace dmodem_android
@@ -75,3 +77,4 @@ int dmodem_libusb_open(libusb_device *dev, libusb_device_handle **h)
}
return libusb_open(dev, h);
}
+13
View File
@@ -14,6 +14,11 @@
* wrap the descriptor given (libusb_wrap_sys_device()) and give that
* device as the only one on the bus, already open.
*
* libusb_submit_transfer() is renamed too (usb_line.cpp, usb_agere.cpp):
* each transfer counted, each way, as it is sent and as it comes back,
* for the log (usb_transfer_stats()): a modem whose samples don't come
* says whether its transfers never come back or come back empty.
*
*/
#ifndef DMODEM_ANDROID_USB_ANDROID_H
#define DMODEM_ANDROID_USB_ANDROID_H
@@ -26,6 +31,13 @@ namespace dmodem_android {
(-1: none); the caller's, kept open while it is used */
void usb_use_fd(int fd);
/* the transfers since the last usb_use_fd() of a descriptor, each way, as a line for
the log: sent, back (and how: their octets, those that failed) */
void usb_transfer_stats(char *buf, size_t len);
/* the counts started again (usb_use_fd() of a descriptor) */
void usb_transfers_reset();
} // namespace dmodem_android
extern "C" {
@@ -33,6 +45,7 @@ int dmodem_libusb_init(libusb_context **ctx);
ssize_t dmodem_libusb_get_device_list(libusb_context *ctx,
libusb_device ***list);
int dmodem_libusb_open(libusb_device *dev, libusb_device_handle **h);
int dmodem_libusb_submit_transfer(struct libusb_transfer *t);
}
#endif
+7 -4
View File
@@ -113,18 +113,21 @@ int UsbModem::run()
u64 now = dcore::monotonic_ms();
if (now >= next_say) {
u64 wall = now - t0, clock = line_->now_ms();
char stats[256];
usb_transfer_stats(stats, sizeof(stats));
if (!warned && clock < wall / 2) {
ERR("usb: the modem's samples %s: its clock %llu ms "
"after %llu ms (no dialling, no timers without them)\n",
"after %llu ms (no dialling, no timers without "
"them); its transfers: %s\n",
clock ? "come too slowly" : "don't come",
(unsigned long long)clock,
(unsigned long long)wall);
(unsigned long long)wall, stats);
warned = true;
}
dcore::log_printf(1, "usb: the line's clock %llu ms, "
"the wall's %llu ms\n",
"the wall's %llu ms; %s\n",
(unsigned long long)clock,
(unsigned long long)wall);
(unsigned long long)wall, stats);
next_say = now + CLOCK_LOG_MS;
}
long wait = dmd::RunLoop::poll(modems, 1);
+109
View File
@@ -0,0 +1,109 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 Neo-Desktop
*
* usb_transfers.cpp - the USB line's transfers counted, for the log
* (usb_android.h)
*
*/
#include "usb_android.h"
#include <inttypes.h>
#include <stdio.h>
#include <unordered_map>
#include <log.h>
namespace {
/* (one modem in the process: its thread alone uses these) a way's transfers: sent (and refused), back (completed, and their
octets; failed, and the last failure's status) */
struct Way {
uint64_t sent = 0, refused = 0, done = 0, octets = 0, failed = 0;
int last_status = 0;
};
Way in_, out_;
/* the first IN transfers back: logged, their octets' head */
const int IN_SAID = 3;
int in_said_ = 0;
/* each transfer's own callback, called after it's counted */
std::unordered_map<libusb_transfer *, libusb_transfer_cb_fn> callbacks_;
bool is_in(const libusb_transfer *t)
{
return t->endpoint & LIBUSB_ENDPOINT_IN;
}
void LIBUSB_CALL counted(libusb_transfer *t)
{
Way &w = is_in(t) ? in_ : out_;
w.done++;
if (t->status == LIBUSB_TRANSFER_COMPLETED) {
w.octets += static_cast<uint64_t>(t->actual_length);
/* (an isochronous one's are its packets') */
for (int i = 0; i < t->num_iso_packets; i++)
w.octets += t->iso_packet_desc[i].actual_length;
} else {
w.failed++;
w.last_status = t->status;
}
if (is_in(t) && in_said_ < IN_SAID &&
t->type == LIBUSB_TRANSFER_TYPE_BULK) {
char head[3 * 8 + 1] = "";
int n = t->actual_length < 8 ? t->actual_length : 8;
for (int i = 0; i < n; i++)
snprintf(head + 3 * i, 4, " %02x", t->buffer[i]);
dcore::log_printf(1, "usb: IN transfer %d back: status %d, "
"%d octets:%s\n", in_said_ + 1, t->status,
t->actual_length, head);
in_said_++;
}
auto it = callbacks_.find(t);
if (it != callbacks_.end() && it->second)
it->second(t);
}
} // namespace
namespace dmodem_android {
void usb_transfers_reset()
{
in_ = Way();
out_ = Way();
in_said_ = 0;
callbacks_.clear();
}
void usb_transfer_stats(char *buf, size_t len)
{
snprintf(buf, len,
"IN %" PRIu64 " sent, %" PRIu64 " back (%" PRIu64
" octets, %" PRIu64 " failed, last %d)%s; "
"OUT %" PRIu64 " sent, %" PRIu64 " back (%" PRIu64
" failed, last %d)%s",
in_.sent, in_.done, in_.octets, in_.failed, in_.last_status,
in_.refused ? ", some refused" : "",
out_.sent, out_.done, out_.failed, out_.last_status,
out_.refused ? ", some refused" : "");
}
} // namespace dmodem_android
/* the transfer counted as it's sent and as it comes back (its own
callback then: a resubmitted one keeps the one it had) */
int dmodem_libusb_submit_transfer(struct libusb_transfer *t)
{
if (t->callback != counted) {
callbacks_[t] = t->callback;
t->callback = counted;
}
int err = libusb_submit_transfer(t);
Way &w = is_in(t) ? in_ : out_;
if (err == 0)
w.sent++;
else
w.refused++;
return err;
}
+9 -2
View File
@@ -93,13 +93,19 @@ 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}/usb_agere.cpp ${_dmd}/si3056.cpp
usb_fake_device.cpp ${_dmd}/test/fake_pico_line.cpp)
${_app}/usb/usb_transfers.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
${DMODEM_LIBUSB_SRC}/libusb)
target_compile_definitions(dmodem_engine_usb PUBLIC DMODEM_ENGINE_USB)
target_link_libraries(dmodem_engine_usb PUBLIC dmodem_sip)
# (the transfers counted as the app's are, for the log: usb_transfers.cpp)
set_source_files_properties(${_dmd}/usb_line.cpp ${_dmd}/usb_agere.cpp
PROPERTIES COMPILE_DEFINITIONS
"libusb_submit_transfer=dmodem_libusb_submit_transfer")
add_executable(usb_host usb_host.cpp)
target_link_libraries(usb_host PRIVATE dmodem_engine_usb Threads::Threads)
target_compile_options(usb_host PRIVATE -Wall -Wextra)
@@ -119,7 +125,8 @@ set_tests_properties(usb_line PROPERTIES TIMEOUT 300)
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)
${_dmd}/si3056.cpp ${_app}/usb/usb_transfers.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)
+5 -1
View File
@@ -11,7 +11,11 @@
namespace dmodem_android {
void usb_use_fd(int) {}
void usb_use_fd(int fd)
{
if (fd >= 0)
usb_transfers_reset();
}
} // namespace dmodem_android
+7
View File
@@ -135,6 +135,13 @@ try:
and cb.group(1).startswith(b"CONNECT"))
check("the exchange heard 1002 dialled", "1001 dialled 1002" in events)
check("1001's status: online, off hook", a.logged(r"^STATUS 2 1 [1-9]\d* [1-9]\d*$", 5))
check("1001's transfers counted, both ways, none failed",
a.logged(r"the line's clock [1-9]\d* ms, the wall's \d+ ms; IN [1-9]\d* sent, "
r"[1-9]\d* back \([1-9]\d* octets, 0 failed, last 0\); "
r"OUT [1-9]\d* sent, [1-9]\d* back \(0 failed", 12))
if agere:
check("1001's first IN transfer logged: 48 samples",
a.logged(r"usb: IN transfer 1 back: status 0, 100 octets: 30 00", 1))
a.write(b"hello from 1001\r\n")
check("1002 gets 1001's line", b.expect(rb"hello from 1001", 10))
b.write(b"hello from 1002\r\n")