Skip to main content

ClientSession

Struct ClientSession 

Source
pub struct ClientSession<P, const N: usize = 1024> { /* private fields */ }
Expand description

A client association.

N is the scratch buffer the session uses to build an APDU before protecting it. It must be at least as large as the largest APDU the client sends; 1024 covers every request that is not a block transfer.

Implementations§

Source§

impl<P: CryptoProvider, const N: usize> ClientSession<P, N>

Source

pub fn new(config: ClientConfig, provider: P) -> Self

A session that will use provider for cryptography.

Source

pub const fn state(&self) -> SessionState

What the session is doing.

Source

pub const fn negotiated(&self) -> Option<&InitiateResponse>

What both sides agreed to, once the association is open.

Source

pub const fn server_system_title(&self) -> Option<SystemTitle>

The server’s system title, learned from the AARE.

Source

pub const fn invocation_counter(&self) -> u32

The invocation counter the client last sent under.

Persist this. Restoring a lower value after a restart reuses a nonce; see ClientConfig::invocation_counter.

Source

pub const fn set_invocation_counter(&mut self, value: u32)

Start the invocation counter from a known value, after reading the meter’s record of it or restoring one from storage.

Only ever move it forward. There is no check here, because a client that has just read a higher value out of the meter’s own counter object is doing the right thing and a client that sets it backwards is burning the key either way.

Source

pub const fn peer_invocation_counter(&self) -> Option<u32>

The highest invocation counter accepted from the server, if any.

Source

pub fn associate_request(&mut self, out: &mut [u8]) -> Result<usize>

Build the AARQ that opens the association.

§Errors

When the buffer is too small, when high level security is configured without a system title, or when a challenge is needed and the provider has no entropy.

Source

pub fn handle_associate_response( &mut self, apdu: &[u8], ) -> Result<AssociationStep>

Consume the AARE and say what happens next.

§Errors

When the APDU is not an AARE, when the negotiated version is not one this crate speaks, or when the server’s InitiateResponse cannot be unprotected.

Source

pub fn hls_reply_request(&mut self, out: &mut [u8]) -> Result<usize>

Build the reply_to_HLS_authentication request that answers the server’s challenge.

§Errors

When the mechanism is not one this crate implements, or the session is not at this step.

Source

pub fn handle_hls_reply_response( &mut self, apdu: &[u8], ) -> Result<AssociationStep>

Consume the server’s answer to the HLS reply.

§Errors

ErrorKind::BadTag when the server’s proof does not match the challenge the client sent — which means the server does not hold the key, and the association must not be used.

Source

