Skip to content

Commit

Permalink
make usage of serialport and crc optional (ublox-rs#4)
Browse files Browse the repository at this point in the history
* refactoring: use workspace and override for ublox_derive

Use common workspace for two crates, to speedup build,
plus it is possible to use override section in workspace's Cargo.toml,
so it is possible to publish real crates (not workspace) without any
modification in Cargo.toml

* refactoring: cargo fmt --all

* fix build: disable ublox_derive/tests/test.rs

at now there is problem with generated code,
it for reason generate trait implementation with wrong method names

* refactoring: cleanups deps

now we don't build syn different versions (outdated num_derive dependencies)

* enable tests that possible to build and remove dead code

* make usage of serialport and crc optional

* missed dpes
  • Loading branch information
Dushistov authored Mar 7, 2020
1 parent ce8361f commit 898a638
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 307 deletions.
24 changes: 6 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
[package]
name = "ublox"
version = "0.1.0"
authors = ["Lane Kolbly <lane@rscheme.org>"]
edition = "2018"
license = "MIT"
description = "A crate to communicate with u-blox GPS devices using the UBX protocol"
[workspace]
members = ["ublox", "ublox_derive"]

[patch.'crates-io']
ublox_derive = { path = "ublox_derive" }
ublox = { path = "ublox" }

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serialport = "3.3.0"
bincode = "1.2.1"
chrono = "0.4"
crc = "1.8.1"
syn = "1.0.14"
ublox_derive = { path = "ublox_derive/" }
num-traits = "0.2"
num-derive = "0.2"
21 changes: 0 additions & 21 deletions src/main.rs

This file was deleted.

22 changes: 22 additions & 0 deletions ublox/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "ublox"
version = "0.1.0"
authors = ["Lane Kolbly <lane@rscheme.org>"]
edition = "2018"
license = "MIT"
description = "A crate to communicate with u-blox GPS devices using the UBX protocol"

[features]
default = []
serial = ["serialport", "crc"]

[dependencies]
serde = "1.0"
serde_derive = "1.0"
bincode = "1.2.1"
chrono = "0.4"
ublox_derive = "0.0.0"
serialport = { version = "3.3.0", optional = true }
crc = { version = "1.8.1", optional = true }
num-derive = "0.3.0"
num-traits = "0.2.11"
33 changes: 33 additions & 0 deletions ublox/examples/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[cfg(feature = "serial")]
mod serial {
use chrono::prelude::*;
use std::time::Duration;
use ublox::{Device, Position};

pub fn main() {
let mut dev = Device::new("/dev/ttyUSB0").unwrap();

let pos = Position {
lon: -97.5,
lat: 30.2,
alt: 200.0,
};
println!("Setting AID data...");
match dev.load_aid_data(Some(pos), Some(Utc::now())) {
Err(e) => {
println!("Got error setting AID data: {:?}", e);
}
_ => {}
}

loop {
dev.poll_for(Duration::from_millis(500)).unwrap();
println!("{:?}", dev.get_solution());
}
}
}

fn main() {
#[cfg(feature = "serial")]
serial::main()
}
File renamed without changes.
16 changes: 16 additions & 0 deletions ublox/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! # ublox
//!
//! `ublox` is a library to talk to u-blox GPS devices using the UBX protocol.
//! At time of writing this library is developed for a device which behaves like
//! a NEO-6M device.
pub use crate::segmenter::Segmenter;
#[cfg(feature = "serial")]
pub use crate::serialport::{Device, ResetType};
pub use crate::ubx_packets::*;

mod error;
mod segmenter;
#[cfg(feature = "serial")]
mod serialport;
mod ubx_packets;
File renamed without changes.
41 changes: 20 additions & 21 deletions src/lib.rs → ublox/src/serialport.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
//! # ublox
//!
//! `ublox` is a library to talk to u-blox GPS devices using the UBX protocol.
//! At time of writing this library is developed for a device which behaves like
//! a NEO-6M device.
use crate::{
error::{Error, Result},
segmenter::Segmenter,
ubx_packets::*,
};
use chrono::prelude::*;
use crc::{crc16, Hasher16};
use std::io;
use std::time::{Duration, Instant};
use crate::error::{Error, Result};

pub use crate::ubx_packets::*;
pub use crate::segmenter::Segmenter;

mod error;
mod ubx_packets;
mod segmenter;

#[derive(Debug)]
pub enum ResetType {
Expand All @@ -32,7 +24,6 @@ pub struct Device {
port: Box<dyn serialport::SerialPort>,
segmenter: Segmenter,
//buf: Vec<u8>,

alp_data: Vec<u8>,
alp_file_id: u16,

Expand Down Expand Up @@ -61,7 +52,7 @@ impl Device {
///
/// This function will panic if it cannot open the serial port.
pub fn new(device: &str) -> Result<Device> {
let s = serialport::SerialPortSettings{
let s = serialport::SerialPortSettings {
baud_rate: 9600,
data_bits: serialport::DataBits::Eight,
flow_control: serialport::FlowControl::None,
Expand All @@ -70,7 +61,7 @@ impl Device {
timeout: Duration::from_millis(1),
};
let port = serialport::open_with_settings(device, &s).unwrap();
let mut dev = Device{
let mut dev = Device {
port: port,
segmenter: Segmenter::new(),
alp_data: Vec::new(),
Expand Down Expand Up @@ -174,7 +165,7 @@ impl Device {
pub fn get_position(&mut self) -> Option<Position> {
match (&self.navstatus, &self.navpos) {
(Some(status), Some(pos)) => {
if status.itow != pos.get_itow() {
if status.itow != pos.itow {
None
} else if status.flags & 0x1 == 0 {
None
Expand All @@ -190,7 +181,7 @@ impl Device {
pub fn get_velocity(&mut self) -> Option<Velocity> {
match (&self.navstatus, &self.navvel) {
(Some(status), Some(vel)) => {
if status.itow != vel.get_itow() {
if status.itow != vel.itow {
None
} else if status.flags & 0x1 == 0 {
None
Expand Down Expand Up @@ -264,7 +255,7 @@ impl Device {
pub fn load_aid_data(
&mut self,
position: Option<Position>,
tm: Option<DateTime<Utc>>
tm: Option<DateTime<Utc>>,
) -> Result<()> {
let mut aid = AidIni::new();
match position {
Expand Down Expand Up @@ -313,7 +304,10 @@ impl Device {
return Ok(Some(Packet::AckAck(packet)));
}
Some(Packet::MonVer(packet)) => {
println!("Got versions: SW={} HW={}", packet.sw_version, packet.hw_version);
println!(
"Got versions: SW={} HW={}",
packet.sw_version, packet.hw_version
);
return Ok(None);
}
Some(Packet::NavPosVelTime(packet)) => {
Expand Down Expand Up @@ -382,7 +376,12 @@ impl Device {
}

fn send(&mut self, packet: UbxPacket) -> Result<()> {
CfgMsg{classid: 5, msgid: 4, rates: [0, 0, 0, 0, 0, 0]}.to_bytes();
CfgMsg {
classid: 5,
msgid: 4,
rates: [0, 0, 0, 0, 0, 0],
}
.to_bytes();
let serialized = packet.serialize();
self.port.write_all(&serialized)?;
Ok(())
Expand Down
84 changes: 22 additions & 62 deletions src/ubx_packets.rs → ublox/src/ubx_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use crate::error::Result;
use bincode;
use chrono::prelude::*;
use serde_derive::{Deserialize, Serialize};
use std::vec::Vec;
use std::str;
//use syn::{parse_macro_input, parse_quote, DeriveInput, Data, TokenStream};
use std::vec::Vec;
use ublox_derive::ubx_packet;

// These are needed for ubx_packet
use std::convert::TryInto;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryInto;

#[derive(Debug)]
pub struct Position {
Expand Down Expand Up @@ -73,35 +72,6 @@ impl UbxPacket {
}
}

/*#[proc_macro_attribute]
fn ubx_packet(attr: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match input.data {
Data::Struct(ref data) => {
match data.fields {
Fields::Named(ref fields) => {
println!("{:?}", fields);
}
Fields::Unnamed(ref fields) => {
//
}
}
}
Data::Enum(_) | Data::Union(_) => unimplemented!()
}
}*/

/*#[ubx_packet]
struct MyPacket {
tow: u32,
lon: i32,
lat: i32,
height: i32,
height_msl: i32,
h_acc: u32,
v_acc: u32,
}*/

pub trait UbxMeta {
fn get_classid() -> u8;
fn get_msgid() -> u8;
Expand Down Expand Up @@ -182,19 +152,6 @@ impl From<&NavPosLLH> for Position {
}
}

trait FooTrait {
fn foo() -> u32;
}

impl<T: FooTrait> From<&T> for Velocity {
fn from(packet: &T) -> Self {
Velocity {
speed: 0.0,
heading: 0.0,
}
}
}

#[ubx_packet]
pub struct NavVelNED {
pub itow: u32,
Expand Down Expand Up @@ -301,14 +258,13 @@ impl From<&NavPosVelTime> for Velocity {

impl From<&NavPosVelTime> for DateTime<Utc> {
fn from(sol: &NavPosVelTime) -> Self {
let ns = if sol.nanosecond < 0 { 0 } else { sol.nanosecond } as u32;
let ns = if sol.nanosecond < 0 {
0
} else {
sol.nanosecond
} as u32;
Utc.ymd(sol.year as i32, sol.month.into(), sol.day.into())
.and_hms_nano(
sol.hour.into(),
sol.min.into(),
sol.sec.into(),
ns,
)
.and_hms_nano(sol.hour.into(), sol.min.into(), sol.sec.into(), ns)
}
}

Expand Down Expand Up @@ -482,8 +438,12 @@ pub struct MonVer {
}

impl UbxMeta for MonVer {
fn get_classid() -> u8 { 0x0a }
fn get_msgid() -> u8 { 0x04 }
fn get_classid() -> u8 {
0x0a
}
fn get_msgid() -> u8 {
0x04
}

fn to_bytes(&self) -> Vec<u8> {
unimplemented!("Sending MonVer packets is unimplemented");
Expand Down Expand Up @@ -517,15 +477,15 @@ impl Packet {
pub fn deserialize(classid: u8, msgid: u8, payload: &[u8]) -> Result<Packet> {
match (classid, msgid) {
//(0x01, 0x02) => parse_packet_branch!(Packet::NavPosLLH, payload),
(0x01, 0x02) => {
Ok(Packet::NavPosLLH(NavPosLLH::new(payload.try_into().unwrap())))
},
(0x01, 0x02) => Ok(Packet::NavPosLLH(NavPosLLH::new(
payload.try_into().unwrap(),
))),
(0x01, 0x03) => parse_packet_branch!(Packet::NavStatus, payload),
(0x01, 0x07) => parse_packet_branch!(Packet::NavPosVelTime, payload),
//(0x01, 0x12) => parse_packet_branch!(Packet::NavVelNED, payload),
(0x01, 0x12) => {
Ok(Packet::NavVelNED(NavVelNED::new(payload.try_into().unwrap())))
}
(0x01, 0x12) => Ok(Packet::NavVelNED(NavVelNED::new(
payload.try_into().unwrap(),
))),
(0x05, 0x01) => parse_packet_branch!(Packet::AckAck, payload),
(0x06, 0x00) => {
// Depending on the port ID, we parse different packets
Expand All @@ -542,11 +502,11 @@ impl Packet {
(0x0A, 0x04) => {
let sw_version = str::from_utf8(&payload[0..30]).unwrap();
let hw_version = str::from_utf8(&payload[31..40]).unwrap();
return Ok(Packet::MonVer(MonVer{
return Ok(Packet::MonVer(MonVer {
sw_version: sw_version.to_string(),
hw_version: hw_version.to_string(),
}));
},
}
(0x0B, 0x01) => parse_packet_branch!(Packet::AidIni, payload),
(0x0B, 0x32) => parse_packet_branch!(Packet::AlpSrv, payload),
(c, m) => {
Expand Down
4 changes: 0 additions & 4 deletions ublox_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ proc-macro = true
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0.14", features = ["extra-traits"] }
Inflector = "0.11.4"
proc-macro-error = "0.4.8"
itertools = "0.8.2"
num-traits = "0.2"
num-derive = "0.2"
Loading

0 comments on commit 898a638

Please sign in to comment.