1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Node of ROS2.
//! Nodes can be create by `Context::create_node`.
//!
//! # Example
//!
//! ```
//! use safe_drive::context::Context;
//!
//! // Create a context.
//! let ctx = Context::new().unwrap();
//!
//! // Create a node.
//! let node = ctx
//! .create_node("node_rs", Some("namespace"), Default::default())
//! .unwrap();
//! ```
use libc::atexit;
use crate::{
context::{remove_context, Context},
error::{DynError, RCLResult},
helper::InitOnce,
msg::{ServiceMsg, TypeSupport},
parameter::ParameterServer,
qos, rcl,
service::{client::Client, server::Server},
topic::publisher::Publisher,
topic::subscriber::Subscriber,
};
use std::{ffi::CString, sync::Arc};
static SET_ATEXIT: InitOnce = InitOnce::new();
/// Node of ROS2.
pub struct Node {
node: rcl::rcl_node_t,
init_param_server: InitOnce,
pub(crate) context: Arc<Context>,
}
impl Node {
pub(crate) fn new(
context: Arc<Context>,
name: &str,
namespace: Option<&str>,
options: NodeOptions,
) -> RCLResult<Arc<Self>> {
let mut node = rcl::MTSafeFn::rcl_get_zero_initialized_node();
let name_c = CString::new(name).unwrap();
let namespace_c = CString::new(namespace.unwrap_or_default()).unwrap();
{
let guard = rcl::MT_UNSAFE_FN.lock();
guard.rcl_node_init(
&mut node,
name_c.as_ptr(),
namespace_c.as_ptr(),
unsafe { context.as_ptr_mut() },
options.as_ptr(),
)?;
}
// FastDDS uses atexit(3) to destroy resources when creating a node.
// Because of functions registed to atexit(3) will be invoked reverse order,
// remove_context() must be set here.
SET_ATEXIT.init(|| unsafe { atexit(remove_context) }, 0);
Ok(Arc::new(Node {
node,
init_param_server: InitOnce::new(),
context,
}))
}
pub(crate) fn as_ptr(&self) -> *const rcl::rcl_node_t {
&self.node
}
pub(crate) unsafe fn as_ptr_mut(&self) -> *mut rcl::rcl_node_t {
&self.node as *const _ as *mut _
}
pub fn get_name(&self) -> RCLResult<String> {
rcl::MTSafeFn::rcl_node_get_name(&self.node)
}
pub fn get_fully_qualified_name(&self) -> RCLResult<String> {
rcl::MTSafeFn::rcl_node_get_fully_qualified_name(&self.node)
}
pub fn get_namespace(&self) -> RCLResult<String> {
rcl::MTSafeFn::rcl_node_get_namespace(&self.node)
}
pub fn create_parameter_server(self: &Arc<Self>) -> Result<ParameterServer, DynError> {
self.init_param_server.init(
|| ParameterServer::new(self.clone()),
Err("a parameter server has been already created".into()),
)
}
/// Create a publisher.
/// If `qos` is specified `None`,
/// the default profile is used.
///
/// `T` is the type of messages the created publisher send.
///
/// # Example
///
/// ```
/// use safe_drive::{msg::common_interfaces::std_msgs, node::Node, topic::publisher::Publisher};
/// use std::sync::Arc;
///
/// fn create_new_publisher(node: Arc<Node>) -> Publisher<std_msgs::msg::Bool> {
/// node.create_publisher("topic_name", None).unwrap()
/// }
/// ```
pub fn create_publisher<T: TypeSupport>(
self: &Arc<Self>,
topic_name: &str,
qos: Option<qos::Profile>,
) -> RCLResult<Publisher<T>> {
Publisher::new(self.clone(), topic_name, qos)
}
/// Create a publisher.
/// If `qos` is specified `None`,
/// the default profile is used.
///
/// `T` is the type of messages the created publisher send.
///
/// This function is the same as `create_publisher` but it disables loaned message.
///
/// # Example
///
/// ```
/// use safe_drive::{msg::common_interfaces::std_msgs, node::Node, topic::publisher::Publisher};
/// use std::sync::Arc;
///
/// fn create_publisher_disable_loaned_message(node: Arc<Node>) -> Publisher<std_msgs::msg::Bool> {
/// node.create_publisher_disable_loaned_message("topic_name", None).unwrap()
/// }
/// ```
pub fn create_publisher_disable_loaned_message<T: TypeSupport>(
self: &Arc<Self>,
topic_name: &str,
qos: Option<qos::Profile>,
) -> RCLResult<Publisher<T>> {
Publisher::new_disable_loaned_message(self.clone(), topic_name, qos)
}
/// Create a subscriber.
/// If `qos` is specified `None`,
/// the default profile is used.
///
/// `T` is the type of messages the created subscriber receive.
///
/// # Example
///
/// ```
/// use safe_drive::{msg::common_interfaces::std_msgs, node::Node, topic::subscriber::Subscriber};
/// use std::sync::Arc;
///
/// fn create_new_subscriber(node: Arc<Node>) -> Subscriber<std_msgs::msg::Bool> {
/// node.create_subscriber("topic_name", None).unwrap()
/// }
/// ```
pub fn create_subscriber<T: TypeSupport>(
self: &Arc<Self>,
topic_name: &str,
qos: Option<qos::Profile>,
) -> RCLResult<Subscriber<T>> {
Subscriber::new(self.clone(), topic_name, qos)
}
/// Create a subscriber.
/// If `qos` is specified `None`,
/// the default profile is used.
///
/// `T` is the type of messages the created subscriber receive.
///
/// # Example
///
/// ```
/// use safe_drive::{msg::common_interfaces::std_msgs, node::Node, topic::subscriber::Subscriber};
/// use std::sync::Arc;
///
/// fn create_subscriber_disable_loaned_message(node: Arc<Node>) -> Subscriber<std_msgs::msg::Bool> {
/// node.create_subscriber_disable_loaned_message("topic_name", None).unwrap()
/// }
/// ```
pub fn create_subscriber_disable_loaned_message<T: TypeSupport>(
self: &Arc<Self>,
topic_name: &str,
qos: Option<qos::Profile>,
) -> RCLResult<Subscriber<T>> {
Subscriber::new_disable_loaned_message(self.clone(), topic_name, qos)
}
/// Create a server.
/// If `qos` is specified `None`,
/// the default profile is used.
///
/// A server must receive `ServiceMsg::Request` and send `ServiceMsg::Response`.
///
/// # Example
///
/// ```
/// use safe_drive::{msg::common_interfaces::std_srvs, node::Node, service::server::Server};
/// use std::sync::Arc;
///
/// fn create_new_server(node: Arc<Node>) -> Server<std_srvs::srv::Empty> {
/// node.create_server("service_name", None).unwrap()
/// }
/// ```
pub fn create_server<T: ServiceMsg>(
self: &Arc<Self>,
service_name: &str,
qos: Option<qos::Profile>,
) -> RCLResult<Server<T>> {
Server::new(self.clone(), service_name, qos)
}
/// Create a client.
/// If `qos` is specified `None`,
/// the default profile is used.
///
/// A client must send `ServiceMsg::Request` and receive `ServiceMsg::Response`.
///
/// # Example
///
/// ```
/// use safe_drive::{msg::common_interfaces::std_srvs, node::Node, service::client::Client};
/// use std::sync::Arc;
///
/// fn create_new_client(node: Arc<Node>) -> Client<std_srvs::srv::Empty> {
/// node.create_client("service_name", None).unwrap()
/// }
/// ```
pub fn create_client<T: ServiceMsg>(
self: &Arc<Self>,
service_name: &str,
qos: Option<qos::Profile>,
) -> RCLResult<Client<T>> {
Client::new(self.clone(), service_name, qos)
}
}
impl Drop for Node {
fn drop(&mut self) {
let guard = rcl::MT_UNSAFE_FN.lock();
let _ = guard.rcl_node_fini(&mut self.node);
}
}
/// Options for nodes.
pub struct NodeOptions {
options: rcl::rcl_node_options_t,
}
impl Default for NodeOptions {
fn default() -> Self {
let options = rcl::MTSafeFn::rcl_node_get_default_options();
NodeOptions { options }
}
}
impl NodeOptions {
/// Create options to create a node
pub fn new() -> Self {
// TODO: allow setting options
Default::default()
}
pub(crate) fn as_ptr(&self) -> *const rcl::rcl_node_options_t {
&self.options
}
}
impl Drop for NodeOptions {
fn drop(&mut self) {
let guard = rcl::MT_UNSAFE_FN.lock();
let _ = guard.rcl_node_options_fini(&mut self.options);
}
}
unsafe impl Sync for Node {}
unsafe impl Send for Node {}