embedded_nano_mesh/mesh_lib/node/packet/
types.rs

1use super::constants::{ADDRESS_TYPE_SIZE, CONTENT_SIZE, DATA_LENGTH_TYPE_SIZE, PACKET_BYTES_SIZE};
2
3use heapless::Vec;
4
5use super::FromBytes;
6
7/// Type alias for packet identification number.
8pub type IdType = u8;
9
10/// Type alias for packet bit flags.
11pub type FlagsType = u8;
12
13/// Type alias for device address identification number.
14/// It can contain only non-zero positive number.
15/// The zero value is reserved for broadcast address.
16pub type ExactAddressType = core::num::NonZeroU8;
17
18/// Type to strict interaction with addressing during use of the library.
19/// It provides options to send packet to exact device or to all devices it can reach.
20#[derive(Eq, PartialEq, Clone)]
21pub enum GeneralAddressType {
22    /// Sends the packet to exact device with this address.
23    Exact(ExactAddressType),
24
25    /// Sends the packet to all devices it can reach.
26    Broadcast,
27}
28
29impl Into<GeneralAddressType> for ExactAddressType {
30    fn into(self) -> GeneralAddressType {
31        GeneralAddressType::Exact(self)
32    }
33}
34
35impl Into<AddressType> for GeneralAddressType {
36    fn into(self) -> AddressType {
37        match self {
38            Self::Exact(address) => address.get(),
39            Self::Broadcast => 0 as AddressType,
40        }
41    }
42}
43
44impl From<AddressType> for GeneralAddressType {
45    fn from(address: AddressType) -> Self {
46        match core::num::NonZeroU8::new(address) {
47            Some(address) => Self::Exact(address),
48            None => Self::Broadcast,
49        }
50    }
51}
52
53/// Type alias for packet address identification number.
54pub type AddressType = u8;
55
56/// Type alias for packet checksum.
57pub type ChecksumType = u8;
58
59/// Type alias for packet data length.
60pub type DataLengthType = u16;
61
62/// Type alias for packet lifetime. This value contains the information,
63/// about for how many times the packet can be re-sent.
64/// It has sense to contain same capacity of possible values same
65/// as `AddressType` - in order to make the packet possible
66/// to pass all the nodes of the network.
67pub type LifeTimeType = AddressType;
68
69/// Type alias for bytes of data contained in the packet.
70pub type PacketDataBytes = Vec<u8, { CONTENT_SIZE }>;
71
72/// Type alias that represents serialized packet bytes sequence.
73pub type PacketSerializedBytes = Vec<u8, { PACKET_BYTES_SIZE }>;
74
75impl FromBytes<ADDRESS_TYPE_SIZE> for AddressType {
76    fn from_be_bytes(bytes: [u8; ADDRESS_TYPE_SIZE]) -> Self {
77        Self::from_be_bytes(bytes)
78    }
79}
80
81impl FromBytes<DATA_LENGTH_TYPE_SIZE> for DataLengthType {
82    fn from_be_bytes(bytes: [u8; DATA_LENGTH_TYPE_SIZE]) -> Self {
83        Self::from_be_bytes(bytes)
84    }
85}
86
87/// State of the packet.
88#[derive(PartialEq, Eq, Clone)]
89pub enum PacketState {
90    /// End-receiver of packet with this state - just receives the data.
91    /// does no additional logic over it.
92    Normal,
93
94    /// Packet with this state being set - forces end-receiver device,
95    /// to automatically respond packet with same content back with
96    /// `Pong` state being set. Also receiving device receives content
97    /// of the packet.
98    Ping,
99
100    /// Packet with this state being set - indicates, that receiver
101    /// has successfully processed the packet.
102    Pong,
103
104    /// Packet with this state being set - forces end-receiver device,
105    /// to get ready to do full transaction.
106    SendTransaction,
107
108    /// Packet with this state being set - is sent by receiver and
109    /// informs sender device, that receiver have the
110    /// transaction being started.
111    AcceptTransaction,
112
113    /// Packet with this state being set - is sent by sender and
114    /// forces end-receiver device, to do the transaction finish.
115    InitTransaction,
116
117    /// Packet with this state being set - is sent by receiver and
118    /// informs sender device, that receiver have the transaction finished.
119    FinishTransaction,
120}