pub fn get_request( &mut self, descriptor: AttributeDescriptor, access: Option<SelectiveAccess<'_>>, out: &mut [u8], ) -> Result<usize>

Build a GET request for one attribute.

§Errors

When the association is not open, when the server did not agree to GET, or when the buffer is too small.

Source

pub fn get<C: InterfaceClass>( &mut self, logical_name: Obis, attribute: i8, out: &mut [u8], ) -> Result<usize>

Build a GET request for an attribute of a typed class.

§Errors

As ClientSession::get_request.

Source

pub fn get_request_with_list( &mut self, items: &[AttributeDescriptorWithSelection<'_>], out: &mut [u8], ) -> Result<usize>

Build a GET request for several attributes at once.

One round trip instead of one per attribute. On a GPRS or LPWAN link the round trip dominates everything else, so reading thirty registers one at a time is not thirty times the bytes — it is thirty times the latency, which is the difference between a meter read taking a second and taking a minute.

Each item gets its own result: a list where one attribute is unreadable comes back with a value for the others and a DataAccessResult in that one’s place, so a single denied object does not lose the whole read.

§Errors

When the association is not open, when the server did not agree to Conformance::GET or Conformance::MULTIPLE_REFERENCES, when the list is empty, or when a buffer is too small.

Source

pub fn get_next_block_request( &mut self, block_number: u32, out: &mut [u8], ) -> Result<usize>

Build the request for the next block of a long response.

block_number acknowledges the block just received; the server answers with the one after it. The request carries the invoke id of the read it is continuing, not a fresh one — a get-request-next is part of an invocation already in flight, and giving it a new id asks the server about something it has never been asked.

§Errors

When the association is not open, when no request is outstanding to continue, or when the buffer is too small.

Source

pub fn set_request( &mut self, descriptor: AttributeDescriptor, access: Option<SelectiveAccess<'_>>, value: Data<'_>, out: &mut [u8], ) -> Result<usize>

Build a SET request.

§Errors

When the association is not open, when the server did not agree to SET, or when the buffer is too small.

Source

pub fn set_request_with_list( &mut self, items: &[AttributeDescriptorWithSelection<'_>], values: &[Data<'_>], out: &mut [u8], ) -> Result<usize>

Write several attributes in one exchange, one result each.

The values are positional: value i is written to attribute i. A server answers with one DataAccessResult per item, so a single refused write does not lose the batch.

§Errors

When the association is not open, when the server did not agree to Conformance::SET or Conformance::MULTIPLE_REFERENCES, when the two lists are different lengths, or when a buffer is too small.

Source

pub fn action_request_with_list( &mut self, descriptors: &[MethodDescriptor], parameters: &[Option<Data<'_>>], out: &mut [u8], ) -> Result<usize>

Invoke several methods in one exchange, one result each.

The parameters are positional: parameter i goes to method i, and a method that takes none gets null-data in its slot. The answer is a Response::ActionResults with one outcome per method, so a method that refuses does not lose the batch — which matters more here than for a read, because a batch that opened a breaker and then failed would otherwise report only the failure.

§Errors

When the association is not open, when the server did not agree to Conformance::ACTION or Conformance::MULTIPLE_REFERENCES, when the two lists are different lengths, or when a buffer is too small.

Source

pub fn set_transfer_with_list<'v>( &mut self, items: &'v [AttributeDescriptorWithSelection<'v>], values: &'v [u8], scratch: &'v mut [u8], ) -> Result<BlockSender<'v>>

Begin writing several attributes whose values together exceed the PDU size.

values is the encoded value-list — a count prefix and one Data per attribute, which is exactly what ClientSession::encode_value_list produces — and it stays the caller’s, as every other long value does.

§Errors

When the association is not open, or the server did not agree to Conformance::MULTIPLE_REFERENCES and Conformance::BLOCK_TRANSFER_WITH_SET_OR_WRITE.

Source

pub fn encode_value_list(values: &[Data<'_>], out: &mut [u8]) -> Result<usize>

Encode a value-list for ClientSession::set_transfer_with_list.

A free function on the session rather than a method on a list type, because what it produces is the field — count prefix included — and that is the thing the blocks carry.

§Errors

When out is too small.

Source

pub fn set_transfer<'v>( &mut self, descriptor: AttributeDescriptor, access: Option<SelectiveAccess<'v>>, value: &'v [u8], ) -> Result<BlockSender<'v>>

Begin writing a value larger than the negotiated PDU size.

value is the encoded attribute value, and it stays the caller’s: how large a value a deployment writes is not a decision for this crate, and a session that buffered it would put a ceiling on it that nothing in the standard has. Drive the transfer with ClientSession::next_block_request until it reports done.

§Errors

When the association is not open, or the server did not agree to Conformance::BLOCK_TRANSFER_WITH_SET_OR_WRITE.

Source

pub fn action_transfer<'v>( &mut self, descriptor: MethodDescriptor, parameters: &'v [u8], ) -> Result<BlockSender<'v>>

Begin invoking a method whose parameter is larger than the negotiated PDU size.

§Errors

When the association is not open, or the server did not agree to Conformance::BLOCK_TRANSFER_WITH_ACTION.

Source

pub fn next_block_request( &mut self, sender: &mut BlockSender<'_>, out: &mut [u8], ) -> Result<usize>

Emit the next block of a transfer started with ClientSession::set_transfer or ClientSession::action_transfer.

Every block of one transfer carries the same invoke id: a fresh one names a different invocation, and a server that checks refuses it while one that does not writes the fragment into something else.

§Errors

When the transfer is already finished, or the buffer is too small.

Source

pub fn action_next_block_request( &mut self, block_number: u32, out: &mut [u8], ) -> Result<usize>

Ask for the next block of a long ACTION result.

The counterpart of ClientSession::get_next_block_request for a method that returns more than one APDU can carry.

§Errors

When the association is not open, or no invocation is outstanding.

Source

pub fn access_request( &mut self, items: &[AccessItem<'_>], out: &mut [u8], ) -> Result<usize>

Read, write and invoke in one exchange — the ACCESS service.

This is what a battery-powered device on a low-power network uses: the cost there is round trips, not bytes, and ACCESS is the only service that mixes the three operations into one. It has no ciphered tag of its own, so in a protected association it travels inside general-glo-ciphering and needs Conformance::GENERAL_PROTECTION as well as Conformance::ACCESS.

The answer comes back as Response::Access, positionally: entry i of the result list is the outcome of item i, and entry i of the data list is what it produced — null-data for a write, and for a read that failed.

§Errors

When the association is not open, when the server did not agree to Conformance::ACCESS, when items is empty, or when a buffer is too small.

Source

pub fn read_request( &mut self, items: &[VariableAccess<'_>], out: &mut [u8], ) -> Result<usize>

Read attributes by short name — the legacy addressing mode.

One entry per thing to read, answered by a Response::ReadResults with one result in the same position. A short name is an object’s base name plus an offset; crate::cosem::ShortName computes one, and the base names come from the meter’s own Association SN object list.

Short-name referencing is a property of the association, not of a request: set ClientConfig::referencing to Referencing::ShortName so the AARQ proposes the short-name application context. A meter that answered a logical-name association with short names would be answering a different question, so this refuses to send one into the wrong context.

§Errors

When the association is not open or is not a short-name one, when the server did not agree to Conformance::READ, when the list is empty, or when a buffer is too small.

Source

pub fn read_next_block_request( &mut self, block_number: u16, out: &mut [u8], ) -> Result<usize>

Ask for the next block of a long short-name read.

block_number acknowledges the block just received. Unlike the logical-name services this is an ordinary read entry rather than a form of its own, which is why it takes the same path.

§Errors

As ClientSession::read_request.

Source

pub fn write_request( &mut self, items: &[VariableAccess<'_>], values: &[Data<'_>], out: &mut [u8], ) -> Result<usize>

Write attributes by short name.

The two lists are positional: value i goes to entry i. The answer is a Response::WriteResults with one outcome per entry, so a single refused write does not lose the batch.

§Errors

When the association is not open or is not a short-name one, when the server did not agree to Conformance::WRITE, when the two lists are different lengths, or when a buffer is too small.

Source

pub fn unconfirmed_write_request( &mut self, items: &[VariableAccess<'_>], values: &[Data<'_>], out: &mut [u8], ) -> Result<usize>

The same write, with no answer expected.

unconfirmed-write is a distinct service rather than a flag on the confirmed one, and this is a distinct method for the same reason: a caller that sends one must not then wait for a reply. It costs Conformance::UNCONFIRMED_WRITE rather than Conformance::WRITE.

§Errors

As ClientSession::write_request.

Source

pub fn action_request( &mut self, descriptor: MethodDescriptor, parameters: Option<Data<'_>>, out: &mut [u8], ) -> Result<usize>

Build an ACTION request.

§Errors

When the association is not open, when the server did not agree to ACTION, or when the buffer is too small.

Source

pub fn release_request(&mut self, out: &mut [u8]) -> Result<usize>

Build the request that releases the association.

§Errors

When the buffer is too small.

Source

pub fn handle_response<'b>( &mut self, apdu: &[u8], buf: &'b mut [u8], ) -> Result<Response<'b>>

Consume a response APDU.

buf receives the unprotected APDU, and the returned Response borrows from it. That is why it is a parameter: the caller decides where a decrypted APDU lives, and it lives no longer than the caller lets it.

General block transfer. A response the server sends as GBT arrives one block per call. Each block is gathered into buf, so pass the same buffer for every block of one response, large enough for the whole protected response. Until it is complete the answer is Response::Pending (receive again) or Response::Acknowledge (send its bytes, then receive again); then it is the response itself. The Client driver does all of this.

§Errors

When the APDU is malformed, protected more weakly than the policy demands, or not a response at all; when a GBT response outgrows buf.

Source

pub const fn is_receiving_blocks(&self) -> bool

True while a response is arriving as general block transfer.

Trait Implementations§

Source§

impl<P: Debug, const N: usize> Debug for ClientSession<P, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, const N: usize> Forget for ClientSession<P, N>

§

impl<P, const N: usize> Freeze for ClientSession<P, N>
where InternalBitFlags: Freeze, P: Freeze,

§

impl<P, const N: usize> Move for ClientSession<P, N>

§

impl<P, const N: usize> RefUnwindSafe for ClientSession<P, N>
where InternalBitFlags: RefUnwindSafe, P: RefUnwindSafe,

§

impl<P, const N: usize> Send for ClientSession<P, N>
where P: Send, InternalBitFlags: Send,

§

impl<P, const N: usize> Sync for ClientSession<P, N>
where P: Sync, InternalBitFlags: Sync,

§

impl<P, const N: usize> Unpin for ClientSession<P, N>
where P: Unpin, InternalBitFlags: Unpin,

§

impl<P, const N: usize> UnsafeUnpin for ClientSession<P, N>
where InternalBitFlags: UnsafeUnpin, P: UnsafeUnpin,

§

impl<P, const N: usize> UnwindSafe for ClientSession<P, N>
where InternalBitFlags: UnwindSafe, P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.