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.
588 lines
14 KiB
588 lines
14 KiB
10 years ago
|
/****************************************************************************
|
||
|
*
|
||
9 years ago
|
* Copyright (c) 2012-2016 PX4 Development Team. All rights reserved.
|
||
10 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.
|
||
|
*
|
||
|
****************************************************************************/
|
||
|
|
||
7 years ago
|
#include "uORBDeviceNode.hpp"
|
||
9 years ago
|
|
||
10 years ago
|
#include "uORBUtils.hpp"
|
||
10 years ago
|
#include "uORBManager.hpp"
|
||
7 years ago
|
|
||
6 years ago
|
#include "SubscriptionCallback.hpp"
|
||
|
|
||
7 years ago
|
#ifdef ORB_COMMUNICATOR
|
||
10 years ago
|
#include "uORBCommunicator.hpp"
|
||
7 years ago
|
#endif /* ORB_COMMUNICATOR */
|
||
|
|
||
5 years ago
|
static uORB::SubscriptionInterval *filp_to_subscription(cdev::file_t *filp) { return static_cast<uORB::SubscriptionInterval *>(filp->f_priv); }
|
||
|
|
||
4 years ago
|
// Determine the data range
|
||
|
static inline bool is_in_range(unsigned left, unsigned value, unsigned right)
|
||
|
{
|
||
|
if (right > left) {
|
||
|
return (left <= value) && (value <= right);
|
||
|
|
||
|
} else { // Maybe the data overflowed and a wraparound occurred
|
||
|
return (left <= value) || (value <= right);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// round up to nearest power of two
|
||
|
// Such as 0 => 1, 1 => 1, 2 => 2 ,3 => 4, 10 => 16, 60 => 64, 65...255 => 128
|
||
|
// Note: When the input value > 128, the output is always 128
|
||
|
static inline uint8_t round_pow_of_two_8(uint8_t n)
|
||
|
{
|
||
|
if (n == 0) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// Avoid is already a power of 2
|
||
|
uint8_t value = n - 1;
|
||
|
|
||
|
// Fill 1
|
||
|
value |= value >> 1U;
|
||
|
value |= value >> 2U;
|
||
|
value |= value >> 4U;
|
||
|
|
||
|
// Unable to round-up, take the value of round-down
|
||
|
if (value == UINT8_MAX) {
|
||
|
value >>= 1U;
|
||
|
}
|
||
|
|
||
|
return value + 1;
|
||
|
}
|
||
|
|
||
6 years ago
|
uORB::DeviceNode::DeviceNode(const struct orb_metadata *meta, const uint8_t instance, const char *path,
|
||
5 years ago
|
uint8_t queue_size) :
|
||
7 years ago
|
CDev(path),
|
||
10 years ago
|
_meta(meta),
|
||
5 years ago
|
_instance(instance),
|
||
4 years ago
|
_queue_size(round_pow_of_two_8(queue_size))
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
|
uORB::DeviceNode::~DeviceNode()
|
||
|
{
|
||
5 years ago
|
delete[] _data;
|
||
6 years ago
|
|
||
|
CDev::unregister_driver_and_memory();
|
||
10 years ago
|
}
|
||
|
|
||
|
int
|
||
7 years ago
|
uORB::DeviceNode::open(cdev::file_t *filp)
|
||
10 years ago
|
{
|
||
10 years ago
|
/* is this a publisher? */
|
||
7 years ago
|
if (filp->f_oflags == PX4_F_WRONLY) {
|
||
10 years ago
|
|
||
10 years ago
|
lock();
|
||
5 years ago
|
mark_as_advertised();
|
||
10 years ago
|
unlock();
|
||
10 years ago
|
|
||
10 years ago
|
/* now complete the open */
|
||
5 years ago
|
return CDev::open(filp);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/* is this a new subscriber? */
|
||
7 years ago
|
if (filp->f_oflags == PX4_F_RDONLY) {
|
||
10 years ago
|
|
||
10 years ago
|
/* allocate subscriber data */
|
||
5 years ago
|
SubscriptionInterval *sd = new SubscriptionInterval(_meta, 0, _instance);
|
||
10 years ago
|
|
||
10 years ago
|
if (nullptr == sd) {
|
||
|
return -ENOMEM;
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
filp->f_priv = (void *)sd;
|
||
10 years ago
|
|
||
5 years ago
|
int ret = CDev::open(filp);
|
||
10 years ago
|
|
||
9 years ago
|
if (ret != PX4_OK) {
|
||
8 years ago
|
PX4_ERR("CDev::open failed");
|
||
10 years ago
|
delete sd;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
return ret;
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
if (filp->f_oflags == 0) {
|
||
7 years ago
|
return CDev::open(filp);
|
||
|
}
|
||
|
|
||
10 years ago
|
/* can only be pub or sub, not both */
|
||
|
return -EINVAL;
|
||
10 years ago
|
}
|
||
|
|
||
|
int
|
||
7 years ago
|
uORB::DeviceNode::close(cdev::file_t *filp)
|
||
10 years ago
|
{
|
||
5 years ago
|
if (filp->f_oflags == PX4_F_RDONLY) { /* subscriber */
|
||
5 years ago
|
SubscriptionInterval *sd = filp_to_subscription(filp);
|
||
|
delete sd;
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
return CDev::close(filp);
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
bool
|
||
5 years ago
|
uORB::DeviceNode::copy(void *dst, unsigned &generation)
|
||
6 years ago
|
{
|
||
|
if ((dst != nullptr) && (_data != nullptr)) {
|
||
5 years ago
|
if (_queue_size == 1) {
|
||
|
ATOMIC_ENTER;
|
||
|
memcpy(dst, _data, _meta->o_size);
|
||
|
generation = _generation.load();
|
||
|
ATOMIC_LEAVE;
|
||
|
return true;
|
||
6 years ago
|
|
||
5 years ago
|
} else {
|
||
|
ATOMIC_ENTER;
|
||
|
const unsigned current_generation = _generation.load();
|
||
6 years ago
|
|
||
4 years ago
|
if (current_generation == generation) {
|
||
5 years ago
|
/* The subscriber already read the latest message, but nothing new was published yet.
|
||
|
* Return the previous message
|
||
|
*/
|
||
|
--generation;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
// Compatible with normal and overflow conditions
|
||
|
if (!is_in_range(current_generation - _queue_size, generation, current_generation - 1)) {
|
||
|
// Reader is too far behind: some messages are lost
|
||
|
generation = current_generation - _queue_size;
|
||
|
}
|
||
|
|
||
5 years ago
|
memcpy(dst, _data + (_meta->o_size * (generation % _queue_size)), _meta->o_size);
|
||
|
ATOMIC_LEAVE;
|
||
6 years ago
|
|
||
4 years ago
|
++generation;
|
||
6 years ago
|
|
||
5 years ago
|
return true;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
return false;
|
||
6 years ago
|
}
|
||
|
|
||
10 years ago
|
ssize_t
|
||
7 years ago
|
uORB::DeviceNode::read(cdev::file_t *filp, char *buffer, size_t buflen)
|
||
10 years ago
|
{
|
||
10 years ago
|
/* if the caller's buffer is the wrong size, that's an error */
|
||
|
if (buflen != _meta->o_size) {
|
||
|
return -EIO;
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
return filp_to_subscription(filp)->copy(buffer) ? _meta->o_size : 0;
|
||
10 years ago
|
}
|
||
|
|
||
|
ssize_t
|
||
7 years ago
|
uORB::DeviceNode::write(cdev::file_t *filp, const char *buffer, size_t buflen)
|
||
10 years ago
|
{
|
||
10 years ago
|
/*
|
||
|
* Writes are legal from interrupt context as long as the
|
||
|
* object has already been initialised from thread context.
|
||
|
*
|
||
|
* Writes outside interrupt context will allocate the object
|
||
|
* if it has not yet been allocated.
|
||
|
*
|
||
|
* Note that filp will usually be NULL.
|
||
|
*/
|
||
|
if (nullptr == _data) {
|
||
6 years ago
|
|
||
9 years ago
|
#ifdef __PX4_NUTTX
|
||
|
|
||
10 years ago
|
if (!up_interrupt_context()) {
|
||
6 years ago
|
#endif /* __PX4_NUTTX */
|
||
10 years ago
|
|
||
|
lock();
|
||
|
|
||
|
/* re-check size */
|
||
|
if (nullptr == _data) {
|
||
9 years ago
|
_data = new uint8_t[_meta->o_size * _queue_size];
|
||
10 years ago
|
}
|
||
|
|
||
|
unlock();
|
||
|
|
||
6 years ago
|
#ifdef __PX4_NUTTX
|
||
9 years ago
|
}
|
||
|
|
||
6 years ago
|
#endif /* __PX4_NUTTX */
|
||
9 years ago
|
|
||
10 years ago
|
/* failed or could not allocate */
|
||
|
if (nullptr == _data) {
|
||
|
return -ENOMEM;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* If write size does not match, that is an error */
|
||
|
if (_meta->o_size != buflen) {
|
||
|
return -EIO;
|
||
|
}
|
||
|
|
||
|
/* Perform an atomic copy. */
|
||
9 years ago
|
ATOMIC_ENTER;
|
||
5 years ago
|
/* wrap-around happens after ~49 days, assuming a publisher rate of 1 kHz */
|
||
|
unsigned generation = _generation.fetch_add(1);
|
||
|
|
||
|
memcpy(_data + (_meta->o_size * (generation % _queue_size)), buffer, _meta->o_size);
|
||
10 years ago
|
|
||
6 years ago
|
// callbacks
|
||
|
for (auto item : _callbacks) {
|
||
|
item->call();
|
||
|
}
|
||
|
|
||
4 years ago
|
/* Mark at least one data has been published */
|
||
|
_data_valid = true;
|
||
|
|
||
9 years ago
|
ATOMIC_LEAVE;
|
||
9 years ago
|
|
||
10 years ago
|
/* notify any poll waiters */
|
||
|
poll_notify(POLLIN);
|
||
|
|
||
|
return _meta->o_size;
|
||
10 years ago
|
}
|
||
|
|
||
|
int
|
||
7 years ago
|
uORB::DeviceNode::ioctl(cdev::file_t *filp, int cmd, unsigned long arg)
|
||
10 years ago
|
{
|
||
10 years ago
|
switch (cmd) {
|
||
6 years ago
|
case ORBIOCUPDATED: {
|
||
|
ATOMIC_ENTER;
|
||
5 years ago
|
*(bool *)arg = filp_to_subscription(filp)->updated();
|
||
6 years ago
|
ATOMIC_LEAVE;
|
||
|
return PX4_OK;
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
case ORBIOCSETINTERVAL:
|
||
|
filp_to_subscription(filp)->set_interval_us(arg);
|
||
|
return PX4_OK;
|
||
10 years ago
|
|
||
10 years ago
|
case ORBIOCGADVERTISER:
|
||
|
*(uintptr_t *)arg = (uintptr_t)this;
|
||
9 years ago
|
return PX4_OK;
|
||
10 years ago
|
|
||
5 years ago
|
case ORBIOCSETQUEUESIZE: {
|
||
|
lock();
|
||
|
int ret = update_queue_size(arg);
|
||
|
unlock();
|
||
|
return ret;
|
||
|
}
|
||
9 years ago
|
|
||
5 years ago
|
case ORBIOCGETINTERVAL:
|
||
|
*(unsigned *)arg = filp_to_subscription(filp)->get_interval_us();
|
||
|
return PX4_OK;
|
||
9 years ago
|
|
||
5 years ago
|
case ORBIOCISADVERTISED:
|
||
|
*(unsigned long *)arg = _advertised;
|
||
7 years ago
|
|
||
5 years ago
|
return PX4_OK;
|
||
7 years ago
|
|
||
10 years ago
|
default:
|
||
|
/* give it to the superclass */
|
||
8 years ago
|
return CDev::ioctl(filp, cmd, arg);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
ssize_t
|
||
9 years ago
|
uORB::DeviceNode::publish(const orb_metadata *meta, orb_advert_t handle, const void *data)
|
||
10 years ago
|
{
|
||
9 years ago
|
uORB::DeviceNode *devnode = (uORB::DeviceNode *)handle;
|
||
10 years ago
|
int ret;
|
||
|
|
||
7 years ago
|
/* check if the device handle is initialized and data is valid */
|
||
|
if ((devnode == nullptr) || (meta == nullptr) || (data == nullptr)) {
|
||
8 years ago
|
errno = EFAULT;
|
||
7 years ago
|
return PX4_ERROR;
|
||
8 years ago
|
}
|
||
|
|
||
|
/* check if the orb meta data matches the publication */
|
||
9 years ago
|
if (devnode->_meta != meta) {
|
||
10 years ago
|
errno = EINVAL;
|
||
7 years ago
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
/* call the devnode write method with no file pointer */
|
||
|
ret = devnode->write(nullptr, (const char *)data, meta->o_size);
|
||
10 years ago
|
|
||
|
if (ret < 0) {
|
||
8 years ago
|
errno = -ret;
|
||
7 years ago
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
|
||
|
if (ret != (int)meta->o_size) {
|
||
|
errno = EIO;
|
||
7 years ago
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
#ifdef ORB_COMMUNICATOR
|
||
10 years ago
|
/*
|
||
|
* if the write is successful, send the data over the Multi-ORB link
|
||
|
*/
|
||
|
uORBCommunicator::IChannel *ch = uORB::Manager::get_instance()->get_uorb_communicator();
|
||
|
|
||
|
if (ch != nullptr) {
|
||
|
if (ch->send_message(meta->o_name, meta->o_size, (uint8_t *)data) != 0) {
|
||
7 years ago
|
PX4_ERR("Error Sending [%s] topic data over comm_channel", meta->o_name);
|
||
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
#endif /* ORB_COMMUNICATOR */
|
||
|
|
||
9 years ago
|
return PX4_OK;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
int uORB::DeviceNode::unadvertise(orb_advert_t handle)
|
||
|
{
|
||
|
if (handle == nullptr) {
|
||
|
return -EINVAL;
|
||
|
}
|
||
|
|
||
|
uORB::DeviceNode *devnode = (uORB::DeviceNode *)handle;
|
||
|
|
||
|
/*
|
||
|
* We are cheating a bit here. First, with the current implementation, we can only
|
||
|
* have multiple publishers for instance 0. In this case the caller will have
|
||
|
* instance=nullptr and _published has no effect at all. Thus no unadvertise is
|
||
|
* necessary.
|
||
|
* In case of multiple instances, we have at most 1 publisher per instance and
|
||
|
* we can signal an instance as 'free' by setting _published to false.
|
||
|
* We never really free the DeviceNode, for this we would need reference counting
|
||
|
* of subscribers and publishers. But we also do not have a leak since future
|
||
|
* publishers reuse the same DeviceNode object.
|
||
|
*/
|
||
5 years ago
|
devnode->_advertised = false;
|
||
9 years ago
|
|
||
|
return PX4_OK;
|
||
|
}
|
||
|
|
||
7 years ago
|
#ifdef ORB_COMMUNICATOR
|
||
5 years ago
|
int16_t uORB::DeviceNode::topic_advertised(const orb_metadata *meta)
|
||
9 years ago
|
{
|
||
|
uORBCommunicator::IChannel *ch = uORB::Manager::get_instance()->get_uorb_communicator();
|
||
8 years ago
|
|
||
9 years ago
|
if (ch != nullptr && meta != nullptr) {
|
||
|
return ch->topic_advertised(meta->o_name);
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
return -1;
|
||
|
}
|
||
7 years ago
|
|
||
9 years ago
|
/*
|
||
|
//TODO: Check if we need this since we only unadvertise when things all shutdown and it doesn't actually remove the device
|
||
5 years ago
|
int16_t uORB::DeviceNode::topic_unadvertised(const orb_metadata *meta)
|
||
9 years ago
|
{
|
||
|
uORBCommunicator::IChannel *ch = uORB::Manager::get_instance()->get_uorb_communicator();
|
||
|
if (ch != nullptr && meta != nullptr) {
|
||
|
return ch->topic_unadvertised(meta->o_name);
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
*/
|
||
7 years ago
|
#endif /* ORB_COMMUNICATOR */
|
||
9 years ago
|
|
||
5 years ago
|
px4_pollevent_t
|
||
7 years ago
|
uORB::DeviceNode::poll_state(cdev::file_t *filp)
|
||
10 years ago
|
{
|
||
5 years ago
|
// If the topic appears updated to the subscriber, say so.
|
||
5 years ago
|
return filp_to_subscription(filp)->updated() ? POLLIN : 0;
|
||
10 years ago
|
}
|
||
|
|
||
|
void
|
||
5 years ago
|
uORB::DeviceNode::poll_notify_one(px4_pollfd_struct_t *fds, px4_pollevent_t events)
|
||
10 years ago
|
{
|
||
5 years ago
|
// If the topic looks updated to the subscriber, go ahead and notify them.
|
||
5 years ago
|
if (filp_to_subscription((cdev::file_t *)fds->priv)->updated()) {
|
||
8 years ago
|
CDev::poll_notify_one(fds, events);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
bool
|
||
5 years ago
|
uORB::DeviceNode::print_statistics(int max_topic_length)
|
||
9 years ago
|
{
|
||
5 years ago
|
if (!_advertised) {
|
||
9 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
lock();
|
||
|
|
||
5 years ago
|
const uint8_t instance = get_instance();
|
||
|
const int8_t sub_count = subscriber_count();
|
||
|
const uint8_t queue_size = get_queue_size();
|
||
9 years ago
|
|
||
|
unlock();
|
||
|
|
||
5 years ago
|
PX4_INFO_RAW("%-*s %2i %4i %2i %4i %s\n", max_topic_length, get_meta()->o_name, (int)instance, (int)sub_count,
|
||
|
queue_size, get_meta()->o_size, get_devname());
|
||
5 years ago
|
|
||
9 years ago
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
void uORB::DeviceNode::add_internal_subscriber()
|
||
|
{
|
||
9 years ago
|
lock();
|
||
|
_subscriber_count++;
|
||
|
|
||
7 years ago
|
#ifdef ORB_COMMUNICATOR
|
||
|
uORBCommunicator::IChannel *ch = uORB::Manager::get_instance()->get_uorb_communicator();
|
||
|
|
||
10 years ago
|
if (ch != nullptr && _subscriber_count > 0) {
|
||
9 years ago
|
unlock(); //make sure we cannot deadlock if add_subscription calls back into DeviceNode
|
||
10 years ago
|
ch->add_subscription(_meta->o_name, 1);
|
||
9 years ago
|
|
||
7 years ago
|
} else
|
||
|
#endif /* ORB_COMMUNICATOR */
|
||
|
|
||
|
{
|
||
9 years ago
|
unlock();
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void uORB::DeviceNode::remove_internal_subscriber()
|
||
|
{
|
||
9 years ago
|
lock();
|
||
|
_subscriber_count--;
|
||
|
|
||
7 years ago
|
#ifdef ORB_COMMUNICATOR
|
||
|
uORBCommunicator::IChannel *ch = uORB::Manager::get_instance()->get_uorb_communicator();
|
||
|
|
||
10 years ago
|
if (ch != nullptr && _subscriber_count == 0) {
|
||
9 years ago
|
unlock(); //make sure we cannot deadlock if remove_subscription calls back into DeviceNode
|
||
10 years ago
|
ch->remove_subscription(_meta->o_name);
|
||
9 years ago
|
|
||
7 years ago
|
} else
|
||
|
#endif /* ORB_COMMUNICATOR */
|
||
|
{
|
||
9 years ago
|
unlock();
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
#ifdef ORB_COMMUNICATOR
|
||
10 years ago
|
int16_t uORB::DeviceNode::process_add_subscription(int32_t rateInHz)
|
||
10 years ago
|
{
|
||
10 years ago
|
// if there is already data in the node, send this out to
|
||
|
// the remote entity.
|
||
|
// send the data to the remote entity.
|
||
|
uORBCommunicator::IChannel *ch = uORB::Manager::get_instance()->get_uorb_communicator();
|
||
|
|
||
|
if (_data != nullptr && ch != nullptr) { // _data will not be null if there is a publisher.
|
||
|
ch->send_message(_meta->o_name, _meta->o_size, _data);
|
||
|
}
|
||
|
|
||
9 years ago
|
return PX4_OK;
|
||
10 years ago
|
}
|
||
|
|
||
|
int16_t uORB::DeviceNode::process_remove_subscription()
|
||
|
{
|
||
9 years ago
|
return PX4_OK;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
int16_t uORB::DeviceNode::process_received_message(int32_t length, uint8_t *data)
|
||
10 years ago
|
{
|
||
10 years ago
|
int16_t ret = -1;
|
||
|
|
||
|
if (length != (int32_t)(_meta->o_size)) {
|
||
6 years ago
|
PX4_ERR("Received '%s' with DataLength[%d] != ExpectedLen[%d]", _meta->o_name, (int)length, (int)_meta->o_size);
|
||
7 years ago
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
|
||
|
/* call the devnode write method with no file pointer */
|
||
|
ret = write(nullptr, (const char *)data, _meta->o_size);
|
||
|
|
||
|
if (ret < 0) {
|
||
7 years ago
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
|
||
|
if (ret != (int)_meta->o_size) {
|
||
|
errno = EIO;
|
||
7 years ago
|
return PX4_ERROR;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
return PX4_OK;
|
||
10 years ago
|
}
|
||
7 years ago
|
#endif /* ORB_COMMUNICATOR */
|
||
|
|
||
|
int uORB::DeviceNode::update_queue_size(unsigned int queue_size)
|
||
|
{
|
||
|
if (_queue_size == queue_size) {
|
||
|
return PX4_OK;
|
||
|
}
|
||
|
|
||
|
//queue size is limited to 255 for the single reason that we use uint8 to store it
|
||
|
if (_data || _queue_size > queue_size || queue_size > 255) {
|
||
|
return PX4_ERROR;
|
||
|
}
|
||
|
|
||
4 years ago
|
_queue_size = round_pow_of_two_8(queue_size);
|
||
7 years ago
|
return PX4_OK;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
unsigned uORB::DeviceNode::get_initial_generation()
|
||
|
{
|
||
|
ATOMIC_ENTER;
|
||
|
|
||
|
// If there any previous publications allow the subscriber to read them
|
||
|
unsigned generation = _generation.load() - (_data_valid ? 1 : 0);
|
||
|
|
||
|
ATOMIC_LEAVE;
|
||
|
|
||
|
return generation;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool
|
||
|
uORB::DeviceNode::register_callback(uORB::SubscriptionCallback *callback_sub)
|
||
|
{
|
||
|
if (callback_sub != nullptr) {
|
||
|
ATOMIC_ENTER;
|
||
|
|
||
|
// prevent duplicate registrations
|
||
|
for (auto existing_callbacks : _callbacks) {
|
||
|
if (callback_sub == existing_callbacks) {
|
||
|
ATOMIC_LEAVE;
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_callbacks.add(callback_sub);
|
||
|
ATOMIC_LEAVE;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
uORB::DeviceNode::unregister_callback(uORB::SubscriptionCallback *callback_sub)
|
||
|
{
|
||
|
ATOMIC_ENTER;
|
||
|
_callbacks.remove(callback_sub);
|
||
|
ATOMIC_LEAVE;
|
||
|
}
|