diff --git a/msg/templates/uorb/msg.h.em b/msg/templates/uorb/msg.h.em index 0180ebcf0d..9d82de50f4 100644 --- a/msg/templates/uorb/msg.h.em +++ b/msg/templates/uorb/msg.h.em @@ -70,6 +70,10 @@ topic_name = spec.short_name #include +#ifdef __cplusplus +#include +#endif + @############################## @# Includes for dependencies @############################## @@ -135,5 +139,11 @@ ORB_DECLARE(@multi_topic); @[end for] #ifdef __cplusplus +@[for multi_topic in topics]@ +template<> struct ORBTypeMap { + using type = @(uorb_struct); +}; +@[end for] + void print_message(const @uorb_struct& message); #endif diff --git a/msg/templates/uorb/uORBTopics.hpp.em b/msg/templates/uorb/uORBTopics.hpp.em index d1d33f4816..d4bf191999 100644 --- a/msg/templates/uorb/uORBTopics.hpp.em +++ b/msg/templates/uorb/uORBTopics.hpp.em @@ -73,4 +73,6 @@ enum class ORB_ID : uint8_t { INVALID }; +template struct ORBTypeMap; + const struct orb_metadata *get_orb_meta(ORB_ID id); diff --git a/src/modules/uORB/Publication2.hpp b/src/modules/uORB/Publication2.hpp new file mode 100644 index 0000000000..8be6407f89 --- /dev/null +++ b/src/modules/uORB/Publication2.hpp @@ -0,0 +1,108 @@ +/**************************************************************************** + * + * Copyright (c) 2020 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file Publication2.hpp + * + */ + +//#warning uORB::Publication2 is under development and not intended for regular usage + +#pragma once + +#include +#include + +#include +#include "uORBDeviceNode.hpp" +#include + +namespace uORB +{ + +class Publication2Base +{ +public: + bool advertised() const { return _handle != nullptr; } + bool unadvertise() { return (DeviceNode::unadvertise(_handle) == PX4_OK); } + +protected: + Publication2Base() = default; + ~Publication2Base() + { + if (_handle != nullptr) { + // don't automatically unadvertise queued publications (eg vehicle_command) + if (static_cast(_handle)->get_queue_size() == 1) { + unadvertise(); + } + } + } + + orb_advert_t _handle{nullptr}; +}; + +/** + * uORB publication wrapper class + */ +template +class Publication2 : public Publication2Base +{ +public: + Publication2() = default; + + bool advertise() + { + if (!advertised()) { + _handle = orb_advertise_queue(get_orb_meta(T), nullptr, ORB_QSIZE); + } + + return advertised(); + } + + using S = typename ORBTypeMap::type; + + /** + * Publish the struct + * @param data The uORB message struct we are updating. + */ + bool publish(const S &data) + { + if (!advertised()) { + advertise(); + } + + return (DeviceNode::publish(get_orb_meta(T), _handle, &data) == PX4_OK); + } +}; + +} // namespace uORB