Skip to content

Commit

Permalink
feat: support ROS2 humble
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan committed Dec 10, 2024
1 parent 4f34079 commit a442bc7
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 187 deletions.
14 changes: 7 additions & 7 deletions rmw_zenoh_cpp/src/detail/attachment_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ namespace rmw_zenoh_cpp
AttachmentData::AttachmentData(
const int64_t sequence_number,
const int64_t source_timestamp,
const uint8_t source_gid[RMW_GID_STORAGE_SIZE])
const uint8_t source_gid[16])
: sequence_number_(sequence_number),
source_timestamp_(source_timestamp)
{
memcpy(source_gid_, source_gid, RMW_GID_STORAGE_SIZE);
memcpy(source_gid_, source_gid, 16);
gid_hash_ = hash_gid(source_gid_);
}

Expand All @@ -45,7 +45,7 @@ AttachmentData::AttachmentData(AttachmentData && data)
source_timestamp_(std::move(data.source_timestamp_)),
gid_hash_(std::move(data.gid_hash_))
{
memcpy(source_gid_, data.source_gid_, RMW_GID_STORAGE_SIZE);
memcpy(source_gid_, data.source_gid_, 16);
}

///=============================================================================
Expand Down Expand Up @@ -90,7 +90,7 @@ AttachmentData::AttachmentData(const z_loaned_bytes_t * attachment)
if (ze_deserializer_deserialize_slice(&deserializer, &slice)) {
throw std::runtime_error("Failed to deserialize the source_gid.");
}
if (z_slice_len(z_loan(slice)) != RMW_GID_STORAGE_SIZE) {
if (z_slice_len(z_loan(slice)) != 16) {
throw std::runtime_error("The length of source_gid mismatched.");
}
memcpy(this->source_gid_, z_slice_data(z_loan(slice)), z_slice_len(z_loan(slice)));
Expand All @@ -111,9 +111,9 @@ int64_t AttachmentData::source_timestamp() const
}

///=============================================================================
void AttachmentData::copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const
void AttachmentData::copy_gid(uint8_t out_gid[16]) const
{
memcpy(out_gid, source_gid_, RMW_GID_STORAGE_SIZE);
memcpy(out_gid, source_gid_, 16);
}

///=============================================================================
Expand All @@ -132,7 +132,7 @@ void AttachmentData::serialize_to_zbytes(z_owned_bytes_t * attachment)
ze_serializer_serialize_str(z_loan_mut(serializer), "source_timestamp");
ze_serializer_serialize_int64(z_loan_mut(serializer), this->source_timestamp_);
ze_serializer_serialize_str(z_loan_mut(serializer), "source_gid");
ze_serializer_serialize_buf(z_loan_mut(serializer), this->source_gid_, RMW_GID_STORAGE_SIZE);
ze_serializer_serialize_buf(z_loan_mut(serializer), this->source_gid_, 16);
ze_serializer_finish(z_move(serializer), attachment);
}
} // namespace rmw_zenoh_cpp
6 changes: 3 additions & 3 deletions rmw_zenoh_cpp/src/detail/attachment_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class AttachmentData final
AttachmentData(
const int64_t sequence_number,
const int64_t source_timestamp,
const uint8_t source_gid[RMW_GID_STORAGE_SIZE]);
const uint8_t source_gid[16]);
explicit AttachmentData(const z_loaned_bytes_t *);
explicit AttachmentData(AttachmentData && data);

int64_t sequence_number() const;
int64_t source_timestamp() const;
void copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const;
void copy_gid(uint8_t out_gid[16]) const;
size_t gid_hash() const;

void serialize_to_zbytes(z_owned_bytes_t *);

