SMACC2
Loading...
Searching...
No Matches
cl_px4_mr::CpMicroRosAgent Class Reference

#include <cp_micro_ros_agent.hpp>

Inheritance diagram for cl_px4_mr::CpMicroRosAgent:
Inheritance graph
Collaboration diagram for cl_px4_mr::CpMicroRosAgent:
Collaboration graph

Public Member Functions

 CpMicroRosAgent (std::string command="ros2 run micro_ros_agent micro_ros_agent udp4 -p 8888 2>&1 | tee /tmp/xrce_agent.log", std::string nodeName="/px4_micro_xrce_dds")
 
 ~CpMicroRosAgent () override
 
void launch ()
 
void shutdown ()
 
bool isLaunched () const
 
pid_t getPid () const
 
std::string getNodeName () const
 
- Public Member Functions inherited from smacc2::ISmaccComponent
 ISmaccComponent ()
 
virtual ~ISmaccComponent ()
 
 ISmaccComponent (const ISmaccComponent &)=delete
 
ISmaccComponent & operator= (const ISmaccComponent &)=delete
 
 ISmaccComponent (ISmaccComponent &&)=delete
 
ISmaccComponent & operator= (ISmaccComponent &&)=delete
 
virtual std::string getName () const
 

Private Attributes

pid_t agentPid_ = -1
 
std::atomic< bool > launched_ {false}
 
std::atomic< bool > shutdownRequested_ {false}
 
std::string command_
 
std::string nodeName_
 
std::mutex mutex_
 
int pipeFd_ = -1
 
pid_t watchdogPid_ = -1
 
int lifeFd_ = -1
 
std::thread readerThread_
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::ISmaccComponent
virtual void onInitialize ()
 
