1 //
2 // Copyright 2022 Google, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 use std::env;
17 use std::path::{Path, PathBuf};
18 use std::process::Command;
19
main()20 fn main() {
21 let packets_prebuilt = match env::var("LMP_PACKETS_PREBUILT") {
22 Ok(dir) => PathBuf::from(dir),
23 Err(_) => PathBuf::from("lmp_packets.rs"),
24 };
25 if Path::new(packets_prebuilt.as_os_str()).exists() {
26 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
27 let outputted = out_dir.join("lmp_packets.rs");
28 std::fs::copy(
29 packets_prebuilt.as_os_str().to_str().unwrap(),
30 out_dir.join(outputted.file_name().unwrap()).as_os_str().to_str().unwrap(),
31 )
32 .unwrap();
33 } else {
34 generate_packets();
35 }
36 }
37
generate_packets()38 fn generate_packets() {
39 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
40
41 // Find the packetgen tool. Expecting it at CARGO_HOME/bin
42 let packetgen = match env::var("CARGO_HOME") {
43 Ok(dir) => PathBuf::from(dir).join("bin").join("bluetooth_packetgen"),
44 Err(_) => PathBuf::from("bluetooth_packetgen"),
45 };
46
47 if !Path::new(packetgen.as_os_str()).exists() {
48 panic!(
49 "Unable to locate bluetooth packet generator:{:?}",
50 packetgen.as_os_str().to_str().unwrap()
51 );
52 }
53
54 println!("cargo:rerun-if-changed=lmp_packets.pdl");
55 let output = Command::new(packetgen.as_os_str().to_str().unwrap())
56 .arg("--out=".to_owned() + out_dir.as_os_str().to_str().unwrap())
57 .arg("--include=.")
58 .arg("--rust")
59 .arg("lmp_packets.pdl")
60 .output()
61 .unwrap();
62
63 println!(
64 "Status: {}, stdout: {}, stderr: {}",
65 output.status,
66 String::from_utf8_lossy(output.stdout.as_slice()),
67 String::from_utf8_lossy(output.stderr.as_slice())
68 );
69 }
70