-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime Interface Reflection: rcl (#1025)
* Implement runtime type typesupport struct Signed-off-by: methylDragon <methylDragon@gmail.com> * Implement first cut Signed-off-by: methylDragon <methylDragon@gmail.com> * Migrate to rosidl_dynamic_typesupport and update field IDs Signed-off-by: methylDragon <methylDragon@gmail.com> * Migrate to type description interfaces Signed-off-by: methylDragon <methylDragon@gmail.com> * Fix const Signed-off-by: methylDragon <methylDragon@gmail.com> * Refine signatures and use return types Signed-off-by: methylDragon <methylDragon@gmail.com> * Fix C linkage Signed-off-by: methylDragon <methylDragon@gmail.com> * Migrate methods to use return types Signed-off-by: methylDragon <methylDragon@gmail.com> * Lint Signed-off-by: methylDragon <methylDragon@gmail.com> * Support type hashes Signed-off-by: methylDragon <methylDragon@gmail.com> * Remove identifier alias Signed-off-by: methylDragon <methylDragon@gmail.com> * Change fini to destroy Signed-off-by: methylDragon <methylDragon@gmail.com> * Use create instead of init Signed-off-by: methylDragon <methylDragon@gmail.com> * Add rcutils conversion function and remove rmw interfaces Signed-off-by: methylDragon <methylDragon@gmail.com> * Clean up some comments Signed-off-by: methylDragon <methylDragon@gmail.com> --------- Signed-off-by: methylDragon <methylDragon@gmail.com>
- Loading branch information
1 parent
8f878ab
commit 9a0f81b
Showing
8 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2022 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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. | ||
|
||
#ifndef RCL__DYNAMIC_MESSAGE_TYPE_SUPPORT_H_ | ||
#define RCL__DYNAMIC_MESSAGE_TYPE_SUPPORT_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <rosidl_runtime_c/type_description/type_description__struct.h> | ||
#include <rosidl_runtime_c/message_type_support_struct.h> | ||
|
||
#include "rcl/macros.h" | ||
#include "rcl/types.h" | ||
#include "rcl/visibility_control.h" | ||
|
||
/// Does not take ownership of description (copies) | ||
/// Allocates the `ts` arg. The caller takes ownership of the `ts` arg. | ||
RCL_PUBLIC | ||
RCL_WARN_UNUSED | ||
rcl_ret_t | ||
rcl_dynamic_message_type_support_handle_create( | ||
const char * serialization_lib_name, | ||
const rosidl_runtime_c__type_description__TypeDescription * desc, | ||
rosidl_message_type_support_t ** ts); // OUT | ||
|
||
/// Finalize a rosidl_message_type_support_t obtained with | ||
/// `rcl_dynamic_message_type_support_handle_create()` | ||
RCL_PUBLIC | ||
RCL_WARN_UNUSED | ||
rcl_ret_t | ||
rcl_dynamic_message_type_support_handle_destroy(rosidl_message_type_support_t * ts); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // RCL__DYNAMIC_MESSAGE_TYPE_SUPPORT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2022 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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. | ||
|
||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
#include <rcutils/logging_macros.h> | ||
#include <rosidl_runtime_c/message_type_support_struct.h> | ||
#include <rosidl_runtime_c/type_description/type_description__struct.h> | ||
|
||
#include "rmw/dynamic_message_type_support.h" | ||
|
||
#include "rcl/common.h" | ||
#include "rcl/error_handling.h" | ||
#include "rcl/dynamic_message_type_support.h" | ||
#include "rcl/type_hash.h" | ||
#include "rcl/types.h" | ||
|
||
|
||
/// Create a rosidl_message_type_support_t from a TypeDescription message | ||
RCL_PUBLIC | ||
RCL_WARN_UNUSED | ||
rcl_ret_t | ||
rcl_dynamic_message_type_support_handle_create( | ||
const char * serialization_lib_name, | ||
const rosidl_runtime_c__type_description__TypeDescription * description, | ||
rosidl_message_type_support_t ** ts) | ||
{ | ||
RCUTILS_CHECK_ARGUMENT_FOR_NULL(ts, RCUTILS_RET_INVALID_ARGUMENT); | ||
|
||
// TODO(methylDragon): Remove if and when the deferred description path is supported | ||
if (description == NULL) { | ||
RCUTILS_SET_ERROR_MSG( | ||
"Deferred type description is not currently supported. You must provide a type description."); | ||
return RCUTILS_RET_INVALID_ARGUMENT; | ||
} | ||
|
||
bool middleware_supports_type_discovery = rmw_feature_supported( | ||
RMW_MIDDLEWARE_SUPPORTS_TYPE_DISCOVERY); | ||
if (!middleware_supports_type_discovery && description == NULL) { | ||
RCL_SET_ERROR_MSG( | ||
"Middleware does not support type discovery. Deferred dynamic type message type support will " | ||
"never be populated. You must provide a type description."); | ||
return RCUTILS_RET_INVALID_ARGUMENT; | ||
} | ||
// TODO(methylDragon): Remove if and when the deferred description path is supported | ||
if (description == NULL) { | ||
RCL_SET_ERROR_MSG( | ||
"Deferred type description is not currently supported. You must provide a type description."); | ||
return RCUTILS_RET_INVALID_ARGUMENT; | ||
} | ||
|
||
rosidl_dynamic_typesupport_serialization_support_t * serialization_support = NULL; | ||
rcl_ret_t ret = rcl_convert_rmw_ret_to_rcl_ret( | ||
rmw_get_serialization_support(serialization_lib_name, &serialization_support)); | ||
if (ret != RCL_RET_OK || serialization_support == NULL) { | ||
RCL_SET_ERROR_MSG("failed to get serialization support"); | ||
if (ret == RCL_RET_OK) { // It means serialization support was NULL | ||
return RCL_RET_ERROR; | ||
} else { | ||
return ret; | ||
} | ||
} | ||
|
||
rcutils_allocator_t allocator = rcutils_get_default_allocator(); | ||
rosidl_type_hash_t * type_hash = allocator.zero_allocate( | ||
1, sizeof(rosidl_type_hash_t), &allocator.state); | ||
if (!type_hash) { | ||
RCUTILS_SET_ERROR_MSG("Could not allocate type hash"); | ||
return RCL_RET_ERROR; | ||
} | ||
|
||
ret = rcl_calculate_type_hash( | ||
// TODO(methylDragon): Replace this cast with the conversion function when it is ready | ||
// Either a custom function, or from https://github.com/ros2/rcl/pull/1052 | ||
(const type_description_interfaces__msg__TypeDescription *) description, type_hash); | ||
if (ret != RCL_RET_OK || type_hash == NULL) { | ||
RCL_SET_ERROR_MSG("failed to get type hash"); | ||
allocator.deallocate(type_hash, &allocator.state); | ||
if (ret == RCL_RET_OK) { | ||
return RCL_RET_ERROR; | ||
} else { | ||
return ret; | ||
} | ||
} | ||
|
||
ret = rcl_convert_rcutils_ret_to_rcl_ret( | ||
rosidl_dynamic_message_type_support_handle_create( | ||
serialization_support, | ||
type_hash, // type_hash | ||
description, // type_description | ||
NULL, // type_description_sources | ||
ts | ||
) | ||
); | ||
|
||
if (!ts) { | ||
RCL_SET_ERROR_MSG("failed to init rosidl_message_type_support"); | ||
allocator.deallocate(type_hash, &allocator.state); | ||
if (ret == RCL_RET_OK) { | ||
return RCL_RET_ERROR; | ||
} else { | ||
return ret; | ||
} | ||
} | ||
return RCL_RET_OK; | ||
} | ||
|
||
|
||
RCL_PUBLIC | ||
RCL_WARN_UNUSED | ||
rcl_ret_t | ||
rcl_dynamic_message_type_support_handle_destroy(rosidl_message_type_support_t * ts) | ||
{ | ||
RCL_CHECK_ARGUMENT_FOR_NULL(ts, RCL_RET_INVALID_ARGUMENT); | ||
return rcl_convert_rcutils_ret_to_rcl_ret(rosidl_dynamic_message_type_support_handle_destroy(ts)); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters