From 2bf9029213730caa9a8783d1f0045ef5b60264f5 Mon Sep 17 00:00:00 2001 From: tkeairns Date: Sat, 5 Dec 2026 16:22:33 -0800 Subject: [PATCH] mms: data sets of whole data objects, a clock the node can be given A data set may list data objects (functionally constrained data): such a member is named to its data object and reported as the structure a read of it gives, value, q and t. It is reported when a leaf of it triggers, with that leaf's reason, or both. zelos_mms_clock() gives the server UTC and a TimeQuality; a change is stamped with the clock as it was then, and TimeofEntry follows it. Without a call nothing changes: time since boot, not synchronized. A buffered entry keeps the values with their change times, so a replayed data object carries the t it had. full: data set ds_pos (XSWI1-8 Pos whole) with the buffered block brcb_pos01; ds_pdu and ds_dcdc are GOOSE's too and stay as they are. UDS routine 0x6186 sets the clock for a test; the routine option record may now be 8 bytes. --- can-full/README.md | 14 ++- include/zelos/mms.h | 82 ++++++++++++--- include/zelos/uds.h | 2 +- lib/mms/core/mms_core.c | 9 +- lib/mms/core/mms_internal.h | 19 +++- lib/mms/core/mms_model.c | 117 +++++++++++++++------ lib/mms/core/mms_report.c | 164 +++++++++++++++++------------- lib/mms/core/mms_service.c | 8 +- lib/mms/zephyr/mms.c | 48 ++++++++- nodes/dcdc/src/uds.c | 7 ++ nodes/full/src/mms.c | 11 +- nodes/full/zelosfull.cid | 18 +++- testing/suites/mms/test_server.py | 52 +++++++++- tests/lib/mms/src/main.c | 46 +++++++++ 14 files changed, 458 insertions(+), 139 deletions(-) diff --git a/can-full/README.md b/can-full/README.md index 8a54b0c..47378d2 100644 --- a/can-full/README.md +++ b/can-full/README.md @@ -111,17 +111,21 @@ authentication. `../nodes/full/zelosfull.cid` is its station file. Each data object reads with its value, quality and time: `XSWI1`-`XSWI8` `Pos`, `PTRC1`-`PTRC8` `Tr`, `EmcyGGIO1` `IntIn1` for the PDU; `OutMMDC1` `Vol` and `Amp`, `InMMDC1` `Amp`, `STMP1` `Tmp`, `CycGGIO1` `CntVal1` for the DC-DC. The -data sets are GOOSE's own, one report control block each: +data sets `ds_pdu` and `ds_dcdc` are GOOSE's own; `ds_pos` lists the channels' +`Pos` as whole data objects. One report control block each: | Report control block | Kind | Data set | Reports | |---|---|---|---| | `LLN0$BR$brcb_pdu01` | buffered | `ds_pdu` | a PDU change as it happens; the last 32 kept while no client is connected and replayed to one that resumes from its last EntryID | | `LLN0$RP$urcb_dcdc01` | unbuffered | `ds_dcdc` | the DC-DC's members that moved, every 100 ms | +| `LLN0$BR$brcb_pos01` | buffered | `ds_pos` | a channel's `Pos` with its quality and time when it switches; kept and replayed as `brcb_pdu01`'s | -Both start with data change, quality change and general interrogation as +All start with data change, quality change and general interrogation as triggers and no integrity period; a client sets its own. The node has no clock: -time stamps are time since boot, flagged clock not synchronized. No controls, -files or setting groups. +time stamps are time since boot, flagged clock not synchronized. UDS routine +`0x6186`, in the extended session, gives it one for a test: the TimeQuality +octet, then UTC in ms since 1970 (6 bytes, big-endian), 0 for time since boot +again. No controls, files or setting groups. As an AUTOSAR ECU it sends what `../arxml/can-full.arxml` (an AUTOSAR R19-11 System Template of this bench, `bench.dbc` included) describes: E2E profiles 1, @@ -243,7 +247,7 @@ limit, `0x1003` for a wrong key, `0x1` for any other command. | `../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` | | `../testing/suites/someip` | DC-DC | Its SOME/IP service (DcdcService `0x5A00`, `../lib/someip`) and its SD, checked with a client written from the PRS: offers every second with TTL 3 and the service's UDP endpoint (where the SD group reaches the host: on this bench, not on the hardware bench, whose switch forwards it to IGMPv2 members only); FindService answered unicast within 100 ms; a subscription acknowledged, the Setpoint field's value at once, then DcdcStatus every 100 ms with none missing, each equal, within the frame's resolution, to the DCDC_Status of the same Counter; the getter equal to DID `0x0200`, and the notifier within 150 ms of a write over UDS; 0x28 stops the events, not SD or the getter; error codes; StopOffer at a reset, and the Reboot flag after it. Core: ztests in `../tests/lib/someip` | | `../testing/suites/goose` | DC-DC, PDU | Its GOOSE (`../lib/goose`) captured raw on the host interface on the node's subnet and decoded by a BER walk written from IEC 61850-8-1 Annex A: both control blocks with their destinations, references, goID and data set sizes; timeAllowedtoLive twice the period, or one of the on-change ladder's; stNum and sqNum in sequence, none missed; `gcb_dcdc` every 100 ms (within 5 % on hardware). Needs CAP_NET_RAW, which the bench's tester has. Core: ztests in `../tests/lib/goose` | -| `../testing/suites/mms` | DC-DC, PDU | Its MMS server (`../lib/mms`) with an MMS client on the host: the data sets and report control blocks the station file declares; a data object read with its quality and time; the unbuffered block reporting the DC-DC every 100 ms, none missed; a PDU channel switched over CANopen reported by the buffered block, and a change made while no client was connected replayed to the client that resumes. Core: ztests in `../tests/lib/mms` | +| `../testing/suites/mms` | DC-DC, PDU | Its MMS server (`../lib/mms`) with an MMS client on the host: the data sets and report control blocks the station file declares; a data object read with its quality and time; the unbuffered block reporting the DC-DC every 100 ms, none missed; a PDU channel switched over CANopen reported by the buffered block, and a change made while no client was connected replayed to the client that resumes; a channel's `Pos` reported whole, its time by the clock set over UDS. Core: ztests in `../tests/lib/mms` | | `../testing/suites/full` | all three | Every protocol at once for `--soak-s` (120 s here, 10 s on hardware): XCP DAQ, a UDS 64 KiB download and upload loop, CANopen operational with SYNC every 10 ms and RPDO1, J1939 and DCDC_Status. No EMCY, no DAQ sample lost, periods kept, and the node's counters (UDS DID `0x0110`) show no TX queue overflow, TX error or RX FIFO overrun | The suites drive the other nodes' roles themselves when those nodes are absent, diff --git a/include/zelos/mms.h b/include/zelos/mms.h index 652209f..9efd9c1 100644 --- a/include/zelos/mms.h +++ b/include/zelos/mms.h @@ -32,8 +32,13 @@ * reads from it and reports its changes. They are read when poked, every * scan_ms, and before a read is served. A quality leaf without a value is * always good. A time leaf is core-owned: when a value of its data object last - * changed, as time since boot from 1970-01-01 with the quality "clock not - * synchronized"; 0 until the first change. + * changed, by the clock as it was then (zelos_mms_clock()); 0 until the first + * change. Without a clock it is time since boot from 1970-01-01 with the + * quality "clock not synchronized". + * + * A data set lists data attributes or, with objects set, whole data objects + * (functionally constrained data): such a member is named to its data object + * and is the structure a read of it gives, t included. * * Report control blocks (IEC 61850-7-2), attributes in the order of 8-1: * RptID, RptEna, Resv (unbuffered only), DatSet, ConfRev, OptFlds, BufTm, @@ -50,7 +55,9 @@ * buffered one with ResvTms above 0 stays reserved that many seconds for * a client of the same address. ResvTms -1 is not used. * - Triggers: data change (a value leaf), quality change (a quality leaf), - * integrity every IntgPd ms, general interrogation. Changes within BufTm + * integrity every IntgPd ms, general interrogation. A data object is + * reported whole when a leaf of it triggers, with that leaf's reason, or + * both. Changes within BufTm * ms of the first go in one report; a member changing twice sends the * first report at once. SqNum starts at 0 when enabled and wraps at 8 * bits unbuffered, 16 buffered. @@ -70,8 +77,13 @@ * without a client they would push the changes out of the ring. * - Writing an attribute the value it has purges nothing. Changing RptID, * DatSet, BufTm, TrgOps or IntgPd purges the buffer, as does PurgeBuf. - * - TimeofEntry is a BINARY-TIME, which cannot hold a date before 1984: it - * is time since boot from 1984-01-01. + * - TimeofEntry is a BINARY-TIME, which cannot hold a date before 1984: + * without a clock it is time since boot from 1984-01-01. + * + * Test knob: UDS RoutineControl ZELOS_MMS_ROUTINE (extended session; with + * lib/uds) sets the clock. Option record: the TimeQuality octet, then UTC in + * ms since 1970-01-01 (6 bytes, big-endian), 0 for time since boot. Result: + * the same two as the node has them. * * A send that fails closes that connection. The core never waits on a client; * the socket layer's send may, for half a second at most. @@ -104,6 +116,13 @@ #define ZELOS_MMS_BURST 8 /* A leaf without a value: a quality that is always good, or a time. */ #define ZELOS_MMS_NO_VALUE 0xFF +/* TimeQuality (IEC 61850-7-2): two of its flags, and the fraction's accuracy unspecified. */ +#define ZELOS_MMS_CLOCK_FAILURE 0x40 +#define ZELOS_MMS_CLOCK_NOT_SYNCHRONIZED 0x20 +#define ZELOS_MMS_ACCURACY_UNSPECIFIED 0x1F +#define ZELOS_MMS_NO_CLOCK (ZELOS_MMS_CLOCK_NOT_SYNCHRONIZED | ZELOS_MMS_ACCURACY_UNSPECIFIED) +#define ZELOS_MMS_ROUTINE 0x6186 +#define ZELOS_MMS_ROUTINE_OPTION 7 /* A report control block without a data set. */ #define ZELOS_MMS_NO_DATA_SET 0xFF @@ -144,11 +163,15 @@ struct zelos_mms_leaf { uint8_t value; }; -/* A data set: "LLN0$ds", its members as indices into the leaves. */ +/* + * A data set: "LLN0$ds", its members as indices into the leaves. With objects, + * each is the first leaf of a data object, and the member is that object. + */ struct zelos_mms_data_set { const char *name; const uint8_t *members; uint8_t count; + bool objects; }; /* A report control block: "LLN0", "brcb01". Its logical node must have a leaf. */ @@ -165,7 +188,8 @@ struct zelos_mms_rcb { * The server's model: one logical device. The leaves go depth first: those of * one logical node together, of one functional constraint within it together, * and so on down the path. A time leaf stamps the values of its own data - * object (the path's first part). Data set members are not time leaves. + * object (the path's first part). A data attribute listed in a data set is + * not a time leaf. */ struct zelos_mms_model { /* The logical device: the MMS domain. */ @@ -198,6 +222,15 @@ int zelos_mms_start(const struct zelos_mms_model *model); /* A value may have changed: read them within 10 ms and report what did. Any context. */ void zelos_mms_poke(void); +/* + * The node's clock: UTC now in ms since 1970-01-01, and its TimeQuality. + * Changes from here on are stamped by it. Any context. + */ +void zelos_mms_clock(int64_t utc_ms, uint8_t quality); + +/* The UDS routine ZELOS_MMS_ROUTINE: option as above; returns the result's length. */ +uint8_t zelos_mms_routine(const uint8_t *option, uint8_t *result); + #endif /* CONFIG_ZELOS_MMS */ /* @@ -258,14 +291,28 @@ struct zelos_mms_conn { uint8_t buf[ZELOS_MMS_SPDU_MAX]; }; -/* One report as a buffered block keeps it; members by their place in the data set. */ +/* The values as read; when each last changed, UTC ms, and the clock's quality then. */ +struct zelos_mms_values { + union zelos_mms_value value[ZELOS_MMS_VALUES_MAX]; + int64_t changed[ZELOS_MMS_VALUES_MAX]; + uint8_t quality[ZELOS_MMS_VALUES_MAX]; +}; + +/* Members of a data set, by their place in it: those a value's change includes, a quality's. */ +struct zelos_mms_hits { + uint32_t dchg; + uint32_t qchg; +}; + +/* One report as a buffered block keeps it: its members and the values as they were. */ struct zelos_mms_entry { uint64_t id; + /* UTC ms. */ int64_t time; - uint32_t inclusion; + struct zelos_mms_hits hits; /* Every member's reason for inclusion, or 0: each one's own change. */ uint8_t reason; - union zelos_mms_value value[ZELOS_MMS_MEMBERS_MAX]; + struct zelos_mms_values at; }; struct zelos_mms_ring { @@ -303,7 +350,7 @@ struct zelos_mms_rcb_state { /* A general interrogation is due. */ bool gi; /* Members changed and not reported yet, since when, and when BufTm ends. */ - uint32_t pending; + struct zelos_mms_hits pending; int64_t pending_since; int64_t pending_until; int64_t integrity_at; @@ -321,9 +368,13 @@ struct zelos_mms_core { struct zelos_mms_conn conn[ZELOS_MMS_SOCKETS]; struct zelos_mms_rcb_state rcb[ZELOS_MMS_RCBS_MAX]; struct zelos_mms_ring ring[ZELOS_MMS_BUFFERED_MAX]; - /* The values as last read, and when each last changed. */ - union zelos_mms_value last[ZELOS_MMS_VALUES_MAX]; - int64_t changed[ZELOS_MMS_VALUES_MAX]; + /* UTC less now, ms, and the TimeQuality of a change stamped now. */ + int64_t ahead; + uint8_t time_quality; + /* The values as last read. */ + struct zelos_mms_values last; + /* The report being made: kept off the stack. */ + struct zelos_mms_entry entry; uint8_t out[ZELOS_MMS_FRAME_MAX]; }; @@ -358,6 +409,9 @@ int64_t zelos_mms_core_tick(struct zelos_mms_core *mms, int64_t now); /** A value may have changed: read them now, then as zelos_mms_core_tick(). */ int64_t zelos_mms_core_poke(struct zelos_mms_core *mms, int64_t now); +/** The clock: UTC is `ahead` ms ahead of `now`, with this TimeQuality. */ +void zelos_mms_core_clock(struct zelos_mms_core *mms, int64_t ahead, uint8_t quality); + /* Associated clients. */ uint8_t zelos_mms_core_clients(const struct zelos_mms_core *mms); diff --git a/include/zelos/uds.h b/include/zelos/uds.h index 21eff36..732c0e8 100644 --- a/include/zelos/uds.h +++ b/include/zelos/uds.h @@ -170,7 +170,7 @@ struct zelos_uds_io { }; #define ZELOS_UDS_ROUTINE_RESULT_MAX 8 -#define ZELOS_UDS_ROUTINE_OPTION_MAX 4 +#define ZELOS_UDS_ROUTINE_OPTION_MAX 8 /* * A node's routine: started with 0x31 0x01 and an option record of exactly diff --git a/lib/mms/core/mms_core.c b/lib/mms/core/mms_core.c index b0213fb..01c0118 100644 --- a/lib/mms/core/mms_core.c +++ b/lib/mms/core/mms_core.c @@ -73,13 +73,20 @@ int zelos_mms_core_init(struct zelos_mms_core *mms, const struct zelos_mms_model mms->model = model; mms->ops = ops; mms->ctx = ctx; + mms->time_quality = ZELOS_MMS_NO_CLOCK; /* The values at start, before any block looks at them: no change, no report. */ mms_scan(mms); - memset(mms->changed, 0, sizeof(mms->changed)); + memset(mms->last.changed, 0, sizeof(mms->last.changed)); mms_rcb_init(mms, boot); return 0; } +void zelos_mms_core_clock(struct zelos_mms_core *mms, int64_t ahead, uint8_t quality) +{ + mms->ahead = ahead; + mms->time_quality = quality; +} + int zelos_mms_core_accept(struct zelos_mms_core *mms, const uint8_t *peer, size_t peer_len, int64_t now) { diff --git a/lib/mms/core/mms_internal.h b/lib/mms/core/mms_internal.h index a60840c..7fe15a5 100644 --- a/lib/mms/core/mms_internal.h +++ b/lib/mms/core/mms_internal.h @@ -130,6 +130,12 @@ size_t mms_join(char *out, size_t cap, const char *a, char sep, const char *b); size_t mms_leaf_name(const struct zelos_mms_model *model, uint8_t leaf, char *out); /* The data set named item, or -1. */ int mms_data_set(const struct zelos_mms_model *model, const uint8_t *item, size_t len); +/* One past the last leaf of a data set's member m: its one leaf, or its data object's. */ +uint8_t mms_member_end(const struct zelos_mms_model *model, const struct zelos_mms_data_set *ds, + size_t m); +/* The member's item name, a data object's without its attributes; its length. */ +size_t mms_member_name(const struct zelos_mms_model *model, const struct zelos_mms_data_set *ds, + size_t m, char *out); enum { MMS_NONE, @@ -140,11 +146,16 @@ enum { /* What item names: MMS_LEAF fills *at. */ int mms_lookup(const struct zelos_mms_core *mms, const uint8_t *item, size_t len, struct mms_object *at); -/* The object item names, as Data or as a TypeSpecification. MMS_NONE writes nothing. */ +/* + * The object item names, as a TypeSpecification or as Data from the values at. + * MMS_NONE writes nothing. + */ int mms_object(const struct zelos_mms_core *mms, struct mms_enc *e, const uint8_t *item, size_t len, - bool type); -/* A model leaf's value as Data. */ -void mms_leaf_value(const struct zelos_mms_core *mms, struct mms_enc *e, uint8_t leaf); + bool type, const struct zelos_mms_values *at); +/* A data set member's value as Data, from the values at. */ +void mms_member_value(const struct zelos_mms_core *mms, struct mms_enc *e, + const struct zelos_mms_data_set *ds, size_t m, + const struct zelos_mms_values *at); const char *mms_rcb_attr(uint8_t attr); bool mms_rcb_has(const struct zelos_mms_rcb *rcb, uint8_t attr); diff --git a/lib/mms/core/mms_model.c b/lib/mms/core/mms_model.c index 8d2e885..b239e45 100644 --- a/lib/mms/core/mms_model.c +++ b/lib/mms/core/mms_model.c @@ -8,8 +8,6 @@ #include -/* TimeQuality: clock not synchronized, accuracy unspecified. */ -#define TIME_QUALITY 0x3F #define MSEC_PER_S 1000 size_t mms_join(char *out, size_t cap, const char *a, char sep, const char *b) @@ -170,6 +168,41 @@ int mms_data_set(const struct zelos_mms_model *model, const uint8_t *item, size_ return -1; } +/* Are two leaves of one data object? */ +static bool same_object(const struct zelos_mms_model *model, size_t a, size_t b) +{ + const struct zelos_mms_leaf *x = &model->leaves[a]; + const struct zelos_mms_leaf *y = &model->leaves[b]; + const size_t object = strcspn(x->path, "$"); + + return strcmp(x->ln, y->ln) == 0 && strcmp(x->fc, y->fc) == 0 && + strcspn(y->path, "$") == object && memcmp(x->path, y->path, object) == 0; +} + +uint8_t mms_member_end(const struct zelos_mms_model *model, const struct zelos_mms_data_set *ds, + size_t m) +{ + uint8_t end = ds->members[m] + 1; + + while (ds->objects && end < model->leaf_count && same_object(model, ds->members[m], end)) { + end++; + } + return end; +} + +size_t mms_member_name(const struct zelos_mms_model *model, const struct zelos_mms_data_set *ds, + size_t m, char *out) +{ + const struct zelos_mms_leaf *l = &model->leaves[ds->members[m]]; + size_t n = mms_leaf_name(model, ds->members[m], out); + + if (ds->objects) { + n = strlen(l->ln) + 1 + strlen(l->fc) + 1 + strcspn(l->path, "$"); + out[n] = 0; + } + return n; +} + int mms_model_check(const struct zelos_mms_model *model) { struct mms_iter a = {0}; @@ -199,8 +232,12 @@ int mms_model_check(const struct zelos_mms_model *model) return -EINVAL; } for (size_t m = 0; m < ds->count; m++) { - if (ds->members[m] >= model->leaf_count || - model->leaves[ds->members[m]].type == ZELOS_MMS_TIME) { + const uint8_t leaf = ds->members[m]; + + /* A data attribute, no time; or a data object from its first leaf. */ + if (leaf >= model->leaf_count || + (ds->objects ? leaf > 0 && same_object(model, leaf - 1, leaf) + : model->leaves[leaf].type == ZELOS_MMS_TIME)) { return -EINVAL; } } @@ -268,45 +305,46 @@ int mms_lookup(const struct zelos_mms_core *mms, const uint8_t *item, size_t len return MMS_NONE; } -/* When a value of the leaf's data object last changed. */ -static int64_t stamp(const struct zelos_mms_core *mms, uint8_t leaf) +/* A time leaf: when a value of its data object last changed, by the clock as it was then. */ +static void stamp(const struct zelos_mms_core *mms, struct mms_enc *e, uint8_t leaf, + const struct zelos_mms_values *at) { - const struct zelos_mms_leaf *t = &mms->model->leaves[leaf]; - const size_t object = strcspn(t->path, "$"); - int64_t at = 0; + int64_t ms = 0; + uint8_t quality = mms->time_quality; + uint32_t s; + uint32_t frac; for (size_t i = 0; i < mms->model->leaf_count; i++) { - const struct zelos_mms_leaf *l = &mms->model->leaves[i]; + const uint8_t value = mms->model->leaves[i].value; - if (l->value < mms->model->value_count && strcmp(l->ln, t->ln) == 0 && - strcmp(l->fc, t->fc) == 0 && strcspn(l->path, "$") == object && - memcmp(l->path, t->path, object) == 0 && mms->changed[l->value] > at) { - at = mms->changed[l->value]; + if (value < mms->model->value_count && same_object(mms->model, leaf, i) && + at->changed[value] > ms) { + ms = at->changed[value]; + quality = at->quality[value]; } } - return at; + s = (uint32_t)(ms / MSEC_PER_S); + /* The fraction of a second in 24 bits. */ + frac = (uint32_t)(((uint64_t)(ms % MSEC_PER_S) << 24) / MSEC_PER_S); + mms_tlv(e, MMS_UTC_TIME, + (uint8_t[]){(uint8_t)(s >> 24), (uint8_t)(s >> 16), (uint8_t)(s >> 8), (uint8_t)s, + (uint8_t)(frac >> 16), (uint8_t)(frac >> 8), (uint8_t)frac, quality}, + 8); } -void mms_leaf_value(const struct zelos_mms_core *mms, struct mms_enc *e, uint8_t leaf) +/* A model leaf's value as Data, from the values at. */ +static void leaf_value(const struct zelos_mms_core *mms, struct mms_enc *e, uint8_t leaf, + const struct zelos_mms_values *at) { const struct zelos_mms_leaf *l = &mms->model->leaves[leaf]; union zelos_mms_value v = {0}; if (l->type == ZELOS_MMS_TIME) { - const int64_t ms = stamp(mms, leaf); - const uint32_t s = (uint32_t)(ms / MSEC_PER_S); - /* The fraction of a second in 24 bits. */ - const uint32_t frac = (uint32_t)(((uint64_t)(ms % MSEC_PER_S) << 24) / MSEC_PER_S); - - mms_tlv(e, MMS_UTC_TIME, - (uint8_t[]){(uint8_t)(s >> 24), (uint8_t)(s >> 16), (uint8_t)(s >> 8), - (uint8_t)s, (uint8_t)(frac >> 16), (uint8_t)(frac >> 8), - (uint8_t)frac, TIME_QUALITY}, - 8); + stamp(mms, e, leaf, at); return; } if (l->value != ZELOS_MMS_NO_VALUE) { - v = mms->last[l->value]; + v = at->value[l->value]; } mms_value(e, l->type, v); } @@ -347,7 +385,7 @@ static void leaf_type(struct mms_enc *e, uint8_t type) } static void leaf(const struct zelos_mms_core *mms, struct mms_enc *e, const struct mms_object *at, - bool type) + bool type, const struct zelos_mms_values *values) { if (at->leaf < 0) { if (type) { @@ -358,7 +396,7 @@ static void leaf(const struct zelos_mms_core *mms, struct mms_enc *e, const stru } else if (type) { leaf_type(e, mms->model->leaves[at->leaf].type); } else { - mms_leaf_value(mms, e, (uint8_t)at->leaf); + leaf_value(mms, e, (uint8_t)at->leaf, values); } } @@ -391,8 +429,23 @@ static void close_structure(struct mms_enc *e, bool named, bool type) } } +void mms_member_value(const struct zelos_mms_core *mms, struct mms_enc *e, + const struct zelos_mms_data_set *ds, size_t m, + const struct zelos_mms_values *at) +{ + char name[ZELOS_MMS_NAME_MAX + 1]; + + if (ds->objects) { + const size_t n = mms_member_name(mms->model, ds, m, name); + + (void)mms_object(mms, e, (const uint8_t *)name, n, false, at); + } else { + leaf_value(mms, e, ds->members[m], at); + } +} + int mms_object(const struct zelos_mms_core *mms, struct mms_enc *e, const uint8_t *item, size_t len, - bool type) + bool type, const struct zelos_mms_values *at) { struct mms_iter it = {0}; char prev[ZELOS_MMS_NAME_MAX + 1]; @@ -417,7 +470,7 @@ int mms_object(const struct zelos_mms_core *mms, struct mms_enc *e, const uint8_ continue; } if (it.name[len] == 0) { - leaf(mms, e, &it.at, type); + leaf(mms, e, &it.at, type, at); return MMS_LEAF; } if (!found) { @@ -441,7 +494,7 @@ int mms_object(const struct zelos_mms_core *mms, struct mms_enc *e, const uint8_ mms_tlv(e, 0x80, name, n); mms_begin(e, 0xA1); } - leaf(mms, e, &it.at, type); + leaf(mms, e, &it.at, type, at); if (type) { mms_end(e); mms_end(e); diff --git a/lib/mms/core/mms_report.c b/lib/mms/core/mms_report.c index e4b8817..5cda653 100644 --- a/lib/mms/core/mms_report.c +++ b/lib/mms/core/mms_report.c @@ -28,6 +28,8 @@ #define BRCB 2 #define MSEC_PER_DAY 86400000 #define MSEC_PER_S 1000 +/* 1970-01-01 to 1984-01-01. */ +#define EPOCH_1984_MS (5113LL * MSEC_PER_DAY) static const struct { const char *name; @@ -103,9 +105,13 @@ void mms_rcb_init(struct zelos_mms_core *mms, uint32_t boot) } } -/* BINARY-TIME with a date: ms of the day, then days since 1984-01-01. */ -static void binary_time(struct mms_enc *e, int64_t ms) +/* + * BINARY-TIME with a date: ms of the day, then days since 1984-01-01. A time + * before 1984 is one without a clock, since boot: taken from 1984 as it is. + */ +static void binary_time(struct mms_enc *e, int64_t utc_ms) { + const int64_t ms = utc_ms >= EPOCH_1984_MS ? utc_ms - EPOCH_1984_MS : utc_ms; const uint32_t of_day = (uint32_t)(ms % MSEC_PER_DAY); const uint16_t days = (uint16_t)(ms / MSEC_PER_DAY); @@ -226,18 +232,36 @@ void mms_rcb_type(struct mms_enc *e, const struct zelos_mms_rcb *rcb, uint8_t at } } -static uint32_t all_members(const struct zelos_mms_data_set *ds) +static struct zelos_mms_hits all_members(const struct zelos_mms_data_set *ds) { - return (uint32_t)((1ULL << ds->count) - 1); + return (struct zelos_mms_hits){.dchg = (uint32_t)((1ULL << ds->count) - 1)}; } -/* The most a member's value takes as Data. */ -static size_t value_max(uint8_t type) +static uint32_t members(struct zelos_mms_hits hits) { - return type == ZELOS_MMS_BOOLEAN ? 3 - : type == ZELOS_MMS_DBPOS ? 4 - : type == ZELOS_MMS_QUALITY ? 5 - : 7; + return hits.dchg | hits.qchg; +} + +/* The most a member's value takes as Data; for a data object an upper bound. */ +static size_t member_max(const struct zelos_mms_model *model, const struct zelos_mms_data_set *ds, + size_t m) +{ + size_t n = 0; + + for (uint8_t i = ds->members[m]; i < mms_member_end(model, ds, m); i++) { + const uint8_t type = model->leaves[i].type; + + n += type == ZELOS_MMS_BOOLEAN ? 3 + : type == ZELOS_MMS_DBPOS ? 4 + : type == ZELOS_MMS_QUALITY ? 5 + : type == ZELOS_MMS_TIME ? 10 + : 7; + /* A structure for every level above the leaf, its length in up to 3 bytes. */ + for (const char *p = model->leaves[i].path; ds->objects && *p != 0; p++) { + n += *p == '$' ? 4 : 0; + } + } + return n; } /* The block's largest report as an MMS PDU: every member, each value at its longest. */ @@ -259,9 +283,9 @@ static size_t report_max(const struct zelos_mms_core *mms, uint8_t r) n += (st->opt_flds & OPT_CONF_REV) != 0 ? 7 : 0; for (size_t m = 0; m < ds->count; m++) { if ((st->opt_flds & OPT_DATA_REF) != 0) { - n += 3 + ld + 1 + mms_leaf_name(model, ds->members[m], name); + n += 3 + ld + 1 + mms_member_name(model, ds, m, name); } - n += value_max(model->leaves[ds->members[m]].type); + n += member_max(model, ds, m); n += (st->opt_flds & OPT_REASON) != 0 ? 4 : 0; } return n; @@ -279,6 +303,7 @@ static void send_report(struct zelos_mms_core *mms, uint8_t r, const struct zelo (ring != NULL ? OPT_FLDS : OPT_FLDS & ~(OPT_BUF_OVFL | OPT_ENTRY_ID)); struct mms_enc e = {.buf = &mms->out[ZELOS_MMS_HEAD], .cap = mms->conn[st->conn].pdu_size}; uint8_t bits[1 + (ZELOS_MMS_MEMBERS_MAX + 7) / 8] = {(uint8_t)((8 - ds->count % 8) % 8)}; + const uint32_t inclusion = members(en->hits); char ref[ZELOS_MMS_REF_MAX + 1]; char name[ZELOS_MMS_NAME_MAX + 1]; @@ -311,28 +336,31 @@ static void send_report(struct zelos_mms_core *mms, uint8_t r, const struct zelo mms_int(&e, MMS_UNSIGNED, model->rcbs[r].conf_rev); } for (size_t m = 0; m < ds->count; m++) { - if ((en->inclusion >> m & 1) != 0) { + if ((inclusion >> m & 1) != 0) { bits[1 + m / 8] |= (uint8_t)(0x80 >> (m % 8)); } } mms_tlv(&e, MMS_BIT_STRING, bits, 1 + (ds->count + 7) / 8); for (size_t m = 0; m < ds->count && (opt & OPT_DATA_REF) != 0; m++) { - if ((en->inclusion >> m & 1) != 0) { - mms_leaf_name(model, ds->members[m], name); + if ((inclusion >> m & 1) != 0) { + mms_member_name(model, ds, m, name); mms_join(ref, sizeof(ref), model->ld, '/', name); mms_str(&e, MMS_VISIBLE_STRING, ref); } } for (size_t m = 0; m < ds->count; m++) { - if ((en->inclusion >> m & 1) != 0) { - mms_value(&e, model->leaves[ds->members[m]].type, en->value[m]); + if ((inclusion >> m & 1) != 0) { + mms_member_value(mms, &e, ds, m, &en->at); } } for (size_t m = 0; m < ds->count && (opt & OPT_REASON) != 0; m++) { - const bool quality = model->leaves[ds->members[m]].type == ZELOS_MMS_QUALITY; - const uint8_t reason = en->reason != 0 ? en->reason : quality ? QCHG : DCHG; + uint8_t reason = en->reason; - if ((en->inclusion >> m & 1) != 0) { + if (reason == 0) { + reason = ((en->hits.dchg >> m & 1) != 0 ? DCHG : 0) | + ((en->hits.qchg >> m & 1) != 0 ? QCHG : 0); + } + if ((inclusion >> m & 1) != 0) { mms_tlv(&e, MMS_BIT_STRING, (uint8_t[]){1, reason}, 2); } } @@ -350,24 +378,15 @@ static void send_report(struct zelos_mms_core *mms, uint8_t r, const struct zelo } } -/* A report of the members in inclusion, their values as last read. */ -static void fill(const struct zelos_mms_core *mms, uint8_t r, struct zelos_mms_entry *en, - uint32_t inclusion, uint8_t reason, int64_t time) +/* A report of the members in hits, made at `time`, the values as last read. */ +static void fill(const struct zelos_mms_core *mms, struct zelos_mms_entry *en, + struct zelos_mms_hits hits, uint8_t reason, int64_t time) { - const struct zelos_mms_model *model = mms->model; - const struct zelos_mms_data_set *ds = &model->data_sets[mms->rcb[r].data_set]; - - memset(en, 0, sizeof(*en)); - en->time = time; - en->inclusion = inclusion; + en->id = 0; + en->time = time + mms->ahead; + en->hits = hits; en->reason = reason; - for (size_t m = 0; m < ds->count; m++) { - const uint8_t value = model->leaves[ds->members[m]].value; - - if (value < model->value_count) { - en->value[m] = mms->last[value]; - } - } + en->at = mms->last; } /* Buffered: into the ring, the oldest entry making room. Unbuffered: to the client now. */ @@ -402,19 +421,19 @@ static void emit(struct zelos_mms_core *mms, uint8_t r, const struct zelos_mms_e static void flush(struct zelos_mms_core *mms, uint8_t r) { struct zelos_mms_rcb_state *st = &mms->rcb[r]; - struct zelos_mms_entry en; + struct zelos_mms_entry *en = &mms->entry; - if (st->pending == 0) { + if (members(st->pending) == 0) { return; } - fill(mms, r, &en, st->pending, 0, st->pending_since); - st->pending = 0; - emit(mms, r, &en); + fill(mms, en, st->pending, 0, st->pending_since); + st->pending = (struct zelos_mms_hits){0}; + emit(mms, r, en); } static void purge(struct zelos_mms_rcb_state *st) { - st->pending = 0; + st->pending = (struct zelos_mms_hits){0}; if (st->ring != NULL) { st->ring->count = 0; st->ring->unsent = 0; @@ -447,7 +466,7 @@ static void disable(struct zelos_mms_rcb_state *st) st->enabled = false; st->gi = false; if (st->ring == NULL) { - st->pending = 0; + st->pending = (struct zelos_mms_hits){0}; } } @@ -730,28 +749,34 @@ static union zelos_mms_value clean(uint8_t type, union zelos_mms_value v) } /* The block's members among the values that changed, by its trigger options. */ -static uint32_t triggered(const struct zelos_mms_core *mms, uint8_t r, const bool *changed) +static struct zelos_mms_hits triggered(const struct zelos_mms_core *mms, uint8_t r, + const bool *changed) { const struct zelos_mms_model *model = mms->model; const struct zelos_mms_rcb_state *st = &mms->rcb[r]; const struct zelos_mms_data_set *ds; - uint32_t hit = 0; + struct zelos_mms_hits hits = {0}; /* An unbuffered block that is not enabled notes nothing. */ if (st->data_set == ZELOS_MMS_NO_DATA_SET || (st->ring == NULL && !st->enabled)) { - return 0; + return hits; } ds = &model->data_sets[st->data_set]; for (size_t m = 0; m < ds->count; m++) { - const struct zelos_mms_leaf *l = &model->leaves[ds->members[m]]; - const uint8_t trigger = l->type == ZELOS_MMS_QUALITY ? QCHG : DCHG; + for (uint8_t i = ds->members[m]; i < mms_member_end(model, ds, m); i++) { + const struct zelos_mms_leaf *l = &model->leaves[i]; - if (l->value < model->value_count && changed[l->value] && - (st->trg_ops & trigger) != 0) { - hit |= 1U << m; + if (l->value >= model->value_count || !changed[l->value]) { + continue; + } + if (l->type == ZELOS_MMS_QUALITY) { + hits.qchg |= (st->trg_ops & QCHG) != 0 ? 1U << m : 0; + } else { + hits.dchg |= (st->trg_ops & DCHG) != 0 ? 1U << m : 0; + } } } - return hit; + return hits; } void mms_scan(struct zelos_mms_core *mms) @@ -769,8 +794,8 @@ void mms_scan(struct zelos_mms_core *mms) if (l->value < model->value_count) { now[l->value] = clean(l->type, now[l->value]); /* By its bytes: a NaN that stays is no change. */ - changed[l->value] = - memcmp(&now[l->value], &mms->last[l->value], sizeof(now[0])) != 0; + changed[l->value] = memcmp(&now[l->value], &mms->last.value[l->value], + sizeof(now[0])) != 0; any = any || changed[l->value]; } } @@ -779,25 +804,27 @@ void mms_scan(struct zelos_mms_core *mms) } /* A member that changes again while its first change waits: that report goes first. */ for (uint8_t r = 0; r < model->rcb_count; r++) { - if ((triggered(mms, r, changed) & mms->rcb[r].pending) != 0) { + if ((members(triggered(mms, r, changed)) & members(mms->rcb[r].pending)) != 0) { flush(mms, r); } } for (size_t i = 0; i < model->value_count; i++) { if (changed[i]) { - mms->last[i] = now[i]; - mms->changed[i] = mms->now; + mms->last.value[i] = now[i]; + mms->last.changed[i] = mms->now + mms->ahead; + mms->last.quality[i] = mms->time_quality; } } for (uint8_t r = 0; r < model->rcb_count; r++) { struct zelos_mms_rcb_state *st = &mms->rcb[r]; - const uint32_t hit = triggered(mms, r, changed); + const struct zelos_mms_hits hits = triggered(mms, r, changed); - if (hit != 0 && st->pending == 0) { + if (members(hits) != 0 && members(st->pending) == 0) { st->pending_since = mms->now; st->pending_until = mms->now + st->buf_tm; } - st->pending |= hit; + st->pending.dchg |= hits.dchg; + st->pending.qchg |= hits.qchg; } } @@ -809,7 +836,7 @@ int64_t mms_report_run(struct zelos_mms_core *mms) for (uint8_t r = 0; r < mms->model->rcb_count; r++) { struct zelos_mms_rcb_state *st = &mms->rcb[r]; struct zelos_mms_ring *ring = st->ring; - struct zelos_mms_entry en; + struct zelos_mms_entry *en = &mms->entry; bool integrity; if (st->conn < 0 && st->hold_until != 0 && now >= st->hold_until) { @@ -821,9 +848,9 @@ int64_t mms_report_run(struct zelos_mms_core *mms) flush(mms, r); } if (integrity && st->enabled && now >= st->integrity_at) { - fill(mms, r, &en, all_members(&mms->model->data_sets[st->data_set]), - INTEGRITY, now); - emit(mms, r, &en); + fill(mms, en, all_members(&mms->model->data_sets[st->data_set]), INTEGRITY, + now); + emit(mms, r, en); st->integrity_at += st->intg_pd; /* Late: on from now, no catch-up burst. */ if (st->integrity_at <= now) { @@ -839,19 +866,18 @@ int64_t mms_report_run(struct zelos_mms_core *mms) send_report(mms, r, &ring->entry[at]); } if (st->enabled && st->gi && (ring == NULL || ring->unsent == 0)) { - fill(mms, r, &en, all_members(&mms->model->data_sets[st->data_set]), GI, - now); + fill(mms, en, all_members(&mms->model->data_sets[st->data_set]), GI, now); if (ring != NULL) { - en.id = ++ring->last; + en->id = ++ring->last; } st->gi = false; - send_report(mms, r, &en); + send_report(mms, r, en); } if (st->conn < 0 && st->hold_until != 0) { next = MMS_MIN(next, st->hold_until); } - if (st->pending != 0) { + if (members(st->pending) != 0) { next = MMS_MIN(next, st->pending_until); } if (st->enabled && (st->trg_ops & INTEGRITY) != 0 && st->intg_pd > 0) { diff --git a/lib/mms/core/mms_service.c b/lib/mms/core/mms_service.c index aa09def..a88d23b 100644 --- a/lib/mms/core/mms_service.c +++ b/lib/mms/core/mms_service.c @@ -263,7 +263,7 @@ static void read_variables(struct zelos_mms_core *mms, int s, const struct mms_t return; } for (size_t m = 0; m < model->data_sets[ds].count; m++) { - mms_leaf_value(mms, &e, model->data_sets[ds].members[m]); + mms_member_value(mms, &e, &model->data_sets[ds], m, &mms->last); } } for (p = spec.v; spec.tag == 0xA0 && p < spec.v + spec.len;) { @@ -279,7 +279,7 @@ static void read_variables(struct zelos_mms_core *mms, int s, const struct mms_t /* Alternate access. */ failure(&e, MMS_ACCESS_UNSUPPORTED); } else if (!object_item(mms, &name, &item) || - mms_object(mms, &e, item.v, item.len, false) == MMS_NONE) { + mms_object(mms, &e, item.v, item.len, false, &mms->last) == MMS_NONE) { failure(&e, MMS_NON_EXISTENT); } } @@ -384,7 +384,7 @@ static void get_variable_access_attributes(struct zelos_mms_core *mms, int s, mms_bool(&e, 0x80, false); mms_begin(&e, 0xA2); if (!mms_find(req, 0xA0, &name) || !object_item(mms, &name, &item) || - mms_object(mms, &e, item.v, item.len, true) == MMS_NONE) { + mms_object(mms, &e, item.v, item.len, true, NULL) == MMS_NONE) { error(mms, s, id, ERROR_DEFINITION, OBJECT_UNDEFINED); return; } @@ -411,7 +411,7 @@ static void get_named_variable_list_attributes(struct zelos_mms_core *mms, int s mms_bool(&e, 0x80, false); mms_begin(&e, 0xA1); for (size_t m = 0; m < model->data_sets[ds].count; m++) { - mms_leaf_name(model, model->data_sets[ds].members[m], name); + mms_member_name(model, &model->data_sets[ds], m, name); mms_begin(&e, 0x30); mms_begin(&e, 0xA0); mms_begin(&e, 0xA1); diff --git a/lib/mms/zephyr/mms.c b/lib/mms/zephyr/mms.c index 529ccf0..b45accb 100644 --- a/lib/mms/zephyr/mms.c +++ b/lib/mms/zephyr/mms.c @@ -6,7 +6,8 @@ * blocking send waits CONFIG_NET_SOCKET_MAX_SEND_WAIT, 10 s, unless * CONFIG_NET_CONTEXT_SNDTIMEO is built in and SO_SNDTIMEO set. TCP keepalive * probes a silent client every 5 s and closes one that does not answer three - * probes a second apart. zelos_mms_poke() is served within POLL_MS. + * probes a second apart. zelos_mms_poke() and zelos_mms_clock() are served + * within POLL_MS. */ #include @@ -19,7 +20,9 @@ #include #include #include +#include #include +#include LOG_MODULE_REGISTER(zelos_mms, LOG_LEVEL_INF); @@ -48,11 +51,48 @@ static atomic_t poked; /* Set by the reboot hook: the MMS thread touches no socket after. */ static atomic_t rebooting; +/* The clock as zelos_mms_clock() gave it, for the MMS thread to hand the core. */ +static struct k_spinlock clock_lock; +static int64_t clock_ahead; +static uint8_t clock_quality = ZELOS_MMS_NO_CLOCK; +static atomic_t clock_new; + void zelos_mms_poke(void) { atomic_set(&poked, 1); } +void zelos_mms_clock(int64_t utc_ms, uint8_t quality) +{ + const int64_t ahead = utc_ms - k_uptime_get(); + const k_spinlock_key_t key = k_spin_lock(&clock_lock); + + clock_ahead = ahead; + clock_quality = quality; + k_spin_unlock(&clock_lock, key); + atomic_set(&clock_new, 1); +} + +static int64_t clock_get(uint8_t *quality) +{ + const k_spinlock_key_t key = k_spin_lock(&clock_lock); + const int64_t ahead = clock_ahead; + + *quality = clock_quality; + k_spin_unlock(&clock_lock, key); + return ahead; +} + +uint8_t zelos_mms_routine(const uint8_t *option, uint8_t *result) +{ + const int64_t utc_ms = (int64_t)sys_get_be48(&option[1]); + + /* No time: time since boot. */ + zelos_mms_clock(utc_ms != 0 ? utc_ms : k_uptime_get(), option[0]); + sys_put_be48((uint64_t)(k_uptime_get() + clock_get(&result[0])), &result[1]); + return ZELOS_MMS_ROUTINE_OPTION; +} + static bool send_all(void *ctx, int s, const uint8_t *data, size_t len) { ARG_UNUSED(ctx); @@ -174,6 +214,12 @@ static void run(void *p1, void *p2, void *p3) if (atomic_get(&rebooting)) { k_sleep(K_FOREVER); } + if (atomic_clear(&clock_new)) { + uint8_t quality; + const int64_t ahead = clock_get(&quality); + + zelos_mms_core_clock(&core, ahead, quality); + } up = zelos_eth_ipv4_usable(zelos_eth_ipv4()); if (up && listener < 0) { listener = open_listener(); diff --git a/nodes/dcdc/src/uds.c b/nodes/dcdc/src/uds.c index f82ff8a..11b9b28 100644 --- a/nodes/dcdc/src/uds.c +++ b/nodes/dcdc/src/uds.c @@ -33,6 +33,8 @@ * while DTC n below is failing; 0x00 passed. * Routine 0x6185, with lib/goose: GOOSE test knobs, extended, one option byte * (include/zelos/goose.h). + * Routine 0x6186, with lib/mms: the MMS server's clock, extended, seven option + * bytes (include/zelos/mms.h). * DTCs, from the model, each tested every 50 ms cycle (SAE J2012 generic * numbers, failure type 00): * 0 0x0A9400 P0A94 DC/DC converter performance: temperature above 70 degC @@ -71,6 +73,7 @@ #include #include #include +#include #include #include @@ -327,6 +330,10 @@ static const struct zelos_uds_routine routines[] = { #if defined(CONFIG_ZELOS_GOOSE) {ZELOS_GOOSE_ROUTINE, ZELOS_UDS_SESSION_EXTENDED, 0, zelos_goose_routine, 1}, #endif +#if defined(CONFIG_ZELOS_MMS) + {ZELOS_MMS_ROUTINE, ZELOS_UDS_SESSION_EXTENDED, 0, zelos_mms_routine, + ZELOS_MMS_ROUTINE_OPTION}, +#endif }; static const struct zelos_uds_memory memories[] = { diff --git a/nodes/full/src/mms.c b/nodes/full/src/mms.c index a96a7ba..cd6f388 100644 --- a/nodes/full/src/mms.c +++ b/nodes/full/src/mms.c @@ -4,8 +4,9 @@ * value, q and t. * * LLN0: Beh (on); the data sets ds_pdu and ds_dcdc, GOOSE's own, members in - * the same order; report control blocks brcb_pdu01 (buffered, ds_pdu) and - * urcb_dcdc01 (unbuffered, ds_dcdc). + * the same order, and ds_pos, the channels' Pos as whole data objects; + * report control blocks brcb_pdu01 (buffered, ds_pdu), urcb_dcdc01 + * (unbuffered, ds_dcdc) and brcb_pos01 (buffered, ds_pos). * XSWI1-8 Pos (Dbpos), PTRC1-8 Tr, EmcyGGIO1 IntIn1: the PDU's channels * powered, their trip latches, overcurrent EMCYs since boot. Read when the * PDU says they changed. @@ -66,15 +67,21 @@ static const uint8_t ds_pdu[] = { LEAF(10), LEAF(11), LEAF(12), LEAF(13), LEAF(14), LEAF(15), LEAF(16), LEAF(17), }; static const uint8_t ds_dcdc[] = {LEAF(18), LEAF(19), LEAF(20), LEAF(21), LEAF(22)}; +/* XSWI1-8 Pos, each with its stVal, q and t. */ +static const uint8_t ds_pos[] = { + LEAF(1), LEAF(2), LEAF(3), LEAF(4), LEAF(5), LEAF(6), LEAF(7), LEAF(8), +}; static const struct zelos_mms_data_set data_sets[] = { {"LLN0$ds_pdu", ds_pdu, ARRAY_SIZE(ds_pdu)}, {"LLN0$ds_dcdc", ds_dcdc, ARRAY_SIZE(ds_dcdc)}, + {"LLN0$ds_pos", ds_pos, ARRAY_SIZE(ds_pos), true}, }; static const struct zelos_mms_rcb rcbs[] = { {"LLN0", "brcb_pdu01", true, 0, 1}, {"LLN0", "urcb_dcdc01", false, 1, 1}, + {"LLN0", "brcb_pos01", true, 2, 1}, }; static void read_values(union zelos_mms_value *v) diff --git a/nodes/full/zelosfull.cid b/nodes/full/zelosfull.cid index 943565b..34043a5 100644 --- a/nodes/full/zelosfull.cid +++ b/nodes/full/zelosfull.cid @@ -2,7 +2,8 @@ + Published as GOOSE and served over MMS, with one report control block each. + ds_pos: channels 1-8 Pos as whole data objects (stVal, q, t), reported only. -->
@@ -62,6 +63,16 @@ + + + + + + + + + + @@ -73,6 +84,11 @@ + + + + + diff --git a/testing/suites/mms/test_server.py b/testing/suites/mms/test_server.py index 8d78b42..f60af4a 100644 --- a/testing/suites/mms/test_server.py +++ b/testing/suites/mms/test_server.py @@ -3,16 +3,19 @@ What is tested, one visible behaviour per test: the model is the station file's (nodes/full/zelosfull.cid), a data object reads with its quality and time, the -unbuffered block reports the DC-DC as it moves, and the buffered block replays -a PDU change made while no client was connected. +unbuffered block reports the DC-DC as it moves, the buffered block replays +a PDU change made while no client was connected, and the block of whole data +objects reports a change with its quality and time, by the clock the node is +given over UDS. -The last test switches a PDU channel over CANopen, so it takes the node from a +The last two switch a PDU channel over CANopen, so they take the node from a bench's own CANopen manager, as testing/suites/canopen does. On Renode, from the repository root: just test-bench mms can-full/build/full.elf """ +import contextlib import dataclasses import queue import socket @@ -27,6 +30,7 @@ from zelos_testing.bench_plugin import eth_link from zelos_testing.frames import bound +from zelos_testing.uds import DEFAULT, EXTENDED, UdsEcu ROOT = Path(__file__).resolve().parents[3] CID = ROOT / "nodes" / "full" / "zelosfull.cid" @@ -36,6 +40,10 @@ LD = "ZELOSFULL" BRCB = ObjectName("LLN0$BR$brcb_pdu01", LD) URCB = ObjectName("LLN0$RP$urcb_dcdc01", LD) +POS = ObjectName("LLN0$BR$brcb_pos01", LD) +# The clock knob (include/zelos/mms.h): a TimeQuality octet, then UTC ms. Synchronized to the +# millisecond, and the node's own: time since boot, not synchronized. +ROUTINE, SYNCHRONIZED, NO_CLOCK = 0x6186, 0x0A, 0x3F # Reports on a change and on general interrogation, one asked for when enabling; no integrity # period. The same settings every time: changing them empties a buffered block. SETTINGS = rcb.RcbSettings(trg_ops=TrgOps(data_change=True, quality_change=True, general_interrogation=True), intg_pd_ms=0) @@ -46,7 +54,7 @@ # The PDU's CANopen node, and channel 2's bit in its outputs (0x6200). PDU_NODE = 0x20 CHANNEL_2 = 0b0000_0010 -# ds_pdu's member for channel 2: XSWI2.Pos.stVal, a Dbpos. +# ds_pdu's and ds_pos's member for channel 2: XSWI2.Pos.stVal, a Dbpos, and XSWI2.Pos. XSWI2 = 1 DBPOS_OFF, DBPOS_ON = 0b01, 0b10 @@ -139,8 +147,9 @@ def test_model_is_the_station_file(reports): client = reports.client ln0 = station() for data_set in ln0.findall("scl:DataSet", SCL): + # A member without daName is the data object whole. want = [ - item(f"{m.get('prefix')}{m.get('lnClass')}{m.get('lnInst')}", m.get("fc"), m.get("doName"), *m.get("daName").split(".")) + item(f"{m.get('prefix')}{m.get('lnClass')}{m.get('lnInst')}", m.get("fc"), m.get("doName"), *(m.get("daName").split(".") if m.get("daName") else ())) for m in data_set.findall("scl:FCDA", SCL) ] assert client.get_data_set_members(item("LLN0", data_set.get("name"))) == want @@ -212,3 +221,36 @@ def test_buffered_block_replays_a_change_made_while_away(host, pdu): finally: rcb.disable(second.client, BRCB) second.client.close() + + +def test_data_objects_report_with_the_time_of_the_change(host, pdu, pytestconfig): + interface = pytestconfig.getoption("interface") + interface = "zelos-socketcan" if interface == "socketcan" else interface + with UdsEcu(interface, pytestconfig.getoption("channel"), timeout_s=bound(2.0)) as ecu: + + def clock(quality: int, utc_ms: int) -> None: + ecu.client.start_routine(ROUTINE, bytes([quality]) + utc_ms.to_bytes(6, "big")) + + reports = Reports(host) + try: + rcb.enable(reports.client, POS, dataclasses.replace(SETTINGS, purge_buf=True)) + reports.until(is_gi) + # The host's clock, synchronized: the node counts on from it. + ecu.enter(EXTENDED) + before = time.time() + clock(SYNCHRONIZED, int(before * 1000)) + pdu.raw = CHANNEL_2 + on = reports.until(lambda r: any(e.reason.data_change for e in r.entries))[-1] + after = time.time() + finally: + with contextlib.suppress(Exception): + clock(NO_CLOCK, 0) + ecu.client.change_session(DEFAULT) + with contextlib.suppress(Exception): + rcb.disable(reports.client, POS) + reports.client.close() + # Channel 2's Pos alone: stVal, q (good) and t, when it switched. + (entry,) = on.entries + st_val, q, t = entry.value.members + assert (entry.index, dbpos(st_val), any(q.value)) == (XSWI2, DBPOS_ON, False) + assert t.quality == SYNCHRONIZED and before - 0.001 <= t.value.timestamp() <= after, t diff --git a/tests/lib/mms/src/main.c b/tests/lib/mms/src/main.c index 6e3d72a..4e6a115 100644 --- a/tests/lib/mms/src/main.c +++ b/tests/lib/mms/src/main.c @@ -968,6 +968,48 @@ ZTEST(mms, test_any_split_and_bad_input) zassert_false(closed[a]); } +ZTEST(mms, test_data_objects) +{ + /* Ind1 and Vol whole: the value, q and t. */ + static const uint8_t whole[] = {3, 6}; + static const struct zelos_mms_data_set sets[] = {{"LLN0$ds", whole, 2, true}}; + /* 0x65000000 s UTC at 2000 ms: 0x014FF000 ms into day 0x38A3 from 1984-01-01. */ + const int64_t ahead = 0x65000000LL * 1000 - 2000; + struct zelos_mms_model objects = model; + int s; + + objects.data_sets = sets; + zassert_ok(zelos_mms_core_init(&mms, &objects, 0, &ops, NULL)); + /* A synchronized clock, to the millisecond. */ + zelos_mms_core_clock(&mms, ahead, 0x0A); + s = associate(peer, 0); + /* Sequence number, time stamp, reason and data reference. */ + WRITE_ATTR(s, "LLN0$RP$urcb01$OptFlds", WRITTEN, 0x84, 0x03, 0x06, 0x74, 0x00); + WRITE_ATTR(s, "LLN0$RP$urcb01$RptEna", WRITTEN, B_TRUE); + sends[s] = 0; + + /* A value and its quality change: the object whole, named to it, both reasons, t then. */ + now = 2000; + values[IND].boolean = true; + values[IND_Q].quality = 0x4000; + zelos_mms_core_poke(&mms, now); + zassert_equal(sends[s], 1); + REPORT(s, 0, URCB_ID, 0x84, 0x03, 0x06, 0x74, 0x00, UNSIGNED(0), 0x8C, 0x06, 0x01, 0x4F, + 0xF0, 0x00, 0x38, 0xA3, 0x84, 0x02, 0x06, 0x80, 0x8A, 0x11, 'I', 'E', 'D', '/', 'G', + 'G', 'I', 'O', '1', '$', 'S', 'T', '$', 'I', 'n', 'd', '1', 0xA2, 0x12, B_TRUE, 0x84, + 0x03, 0x03, 0x40, 0x00, 0x91, 0x08, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, + 0x84, 0x02, 0x01, 0x60); + + /* The clock is lost: Ind1's t keeps the quality it was stamped with; Vol never changed. */ + zelos_mms_core_clock(&mms, ahead, ZELOS_MMS_NO_CLOCK); + REQUEST(s, 0xA0, 0x19, 0x02, 0x01, 0x09, 0xA4, 0x14, 0xA1, 0x12, 0xA1, 0x10, 0xA1, 0x0E, + 0x1A, 0x03, 'I', 'E', 'D', 0x1A, 0x07, 'L', 'L', 'N', '0', '$', 'd', 's'); + RESULTS(s, 0xA4, 0xA2, 0x12, B_TRUE, 0x84, 0x03, 0x03, 0x40, 0x00, 0x91, 0x08, 0x65, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0xA2, 0x18, 0xA2, 0x07, 0x87, 0x05, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x84, 0x03, 0x03, 0x00, 0x00, 0x91, 0x08, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x3F); +} + ZTEST(mms, test_scan_period) { struct zelos_mms_model scanned = model; @@ -1006,6 +1048,10 @@ ZTEST(mms, test_model_is_checked) bad = model; bad.value_count = VOL; zassert_equal(zelos_mms_core_init(&mms, &bad, 0, &ops, NULL), -EINVAL); + /* A data object listed from its second leaf. */ + bad = model; + bad.data_sets = &(const struct zelos_mms_data_set){"LLN0$ds", &members[1], 1, true}; + zassert_equal(zelos_mms_core_init(&mms, &bad, 0, &ops, NULL), -EINVAL); } ZTEST_SUITE(mms, NULL, NULL, before, NULL, NULL);