Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ pragma_once = true
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
cpp_compat = true

# C++-only re-declaration that adds default arguments to viam_dial's trailing
# params, letting C++ callers (e.g. the C++ SDK) keep using the original 7-arg
# signature. C callers must pass all arguments. Keep this parameter list in
# sync with `viam_dial` in src/ffi/dial_ffi.rs — drift will surface as a C++
# redeclaration mismatch at the caller's compile time.
trailer = '''
#ifdef __cplusplus
extern "C" char *viam_dial(const char *c_uri,
const char *c_entity,
const char *c_type,
const char *c_payload,
bool c_allow_insec,
float c_timeout,
struct viam_dial_ffi *rt_ptr,
bool c_force_relay = false,
bool c_force_p2p = false,
const char *c_turn_uri = nullptr);
#endif
'''


############################ Code Style Options ################################

Expand Down
66 changes: 55 additions & 11 deletions src/ffi/dial_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,37 @@
uri: String,
allow_insec: bool,
disable_webrtc: bool,
force_relay: bool,
force_p2p: bool,
turn_uri: Option<String>,
) -> Result<DialBuilder<WithoutCredentials>> {
let c = DialOptions::builder().uri(&uri).without_credentials();
let c = if disable_webrtc {
c.disable_webrtc()
} else {
c
};
let c = if disable_webrtc { c.disable_webrtc() } else { c };
let c = if allow_insec { c.allow_downgrade() } else { c };
let c = if force_relay { c.force_relay() } else { c };
let c = if force_p2p { c.force_p2p() } else { c };
let c = if let Some(u) = turn_uri { c.turn_uri(u) } else { c };
Ok(c)
}

fn dial_with_cred(
uri: String,
entity: Option<String>,
r#type: &str,
payload: &str,
allow_insec: bool,
disable_webrtc: bool,
force_relay: bool,
force_p2p: bool,
turn_uri: Option<String>,
) -> Result<DialBuilder<WithCredentials>> {

Check warning on line 107 in src/ffi/dial_ffi.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (9/7)

warning: this function has too many arguments (9/7) --> src/ffi/dial_ffi.rs:97:1 | 97 | / fn dial_with_cred( 98 | | uri: String, 99 | | entity: Option<String>, 100 | | r#type: &str, ... | 106 | | turn_uri: Option<String>, 107 | | ) -> Result<DialBuilder<WithCredentials>> { | |_________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default
let creds = RPCCredentials::new(entity, String::from(r#type), String::from(payload));
let c = DialOptions::builder().uri(&uri).with_credentials(creds);
let c = if disable_webrtc {
c.disable_webrtc()
} else {
c
};
let c = if disable_webrtc { c.disable_webrtc() } else { c };
let c = if allow_insec { c.allow_downgrade() } else { c };
let c = if force_relay { c.force_relay() } else { c };
let c = if force_p2p { c.force_p2p() } else { c };
let c = if let Some(u) = turn_uri { c.turn_uri(u) } else { c };
Ok(c)
}

Expand All @@ -124,6 +128,15 @@
/// * `c_allow_insecure` a bool, set to true when allowing insecure connection to your robot
/// * `c_timeout` a float, set how many seconds we should try to dial before timing out
/// * `rt_ptr` a pointer to a rust runtime previously obtained with init_rust_runtime
//
// NOTE: C++ SDK's existing 7-arg call site of viam_dial relies on `rt_ptr` keeping its original trailing position; the following
// args are appended so cbindgen.toml's C++ default-argument trailer can fill them in for 7-arg callers.
//
/// * `c_force_relay` a bool, set to true to force ICE relay-only policy (TURN candidates only)
/// * `c_force_p2p` a bool, set to true to strip TURN servers and force host/reflexive candidates only
/// * `c_turn_uri` a C-style string with a TURN URI filter (e.g. "turn:turn.viam.com:443"); set to
/// NULL or empty to use all TURN servers. The filter matches TURN URLs by scheme, host, port,
/// and transport (transport defaults to "udp" if unspecified).
#[no_mangle]
pub unsafe extern "C" fn viam_dial(
c_uri: *const c_char,
Expand All @@ -133,6 +146,9 @@
c_allow_insec: bool,
c_timeout: f32,
rt_ptr: Option<&mut DialFfi>,
c_force_relay: bool,
c_force_p2p: bool,
c_turn_uri: *const c_char,
) -> *mut c_char {
let uri = {
if c_uri.is_null() {
Expand Down Expand Up @@ -215,6 +231,20 @@
};
let timeout_duration = Duration::from_secs_f32(c_timeout);

let turn_uri_opt = {
match c_turn_uri.is_null() {
true => None,
false => match CStr::from_ptr(c_turn_uri).to_str() {
Ok(s) if !s.is_empty() => Some(s.to_string()),
Ok(_) => None,
Err(e) => {
log::error!("Error parsing turn_uri string: {:?}", e);
return ptr::null_mut();
}
},
}
};

let (server, channel) = match runtime.block_on(async move {
let channel = match (r#type, payload) {
(Some(t), Some(p)) => {
Expand All @@ -227,6 +257,9 @@
p.to_str()?,
allow_insec,
disable_webrtc,
c_force_relay,
c_force_p2p,
turn_uri_opt,
)?
.connect(),
)
Expand All @@ -235,7 +268,15 @@
(None, None) => {
timeout(
timeout_duration,
dial_without_cred(uri_str, allow_insec, disable_webrtc)?.connect(),
dial_without_cred(
uri_str,
allow_insec,
disable_webrtc,
c_force_relay,
c_force_p2p,
turn_uri_opt,
)?
.connect(),
)
.await?
}
Expand Down Expand Up @@ -272,24 +313,24 @@
let server = server.with_graceful_shutdown(async {
rx.await.ok();
});
let _ = runtime.spawn(async {
let _ = server.await;
});

Check warning on line 318 in src/ffi/dial_ffi.rs

View workflow job for this annotation

GitHub Actions / clippy

non-binding `let` on a future

warning: non-binding `let` on a future --> src/ffi/dial_ffi.rs:316:5 | 316 | / let _ = runtime.spawn(async { 317 | | let _ = server.await; 318 | | }); | |_______^ | = help: consider awaiting the future or dropping explicitly with `std::mem::drop` = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#let_underscore_future = note: `#[warn(clippy::let_underscore_future)]` on by default
ctx.push_signal(tx);
path.into_raw()
}

#[no_mangle]
#[deprecated]
pub unsafe extern "C" fn dial(
c_uri: *const c_char,
c_entity: *const c_char,
c_type: *const c_char,
c_payload: *const c_char,
c_allow_insec: bool,
c_timeout: f32,
rt_ptr: Option<&mut DialFfi>,
) -> *mut c_char {

Check warning on line 333 in src/ffi/dial_ffi.rs

View workflow job for this annotation

GitHub Actions / clippy

unsafe function's docs are missing a `# Safety` section

warning: unsafe function's docs are missing a `# Safety` section --> src/ffi/dial_ffi.rs:325:1 | 325 | / pub unsafe extern "C" fn dial( 326 | | c_uri: *const c_char, 327 | | c_entity: *const c_char, 328 | | c_type: *const c_char, ... | 332 | | rt_ptr: Option<&mut DialFfi>, 333 | | ) -> *mut c_char { | |________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#missing_safety_doc = note: `#[warn(clippy::missing_safety_doc)]` on by default
viam_dial(
c_uri,
c_entity,
Expand All @@ -298,6 +339,9 @@
c_allow_insec,
c_timeout,
rt_ptr,
false,
false,
ptr::null(),
)
}

Expand All @@ -318,7 +362,7 @@

#[no_mangle]
#[deprecated]
pub unsafe extern "C" fn free_string(s: *mut c_char) {

Check warning on line 365 in src/ffi/dial_ffi.rs

View workflow job for this annotation

GitHub Actions / clippy

unsafe function's docs are missing a `# Safety` section

warning: unsafe function's docs are missing a `# Safety` section --> src/ffi/dial_ffi.rs:365:1 | 365 | pub unsafe extern "C" fn free_string(s: *mut c_char) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#missing_safety_doc
viam_free_string(s)
}

Expand Down
Loading