Skip to content

Commit

Permalink
[thread host] add ThreadHost Error code
Browse files Browse the repository at this point in the history
  • Loading branch information
Irving-cl committed Dec 5, 2024
1 parent ff4ea4e commit 663c03a
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 89 deletions.
28 changes: 22 additions & 6 deletions src/dbus/server/dbus_thread_object_ncp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ using std::placeholders::_2;
namespace otbr {
namespace DBus {

/*
* TODO: Update DBusRequest::ReplyOtResult so that it can return Host error code.
* This method is a workaround to cast Ncp::Error to otError and thus can be passed to DBusRequest::ReplyOtResult.
*/
static otError HostErrorToOtError(Ncp::Error aError)
{
otError error = OT_ERROR_FAILED;

if (aError >= Ncp::kErrorNone && aError <= Ncp::kErrorGeneric)
{
error = static_cast<otError>(aError);
}

return error;
}

DBusThreadObjectNcp::DBusThreadObjectNcp(DBusConnection &aConnection,
const std::string &aInterfaceName,
otbr::Ncp::NcpHost &aHost)
Expand Down Expand Up @@ -111,9 +127,9 @@ void DBusThreadObjectNcp::JoinHandler(DBusRequest &aRequest)
std::copy(dataset.begin(), dataset.end(), activeOpDatasetTlvs.mTlvs);
activeOpDatasetTlvs.mLength = dataset.size();

mHost.Join(activeOpDatasetTlvs, [aRequest](otError aError, const std::string &aErrorInfo) mutable {
mHost.Join(activeOpDatasetTlvs, [aRequest](Ncp::Error aError, const std::string &aErrorInfo) mutable {
OT_UNUSED_VARIABLE(aErrorInfo);
aRequest.ReplyOtResult(aError);
aRequest.ReplyOtResult(HostErrorToOtError(aError));
});

exit:
Expand All @@ -125,9 +141,9 @@ void DBusThreadObjectNcp::JoinHandler(DBusRequest &aRequest)

void DBusThreadObjectNcp::LeaveHandler(DBusRequest &aRequest)
{
mHost.Leave(true /* aEraseDataset */, [aRequest](otError aError, const std::string &aErrorInfo) mutable {
mHost.Leave(true /* aEraseDataset */, [aRequest](Ncp::Error aError, const std::string &aErrorInfo) mutable {
OT_UNUSED_VARIABLE(aErrorInfo);
aRequest.ReplyOtResult(aError);
aRequest.ReplyOtResult(HostErrorToOtError(aError));
});
}

Expand All @@ -148,9 +164,9 @@ void DBusThreadObjectNcp::ScheduleMigrationHandler(DBusRequest &aRequest)

SuccessOrExit(error = agent::ThreadHelper::ProcessDatasetForMigration(pendingOpDatasetTlvs, delayInMilli));

mHost.ScheduleMigration(pendingOpDatasetTlvs, [aRequest](otError aError, const std::string &aErrorInfo) mutable {
mHost.ScheduleMigration(pendingOpDatasetTlvs, [aRequest](Ncp::Error aError, const std::string &aErrorInfo) mutable {
OT_UNUSED_VARIABLE(aErrorInfo);
aRequest.ReplyOtResult(aError);
aRequest.ReplyOtResult(HostErrorToOtError(aError));
});

exit:
Expand Down
2 changes: 1 addition & 1 deletion src/ncp/rcp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ void RcpHost::Join(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs, const A
OT_UNUSED_VARIABLE(aActiveOpDatasetTlvs);

// TODO: Implement Join under RCP mode.
mTaskRunner.Post([aReceiver](void) { aReceiver(OT_ERROR_NOT_IMPLEMENTED, "Not implemented!"); });
mTaskRunner.Post([aReceiver](void) { aReceiver(kErrorNotImplemented, "Not implemented!"); });
}

void RcpHost::Leave(bool aEraseDataset, const AsyncResultReceiver &aReceiver)
Expand Down
4 changes: 2 additions & 2 deletions src/ncp/rcp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ class RcpHost : public MainloopProcessor, public ThreadHost, public OtNetworkPro
}

private:
static void SafeInvokeAndClear(AsyncResultReceiver &aReceiver, otError aError, const std::string &aErrorInfo = "")
static void SafeInvokeAndClear(AsyncResultReceiver &aReceiver, Error aError, const std::string &aErrorInfo = "")
{
if (aReceiver)
{
aReceiver(aError, aErrorInfo);
aReceiver = nullptr;
}
}
static void SafeInvoke(const AsyncResultReceiver &aReceiver, otError aError, const std::string &aErrorInfo = "")
static void SafeInvoke(const AsyncResultReceiver &aReceiver, Error aError, const std::string &aErrorInfo = "")
{
if (aReceiver)
{
Expand Down
53 changes: 51 additions & 2 deletions src/ncp/thread_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,56 @@ enum ThreadEnabledState
kStateInvalid = 255,
};

/**
* Some cases in ThreadHost APIs cannot be denoted by otError. This enumeration is a superset of otErrors
* and contains some error code specific to ThreadHost APIs.
*/
typedef int16_t Error;

// otErrors: 0 ~ 255
constexpr Error kErrorNone = OT_ERROR_NONE;
constexpr Error kErrorFailed = OT_ERROR_FAILED;
constexpr Error kErrorDrop = OT_ERROR_DROP;
constexpr Error kErrorNoBufs = OT_ERROR_NO_BUFS;
constexpr Error kErrorNoRoute = OT_ERROR_NO_ROUTE;
constexpr Error kErrorBusy = OT_ERROR_BUSY;
constexpr Error kErrorParse = OT_ERROR_PARSE;
constexpr Error kErrorInvalidArgs = OT_ERROR_INVALID_ARGS;
constexpr Error kErrorSecurity = OT_ERROR_SECURITY;
constexpr Error kErrorAddressQuery = OT_ERROR_ADDRESS_QUERY;
constexpr Error kErrorNoAddress = OT_ERROR_NO_ADDRESS;
constexpr Error kErrorAbort = OT_ERROR_ABORT;
constexpr Error kErrorNotImplemented = OT_ERROR_NOT_IMPLEMENTED;
constexpr Error kErrorInvalidState = OT_ERROR_INVALID_STATE;
constexpr Error kErrorNoAck = OT_ERROR_NO_ACK;
constexpr Error kErrorChannelAccessFailure = OT_ERROR_CHANNEL_ACCESS_FAILURE;
constexpr Error kErrorDetached = OT_ERROR_DETACHED;
constexpr Error kErrorFcs = OT_ERROR_FCS;
constexpr Error kErrorNoFrameReceived = OT_ERROR_NO_FRAME_RECEIVED;
constexpr Error kErrorUnknownNeighbor = OT_ERROR_UNKNOWN_NEIGHBOR;
constexpr Error kErrorInvalidSourceAddress = OT_ERROR_INVALID_SOURCE_ADDRESS;
constexpr Error kErrorAddressFiltered = OT_ERROR_ADDRESS_FILTERED;
constexpr Error kErrorDestinationAddressFiltered = OT_ERROR_DESTINATION_ADDRESS_FILTERED;
constexpr Error kErrorNotFound = OT_ERROR_NOT_FOUND;
constexpr Error kErrorAlready = OT_ERROR_ALREADY;
constexpr Error kErrorIp6AddressCreationFailure = OT_ERROR_IP6_ADDRESS_CREATION_FAILURE;
constexpr Error kErrorNotCapable = OT_ERROR_NOT_CAPABLE;
constexpr Error kErrorResponseTimeout = OT_ERROR_RESPONSE_TIMEOUT;
constexpr Error kErrorDuplicated = OT_ERROR_DUPLICATED;
constexpr Error kErrorReassemblyTimeout = OT_ERROR_REASSEMBLY_TIMEOUT;
constexpr Error kErrorNotTmf = OT_ERROR_NOT_TMF;
constexpr Error kErrorNotLowpanDataFrame = OT_ERROR_NOT_LOWPAN_DATA_FRAME;
constexpr Error kErrorLinkMarginLow = OT_ERROR_LINK_MARGIN_LOW;
constexpr Error kErrorInvalidCommand = OT_ERROR_INVALID_COMMAND;
constexpr Error kErrorPending = OT_ERROR_PENDING;
constexpr Error kErrorRejected = OT_ERROR_REJECTED;
constexpr Error kErrorGeneric = OT_ERROR_GENERIC;

// Proprietary errors: -256 ~ -1
constexpr Error kErrorUnsupportedChannel = -1; // The channel provided is not supported.
constexpr Error kErrorDisabled = -2; // The action didn't succeed because Thread is in Disabled state.
constexpr Error kErrorFailedPrecondition = -3; // The action didn't succeed because some precondition isn't satisfied.

/**
* This class is an interface which provides a set of async APIs to control the
* Thread network.
Expand All @@ -115,10 +165,9 @@ enum ThreadEnabledState
class ThreadHost : virtual public NetworkProperties
{
public:
using AsyncResultReceiver = std::function<void(otError, const std::string &)>;
using AsyncResultReceiver = std::function<void(Error, const std::string &)>;
using ChannelMasksReceiver =
std::function<void(uint32_t /*aSupportedChannelMask*/, uint32_t /*aPreferredChannelMask*/)>;
using DeviceRoleHandler = std::function<void(otError, otDeviceRole)>;
using ThreadStateChangedCallback = std::function<void(otChangedFlags aFlags)>;
using ThreadEnabledStateCallback = std::function<void(ThreadEnabledState aState)>;

Expand Down
Loading

0 comments on commit 663c03a

Please sign in to comment.