1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::errors_internal::Error;
/// A helper struct representing the ID of a node in the mesh.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeId(u32);
impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl PartialEq<u32> for NodeId {
fn eq(&self, other: &u32) -> bool {
self.0 == *other
}
}
impl PartialOrd<u32> for NodeId {
fn partial_cmp(&self, other: &u32) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(other)
}
}
impl NodeId {
/// Creates a new `NodeId` from a `u32`.
pub fn new(id: u32) -> NodeId {
NodeId(id)
}
/// Returns the `u32` id of the `NodeId`.
pub fn id(&self) -> u32 {
self.0
}
}
impl From<u32> for NodeId {
fn from(value: u32) -> Self {
NodeId(value)
}
}
pub mod encoded_data {
/// A struct that represents incoming encoded data from a radio connection.
/// The wrapped data may contain a whole packet, a partial packet, or multiple packets.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IncomingStreamData(Vec<u8>);
impl std::fmt::Display for IncomingStreamData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl IncomingStreamData {
/// Creates a new `IncomingStreamData` struct from a `Vec<u8>`.
pub fn new(data: Vec<u8>) -> IncomingStreamData {
IncomingStreamData(data)
}
/// Returns a copy of the `Vec<u8>` data contained within the `IncomingStreamData` struct.
pub fn data_vec(&self) -> Vec<u8> {
self.0.clone()
}
}
impl From<Vec<u8>> for IncomingStreamData {
fn from(value: Vec<u8>) -> Self {
IncomingStreamData(value)
}
}
impl From<&[u8]> for IncomingStreamData {
fn from(value: &[u8]) -> Self {
IncomingStreamData(value.to_vec())
}
}
impl AsRef<[u8]> for IncomingStreamData {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
/// A struct that represents encoded binary data that will be used within the `protobufs::Data`
/// field of an outgoing `protobufs::MeshPacket`.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EncodedMeshPacketData(Vec<u8>);
impl std::fmt::Display for EncodedMeshPacketData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl EncodedMeshPacketData {
/// Creates a new `EncodedMeshPacketData` struct from a `Vec<u8>`.
pub fn new(data: Vec<u8>) -> EncodedMeshPacketData {
EncodedMeshPacketData(data)
}
/// Returns a reference to the `Vec<u8>` data contained within the `EncodedMeshPacketData` struct.
pub fn data(&self) -> &[u8] {
&self.0
}
/// Returns a copy of the `Vec<u8>` data contained within the `EncodedMeshPacketData` struct.
pub fn data_vec(&self) -> Vec<u8> {
self.0.clone()
}
}
impl From<Vec<u8>> for EncodedMeshPacketData {
fn from(value: Vec<u8>) -> Self {
EncodedMeshPacketData(value)
}
}
impl From<&[u8]> for EncodedMeshPacketData {
fn from(value: &[u8]) -> Self {
EncodedMeshPacketData(value.to_vec())
}
}
/// A struct that represents the binary encoding of an outgoing `protobufs::ToRadio` packet.
/// This data **does not** include a packet header.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EncodedToRadioPacket(Vec<u8>);
impl std::fmt::Display for EncodedToRadioPacket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl EncodedToRadioPacket {
/// Creates a new `EncodedToRadioPacket` struct from a `Vec<u8>`.
pub fn new(data: Vec<u8>) -> EncodedToRadioPacket {
EncodedToRadioPacket(data)
}
/// Returns a reference to the `Vec<u8>` data contained within the `EncodedToRadioPacket` struct.
pub fn data(&self) -> &[u8] {
&self.0
}
/// Returns a copy of the `Vec<u8>` data contained within the `EncodedToRadioPacket` struct.
pub fn data_vec(&self) -> Vec<u8> {
self.0.clone()
}
}
impl From<Vec<u8>> for EncodedToRadioPacket {
fn from(value: Vec<u8>) -> Self {
EncodedToRadioPacket(value)
}
}
impl From<&[u8]> for EncodedToRadioPacket {
fn from(value: &[u8]) -> Self {
EncodedToRadioPacket(value.to_vec())
}
}
/// A struct that represents the binary encoding of an outgoing `protobufs::ToRadio` packet.
/// This encoding can be sent to a radio, as it includes the required 4-byte packet header.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EncodedToRadioPacketWithHeader(Vec<u8>);
impl std::fmt::Display for EncodedToRadioPacketWithHeader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl EncodedToRadioPacketWithHeader {
/// Creates a new `EncodedToRadioPacketWithHeader` struct from a `Vec<u8>`.
pub fn new(data: Vec<u8>) -> EncodedToRadioPacketWithHeader {
EncodedToRadioPacketWithHeader(data)
}
/// Returns a reference to the `Vec<u8>` data contained within the `EncodedToRadioPacketWithHeader` struct.
pub fn data(&self) -> &[u8] {
&self.0
}
/// Returns a copy of the `Vec<u8>` data contained within the `EncodedToRadioPacketWithHeader` struct.
pub fn data_vec(&self) -> Vec<u8> {
self.0.clone()
}
}
impl From<Vec<u8>> for EncodedToRadioPacketWithHeader {
fn from(value: Vec<u8>) -> Self {
EncodedToRadioPacketWithHeader(value)
}
}
impl From<&[u8]> for EncodedToRadioPacketWithHeader {
fn from(value: &[u8]) -> Self {
EncodedToRadioPacketWithHeader(value.to_vec())
}
}
}
pub mod mesh_channel {
use super::*;
/// A struct that represents a messaging channel on the mesh. This struct is used to
/// limit the valid channel indices to be in the range [0..7].
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MeshChannel(u32);
impl std::fmt::Display for MeshChannel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl MeshChannel {
/// Creates a new `MeshChannel` struct from a `u32`. This method will return an error
/// if the passed `u32` is not in the range [0..7].
pub fn new(channel: u32) -> Result<MeshChannel, Error> {
if !(0..=7).contains(&channel) {
return Err(Error::InvalidChannelIndex { channel });
}
Ok(MeshChannel(channel))
}
/// Returns the `u32` channel index of the `MeshChannel`.
pub fn channel(&self) -> u32 {
self.0
}
}
impl From<u32> for MeshChannel {
fn from(channel: u32) -> Self {
MeshChannel(channel)
}
}
}