crazyflie_link/
connection.rs

1use crate::error::Result;
2use crate::Packet;
3use async_trait::async_trait;
4
5/// Describe the current link connection status
6#[derive(Clone, Debug)]
7pub enum ConnectionStatus {
8    /// The link is connecting (ie. Safelink not enabled yet)
9    Connecting,
10    /// The link is connected and active (Safelink enabled and no timeout)
11    Connected,
12    /// The link is disconnected, the string contains the human-readable reason
13    Disconnected(String),
14}
15
16// Describes the interface for a connection to a Crazyflie
17#[async_trait]
18pub trait ConnectionTrait {
19    /// Wait for the connection to be closed. Returns the message stored in the
20    /// disconnected connection status that indicate the reason for the disconnection
21    async fn wait_close(&self) -> String;
22
23    /// Close the connection and wait for the connection task to stop.
24    ///
25    /// The connection can also be closed by simply dropping the connection object.
26    /// Though, if the connection task is currently processing a packet, it will continue running
27    /// until the current packet has been processed. This function will wait for any ongoing packet
28    /// to be processed and for the communication task to stop.
29    async fn close(&self);
30
31    /// Return the connection status
32    async fn status(&self) -> ConnectionStatus;
33
34    /// Block until the connection is dropped. The `status()` function can be used to get the reason
35    /// for the disconnection.
36    async fn wait_disconnect(&self);
37
38    /// Send a packet to the connected Crazyflie
39    ///
40    /// This function can return an error if the connection task is not active anymore.
41    /// This can happen if the Crazyflie is disconnected due to a timeout
42    async fn send_packet(&self, packet: Packet) -> Result<()>;
43
44    /// Receive a packet from the connected Crazyflie
45    ///
46    /// This function can return an error if the connection task is not active anymore.
47    /// This can happen if the Crazyflie is disconnected due to a timeout
48    async fn recv_packet(&self) -> Result<Packet>;
49}
50
51/// Connection to a Crazyflie
52pub struct Connection {
53    /// Reference to the internal connection object
54    internal_connection: Box<dyn ConnectionTrait + Send + Sync>,
55}
56
57impl Connection {
58    /// Create a new connection object
59    pub fn new(internal_connection: Box<dyn ConnectionTrait + Send + Sync>) -> Self {
60        Self {
61            internal_connection,
62        }
63    }
64
65    /// Wait for the connection to be closed. Returns the message stored in the
66    /// disconnected connection status that indicate the reason for the disconnection
67    pub async fn wait_close(&self) -> String {
68        self.internal_connection.wait_close().await
69    }
70
71    /// Close the connection and wait for the connection task to stop.
72    ///
73    /// The connection can also be closed by simply dropping the connection object.
74    /// Though, if the connection task is currently processing a packet, it will continue running
75    /// until the current packet has been processed. This function will wait for any ongoing packet
76    /// to be processed and for the communication task to stop.
77    pub async fn close(&self) {
78        self.internal_connection.close().await
79    }
80
81    /// Return the connection status
82    pub async fn status(&self) -> ConnectionStatus {
83        self.internal_connection.status().await
84    }
85
86    /// Block until the connection is dropped. The `status()` function can be used to get the reason
87    /// for the disconnection.
88    pub async fn wait_disconnect(&self) {
89        self.internal_connection.wait_disconnect().await
90    }
91
92    /// Send a packet to the connected Crazyflie
93    ///
94    /// This function can return an error if the connection task is not active anymore.
95    /// This can happen if the Crazyflie is disconnected due to a timeout
96    pub async fn send_packet(&self, packet: Packet) -> Result<()> {
97        self.internal_connection.send_packet(packet).await
98    }
99
100    /// Receive a packet from the connected Crazyflie
101    ///
102    /// This function can return an error if the connection task is not active anymore.
103    /// This can happen if the Crazyflie is disconnected due to a timeout
104    pub async fn recv_packet(&self) -> Result<Packet> {
105        self.internal_connection.recv_packet().await
106    }
107}