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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use tokio::net::UdpSocket;
use tokio::time::{sleep, Duration};

use crate::errors::{Result, TelloError};
use crate::wifi::wait_for_wifi;

const DEFAULT_DRONE_HOST:&str = "192.168.10.1";

const CONTROL_UDP_PORT:i32 = 8889;
// const STATE_UDP_PORT = 8890

/// Initial state - no WiFi network
#[derive(Debug)]
pub struct NoWifi;

/// The drone WiFi has been joined, but no UDP messages have been sent or received.
#[derive(Debug)]
pub struct Disconnected;

/// The connection exchange has been completed and the drone is ready to fly.
#[derive(Debug)]
pub struct Connected {
    sock: UdpSocket,
}

/// For interacting with the Tello EDU drone using the simple text-based UDP protocol.
///
/// The basic flow from the user's point of view is
///
///    SEND `command` → drone does something → RECEIVE `response` when it's finished
///
/// Messages are plain ASCII text, eg command `forward 10` → response `ok`
///
/// ```
/// use tello_edu::{Tello, Result};
/// 
/// #[tokio::main]
/// async fn main() {
///     fly().await.unwrap();
/// }
/// 
/// async fn fly() -> Result<()> {
///     // create a new drone in the `NoWifi` state     
///     let drone = Tello::new();
///
///     // wait until the host computer joins the drone's WiFi network
///     // (joining the network is not automatic - how it happens is up to you)
///     let drone = drone.wait_for_wifi().await?;
/// 
///     // establish connection and put the drone in "command" mode
///     let drone = drone.connect().await?;
/// 
///     // fly!
///     drone.take_off().await?;
///     drone.turn_clockwise(360).await?;
///     drone.land().await?;
/// 
///     Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct Tello<S = NoWifi> {
    /// The connection state of the drone.
    state: S
}

impl Tello<NoWifi> {
    /// Create a new drone in a completely unconnected state.
    pub fn new() -> Self {
        Self { state: NoWifi }
    }

    /// Wait until the host joins the drone's WiFi network
    ///
    /// *nb* exactly how the the network is joined is up to you
    ///
    pub async fn wait_for_wifi(&self) -> Result<Tello<Disconnected>>  {
        println!("[Tello] waiting for WiFi...");
        wait_for_wifi("TELLO").await?;
        Ok(Tello { state: Disconnected })
    }

    /// Use this if you are already in the appropriate WiFi network. 
    pub async fn assume_wifi(&self) -> Result<Tello<Disconnected>>  {
        println!("[Tello] assuming WiFi has already been joined");
        Ok(Tello { state: Disconnected })
    }    
}

impl Tello<Disconnected> {
    pub async fn connect(&self) -> Result<Tello<Connected>> {
        let local_address = format!("0.0.0.0:{CONTROL_UDP_PORT}");

        let drone_host = DEFAULT_DRONE_HOST;
        let drone_address = format!("{drone_host}:{CONTROL_UDP_PORT}");

        println!("[Tello] CONNECT {local_address} → {drone_address}");

        // bind local socket
        println!("[Tello] binding local {local_address}...");
        let sock = UdpSocket::bind(&local_address).await?;
        
        // connect to drone
        println!("[Tello] connecting to drone at {drone_address}...");
        let mut i = 0;
        loop {
            i = i + 1;
            match sock.connect(&drone_address).await {
                Ok(_) => {
                    println!("[Tello] CONNECTED");
                    break;
                }
                Err(err) => {
                    println!("[Tello] connection attempt #{i} failed ({err}), retrying...");
                    sleep(Duration::from_millis(100)).await;
                }
            }
        }

        let drone = Tello { state: Connected { sock } };

        // tell drone to expect text SDK commands (not the private binary protocol)
        println!("[Tello] putting drone in command mode...");
        drone.send_expect_ok("command").await?;

        // check battery
        let b = drone.battery().await?;
        if b < 10 {
            println!("[Tello] WARNING low battery: {b}%");
        }
        else {
            println!("[Tello] battery: {b}%");  
        }

        Ok(drone)
    } 
}

impl Tello<Connected> {
    /// Disconnect from the drone.
    pub fn disconnect(&self) -> Tello<Disconnected> {
        println!("[Tello] DISCONNECT");
        Tello { state: Disconnected }
    }

    /// Sends a command to the drone using the simple Tello UDP protocol, returning the reponse.
    ///
    /// The basic flow from the user's point of view is
    ///
    ///    SEND command → drone does something → RECEIVE response when it's finished
    ///
    /// Messages are plain ASCII text, eg command `forward 10` → response `ok`
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send(&self, command: &str) -> Result<String> {
        println!("[Tello] SEND {command}");

        let s = &self.state.sock;
        s.send(command.as_bytes()).await?;

        let response = self.recv().await?;

        // the drone sends "forced stop" after "stop" after a delay which may
        // arrive after more commands have been sent
        if response == "forced stop" {
            self.on_forced_stop();

            // try again
            self.recv().await
        }
        else {
            Ok(response)
        }          
    }

    async fn recv(&self) -> Result<String> {
        let s = &self.state.sock;
        let mut buf = vec![0; 256];        
        let n = s.recv(&mut buf).await?;

        buf.truncate(n);
        let r = String::from_utf8(buf)?;
        let response = r.trim().to_string();

        println!("[Tello] RECEIVED {response}");

        Ok(response)
    }

    fn on_forced_stop(&self) {
        println!("[Tello] FORCED STOP");
    }

    /// Sends a command, resolving to an error if the response is not "ok"
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send_expect_ok(&self, command: &str) -> Result<()> {
        match self.send(command).await {
            Ok(response) => {
                if response == "ok" {
                    Ok(())
                }
                else {
                    Err(TelloError::from_not_ok_response(response))
                }
            }
            Err(err) => Err(err)
        }
    }

