Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion can-full/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ model sets and heals them; the node has no MIL.
| DM11 | 65235 | Request | clears the active DTCs and their counts; a positive Acknowledgment (to `0xFF`, byte 5 the requester) if asked of the genset, nothing if asked of everyone |
| DM3 | 65228 | Request | the same for the previously active DTCs |
| DM22 | 49920 | DM22 to the genset: control byte 1 (previously active) or 17 (active), `FF FF FF FF`, SPN and FMI | DM22 to the sender: control byte + 1 cleared, + 2 refused with byte 2 the reason (2 unknown DTC, 3 not previously active, 4 not active) |
| DM13 | 57088 | DM13 to the genset or to everyone: byte 1 bits 1-2 (this network) or bits 7-8 (this data link) 0 to stop, 1 to start | no answer: every periodic PG stops, requests are still answered, and they resume on a start or 6 s after the last stop |
| DM7 | 58112 | DM7 to the genset: test 247, SPN and FMI, `FF FF FF FF` | DM30 (41984) to the sender: SPN 110 or 100 as its PG carries it, with the limit its DTC sets at; any other test "cannot be performed" (`0xFB01`). The SLOT identifier is `0xFFFF` |
| DM14, DM16 | 55552, 55040 | see below | DM15 (55296), DM16 |

