mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-20 15:23:33 +08:00
Meaningful error codes instead of plain -1
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uavcan/error.hpp>
|
||||
#include <uavcan/node/abstract_node.hpp>
|
||||
#include <uavcan/data_type.hpp>
|
||||
#include <uavcan/node/global_data_type_registry.hpp>
|
||||
@@ -35,11 +36,11 @@ class GenericPublisher
|
||||
INode& node_;
|
||||
LazyConstructor<TransferSender> sender_;
|
||||
|
||||
bool checkInit()
|
||||
int checkInit()
|
||||
{
|
||||
if (sender_)
|
||||
{
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
GlobalDataTypeRegistry::instance().freeze();
|
||||
@@ -50,11 +51,11 @@ class GenericPublisher
|
||||
if (!descr)
|
||||
{
|
||||
UAVCAN_TRACE("GenericPublisher", "Type [%s] is not registered", DataSpec::getDataTypeFullName());
|
||||
return false;
|
||||
return -ErrUnknownDataType;
|
||||
}
|
||||
sender_.template construct<Dispatcher&, const DataTypeDescriptor&, CanTxQueue::Qos, MonotonicDuration>
|
||||
(node_.getDispatcher(), *descr, CanTxQueue::Qos(Qos), max_transfer_interval_);
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
MonotonicTime getTxDeadline() const { return node_.getMonotonicTime() + tx_timeout_; }
|
||||
@@ -67,15 +68,16 @@ class GenericPublisher
|
||||
int genericPublish(const DataStruct& message, TransferType transfer_type, NodeID dst_node_id,
|
||||
TransferID* tid, MonotonicTime blocking_deadline)
|
||||
{
|
||||
if (!checkInit())
|
||||
const int res = checkInit();
|
||||
if (res < 0)
|
||||
{
|
||||
return -1;
|
||||
return res;
|
||||
}
|
||||
|
||||
IMarshalBuffer* const buf = getBuffer();
|
||||
if (!buf)
|
||||
{
|
||||
return -1;
|
||||
return -ErrMemory;
|
||||
}
|
||||
|
||||
{
|
||||
@@ -85,7 +87,7 @@ class GenericPublisher
|
||||
if (encode_res <= 0)
|
||||
{
|
||||
assert(0); // Impossible, internal error
|
||||
return -1;
|
||||
return -ErrInvalidMarshalData;
|
||||
}
|
||||
}
|
||||
if (tid)
|
||||
@@ -117,7 +119,7 @@ public:
|
||||
|
||||
int init()
|
||||
{
|
||||
return checkInit() ? 0 : -1;
|
||||
return checkInit();
|
||||
}
|
||||
|
||||
int publish(const DataStruct& message, TransferType transfer_type, NodeID dst_node_id,
|
||||
@@ -134,7 +136,7 @@ public:
|
||||
|
||||
TransferSender* getTransferSender()
|
||||
{
|
||||
checkInit();
|
||||
(void)checkInit();
|
||||
return sender_.isConstructed() ? static_cast<TransferSender*>(sender_) : NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uavcan/error.hpp>
|
||||
#include <uavcan/node/abstract_node.hpp>
|
||||
#include <uavcan/data_type.hpp>
|
||||
#include <uavcan/node/global_data_type_registry.hpp>
|
||||
@@ -113,11 +114,11 @@ class GenericSubscriber : Noncopyable
|
||||
ReceivedDataStructureSpec message_;
|
||||
uint32_t failure_count_;
|
||||
|
||||
bool checkInit()
|
||||
int checkInit()
|
||||
{
|
||||
if (forwarder_)
|
||||
{
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
GlobalDataTypeRegistry::instance().freeze();
|
||||
@@ -128,11 +129,11 @@ class GenericSubscriber : Noncopyable
|
||||
if (!descr)
|
||||
{
|
||||
UAVCAN_TRACE("GenericSubscriber", "Type [%s] is not registered", DataSpec::getDataTypeFullName());
|
||||
return false;
|
||||
return -ErrUnknownDataType;
|
||||
}
|
||||
forwarder_.template construct<SelfType&, const DataTypeDescriptor&, IAllocator&>
|
||||
(*this, *descr, node_.getAllocator());
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool decodeTransfer(IncomingTransfer& transfer)
|
||||
@@ -167,19 +168,20 @@ class GenericSubscriber : Noncopyable
|
||||
{
|
||||
stop();
|
||||
|
||||
if (!checkInit())
|
||||
const int res = checkInit();
|
||||
if (res < 0)
|
||||
{
|
||||
UAVCAN_TRACE("GenericSubscriber", "Initialization failure [%s]", DataSpec::getDataTypeFullName());
|
||||
return -1;
|
||||
return res;
|
||||
}
|
||||
|
||||
if (!(node_.getDispatcher().*registration_method)(forwarder_))
|
||||
{
|
||||
UAVCAN_TRACE("GenericSubscriber", "Failed to register transfer listener [%s]",
|
||||
DataSpec::getDataTypeFullName());
|
||||
return -1;
|
||||
return -ErrInvalidTransferListener;
|
||||
}
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <uavcan/error.hpp>
|
||||
#include <uavcan/impl_constants.hpp>
|
||||
#include <uavcan/dynamic_memory.hpp>
|
||||
#include <uavcan/node/abstract_node.hpp>
|
||||
@@ -94,7 +95,7 @@ public:
|
||||
{
|
||||
return INode::spin(deadline);
|
||||
}
|
||||
return -1;
|
||||
return -ErrNotInited;
|
||||
}
|
||||
|
||||
int spin(MonotonicDuration duration)
|
||||
@@ -103,7 +104,7 @@ public:
|
||||
{
|
||||
return INode::spin(duration);
|
||||
}
|
||||
return -1;
|
||||
return -ErrNotInited;
|
||||
}
|
||||
|
||||
bool isStarted() const { return started_; }
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
if (!dst_node_id.isUnicast())
|
||||
{
|
||||
assert(0);
|
||||
return -1;
|
||||
return -ErrInvalidParam;
|
||||
}
|
||||
return publish(message, TransferTypeMessageUnicast, dst_node_id);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uavcan/error.hpp>
|
||||
#include <uavcan/linked_list.hpp>
|
||||
#include <uavcan/transport/dispatcher.hpp>
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ public:
|
||||
if (!isCallbackValid())
|
||||
{
|
||||
UAVCAN_TRACE("ServiceClient", "Invalid callback");
|
||||
return -1;
|
||||
return -ErrInvalidParam;
|
||||
}
|
||||
pending_ = true;
|
||||
|
||||
@@ -196,7 +196,7 @@ public:
|
||||
{
|
||||
UAVCAN_TRACE("ServiceClient", "Type [%s] is not registered", DataType::getDataTypeFullName());
|
||||
cancel();
|
||||
return -1;
|
||||
return -ErrUnknownDataType;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -211,7 +211,7 @@ public:
|
||||
{
|
||||
UAVCAN_TRACE("ServiceClient", "OTR access failure, dtd=%s", descr->toString().c_str());
|
||||
cancel();
|
||||
return -1;
|
||||
return -ErrMemory;
|
||||
}
|
||||
const TransferID transfer_id = *otr_tid;
|
||||
otr_tid->increment();
|
||||
@@ -235,7 +235,7 @@ public:
|
||||
{
|
||||
assert(0); // Must have been created
|
||||
cancel();
|
||||
return -1;
|
||||
return -ErrLogic;
|
||||
}
|
||||
const typename TransferListenerType::ExpectedResponseParams erp(server_node_id, transfer_id);
|
||||
tl->setExpectedResponseParams(erp);
|
||||
|
||||
@@ -91,7 +91,7 @@ public:
|
||||
if (!try_implicit_cast<bool>(callback, true))
|
||||
{
|
||||
UAVCAN_TRACE("ServiceServer", "Invalid callback");
|
||||
return -1;
|
||||
return -ErrInvalidParam;
|
||||
}
|
||||
callback_ = callback;
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ public:
|
||||
if (!try_implicit_cast<bool>(callback, true))
|
||||
{
|
||||
UAVCAN_TRACE("Subscriber", "Invalid callback");
|
||||
return -1;
|
||||
return -ErrInvalidParam;
|
||||
}
|
||||
callback_ = callback;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user