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
//! GPS module.

#![allow(missing_debug_implementations)]

use std::{
    fmt,
    io::{self, Read, Write},
    str::FromStr,
    sync::Mutex,
    thread,
    time::{Duration, Instant},
};

use chrono::{DateTime, Utc};
use failure::{Error, ResultExt};
use lazy_static::lazy_static;
use log::{info, warn};
use sysfs_gpio::Direction;
use tokio::{
    self,
    codec::{Decoder, LinesCodec},
    prelude::{Future, Stream},
};
use tokio_serial::{Serial, SerialPortSettings};

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

lazy_static! {
    /// GPS data for concurrent check.
    pub static ref GPS: Mutex<Gps> = Mutex::new(Gps::default());
}

/// GPS information structure.
#[derive(Debug, Default)]
pub struct Gps {
    latest_data: Option<Frame>,
}

impl Gps {
    /// Initializes the GPS.
    pub fn initialize(&mut self) -> Result<(), Error> {
        info!("Initializing GPS\u{2026}");
        CONFIG
            .gps()
            .power_gpio()
            .set_direction(Direction::Out)
            .context(error::Gps::Init)?;

        if self.is_on().context(error::Gps::Init)? {
            info!("GPS is on, turning off for 2 seconds for stability");
            self.turn_off().context(error::Gps::Init)?;
            thread::sleep(Duration::from_secs(2))
        }

        info!("Turning GPS on\u{2026}");
        self.turn_on().context(error::Gps::Init)?;
        info!("GPS on.");

        info!("Starting serial connection\u{2026}");
        let mut serial_settings = SerialPortSettings::default();
        serial_settings.baud_rate = CONFIG.gps().baud_rate();
        let mut serial =
            Serial::from_path(CONFIG.gps().uart(), &serial_settings).context(error::Gps::Init)?;
        serial.set_exclusive(false).context(error::Gps::Init)?;
        info!("Serial connection started.");

        info!("Sending configuration frames\u{2026}");
        let messages = [
            // Set refresh
            vec![
                0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0x64, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7A, 0x12,
            ],
            // Disable GSV:
            vec![0xB5, 0x62, 0x05, 0x01, 0x02, 0x00, 0x06, 0x01, 0x0F, 0x38],
            // Disable VTG:
            vec![
                0xB5, 0x62, 0x06, 0x01, 0x03, 0x00, 0xF0, 0x01, 0x00, 0xFB, 0x11,
            ],
            // Disable GLL:
            vec![
                0xB5, 0x62, 0x06, 0x01, 0x03, 0x00, 0xF0, 0x03, 0x00, 0xFD, 0x15,
            ],
            // Disable ZDA:
            vec![
                0xB5, 0x62, 0x06, 0x01, 0x03, 0x00, 0xF0, 0x05, 0x00, 0xFF, 0x19,
            ],
        ];

        for message in &messages {
            for _ in 0..100 {
                serial.write_all(message).context(error::Gps::Init)?;
                thread::sleep(Duration::from_millis(10));
            }
        }
        info!("Configuration frames sent");

        info!("Setting GPS to airborne (<1g) mode");
        if Gps::enter_airborne_1g_mode(&mut serial).is_ok() {
            info!("GPS entered airborne (<1g) mode successfully");
        } else {
            warn!("GPS failed to enter airborne (<1g) mode");
        }

        // TODO: select appropriate maximum length
        let (_writer, reader) = LinesCodec::new_with_max_length(250)
            .framed(serial)
            .then(Self::parse_frame)
            .split();

        let processor = reader
            .for_each(|frame| {
                GPS.lock().unwrap().latest_data = if frame.is_valid() { Some(frame) } else { None };
                Ok(())
            })
            .map_err(|e| warn!("Error processing frame: {}", e));
        tokio::run(processor);

        Ok(())
    }

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

    /// Turns the GPS on.
    pub fn turn_on(&self) -> Result<(), Error> {
        if self.is_on()? {
            warn!("Turning on the GPS but it was already on.");
        } else {
            CONFIG.gps().power_gpio().set_value(1)?
        }

        Ok(())
    }

    /// Turns the GPS off.
    pub fn turn_off(&self) -> Result<(), Error> {
        if self.is_on()? {
            CONFIG.gps().power_gpio().set_value(0)?
        } else {
            warn!("Turning off the GPS but it was already off.");
        }

        Ok(())
    }

    /// Enters airborne (<1g) GPS mode.
    fn enter_airborne_1g_mode(serial: &mut Serial) -> Result<(), Error> {
        let msg = [
            // Header, class, ID, Length
            0xB5, 0x62, 0x06, 0x24, 0x24, 0x00,
            // Payload:
            // Mask, Dynmodel, FixType
            0xFF, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00,
            0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Checksum
            0x16, 0xDC,
        ];
        let mut ack = [
            0xB5, 0x62, 0x05, 0x01, 0x02, 0x00, msg[2], msg[3], 0x00, 0x00,
        ];
        // Compute checksum
        for i in 2..8 {
            ack[8] += ack[i];
            ack[9] += ack[8];
        }

        let start = Instant::now();
        while start.elapsed() < Duration::from_secs(6) {
            // Send the message
            serial.flush()?;
            serial.write_all(&[0xFF])?;
            thread::sleep(Duration::from_millis(500));
            serial.write_all(&msg)?;

            // Wait for the ACK
            let mut checked_bytes = 0;
            let ack_start = Instant::now();
            while ack_start.elapsed() < Duration::from_secs(3) {
                if checked_bytes == 10 {
                    return Ok(());
                }

                let mut byte = [0];
                serial.read_exact(&mut byte)?; // FIXME: could this block?
                if byte[0] == ack[checked_bytes] {
                    checked_bytes += 1;
                } else {
                    checked_bytes = 0;
                }
            }
        }

        unimplemented!() // TODO: return error
    }

