Skip to content

Commit

Permalink
expose rcl_shutdown function
Browse files Browse the repository at this point in the history
  • Loading branch information
benmaidel committed Dec 2, 2024
1 parent c31d886 commit 1195ab7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion rcldotnet/RCLdotnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ internal delegate void NativeRCLGetErrorStringType(

internal static NativeRCLOkType native_rcl_ok = null;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate RCLRet NativeRCLShutdownType();

internal static NativeRCLShutdownType native_rcl_shutdown = null;

[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal delegate RCLRet NativeRCLCreateNodeHandleType(
ref SafeNodeHandle nodeHandle, [MarshalAs(UnmanagedType.LPStr)] string nodeName, [MarshalAs(UnmanagedType.LPStr)] string nodeNamespace);
Expand Down Expand Up @@ -409,6 +414,12 @@ static RCLdotnetDelegates()
(NativeRCLOkType)Marshal.GetDelegateForFunctionPointer(
native_rcl_ok_ptr, typeof(NativeRCLOkType));

IntPtr native_rcl_shutdown_ptr =
_dllLoadUtils.GetProcAddress(nativeLibrary, "native_rcl_shutdown");
RCLdotnetDelegates.native_rcl_shutdown =
(NativeRCLShutdownType)Marshal.GetDelegateForFunctionPointer(
native_rcl_shutdown_ptr, typeof(NativeRCLShutdownType));

IntPtr native_rcl_create_node_handle_ptr =
_dllLoadUtils.GetProcAddress(nativeLibrary, "native_rcl_create_node_handle");
RCLdotnetDelegates.native_rcl_create_node_handle =
Expand Down Expand Up @@ -744,7 +755,8 @@ public static class RCLdotnet

public static bool Ok()
{
return RCLdotnetDelegates.native_rcl_ok() != 0;
bool ret = RCLdotnetDelegates.native_rcl_ok() != 0;
return ret;
}

public static Node CreateNode(string nodeName)
Expand Down Expand Up @@ -774,6 +786,12 @@ public static void Spin(Node node)
}
}

public static void Shutdown()
{
RCLRet ret = RCLdotnetDelegates.native_rcl_shutdown();
RCLExceptionHelper.CheckReturnValue(ret, $"{nameof(RCLdotnetDelegates.native_rcl_shutdown)}() failed.");
}

public static Clock CreateClock(ClockType type = ClockType.RosTime)
{
var clockHandle = new SafeClockHandle();
Expand Down
14 changes: 14 additions & 0 deletions rcldotnet/rcldotnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ int32_t native_rcl_ok() {
return result ? 1 : 0;
}

int32_t native_rcl_shutdown() {
rcl_ret_t ret = rcl_shutdown(&context);
if (ret != RCL_RET_OK) {
return ret;
}

// TODO: Add a `SafeContextHandle` and manipulate it's reference count
// in the child handles created from the context. This should be similar to
// `SafeNodeHandle` being referenced by `SafePublisherHandle` for example.
// See the documentation in rcl how the lifetime of the context object should work:
// https://github.com/ros2/rcl/blob/2a72dbaaf09458a998fad39d898adc9b8a60565a/rcl/include/rcl/context.h#L48-L112
return rcl_context_fini(&context);
}

int32_t native_rcl_create_node_handle(void **node_handle, const char *name, const char *namespace) {
rcl_node_t *node = (rcl_node_t *)malloc(sizeof(rcl_node_t));
*node = rcl_get_zero_initialized_node();
Expand Down
3 changes: 3 additions & 0 deletions rcldotnet/rcldotnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void RCLDOTNET_CDECL native_rcl_reset_error(void);
RCLDOTNET_EXPORT
int32_t RCLDOTNET_CDECL native_rcl_ok();

RCLDOTNET_EXPORT
int32_t RCLDOTNET_CDECL native_rcl_shutdown();

RCLDOTNET_EXPORT
int32_t RCLDOTNET_CDECL native_rcl_create_node_handle(void **, const char *, const char *);

Expand Down

0 comments on commit 1195ab7

Please sign in to comment.