dlms-cosem-kit

Services and segmentation

GET, SET, ACTION and ACCESS in dlms-cosem-kit, the batched with-list forms, selective access, and the three segmentation mechanisms that compose rather than substitute.

The four services

Logical-name referencing has four, and the crate drives all of them in both roles.

ServiceAsksBatched form
GETread an attributeget-request-with-list
SETwrite oneset-request-with-list
ACTIONinvoke a methodaction-request-with-list
ACCESSall three, mixed, in one exchange— it is the batch

A request identifies an object by class and OBIS code, then names an attribute or method.

Batched reads

A list GET returns one result per attribute in request order. A refused attribute retains its position and its access error.

let n = session.get_request_with_list(&items, &mut request)?;
// …
if let Response::DataList(results) = session.handle_response(&answer, &mut scratch)? {
    for (descriptor, result) in wanted.iter().zip(results.iter()) {
        match result?.value() {
            Ok(value) => println!("{descriptor:?} = {value:?}"),
            Err(why)  => println!("{descriptor:?} refused: {why:?}"),   // its own slot
        }
    }
}

ACCESS

ACCESS is the one service that mixes reads, writes and invocations into a single request. It is what a battery-powered device on a low-power network wants, and the Green Book's LPWAN examples are built on it.

Its three lists are positional: entry i of each belongs to item i. A write and a failed read occupy their slot with null-data rather than being omitted, so a caller never has to count which entries produced anything.

ACCESS has no ciphered tag of its own. On a protected link it can only travel inside general-glo-ciphering, so it needs the general-protection conformance bit as well as access. Both are proposed by default.

Selective access

A selector narrows a read at the meter, which is the difference between fetching a day and fetching a year. Two are defined: by a range of the sort value (usually the clock), and by entry position.

let mut params = [0u8; 96];
let access = RangeDescriptor::by_clock(from, to).to_selective_access(&mut params)?;
let n = session.get_request(descriptor, Some(access), &mut request)?;

The server passes the selector to the object store. Apply it to the requested rows or return an access error for an unsupported selector.

Size a blocking GET

Client::with_response_capacity sets the maximum encoded result size (64 KiB by default). get_into(attribute, selection, storage) also limits the value to storage.len(). An exactly fitting value succeeds for ordinary, service-block and GBT reads. For example, three 37-byte rows plus a two-byte array header need 113 bytes. When delivered in three service replies with ten bytes of overhead each, the incoming APDU total is 143 bytes. A transport budget counts that overhead separately.

The driver checks service fragments before appending. For GBT it first stages the protected APDU for verification, allowing one receive frame of envelope overhead beyond the effective value limit. The driver reuses that workspace across requests. The caller's output is usable only on success. A completed response that exceeds the value limit leaves the association usable. An interrupted or unverified transfer sets reconnect_required on the capacity error and closes the local session.

End an association

release sends a release request and succeeds only on a release response whose reason is absent or normal. A refusal is ReleaseRefused. A meter that hangs up first is an I/O error. After a closed session every call is NotAssociated and nothing is sent; use disconnect to drop the link without a release.

Segmentation

Cuts upBounded by
Block transfera value — GET, SET or ACTIONthe negotiated PDU size
General block transferan APDU, whatever service it carriesthe negotiated PDU size
HDLC segmentationan APDUthe negotiated information field

For example, a 1024-byte APDU can span multiple 128-byte HDLC information fields. Larger values can also require multiple APDUs.

Block transfer, both directions

ServiceOut of the serverInto the server
GETget-response-with-datablock—
SET—set-request-with-first-datablock, then …-with-datablock
ACTIONaction-response-with-pblockaction-request-with-first-pblock, then …-with-pblock

On the client, BlockCollector gathers a long response and BlockSender emits a long request. The bytes stay yours in both directions, for the same reason: how large a value a deployment moves is a property of the deployment, and a buffer allocated here would be this crate deciding how large a load profile may be.

Two rules keep it honest. Block numbers must arrive consecutively from one — a duplicated or reordered fragment concatenated in the wrong place usually still decodes, into a value that is simply wrong and that carries no error. And every block of one transfer carries the invoke id of the invocation it belongs to; a fresh id names a different invocation, and a server that checks refuses it while one that does not writes the fragment into something else.

General block transfer

GBT is the service-independent one: it cuts an APDU into blocks whatever the APDU carries — including the services that have no block form of their own, which is the whole reason it exists. Both engines choose it themselves: when the association negotiated general-block-transfer, a reply too large for the client goes out as GBT for every service — ACCESS and the list forms included — and a long request may arrive as GBT and is served exactly as if it had come whole.

