knx_ip_client/packets/
tpdu.rs

1use super::apdu::APDU;
2
3// Transport Protocol Data Unit (TPDU)
4// Is the same as the APDU, but with the transport control field
5// that depends on transport medium
6// 03.03.04 Trasport Layer chapter 2
7//
8#[derive(Debug)]
9pub struct TPDU {
10    pub address_type: TpduAddressType,
11    pub kind: TpduKind,
12    pub numbered: bool,
13    pub sequence_number: u8,
14    pub apdu: APDU,
15}
16
17#[derive(Debug)]
18pub enum TpduKind {
19    Data,
20    Control,
21}
22
23#[derive(Debug)]
24pub enum TpduAddressType {
25    Individual,
26    Group,
27}
28
29impl TPDU {
30    pub fn t_data_broadcast(apdu: APDU) -> Self {
31        Self {
32            address_type: TpduAddressType::Group,
33            kind: TpduKind::Data,
34            numbered: false,
35            sequence_number: 0,
36            apdu,
37        }
38    }
39    pub fn t_data_group(apdu: APDU) -> Self {
40        Self {
41            address_type: TpduAddressType::Group,
42            kind: TpduKind::Data,
43            numbered: false,
44            sequence_number: 0,
45            apdu,
46        }
47    }
48    pub fn t_data_tag_group(apdu: APDU) -> Self {
49        Self {
50            address_type: TpduAddressType::Group,
51            kind: TpduKind::Data,
52            numbered: false,
53            sequence_number: 1,
54            apdu,
55        }
56    }
57    pub fn t_data_individual(apdu: APDU) -> Self {
58        Self {
59            address_type: TpduAddressType::Individual,
60            kind: TpduKind::Data,
61            numbered: false,
62            sequence_number: 0,
63            apdu,
64        }
65    }
66    pub fn t_data_connected(apdu: APDU, sequence_number: u8) -> Self {
67        Self {
68            address_type: TpduAddressType::Individual,
69            kind: TpduKind::Data,
70            numbered: true,
71            sequence_number,
72            apdu,
73        }
74    }
75    pub fn t_connect(apdu: APDU) -> Self {
76        Self {
77            address_type: TpduAddressType::Individual,
78            kind: TpduKind::Control,
79            numbered: false,
80            sequence_number: 0,
81            apdu,
82        }
83    }
84    pub fn t_disconnect(apdu: APDU) -> Self {
85        Self {
86            address_type: TpduAddressType::Individual,
87            kind: TpduKind::Control,
88            numbered: false,
89            sequence_number: 1,
90            apdu,
91        }
92    }
93
94    pub fn packet(&self) -> Vec<u8> {
95        self.apdu.packet()
96    }
97}