gpsd-json

A Rust library for parsing GPSD JSON protocol messages without dependencies on libgps.
Installation
Add this to your Cargo.toml:
[dependencies]
gpsd-json = "0.1.0"
Quick Start
Async (tokio)
use gpsd_json::client::{GpsdClient, StreamOptions};
use futures::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = GpsdClient::connect("127.0.0.1:2947").await?;
let mut stream = client.stream(StreamOptions::json()).await?;
while let Some(Ok(msg)) = stream.next().await {
println!("Received: {:?}", msg);
}
Ok(())
}
Blocking
use gpsd_json::client::{blocking, StreamOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = blocking::GpsdClient::connect("127.0.0.1:2947")?;
let mut stream = client.stream(StreamOptions::json())?;
while let Some(Ok(msg)) = stream.next() {
println!("Received: {:?}", msg);
}
Ok(())
}
Features
- Pure Rust - No C dependencies or libgps required
- Type-safe - Leverage Rust's type system for safe GPS data handling
- Multiple protocols - Support for JSON, NMEA, and raw data streams
- Async and Blocking - Both async (tokio) and blocking I/O support
- Streaming API - Efficient iterator-based data processing
- Flexible configuration - Fine-grained control over data streams
Usage
JSON Stream (Async)
use gpsd_json::{
client::{GpsdClient, StreamOptions},
protocol::v3::ResponseMessage,
};
use futures::StreamExt;
#[tokio::main]
async fn main() {
let mut client = GpsdClient::connect("127.0.0.1:2947").await.unwrap();
let opts = StreamOptions::json()
.pps(true) .timing(true);
let mut stream = client.stream(opts).await.unwrap();
while let Some(Ok(msg)) = stream.next().await {
match msg {
ResponseMessage::Tpv(tpv) => {
if let (Some(lat), Some(lon)) = (tpv.lat, tpv.lon) {
println!("Position: lat {}, lon {}", lat, lon);
}
}
ResponseMessage::Sky(sky) => {
println!("Satellites in view: {}", sky.satellites.len());
}
_ => {}
}
}
}
Raw Data Stream (Async)
use gpsd_json::client::{GpsdClient, StreamOptions};
use futures::StreamExt;
#[tokio::main]
async fn main() {
let mut client = GpsdClient::connect("127.0.0.1:2947").await.unwrap();
let opts = StreamOptions::raw();
let mut stream = client.stream(opts).await.unwrap();
while let Some(Ok(data)) = stream.next().await {
let msg = String::from_utf8_lossy(&data);
println!("Raw data: {}", msg.trim_end());
}
}
Examples
See the examples directory for more usage examples:
tcp_simple.rs - Async TCP connection and JSON streaming with tokio
tcp_blocking.rs - Blocking TCP connection and JSON streaming
raw_stream.rs - Raw data streaming example with async
Documentation
For detailed API documentation, please visit docs.rs/gpsd-json.
Requirements
- Rust 1.85 or later (2024 edition)
- Running GPSD instance (for actual GPS data)
License
This project is licensed under the BSD-2-Clause License, following GPSD's licensing terms. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.