Skip to content

Commit d41a368

Browse files
authored
RSDK-12650 - use rust-utils header (#507)
1 parent 7b7c69b commit d41a368

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,31 @@ find_package(Threads REQUIRED)
253253
# `target_link_directories` call down in src/CMakeLists.txt, as it
254254
# will no longer be needed.
255255
set(viam_rust_utils_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}viam_rust_utils${CMAKE_STATIC_LIBRARY_SUFFIX})
256+
set(viam_rust_utils_header_file ${CMAKE_CURRENT_BINARY_DIR}/viam_rust_utils.h)
256257

257258
file(GLOB viam_rust_utils_files ${PROJECT_SOURCE_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}viam_rust_utils*${CMAKE_STATIC_LIBRARY_SUFFIX})
259+
file(GLOB existing_viam_rust_utils_header_file ${PROJECT_SOURCE_DIR}/viam_rust_utils.h)
260+
261+
if (NOT existing_viam_rust_utils_header_file)
262+
file(
263+
DOWNLOAD
264+
https://github.com/viamrobotics/rust-utils/releases/latest/download/viam_rust_utils.h
265+
${viam_rust_utils_header_file}
266+
STATUS lvruh_status
267+
)
268+
list(GET lvruh_status 0 lvruh_status_code)
269+
list(GET lvruh_status 1 lvruh_status_string)
270+
271+
if(NOT lvruh_status_code EQUAL 0)
272+
message(FATAL_ERROR "No local viam_rust_utils.h found and failed to download: ${lvruh_status_string}")
273+
endif()
274+
else()
275+
FILE(COPY_FILE
276+
${existing_viam_rust_utils_header_file}
277+
${viam_rust_utils_header_file}
278+
ONLY_IF_DIFFERENT
279+
)
280+
endif()
258281

259282
if (viam_rust_utils_files)
260283
list(LENGTH viam_rust_utils_files num_viam_rust_utils_files)

src/viam/examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ If you are connecting to a robot with authentication you will need to
4949
add credentials. Update path code :
5050

5151
``` c++
52-
void *ptr = init_rust_runtime();
53-
char *path = dial("<your robot uri here>", "<your authentication type>", "<your authentication payload>", false, ptr);
52+
void *ptr = viam_init_rust_runtime();
53+
char *path = viam_dial("<your robot uri here>", "<your authentication type>", "<your authentication payload>", false, ptr);
5454
```
5555

5656
Then to obtain a robot client do :

src/viam/sdk/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ else()
2828
endif()
2929
message(WARNING "api proto label is ${VIAMCPPSDK_API_PROTO_LABEL}")
3030
configure_file(common/private/version_metadata.hpp.in common/private/version_metadata.hpp @ONLY)
31+
configure_file(${CMAKE_CURRENT_BINARY_DIR}/../../../viam_rust_utils.h rpc/private/viam_rust_utils.h COPYONLY)
3132

3233
# Configure the grpc client forward declarations file
3334
configure_file(common/grpc_fwd.hpp.in common/grpc_fwd.hpp)
@@ -316,7 +317,7 @@ endif()
316317
# TODO several of these dependencies should properly be attached to `viam_rust_utils`,
317318
# not `viamsdk`. However, we currently are unable to do so while maintaining compilation
318319
if (APPLE)
319-
target_link_libraries(viamsdk PUBLIC "-framework Security")
320+
target_link_libraries(viamsdk PUBLIC "-framework CoreFoundation" "-framework Security")
320321
elseif (NOT WIN32)
321322
target_link_libraries(viamsdk PRIVATE dl)
322323
target_link_libraries(viamsdk PRIVATE rt)

src/viam/sdk/rpc/dial.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace viam {
2020
namespace sdk {
2121

2222
struct ViamChannel::impl {
23-
impl(const char* path, void* runtime) : path(path), rust_runtime(runtime) {}
23+
impl(const char* path, void* runtime)
24+
: path(path), rust_runtime(reinterpret_cast<viam_dial_ffi*>(runtime)) {}
2425

2526
impl(const impl&) = delete;
2627

@@ -38,12 +39,13 @@ struct ViamChannel::impl {
3839
}
3940

4041
~impl() {
41-
free_string(path);
42-
free_rust_runtime(rust_runtime);
42+
auto& p = const_cast<char*&>(path);
43+
viam_free_string(p);
44+
viam_free_rust_runtime(rust_runtime);
4345
}
4446

4547
const char* path;
46-
void* rust_runtime;
48+
viam_dial_ffi* rust_runtime;
4749
};
4850

4951
ViamChannel::ViamChannel(std::shared_ptr<grpc::Channel> channel, const char* path, void* runtime)
@@ -158,7 +160,7 @@ ViamChannel ViamChannel::dial_initial(const char* uri,
158160
}
159161

160162
ViamChannel ViamChannel::dial(const char* uri, const boost::optional<DialOptions>& options) {
161-
void* ptr = init_rust_runtime();
163+
viam_dial_ffi* ptr = viam_init_rust_runtime();
162164
const DialOptions opts = options.get_value_or(DialOptions());
163165
const std::chrono::duration<float> float_timeout = opts.timeout();
164166
const char* type = nullptr;
@@ -172,10 +174,10 @@ ViamChannel ViamChannel::dial(const char* uri, const boost::optional<DialOptions
172174
if (opts.entity()) {
173175
entity = opts.entity()->c_str();
174176
}
175-
char* proxy_path = ::dial(
177+
char* proxy_path = ::viam_dial(
176178
uri, entity, type, payload, opts.allows_insecure_downgrade(), float_timeout.count(), ptr);
177179
if (!proxy_path) {
178-
free_rust_runtime(ptr);
180+
viam_free_rust_runtime(ptr);
179181
throw Exception(ErrorCondition::k_connection, "Unable to establish connecting path");
180182
}
181183

src/viam/sdk/rpc/private/viam_rust_utils.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/viam/sdk/rpc/private/viam_rust_utils_stubs.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
#include <cstdlib>
44

5-
void* init_rust_runtime() {
5+
viam_dial_ffi* viam_init_rust_runtime() {
66
abort();
77
}
88

9-
int free_rust_runtime(void*) {
9+
int viam_free_rust_runtime(viam_dial_ffi*) {
1010
abort();
1111
}
1212

13-
void free_string(const char*) {
13+
void viam_free_string(char*) {
1414
abort();
1515
}
1616

17-
char* dial(const char*, const char*, const char*, const char*, bool, float, void*) {
17+
char* viam_dial(const char*, const char*, const char*, const char*, bool, float, viam_dial_ffi*) {
1818
abort();
1919
}

0 commit comments

Comments
 (0)