From dc6b912aa94320f2b2839260b2f18ee98379c5be Mon Sep 17 00:00:00 2001 From: Li Cao Date: Tue, 22 Oct 2024 17:32:11 +0800 Subject: [PATCH] [thread-host] add set country code api (#2550) This commit adds the `SetCountryCode` method to `ThreadHost`. The commit implements this method for `RcpHost`. The implementation for NCP will be added later. --- src/ncp/ncp_host.cpp | 8 ++++++++ src/ncp/ncp_host.hpp | 1 + src/ncp/rcp_host.cpp | 20 ++++++++++++++++++++ src/ncp/rcp_host.hpp | 1 + src/ncp/thread_host.hpp | 12 ++++++++++++ 5 files changed, 42 insertions(+) diff --git a/src/ncp/ncp_host.cpp b/src/ncp/ncp_host.cpp index c6ea4f7d89e..907aa73fdca 100644 --- a/src/ncp/ncp_host.cpp +++ b/src/ncp/ncp_host.cpp @@ -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); diff --git a/src/ncp/ncp_host.hpp b/src/ncp/ncp_host.hpp index 4ea6b887d1d..5d4a0a30635 100644 --- a/src/ncp/ncp_host.hpp +++ b/src/ncp/ncp_host.hpp @@ -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; } diff --git a/src/ncp/rcp_host.cpp b/src/ncp/rcp_host.cpp index bd96fd8a584..60be4291b01 100644 --- a/src/ncp/rcp_host.cpp +++ b/src/ncp/rcp_host.cpp @@ -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((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 */ diff --git a/src/ncp/rcp_host.hpp b/src/ncp/rcp_host.hpp index f5d3b24158d..56202cb3c07 100644 --- a/src/ncp/rcp_host.hpp +++ b/src/ncp/rcp_host.hpp @@ -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 { diff --git a/src/ncp/thread_host.hpp b/src/ncp/thread_host.hpp index bdf464a7e8a..5d280d04878 100644 --- a/src/ncp/thread_host.hpp +++ b/src/ncp/thread_host.hpp @@ -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. */