private:
int64_t sequence_number_;
int64_t source_timestamp_;
uint8_t source_gid_[RMW_GID_STORAGE_SIZE];
uint8_t source_gid_[16];
size_t gid_hash_;
};
} // namespace rmw_zenoh_cpp
Expand Down
10 changes: 5 additions & 5 deletions rmw_zenoh_cpp/src/detail/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ static const std::unordered_map<rmw_event_type_t, rmw_zenoh_cpp::rmw_zenoh_event
{RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE, rmw_zenoh_cpp::ZENOH_EVENT_REQUESTED_QOS_INCOMPATIBLE},
{RMW_EVENT_OFFERED_QOS_INCOMPATIBLE, rmw_zenoh_cpp::ZENOH_EVENT_OFFERED_QOS_INCOMPATIBLE},
{RMW_EVENT_MESSAGE_LOST, rmw_zenoh_cpp::ZENOH_EVENT_MESSAGE_LOST},
{RMW_EVENT_SUBSCRIPTION_MATCHED, rmw_zenoh_cpp::ZENOH_EVENT_SUBSCRIPTION_MATCHED},
{RMW_EVENT_PUBLICATION_MATCHED, rmw_zenoh_cpp::ZENOH_EVENT_PUBLICATION_MATCHED},
{RMW_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE,
rmw_zenoh_cpp::ZENOH_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE},
{RMW_EVENT_PUBLISHER_INCOMPATIBLE_TYPE, rmw_zenoh_cpp::ZENOH_EVENT_PUBLISHER_INCOMPATIBLE_TYPE}
// {RMW_EVENT_SUBSCRIPTION_MATCHED, rmw_zenoh_cpp::ZENOH_EVENT_SUBSCRIPTION_MATCHED},
// {RMW_EVENT_PUBLICATION_MATCHED, rmw_zenoh_cpp::ZENOH_EVENT_PUBLICATION_MATCHED},
// {RMW_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE,
// rmw_zenoh_cpp::ZENOH_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE},
// {RMW_EVENT_PUBLISHER_INCOMPATIBLE_TYPE, rmw_zenoh_cpp::ZENOH_EVENT_PUBLISHER_INCOMPATIBLE_TYPE}
// TODO(clalancette): Implement remaining events
};
} // namespace
Expand Down
26 changes: 13 additions & 13 deletions rmw_zenoh_cpp/src/detail/graph_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "rmw/validate_namespace.h"
#include "rmw/validate_node_name.h"

#include "rosidl_runtime_c/type_hash.h"
// #include "rosidl_runtime_c/type_hash.h"

#include "graph_cache.hpp"
#include "logging_macros.hpp"
Expand Down Expand Up @@ -1148,18 +1148,18 @@ rmw_ret_t GraphCache::get_entities_info_by_topic(
return ret;
}

rosidl_type_hash_t type_hash;
rcutils_ret_t rc_ret = rosidl_parse_type_hash_string(
topic_data->info_.type_hash_.c_str(),
&type_hash);
if (RCUTILS_RET_OK == rc_ret) {
ret = rmw_topic_endpoint_info_set_topic_type_hash(&ep, &type_hash);
if (RMW_RET_OK != ret) {
return ret;
}
}

memset(ep.endpoint_gid, 0, RMW_GID_STORAGE_SIZE);
// rosidl_type_hash_t type_hash;
// rcutils_ret_t rc_ret = rosidl_parse_type_hash_string(
// topic_data->info_.type_hash_.c_str(),
// &type_hash);
// if (RCUTILS_RET_OK == rc_ret) {
// ret = rmw_topic_endpoint_info_set_topic_type_hash(&ep, &type_hash);
// if (RMW_RET_OK != ret) {
// return ret;
// }
// }

memset(ep.endpoint_gid, 0, 16);
entity->copy_gid(ep.endpoint_gid);

endpoints.push_back(ep);
Expand Down
13 changes: 7 additions & 6 deletions rmw_zenoh_cpp/src/detail/liveliness_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ static const std::unordered_map<std::string, rmw_qos_liveliness_policy_e> str_to
{std::to_string(RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC),
RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC},
{std::to_string(RMW_QOS_POLICY_LIVELINESS_UNKNOWN), RMW_QOS_POLICY_LIVELINESS_UNKNOWN},
{std::to_string(RMW_QOS_POLICY_LIVELINESS_BEST_AVAILABLE),
RMW_QOS_POLICY_LIVELINESS_BEST_AVAILABLE}
// {std::to_string(RMW_QOS_POLICY_LIVELINESS_BEST_AVAILABLE),
// RMW_QOS_POLICY_LIVELINESS_BEST_AVAILABLE}
};

std::vector<std::string> split_keyexpr(
Expand Down Expand Up @@ -638,9 +638,9 @@ std::string Entity::liveliness_keyexpr() const
}

///=============================================================================
void Entity::copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const
void Entity::copy_gid(uint8_t out_gid[16]) const
{
memcpy(out_gid, gid_, RMW_GID_STORAGE_SIZE);
memcpy(out_gid, gid_, 16);
}

///=============================================================================
Expand Down Expand Up @@ -679,12 +679,13 @@ std::string demangle_name(const std::string & input)
} // namespace liveliness

