Skip to content

Commit e2fcd11

Browse files
Schildkroetclaude
andcommitted
LindeAPI: test against a simulated lin_usb device, fix what it found
FakeLinUsbDevice transcribes the USB layer of the firmware's lin_usb.c (STALL rules, one ACK per set-data packet, held ACK with the IN queue full, dropped counter). The LIN engine is not in this repository; its part of the fake was measured on a CANILFD adapter with the new hw_compare commands linusb-probe / linusb-engine / linusb-driver (0 differences). Tests link FakeUsbVendorInterface.cpp in place of UsbVendorInterfaceLibusb.cpp. Driver fixes: - selecting a schedule table that was never loaded stopped the master's running schedule; it now keeps the current one and warns - BUS_STATE.dropped counts since the device's last USB reset, so drops of an earlier session showed up as overruns at measurement start (seen on the adapter); counted from open() now - device timestamps jumped 49 days back when HAL_GetTick() wrapped; the difference is taken modulo 2^32 tests/support/LogCapture is thread safe now: drivers log from reader threads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 97e3ac2 commit e2fcd11

16 files changed

Lines changed: 2266 additions & 15 deletions

‎src/driver/LindeApiDriver/LindeApiInterface.cpp‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ void LindeApiInterface::open()
382382
_numTxErr = 0;
383383
_numRxErr = 0;
384384
_devRxDropped = 0;
385+
// The device counts drops since its last USB reset, across host sessions:
386+
// only those from now on belong to this measurement.
387+
_devRxDroppedBase = 0;
388+
if (_sharedDev->features & LIN_USB_FEATURE_BUS_STATE)
389+
{
390+
lin_usb_bus_state_t hwState{};
391+
if (_sharedDev->getBusState(_channel, hwState))
392+
_devRxDroppedBase = hwState.dropped;
393+
}
385394
_isOpen.store(true);
386395
}
387396

@@ -535,13 +544,18 @@ void LindeApiInterface::setLinScheduleTable(uint8_t tableIndex)
535544
log_warning(QStringLiteral("LindeAPI: schedule table %1 out of range").arg(tableIndex));
536545
return;
537546
}
538-
_sharedDev->scheduleStop(_channel);
539-
_activeTable = tableIndex;
547+
// A table that was never uploaded (not in the LDF, or beyond the device's
548+
// tables) has no entries: keep the running schedule instead of stopping it.
540549
const uint8_t count = _tableEntryCounts[tableIndex];
541-
if (count > 0)
550+
if (count == 0)
542551
{
543-
_sharedDev->scheduleStart(_channel, tableIndex, count);
552+
log_warning(QStringLiteral("LindeAPI: schedule table %1 not loaded, keeping table %2")
553+
.arg(tableIndex).arg(_activeTable));
554+
return;
544555
}
556+
_sharedDev->scheduleStop(_channel);
557+
_activeTable = tableIndex;
558+
_sharedDev->scheduleStart(_channel, tableIndex, count);
545559
}
546560

547561
void LindeApiInterface::sendLinDiagRequest(uint8_t nad, const uint8_t *data, uint8_t len)
@@ -579,7 +593,9 @@ uint32_t LindeApiInterface::getState()
579593
if (!_sharedDev->getBusState(_channel, hwState))
580594
return state_unknown;
581595

582-
_devRxDropped.store(hwState.dropped);
596+
// The counter only restarts at a USB reset (then it is below the base)
597+
const uint16_t base = _devRxDroppedBase;
598+
_devRxDropped.store(hwState.dropped >= base ? hwState.dropped - base : hwState.dropped);
583599

584600
switch (static_cast<lin_usb_bus_state_e>(hwState.state))
585601
{

‎src/driver/LindeApiDriver/LindeApiInterface.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class LindeApiInterface : public BusInterface
6868

6969
// Frames the device itself had to drop; refreshed by getState().
7070
std::atomic<uint32_t> _devRxDropped{0};
71+
// BUS_STATE.dropped at open(); written before _isOpen, read by getState()
72+
uint16_t _devRxDroppedBase{0};
7173

7274
// schedule entry counts per table (populated during open)
7375
static constexpr uint8_t MAX_TABLES = 8;

‎src/driver/LindeApiDriver/LindeSharedDevice.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ int64_t LindeSharedDevice::deviceTimestampToHostMs(uint32_t ts_ms)
191191
QMutexLocker lock(&timestampMutex);
192192
if (!timestampValid)
193193
return QDateTime::currentMSecsSinceEpoch();
194-
const int64_t delta = static_cast<int64_t>(ts_ms) - static_cast<int64_t>(deviceTicksStart_ms);
194+
// HAL_GetTick() wraps after 49.7 days: take the difference modulo 2^32,
195+
// signed so frames stamped just before the epoch stay slightly earlier.
196+
const int64_t delta = static_cast<int32_t>(ts_ms - deviceTicksStart_ms);
195197
return hostOffsetStart_ms + delta;
196198
}
197199

