Skip to content

Commit aea2a06

Browse files
authored
Adding support for other message sets (mavlink#40)
* MavMessage::parse now maps results instead of unwrapping them, so there's no panic * also compile ardupilotmega.xml * rustfmt * updated connections to be generic * it builds! support for ardupilotmega messages added, still need parsing + tests * it all works! it also no longer compiles on rust 1.40.0 for some reason, but it works fine on rust stable 1.41.1 so i don't think it's a big deal * documented features, added build binder * fixed Cargo.toml Signed-off-by: Ibiyemi Abiodun <[email protected]> * added ability to patch mavlink schemas Signed-off-by: Ibiyemi Abiodun <[email protected]> * removed extraneous println statements Signed-off-by: Ibiyemi Abiodun <[email protected]> * mavlink should no longer rebuild every single time Signed-off-by: Ibiyemi Abiodun <[email protected]> * fixed extra_crc Signed-off-by: Ibiyemi Abiodun <[email protected]> * fixed travis ci Signed-off-by: Ibiyemi Abiodun <[email protected]> * fixed travis ci again? Signed-off-by: Ibiyemi Abiodun <[email protected]> * fixed merge mistake Signed-off-by: Ibiyemi Abiodun <[email protected]> * rustfmt + fix merge mistake part 2 Signed-off-by: Ibiyemi Abiodun <[email protected]> * updated readme and crate documentation Signed-off-by: Ibiyemi Abiodun <[email protected]> * tests are now feature-gated + added common feature Signed-off-by: Ibiyemi Abiodun <[email protected]> * feature-gated the rest of the tests + added common as a default feature Signed-off-by: Ibiyemi Abiodun <[email protected]> * build with all features on docs.rs Signed-off-by: Ibiyemi Abiodun <[email protected]> * fixed imports in tests Signed-off-by: Ibiyemi Abiodun <[email protected]>
1 parent 9f665ea commit aea2a06

23 files changed

+971
-526
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
language: rust
22
cache: cargo
3+
script:
4+
- cargo build --verbose --all-features
5+
- cargo test --verbose --all-features

Cargo.toml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
[package]
33
name = "mavlink"
4-
version = "0.7.0"
5-
authors = ["Todd Stellanova", "Michal Podhradsky", "Kevin Mehall", "Tim Ryan", "Patrick José Pereira"]
4+
version = "0.8.0"
5+
authors = ["Todd Stellanova", "Michal Podhradsky", "Kevin Mehall", "Tim Ryan", "Patrick José Pereira", "Ibiyemi Abiodun"]
66
build = "build/main.rs"
77
description = "Implements the MAVLink data interchange format for UAVs."
88
readme = "README.md"
@@ -15,12 +15,12 @@ crc16 = "0.3.3"
1515
bytes = "0.4"
1616
xml-rs = "0.2"
1717
quote = "0.3"
18-
rustfmt = "0.9"
1918
lazy_static = "1.2.0"
20-
serde = { version = "1.0.101", optional = true }
19+
serde = { version = "1.0.101", optional = true, features = ["derive"] }
2120

2221
[[bin]]
2322
name = "mavlink-dump"
23+
required-features = ["common"]
2424

2525
[dependencies]
2626
crc16 = "0.3.3"
@@ -30,17 +30,33 @@ num-derive = "0.2"
3030
bitflags = "1.0.4"
3131
serial = { version = "0.4", optional = true }
3232
serde = { version = "1.0.101", optional = true }
33-
34-
[dependencies.byteorder]
35-
version = "0.5.3"
36-
optional = true
37-
33+
byteorder = { version = "0.5", optional = true }
3834

3935
[features]
36+
"ardupilotmega" = []
37+
"asluav" = []
38+
"autoquad" = []
39+
"matrixpilot" = []
40+
"minimal" = []
41+
"paparazzi" = []
42+
"python_array_test" = []
43+
"slugs" = []
44+
"standard" = []
45+
"test" = []
46+
"ualberta" = []
47+
"uavionix" = []
48+
"icarous" = []
49+
"common" = []
50+
4051
"emit-description" = []
4152
"emit-extensions" = []
4253
"std" = ["byteorder"]
4354
"udp" = []
4455
"tcp" = []
4556
"direct-serial" = []
46-
default= ["std", "tcp", "udp", "direct-serial", "serial", "serde"]
57+
default= ["std", "tcp", "udp", "direct-serial", "serial", "serde", "common"]
58+
59+
# build with all features on docs.rs so that users viewing documentation
60+
# can see everything
61+
[package.metadata.docs.rs]
62+
all-features = true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
[![Documentation](https://docs.rs/mavlink/badge.svg)](https://docs.rs/mavlink)
66

77
Rust implementation of the [MAVLink](http://qgroundcontrol.org/mavlink/start) UAV messaging protocol,
8-
with bindings for the [common message set](http://mavlink.org/messages/common).
8+
with bindings for all message sets.
99

1010
Add to your Cargo.toml:
1111

1212
```
13-
mavlink = "0.7"
13+
mavlink = "0.8"
1414
```
1515

1616
## Examples

build/binder.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use quote::Ident;
2+
use std::io::Write;
3+
4+
pub fn generate<W: Write>(modules: Vec<String>, out: &mut W) {
5+
let modules_tokens = modules.into_iter().map(|module| {
6+
let file_name = module.clone() + ".rs";
7+
let module_ident = Ident::from(module.clone());
8+
9+
quote! {
10+
#[allow(non_camel_case_types)]
11+
#[allow(non_snake_case)]
12+
#[allow(unused_variables)]
13+
#[allow(unused_mut)]
14+
#[cfg(feature = #module)]
15+
pub mod #module_ident {
16+
use crate::MavlinkVersion; //TODO verify
17+
include!(#file_name);
18+
}
19+
}
20+
});
21+
22+
let tokens = quote! {
23+
#(#modules_tokens)*
24+
};
25+
26+
writeln!(out, "{}", tokens).unwrap();
27+
}

build/main.rs

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
#![recursion_limit="256"]
1+
#![recursion_limit = "256"]
22
#[macro_use]
33
extern crate quote;
44

55
extern crate crc16;
66
extern crate xml;
77

8+
mod binder;
89
mod parser;
10+
mod util;
911

12+
use crate::util::to_module_name;
1013
use std::env;
11-
use std::fs::File;
14+
use std::fs::{read_dir, File};
1215
use std::path::{Path, PathBuf};
1316
use std::process::Command;
1417

@@ -23,28 +26,89 @@ pub fn main() {
2326
.current_dir(&src_dir)
2427
.status()
2528
{
26-
Ok(content) => println!("{}", content),
29+
Ok(_) => {}
2730
Err(error) => eprintln!("{}", error),
2831
}
2932

33+
// find & apply patches to XML definitions to avoid crashes
34+
let mut patch_dir = src_dir.to_path_buf();
35+
patch_dir.push("build/patches");
36+
let mut mavlink_dir = src_dir.to_path_buf();
37+
mavlink_dir.push("mavlink");
38+
39+
if let Ok(dir) = read_dir(patch_dir) {
40+
for entry in dir {
41+
if let Ok(entry) = entry {
42+
match Command::new("git")
43+
.arg("apply")
44+
.arg(entry.path().as_os_str())
45+
.current_dir(&mavlink_dir)
46+
.status()
47+
{
48+
Ok(_) => (),
49+
Err(error) => eprintln!("{}", error),
50+
}
51+
}
52+
}
53+
}
54+
3055
let mut definitions_dir = src_dir.to_path_buf();
3156
definitions_dir.push("mavlink/message_definitions/v1.0");
32-
let definition_file = PathBuf::from("common.xml");
33-
let mut definition_rs = definition_file.clone();
34-
definition_rs.set_extension("rs");
35-
36-
let in_path = Path::new(&definitions_dir).join(&definition_file);
37-
let mut inf = File::open(&in_path).unwrap();
3857

3958
let out_dir = env::var("OUT_DIR").unwrap();
40-
let dest_path = Path::new(&out_dir).join(definition_rs);
41-
let mut outf = File::create(&dest_path).unwrap();
4259

43-
parser::generate(&mut inf, &mut outf);
60+
let mut modules = vec![];
61+
62+
for entry in read_dir(&definitions_dir).expect("could not read definitions directory") {
63+
let entry = entry.expect("could not read directory entry");
64+
65+
let definition_file = entry.file_name();
66+
let module_name = to_module_name(&definition_file);
67+
68+
let mut definition_rs = PathBuf::from(&module_name);
69+
definition_rs.set_extension("rs");
70+
71+
modules.push(module_name);
72+
73+
let in_path = Path::new(&definitions_dir).join(&definition_file);
74+
let mut inf = File::open(&in_path).unwrap();
4475

45-
// Re-run build if common.xml changes
46-
println!(
47-
"cargo:rerun-if-changed={}",
48-
definition_file.file_name().unwrap().to_str().unwrap()
49-
);
76+
let dest_path = Path::new(&out_dir).join(definition_rs);
77+
let mut outf = File::create(&dest_path).unwrap();
78+
79+
// generate code
80+
parser::generate(&mut inf, &mut outf);
81+
82+
// format code
83+
match Command::new("rustfmt")
84+
.arg(dest_path.as_os_str())
85+
.current_dir(&out_dir)
86+
.status()
87+
{
88+
Ok(_) => (),
89+
Err(error) => eprintln!("{}", error),
90+
}
91+
92+
// Re-run build if common.xml changes
93+
println!("cargo:rerun-if-changed={}", entry.path().to_string_lossy());
94+
}
95+
96+
// output mod.rs
97+
{
98+
let dest_path = Path::new(&out_dir).join("mod.rs");
99+
let mut outf = File::create(&dest_path).unwrap();
100+
101+
// generate code
102+
binder::generate(modules, &mut outf);
103+
104+
// format code
105+
match Command::new("rustfmt")
106+
.arg(dest_path.as_os_str())
107+
.current_dir(&out_dir)
108+
.status()
109+
{
110+
Ok(_) => (),
111+
Err(error) => eprintln!("{}", error),
112+
}
113+
}
50114
}

0 commit comments

Comments
 (0)