Expand Down Expand Up @@ -218,7 +219,7 @@ limit, `0x1003` for a wrong key, `0x1` for any other command.
| `../testing/suites/uds` | DC-DC | A conformance suite for any UDS server on any python-can bus: this node, or the UDS extension's demo ECU in-process. Used as a tester uses an ECU: zelos-can opens the bus, can-isotp carries, udsoncan asks. Every Tier A service with its NRCs and their ISO 14229-1 order, physical and functional; sessions, S3, P2; both security levels, lockout and power-up delay; memory by address; erase, download, check, upload of 64 KiB. `test_dcdc_node.py`, against the node itself: 0x28 stops DCDC_Status, the setpoint drives the output, DTCs follow the model through operation cycles, a reset is a fresh boot, XCP and UDS share the ECU. Core conformance: ztests in `../tests/lib/uds` |
| `../testing/suites/obd` | DC-DC | A scan tool's view of the same server, SAE J1979 on 0x7DF: supported PIDs walked from the bitmaps; PIDs against DCDC_Status; MIL and DTC count; a lost BMS pending in its first cycle and confirmed in its second, the MIL off after three passing cycles, the freeze frame stored with the pending code; a permanent DTC surviving 0x04 and gone after one passing cycle; J1979-2 protocol identification, multi-DID subsets, no 0x13 to OBD requests; the VIN (multi-frame) and ECU identity; J1979-2 (OBDonUDS) DIDs and 0x19 0x42/0x55/0x56 equal to the legacy services; silence to unsupported requests. Runs alone in CI (`just test-bench obd`) too, so its fault tests can play the BMS |
| `../testing/suites/autosar` | DC-DC | Decoded as a bench decodes an AUTOSAR ECU, from the ARXML alone: ids, lengths and cycles as described; E2E profile 1 (by hand), 2 and 5 (cantools) CRCs and counters; the container's PDUs in turn; the multiplexed PDU's alternatives; the SecOC MAC verified with the key; every value equal to DCDC_Status's of the same sample; UDS on the CanTp addresses the ARXML gives, its flow control as described; 0x28 silences the PDUs. The ARXML carries `bench.dbc` unchanged. Core: ztests in `../tests/lib/autosar` with the PRS E2E examples |
| `../testing/suites/j1939` | genset | Address claim on request; defending against a higher NAME and yielding to a lower one; every slow PG answered on request, a NACK for one the node lacks; every PG at its period; values agreeing across PGs (frequency and voltage from speed, P = 3 V I pf, S and Q, balanced phases, fuel only while turning) and the counters integrating speed, fuel rate and power; the status fields following the mode; TSC1 speed control and a speed limit honoured and reported in EEC1, dropped on its timeout; DM1 with one DTC in a frame, then two by BAM, lamps and occurrence counts as they should be, reassembled by python-can-j1939; DM2 by BAM to a global request and by RTS/CTS to ours, python-can-j1939 granting the windows; SOFT by RTS/CTS and by BAM, CI by BAM; the J1939-73 messages above: each answered on request, a PDU1 answer to the requester, every DTC list against DM5's and DM29's counts idle and hot, DM22, DM3 and DM11 clearing and acknowledged, the freeze frame laid out as DM24 says, DM7 answered by DM30, memory read and written behind the seed and key and each refusal's error indicator. Originator conformance (windows, retransmit, abort, T3/T4): ztests in `../tests/lib/j1939` |
| `../testing/suites/j1939` | genset | Address claim on request; defending against a higher NAME and yielding to a lower one; every slow PG answered on request, a NACK for one the node lacks; every PG at its period; values agreeing across PGs (frequency and voltage from speed, P = 3 V I pf, S and Q, balanced phases, fuel only while turning) and the counters integrating speed, fuel rate and power; the status fields following the mode; TSC1 speed control and a speed limit honoured and reported in EEC1, dropped on its timeout; DM1 with one DTC in a frame, then two by BAM, lamps and occurrence counts as they should be, reassembled by python-can-j1939; DM2 by BAM to a global request and by RTS/CTS to ours, python-can-j1939 granting the windows; SOFT by RTS/CTS and by BAM, CI by BAM; the J1939-73 messages above: each answered on request, a PDU1 answer to the requester, every DTC list against DM5's and DM29's counts idle and hot, DM22, DM3 and DM11 clearing and acknowledged, the freeze frame laid out as DM24 says, DM7 answered by DM30, memory read and written behind the seed and key and each refusal's error indicator, DM13 stopping every periodic PG while requests are still answered, starting them again, and a stop ending by itself. Originator conformance (windows, retransmit, abort, T3/T4): ztests in `../tests/lib/j1939` |
| `../testing/suites/canopen` | PDU | Boot-up and heartbeat; NMT start, stop and pre-operational; SDO identity and downloads; RPDO commands reflected in TPDOs; an overcurrent trip raises EMCY and recovers; SYNC answered by the synchronous TPDO |
| `../testing/suites/eth` | DC-DC | The board's Ethernet (`../lib/eth`): link 100 Mb/s full duplex, the static address up after conflict detection, ping, the host's neighbour entry equal to the node's MAC, and the node's counters (UDS DID `0x0111`, read over CAN). The host end is whichever interface is on the node's subnet: the bench's `tap0`, or the hardware bench's own |
| `../testing/suites/doip` | DC-DC | Its UDS server as a DoIP entity (ISO 13400-2, `../lib/doip`), checked with a client written from the standard and with zelos-can's (doipclient), as the UDS and OBD extensions connect: vehicle identification by plain request, EID and VIN, answered within 500 ms, and one responder to a broadcast; entity status, power mode, header NACKs; routing activation's codes (0x10, 0x00, 0x02, 0x03 and 0x01 decided by alive checks, 0x06), initial inactivity; diagnostic messages acknowledged, NACKed (0x02, 0x03, 0x04 above MDS) or answered; three announcements 500 ms apart after a reset. The UDS conformance suite, unchanged, over DoIP. One server, two transports: a CAN tester's session gives the DoIP tester 0x21 and only OBD, and S3 still ends it; a closed DoIP owner's output returns at once; a pending routine answers its own tester. Core: ztests in `../tests/lib/doip` |
Expand Down
11 changes: 9 additions & 2 deletions include/zelos/j1939.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ struct zelos_j1939 {
* Optional, both called from zelos_j1939_on_frame() once the address is
* claimed. on_request: a request for a PGN outside msgs, to this node
* or to everyone (da); true takes it, false leaves one to this node
* NACKed. on_pg: a single-frame PDU1 PG addressed to this node that the
* NACKed. on_pg: a single-frame PDU1 PG to this node or to everyone (da) that the
* core has no use for.
*/
bool (*on_request)(uint32_t pgn, uint8_t sa, uint8_t da, void *user);
void (*on_pg)(uint32_t pgn, uint8_t sa, const uint8_t *data, uint8_t len, void *user);
void (*on_pg)(uint32_t pgn, uint8_t sa, uint8_t da, const uint8_t *data, uint8_t len,
void *user);
/* The PDU1 PGNs on_pg wants: the Zephyr glue filters on them. */
const uint32_t *rx_pgns;
size_t n_rx_pgns;

/* The periodic sends are held; requests are still answered (DM13). */
bool quiet;

/* Read-only for the caller. */
enum zelos_j1939_state state;
uint8_t address;
Expand Down Expand Up @@ -201,6 +205,9 @@ void zelos_j1939_send_to(struct zelos_j1939_msg *msg, uint8_t da);
/* Whether a message still waits to go out, or its transport session is running. */
bool zelos_j1939_pending(struct zelos_j1939 *j, const struct zelos_j1939_msg *msg);

/* Hold, or resume, the periodic sends; requests are answered either way. */
void zelos_j1939_quiet(struct zelos_j1939 *j, bool quiet);

/*
* Positive Acknowledgment of a request or command for pgn from node `to`, on
* the next poll: to the global address, naming `to`, as the NACK goes.
Expand Down
8 changes: 4 additions & 4 deletions lib/can/boards/nucleo_h753zi.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* event FIFO is never full: Renode's model reads a full one as empty, and its
* frames then never complete (16 at once, when the bus comes back).
*
* 28 std filters x 4 + 8 ext x 8 + (32 + 32) RX x 72 + 16 events x 8
* + 15 TX x 72 = 5992 bytes (0x1768) of the 10 KiB message RAM.
* 28 std filters x 4 + 10 ext x 8 + (32 + 32) RX x 72 + 16 events x 8
* + 15 TX x 72 = 6008 bytes (0x1778) of the 10 KiB message RAM.
*
* Not one TX buffer, though it would keep Zephyr ISO-TP's CFs in order:
* ISO-TP sends straight to the driver, so its CFs would take turns with
Expand All @@ -23,8 +23,8 @@
*/

&fdcan1 {
reg = <0x4000a000 0x400>, <0x4000ac00 0x1768>;
bosch,mram-cfg = <0x0 28 8 32 32 0 16 15>;
reg = <0x4000a000 0x400>, <0x4000ac00 0x1778>;
bosch,mram-cfg = <0x0 28 10 32 32 0 16 15>;
};

&fdcan2 {
Expand Down
12 changes: 9 additions & 3 deletions lib/j1939/core/j1939.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ void zelos_j1939_on_frame(struct zelos_j1939 *j, const struct zelos_j1939_frame
on_request(j, da, sa, frame, now_ms);
} else if (pgn == ZELOS_J1939_PGN_TP_CM) {
on_tp_cm(j, da, sa, frame, now_ms);
} else if (PDU1(pgn) && da == j->address && j->state == ZELOS_J1939_CLAIMED &&
j->on_pg != NULL) {
j->on_pg(pgn, sa, frame->data, frame->len, j->user);
} else if (PDU1(pgn) && (da == j->address || da == ZELOS_J1939_ADDR_GLOBAL) &&
j->state == ZELOS_J1939_CLAIMED && j->on_pg != NULL) {
j->on_pg(pgn, sa, da, frame->data, frame->len, j->user);
}
}

Expand Down Expand Up @@ -383,6 +383,12 @@ static void send_msgs(struct zelos_j1939 *j, uint32_t now_ms)
for (size_t i = 0; i < j->n_msgs; i++) {
struct zelos_j1939_msg *m = &j->msgs[i];
bool periodic = m->period_ms != 0U && reached(now_ms, m->due_ms);

if (periodic && j->quiet) {
/* Held, in phase for when the sends come back. */
m->due_ms += m->period_ms;
periodic = false;
}
/* A periodic send answers a pending request too: it goes to everyone. */
bool to_one = !periodic && m->requester != ZELOS_J1939_ADDR_GLOBAL;

Expand Down
7 changes: 7 additions & 0 deletions lib/j1939/zephyr/j1939.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ bool zelos_j1939_pending(struct zelos_j1939 *j, const struct zelos_j1939_msg *ms
return pending;
}

void zelos_j1939_quiet(struct zelos_j1939 *j, bool quiet)
{
k_mutex_lock(&lock, K_FOREVER);
j->quiet = quiet;
k_mutex_unlock(&lock);
}

void zelos_j1939_ack(struct zelos_j1939 *j, uint8_t to, uint32_t pgn)
{
k_mutex_lock(&lock, K_FOREVER);
Expand Down
49 changes: 47 additions & 2 deletions nodes/genset/src/genset.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ LOG_MODULE_REGISTER(genset, LOG_LEVEL_INF);
#define PGN_DM6 65231U
#define PGN_DM11 65235U
#define PGN_DM12 65236U
#define PGN_DM13 57088U
#define PGN_DM16 55040U
#define PGN_DM19 54016U
#define PGN_DM20 49664U
Expand Down Expand Up @@ -569,12 +570,18 @@ static bool on_request(uint32_t pgn, uint8_t sa, uint8_t da, void *user)
return k_msgq_put(&genset_diag_msgq, &rx, K_NO_WAIT) == 0;
}

static void on_pg(uint32_t pgn, uint8_t sa, const uint8_t *data, uint8_t len, void *user)
static void on_pg(uint32_t pgn, uint8_t sa, uint8_t da, const uint8_t *data, uint8_t len,
void *user)
{
struct diag_rx rx = {.pgn = pgn, .sa = sa, .to_us = true, .len = len};

ARG_UNUSED(user);

/* Only DM13 is for everyone. */
if (da == ZELOS_J1939_ADDR_GLOBAL && pgn != PGN_DM13) {
return;
}

memcpy(rx.data, data, len);
(void)k_msgq_put(&genset_diag_msgq, &rx, K_NO_WAIT);
}
Expand All @@ -583,7 +590,7 @@ static void on_pg(uint32_t pgn, uint8_t sa, const uint8_t *data, uint8_t len, vo
#define PGN_DM14 PGN_OF(GENSET_DM14_FRAME_ID)
#define PGN_DM22 PGN_OF(GENSET_DM22_FRAME_ID)

static const uint32_t rx_pgns[] = {PGN_DM7, PGN_DM14, PGN_DM16, PGN_DM22};
static const uint32_t rx_pgns[] = {PGN_DM7, PGN_DM13, PGN_DM14, PGN_DM16, PGN_DM22};

static struct zelos_j1939 j1939 = {
.name = NAME,
Expand Down Expand Up @@ -1351,11 +1358,47 @@ static void mem_step(void)
}

/* What a tool asked since the last pass. Before the model's step, so a cleared fault that persists is back in this pass. */
/*
* DM13: byte 1 bits 1-2 command this network, bits 7-8 the data link the
* message came on; 0 stops the periodic sends, 1 starts them, else no change.
* A stop holds for DM13_HOLD_MS past the last one; the suspend signal and
* duration are not read.
*/
#define DM13_HOLD_MS 6000
#define DM13_STOP 0U
#define DM13_START 1U

static int64_t quiet_until_ms;

static void broadcast(const struct diag_rx *rx, int64_t now_ms)
{
const uint8_t network = rx->data[0] & 0x03U;
const uint8_t link = rx->data[0] >> 6;

if (network == DM13_START || link == DM13_START) {
quiet_until_ms = 0;
zelos_j1939_quiet(&j1939, false);
LOG_INF("DM13 from 0x%02x: broadcasts on", rx->sa);
} else if (network == DM13_STOP || link == DM13_STOP) {
if (quiet_until_ms == 0) {
LOG_INF("DM13 from 0x%02x: broadcasts off", rx->sa);
}
quiet_until_ms = now_ms + DM13_HOLD_MS;
zelos_j1939_quiet(&j1939, true);
}
}

static void diagnose(struct engine *e)
{
struct diag_rx rx;
const int64_t now_ms = k_uptime_get();

mem_step();
if (quiet_until_ms != 0 && now_ms >= quiet_until_ms) {
quiet_until_ms = 0;
zelos_j1939_quiet(&j1939, false);
LOG_INF("DM13 hold over: broadcasts on");
}

while (k_msgq_get(&genset_diag_msgq, &rx, K_NO_WAIT) == 0) {
if (rx.pgn == PGN_DM3 || rx.pgn == PGN_DM11) {
Expand All @@ -1368,6 +1411,8 @@ static void diagnose(struct engine *e)
mem_write(&rx);
} else if (rx.len < 8U) {
continue;
} else if (rx.pgn == PGN_DM13) {
broadcast(&rx, now_ms);
} else if (rx.pgn == PGN_DM22) {
clear_one(&rx);
} else if (rx.pgn == PGN_DM7) {
Expand Down
47 changes: 47 additions & 0 deletions testing/suites/j1939/test_genset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,50 @@ def test_memory_access_refused(bus, node, command, pointer, length, error):
# The same request again with a key that is not the seed's.
reply = dm15(bus, sa, dm14(sa, command, pointer, length, reply["Seed"] ^ MEM_KEY ^ 1))
assert (reply["Status"], reply["LengthAllowed"], reply["EDCParameter"], reply["EDCPExtension"], reply["Seed"]) == (MEM_FAILED, 0, error, 7, NO_KEY), reply


PGN_DM13 = 57088
# From nodes/genset: a stop holds this long past the last one.
DM13_HOLD_S = 6.0
DM13_STOP, DM13_START = bytes([0xFC]) + b"\xff" * 7, bytes([0xFD]) + b"\xff" * 7


def periodic_from(frames, sa):
"""The node's periodic PGs among frames."""
pgns = {pgn_of(m.frame_id) for m in PERIODIC}
return [m for m in frames if theirs(m) and parse(m)[2] == sa and parse(m)[0] in pgns]


@pytest.mark.parametrize("da", ["node", "global"])
def test_dm13_stops_and_starts_the_broadcasts(bus, node, da):
"""DM13 stop, to the node or to everyone, holds every periodic PG while requests
are still answered; DM13 start brings them back at once."""
sa, _ = node
to = sa if da == "node" else GLOBAL
try:
send(bus, 6, PGN_DM13, DM13_STOP, da=to)
# What was already on its way, then silence well inside the hold.
collect(bus, 0.5)
assert periodic_from(collect(bus, 2.0), sa) == []
request(bus, PGN_HOURS, da=sa)
first(bus, is_from(PGN_HOURS, sa))
finally:
send(bus, 6, PGN_DM13, DM13_START, da=to)
got = window(bus, sa, 10)
assert {parse(m)[0] for m in periodic_from(got, sa)} >= {PGN_EEC1, PGN_EEC2}


def test_dm13_stop_ends_by_itself(bus, node):
"""A stop that is not renewed ends after the hold, and one that is, holds on."""
sa, _ = node
send(bus, 6, PGN_DM13, DM13_STOP, da=sa)
collect(bus, 0.5)
try:
# Renewed every 2 s, quiet past one hold.
for _ in range(4):
assert periodic_from(collect(bus, 2.0), sa) == []
send(bus, 6, PGN_DM13, DM13_STOP, da=sa)
first(bus, is_from(PGN_EEC1, sa), DM13_HOLD_S + bench.WAIT_S)
finally:
send(bus, 6, PGN_DM13, DM13_START, da=sa)

33 changes: 32 additions & 1 deletion tests/lib/j1939/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,17 @@ ZTEST(j1939, test_requests)

static uint32_t command_pgn;
static uint8_t command_len;
static uint8_t command_da;

static bool take_dm11(uint32_t pgn, uint8_t sa, uint8_t da, void *user)
{
return pgn == PGN_DM11;
}

static void take_command(uint32_t pgn, uint8_t sa, const uint8_t *data, uint8_t len, void *user)
static void take_command(uint32_t pgn, uint8_t sa, uint8_t da, const uint8_t *data, uint8_t len,
void *user)
{
command_da = da;
command_pgn = pgn;
command_len = len;
}
Expand Down Expand Up @@ -272,6 +275,34 @@ ZTEST(j1939, test_application_hooks)
inject(ID(6, 0xE300U | SA, TESTER), dm21, 8);
zassert_equal(command_pgn, 0xE300U);
zassert_equal(command_len, 8);
zassert_equal(command_da, SA);
/* One to everyone reaches it too, as such. */
inject(ID(6, 0xDFFFU, TESTER), dm21, 8);
zassert_equal(command_pgn, 0xDF00U);
zassert_equal(command_da, ZELOS_J1939_ADDR_GLOBAL);
}

/* Quiet: no periodic sends, requests still answered, the phase kept after. */
ZTEST(j1939, test_quiet)
{
int from, at;

start(FIXED);
run_until(1500);
j.quiet = true;
from = n_sent;
run_until(4400);
zassert_equal(n_sent, from);

inject_request(SA, PGN_ET1);
run_until(4500);
zassert_equal(n_sent, from + 1);

j.quiet = false;
run_until(6400);
zassert_equal(n_sent, from + 3);
at = find(ID(6, PGN_ET1, SA), from + 1);
zassert_equal(sent[at + 1].at - sent[at].at, 1000);
}

/* DM1 went out by BAM: the announcement, then three packets at legal gaps. */
Expand Down
Loading