‎tests/hw_compare/README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ $B bus /dev/ttyACM0 can0 [elmue] # a SocketCAN channel on the same bus, 500k /
1515
$B candle-probe 206E38A5505650082
1616
$B candle-bus 206E38A5505650082 can0
1717
$B candle-leave 206E38A5505650082 # leave channel 0 running at 250 kbit/s
18+
19+
# lin_usb (LindeAPI driver) through libusb, first device with the LIN interface
20+
$B linusb-probe # control requests + set data, real vs. FakeLinUsbDevice (CANILFD config)
21+
$B linusb-engine # a 2-slot master schedule on the real adapter, frames as reported
22+
$B linusb-driver # LindeApiInterface on the real adapter: open, run, set data, close
1823
```
1924

2025
`probe` sends the same command scripts to both and prints every reply byte.
@@ -88,3 +93,37 @@ with the device at the wrong bitrate, frames were shown as sent that never
8893
reached the bus. That is the firmware's protocol (Elmue's comment calls the echo
8994
"useless"), not something the driver can see.
9095

96+
97+
## Results: lin_usb, CANILFD (LindeAPI driver)
98+
99+
CANILFD CAN+LIN+AIO adapter (Hermelin Labs, `1d50:606f`, bcdDevice 1.03), LIN
100+
interface 1 (EP 0x83 / 0x04), firmware 0.1.0, HW 2. Nothing else on the LIN
101+
bus. `FakeLinUsbDevice` transcribes the USB layer of `lin_usb.c`; the LIN engine
102+
is not in the repository, so its part of the fake was taken from these runs.
103+
104+
| Checked | Result |
105+
|---|---|
106+
| Board config (DEVICE_CONFIG) | 6 tables, baud mask 0x7F (1200 too), HW 2: differs from the repository's `lin_usb_config.h` (4, 0x7E, 1); now `Config::canilfd()` |
107+
| STALLs: channel >= 2, slot >= 16, dlc 0 / 9, table >= 6, unknown request; zero-length IDENTIFY | identical |
108+
| Set-data ACKs: valid, dlc 0 / 9, channel 5, short packet (zeroed echo) | identical |
109+
| Engine (`linusb-engine`) | protected IDs (0x21 -> 0x61); publisher frames RESPONDED + VALID; unanswered subscriber slots SUBSCRIBER + ERROR, data zero, BUS_STATE then ERROR; set data accepted only while started and only for an ID in the active table (publisher or subscriber), otherwise ERROR ACK; new payload from the next slot on; sleep: id 0, dlc 8, `00 FF..`, SLEEP, state SLEEPING; wakeup: id 0, dlc 0, WAKEUP; STOPPED until MODE START. Modelled in the fake, `linusb-probe` 0 differences (6 runs) |
110+
| `LindeApiInterface` (`linusb-driver`) | opens in 40 ms; TX / RX direction, NotResponded errors, set data, rejected set data (TX error), timestamps, counters as in the unit tests; selecting a table that was never loaded keeps the schedule running (fixed driver) |
111+
| BUS_STATE.dropped | counts since the last USB reset, across host sessions: 422 left over from an aborted run showed up in the next one. The driver now counts from its open() |
112+
113+
Found and fixed in the CANILFD firmware (`Libraries/LinIF/LinIF.c`,
114+
`Libraries/USBClasses/lin_usb_engine.c`): a channel stopped by the host
115+
(MODE STOP) restarted itself. `LinIF_Stop()` only cleared `isActive`, so the
116+
RXNE edge from disabling the transceiver (or any bus activity) looked like a
117+
slave wakeup to `LinIF_MasterWakeupISR()`, and 100 ms later `LinIF_Wakeup()`
118+
restarted the schedule while BUS_STATE still said STOPPED; a stopped slave
119+
came back on the next break. A host sleep / wakeup on a stopped channel was
120+
queued or restarted it too, and reported events that did not match the command
121+
(sleep -> wakeup). LinIF now has a `stopped` flag that only `LinIF_Start()`
122+
clears, and the engine ignores sleep / wakeup for a channel the host has not
123+
started. With the fixed firmware `linusb-probe` checks this (sleep / wakeup on
124+
a stopped channel: no events, no stale go-to-sleep at the next START, nothing
125+
sent after STOP): 0 differences in 12 runs; before, the spontaneous wakeup
126+
showed up in about 3 of 5.
127+
128+
Not covered: a real slave on the bus (responses, checksum errors), slave and
129+
listen-only mode, baud rates other than 19200.

‎tests/hw_compare/hw_compare.pro‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ CONFIG += link_pkgconfig
1111
PKGCONFIG += libusb-1.0
1212

1313
HEADERS += \
14+
linusb_compare.h \
15+
../lindeapi_driver/FakeLinUsbDevice.h \
16+
$$SRC_DIR/driver/LindeApiDriver/LindeApiInterface.h \
1417
../slcan_driver/FakeSlcanDevice.h \
1518
../candle_driver/FakeGsUsbDevice.h \
1619
$$SRC_DIR/driver/CandleApiDriver/CandleApiInterface.h \
@@ -21,6 +24,15 @@ HEADERS += \
2124

2225
SOURCES += \
2326
main.cpp \
27+
linusb_compare.cpp \
28+
../lindeapi_driver/FakeLinUsbDevice.cpp \
29+
$$SRC_DIR/driver/LindeApiDriver/LindeApiInterface.cpp \
30+
$$SRC_DIR/driver/LindeApiDriver/LindeSharedDevice.cpp \
31+
$$SRC_DIR/driver/UsbVendorInterface/UsbVendorInterface.cpp \
32+
$$SRC_DIR/driver/UsbVendorInterface/UsbVendorInterfaceLibusb.cpp \
33+
$$SRC_DIR/db/model/LinDb.cpp \
34+
$$SRC_DIR/db/model/LinFrame.cpp \
35+
$$SRC_DIR/db/model/LinSignal.cpp \
2436
../slcan_driver/FakeSlcanDevice.cpp \
2537
../candle_driver/FakeGsUsbDevice.cpp \
2638
candle_usb_hw.cpp \

0 commit comments

Comments
 (0)