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
//! # Fabulous APRS Parser
//!
//! This is a Rust wrapper around Fabulous (or, perhaps, Finnish... or both) APRS Parser, aka 
//! [`libfap`](http://www.pakettiradio.net/libfap/). 
//!
//! To parse a packet:
//!
//! ```rust
//! extern crate aprs; 
//! extern crate fap;
//! use aprs::{Packet, Position, Degrees, Knots}; 
//!
//! let raw = "DISCOF>APT314,RAZOR*,WIDE1*,qAS,GERLCH:/022526h4046.40N/11912.12W-347/001/";
//! let parsed = fap::Packet::new(raw);
//! 
//! match parsed {
//!     Ok(packet) => {
//!         assert_eq!(packet.source(), "DISCOF");
//!         assert_eq!(packet.latitude(), Some(40.7733335));
//!         assert_eq!(packet.longitude(), Some(-119.202));
//!         assert_eq!(packet.course(), Some(Degrees(347.0)));
//!     },
//!     Err(_) => {
//!         panic!("Bad packet!")
//!     }
//! }
//! ```
//!
//! Parsed packet implements `aprs::Packet` trait, see [`aprs` crate documentation](https://docs.rs/aprs) 
//! for details on how to use the returned value. 
//! 
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]

extern crate aprs;

mod bind {
    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
use bind::*;
use aprs::{Packet as AprsPacket, Position, Feet, Knots, KilometersPerHour,
    Meters, Degrees, Fahrenheits, Symbol};
use std::ffi::{CStr, CString, NulError};
use std::os::raw::{c_uint, c_short, c_char};
use std::sync::{Once, ONCE_INIT};
use std::borrow::Cow;
use std::vec::Vec;
use std::fmt;
use std::time::{SystemTime, Duration, UNIX_EPOCH};


#[derive(Debug)]
pub enum Error{
    NulInInputData(NulError),
    Other(String)
}
impl std::error::Error for Error {
    fn description(&self) -> &str {
        match self {
            Error::NulInInputData(_) => "input data must not contain any nulls",
            Error::Other(msg) => msg.as_str(), 
        }
    }

    fn cause(&self) -> Option<&std::error::Error> {
        match self {
            Error::NulInInputData(ref err) => Some(err),
            Error::Other(_) => None, 
        }        
    }
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::NulInInputData(err) => write!(f, "input data must not contain any nulls: {}", err),
            Error::Other(msg) => write!(f, "{}", msg), 
        }        
    }
}

static INIT: Once = ONCE_INIT;

#[derive(Debug)]
pub struct Packet { 
    ptr: *mut fap_packet_t, 
}

impl Drop for Packet {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { fap_free(self.ptr); }                
        }
    }   
}

impl Packet {
    pub fn new<T: Into<Vec<u8>>>(data: T) -> Result<Packet, Error> {
        let data = data.into();
        let len = data.len();
        let data = CString::new(data).map_err(|e| Error::NulInInputData(e))?;
        unsafe {
            INIT.call_once(|| {
                fap_init();
            });
            let ptr = fap_parseaprs(data.as_ptr() as *const c_char, len as c_uint, 0 as c_short);
            if ptr.is_null() {
                return Err(Error::Other("libfap returned null value - allocation failure?".to_string()))
            }   
            let packet = Packet{ ptr }; 
            if !packet.fap().error_code.is_null() {
                let buf = &mut [0 as i8; 64];
                fap_explain_error(*packet.fap().error_code, buf.as_mut_ptr() as *mut c_char);
                let msg = CStr::from_ptr(buf.as_ptr() as *mut c_char);
                return Err(Error::Other(msg.to_string_lossy().into_owned()));
            }
            Ok(packet) 
        }       
    }

    fn fap(&self) -> fap_packet_t {
        debug_assert!(!self.ptr.is_null());
        unsafe { *self.ptr }
    }

    pub fn header(&self) -> Cow<str> {
        debug_assert!(!self.fap().header.is_null());
        unsafe{ CStr::from_ptr(self.fap().header) }.to_string_lossy()
    }

    pub fn body(&self) -> Cow<str>  {
        debug_assert!(!self.fap().body.is_null());
        unsafe{ CStr::from_ptr(self.fap().body) }.to_string_lossy()
    }
}

impl AprsPacket for Packet {
    fn source(&self) -> Cow<str> {
        debug_assert!(!self.fap().src_callsign.is_null());
        unsafe{ CStr::from_ptr(self.fap().src_callsign) }.to_string_lossy()
    }

    fn symbol(&self) -> Symbol {
        Symbol::from_table(self.fap().symbol_table as u8, self.fap().symbol_code as u8)
    }

    fn timestamp(&self) -> Option<SystemTime> {
        if self.fap().timestamp.is_null() {
            return None
        }
        let ts = unsafe{*self.fap().timestamp };
        Some(UNIX_EPOCH + Duration::from_secs(ts as u64))
    }

    fn destination(&self) -> Option<Cow<str>> {
        if self.fap().dst_callsign.is_null() {
            return None
        }
        Some(unsafe{ CStr::from_ptr(self.fap().dst_callsign) }.to_string_lossy())
    }

    fn comment(&self) -> Option<Cow<str>> {
        if self.fap().comment.is_null() {
            return None
        }
        Some(unsafe{ CStr::from_ptr(self.fap().comment) }.to_string_lossy())
    }

    fn latitude(&self) -> Option<f32> {
        if self.fap().latitude.is_null() {
            return None
        }
        Some(unsafe{*self.fap().latitude as f32})
    }

    fn longitude(&self) -> Option<f32> {
        if self.fap().longitude.is_null() {
            return None
        }
        Some(unsafe{*self.fap().longitude as f32})
    }

    fn precision(&self) -> Option<Feet> {
        if self.fap().pos_resolution.is_null() {
            return None
        }
        Some(Feet::from(Meters(unsafe{*self.fap().pos_resolution as f32})))
    }

    fn position(&self) -> Option<aprs::Position> {
        let lat = self.latitude();
        let lng = self.longitude();
        if lat.is_none() || lng.is_none() {
            return None
        }
        match self.precision() {
            Some(p) => Some(Position::from_latlng_precise(lat.unwrap(), lng.unwrap(), p)),
            None => Some(Position::from_latlng(lat.unwrap(), lng.unwrap()))
        }
    }

    fn speed(&self) -> Option<Knots> {
        if self.fap().speed.is_null() {
            return None
        }
        Some(Knots::from(KilometersPerHour(unsafe{*self.fap().speed as f32})))
    }

    fn course(&self) -> Option<Degrees> {
        if self.fap().course.is_null() {
            return None
        }
        let mut v = unsafe{*self.fap().course };
        if v == 0 {
            return None // 0 means "unknown" in libfap
        }
        v = v % 360;
        Some(Degrees(v as f32))
    }

    fn altitude(&self) -> Option<Feet> {
        if self.fap().altitude.is_null() {
            return None
        }
        Some(Feet::from(Meters(unsafe{*self.fap().altitude as f32})))
    }

    fn temperature(&self) -> Option<Fahrenheits> {
        unimplemented!();
    }

    fn wind_direction(&self) -> Option<Degrees> {
        unimplemented!();
    }

    fn wind_speed(&self) -> Option<Knots> {
        unimplemented!();
    }
}

impl fmt::Display for Packet {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:9} {:19} @ {:?}", self.source(), format!("{:?}", self.symbol()), self.position())
    }
}