Skip to content

Commit

Permalink
[thread host] add set channel max power api
Browse files Browse the repository at this point in the history
  • Loading branch information
Irving-cl committed Oct 21, 2024
1 parent 76bfeef commit d7ac367
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ncp/ncp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ void NcpHost::SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceive
mTaskRunner.Post([aReceiver](void) { aReceiver(OT_ERROR_NOT_IMPLEMENTED, "Not implemented!"); });
}

void NcpHost::SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers,
const AsyncResultReceiver &aReceiver)
{
OT_UNUSED_VARIABLE(aChannelMaxPowers);

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

void NcpHost::Process(const MainloopContext &aMainloop)
{
mSpinelDriver.Process(&aMainloop);
Expand Down
2 changes: 2 additions & 0 deletions src/ncp/ncp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class NcpHost : public MainloopProcessor, public ThreadHost, public NcpNetworkPr
void ScheduleMigration(const otOperationalDatasetTlvs &aPendingOpDatasetTlvs,
const AsyncResultReceiver aReceiver) override;
void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) override;
void SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers,
const AsyncResultReceiver &aReceiver) override;
CoprocessorType GetCoprocessorType(void) override { return OT_COPROCESSOR_NCP; }
const char *GetCoprocessorVersion(void) override;
const char *GetInterfaceName(void) const override { return mConfig.mInterfaceName; }
Expand Down
47 changes: 47 additions & 0 deletions src/ncp/rcp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ncp/rcp_host.hpp"

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -463,6 +464,52 @@ void RcpHost::SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceive
}
}

void RcpHost::SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers,
const AsyncResultReceiver &aReceiver)
{
otError error = OT_ERROR_NONE;
std::string errorMsg;
uint8_t channel;
int16_t maxPower;

VerifyOrExit(mInstance != nullptr, error = OT_ERROR_INVALID_STATE, errorMsg = "OT is not initialized");

for (ChannelMaxPower channelMaxPower : aChannelMaxPowers)
{
VerifyOrExit((channelMaxPower.mChannel >= OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN) &&
(channelMaxPower.mChannel <= OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX),
error = OT_ERROR_INVALID_ARGS, errorMsg = "The channel is invalid");
}

for (ChannelMaxPower channelMaxPower : aChannelMaxPowers)
{
channel = static_cast<uint8_t>(channelMaxPower.mChannel);

// INT_MIN indicates that the corresponding channel is disabled in Thread Android API `setChannelMaxPowers()`
if (channelMaxPower.mMaxPower == INT_MIN)
{
// INT16_MAX indicates that the corresponding channel is disabled in OpenThread API
// `otPlatRadioSetChannelTargetPower()`.
maxPower = INT16_MAX;
}
else
{
// Equivalent to a `clamp` operation here. However `std::clamp` is only available since c++17.
maxPower = channelMaxPower.mMaxPower < INT16_MIN ? INT16_MIN
: channelMaxPower.mMaxPower > INT16_MAX - 1 ? INT16_MAX - 1
: channelMaxPower.mMaxPower;
}

otbrLogInfo("Set channel max power: channel=%u, maxPower=%d", static_cast<unsigned int>(channel),
static_cast<int>(maxPower));
SuccessOrExit(error = otPlatRadioSetChannelTargetPower(mInstance, channel, maxPower),
errorMsg = "Failed to set channel max power");
}

exit:
mTaskRunner.Post([aReceiver, error, errorMsg](void) { aReceiver(error, errorMsg); });
}

void RcpHost::DisableThreadAfterDetach(void *aContext)
{
static_cast<RcpHost *>(aContext)->DisableThreadAfterDetach();
Expand Down
2 changes: 2 additions & 0 deletions src/ncp/rcp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class RcpHost : public MainloopProcessor, public ThreadHost, public OtNetworkPro
void ScheduleMigration(const otOperationalDatasetTlvs &aPendingOpDatasetTlvs,
const AsyncResultReceiver aReceiver) override;
void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) override;
void SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers,
const AsyncResultReceiver &aReceiver) override;

CoprocessorType GetCoprocessorType(void) override
{
Expand Down
19 changes: 19 additions & 0 deletions src/ncp/thread_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class ThreadHost : virtual public NetworkProperties
using AsyncResultReceiver = std::function<void(otError, const std::string &)>;
using DeviceRoleHandler = std::function<void(otError, otDeviceRole)>;

struct ChannelMaxPower
{
int mChannel;
int mMaxPower;
};

/**
* Create a Thread Controller Instance.
*
Expand Down Expand Up @@ -157,6 +163,19 @@ class ThreadHost : virtual public NetworkProperties
*/
virtual void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) = 0;

/**
* Sets the max power of each channel.
*
* 1. If the host hasn't been initialized, @p aReceiver will be invoked with error OT_ERROR_INVALID_STATE.
* 2. If any value in @p aChannelMaxPowers is invalid, @p aReceiver will be invoked with error
* OT_ERROR_INVALID_ARGS.
*
* @param[in] aChannelMaxPowers A vector of ChannelMaxPower.
* @param[in] aReceiver A receiver to get the async result of this operation.
*/
virtual void SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers,
const AsyncResultReceiver &aReceiver) = 0;

/**
* Returns the co-processor type.
*/
Expand Down

0 comments on commit d7ac367

Please sign in to comment.