aboutsummaryrefslogtreecommitdiff
path: root/src/ncp
diff options
context:
space:
mode:
authorSimon Lin <simonlin@google.com>2022-01-19 01:20:58 +0800
committerGitHub <noreply@github.com>2022-01-18 09:20:58 -0800
commit4ce3a7b29f01ec65810dc8f03ca44a843ef68362 (patch)
tree299d0edf7ee5daa805fbaef87c8afac027076d43 /src/ncp
parentd6cf4ca301dc5ceb55977125ae8d51559b3345a8 (diff)
downloadot-br-posix-4ce3a7b29f01ec65810dc8f03ca44a843ef68362.tar.gz
[agent] de-initialize `Application` and OpenThread instance (#1183)
Background: - OpenThread Instance was the last member to destruct in Application, and otSysDeinit will be called when destructing OpenThread Instance. However, otSysDeinit may call methods of other Application members, which have been destructed at the moment. This commit introduces the Deinit() method to ControllerOpenThread and other classes to make sure OpenThread are properly de-initialized before destructing Application. The advantages are: - ControllerOpenThread initializes OT instance in Init, so adding Deinit makes it symmetrical with Init. - Provide a predictable runtime environment (Application object fully constructed) for OT instance from otSysInit to otSysDeinit, making sure OT can safely access Application members during its lifetime.
Diffstat (limited to 'src/ncp')
-rw-r--r--src/ncp/ncp_openthread.cpp11
-rw-r--r--src/ncp/ncp_openthread.hpp6
2 files changed, 16 insertions, 1 deletions
diff --git a/src/ncp/ncp_openthread.cpp b/src/ncp/ncp_openthread.cpp
index 22a6e162..42882cc7 100644
--- a/src/ncp/ncp_openthread.cpp
+++ b/src/ncp/ncp_openthread.cpp
@@ -83,7 +83,8 @@ ControllerOpenThread::ControllerOpenThread(const char * aInt
ControllerOpenThread::~ControllerOpenThread(void)
{
- otSysDeinit();
+ // Make sure OpenThread Instance was gracefully de-initialized.
+ assert(mInstance == nullptr);
}
void ControllerOpenThread::Init(void)
@@ -141,6 +142,14 @@ exit:
SuccessOrDie(error, "Failed to initialize NCP!");
}
+void ControllerOpenThread::Deinit(void)
+{
+ assert(mInstance != nullptr);
+
+ otSysDeinit();
+ mInstance = nullptr;
+}
+
void ControllerOpenThread::HandleStateChanged(otChangedFlags aFlags)
{
if (aFlags & OT_CHANGED_THREAD_ROLE)
diff --git a/src/ncp/ncp_openthread.hpp b/src/ncp/ncp_openthread.hpp
index b30dc295..e8ce0c6e 100644
--- a/src/ncp/ncp_openthread.hpp
+++ b/src/ncp/ncp_openthread.hpp
@@ -82,6 +82,12 @@ public:
void Init(void);
/**
+ * This method deinitialize the NCP controller.
+ *
+ */
+ void Deinit(void);
+
+ /**
* This method get mInstance pointer.
*
* @retval The pointer of mInstance.