SMACC2
Loading...
Searching...
No Matches
cp_lifecycle_event_monitor.hpp
Go to the documentation of this file.
1// Copyright 2024 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17// The umbrella header is needed, not component.hpp alone: the subscribe helpers below call
18// ISmaccStateMachine::createSignalConnection, and this header is parsed by the library's own
19// translation units before anything else has completed that class.
20#include <smacc2/smacc.hpp>
22
23#include <lifecycle_msgs/msg/transition_event.hpp>
24#include <rclcpp/rclcpp.hpp>
25
26#include <functional>
27#include <mutex>
28#include <optional>
29#include <string>
30
31// Forward declarations
32namespace cl_lifecycle_node
33{
34class CpLifecycleEventMonitor;
35
36// Event declarations - component is now the source
37template <typename TSourceObject, typename TOrthogonal>
39template <typename TSourceObject, typename TOrthogonal>
41template <typename TSourceObject, typename TOrthogonal>
43template <typename TSourceObject, typename TOrthogonal>
45template <typename TSourceObject, typename TOrthogonal>
47template <typename TSourceObject, typename TOrthogonal>
49template <typename TSourceObject, typename TOrthogonal>
51template <typename TSourceObject, typename TOrthogonal>
53template <typename TSourceObject, typename TOrthogonal>
55template <typename TSourceObject, typename TOrthogonal>
57template <typename TSourceObject, typename TOrthogonal>
59template <typename TSourceObject, typename TOrthogonal>
61template <typename TSourceObject, typename TOrthogonal>
63template <typename TSourceObject, typename TOrthogonal>
65template <typename TSourceObject, typename TOrthogonal>
67template <typename TSourceObject, typename TOrthogonal>
69template <typename TSourceObject, typename TOrthogonal>
71template <typename TSourceObject, typename TOrthogonal>
73template <typename TSourceObject, typename TOrthogonal>
75template <typename TSourceObject, typename TOrthogonal>
77template <typename TSourceObject, typename TOrthogonal>
79template <typename TSourceObject, typename TOrthogonal>
81template <typename TSourceObject, typename TOrthogonal>
83template <typename TSourceObject, typename TOrthogonal>
85template <typename TSourceObject, typename TOrthogonal>
87} // namespace cl_lifecycle_node
88
89namespace cl_lifecycle_node
90{
102{
103public:
108 CpLifecycleEventMonitor(std::string nodeName);
109
110 virtual ~CpLifecycleEventMonitor() = default;
111
118 {
119 owner_ = owner;
120 stateMachine_ = stateMachine;
121 }
122
123 // SMACC2 Signals for lifecycle transition events
124 // Transition initiation signals
134
135 // Configure transition result signals
139
140 // Activate transition result signals
144
145 // Deactivate transition result signals
149
150 // Cleanup transition result signals
154
155 // Shutdown transition result signals
159
160 // Active shutdown specific signals
164
165 // Error processing result signals
169
170 // Typed subscribe helpers, one per signal. They go through the state machine's
171 // createSignalConnection, so a connection made by a state-scoped object (a client behavior
172 // subscribing with `this`) is tracked by that object's address and finalized when the object
173 // is disposed at its state's exit: a transition event arriving after the exit cannot reach a
174 // destroyed subscriber. Behaviors must use these from onStateOrthogonalAllocation (state
175 // machine thread), never a raw connect() on the signal members above (finding T) and never
176 // from an asynchronous onEntry/onExit thread (Async Thread Locking Rule).
177 template <typename T>
178 smacc2::SmaccSignalConnection onTransitionCreate(void (T::*callback)(), T * object)
179 {
180 return this->getStateMachine()->createSignalConnection(onTransitionCreate_, callback, object);
181 }
182
183 template <typename T>
184 smacc2::SmaccSignalConnection onTransitionConfigure(void (T::*callback)(), T * object)
185 {
187 onTransitionConfigure_, callback, object);
188 }
189
190 template <typename T>
191 smacc2::SmaccSignalConnection onTransitionActivate(void (T::*callback)(), T * object)
192 {
193 return this->getStateMachine()->createSignalConnection(onTransitionActivate_, callback, object);
194 }
195
196 template <typename T>
197 smacc2::SmaccSignalConnection onTransitionDeactivate(void (T::*callback)(), T * object)
198 {
200 onTransitionDeactivate_, callback, object);
201 }
202
203 template <typename T>
204 smacc2::SmaccSignalConnection onTransitionCleanup(void (T::*callback)(), T * object)
205 {
206 return this->getStateMachine()->createSignalConnection(onTransitionCleanup_, callback, object);
207 }
208
209 template <typename T>
211 {
213 onTransitionUnconfiguredShutdown_, callback, object);
214 }
215
216 template <typename T>
218 {
220 onTransitionInactiveShutdown_, callback, object);
221 }
222
223 template <typename T>
225 {
227 onTransitionActiveShutdown_, callback, object);
228 }
229
230 template <typename T>
231 smacc2::SmaccSignalConnection onTransitionDestroy(void (T::*callback)(), T * object)
232 {
233 return this->getStateMachine()->createSignalConnection(onTransitionDestroy_, callback, object);
234 }
235
236 template <typename T>
238 {
240 onTransitionOnConfigureSuccess_, callback, object);
241 }
242
243 template <typename T>
245 {
247 onTransitionOnConfigureFailure_, callback, object);
248 }
249
250 template <typename T>
252 {
254 onTransitionOnConfigureError_, callback, object);
255 }
256
257 template <typename T>
259 {
261 onTransitionOnActivateSuccess_, callback, object);
262 }
263
264 template <typename T>
266 {
268 onTransitionOnActivateFailure_, callback, object);
269 }
270
271 template <typename T>
273 {
275 onTransitionOnActivateError_, callback, object);
276 }
277
278 template <typename T>
280 {
282 onTransitionOnDeactivateSuccess_, callback, object);
283 }
284
285 template <typename T>
287 {
289 onTransitionOnDeactivateFailure_, callback, object);
290 }
291
292 template <typename T>
294 {
296 onTransitionOnDeactivateError_, callback, object);
297 }
298
299 template <typename T>
301 {
303 onTransitionOnCleanupSuccess_, callback, object);
304 }
305
306 template <typename T>
308 {
310 onTransitionOnCleanupFailure_, callback, object);
311 }
312
313 template <typename T>
315 {
317 onTransitionOnCleanupError_, callback, object);
318 }
319
320 template <typename T>
322 {
324 onTransitionOnShutdownSuccess_, callback, object);
325 }
326
327 template <typename T>
329 {
331 onTransitionOnShutdownFailure_, callback, object);
332 }
333
334 template <typename T>
336 {
338 onTransitionOnShutdownError_, callback, object);
339 }
340
341 template <typename T>
343 void (T::*callback)(), T * object)
344 {
346 onTransitionOnActiveShutdownSuccess_, callback, object);
347 }
348
349 template <typename T>
351 void (T::*callback)(), T * object)
352 {
354 onTransitionOnActiveShutdownFailure_, callback, object);
355 }
356
357 template <typename T>
359 {
361 onTransitionOnActiveShutdownError_, callback, object);
362 }
363
364 template <typename T>
366 {
368 onTransitionOnErrorSuccess_, callback, object);
369 }
370
371 template <typename T>
373 {
375 onTransitionOnErrorFailure_, callback, object);
376 }
377
378 template <typename T>
380 {
382 onTransitionOnErrorError_, callback, object);
383 }
384
389 std::optional<lifecycle_msgs::msg::TransitionEvent> getLastTransitionEvent() const;
390
394 void onInitialize() override;
395
400 template <typename TOrthogonal, typename TClient>
402 {
403 // Set up event posting lambdas with correct template parameters
404 // Component is the event source (TSourceObject = CpLifecycleEventMonitor)
405
406 postEventConfigure_ = [this]()
408
409 postEventActivate_ = [this]()
411
412 postEventDeactivate_ = [this]()
414
415 postEventCleanup_ = [this]()
417
420
423
424 postEventActiveShutdown_ = [this]()
426
427 postEventDestroy_ = [this]()
429
432
435
438
441
444
447
450
453
456
459
462
463 postEventOnCleanupError_ = [this]()
465
468
471
474
475 postEventOnErrorSuccess_ = [this]()
477
478 postEventOnErrorFailure_ = [this]()
480 }
481
482private:
483 std::string nodeName_;
484 rclcpp::Subscription<lifecycle_msgs::msg::TransitionEvent>::SharedPtr subscription_;
485 lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_;
486 mutable std::mutex eventMutex_;
487
492 void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg);
493
494 // Event posting lambdas - configured via onStateOrthogonalAllocation<TOrthogonal, TClient>()
495 std::function<void()> postEventConfigure_;
496 std::function<void()> postEventActivate_;
497 std::function<void()> postEventDeactivate_;
498 std::function<void()> postEventCleanup_;
499 std::function<void()> postEventUnconfiguredShutdown_;
500 std::function<void()> postEventInactiveShutdown_;
501 std::function<void()> postEventActiveShutdown_;
502 std::function<void()> postEventDestroy_;
503
504 std::function<void()> postEventOnConfigureSuccess_;
505 std::function<void()> postEventOnConfigureFailure_;
506 std::function<void()> postEventOnConfigureError_;
507
508 std::function<void()> postEventOnActivateSuccess_;
509 std::function<void()> postEventOnActivateFailure_;
510 std::function<void()> postEventOnActivateError_;
511
512 std::function<void()> postEventOnDeactivateSuccess_;
513 std::function<void()> postEventOnDeactivateFailure_;
514 std::function<void()> postEventOnDeactivateError_;
515
516 std::function<void()> postEventOnCleanupSuccess_;
517 std::function<void()> postEventOnCleanupFailure_;
518 std::function<void()> postEventOnCleanupError_;
519
520 std::function<void()> postEventOnShutdownSuccess_;
521 std::function<void()> postEventOnShutdownFailure_;
522 std::function<void()> postEventOnShutdownError_;
523
524 std::function<void()> postEventOnErrorSuccess_;
525 std::function<void()> postEventOnErrorFailure_;
526};
527
528} // namespace cl_lifecycle_node
Component that monitors ROS2 lifecycle node transition events.
smacc2::SmaccSignalConnection onTransitionOnErrorError(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionInactiveShutdown(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnDeactivateSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnActivateFailure(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnActiveShutdownSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionUnconfiguredShutdown(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnCleanupSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnConfigureFailure(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnConfigureError(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnShutdownSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignal< void()> onTransitionOnActiveShutdownSuccess_
smacc2::SmaccSignalConnection onTransitionOnConfigureSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnActiveShutdownFailure(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionCleanup(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnActivateSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionDeactivate(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnShutdownFailure(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionActiveShutdown(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionActivate(void(T::*callback)(), T *object)
rclcpp::Subscription< lifecycle_msgs::msg::TransitionEvent >::SharedPtr subscription_
smacc2::SmaccSignalConnection onTransitionConfigure(void(T::*callback)(), T *object)
CpLifecycleEventMonitor(std::string nodeName)
Constructor with node name.
smacc2::SmaccSignalConnection onTransitionOnActivateError(void(T::*callback)(), T *object)
void onStateOrthogonalAllocation()
Configure event posting with orthogonal template parameter This is called from the client's onStateOr...
smacc2::SmaccSignalConnection onTransitionCreate(void(T::*callback)(), T *object)
void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg)
Callback for transition event subscription.
smacc2::SmaccSignalConnection onTransitionDestroy(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnDeactivateFailure(void(T::*callback)(), T *object)
lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_
smacc2::SmaccSignalConnection onTransitionOnCleanupError(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnDeactivateError(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnShutdownError(void(T::*callback)(), T *object)
smacc2::SmaccSignal< void()> onTransitionOnActiveShutdownFailure_
std::optional< lifecycle_msgs::msg::TransitionEvent > getLastTransitionEvent() const
Get the last received transition event.
void setOwner(smacc2::ISmaccClient *owner, smacc2::ISmaccStateMachine *stateMachine)
Set the owner client and state machine for this component.
smacc2::SmaccSignalConnection onTransitionOnErrorSuccess(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnActiveShutdownError(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnCleanupFailure(void(T::*callback)(), T *object)
smacc2::SmaccSignalConnection onTransitionOnErrorFailure(void(T::*callback)(), T *object)
void onInitialize() override
Component initialization - creates subscription.
ISmaccClient * owner_
Definition component.hpp:96
ISmaccStateMachine * getStateMachine()
ISmaccStateMachine * stateMachine_
Definition component.hpp:94
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection SmaccSignalConnection