diff --git a/rmw_fastrtps_cpp/CMakeLists.txt b/rmw_fastrtps_cpp/CMakeLists.txt index 45314ad1a..497953dcf 100644 --- a/rmw_fastrtps_cpp/CMakeLists.txt +++ b/rmw_fastrtps_cpp/CMakeLists.txt @@ -57,6 +57,7 @@ add_library(rmw_fastrtps_cpp src/rmw_client.cpp src/rmw_compare_gids_equal.cpp src/rmw_count.cpp + src/rmw_event.cpp src/rmw_get_gid_for_publisher.cpp src/rmw_get_implementation_identifier.cpp src/rmw_get_serialization_format.cpp diff --git a/rmw_fastrtps_cpp/src/rmw_event.cpp b/rmw_fastrtps_cpp/src/rmw_event.cpp new file mode 100644 index 000000000..5f4684777 --- /dev/null +++ b/rmw_fastrtps_cpp/src/rmw_event.cpp @@ -0,0 +1,50 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// 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. + +#include "rmw/rmw.h" + +#include "rmw_fastrtps_shared_cpp/rmw_common.hpp" + +#include "rmw_fastrtps_cpp/identifier.hpp" + +extern "C" +{ +rmw_ret_t +rmw_publisher_event_init( + rmw_event_t * rmw_event, + const rmw_publisher_t * publisher, + rmw_event_type_t event_type) +{ + return rmw_fastrtps_shared_cpp::__rmw_init_event( + eprosima_fastrtps_identifier, + rmw_event, + publisher->implementation_identifier, + publisher->data, + event_type); +} + +rmw_ret_t +rmw_subscription_event_init( + rmw_event_t * rmw_event, + const rmw_subscription_t * subscription, + rmw_event_type_t event_type) +{ + return rmw_fastrtps_shared_cpp::__rmw_init_event( + eprosima_fastrtps_identifier, + rmw_event, + subscription->implementation_identifier, + subscription->data, + event_type); +} +} // extern "C" diff --git a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt index ace635149..7c5305851 100644 --- a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt +++ b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt @@ -61,6 +61,7 @@ add_library(rmw_fastrtps_dynamic_cpp src/rmw_client.cpp src/rmw_compare_gids_equal.cpp src/rmw_count.cpp + src/rmw_event.cpp src/rmw_get_gid_for_publisher.cpp src/rmw_get_implementation_identifier.cpp src/rmw_get_serialization_format.cpp diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_event.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_event.cpp new file mode 100644 index 000000000..c4b5a72b1 --- /dev/null +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_event.cpp @@ -0,0 +1,50 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// 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. + +#include "rmw/rmw.h" + +#include "rmw_fastrtps_shared_cpp/rmw_common.hpp" + +#include "rmw_fastrtps_dynamic_cpp/identifier.hpp" + +extern "C" +{ +rmw_ret_t +rmw_publisher_event_init( + rmw_event_t * rmw_event, + const rmw_publisher_t * publisher, + rmw_event_type_t event_type) +{ + return rmw_fastrtps_shared_cpp::__rmw_init_event( + eprosima_fastrtps_identifier, + rmw_event, + publisher->implementation_identifier, + publisher->data, + event_type); +} + +rmw_ret_t +rmw_subscription_event_init( + rmw_event_t * rmw_event, + const rmw_subscription_t * subscription, + rmw_event_type_t event_type) +{ + return rmw_fastrtps_shared_cpp::__rmw_init_event( + eprosima_fastrtps_identifier, + rmw_event, + subscription->implementation_identifier, + subscription->data, + event_type); +} +} // extern "C" diff --git a/rmw_fastrtps_shared_cpp/CMakeLists.txt b/rmw_fastrtps_shared_cpp/CMakeLists.txt index a8ba7c2f5..32abbcb64 100644 --- a/rmw_fastrtps_shared_cpp/CMakeLists.txt +++ b/rmw_fastrtps_shared_cpp/CMakeLists.txt @@ -55,6 +55,7 @@ add_library(rmw_fastrtps_shared_cpp src/rmw_client.cpp src/rmw_compare_gids_equal.cpp src/rmw_count.cpp + src/rmw_event.cpp src/rmw_get_gid_for_publisher.cpp src/rmw_get_topic_endpoint_info.cpp src/rmw_guard_condition.cpp diff --git a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_common.hpp b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_common.hpp index a73ce8fbf..13da08486 100644 --- a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_common.hpp +++ b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_common.hpp @@ -117,6 +117,15 @@ __rmw_get_node_names( rcutils_string_array_t * node_names, rcutils_string_array_t * node_namespaces); +RMW_FASTRTPS_SHARED_CPP_PUBLIC +rmw_ret_t +__rmw_init_event( + const char * identifier, + rmw_event_t * rmw_event, + const char * topic_endpoint_impl_identifier, + void * data, + rmw_event_type_t event_type); + RMW_FASTRTPS_SHARED_CPP_PUBLIC rmw_ret_t __rmw_publish( diff --git a/rmw_fastrtps_shared_cpp/src/custom_publisher_info.cpp b/rmw_fastrtps_shared_cpp/src/custom_publisher_info.cpp index 20305fcb2..f38f653dd 100644 --- a/rmw_fastrtps_shared_cpp/src/custom_publisher_info.cpp +++ b/rmw_fastrtps_shared_cpp/src/custom_publisher_info.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include "rmw_fastrtps_shared_cpp/custom_publisher_info.hpp" +#include "types/event_types.hpp" EventListenerInterface * CustomPublisherInfo::getListener() const @@ -59,6 +60,7 @@ void PubListener::on_liveliness_lost( bool PubListener::hasEvent(rmw_event_type_t event_type) const { + assert(rmw_fastrtps_shared_cpp::internal::is_event_supported(event_type)); switch (event_type) { case RMW_EVENT_LIVELINESS_LOST: return liveliness_changes_.load(std::memory_order_relaxed); @@ -72,6 +74,7 @@ bool PubListener::hasEvent(rmw_event_type_t event_type) const bool PubListener::takeNextEvent(rmw_event_type_t event_type, void * event_info) { + assert(rmw_fastrtps_shared_cpp::internal::is_event_supported(event_type)); std::lock_guard lock(internalMutex_); switch (event_type) { case RMW_EVENT_LIVELINESS_LOST: diff --git a/rmw_fastrtps_shared_cpp/src/custom_subscriber_info.cpp b/rmw_fastrtps_shared_cpp/src/custom_subscriber_info.cpp index d3f683502..172c20ab1 100644 --- a/rmw_fastrtps_shared_cpp/src/custom_subscriber_info.cpp +++ b/rmw_fastrtps_shared_cpp/src/custom_subscriber_info.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include "rmw_fastrtps_shared_cpp/custom_subscriber_info.hpp" +#include "types/event_types.hpp" EventListenerInterface * CustomSubscriberInfo::getListener() const @@ -61,6 +62,7 @@ void SubListener::on_liveliness_changed( bool SubListener::hasEvent(rmw_event_type_t event_type) const { + assert(rmw_fastrtps_shared_cpp::internal::is_event_supported(event_type)); switch (event_type) { case RMW_EVENT_LIVELINESS_CHANGED: return liveliness_changes_.load(std::memory_order_relaxed); @@ -74,6 +76,7 @@ bool SubListener::hasEvent(rmw_event_type_t event_type) const bool SubListener::takeNextEvent(rmw_event_type_t event_type, void * event_info) { + assert(rmw_fastrtps_shared_cpp::internal::is_event_supported(event_type)); std::lock_guard lock(internalMutex_); switch (event_type) { case RMW_EVENT_LIVELINESS_CHANGED: diff --git a/rmw_fastrtps_shared_cpp/src/rmw_event.cpp b/rmw_fastrtps_shared_cpp/src/rmw_event.cpp new file mode 100644 index 000000000..25a8c73b8 --- /dev/null +++ b/rmw_fastrtps_shared_cpp/src/rmw_event.cpp @@ -0,0 +1,70 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// 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. + +#include + +#include "rmw/impl/cpp/macros.hpp" + +#include "rmw_fastrtps_shared_cpp/rmw_common.hpp" +#include "types/event_types.hpp" + +static const std::unordered_set g_rmw_event_type_set{ + RMW_EVENT_LIVELINESS_CHANGED, + RMW_EVENT_REQUESTED_DEADLINE_MISSED, + RMW_EVENT_LIVELINESS_LOST, + RMW_EVENT_OFFERED_DEADLINE_MISSED +}; + +namespace rmw_fastrtps_shared_cpp +{ +namespace internal +{ + +bool is_event_supported(rmw_event_type_t event_type) +{ + return g_rmw_event_type_set.count(event_type) == 1; +} + +} // namespace internal + +rmw_ret_t +__rmw_init_event( + const char * identifier, + rmw_event_t * rmw_event, + const char * topic_endpoint_impl_identifier, + void * data, + rmw_event_type_t event_type) +{ + RMW_CHECK_ARGUMENT_FOR_NULL(identifier, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_ARGUMENT_FOR_NULL(rmw_event, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_ARGUMENT_FOR_NULL(topic_endpoint_impl_identifier, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_ARGUMENT_FOR_NULL(data, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + topic endpoint, + topic_endpoint_impl_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + if (!internal::is_event_supported(event_type)) { + RMW_SET_ERROR_MSG("provided event_type is not supported by rmw_fastrtps_cpp"); + return RMW_RET_UNSUPPORTED; + } + + rmw_event->implementation_identifier = topic_endpoint_impl_identifier; + rmw_event->data = data; + rmw_event->event_type = event_type; + + return RMW_RET_OK; +} + +} // namespace rmw_fastrtps_shared_cpp diff --git a/rmw_fastrtps_shared_cpp/src/types/event_types.hpp b/rmw_fastrtps_shared_cpp/src/types/event_types.hpp new file mode 100644 index 000000000..8eb9d68da --- /dev/null +++ b/rmw_fastrtps_shared_cpp/src/types/event_types.hpp @@ -0,0 +1,30 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// 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 TYPES__EVENT_TYPES_HPP_ +#define TYPES__EVENT_TYPES_HPP_ + +#include "rmw/event.h" + +namespace rmw_fastrtps_shared_cpp +{ +namespace internal +{ + +bool is_event_supported(rmw_event_type_t event_type); + +} // namespace internal +} // namespace rmw_fastrtps_shared_cpp + +#endif // TYPES__EVENT_TYPES_HPP_