    /// Sends a command with a single value, resolving to an error if the 
    /// response is not "ok"
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// - `value` the value to append to the command
    /// 
    pub async fn send_value_expect_ok<T: std::fmt::Display>(&self, command: &str, value: T) -> Result<()> {
        match self.send(&format!("{command} {value}")).await {
            Ok(response) => {
                if response == "ok" {
                    Ok(())
                }
                else {
                    Err(TelloError::from_not_ok_response(response))
                }
            }
            Err(err) => Err(err)
        }
    }

    /// Sends a command, expecting no response at all from the drone.
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send_expect_nothing(&self, command: &str) -> Result<()> {
        println!("[Tello] SEND {command}");

        let s = &self.state.sock;
        s.send(command.as_bytes()).await?;

        Ok(())
    }

    /// Sends a command, expecting a response that can be parsed as type `T` from the drone.
    ///
    /// - `command` the command to send, must be a valid Tello SDK command string
    /// 
    pub async fn send_expect<T: std::str::FromStr>(&self, command: &str) -> Result<T> {
        let r = self.send(command).await?;
        let v = r.parse::<T>().map_err(|_| TelloError::ParseResponseError { msg: format!("unexpected response: \"{r}\"")})?;
        Ok(v)
    }

    /// The unique drone serial number.
    pub async fn serial_number(&self) -> Result<String> {
        self.send("sn?").await
    }

    /// The Tello SDK version.
    pub async fn sdk_version(&self) -> Result<String> {
        self.send("sdk?").await
    }

    /// The drone battery level as a percentage.
    pub async fn battery(&self) -> Result<u8> {
        self.send_expect::<u8>("battery?").await
    }

    /// The WiFi signal to noise ratio as a percentage.
    pub async fn wifi_signal_to_noise_ratio(&self) -> Result<u8> {
        self.send_expect::<u8>("wifi?").await
    }

    /// The flight time in seconds, requested directly from the drone.
    pub async fn flight_time(&self) -> Result<u16> {
        self.send_expect::<u16>("time?").await
    }

    /// Immediately stop all motors.
    ///
    /// warning! this will make the drone drop like a brick!
    ///
    pub async fn emergency_stop(&self) -> Result<()> {
        self.send_expect_nothing("emergency").await
    }

    /// Take off and hover.
    pub async fn take_off(&self) -> Result<()> {
        self.send_expect_ok("takeoff").await
    }

    /// Land and stop motors.
    pub async fn land(&self) -> Result<()> {
        self.send_expect_ok("land").await
    }

    /// The drone speed in cm/s, requested directly from the drone.
    pub async fn speed(&self) -> Result<f32> {
        self.send_expect::<f32>("speed?").await
    }

    /// Set the forward speed.
    /// 
    /// - `speed` Desired speed, 10-100 cm/s
    ///
    pub async fn set_speed(&self, speed: u8) -> Result<()> {
        self.send_value_expect_ok("speed", speed).await
    }

    /// Wait for the given length of time.
    ///
    /// - `duration` The time to wait
    ///
    pub async fn wait(&self, duration:Duration) -> Result<()> {
        println!("[Tello] waiting for {duration:#?}");
        sleep(duration).await;
        Ok(())
    }    

    /// Stop and hover in place.
    pub async fn stop(&self) -> Result<()> {
        // will also trigger a "forced stop" response
        self.send_expect_ok("stop").await
    }

    /// Turn clockwise.
    ///
    /// - `degrees` Angle in degrees 1-360°
    ///
    pub async fn turn_clockwise(&self, degrees: u16) -> Result<()> {
        self.send_value_expect_ok("cw", degrees).await   
    }

    /// Turn counter-clockwise.
    ///
    /// - `degrees` Angle in degrees 1-360°
    pub async fn turn_counterclockwise(&self, degrees: u16) -> Result<()> {
        self.send_value_expect_ok("ccw", degrees).await   
    }

    /// Move straight up.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_up(&self, distance: u16) -> Result<()> {
        self.send_value_expect_ok("up", distance).await
    }

    /// Move straight down.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_down(&self, distance: u16) -> Result<()> {
        self.send_value_expect_ok("down", distance).await
    }
    
    /// Move straight left.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_left(&self, distance: u16) -> Result<()> {
        self.send_value_expect_ok("left", distance).await
    }
    
    /// Move straight right.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_right(&self, distance: u16) -> Result<()> {
        self.send_value_expect_ok("right", distance).await
    }
    
    /// Move straight forwards.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_forward(&self, distance: u16) -> Result<()> {
        self.send_value_expect_ok("forward", distance).await
    }
    
    /// Move straight backwards.
    ///
    /// - `distance` Distance to travel, 20-500 cm
    ///
    pub async fn move_back(&self, distance: u16) -> Result<()> {
        self.send_value_expect_ok("back", distance).await
    }

    /// Flip left.
    /// *nb* fails if battery is low 
    pub async fn flip_left(&self) -> Result<()> {
        self.send_expect_ok("flip l").await
    }        

    /// Flip right.
    /// *nb* fails if battery is low 
    pub async fn flip_right(&self) -> Result<()> {
        self.send_expect_ok("flip r").await
    }        

    /// Flip forward.
    /// *nb* fails if battery is low 
    pub async fn flip_forward(&self) -> Result<()> {
        self.send_expect_ok("flip f").await
    }        

    /// Flip back.
    /// *nb* fails if battery is low 
    pub async fn flip_back(&self) -> Result<()> {
        self.send_expect_ok("flip b").await
    }        

}