    /// Gets the latest GPS data.
    pub fn latest_data(&self) -> Option<Frame> {
        self.latest_data
    }

    /// Parses a GPS frame.
    fn parse_frame(line: Result<String, io::Error>) -> Result<Frame, Error> {
        let _line_str = line?; //                 if (bytes_ordered > 9)
                               // 		{
                               // 			return true;
                               // 		}

        // 		gettimeofday(&time_now, NULL);
        // 		ms_now = (long)((time_now.tv_sec)*1000 + (time_now.tv_usec)/1000);

        // 		if (this->serial->available())
        // 		{
        // 			byte = this->serial->read_byte();
        // 			if (byte == ack_packet[bytes_ordered])
        // 			{
        // 				bytes_ordered++;
        // 			}
        // 			else
        // 			{
        // 				bytes_ordered = 0;
        // 			}
        // }
        unimplemented!()
    }
}

impl Drop for Gps {
    fn drop(&mut self) {
        // TODO stop serial, turn GPS off.
    }
}

/// This structure represents a GPS frame.
#[derive(Debug, Clone, Copy)]
pub struct Frame {
    /// Time of the current fix.
    fix_time: DateTime<Utc>,
    /// GPS fix status.
    status: FixStatus,
    /// Number of satellites connected.
    satellites: u8,
    /// Latitude of the GPS antenna, in *°* (degrees).
    latitude: f32,
    /// Longitude of the GPS antenna, in *°* (degrees).
    longitude: f32,
    /// Altitude of the GPS antenna from sea level, in *m*.
    altitude: f32,
    /// Position dilution of precision (3D).
    pdop: f32,
    /// Horizontal dilution of precision (2D).
    hdop: f32,
    /// Vertical dilution of precision (1D).
    vdop: f32,
    /// Speed of the velocity vector, in *m/s*.
    speed: f32,
    /// Course of the velocity vector, in *°* (degrees).
    course: f32,
}

impl Frame {
    /// Gets the time of the current fix.
    pub fn fix_time(&self) -> DateTime<Utc> {
        self.fix_time
    }

    /// Gets the GPS fix status.
    pub fn status(&self) -> FixStatus {
        self.status
    }

    /// Checks if the frame is from a valid fix.
    pub fn is_valid(&self) -> bool {
        self.status == FixStatus::Active
    }

    /// Gets the number of satellites connected.
    pub fn satellites(&self) -> u8 {
        self.satellites
    }

    /// Gets the latitude of the GPS antenna, in *°* (degrees).
    pub fn latitude(&self) -> f32 {
        self.latitude
    }

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

    /// Gets the altitude of the GPS antenna from sea level, in *m*.
    pub fn altitude(&self) -> f32 {
        self.altitude
    }

    /// Gets the position dilution of precision (3D).
    pub fn pdop(&self) -> f32 {
        self.pdop
    }

    /// Gets the horizontal dilution of precision (2D).
    pub fn hdop(&self) -> f32 {
        self.hdop
    }

    /// Gets the vertical dilution of precision (1D).
    pub fn vdop(&self) -> f32 {
        self.vdop
    }

    /// Gets the speed of the velocity vector, in *m/s*.
    pub fn speed(&self) -> f32 {
        self.speed
    }

    /// Gets the course of the velocity vector, in *°* (degrees).
    pub fn course(&self) -> f32 {
        self.course
    }
}

/// GPS fix status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FixStatus {
    /// GPS fix active.
    Active,
    /// GPS fix not valid.
    Void,
}

impl FromStr for FixStatus {
    type Err = error::Gps;

    fn from_str(s: &str) -> Result<FixStatus, Self::Err> {
        match s {
            "A" => Ok(FixStatus::Active),
            "V" => Ok(FixStatus::Void),
            _ => Err(error::Gps::InvalidStatus {
                status: s.to_owned(),
            }),
        }
    }
}

impl fmt::Display for FixStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match *self {
                FixStatus::Active => "A",
                FixStatus::Void => "V",
            }
        )
    }
}

#[cfg(test)]
mod tests {
    use super::{FixStatus, GPS};

    /// Checks the GPS status from string conversion.
    #[test]
    fn gps_status_from_str() {
        assert_eq!("A".parse::<FixStatus>().unwrap(), FixStatus::Active);
        assert_eq!("V".parse::<FixStatus>().unwrap(), FixStatus::Void);

        // Check errors.
        assert!("".parse::<FixStatus>().is_err());
        assert!("invalid".parse::<FixStatus>().is_err());
        assert!("a".parse::<FixStatus>().is_err());
        assert!("Ab".parse::<FixStatus>().is_err());
    }

    /// Checks the GPS status to string conversion.
    #[test]
    fn gps_status_display() {
        assert_eq!(format!("{}", FixStatus::Active), "A");
        assert_eq!(format!("{}", FixStatus::Void), "V");
    }

    /// Checks the GPS initialization.
    #[test]
    #[ignore]
    fn gps_initialize() {
        let mut gps = GPS.lock().unwrap();
        gps.initialize().unwrap();
    }
}