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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Adafruit FONA GSM module.

#![allow(missing_debug_implementations)]

// TODO: timeouts

use std::{fmt, io::Write, sync::Mutex, thread, time::Duration};

use failure::{bail, Error, Fail, ResultExt};
use lazy_static::lazy_static;
use log::{debug, error, info, warn};
use tokio_serial::{Serial, SerialPortSettings};

use crate::{config::CONFIG, error, generate_error_string};

lazy_static! {
    /// The FONA module control structure.
    pub static ref FONA: Mutex<Fona> = Mutex::new(Fona { serial: None });
}

/// Adafruit FONA control structure.
pub struct Fona {
    serial: Option<Serial>,
}

impl fmt::Debug for Fona {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use tokio_serial::SerialPort;

        write!(
            f,
            "Fona {{ serial: {:?} }}",
            if let Some(ref serial) = &self.serial {
                serial.name()
            } else {
                None
            }
        )
    }
}

impl Fona {
    /// Initializes the Adafruit FONA module.
    pub fn initialize(&mut self) -> Result<(), Error> {
        if self.is_on()? {
            info!("FONA module is on, rebooting for stability.");
            self.turn_off()?;
            info!("Module is off, sleeping 3 seconds before turning it on\u{2026}");
            thread::sleep(Duration::from_secs(3));
        }

        self.turn_on()?;
        if !self.is_on()? {
            error!("The module is still off. Finishing initialization.");
            bail!(error::Fona::PowerOn);
        }

        info!("Starting serial connection.");
        let mut settings = SerialPortSettings::default();
        settings.baud_rate = CONFIG.fona().baud_rate();

        let serial = Serial::from_path(CONFIG.fona().uart(), &settings)?;
        // serial.set_timeout(Duration::from_secs(5));
        self.serial = Some(serial);
        info!("Serial connection started.");

        info!("Checking OK initialization (3 times).");
        for _ in 0..2 {
            if self.send_command_read("AT")? != "OK" {
                info!("Not initialized.");
            }
            thread::sleep(Duration::from_millis(100));
        }

        if self.send_command_read("AT")? == "OK" {
            thread::sleep(Duration::from_millis(100));
            info!("Initialization OK.");

            // Turn echo off.
            let _ = self.send_command_read("ATE0")?;
            thread::sleep(Duration::from_millis(100));

            if self.send_command_read("ATE0")? == "OK" {
                Ok(())
            } else {
                Err(error::Fona::EchoOff.into())
            }
        } else {
            error!("Initialization error.");
            Err(error::Fona::Init.into())
        }
    }

    /// Checks if the FONA module is on.
    pub fn is_on(&self) -> Result<bool, Error> {
        Ok(CONFIG.fona().status_gpio().get_value()? == 1)
    }

    /// Turns on the FONA module.
    pub fn turn_on(&mut self) -> Result<(), Error> {
        if self.is_on()? {
            warn!("Trying to turn FONA on but it was already on.");
            Ok(())
        } else {
            info!("Turning FONA on\u{2026}");

            CONFIG.fona().power_gpio().set_value(0)?;
            thread::sleep(Duration::from_secs(2));
            CONFIG.fona().power_gpio().set_value(1)?;

            thread::sleep(Duration::from_secs(3));

            info!("FONA on.");

            Ok(())
        }
    }

    /// Tuns off the FONA module.
    pub fn turn_off(&mut self) -> Result<(), Error> {
        if self.is_on()? {
            info!("Turning FONA off\u{2026}");

            CONFIG.fona().power_gpio().set_value(0)?;
            thread::sleep(Duration::from_secs(2));
            CONFIG.fona().power_gpio().set_value(1)?;

            thread::sleep(Duration::from_secs(3));

            info!("FONA off.");

            Ok(())
        } else {
            warn!("Trying to turn FONA off but it was already off.");
            Ok(())
        }
    }