Streaming — the sender puts a window of blocks on the wire before it waits. A window of zero is not "no window": it is what a sender that is not streaming sends, and reading it as one is how a streaming peer waits forever for an acknowledgement nobody owes it.

Retry — an acknowledgement names the highest block received in order, so one lower than the last block sent is not an error. It is the receiver saying where the run broke, and the sender rewinds to just after it. A lost block therefore costs the blocks after it rather than the transfer.

The corollary is the invariant the whole mechanism rests on: the receiver never stores a block from beyond a gap. Acknowledging the last in-order block is precisely what fetches the missing one; keeping the future block as well would leave a hole nothing fills, and the reassembled APDU would be wrong rather than absent.

Protection covers the whole. The reply is protected once, as one APDU, before it is cut, and the receiver removes protection once, from the reassembled APDU, with the same policy check as for a single one. A block is never decrypted alone, and a plaintext reply smuggled in blocks into a ciphered association is refused as the downgrade it is.

Who drives what. The server's handle returns the first block of a window and poll_block the rest, so a request/response driver can put a whole window on the wire; handle returns zero when a streamed block owes no answer. The client session gathers the blocks into the caller's buffer and answers Response::Pending (receive again) or Response::Acknowledge(ack) (send these seven bytes) until the response is whole. The blocking Client does both. GbtSender and GbtReceiver remain for callers who drive the procedure by hand, and GbtSendCursor / GbtRecvCursor are the same procedure without a borrowed buffer, which is what the engines store.

How the window fields are read — derived. The window field is read as the sender's own receive window, and a block with the streaming bit clear is the one the receiver acknowledges. The Green Book excerpt does not print these clauses; the reading rests on the Ed 9 procedure as implemented here and on one second implementation. It is chosen because it cannot deadlock whichever way a peer reads the field — and a receiver also acknowledges when its own window fills, so a sender that streams past it cannot overrun it. A broken window asks for its retry once: the rest of the window arriving after the gap does not make the sender rewind again.

Without GBT, the engines segment with the service-specific mechanism above, which is what the installed base of meters overwhelmingly uses.

Profile buffers

A load profile is an array of structures whose columns are given by the meter's capture_objects. Three compressions may be in play at once, and ProfileBuffer undoes all three so the visitor sees every column of every row as an absolute value:

  • a column repeating the previous row, sent as null-data;
  • a run of repeated columns at the end of a row, sent by making the row shorter;
  • a column sending a delta from the previous row rather than a value.

Pass the count of columns returned by the selection. A row wider than that count returns InvalidValue before any callback for that row. Earlier rows may already have been visited.

A caller that skipped the last of those gets no error. It gets differences where readings were meant, which for a rising register looks like a meter that suddenly reads almost nothing.

Short-name referencing (feature sn)

A pre-logical-name meter names an attribute by a sixteen-bit number rather than by (class, OBIS, index). That installed base is still in the field, so the mode is built — behind a feature, off by default.

read, write and unconfirmed-write are driven from both engines; information-report decodes. Reads and writes take lists and answer one result per entry, in order, like the with-list forms above.

let n = session.read_request(&[VariableAccess::VariableName(0x0030)], &mut out)?;
if let Response::ReadResults(results) = session.handle_response(&answer, &mut scratch)? {
    for result in results.iter() { /* one per entry */ }
}

Four things differ from the logical-name services:

  • The mode is agreed once, in the application context the AARQ proposes. A short-name service sent into a logical-name association is refused before a byte goes out.
  • There is no ACTION service. A method is invoked by writing its short name, with the value as the parameter.
  • unconfirmed-write really is unconfirmed — the meter sends nothing back, which is why it is a separate method rather than a flag.
  • A long read blocks as a data-block-result inside the read response, continued by a block-number-access entry.

Underneath, nothing differs: the same object store, the same access-control gate, the same audit trail. The cookbook has the recipe, including how a short name is computed from an object's base name.

What is not driven

action-request-with-list-and-first-pblock — a batch of methods whose parameters are themselves too large for one APDU. It decodes; it is not driven, because how the parameter bytes divide between the methods is not stated in the material this project has, and the outcome of guessing is a meter running the right method on the wrong argument.

Short-name write block transfer (write-data-block-access) is decoded and refused per entry for the same reason a refusal beats a guess.