///=============================================================================
size_t hash_gid(const uint8_t gid[RMW_GID_STORAGE_SIZE])
size_t hash_gid(const uint8_t gid[16])
// size_t hash_gid(const int8_t gid[16])
{
std::stringstream hash_str;
hash_str << std::hex;
size_t i = 0;
for (; i < (RMW_GID_STORAGE_SIZE - 1); i++) {
for (; i < (16 - 1); i++) {
hash_str << static_cast<int>(gid[i]);
}
return std::hash<std::string>{}(hash_str.str());
Expand Down
6 changes: 3 additions & 3 deletions rmw_zenoh_cpp/src/detail/liveliness_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Entity
// Two entities are equal if their keyexpr_hash are equal.
bool operator==(const Entity & other) const;

void copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const;
void copy_gid(uint8_t out_gid[16]) const;

private:
Entity(
Expand All @@ -191,7 +191,7 @@ class Entity
NodeInfo node_info_;
std::optional<TopicInfo> topic_info_;
std::string liveliness_keyexpr_;
uint8_t gid_[RMW_GID_STORAGE_SIZE];
uint8_t gid_[16];
};

///=============================================================================
Expand Down Expand Up @@ -239,7 +239,7 @@ std::string zid_to_str(const z_id_t & id);

///=============================================================================
/// Generate a hash for a given GID.
size_t hash_gid(const uint8_t gid[RMW_GID_STORAGE_SIZE]);
size_t hash_gid(const uint8_t gid[16]);
} // namespace rmw_zenoh_cpp

///=============================================================================
Expand Down
43 changes: 25 additions & 18 deletions rmw_zenoh_cpp/src/detail/rmw_client_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ std::shared_ptr<ClientData> ClientData::make(
return nullptr;
}

rcutils_allocator_t * allocator = &node->context->options.allocator;
// rcutils_allocator_t * allocator = &node->context->options.allocator;

const rosidl_type_hash_t * type_hash = type_support->get_type_hash_func(type_support);
// const rosidl_type_hash_t * type_hash = type_support->get_type_hash_func(type_support);
auto service_members = static_cast<const service_type_support_callbacks_t *>(type_support->data);
auto request_members = static_cast<const message_type_support_callbacks_t *>(
service_members->request_members_->data);
Expand All @@ -151,19 +151,20 @@ std::shared_ptr<ClientData> ClientData::make(
}

// Convert the type hash to a string so that it can be included in the keyexpr.
char * type_hash_c_str = nullptr;
rcutils_ret_t stringify_ret = rosidl_stringify_type_hash(
type_hash,
*allocator,
&type_hash_c_str);
if (RCUTILS_RET_BAD_ALLOC == stringify_ret) {
RMW_SET_ERROR_MSG("Failed to allocate type_hash_c_str.");
return nullptr;
}
auto free_type_hash_c_str = rcpputils::make_scope_exit(
[&allocator, &type_hash_c_str]() {
allocator->deallocate(type_hash_c_str, allocator->state);
});
const char * type_hash_c_str = "TypeHashNotSupported";
// char * type_hash_c_str = nullptr;
// rcutils_ret_t stringify_ret = rosidl_stringify_type_hash(
// type_hash,
// *allocator,
// &type_hash_c_str);
// if (RCUTILS_RET_BAD_ALLOC == stringify_ret) {
// RMW_SET_ERROR_MSG("Failed to allocate type_hash_c_str.");
// return nullptr;
// }
// auto free_type_hash_c_str = rcpputils::make_scope_exit(
// [&allocator, &type_hash_c_str]() {
// allocator->deallocate(type_hash_c_str, allocator->state);
// });

std::size_t domain_id = node_info.domain_id_;
auto entity = liveliness::Entity::make(
Expand Down Expand Up @@ -296,7 +297,7 @@ bool ClientData::liveliness_is_valid() const
}

///=============================================================================
void ClientData::copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const
void ClientData::copy_gid(uint8_t out_gid[16]) const
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
entity_->copy_gid(out_gid);
Expand Down Expand Up @@ -386,7 +387,13 @@ rmw_ret_t ClientData::take_response(
RMW_SET_ERROR_MSG("Failed to get source_timestamp from client call attachment");
return RMW_RET_ERROR;
}
attachment.copy_gid(request_header->request_id.writer_guid);

// attachment.copy_gid(request_header->request_id.writer_guid);
uint8_t tmp_writer_guid[16];
attachment.copy_gid(tmp_writer_guid);
for (size_t i = 0; i < 16; ++i) {
request_header->request_id.writer_guid[i] = static_cast<int8_t>(tmp_writer_guid[i]);
}
request_header->received_timestamp = latest_reply->get_received_timestamp();

z_drop(z_move(payload));
Expand Down Expand Up @@ -446,7 +453,7 @@ rmw_ret_t ClientData::send_request(
z_get_options_t opts;
z_get_options_default(&opts);
z_owned_bytes_t attachment;
uint8_t local_gid[RMW_GID_STORAGE_SIZE];
uint8_t local_gid[16];
entity_->copy_gid(local_gid);
create_map_and_set_sequence_num(
&attachment, *sequence_id,
Expand Down
2 changes: 1 addition & 1 deletion rmw_zenoh_cpp/src/detail/rmw_client_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ClientData final : public std::enable_shared_from_this<ClientData>
bool liveliness_is_valid() const;

// Copy the GID of this ClientData into an rmw_gid_t.
void copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const;
void copy_gid(uint8_t out_gid[16]) const;

// Add a new ZenohReply to the queue.
void add_new_reply(std::unique_ptr<rmw_zenoh_cpp::ZenohReply> reply);
Expand Down
35 changes: 18 additions & 17 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,26 @@ std::shared_ptr<PublisherData> PublisherData::make(

rcutils_allocator_t * allocator = &node->context->options.allocator;

const rosidl_type_hash_t * type_hash = type_support->get_type_hash_func(type_support);
// const rosidl_type_hash_t * type_hash = type_support->get_type_hash_func(type_support);
auto callbacks = static_cast<const message_type_support_callbacks_t *>(type_support->data);
auto message_type_support = std::make_unique<MessageTypeSupport>(callbacks);

// Convert the type hash to a string so that it can be included in
// the keyexpr.
char * type_hash_c_str = nullptr;
rcutils_ret_t stringify_ret = rosidl_stringify_type_hash(
type_hash,
*allocator,
&type_hash_c_str);
if (RCUTILS_RET_BAD_ALLOC == stringify_ret) {
RMW_SET_ERROR_MSG("Failed to allocate type_hash_c_str.");
return nullptr;
}
auto always_free_type_hash_c_str = rcpputils::make_scope_exit(
[&allocator, &type_hash_c_str]() {
allocator->deallocate(type_hash_c_str, allocator->state);
});
const char * type_hash_c_str = "TypeHashNotSupported";
// char * type_hash_c_str = nullptr;
// rcutils_ret_t stringify_ret = rosidl_stringify_type_hash(
// type_hash,
// *allocator,
// &type_hash_c_str);
// if (RCUTILS_RET_BAD_ALLOC == stringify_ret) {
// RMW_SET_ERROR_MSG("Failed to allocate type_hash_c_str.");
// return nullptr;
// }
// auto always_free_type_hash_c_str = rcpputils::make_scope_exit(
// [&allocator, &type_hash_c_str]() {
// allocator->deallocate(type_hash_c_str, allocator->state);
// });

std::size_t domain_id = node_info.domain_id_;
auto entity = liveliness::Entity::make(
Expand Down Expand Up @@ -309,7 +310,7 @@ rmw_ret_t PublisherData::publish(
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
z_owned_bytes_t attachment;
uint8_t local_gid[RMW_GID_STORAGE_SIZE];
uint8_t local_gid[16];
entity_->copy_gid(local_gid);
create_map_and_set_sequence_num(&attachment, sequence_number_++, local_gid);
options.attachment = z_move(attachment);
Expand Down Expand Up @@ -357,7 +358,7 @@ rmw_ret_t PublisherData::publish_serialized_message(
// will be encoded with CDR so it does not really matter.
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
uint8_t local_gid[RMW_GID_STORAGE_SIZE];
uint8_t local_gid[16];
entity_->copy_gid(local_gid);
z_owned_bytes_t attachment;
create_map_and_set_sequence_num(&attachment, sequence_number_++, local_gid);
Expand Down Expand Up @@ -395,7 +396,7 @@ liveliness::TopicInfo PublisherData::topic_info() const
}

///=============================================================================
void PublisherData::copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const
void PublisherData::copy_gid(uint8_t out_gid[16]) const
{
std::lock_guard<std::mutex> lock(mutex_);
entity_->copy_gid(out_gid);
Expand Down
2 changes: 1 addition & 1 deletion rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PublisherData final
liveliness::TopicInfo topic_info() const;

// Copy the GID of this PublisherData into an rmw_gid_t.
void copy_gid(uint8_t out_gid[RMW_GID_STORAGE_SIZE]) const;
void copy_gid(uint8_t out_gid[16]) const;

// Returns true if liveliness token is still valid.
bool liveliness_is_valid() const;
Expand Down
Loading

0 comments on commit a442bc7

Please sign in to comment.