Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import ActionUuid from './lib/action/uuid.js';
import ClientGoalHandle from './lib/action/client_goal_handle.js';
import { CancelResponse, GoalResponse } from './lib/action/response.js';
import ServerGoalHandle from './lib/action/server_goal_handle.js';
import ActionEndpointInfo from './lib/action/endpoint_info.js';
import { toJSONSafe, toJSONString } from './lib/message_serialization.js';
import {
getActionClientNamesAndTypesByNode,
Expand Down Expand Up @@ -295,6 +296,9 @@ let rcl = {
/** {@link ActionUuid} class */
ActionUuid: ActionUuid,

/** {@link ActionEndpointInfo} class */
ActionEndpointInfo: ActionEndpointInfo,

/** {@link ClientGoalHandle} class */
ClientGoalHandle: ClientGoalHandle,

Expand Down
95 changes: 95 additions & 0 deletions lib/action/endpoint_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) 2026, The Robot Web Tools Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Aggregated endpoint information for one action client or server.
*/
class ActionEndpointInfo {
/**
* @param {object} info - Raw endpoint information from the native layer.
* @hideconstructor
*/
constructor(info) {
this._goalServiceInfo = info.goal_service_info;
this._cancelServiceInfo = info.cancel_service_info;
this._resultServiceInfo = info.result_service_info;
this._feedbackTopicInfo = info.feedback_topic_info;
this._statusTopicInfo = info.status_topic_info;
}

/**
* @type {object}
*/
get goalServiceInfo() {
return this._goalServiceInfo;
}

/**
* @type {object}
*/
get cancelServiceInfo() {
return this._cancelServiceInfo;
}

/**
* @type {object}
*/
get resultServiceInfo() {
return this._resultServiceInfo;
}

/**
* @type {object}
*/
get feedbackTopicInfo() {
return this._feedbackTopicInfo;
}

/**
* @type {object}
*/
get statusTopicInfo() {
return this._statusTopicInfo;
}

/**
* @type {string}
*/
get nodeName() {
return this._goalServiceInfo.node_name;
}

/**
* @type {string}
*/
get nodeNamespace() {
return this._goalServiceInfo.node_namespace;
}

/**
* @type {string}
*/
get actionType() {
return this._goalServiceInfo.service_type.replace(/_SendGoal$/, '');
}

/**
* @type {number}
*/
get endpointType() {
return this._goalServiceInfo.endpoint_type;
}
}

export default ActionEndpointInfo;
88 changes: 88 additions & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Service from './service.js';
import Subscription from './subscription.js';
import ObservableSubscription from './observable_subscription.js';
import MessageInfo from './message_info.js';
import ActionEndpointInfo from './action/endpoint_info.js';
import { declareQosParameters, _resolveQoS } from './qos_overriding_options.js';
import TimeSource from './time_source.js';
import Timer from './timer.js';
Expand Down Expand Up @@ -1511,6 +1512,82 @@ class Node extends rclnodejs.ShadowNode {
return rclnodejs.getServiceNamesAndTypes(this.handle);
}

/**
* Return the number of action clients on an action.
* @param {string} actionName - The action name to query.
* @returns {number|null} The number of action clients, or null when the ROS distro is older than Lyrical.
*/
countActionClients(actionName) {
if (typeof rclnodejs.countActionClients !== 'function') {
console.warn(
'countActionClients is not supported by this version of ROS 2'
);
return null;
}
return rclnodejs.countActionClients(
this.handle,
this._getValidatedActionName(actionName)
);
}

/**
* Return the number of action servers on an action.
* @param {string} actionName - The action name to query.
* @returns {number|null} The number of action servers, or null when the ROS distro is older than Lyrical.
*/
countActionServers(actionName) {
if (typeof rclnodejs.countActionServers !== 'function') {
console.warn(
'countActionServers is not supported by this version of ROS 2'
);
return null;
}
return rclnodejs.countActionServers(
this.handle,
this._getValidatedActionName(actionName)
);
}

/**
* Return endpoint information for action clients on an action.
* @param {string} actionName - The action name to query.
* @returns {Array<ActionEndpointInfo>|null} The action clients, or null when the ROS distro is older than Rolling.
*/
getActionClientsInfoByAction(actionName) {
if (typeof rclnodejs.getActionClientsInfoByAction !== 'function') {
console.warn(
'getActionClientsInfoByAction is not supported by this version of ROS 2'
);
return null;
}
return rclnodejs
.getActionClientsInfoByAction(
this.handle,
this._getValidatedActionName(actionName)
)
.map((info) => new ActionEndpointInfo(info));
}

/**
* Return endpoint information for action servers on an action.
* @param {string} actionName - The action name to query.
* @returns {Array<ActionEndpointInfo>|null} The action servers, or null when the ROS distro is older than Rolling.
*/
getActionServersInfoByAction(actionName) {
if (typeof rclnodejs.getActionServersInfoByAction !== 'function') {
console.warn(
'getActionServersInfoByAction is not supported by this version of ROS 2'
);
return null;
}
return rclnodejs
.getActionServersInfoByAction(
this.handle,
this._getValidatedActionName(actionName)
)
.map((info) => new ActionEndpointInfo(info));
}

/**
* Return a list of publishers on a given topic.
*
Expand Down Expand Up @@ -2520,6 +2597,17 @@ class Node extends rclnodejs.ShadowNode {
this.syncHandles();
}

// Unlike topic/service queries, the queried action name is not remapped.
_getValidatedActionName(actionName) {
const fullyQualifiedActionName = rclnodejs.expandTopicName(
actionName,
this.name(),
this.namespace()
);
validateFullTopicName(fullyQualifiedActionName);
return fullyQualifiedActionName;
}

_getValidatedTopic(topicName, noDemangle) {
if (noDemangle) {
return topicName;
Expand Down
99 changes: 99 additions & 0 deletions src/rcl_graph_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <rcl/error_handling.h>
#include <rcl/graph.h>
#include <rcl/rcl.h>
#include <rcl_action/graph.h>

#include <string>

Expand All @@ -40,6 +41,12 @@ typedef rcl_ret_t (*rcl_get_info_by_service_func_t)(
rcl_service_endpoint_info_array_t* info_array);
#endif // ROS_VERSION >= 2605

#if ROS_VERSION >= 5000
typedef rcl_ret_t (*rcl_action_get_info_by_action_func_t)(
const rcl_node_t* node, rcutils_allocator_t* allocator,
const char* action_name, rcl_action_endpoint_info_array_t* info_array);
#endif // ROS_VERSION >= 5000

Napi::Value GetPublisherNamesAndTypesByNode(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

Expand Down Expand Up @@ -207,6 +214,34 @@ Napi::Value GetServiceNamesAndTypes(const Napi::CallbackInfo& info) {
return result_list;
}

#if ROS_VERSION >= 2605
Napi::Value CountActionClients(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string action_name = info[1].As<Napi::String>().Utf8Value();
size_t count = 0;

THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_action_count_clients(node, action_name.c_str(), &count),
"Failed to count action clients");
return Napi::Number::New(env, count);
}

Napi::Value CountActionServers(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string action_name = info[1].As<Napi::String>().Utf8Value();
size_t count = 0;

THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_action_count_servers(node, action_name.c_str(), &count),
"Failed to count action servers");
return Napi::Number::New(env, count);
}
#endif // ROS_VERSION >= 2605

Napi::Value GetInfoByTopic(Napi::Env env, rcl_node_t* node,
const char* topic_name, bool no_mangle,
const char* type,
Expand Down Expand Up @@ -324,6 +359,60 @@ Napi::Value GetServersInfoByService(const Napi::CallbackInfo& info) {
}
#endif // ROS_VERSION >= 2605

#if ROS_VERSION >= 5000
Napi::Value GetInfoByAction(
Napi::Env env, rcl_node_t* node, const char* action_name, const char* type,
rcl_action_get_info_by_action_func_t rcl_action_get_info_by_action) {
rcutils_allocator_t allocator = rcutils_get_default_allocator();
rcl_action_endpoint_info_array_t info_array =
rcl_action_get_zero_initialized_endpoint_info_array();

RCPPUTILS_SCOPE_EXIT({
rcl_ret_t fini_ret =
rcl_action_endpoint_info_array_fini(&info_array, &allocator);
if (RCL_RET_OK != fini_ret) {
Napi::Error::New(env, rcl_get_error_string().str)
.ThrowAsJavaScriptException();
rcl_reset_error();
}
});

rcl_ret_t ret =
rcl_action_get_info_by_action(node, &allocator, action_name, &info_array);
if (RCL_RET_OK != ret) {
if (RCL_RET_UNSUPPORTED == ret) {
Napi::Error::New(
env, std::string("Failed to get information by action for ") + type +
": function not supported by RMW_IMPLEMENTATION")
.ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Error::New(
env, std::string("Failed to get information by action for ") + type)
.ThrowAsJavaScriptException();
return env.Undefined();
}

return ConvertToJSActionEndpointInfoList(env, &info_array);
}

Napi::Value GetActionClientsInfoByAction(const Napi::CallbackInfo& info) {
RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string action_name = info[1].As<Napi::String>().Utf8Value();
return GetInfoByAction(info.Env(), node, action_name.c_str(), "clients",
rcl_action_get_clients_info_by_action);
}

Napi::Value GetActionServersInfoByAction(const Napi::CallbackInfo& info) {
RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string action_name = info[1].As<Napi::String>().Utf8Value();
return GetInfoByAction(info.Env(), node, action_name.c_str(), "servers",
rcl_action_get_servers_info_by_action);
}
#endif // ROS_VERSION >= 5000

Napi::Object InitGraphBindings(Napi::Env env, Napi::Object exports) {
exports.Set("getPublisherNamesAndTypesByNode",
Napi::Function::New(env, GetPublisherNamesAndTypesByNode));
Expand All @@ -342,11 +431,21 @@ Napi::Object InitGraphBindings(Napi::Env env, Napi::Object exports) {
exports.Set("getSubscriptionsInfoByTopic",
Napi::Function::New(env, GetSubscriptionsInfoByTopic));
#if ROS_VERSION >= 2605
exports.Set("countActionClients",
Napi::Function::New(env, CountActionClients));
exports.Set("countActionServers",
Napi::Function::New(env, CountActionServers));
exports.Set("getClientsInfoByService",
Napi::Function::New(env, GetClientsInfoByService));
exports.Set("getServersInfoByService",
Napi::Function::New(env, GetServersInfoByService));
#endif // ROS_VERSION >= 2605
#if ROS_VERSION >= 5000
exports.Set("getActionClientsInfoByAction",
Napi::Function::New(env, GetActionClientsInfoByAction));
exports.Set("getActionServersInfoByAction",
Napi::Function::New(env, GetActionServersInfoByAction));
#endif // ROS_VERSION >= 5000
return exports;
}

Expand Down
Loading
Loading