    /// Sends an SMS with the given text to the given phone number.
    pub fn send_sms<M>(&mut self, message: M) -> Result<(), Error>
    where
        M: AsRef<str>,
    {
        let character_count = message.as_ref().chars().count();
        info!(
            "Sending SMS: `{}` ({} characters) to number {}",
            message.as_ref(),
            character_count,
            CONFIG.fona().sms_phone().as_str(),
        );

        #[cfg(not(feature = "no_sms"))]
        {
            if character_count > 160 {
                return Err(error::Fona::LongSms.into());
            }

            if self.send_command_read("AT+CMGF=1")? != "OK" {
                error!("Error sending SMS on `AT+CMGF=1` command.");
                return Err(error::Fona::SmsAtCmgf.into());
            }

            let cmgs_command = format!(r#"AT+CMGS="{}""#, CONFIG.fona().sms_phone().as_str());
            if self.send_command_read_limit(&cmgs_command, 2)? != "> " {
                error!("Error sending SMS on `{}` command.", cmgs_command);
                return Err(error::Fona::SmsAtCmgs.into());
            }

            if let Some(ref mut serial) = self.serial {
                debug!("Sending message\u{2026}");

                // Write message
                serial
                    .write_all(message.as_ref().as_bytes())
                    .context(error::Fona::Command)?;

                debug!("Sent: `{}`", message.as_ref());

                // Write Ctrl+Z
                serial.write_all(&[0x1A]).context(error::Fona::Command)?;

                debug!("Sent Ctrl+Z");
            } else {
                error!(
                    "No serial when trying to send message `{}`",
                    message.as_ref()
                );
                return Err(error::Fona::NoSerial.into());
            }

            let new_line = self.read_line()?;
            if !new_line.is_empty() {
                warn!(
                    "There was some non-flushed output after sending the message: `{}`",
                    new_line
                );
            }

            let response = self.read_line()?;
            if !response.starts_with("+CMGS: ") {
                error!(
                    "Error reading +CMGS response to the message, read `{}`",
                    response
                );
                return Err(error::Fona::SmsCmgs.into());
            }

            let new_line = self.read_line()?;
            if !new_line.is_empty() {
                warn!(
                    "There was some non-flushed output after sending the message: `{}`",
                    new_line
                );
            }

            // TODO read the number of characters and check it.

            let ok = self.read_line()?;
            if ok != "OK" {
                error!("No OK received after sending SMS, received: `{}`", ok);
                return Err(error::Fona::SmsOk.into());
            }

            info!("SMS Sent.");
            Ok(())
        }

        #[cfg(feature = "no_sms")]
        {
            thread::sleep(Duration::from_secs(5));
            Ok(())
        }
    }

    /// Gets the current location using GPRS.
    pub fn location(&mut self) -> Result<Location, Error> {
        if self.send_command_read("AT+CMGF=1")? != "OK" {
            error!("Error getting location on `AT+CMGF=1` response.");
            return Err(error::Fona::LocAtCmgf.into());
        }

        if self.send_command_read("AT+CGATT=1")? != "OK" {
            error!("Error getting location on `AT+CGATT=1` response.");

            if self.send_command_read("AT+SAPBR=0,1")? == "OK" {
                info!("GPRS off.");
                return Err(error::Fona::LocAtCgatt.into());
            } else {
                error!("Error turning GPRS down.");

                return Err(error::Fona::LocAtGprsDown
                    .context(error::Fona::LocAtCgatt)
                    .into());
            }
        }

        if self.send_command_read(r#"AT+SAPBR=3,1,"CONTYPE","GPRS""#)? != "OK" {
            error!(r#"Error getting location on `AT+SAPBR=3,1,"CONTYPE","GPRS"` response."#);

            if self.send_command_read("AT+SAPBR=0,1")? == "OK" {
                info!("GPRS off.");
                return Err(error::Fona::LocAtSapbrContype.into());
            } else {
                error!("Error turning GPRS down.");

                return Err(error::Fona::LocAtGprsDown
                    .context(error::Fona::LocAtSapbrContype)
                    .into());
            }
        }

        let apn_message = format!(
            r#"AT+SAPBR=3,1,"APN","{}""#,
            CONFIG.fona().location_service()
        );
        if self.send_command_read(&apn_message)? != "OK" {
            error!("Error getting location on `{}` response.", apn_message);

            if self.send_command_read("AT+SAPBR=0,1")? == "OK" {
                info!("GPRS off.");
                return Err(error::Fona::LocAtSapbrApn.into());
            } else {
                error!("Error turning GPRS down.");

                return Err(error::Fona::LocAtGprsDown
                    .context(error::Fona::LocAtSapbrApn)
                    .into());
            }
        }

        if self.send_command_read("AT+SAPBR=1,1")? != "OK" {
            error!("Error getting location on `AT+SAPBR=1,1` response.");

            if self.send_command_read("AT+SAPBR=0,1")? == "OK" {
                info!("GPRS off.");
                return Err(error::Fona::LocAtSapbr.into());
            } else {
                error!("Error turning GPRS down.");

                return Err(error::Fona::LocAtGprsDown
                    .context(error::Fona::LocAtSapbr)
                    .into());
            }
        }

        let location_response = self.send_command_read("AT+CIPGSMLOC=1,1")?;
        let mut location_response_iter = location_response.split(',');
        // TODO: response could not be valid
        let longitude = location_response_iter
            .nth(1)
            .ok_or(error::Fona::LocLon)?
            .parse::<f32>()
            .context(error::Fona::LocLon)?;
        let latitude = location_response_iter
            .next()
            .ok_or(error::Fona::LocLat)?
            .parse::<f32>()
            .context(error::Fona::LocLat)?;

        if self.read_line()? != "OK" {
            error!("Error getting location on `AT+CIPGSMLOC=1,1` response.");

            if self.send_command_read("AT+SAPBR=0,1")? == "OK" {
                info!("GPRS off.");
                return Err(error::Fona::LocAtCipgsmloc.into());
            } else {
                error!("Error turning GPRS down.");

                return Err(error::Fona::LocAtGprsDown
                    .context(error::Fona::LocAtCipgsmloc)
                    .into());
            }
        }

        if self.send_command_read("AT+SAPBR=0,1")? == "OK" {
            info!("GPRS off.");
        } else {
            error!("Error turning GPRS down.");

            return Err(error::Fona::LocAtGprsDown.into());
        }

        Ok(Location {
            latitude,
            longitude,
        })
    }

    /// Checks the FONA battery level, in percentage.
    pub fn battery_percent(&mut self) -> Result<f32, Error> {
        let bat_voltage = self.battery_voltage()?;
        Ok((bat_voltage - CONFIG.battery().fona_min())
            / (CONFIG.battery().fona_max() - CONFIG.battery().fona_min()))
    }

    /// Checks the FONA battery voltage, in volts (`V`).
    pub fn battery_voltage(&mut self) -> Result<f32, Error> {
        let response = self.send_command_read("AT+CBC")?;
        let mut tokens = response.split(',');

        // TODO: check beginning of real response.
        if tokens.next() == Some("+CBC:") {
            match tokens.next() {
                Some(val) => Ok(val
                    .parse::<f32>()
                    .context(error::Fona::CBCInvalidResponse)?
                    / 1_000_f32),
                None => Err(error::Fona::CBCInvalidResponse.into()),
            }
        } else {
            Err(error::Fona::CBCInvalidResponse.into())
        }
    }

    /// Gets the ADC (Analog-Digital converter) voltage of the FONA, in volts (`V`).
    pub fn adc_voltage(&mut self) -> Result<f32, Error> {
        let response = self.send_command_read("AT+CADC?")?;
        let mut tokens = response.split(',');

        if tokens.next() == Some("+CADC=1") {
            match tokens.next() {
                Some(val) => Ok(val
                    .parse::<f32>()
                    .context(error::Fona::CADCInvalidResponse)?
                    / 1_000_f32),
                None => Err(error::Fona::CADCInvalidResponse.into()),
            }
        } else {
            Err(error::Fona::CADCInvalidResponse.into())
        }
    }

    /// Checks if the FONA module has GSM connectivity.
    pub fn has_connectivity(&mut self) -> Result<bool, Error> {
        let response = self.send_command_read("AT+CREG?")?;
        Ok(response == "+CREG: 0,1" || response == "+CREG: 0,5")
    }

    /// Sends a command to the FONA module and reads the response.
    fn send_command_read<C>(&mut self, command: C) -> Result<String, Error>
    where
        C: AsRef<[u8]>,
    {
        self.send_command(command.as_ref())?;
        self.read_line()
    }

    /// Sends a command and reads a limited amount of characters.
    fn send_command_read_limit<C>(&mut self, command: C, count: usize) -> Result<String, Error>
    where
        C: AsRef<[u8]>,
    {
        use std::io::Read;

        self.send_command(command)?;

        if let Some(ref mut serial) = self.serial {
            let mut response = Vec::with_capacity(count);
            for res in serial.bytes() {
                response.push(res?);

                // We read enough bytes.
                if response.len() == count {
                    let res = String::from_utf8(response)?;
                    debug!(
                        "Received: `{}`",
                        res.replace('\r', "\\r").replace('\n', "\\n")
                    );
                    return Ok(res);
                }
            }

            Err(error::Fona::SerialEnd.into())
        } else {
            error!("No serial when trying to read response");
            Err(error::Fona::NoSerial.into())
        }
    }

    /// Sends a command to the FONA serial.
    fn send_command<C>(&mut self, command: C) -> Result<(), Error>
    where
        C: AsRef<[u8]>,
    {
        use std::borrow::Cow;

        if let Some(ref mut serial) = self.serial {
            debug!(
                "Sent command: `{}\\r\\n`", // TODO do we need the CRLF when sending Ctrl+Z?
                if command.as_ref() == [0x1A] {
                    Cow::from("Ctrl+Z")
                } else {
                    String::from_utf8_lossy(command.as_ref())
                }
            );

            serial
                .write_all(command.as_ref())
                .context(error::Fona::Command)?;

            // TODO do we need the CRLF when sending Ctrl+Z?
            serial.write_all(b"\r\n").context(error::Fona::Command)?;
        } else {
            error!(
                "No serial when trying to send command `{}`",
                String::from_utf8_lossy(command.as_ref())
            );
            return Err(error::Fona::NoSerial.into());
        }

        if self
            .read_line()
            .context(error::Fona::SendCommandCrlf)?
            .is_empty()
        {
            Ok(())
        } else {
            Err(error::Fona::SendCommandCrlf.into())
        }
    }

    /// Reads a line from the serial.
    fn read_line(&mut self) -> Result<String, Error> {
        use std::io::{ErrorKind, Read};

        if let Some(ref mut serial) = self.serial {
            let mut response = Vec::new();
            for res in serial.bytes() {
                match res {
                    Ok(b'\r') => {}
                    Ok(b'\n') => {
                        let res = String::from_utf8(response)?;
                        debug!("Received: `{}\r\n`", res);
                        return Ok(res);
                    }
                    Ok(b) => {
                        response.push(b);
                    }
                    Err(e) => {
                        return Err(match e.kind() {
                            ErrorKind::TimedOut => {
                                let partial = String::from_utf8(response)?;
                                debug!("Received (partial): `{}`", partial);
                                error::Fona::PartialResponse { response: partial }.into()
                            }
                            _ => e.into(),
                        });
                    }
                }
            }

            Err(error::Fona::SerialEnd.into())
        } else {
            error!("No serial when trying to read response");
            Err(error::Fona::NoSerial.into())
        }
    }
}

impl Drop for Fona {
    fn drop(&mut self) {
        match self.is_on() {
            Ok(true) => {
                info!("Turning FONA off\u{2026}");
                if let Err(e) = self.turn_off() {
                    error!("{}", generate_error_string(&e, "Error turning FONA off"));
                }
                info!("FONA off.");
            }
            Ok(false) => {}
            Err(e) => {
                error!(
                    "{}",
                    generate_error_string(
                        &e,
                        "Could not check if FONA was on when dropping the FONA object",
                    )
                );
            }
        }
    }
}

/// Structure representing the location of the probe as obtained by the FONA module.
#[derive(Debug, Clone, Copy)]
pub struct Location {
    /// Latitude of the location, in degrees (°).
    latitude: f32,
    /// Longitude of the location, in degrees (°).
    longitude: f32,
}

impl Location {
    /// Gets the latitude of the location, in degrees (°).
    pub fn latitude(self) -> f32 {
        self.latitude
    }

    /// Gets the longitude of the location, in degrees (°).
    pub fn longitude(self) -> f32 {
        self.longitude
    }
}

#[cfg(test)]
mod tests {
    use super::FONA;

    /// Tests FONA initialization.
    #[test]
    #[ignore]
    fn it_initialize() {
        FONA.lock().unwrap().initialize().unwrap();
    }

    #[test]
    #[ignore]
    fn it_send_sms() {
        FONA.lock().unwrap().initialize().unwrap();
        FONA.lock()
            .unwrap()
            .send_sms("OpenStratos test SMS")
            .unwrap();
    }

    #[test]
    #[ignore]
    fn it_location() {
        FONA.lock().unwrap().initialize().unwrap();
        let _ = FONA.lock().unwrap().location().unwrap();
    }
}