Skip to main content

spodes_rs/spodus/
catalog.rs

1//! Builders for the standard catalogue objects of the ИВКЭ (СТО 34.01-5.1-013-2023,
2//! Appendix A): Clock, SAP assignment, Security setup and Association LN.
3//!
4//! These reuse the crate's existing COSEM interface classes, instantiated at the
5//! Appendix-A OBIS codes with СТО-013 defaults.
6
7use crate::classes::association_ln::{
8    AssociationLn, AssociationLnConfig, AssociationLnVersion, AuthenticationMechanism,
9};
10use crate::classes::clock::{Clock, ClockConfig};
11use crate::classes::sap_assignment::{SapAssignment, SapAssignmentConfig};
12use crate::classes::security_setup::{SecuritySetup, SecuritySetupConfig};
13use crate::obis::ObisCode;
14use crate::types::attrs::{AssociatedPartnersId, ContextName, SapAssignmentEntry, XDLMSContextInfo};
15
16use super::obis;
17
18/// Client-type identifiers of the ИВКЭ server part (§8.4).
19pub mod client_id {
20    /// Public client — read-only, no security.
21    pub const PUBLIC: u8 = 16;
22    /// Reading — read + selective + some actions, high security.
23    pub const READER: u8 = 32;
24    /// Push output — DataNotification within a preset association.
25    pub const PUSH: u8 = 34;
26    /// Configurator — read/write/selective, high security.
27    pub const CONFIGURATOR: u8 = 48;
28}
29
30/// The ИВКЭ Clock object (`0.0.1.0.0.255`, IC 8).
31pub fn clock() -> Clock {
32    use crate::types::attrs::DateTime;
33    Clock::new(ClockConfig {
34        logical_name: ObisCode::new(0, 0, 1, 0, 0, 255),
35        time: DateTime([0u8; 12]),
36        time_zone: 0,
37        status: 0,
38        daylight_savings_begin: DateTime([0u8; 12]),
39        daylight_savings_end: DateTime([0u8; 12]),
40        daylight_savings_deviation: 0,
41        daylight_savings_enabled: false,
42        clock_base: 0,
43    })
44}
45
46/// The SAP-assignment object (§10.1.8, `0.0.41.0.0.255`, IC 17) listing the
47/// logical devices of the ИВКЭ.
48pub fn sap_assignment(sap_assignment_list: Vec<SapAssignmentEntry>) -> SapAssignment {
49    SapAssignment::new(SapAssignmentConfig { logical_name: obis::sap_assignment(), sap_assignment_list })
50}
51
52/// A Security-setup object (IC 64 v1) at `obis` with the given security policy
53/// (§9: 0 for Public, authenticated+encrypted for Reader/Configurator) and
54/// suite 0.
55pub fn security_setup(obis: ObisCode, security_policy: u8, client_st: Vec<u8>, server_st: Vec<u8>) -> SecuritySetup {
56    SecuritySetup::new(SecuritySetupConfig {
57        logical_name: obis,
58        version: 1,
59        security_policy,
60        security_suite: 0,
61        client_system_title: client_st,
62        server_system_title: server_st,
63        certificates: vec![],
64    })
65}
66
67/// An Association LN object (IC 15 v1) at `obis` for a connection type, with the
68/// given authentication mechanism and a reference to its Security-setup object.
69pub fn association(obis: ObisCode, mechanism: AuthenticationMechanism, security_setup_ref: ObisCode) -> AssociationLn {
70    AssociationLn::new(AssociationLnConfig {
71        logical_name: obis,
72        version: AssociationLnVersion::Version1,
73        object_list: vec![],
74        associated_partners_id: AssociatedPartnersId { client_sap: 0, server_sap: 0 },
75        application_context_name: ContextName::OctetString(vec![]),
76        xdlms_context_info: XDLMSContextInfo {
77            conformance: vec![],
78            max_receive_pdu_size: 0xFFFF,
79            max_send_pdu_size: 0xFFFF,
80            dlms_version_number: 6,
81            quality_of_service: 0,
82            cyphering_info: vec![],
83        },
84        authentication_mechanism: mechanism,
85        secret: vec![],
86        association_status: 0,
87        security_setup_reference: security_setup_ref,
88        user_list: vec![],
89        current_user: None,
90    })
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use crate::interface::InterfaceClass;
97
98    #[test]
99    fn standard_catalogue_objects_have_correct_class_and_obis() {
100        assert_eq!(clock().class_id(), 8);
101        assert_eq!(clock().logical_name(), &ObisCode::new(0, 0, 1, 0, 0, 255));
102
103        let sap = sap_assignment(vec![]);
104        assert_eq!(sap.class_id(), 17);
105        assert_eq!(sap.logical_name(), &obis::sap_assignment());
106
107        let ss = security_setup(ObisCode::new(0, 0, 43, 0, 0, 255), 0, vec![], vec![]);
108        assert_eq!(ss.class_id(), 64);
109        assert_eq!(ss.version(), 1);
110        assert_eq!(ss.logical_name(), &ObisCode::new(0, 0, 43, 0, 0, 255));
111
112        let assoc = association(
113            ObisCode::new(0, 0, 40, 0, 1, 255),
114            AuthenticationMechanism::None,
115            ObisCode::new(0, 0, 43, 0, 0, 255),
116        );
117        assert_eq!(assoc.class_id(), 15);
118        assert_eq!(assoc.logical_name(), &ObisCode::new(0, 0, 40, 0, 1, 255));
119    }
120}