template<typename TOrthogonal , typename TClient >
void onComponentInitialization ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
template<typename TComponent >
void requiresComponent (TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TComponent >
void requiresComponent (const std::string &name, TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TClient >
void requiresClient (TClient *&requiredClientStorage)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingComponent (const TArgs &... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingNamedComponent (const std::string &name, const TArgs &... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger () const
 
ISmaccStateMachine * getStateMachine ()
 
- Protected Attributes inherited from smacc2::ISmaccComponent
ISmaccStateMachine * stateMachine_ = nullptr
 
ISmaccClient * owner_ = nullptr
 

Detailed Description

Definition at line 27 of file cp_micro_ros_agent.hpp.

Constructor & Destructor Documentation

◆ CpMicroRosAgent()

cl_px4_mr::CpMicroRosAgent::CpMicroRosAgent ( std::string command = "ros2 run micro_ros_agent micro_ros_agent udp4 -p 8888 2>&1 | tee /tmp/xrce_agent.log",
std::string nodeName = "/px4_micro_xrce_dds" )

Definition at line 80 of file cp_micro_ros_agent.cpp.

81: command_(std::move(command)), nodeName_(std::move(nodeName))
82{
83}

◆ ~CpMicroRosAgent()

cl_px4_mr::CpMicroRosAgent::~CpMicroRosAgent ( )
override

Definition at line 85 of file cp_micro_ros_agent.cpp.

References shutdown().

Here is the call graph for this function:

Member Function Documentation

◆ getNodeName()

std::string cl_px4_mr::CpMicroRosAgent::getNodeName ( ) const

Definition at line 267 of file cp_micro_ros_agent.cpp.

267{ return nodeName_; }

References nodeName_.

Referenced by cl_px4_mr::CbConnectMicroRosAgent::onEntry().

Here is the caller graph for this function:

◆ getPid()

pid_t cl_px4_mr::CpMicroRosAgent::getPid ( ) const

Definition at line 261 of file cp_micro_ros_agent.cpp.

262{
263 std::lock_guard<std::mutex> lock(mutex_);
264 return agentPid_;
265}

References agentPid_, and mutex_.

◆ isLaunched()

bool cl_px4_mr::CpMicroRosAgent::isLaunched ( ) const

Definition at line 259 of file cp_micro_ros_agent.cpp.

259{ return launched_; }

References launched_.

Referenced by cl_px4_mr::CbConnectMicroRosAgent::onEntry().

Here is the caller graph for this function:

◆ launch()

void cl_px4_mr::CpMicroRosAgent::launch ( )

Definition at line 87 of file cp_micro_ros_agent.cpp.

88{
89 std::lock_guard<std::mutex> lock(mutex_);
90
91 if (launched_)
92 {
93 RCLCPP_INFO(getLogger(), "CpMicroRosAgent: already launched, skipping");
94 return;
95 }
96
97 RCLCPP_INFO(getLogger(), "CpMicroRosAgent: launching '%s'", command_.c_str());
98
99 int pipefd[2];
100 if (pipe(pipefd) == -1)
101 {
102 RCLCPP_ERROR(
103 getLogger(), "CpMicroRosAgent: pipe() failed: %s",
104 std::system_category().message(errno).c_str());
105 return;
106 }
107
108 // Held open by this process only; the watchdog reads its other end
109 int lifefd[2];
110 if (pipe2(lifefd, O_CLOEXEC) == -1)
111 {
112 RCLCPP_ERROR(
113 getLogger(), "CpMicroRosAgent: pipe2() failed: %s",
114 std::system_category().message(errno).c_str());
115 close(pipefd[0]);
116 close(pipefd[1]);
117 return;
118 }
119
120 pid_t pid = fork();
121
122 if (pid == 0)
123 {
124 // Child process: its own process group, so shutdown() can signal the shell and all it starts
125 setpgid(0, 0);
126 dup2(pipefd[1], STDOUT_FILENO);
127 dup2(pipefd[1], STDERR_FILENO);
128 // The agent keeps none of this process's descriptors (sockets, DDS shared memory, the pipe)
129 close_range(3, ~0U, 0);
130
131 execl("/bin/sh", "/bin/sh", "-c", command_.c_str(), nullptr);
132
133 // If execl returns, it failed
134 _exit(1);
135 }
136 else if (pid > 0)
137 {
138 // Parent process
139 close(pipefd[1]);
140 setpgid(pid, pid); // also here, so the group exists whichever process runs first
141 agentPid_ = pid;
142 pipeFd_ = pipefd[0];
143
144 // A second child that outlives a crash or SIGKILL of this process and then stops the agent
145 watchdogPid_ = fork();
146 if (watchdogPid_ == 0) runWatchdog(lifefd, pid);
147 close(lifefd[0]);
148 if (watchdogPid_ > 0)
149 {
150 lifeFd_ = lifefd[1];
151 }
152 else
153 {
154 RCLCPP_WARN(
155 getLogger(), "CpMicroRosAgent: no watchdog, fork() failed: %s",
156 std::system_category().message(errno).c_str());
157 close(lifefd[1]);
158 }
159
160 // Set pipe to non-blocking
161 int flags = fcntl(pipeFd_, F_GETFL, 0);
162 fcntl(pipeFd_, F_SETFL, flags | O_NONBLOCK);
163
164 // Reader thread draining the pipe (prevents buffer fill); joined in shutdown()
165 shutdownRequested_ = false;
166 readerThread_ = std::thread(
167 [this]()
168 {
169 char buf[256];
170 while (!shutdownRequested_)
171 {
172 ssize_t n = read(pipeFd_, buf, sizeof(buf) - 1);
173 if (n > 0)
174 {
175 buf[n] = '\0';
176 RCLCPP_DEBUG(getLogger(), "[micro_ros_agent] %s", buf);
177 }
178 else
179 {
180 std::this_thread::sleep_for(std::chrono::milliseconds(100));
181 }
182 }
183 });
184
185 launched_ = true;
186 RCLCPP_INFO(getLogger(), "CpMicroRosAgent: launched with PID %d", agentPid_);
187 }
188 else
189 {
190 RCLCPP_ERROR(
191 getLogger(), "CpMicroRosAgent: fork() failed: %s",
192 std::system_category().message(errno).c_str());
193 close(pipefd[0]);
194 close(pipefd[1]);
195 close(lifefd[0]);
196 close(lifefd[1]);
197 }
198}
std::atomic< bool > shutdownRequested_
rclcpp::Logger getLogger() const

References agentPid_, command_, smacc2::ISmaccComponent::getLogger(), launched_, lifeFd_, mutex_, pipeFd_, readerThread_, shutdownRequested_, and watchdogPid_.

Referenced by cl_px4_mr::CbConnectMicroRosAgent::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown()

void cl_px4_mr::CpMicroRosAgent::shutdown ( )

Definition at line 200 of file cp_micro_ros_agent.cpp.

201{
202 std::lock_guard<std::mutex> lock(mutex_);
203
204 // The reader checks the flag every 100 ms, so the join is bounded; it must happen before the
205 // pipe is closed and before this object is destroyed.
206 shutdownRequested_ = true;
207 if (readerThread_.joinable())
208 {
209 readerThread_.join();
210 }
211
212 if (agentPid_ <= 0)
213 {
214 return;
215 }
216
217 RCLCPP_INFO(getLogger(), "CpMicroRosAgent: shutting down agent (PID %d)", agentPid_);
218
219 // SIGTERM to the whole group, SIGKILL to whatever is left after two seconds
220 bool leaderReaped = false;
221 kill(-agentPid_, SIGTERM);
222 if (!waitForGroupExit(agentPid_, leaderReaped, std::chrono::seconds(2)))
223 {
224 RCLCPP_WARN(getLogger(), "CpMicroRosAgent: agent still running after SIGTERM, sending SIGKILL");
225 kill(-agentPid_, SIGKILL);
226 if (!waitForGroupExit(agentPid_, leaderReaped, std::chrono::seconds(2)))
227 {
228 RCLCPP_ERROR(
229 getLogger(), "CpMicroRosAgent: process group %d still exists after SIGKILL", agentPid_);
230 }
231 }
232
233 // The agent is stopped, so the watchdog has nothing left to do
234 if (watchdogPid_ > 0)
235 {
236 kill(watchdogPid_, SIGKILL);
237 waitpid(watchdogPid_, nullptr, 0);
238 watchdogPid_ = -1;
239 }
240 if (lifeFd_ >= 0)
241 {
242 close(lifeFd_);
243 lifeFd_ = -1;
244 }
245
246 // Close pipe
247 if (pipeFd_ >= 0)
248 {
249 close(pipeFd_);
250 pipeFd_ = -1;
251 }
252
253 agentPid_ = -1;
254 launched_ = false;
255
256 RCLCPP_INFO(getLogger(), "CpMicroRosAgent: shutdown complete");
257}
bool waitForGroupExit(pid_t group, bool &leaderReaped, std::chrono::milliseconds limit)

References agentPid_, smacc2::ISmaccComponent::getLogger(), launched_, lifeFd_, mutex_, pipeFd_, readerThread_, shutdownRequested_, and watchdogPid_.

Referenced by ~CpMicroRosAgent().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ agentPid_

pid_t cl_px4_mr::CpMicroRosAgent::agentPid_ = -1
private

Definition at line 43 of file cp_micro_ros_agent.hpp.

Referenced by getPid(), launch(), and shutdown().

◆ command_

std::string cl_px4_mr::CpMicroRosAgent::command_
private

Definition at line 46 of file cp_micro_ros_agent.hpp.

Referenced by launch().

◆ launched_

std::atomic<bool> cl_px4_mr::CpMicroRosAgent::launched_ {false}
private

Definition at line 44 of file cp_micro_ros_agent.hpp.

44{false};

Referenced by isLaunched(), launch(), and shutdown().

◆ lifeFd_

int cl_px4_mr::CpMicroRosAgent::lifeFd_ = -1
private

Definition at line 52 of file cp_micro_ros_agent.hpp.

Referenced by launch(), and shutdown().

◆ mutex_

std::mutex cl_px4_mr::CpMicroRosAgent::mutex_
mutableprivate

Definition at line 48 of file cp_micro_ros_agent.hpp.

Referenced by getPid(), launch(), and shutdown().

◆ nodeName_

std::string cl_px4_mr::CpMicroRosAgent::nodeName_
private

Definition at line 47 of file cp_micro_ros_agent.hpp.

Referenced by getNodeName().

◆ pipeFd_

int cl_px4_mr::CpMicroRosAgent::pipeFd_ = -1
private

Definition at line 49 of file cp_micro_ros_agent.hpp.

Referenced by launch(), and shutdown().

◆ readerThread_

std::thread cl_px4_mr::CpMicroRosAgent::readerThread_
private

Definition at line 54 of file cp_micro_ros_agent.hpp.

Referenced by launch(), and shutdown().

◆ shutdownRequested_

std::atomic<bool> cl_px4_mr::CpMicroRosAgent::shutdownRequested_ {false}
private

Definition at line 45 of file cp_micro_ros_agent.hpp.

45{false};

Referenced by launch(), and shutdown().

◆ watchdogPid_

pid_t cl_px4_mr::CpMicroRosAgent::watchdogPid_ = -1
private

Definition at line 51 of file cp_micro_ros_agent.hpp.

Referenced by launch(), and shutdown().


The documentation for this class was generated from the following files: