Skip to content

Commit dbf1ada

Browse files
committed
Split rcl_action_bindings.cpp (#1105)
This PR splits the existing rcl_action_bindings.cpp into separate files for client, goal, and server bindings and adds corresponding API functions in rcl_node_bindings.cpp. It also updates initialization in addon.cpp and the build configuration in binding.gyp to reflect the new file structure. - Adds new action-related functions in rcl_node_bindings.cpp for fetching names and types. - Introduces new binding files for action client, goal, and server. - Updates addon.cpp and binding.gyp to integrate the new files. Fix: #1104
1 parent 425e832 commit dbf1ada

9 files changed

+565
-444
lines changed

binding.gyp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
'./src/addon.cpp',
2222
'./src/executor.cpp',
2323
'./src/handle_manager.cpp',
24-
'./src/rcl_action_bindings.cpp',
24+
'./src/rcl_action_client_bindings.cpp',
25+
'./src/rcl_action_goal_bindings.cpp',
26+
'./src/rcl_action_server_bindings.cpp',
2527
'./src/rcl_bindings.cpp',
2628
'./src/rcl_client_bindings.cpp',
2729
'./src/rcl_context_bindings.cpp',

src/addon.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include <rcutils/logging.h>
1717

1818
#include "macros.h"
19-
#include "rcl_action_bindings.h"
19+
#include "rcl_action_client_bindings.h"
20+
#include "rcl_action_goal_bindings.h"
21+
#include "rcl_action_server_bindings.h"
2022
#include "rcl_bindings.h"
2123
#include "rcl_client_bindings.h"
2224
#include "rcl_context_bindings.h"
@@ -62,6 +64,9 @@ Napi::Object InitModule(Napi::Env env, Napi::Object exports) {
6264
rclnodejs::StoreEnv(env);
6365
// Init the C++ bindings.
6466
rclnodejs::InitBindings(env, exports);
67+
rclnodejs::InitActionClientBindings(env, exports);
68+
rclnodejs::InitActionGoalBindings(env, exports);
69+
rclnodejs::InitActionServerBindings(env, exports);
6570
rclnodejs::InitClientBindings(env, exports);
6671
rclnodejs::InitContextBindings(env, exports);
6772
rclnodejs::InitGraphBindings(env, exports);
@@ -74,7 +79,6 @@ Napi::Object InitModule(Napi::Env env, Napi::Object exports) {
7479
rclnodejs::InitSubscriptionBindings(env, exports);
7580
rclnodejs::InitTimePointBindings(env, exports);
7681
rclnodejs::InitTimerBindings(env, exports);
77-
rclnodejs::InitActionBindings(env, exports);
7882
rclnodejs::InitLifecycleBindings(env, exports);
7983
rclnodejs::ShadowNode::Init(env, exports);
8084
rclnodejs::RclHandle::Init(env, exports);

src/rcl_action_client_bindings.cpp

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// Copyright (c) 2020 Matt Richard. All rights reserved.
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+
#include "rcl_action_client_bindings.h"
16+
17+
#include <rcl/error_handling.h>
18+
#include <rcl/rcl.h>
19+
#include <rcl_action/rcl_action.h>
20+
21+
#include <string>
22+
23+
#include "handle_manager.h"
24+
#include "macros.h"
25+
#include "rcl_handle.h"
26+
#include "rcl_utilities.h"
27+
28+
namespace rclnodejs {
29+
30+
Napi::Value ActionCreateClient(const Napi::CallbackInfo& info) {
31+
Napi::Env env = info.Env();
32+
33+
RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
34+
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
35+
std::string action_name = info[1].As<Napi::String>().Utf8Value();
36+
std::string interface_name = info[2].As<Napi::String>().Utf8Value();
37+
std::string package_name = info[3].As<Napi::String>().Utf8Value();
38+
39+
const rosidl_action_type_support_t* ts =
40+
GetActionTypeSupport(package_name, interface_name);
41+
42+
if (ts) {
43+
rcl_action_client_options_t action_client_ops =
44+
rcl_action_client_get_default_options();
45+
46+
auto goal_service_qos = GetQoSProfile(info[4]);
47+
auto result_service_qos = GetQoSProfile(info[5]);
48+
auto cancel_service_qos = GetQoSProfile(info[6]);
49+
auto feedback_topic_qos = GetQoSProfile(info[7]);
50+
auto status_topic_qos = GetQoSProfile(info[8]);
51+
52+
if (goal_service_qos) {
53+
action_client_ops.goal_service_qos = *goal_service_qos;
54+
}
55+
if (result_service_qos) {
56+
action_client_ops.result_service_qos = *result_service_qos;
57+
}
58+
if (cancel_service_qos) {
59+
action_client_ops.cancel_service_qos = *cancel_service_qos;
60+
}
61+
if (feedback_topic_qos) {
62+
action_client_ops.feedback_topic_qos = *feedback_topic_qos;
63+
}
64+
if (status_topic_qos) {
65+
action_client_ops.status_topic_qos = *status_topic_qos;
66+
}
67+
68+
rcl_action_client_t* action_client = reinterpret_cast<rcl_action_client_t*>(
69+
malloc(sizeof(rcl_action_client_t)));
70+
*action_client = rcl_action_get_zero_initialized_client();
71+
72+
THROW_ERROR_IF_NOT_EQUAL(
73+
rcl_action_client_init(action_client, node, ts, action_name.c_str(),
74+
&action_client_ops),
75+
RCL_RET_OK, rcl_get_error_string().str);
76+
auto js_obj = RclHandle::NewInstance(
77+
env, action_client, node_handle, [node](void* ptr) {
78+
rcl_action_client_t* action_client =
79+
reinterpret_cast<rcl_action_client_t*>(ptr);
80+
rcl_ret_t ret = rcl_action_client_fini(action_client, node);
81+
free(ptr);
82+
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, ret, rcl_get_error_string().str);
83+
});
84+
85+
return js_obj;
86+
} else {
87+
Napi::Error::New(env, GetErrorMessageAndClear())
88+
.ThrowAsJavaScriptException();
89+
return env.Undefined();
90+
}
91+
}
92+
93+
Napi::Value ActionServerIsAvailable(const Napi::CallbackInfo& info) {
94+
Napi::Env env = info.Env();
95+
96+
RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
97+
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
98+
RclHandle* action_client_handle =
99+
RclHandle::Unwrap(info[1].As<Napi::Object>());
100+
rcl_action_client_t* action_client =
101+
reinterpret_cast<rcl_action_client_t*>(action_client_handle->ptr());
102+
103+
bool is_available;
104+
THROW_ERROR_IF_NOT_EQUAL(
105+
RCL_RET_OK,
106+
rcl_action_server_is_available(node, action_client, &is_available),
107+
rcl_get_error_string().str);
108+
109+
return Napi::Boolean::New(env, is_available);
110+
}
111+
112+
Napi::Value ActionSendGoalRequest(const Napi::CallbackInfo& info) {
113+
Napi::Env env = info.Env();
114+
115+
RclHandle* action_client_handle =
116+
RclHandle::Unwrap(info[0].As<Napi::Object>());
117+
rcl_action_client_t* action_client =
118+
reinterpret_cast<rcl_action_client_t*>(action_client_handle->ptr());
119+
void* buffer = info[1].As<Napi::Buffer<char>>().Data();
120+
121+
int64_t sequence_number;
122+
THROW_ERROR_IF_NOT_EQUAL(
123+
rcl_action_send_goal_request(action_client, buffer, &sequence_number),
124+
RCL_RET_OK, rcl_get_error_string().str);
125+
126+
return Napi::Number::New(env, static_cast<int32_t>(sequence_number));
127+
}
128+
129+
Napi::Value ActionTakeCancelRequest(const Napi::CallbackInfo& info) {
130+
Napi::Env env = info.Env();
131+
132+
RclHandle* action_server_handle =
133+
RclHandle::Unwrap(info[0].As<Napi::Object>());
134+
rcl_action_server_t* action_server =
135+
reinterpret_cast<rcl_action_server_t*>(action_server_handle->ptr());
136+
rmw_request_id_t* header =
137+
reinterpret_cast<rmw_request_id_t*>(malloc(sizeof(rmw_request_id_t)));
138+
139+
void* taken_request = info[1].As<Napi::Buffer<char>>().Data();
140+
rcl_ret_t ret =
141+
rcl_action_take_cancel_request(action_server, header, taken_request);
142+
if (ret != RCL_RET_ACTION_SERVER_TAKE_FAILED) {
143+
auto js_obj = RclHandle::NewInstance(env, header, nullptr,
144+
[](void* ptr) { free(ptr); });
145+
return js_obj;
146+
}
147+
148+
return env.Undefined();
149+
}
150+
151+
Napi::Value ActionSendResultRequest(const Napi::CallbackInfo& info) {
152+
Napi::Env env = info.Env();
153+
154+
RclHandle* action_client_handle =
155+
RclHandle::Unwrap(info[0].As<Napi::Object>());
156+
rcl_action_client_t* action_client =
157+
reinterpret_cast<rcl_action_client_t*>(action_client_handle->ptr());
158+
void* buffer = info[1].As<Napi::Buffer<char>>().Data();
159+
160+
int64_t sequence_number;
161+
THROW_ERROR_IF_NOT_EQUAL(
162+
rcl_action_send_result_request(action_client, buffer, &sequence_number),
163+
RCL_RET_OK, rcl_get_error_string().str);
164+
165+
return Napi::Number::New(env, static_cast<int32_t>(sequence_number));
166+
}
167+
168+
Napi::Value ActionTakeFeedback(const Napi::CallbackInfo& info) {
169+
Napi::Env env = info.Env();
170+
171+
RclHandle* action_client_handle =
172+
RclHandle::Unwrap(info[0].As<Napi::Object>());
173+
rcl_action_client_t* action_client =
174+
reinterpret_cast<rcl_action_client_t*>(action_client_handle->ptr());
175+
void* buffer = info[1].As<Napi::Buffer<char>>().Data();
176+
177+
rcl_ret_t ret = rcl_action_take_feedback(action_client, buffer);
178+
if (ret != RCL_RET_OK && ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) {
179+
Napi::Error::New(env, rcl_get_error_string().str)
180+
.ThrowAsJavaScriptException();
181+
rcl_reset_error();
182+
return Napi::Boolean::New(env, false);
183+
}
184+
185+
if (ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) {
186+
return Napi::Boolean::New(env, true);
187+
}
188+
return env.Undefined();
189+
}
190+
191+
Napi::Value ActionTakeStatus(const Napi::CallbackInfo& info) {
192+
Napi::Env env = info.Env();
193+
194+
RclHandle* action_client_handle =
195+
RclHandle::Unwrap(info[0].As<Napi::Object>());
196+
rcl_action_client_t* action_client =
197+
reinterpret_cast<rcl_action_client_t*>(action_client_handle->ptr());
198+
void* buffer = info[1].As<Napi::Buffer<char>>().Data();
199+
200+
rcl_ret_t ret = rcl_action_take_status(action_client, buffer);
201+
if (ret != RCL_RET_OK && ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) {
202+
rcl_reset_error();
203+
Napi::Error::New(env, rcl_get_error_string().str)
204+
.ThrowAsJavaScriptException();
205+
return Napi::Boolean::New(env, false);
206+
}
207+
208+
if (ret != RCL_RET_ACTION_CLIENT_TAKE_FAILED) {
209+
return Napi::Boolean::New(env, true);
210+
}
211+
return env.Undefined();
212+
}
213+
214+
Napi::Object InitActionClientBindings(Napi::Env env, Napi::Object exports) {
215+
exports.Set("actionCreateClient",
216+
Napi::Function::New(env, ActionCreateClient));
217+
exports.Set("actionServerIsAvailable",
218+
Napi::Function::New(env, ActionServerIsAvailable));
219+
exports.Set("actionSendGoalRequest",
220+
Napi::Function::New(env, ActionSendGoalRequest));
221+
exports.Set("actionTakeCancelRequest",
222+
Napi::Function::New(env, ActionTakeCancelRequest));
223+
exports.Set("actionSendResultRequest",
224+
Napi::Function::New(env, ActionSendResultRequest));
225+
exports.Set("actionTakeFeedback",
226+
Napi::Function::New(env, ActionTakeFeedback));
227+
exports.Set("actionTakeStatus", Napi::Function::New(env, ActionTakeStatus));
228+
return exports;
229+
}
230+
231+
} // namespace rclnodejs
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Intel Corporation. All rights reserved.
1+
// Copyright (c) 2020 Matt Richard. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,15 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifndef SRC_RCL_ACTION_BINDINGS_H_
16-
#define SRC_RCL_ACTION_BINDINGS_H_
15+
#ifndef SRC_RCL_ACTION_CLIENT_BINDINGS_H_
16+
#define SRC_RCL_ACTION_CLIENT_BINDINGS_H_
1717

1818
#include <napi.h>
1919

2020
namespace rclnodejs {
2121

22-
Napi::Object InitActionBindings(Napi::Env env, Napi::Object exports);
22+
Napi::Object InitActionClientBindings(Napi::Env env, Napi::Object exports);
2323

24-
} // namespace rclnodejs
24+
}
2525

26-
#endif // SRC_RCL_ACTION_BINDINGS_H_
26+
#endif // SRC_RCL_ACTION_CLIENT_BINDINGS_H_

0 commit comments

Comments
 (0)