Skip to content

Commit

Permalink
[ncp] pass necessary encoder to encoding func (#2576)
Browse files Browse the repository at this point in the history
Only the encoder is needed for the encoding functions. This commit avoids
passing unnecessary context to them.
  • Loading branch information
bukepo authored Nov 1, 2024
1 parent 7d6ccd1 commit 62395a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/ncp/ncp_spinel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "lib/spinel/spinel.h"
#include "lib/spinel/spinel_decoder.hpp"
#include "lib/spinel/spinel_driver.hpp"
#include "lib/spinel/spinel_encoder.hpp"
#include "lib/spinel/spinel_helper.hpp"

namespace otbr {
Expand Down Expand Up @@ -96,8 +97,8 @@ otbrError NcpSpinel::SpinelDataUnpack(const uint8_t *aDataIn, spinel_size_t aDat
void NcpSpinel::DatasetSetActiveTlvs(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs, AsyncTaskPtr aAsyncTask)
{
otError error = OT_ERROR_NONE;
EncodingFunc encodingFunc = [this, &aActiveOpDatasetTlvs] {
return mEncoder.WriteData(aActiveOpDatasetTlvs.mTlvs, aActiveOpDatasetTlvs.mLength);
EncodingFunc encodingFunc = [&aActiveOpDatasetTlvs](ot::Spinel::Encoder &aEncoder) {
return aEncoder.WriteData(aActiveOpDatasetTlvs.mTlvs, aActiveOpDatasetTlvs.mLength);
};

VerifyOrExit(mDatasetSetActiveTask == nullptr, error = OT_ERROR_BUSY);
Expand All @@ -116,8 +117,8 @@ void NcpSpinel::DatasetMgmtSetPending(std::shared_ptr<otOperationalDatasetTlvs>
AsyncTaskPtr aAsyncTask)
{
otError error = OT_ERROR_NONE;
EncodingFunc encodingFunc = [this, aPendingOpDatasetTlvsPtr] {
return mEncoder.WriteData(aPendingOpDatasetTlvsPtr->mTlvs, aPendingOpDatasetTlvsPtr->mLength);
EncodingFunc encodingFunc = [aPendingOpDatasetTlvsPtr](ot::Spinel::Encoder &aEncoder) {
return aEncoder.WriteData(aPendingOpDatasetTlvsPtr->mTlvs, aPendingOpDatasetTlvsPtr->mLength);
};

VerifyOrExit(mDatasetMgmtSetPendingTask == nullptr, error = OT_ERROR_BUSY);
Expand All @@ -135,7 +136,7 @@ void NcpSpinel::DatasetMgmtSetPending(std::shared_ptr<otOperationalDatasetTlvs>
void NcpSpinel::Ip6SetEnabled(bool aEnable, AsyncTaskPtr aAsyncTask)
{
otError error = OT_ERROR_NONE;
EncodingFunc encodingFunc = [this, aEnable] { return mEncoder.WriteBool(aEnable); };
EncodingFunc encodingFunc = [aEnable](ot::Spinel::Encoder &aEncoder) { return aEncoder.WriteBool(aEnable); };

VerifyOrExit(mIp6SetEnabledTask == nullptr, error = OT_ERROR_BUSY);

Expand All @@ -154,7 +155,9 @@ void NcpSpinel::Ip6SetEnabled(bool aEnable, AsyncTaskPtr aAsyncTask)
otbrError NcpSpinel::Ip6Send(const uint8_t *aData, uint16_t aLength)
{
otbrError error = OTBR_ERROR_NONE;
EncodingFunc encodingFunc = [this, aData, aLength] { return mEncoder.WriteDataWithLen(aData, aLength); };
EncodingFunc encodingFunc = [aData, aLength](ot::Spinel::Encoder &aEncoder) {
return aEncoder.WriteDataWithLen(aData, aLength);
};

SuccessOrExit(SetProperty(SPINEL_PROP_STREAM_NET, encodingFunc), error = OTBR_ERROR_OPENTHREAD);

Expand All @@ -165,7 +168,7 @@ otbrError NcpSpinel::Ip6Send(const uint8_t *aData, uint16_t aLength)
void NcpSpinel::ThreadSetEnabled(bool aEnable, AsyncTaskPtr aAsyncTask)
{
otError error = OT_ERROR_NONE;
EncodingFunc encodingFunc = [this, aEnable] { return mEncoder.WriteBool(aEnable); };
EncodingFunc encodingFunc = [aEnable](ot::Spinel::Encoder &aEncoder) { return aEncoder.WriteBool(aEnable); };

VerifyOrExit(mThreadSetEnabledTask == nullptr, error = OT_ERROR_BUSY);

Expand All @@ -184,7 +187,7 @@ void NcpSpinel::ThreadSetEnabled(bool aEnable, AsyncTaskPtr aAsyncTask)
void NcpSpinel::ThreadDetachGracefully(AsyncTaskPtr aAsyncTask)
{
otError error = OT_ERROR_NONE;
EncodingFunc encodingFunc = [] { return OT_ERROR_NONE; };
EncodingFunc encodingFunc = [](ot::Spinel::Encoder &) { return OT_ERROR_NONE; };

VerifyOrExit(mThreadDetachGracefullyTask == nullptr, error = OT_ERROR_BUSY);

Expand Down Expand Up @@ -556,7 +559,9 @@ otbrError NcpSpinel::HandleResponseForPropRemove(spinel_tid_t aTid,
otbrError NcpSpinel::Ip6MulAddrUpdateSubscription(const otIp6Address &aAddress, bool aIsAdded)
{
otbrError error = OTBR_ERROR_NONE;
EncodingFunc encodingFunc = [this, aAddress] { return mEncoder.WriteIp6Address(aAddress); };
EncodingFunc encodingFunc = [&aAddress](ot::Spinel::Encoder &aEncoder) {
return aEncoder.WriteIp6Address(aAddress);
};

if (aIsAdded)
{
Expand Down Expand Up @@ -613,7 +618,7 @@ otError NcpSpinel::SendCommand(spinel_command_t aCmd, spinel_prop_key_t aKey, co

VerifyOrExit(tid != 0, error = OT_ERROR_BUSY);
SuccessOrExit(error = mEncoder.BeginFrame(header, aCmd, aKey));
SuccessOrExit(error = aEncodingFunc());
SuccessOrExit(error = aEncodingFunc(mEncoder));
SuccessOrExit(error = mEncoder.EndFrame());
SuccessOrExit(error = SendEncodedFrame());

Expand Down
2 changes: 1 addition & 1 deletion src/ncp/ncp_spinel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class NcpSpinel : public Netif::Dependencies
spinel_tid_t GetNextTid(void);
void FreeTidTableItem(spinel_tid_t aTid);

using EncodingFunc = std::function<otError(void)>;
using EncodingFunc = std::function<otError(ot::Spinel::Encoder &aEncoder)>;
otError SendCommand(spinel_command_t aCmd, spinel_prop_key_t aKey, const EncodingFunc &aEncodingFunc);
otError SetProperty(spinel_prop_key_t aKey, const EncodingFunc &aEncodingFunc);
otError InsertProperty(spinel_prop_key_t aKey, const EncodingFunc &aEncodingFunc);
Expand Down

0 comments on commit 62395a5

Please sign in to comment.