Skip to content

Commit

Permalink
[thread-host] add set country code api (#2550)
Browse files Browse the repository at this point in the history
This commit adds the `SetCountryCode` method to `ThreadHost`.

The commit implements this method for `RcpHost`. The implementation
for NCP will be added later.
  • Loading branch information
Irving-cl authored Oct 22, 2024
1 parent 9506b2e commit dc6b912
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ncp/ncp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ void NcpHost::SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceive
mTaskRunner.Post([aReceiver](void) { aReceiver(OT_ERROR_NOT_IMPLEMENTED, "Not implemented!"); });
}

void NcpHost::SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver)
{
OT_UNUSED_VARIABLE(aCountryCode);

// TODO: Implement SetCountryCode 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
1 change: 1 addition & 0 deletions src/ncp/ncp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ 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 SetCountryCode(const std::string &aCountryCode, 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
20 changes: 20 additions & 0 deletions src/ncp/rcp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,26 @@ void RcpHost::DisableThreadAfterDetach(void)
SafeInvokeAndClear(mSetThreadEnabledReceiver, error, errorMsg);
}

void RcpHost::SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver)
{
static constexpr int kCountryCodeLength = 2;
otError error = OT_ERROR_NONE;
std::string errorMsg;
uint16_t countryCode;

VerifyOrExit((aCountryCode.length() == kCountryCodeLength) && isalpha(aCountryCode[0]) && isalpha(aCountryCode[1]),
error = OT_ERROR_INVALID_ARGS, errorMsg = "The country code is invalid");

otbrLogInfo("Set country code: %c%c", aCountryCode[0], aCountryCode[1]);
VerifyOrExit(mInstance != nullptr, error = OT_ERROR_INVALID_STATE, errorMsg = "OT is not initialized");

countryCode = static_cast<uint16_t>((aCountryCode[0] << 8) | aCountryCode[1]);
SuccessOrExit(error = otLinkSetRegion(mInstance, countryCode), errorMsg = "Failed to set the country code");

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

/*
* Provide, if required an "otPlatLog()" function
*/
Expand Down
1 change: 1 addition & 0 deletions src/ncp/rcp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ 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 SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver) override;

CoprocessorType GetCoprocessorType(void) override
{
Expand Down
12 changes: 12 additions & 0 deletions src/ncp/thread_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ class ThreadHost : virtual public NetworkProperties
*/
virtual void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) = 0;

/**
* This method sets the country code.
*
* The country code refers to the 2-alpha code defined in ISO-3166.
*
* 1. If @p aCountryCode isn't valid, @p aReceiver will be invoked with error OT_ERROR_INVALID_ARGS.
* 2. If the host hasn't been initialized, @p aReceiver will be invoked with error OT_ERROR_INVALID_STATE.
*
* @param[in] aCountryCode The country code.
*/
virtual void SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver) = 0;

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

0 comments on commit dc6b912

Please sign in to comment.