@@ -33,6 +33,11 @@ typedef rcl_ret_t (*rcl_get_info_by_topic_func_t)(
3333 const char * topic_name, bool no_mangle,
3434 rcl_topic_endpoint_info_array_t * info_array);
3535
36+ typedef rcl_ret_t (*rcl_get_info_by_service_func_t )(
37+ const rcl_node_t * node, rcutils_allocator_t * allocator,
38+ const char * service_name, bool no_mangle,
39+ rcl_service_endpoint_info_array_t * info_array);
40+
3641Napi::Value GetPublisherNamesAndTypesByNode (const Napi::CallbackInfo& info) {
3742 Napi::Env env = info.Env ();
3843
@@ -257,6 +262,64 @@ Napi::Value GetSubscriptionsInfoByTopic(const Napi::CallbackInfo& info) {
257262 " subscriptions" , rcl_get_subscriptions_info_by_topic);
258263}
259264
265+ Napi::Value GetInfoByService (
266+ Napi::Env env, rcl_node_t * node, const char * service_name, bool no_mangle,
267+ const char * type, rcl_get_info_by_service_func_t rcl_get_info_by_service) {
268+ rcutils_allocator_t allocator = rcutils_get_default_allocator ();
269+ rcl_service_endpoint_info_array_t info_array =
270+ rcl_get_zero_initialized_service_endpoint_info_array ();
271+
272+ RCPPUTILS_SCOPE_EXIT ({
273+ rcl_ret_t fini_ret =
274+ rcl_service_endpoint_info_array_fini (&info_array, &allocator);
275+ if (RCL_RET_OK != fini_ret) {
276+ Napi::Error::New (env, rcl_get_error_string ().str )
277+ .ThrowAsJavaScriptException ();
278+ rcl_reset_error ();
279+ }
280+ });
281+
282+ rcl_ret_t ret = rcl_get_info_by_service (node, &allocator, service_name,
283+ no_mangle, &info_array);
284+ if (RCL_RET_OK != ret) {
285+ if (RCL_RET_UNSUPPORTED == ret) {
286+ Napi::Error::New (
287+ env, std::string (" Failed to get information by service for " ) + type +
288+ " : function not supported by RMW_IMPLEMENTATION" )
289+ .ThrowAsJavaScriptException ();
290+ return env.Undefined ();
291+ }
292+ Napi::Error::New (
293+ env, std::string (" Failed to get information by service for " ) + type)
294+ .ThrowAsJavaScriptException ();
295+ return env.Undefined ();
296+ }
297+
298+ return ConvertToJSServiceEndpointInfoList (env, &info_array);
299+ }
300+
301+ #if ROS_VERSION > 2405
302+ Napi::Value GetClientsInfoByService (const Napi::CallbackInfo& info) {
303+ RclHandle* node_handle = RclHandle::Unwrap (info[0 ].As <Napi::Object>());
304+ rcl_node_t * node = reinterpret_cast <rcl_node_t *>(node_handle->ptr ());
305+ std::string service_name = info[1 ].As <Napi::String>().Utf8Value ();
306+ bool no_mangle = info[2 ].As <Napi::Boolean>();
307+
308+ return GetInfoByService (info.Env (), node, service_name.c_str (), no_mangle,
309+ " clients" , rcl_get_clients_info_by_service);
310+ }
311+
312+ Napi::Value GetServersInfoByService (const Napi::CallbackInfo& info) {
313+ RclHandle* node_handle = RclHandle::Unwrap (info[0 ].As <Napi::Object>());
314+ rcl_node_t * node = reinterpret_cast <rcl_node_t *>(node_handle->ptr ());
315+ std::string service_name = info[1 ].As <Napi::String>().Utf8Value ();
316+ bool no_mangle = info[2 ].As <Napi::Boolean>();
317+
318+ return GetInfoByService (info.Env (), node, service_name.c_str (), no_mangle,
319+ " servers" , rcl_get_servers_info_by_service);
320+ }
321+ #endif
322+
260323Napi::Object InitGraphBindings (Napi::Env env, Napi::Object exports) {
261324 exports.Set (" getPublisherNamesAndTypesByNode" ,
262325 Napi::Function::New (env, GetPublisherNamesAndTypesByNode));
@@ -274,6 +337,12 @@ Napi::Object InitGraphBindings(Napi::Env env, Napi::Object exports) {
274337 Napi::Function::New (env, GetPublishersInfoByTopic));
275338 exports.Set (" getSubscriptionsInfoByTopic" ,
276339 Napi::Function::New (env, GetSubscriptionsInfoByTopic));
340+ #if ROS_VERSION > 2405
341+ exports.Set (" getClientsInfoByService" ,
342+ Napi::Function::New (env, GetClientsInfoByService));
343+ exports.Set (" getServersInfoByService" ,
344+ Napi::Function::New (env, GetServersInfoByService));
345+ #endif
277346 return exports;
278347}
279348
0 commit comments