You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.3 KiB
167 lines
4.3 KiB
12 years ago
|
/****************************************************************************
|
||
|
*
|
||
6 years ago
|
* Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
|
||
12 years ago
|
*
|
||
|
* 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.
|
||
|
*
|
||
|
****************************************************************************/
|
||
|
|
||
|
/**
|
||
8 years ago
|
* @file Publication.hpp
|
||
12 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
5 years ago
|
#include <px4_platform_common/defines.h>
|
||
9 years ago
|
#include <systemlib/err.h>
|
||
5 years ago
|
|
||
6 years ago
|
#include <uORB/uORB.h>
|
||
5 years ago
|
#include "uORBDeviceNode.hpp"
|
||
|
#include <uORB/topics/uORBTopics.hpp>
|
||
12 years ago
|
|
||
11 years ago
|
namespace uORB
|
||
12 years ago
|
{
|
||
|
|
||
4 years ago
|
template <typename U> class DefaultQueueSize
|
||
|
{
|
||
|
private:
|
||
|
template <typename T>
|
||
|
static constexpr uint8_t get_queue_size(decltype(T::ORB_QUEUE_LENGTH) *)
|
||
|
{
|
||
|
return T::ORB_QUEUE_LENGTH;
|
||
|
}
|
||
|
|
||
|
template <typename T> static constexpr uint8_t get_queue_size(...)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
static constexpr unsigned value = get_queue_size<U>(nullptr);
|
||
|
};
|
||
|
|
||
5 years ago
|
class PublicationBase
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
bool advertised() const { return _handle != nullptr; }
|
||
|
|
||
|
bool unadvertise() { return (DeviceNode::unadvertise(_handle) == PX4_OK); }
|
||
|
|
||
|
orb_id_t get_topic() const { return get_orb_meta(_orb_id); }
|
||
|
|
||
|
protected:
|
||
|
|
||
|
PublicationBase(ORB_ID id) : _orb_id(id) {}
|
||
|
|
||
|
~PublicationBase()
|
||
|
{
|
||
|
if (_handle != nullptr) {
|
||
|
// don't automatically unadvertise queued publications (eg vehicle_command)
|
||
|
if (static_cast<DeviceNode *>(_handle)->get_queue_size() == 1) {
|
||
|
unadvertise();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
orb_advert_t _handle{nullptr};
|
||
|
const ORB_ID _orb_id;
|
||
|
};
|
||
|
|
||
12 years ago
|
/**
|
||
5 years ago
|
* uORB publication wrapper class
|
||
12 years ago
|
*/
|
||
4 years ago
|
template<typename T, uint8_t ORB_QSIZE = DefaultQueueSize<T>::value>
|
||
5 years ago
|
class Publication : public PublicationBase
|
||
12 years ago
|
{
|
||
|
public:
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Constructor
|
||
|
*
|
||
6 years ago
|
* @param meta The uORB metadata (usually from the ORB_ID() macro) for the topic.
|
||
11 years ago
|
*/
|
||
5 years ago
|
Publication(ORB_ID id) : PublicationBase(id) {}
|
||
|
Publication(const orb_metadata *meta) : PublicationBase(static_cast<ORB_ID>(meta->o_id)) {}
|
||
|
|
||
|
bool advertise()
|
||
|
{
|
||
|
if (!advertised()) {
|
||
|
_handle = orb_advertise_queue(get_topic(), nullptr, ORB_QSIZE);
|
||
|
}
|
||
|
|
||
|
return advertised();
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
6 years ago
|
* Publish the struct
|
||
11 years ago
|
* @param data The uORB message struct we are updating.
|
||
|
*/
|
||
6 years ago
|
bool publish(const T &data)
|
||
|
{
|
||
5 years ago
|
if (!advertised()) {
|
||
|
advertise();
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
return (DeviceNode::publish(get_topic(), _handle, &data) == PX4_OK);
|
||
6 years ago
|
}
|
||
12 years ago
|
};
|
||
|
|
||
11 years ago
|
/**
|
||
6 years ago
|
* The publication class with data embedded.
|
||
11 years ago
|
*/
|
||
6 years ago
|
template<typename T>
|
||
|
class PublicationData : public Publication<T>
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
/**
|
||
|
* Constructor
|
||
|
*
|
||
6 years ago
|
* @param meta The uORB metadata (usually from the ORB_ID() macro) for the topic.
|
||
11 years ago
|
*/
|
||
5 years ago
|
PublicationData(ORB_ID id) : Publication<T>(id) {}
|
||
6 years ago
|
PublicationData(const orb_metadata *meta) : Publication<T>(meta) {}
|
||
11 years ago
|
|
||
6 years ago
|
T &get() { return _data; }
|
||
|
void set(const T &data) { _data = data; }
|
||
8 years ago
|
|
||
6 years ago
|
// Publishes the embedded struct.
|
||
|
bool update() { return Publication<T>::publish(_data); }
|
||
|
bool update(const T &data)
|
||
6 years ago
|
{
|
||
|
_data = data;
|
||
6 years ago
|
return Publication<T>::publish(_data);
|
||
6 years ago
|
}
|
||
|
|
||
9 years ago
|
private:
|
||
6 years ago
|
T _data{};
|
||
12 years ago
|
};
|
||
|
|
||
11 years ago
|
} // namespace uORB
|