rework lib

This commit is contained in:
2025-11-14 22:18:15 +01:00
parent 77d5454629
commit 5613696b89
5 changed files with 852 additions and 287 deletions
Generated
+25
View File
@@ -2,6 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]] [[package]]
name = "connect_demo" name = "connect_demo"
version = "0.0.0" version = "0.0.0"
@@ -9,6 +15,25 @@ dependencies = [
"open_wrapper", "open_wrapper",
] ]
[[package]]
name = "libloading"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60"
dependencies = [
"cfg-if",
"windows-link",
]
[[package]] [[package]]
name = "open_wrapper" name = "open_wrapper"
version = "0.1.0" version = "0.1.0"
dependencies = [
"libloading",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
+4 -1
View File
@@ -2,4 +2,7 @@
name = "open_wrapper" name = "open_wrapper"
version = "0.1.0" version = "0.1.0"
edition.workspace = true edition.workspace = true
build = "build.rs"
[dependencies]
libloading = "0.9.0"
-20
View File
@@ -1,20 +0,0 @@
use std::{env, path::PathBuf};
fn main() {
let manifest_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set"));
let lib_dir = manifest_dir.join("lib");
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rerun-if-changed=build.rs");
let open_lib = lib_dir.join("open.lib");
if open_lib.exists() {
println!("cargo:rerun-if-changed={}", open_lib.display());
} else {
println!(
"cargo:warning=Expected to find open.lib in '{}'. Copy the vendor import library there.",
lib_dir.display()
);
}
}
+803 -168
View File
File diff suppressed because it is too large Load Diff
+21 -99
View File
@@ -1,108 +1,30 @@
use std::io::{ self, Write }; use std::{ env, path::PathBuf, process };
use open_wrapper::{ self, OpenReturnCodes }; use open_wrapper::OpenApi;
fn main() { fn main() {
println!("Rust connect demo for open.dll"); let dll_path = env
println!("(make sure open.dll is discoverable and open.lib is linked)\n"); ::args_os()
print_menu(); .nth(1)
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("open_wrapper/lib/open.dll"));
let stdin = io::stdin(); println!("Loading Open API from '{}'", dll_path.display());
let mut state = ConnectionState::default(); let api = match (unsafe { OpenApi::load(&dll_path) }) {
Ok(api) => api,
Err(err) => {
eprintln!("Failed to load '{}': {err}", dll_path.display());
process::exit(1);
}
};
loop { println!("Calling connect() ...");
print!("> "); let rc = unsafe { api.connect() };
if io::stdout().flush().is_err() {
eprintln!("Failed to flush stdout.");
break;
}
let mut line = String::new();
if stdin.read_line(&mut line).is_err() {
eprintln!("Failed to read input, exiting.");
break;
}
let cmd = line.trim().to_lowercase();
if cmd.is_empty() {
continue;
}
match cmd.as_str() {
"1" | "c" | "connect" => handle_connect(&mut state),
"2" | "d" | "disconnect" => handle_disconnect(&mut state),
// "3" | "g" | "get-speed" => handle_get_speed(),
// "4" | "r" | "realtime" => handle_set_speed(100, "SetSimulationSpeed (100%)"),
// "5" | "m" | "max" => handle_set_speed(u32::MAX, "SetSimulationSpeed (u32::MAX)"),
"h" | "help" | "?" => print_menu(),
"q" | "quit" | "exit" => {
println!("Bye!");
break;
}
_ => println!("Unknown command. Type `help` to see the list of commands."),
}
}
}
#[derive(Default)]
struct ConnectionState {
connected: bool,
}
fn handle_connect(state: &mut ConnectionState) {
if state.connected {
println!("Already connected. Run `disconnect` first if you need a fresh session.");
return;
}
let rc = call_and_report("Connect", || unsafe { open_wrapper::connect() });
if rc.is_ok() { if rc.is_ok() {
state.connected = true; println!("connect() succeeded with status {rc:?}");
} else {
eprintln!("connect() failed with status {rc:?}");
process::exit(2);
} }
} }
fn handle_disconnect(state: &mut ConnectionState) {
if !state.connected {
println!("Not connected yet. Use `connect` first.");
return;
}
let rc = call_and_report("Disconnect", || unsafe { open_wrapper::disconnect() });
if rc.is_ok() {
state.connected = false;
}
}
// fn handle_get_speed() {
// println!("GetSimulationSpeed called ...");
// let mut sim_speed = 0u32;
// let rc = unsafe { open_wrapper::get_simulation_speed(&mut sim_speed) };
// if rc.is_ok() {
// println!("Current simulation speed: {sim_speed}%");
// }
// println!("GetSimulationSpeed returned: {rc:?}");
// }
// fn handle_set_speed(value: u32, label: &str) {
// println!("{label} called ...");
// let rc = unsafe { open_wrapper::set_simulation_speed(value) };
// println!("{label} returned: {rc:?}");
// }
fn call_and_report(action: &str, mut op: impl FnMut() -> OpenReturnCodes) -> OpenReturnCodes {
println!("{action} called ...");
let rc = op();
println!("{action} returned: {rc:?}");
rc
}
fn print_menu() {
println!("Commands:");
println!(" (1) connect - connect to the running controller");
println!(" (2) disconnect - disconnect the session");
println!(" (3) get-speed - read the current simulation speed");
println!(" (4) realtime - set speed to 100%");
println!(" (5) max - set speed to u32::MAX");
println!(" (h) help - show this list");
println!(" (q) quit - exit the demo");
}