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
extern crate serde;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_json;
extern crate chrono;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate log;
use std::net::{ToSocketAddrs, TcpStream};
use std::io::{BufRead, BufReader, Write};
use std::time::Duration;
pub mod errors {
error_chain! {
types {
Error, ErrorKind, ResultExt, GpsdResult;
}
foreign_links {
Io(::std::io::Error);
Serde(::serde_json::Error);
}
errors {
DeserFailed(s: String, e: ::serde_json::Error) {
display("failed to deserialize text '{}': {}", s, e)
}
GpsdFailed(s: String) {
display("gpsd connection closed")
}
}
}
}
pub use errors::GpsdResult;
pub mod types;
#[cfg(test)]
pub mod tests;
use types::*;
pub struct GpsdConnection {
raw_data: bool,
inner: BufReader<TcpStream>
}
impl GpsdConnection {
pub fn new<A: ToSocketAddrs>(addr: A) -> GpsdResult<Self> {
let stream = TcpStream::connect(addr)?;
let inner = BufReader::new(stream);
Ok(Self { inner, raw_data: false })
}
fn _watch(&mut self, watch: bool, json: bool, raw: u8) -> GpsdResult<()> {
let stream = self.inner.get_mut();
let watch_data = json!({
"class": "WATCH",
"enable": watch,
"json": json,
"raw": raw,
});
self.raw_data = raw > 0;
let msg = format!("?WATCH={}\n", watch_data.to_string());
stream.write_all(msg.as_bytes())?;
Ok(())
}
pub fn watch(&mut self, watch: bool) -> GpsdResult<()> {
self._watch(watch, true, 0)
}
pub fn watch_raw(&mut self, watch: bool, json: bool, raw: u8) -> GpsdResult<()> {
self._watch(watch, json, raw)
}
pub fn poll(&mut self) -> GpsdResult<()> {
let stream = self.inner.get_mut();
stream.write_all("?POLL;\n".as_bytes())?;
Ok(())
}
pub fn version(&mut self) -> GpsdResult<()> {
let stream = self.inner.get_mut();
stream.write_all("?VERSION;\n".as_bytes())?;
Ok(())
}
pub fn devices(&mut self) -> GpsdResult<()> {
let stream = self.inner.get_mut();
stream.write_all("?DEVICES;\n".as_bytes())?;
Ok(())
}
pub fn set_read_timeout(&mut self, dur: Option<Duration>) -> GpsdResult<()> {
self.inner.get_ref().set_read_timeout(dur)?;
Ok(())
}
pub fn get_response(&mut self) -> GpsdResult<Response> {
loop {
let mut buf = String::new();
let read_result = self.inner.read_line(&mut buf);
if let Ok(size) = read_result {
if size == 0 {
bail!(errors::ErrorKind::GpsdFailed(String::from("Gpsd Connection Closed")));
}
}
if buf == "" {
debug!("empty line received from GPSD");
continue;
}
debug!("raw GPSD data: {}", buf);
let data = serde_json::from_str(&buf);
debug!("serde output: {:?}", data);
match data {
Err(e) => {
if self.raw_data {
return Ok(Response::Raw(buf))
} else {
debug!("deserializing response failed: {:?}", e);
bail!(errors::ErrorKind::DeserFailed(buf, e));
}
},
Ok(x) => return Ok(x)
}
}
}
}