From 1ea52802996041c5a8e98c0b47f9056e9fcb1b11 Mon Sep 17 00:00:00 2001 From: TSC21 Date: Mon, 16 Mar 2020 14:44:03 +0000 Subject: [PATCH] microRTPS: use package.version to compare FastRTPS lib versions --- msg/templates/urtps/Publisher.cpp.em | 6 +++--- msg/templates/urtps/Publisher.h.em | 5 +++-- msg/templates/urtps/RtpsTopics.h.em | 7 ++++--- msg/templates/urtps/Subscriber.cpp.em | 3 ++- msg/templates/urtps/Subscriber.h.em | 5 +++-- msg/templates/urtps/microRTPS_timesync.h.em | 7 ++++--- msg/templates/urtps/msg.idl.em | 15 +++++++++------ msg/tools/generate_microRTPS_bridge.py | 10 +++------- msg/tools/px_generate_uorb_topic_files.py | 3 ++- 9 files changed, 33 insertions(+), 28 deletions(-) diff --git a/msg/templates/urtps/Publisher.cpp.em b/msg/templates/urtps/Publisher.cpp.em index 78fb9c00dd..d63d4049b6 100644 --- a/msg/templates/urtps/Publisher.cpp.em +++ b/msg/templates/urtps/Publisher.cpp.em @@ -11,6 +11,7 @@ @# - ids (List) list of all RTPS msg ids @############################################### @{ +from packaging import version import genmsg.msgs from px_generate_uorb_topic_helper import * # this is in Tools/ @@ -69,13 +70,12 @@ except AttributeError: #include -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ #include @[end if]@ #include "@(topic)_Publisher.h" - @(topic)_Publisher::@(topic)_Publisher() : mp_participant(nullptr), mp_publisher(nullptr) @@ -91,7 +91,7 @@ bool @(topic)_Publisher::init() // Create RTPSParticipant ParticipantAttributes PParam; PParam.rtps.builtin.domainId = 0; -@[if fastrtps_version <= 1.8]@ +@[if version.parse(fastrtps_version) <= version.parse('1.8')]@ PParam.rtps.builtin.leaseDuration = c_TimeInfinite; @[else]@ PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite; diff --git a/msg/templates/urtps/Publisher.h.em b/msg/templates/urtps/Publisher.h.em index 29b22266b6..ab88424204 100644 --- a/msg/templates/urtps/Publisher.h.em +++ b/msg/templates/urtps/Publisher.h.em @@ -11,6 +11,7 @@ @# - ids (List) list of all RTPS msg ids @############################################### @{ +from packaging import version import genmsg.msgs from px_generate_uorb_topic_helper import * # this is in Tools/ @@ -68,7 +69,7 @@ except AttributeError: #include #include -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ #include "@(topic)_PubSubTypes.h" @[else]@ #include "@(topic)PubSubTypes.h" @@ -77,7 +78,7 @@ except AttributeError: using namespace eprosima::fastrtps; using namespace eprosima::fastrtps::rtps; -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ @[ if ros2_distro]@ using @(topic)_msg_t = @(package)::msg::dds_::@(topic)_; using @(topic)_msg_datatype = @(package)::msg::dds_::@(topic)_PubSubType; diff --git a/msg/templates/urtps/RtpsTopics.h.em b/msg/templates/urtps/RtpsTopics.h.em index 7eaa8520c5..465e5e91f1 100644 --- a/msg/templates/urtps/RtpsTopics.h.em +++ b/msg/templates/urtps/RtpsTopics.h.em @@ -11,6 +11,7 @@ @############################################### @{ import os +from packaging import version import genmsg.msgs @@ -74,7 +75,7 @@ except AttributeError: @[for topic in (recv_topics + send_topics)]@ -@[ if fastrtps_version <= 1.7]@ +@[ if version.parse(fastrtps_version) <= version.parse('1.7')]@ @[ if ros2_distro]@ using @(topic)_msg_t = @(package)::msg::dds_::@(topic)_; @[ else]@ @@ -116,7 +117,7 @@ private: @[end if]@ /** Msg metada Getters **/ -@[if fastrtps_version <= 1.7 or not ros2_distro]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7') or not ros2_distro]@ template inline uint64_t getMsgTimestamp(const T* msg) { return msg->timestamp_(); } @@ -137,7 +138,7 @@ private: @[end if]@ /** Msg metadata Setters **/ -@[if fastrtps_version <= 1.7 or not ros2_distro]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7') or not ros2_distro]@ template inline uint64_t setMsgTimestamp(T* msg, const uint64_t& timestamp) { msg->timestamp_() = timestamp; } diff --git a/msg/templates/urtps/Subscriber.cpp.em b/msg/templates/urtps/Subscriber.cpp.em index 40178eb7f6..8583379fc5 100644 --- a/msg/templates/urtps/Subscriber.cpp.em +++ b/msg/templates/urtps/Subscriber.cpp.em @@ -11,6 +11,7 @@ @# - ids (List) list of all RTPS msg ids @############################################### @{ +from packaging import version import genmsg.msgs from px_generate_uorb_topic_helper import * # this is in Tools/ @@ -90,7 +91,7 @@ bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable* t_send // Create RTPSParticipant ParticipantAttributes PParam; PParam.rtps.builtin.domainId = 0; // MUST BE THE SAME AS IN THE PUBLISHER -@[if fastrtps_version <= 1.8]@ +@[if version.parse(fastrtps_version) <= version.parse('1.8')]@ PParam.rtps.builtin.leaseDuration = c_TimeInfinite; @[else]@ PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite; diff --git a/msg/templates/urtps/Subscriber.h.em b/msg/templates/urtps/Subscriber.h.em index b83a6f5729..aab2ab3913 100644 --- a/msg/templates/urtps/Subscriber.h.em +++ b/msg/templates/urtps/Subscriber.h.em @@ -11,6 +11,7 @@ @# - ids (List) list of all RTPS msg ids @############################################### @{ +from packaging import version import genmsg.msgs from px_generate_uorb_topic_helper import * # this is in Tools/ @@ -68,7 +69,7 @@ except AttributeError: #include #include #include -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ #include "@(topic)_PubSubTypes.h" @[else]@ #include "@(topic)PubSubTypes.h" @@ -81,7 +82,7 @@ except AttributeError: using namespace eprosima::fastrtps; using namespace eprosima::fastrtps::rtps; -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ @[ if ros2_distro]@ using @(topic)_msg_t = @(package)::msg::dds_::@(topic)_; using @(topic)_msg_datatype = @(package)::msg::dds_::@(topic)_PubSubType; diff --git a/msg/templates/urtps/microRTPS_timesync.h.em b/msg/templates/urtps/microRTPS_timesync.h.em index c37f2289cf..7a506b15b6 100644 --- a/msg/templates/urtps/microRTPS_timesync.h.em +++ b/msg/templates/urtps/microRTPS_timesync.h.em @@ -11,6 +11,7 @@ @# - ids (List) list of all RTPS msg ids @############################################### @{ +from packaging import version import genmsg.msgs from px_generate_uorb_topic_helper import * # this is in Tools/ @@ -86,7 +87,7 @@ static constexpr int64_t TRIGGER_RESET_THRESHOLD_NS = 100ll * 1000ll * 1000ll; static constexpr int REQUEST_RESET_COUNTER_THRESHOLD = 5; @# Sets the timesync DDS type according to the FastRTPS and ROS2 version -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ @[ if ros2_distro]@ using timesync_msg_t = @(package)::msg::dds_::Timesync_; @[ else]@ @@ -213,7 +214,7 @@ private: inline void updateOffset(const uint64_t& offset) { _offset_ns.store(offset, std::memory_order_relaxed); } /** Timesync msg Getters **/ -@[if fastrtps_version <= 1.7 or not ros2_distro]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7') or not ros2_distro]@ inline uint64_t getMsgTimestamp(const timesync_msg_t* msg) { return msg->timestamp_(); } inline uint8_t getMsgSysID(const timesync_msg_t* msg) { return msg->sys_id_(); } inline uint8_t getMsgSeq(const timesync_msg_t* msg) { return msg->seq_(); } @@ -228,7 +229,7 @@ private: @[end if]@ /** Timesync msg Setters **/ -@[if fastrtps_version <= 1.7 or not ros2_distro]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7') or not ros2_distro]@ inline uint64_t setMsgTimestamp(timesync_msg_t* msg, const uint64_t& timestamp) { msg->timestamp_() = timestamp; } inline uint8_t setMsgSysID(timesync_msg_t* msg, const uint8_t& sys_id) { msg->sys_id_() = sys_id; } inline uint8_t setMsgSeq(timesync_msg_t* msg, const uint8_t& seq) { msg->seq_() = seq; } diff --git a/msg/templates/urtps/msg.idl.em b/msg/templates/urtps/msg.idl.em index 269ea0a87d..9bd7a70255 100644 --- a/msg/templates/urtps/msg.idl.em +++ b/msg/templates/urtps/msg.idl.em @@ -37,6 +37,7 @@ @# @################################################################################ @{ +from packaging import version import genmsg.msgs from px_generate_uorb_topic_helper import * # this is in Tools/ @@ -57,7 +58,7 @@ def get_include_directives(spec): if genmsg.msgs.is_valid_constant_type(genmsg.msgs.bare_msg_type(field.type)): continue builtin_type = str(field.base_type).replace('px4/', '') - if fastrtps_version <= 1.7: + if version.parse(fastrtps_version) <= version.parse('1.7'): include_directive = '#include "%s_.idl"' % builtin_type else: include_directive = '#include "%s.idl"' % builtin_type @@ -77,12 +78,12 @@ def get_idl_type_name(field_type): def add_msg_field(field): if (not field.is_header): if field.is_array: - if fastrtps_version <= 1.7: + if version.parse(fastrtps_version) <= version.parse('1.7'): print(' {0}__{1}_array_{2} {3}_;'.format(topic, str(get_idl_type_name(field.base_type)).replace(" ", "_"), str(field.array_len), field.name)) else: print(' {0}__{1}_array_{2} {3};'.format(topic, str(get_idl_type_name(field.base_type)).replace(" ", "_"), str(field.array_len), field.name)) else: - if fastrtps_version <= 1.7: + if version.parse(fastrtps_version) <= version.parse('1.7'): base_type = get_idl_type_name(field.base_type) + "_" if get_idl_type_name(field.base_type) in builtin_types else get_idl_type_name(field.base_type) else: base_type = get_idl_type_name(field.base_type) if get_idl_type_name(field.base_type) in builtin_types else get_idl_type_name(field.base_type) @@ -96,7 +97,7 @@ def add_msg_fields(): def add_array_typedefs(): for field in spec.parsed_fields(): if not field.is_header and field.is_array: - if fastrtps_version <= 1.7: + if version.parse(fastrtps_version) <= version.parse('1.7'): base_type = get_idl_type_name(field.base_type) + "_" if get_idl_type_name(field.base_type) in builtin_types else get_idl_type_name(field.base_type) else: base_type = get_idl_type_name(field.base_type) if get_idl_type_name(field.base_type) in builtin_types else get_idl_type_name(field.base_type) @@ -123,18 +124,20 @@ def add_msg_constants(): @[for line in get_include_directives(spec)]@ @(line)@ @[end for]@ + + @# Constants @add_msg_constants() @# Array types @add_array_typedefs() -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ struct @(topic)_ @[else]@ struct @(topic) @[end if]@ { @add_msg_fields() -@[if fastrtps_version <= 1.7]@ +@[if version.parse(fastrtps_version) <= version.parse('1.7')]@ }; // struct @(topic)_ #pragma keylist @(topic)_ diff --git a/msg/tools/generate_microRTPS_bridge.py b/msg/tools/generate_microRTPS_bridge.py index 923e0b9bea..fea35bfd4d 100644 --- a/msg/tools/generate_microRTPS_bridge.py +++ b/msg/tools/generate_microRTPS_bridge.py @@ -227,13 +227,9 @@ if fastrtpsgen_include is not None and fastrtpsgen_include != '': os.path.abspath( args.fastrtpsgen_include) + " " -# get FastRTPS version (major.minor, since patch is not relevant at this stage) -fastrtps_version = "" -try: - fastrtps_version = float(subprocess.check_output( - "ldconfig -v | grep libfastrtps | tail -c 6", shell=True).decode("utf-8").strip()[-5:-2]) -except ValueError: - print("No valid version found to FasRTPS. Make sure it is installed.") +# get FastRTPS version +fastrtps_version = subprocess.check_output( + "ldconfig -v | grep libfastrtps", shell=True).decode("utf-8").strip().split('so.')[-1] # get ROS 2 version, if exists ros2_distro = "" diff --git a/msg/tools/px_generate_uorb_topic_files.py b/msg/tools/px_generate_uorb_topic_files.py index 9fea531626..5a0a451607 100755 --- a/msg/tools/px_generate_uorb_topic_files.py +++ b/msg/tools/px_generate_uorb_topic_files.py @@ -43,6 +43,7 @@ import shutil import filecmp import argparse import sys +from packaging import version try: import em @@ -187,7 +188,7 @@ def generate_idl_file(filename_msg, msg_dir, alias, outputdir, templatedir, pack os.makedirs(outputdir) template_file = os.path.join(templatedir, IDL_TEMPLATE_FILE) - if 1.5 <= fastrtps_version <= 1.7: + if version.parse(fastrtps_version) <= version.parse('1.7'): output_file = os.path.join(outputdir, IDL_TEMPLATE_FILE.replace( "msg.idl.em", str(spec_short_name + "_.idl"))) else: