@@ -25,16 +25,36 @@ namespace ROS2
2525 internal static class TimerDelegates
2626 {
2727 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
28- internal delegate RCLRet NativeRCLTimerFunctionType ( SafeTimerHandle timerHandle ) ;
28+ internal delegate RCLRet NativeRCLTimerCallType ( SafeTimerHandle timerHandle ) ;
2929
30- internal static NativeRCLTimerFunctionType native_rcl_timer_call = null ;
30+ internal static NativeRCLTimerCallType native_rcl_timer_call = null ;
31+
32+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
33+ internal delegate RCLRet NativeRCLTimerCancelType ( SafeTimerHandle timerHandle ) ;
34+ internal static NativeRCLTimerCancelType native_rcl_timer_cancel = null ;
35+
36+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
37+ internal delegate RCLRet NativeRCLTimerResetType ( SafeTimerHandle timerHandle ) ;
38+ internal static NativeRCLTimerResetType native_rcl_timer_reset = null ;
39+
40+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
41+ internal delegate RCLRet NativeRCLTimerIsCanceledType ( SafeTimerHandle timerHandle , out int isCanceled ) ;
42+ internal static NativeRCLTimerIsCanceledType native_rcl_timer_is_canceled = null ;
43+
44+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
45+ internal delegate RCLRet NativeRCLTimerIsReadyType ( SafeTimerHandle timerHandle , out int isReady ) ;
46+ internal static NativeRCLTimerIsReadyType native_rcl_timer_is_ready = null ;
3147
3248 static TimerDelegates ( )
3349 {
3450 var dllLoadUtils = DllLoadUtilsFactory . GetDllLoadUtils ( ) ;
3551 IntPtr nativeLibrary = dllLoadUtils . LoadLibrary ( "rcldotnet" ) ;
3652
3753 dllLoadUtils . RegisterNativeFunction ( nativeLibrary , nameof ( native_rcl_timer_call ) , out native_rcl_timer_call ) ;
54+ dllLoadUtils . RegisterNativeFunction ( nativeLibrary , nameof ( native_rcl_timer_cancel ) , out native_rcl_timer_cancel ) ;
55+ dllLoadUtils . RegisterNativeFunction ( nativeLibrary , nameof ( native_rcl_timer_reset ) , out native_rcl_timer_reset ) ;
56+ dllLoadUtils . RegisterNativeFunction ( nativeLibrary , nameof ( native_rcl_timer_is_canceled ) , out native_rcl_timer_is_canceled ) ;
57+ dllLoadUtils . RegisterNativeFunction ( nativeLibrary , nameof ( native_rcl_timer_is_ready ) , out native_rcl_timer_is_ready ) ;
3858 }
3959 }
4060
@@ -62,11 +82,48 @@ internal Timer(Clock clock, TimeSpan period, Action<TimeSpan> callback)
6282 Handle = handle ;
6383 }
6484
85+ public void Cancel ( )
86+ {
87+ RCLRet ret = TimerDelegates . native_rcl_timer_cancel ( Handle ) ;
88+ RCLExceptionHelper . CheckReturnValue ( ret , $ "{ nameof ( TimerDelegates . native_rcl_timer_cancel ) } () failed.") ;
89+ }
90+
91+ public void Reset ( )
92+ {
93+ RCLRet ret = TimerDelegates . native_rcl_timer_reset ( Handle ) ;
94+ RCLExceptionHelper . CheckReturnValue ( ret , $ "{ nameof ( TimerDelegates . native_rcl_timer_reset ) } () failed.") ;
95+ }
96+
97+ public bool IsCanceled
98+ {
99+ get
100+ {
101+ RCLRet ret = TimerDelegates . native_rcl_timer_is_canceled ( Handle , out int isCanceled ) ;
102+ RCLExceptionHelper . CheckReturnValue ( ret , $ "{ nameof ( TimerDelegates . native_rcl_timer_is_canceled ) } () failed.") ;
103+ return isCanceled != 0 ;
104+ }
105+ }
106+
107+ public bool IsReady
108+ {
109+ get
110+ {
111+ RCLRet ret = TimerDelegates . native_rcl_timer_is_ready ( Handle , out int isReady ) ;
112+ RCLExceptionHelper . CheckReturnValue ( ret , $ "{ nameof ( TimerDelegates . native_rcl_timer_is_ready ) } () failed.") ;
113+ return isReady != 0 ;
114+ }
115+ }
116+
65117 internal SafeTimerHandle Handle { get ; }
66118
67119 internal void Call ( )
68120 {
69121 RCLRet ret = TimerDelegates . native_rcl_timer_call ( Handle ) ;
122+ if ( ret == ROS2 . RCLRet . TimerCanceled )
123+ {
124+ // Timer was canceled, do nothing.
125+ return ;
126+ }
70127 RCLExceptionHelper . CheckReturnValue ( ret , $ "{ nameof ( TimerDelegates . native_rcl_timer_call ) } () failed.") ;
71128 }
72129
0 commit comments