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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use PhantomData;
use Arc;
use Duration;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate*;
/// MAVLink node.
///
/// A node is a member of a MAVLink network that manages I/O connection and provides interface for
/// communicating with other MAVLink devices. [`Node`] is API-agnostic, both synchronous and
/// asynchronous API extend its functionality with specific methods and behavior relevant to the
/// underlying concurrency model. Asynchronous API is based on [Tokio](https://tokio.rs).
///
/// There are two fundamental kinds of a node defined by [`NodeKind`] generic parameter:
///
/// * Edge node ([`asnc::node::EdgeNode`](crate::asnc::node::EdgeNode) /
/// [`sync::node::EdgeNode`](crate::sync::node::EdgeNode)) is a MAVlink device with defined system
/// `ID` and component `ID`. It can send and receive MAVLink messages, emit automatic heartbeats
/// and perform other active tasks. This is the kind of node you are mostly interested in.
/// * Proxy node ([`asnc::node::ProxyNode`](crate::asnc::node::ProxyNode) /
/// [`sync::node::ProxyNode`](crate::sync::node::ProxyNode)), on the other hand, does not have a
/// specified `ID` and component `ID`. It only can receive and proxy MAVLink frames. This node
/// can't perform active tasks and is used to pass frames between different parts of a MAVLink
/// network.
///
/// ## Sending and Receiving
///
/// Sending operations are represented by [`SendFrame`] / [`SendMessage`] traits. To send messages
/// you may use either [`SendMessage::send`], or [`SendFrame::send_frame`]. The former accepts
/// MAVLink messages and decodes them into frames, the latter one is sending MAVLink frames
/// directly. Only edge nodes implement [`SendMessage`] traits.
///
/// Receiving operations are represented by [`ReceiveEvent`](crate::sync::node::ReceiveEvent) /
/// [`ReceiveFrame`](crate::sync::node::ReceiveFrame) for synchronous api and
/// [`ReceiveEvent`](crate::asnc::node::ReceiveEvent) / [`ReceiveFrame`](crate::asnc::node::ReceiveFrame)
/// for asynchronous API.
///
/// The suggested approach for receiving incoming frames is subscribing to `events`. This method
/// returns an iterator (or a stream in the case of asynchronous API) over node events, such as
/// incoming frames, invalid frames, that hasn't passed signature validation, new peers, and so on.
///
/// You can also subscribe to valid frames using `frames` method.
///
/// ## Frame Validation
///
/// Since MAVLink is a connectionless protocol, the only way to ensure frame consistency, is to
/// validate its checksum. The checksum serves two purposes: first, it ensures, that message was
/// not damaged during sending, and second, it guarantees that sender and receiver use the same
/// version of a dialect. Unfortunately, that means, that in order to validate a frame, you have
/// to know `CRC_EXTRA` of the exact message this frame encodes. Frames will be validated according
/// to [`Node::known_dialects`] upon sending and receiving. Invalid incoming frames will be
/// available as corresponding events. You can always validate frames against arbitrary dialect
/// using [`Frame::validate_checksum`].
///
/// ## Message Signing
///
/// MAVLink [message signing](https://mavlink.io/en/guide/message_signing.html) is provided by
/// [`FrameSigner`] that can be provided upon node configuration. It can be configured with
/// incoming and outgoing [`SignStrategy`] for a fine-grained control over what and when should be
/// signed. It is possible to validate several authenticated links with additional keys, but only
/// one link `ID` / key pair will be used to sign frames.
///
/// ## Multiple Connections
///
/// It is possible to create a node with multiple connections. There is special transport called
/// [`Network`], that encapsulates several proxy nodes. Such nodes may have individual setting,
/// such as message signing configuration.
///
/// ## Examples
///
/// Create a synchronous TCP server node:
///
/// ```rust,no_run
/// # #[cfg(feature = "sync")] {
/// use maviola::prelude::*;
/// use maviola::sync::prelude::*;
///
/// let addr = "127.0.0.1:5600";
///
/// // Create a node from synchronous configuration
/// // with MAVLink protocol set to `V2`
/// let mut node = Node::sync::<V2>()
/// .id(MavLinkId::new(1, 1)) // Set system and component IDs
/// .connection(
/// TcpServer::new(addr) // Configure TCP server connection
/// .unwrap()
/// ).build().unwrap();
///
/// // Activate node to start sending heartbeats
/// node.activate().unwrap();
/// # }
/// ```
///
/// Create an asynchronous TCP server node:
///
/// ```rust,no_run
/// # #[cfg(not(feature = "async"))] fn main() {}
/// # #[cfg(feature = "async")]
/// # #[tokio::main(flavor = "current_thread")] async fn main() {
/// use maviola::prelude::*;
/// use maviola::asnc::prelude::*;
///
/// let addr = "127.0.0.1:5600";
///
/// // Create a node from asynchronous configuration
/// // with MAVLink protocol set to `V2`
/// let mut node = Node::asnc::<V2>()
/// .id(MavLinkId::new(1, 1)) // Set system and component IDs
/// .connection(
/// TcpServer::new(addr) // Configure TCP server connection
/// .unwrap()
/// ).build().await.unwrap();
///
/// // Activate node to start sending heartbeats
/// node.activate().await.unwrap();
/// # }
/// ```
///
/// Create a synchronous node, that signs all outgoing messages and rejects unsigned or incorrectly
/// signed incoming messages:
///
/// ```rust,no_run
/// # #[cfg(feature = "sync")] {
/// use maviola::protocol::dialects::minimal::messages::Heartbeat;
/// use maviola::prelude::*;
/// use maviola::sync::prelude::*;
///
/// let node = Node::sync::<V2>()
/// .id(MavLinkId::new(1, 1))
/// .connection(TcpServer::new("127.0.0.1:5600").unwrap())
/// .signer(
/// FrameSigner::builder()
/// // Set `ID` of a signed link
/// .link_id(1)
/// // Set secret key
/// .key("unsecure")
/// // Reject unsigned or incorrect incoming messages
/// .incoming(SignStrategy::Strict)
/// // Sign all outgoing messages
/// .outgoing(SignStrategy::Sign)
/// )
/// .build().unwrap();
///
/// // The following message will be signed during sending
/// node.send(&Heartbeat::default()).unwrap();
///
/// // Incoming frames are always correctly signed
/// let (frame, _) = node.recv_frame().unwrap();
/// assert!(frame.is_signed());
/// # }
/// ```
///
/// Create an asynchronous node with a network containing two TCP servers:
///
/// ```rust,no_run
/// # #[cfg(not(feature = "async"))] fn main() {}
/// # #[cfg(feature = "async")]
/// # #[tokio::main] async fn main() {
/// use maviola::prelude::*;
/// use maviola::asnc::prelude::*;
///
/// let node = Node::asnc::<V2>()
/// .id(MavLinkId::new(1, 17))
/// .connection(
/// Network::asnc()
/// .add_connection(TcpServer::new("127.0.0.1:5600").unwrap())
/// .add_connection(TcpServer::new("127.0.0.1:5601").unwrap())
/// )
/// .build().await.unwrap();
/// # }
/// ```