dis_rs/common/transfer_ownership/
model.rs

1use crate::common::{BodyInfo, Interaction};
2use crate::constants::EIGHT_OCTETS;
3use crate::enumerations::{PduType, RequiredReliabilityService, TransferControlTransferType};
4use crate::model::{
5    length_padded_to_num, EntityId, PduBody, RecordSpecification, BASE_RECORD_SPEC_RECORD_LENGTH,
6};
7use crate::transfer_ownership::builder::TransferOwnershipBuilder;
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11const BASE_TRANSFER_OWNERSHIP_BODY_LENGTH: u16 = 28;
12
13/// 5.9.4 Transfer Ownership (TO) PDU
14///
15/// 7.8.4 Transfer Ownership (TO) PDU
16#[derive(Clone, Debug, Default, PartialEq)]
17#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
18pub struct TransferOwnership {
19    pub originating_id: EntityId,
20    pub receiving_id: EntityId,
21    pub request_id: u32,
22    pub required_reliability_service: RequiredReliabilityService,
23    pub transfer_type: TransferControlTransferType,
24    pub transfer_entity_id: EntityId,
25    pub record_specification: RecordSpecification,
26}
27
28impl TransferOwnership {
29    #[must_use]
30    pub fn builder() -> TransferOwnershipBuilder {
31        TransferOwnershipBuilder::new()
32    }
33
34    #[must_use]
35    pub fn into_builder(self) -> TransferOwnershipBuilder {
36        TransferOwnershipBuilder::new_from_body(self)
37    }
38
39    #[must_use]
40    pub fn into_pdu_body(self) -> PduBody {
41        PduBody::TransferOwnership(self)
42    }
43}
44
45impl BodyInfo for TransferOwnership {
46    fn body_length(&self) -> u16 {
47        BASE_TRANSFER_OWNERSHIP_BODY_LENGTH
48            + (self
49                .record_specification
50                .record_sets
51                .iter()
52                .map(|record| {
53                    let data_length_bytes = record
54                        .records
55                        .iter()
56                        .map(|rec| rec.len() as u16)
57                        .sum::<u16>();
58                    let padded_record =
59                        length_padded_to_num(data_length_bytes.into(), EIGHT_OCTETS);
60                    BASE_RECORD_SPEC_RECORD_LENGTH + padded_record.record_length as u16
61                })
62                .sum::<u16>())
63    }
64
65    fn body_type(&self) -> PduType {
66        PduType::TransferOwnership
67    }
68}
69
70impl Interaction for TransferOwnership {
71    fn originator(&self) -> Option<&EntityId> {
72        Some(&self.originating_id)
73    }
74
75    fn receiver(&self) -> Option<&EntityId> {
76        Some(&self.receiving_id)
77    }
78}