openthread-br  0.3.0-72c0388
ncp_openthread.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef OTBR_AGENT_NCP_OPENTHREAD_HPP_
35 #define OTBR_AGENT_NCP_OPENTHREAD_HPP_
36 
37 #include <chrono>
38 #include <memory>
39 
40 #include <assert.h>
41 
42 #include <openthread/backbone_router_ftd.h>
43 #include <openthread/cli.h>
44 #include <openthread/instance.h>
45 #include <openthread/openthread-system.h>
46 
47 #include "common/mainloop.hpp"
48 #include "common/task_runner.hpp"
49 #include "common/types.hpp"
50 #include "utils/thread_helper.hpp"
51 
52 namespace otbr {
53 #if OTBR_ENABLE_FEATURE_FLAGS
54 // Forward declaration of FeatureFlagList proto.
55 class FeatureFlagList;
56 #endif
57 
58 namespace Ncp {
59 
65 {
66 public:
67  using ThreadStateChangedCallback = std::function<void(otChangedFlags aFlags)>;
68 
79  ControllerOpenThread(const char * aInterfaceName,
80  const std::vector<const char *> &aRadioUrls,
81  const char * aBackboneInterfaceName,
82  bool aDryRun,
83  bool aEnableAutoAttach);
84 
89  void Init(void);
90 
95  void Deinit(void);
96 
103  otInstance *GetInstance(void)
104  {
105  assert(mInstance != nullptr);
106  return mInstance;
107  }
108 
116  {
117  assert(mThreadHelper != nullptr);
118  return mThreadHelper.get();
119  }
120 
121  void Update(MainloopContext &aMainloop) override;
122  void Process(const MainloopContext &aMainloop) override;
123 
131  void PostTimerTask(Milliseconds aDelay, TaskRunner::Task<void> aTask);
132 
139  void RegisterResetHandler(std::function<void(void)> aHandler);
140 
147  void AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback);
148 
153  void Reset(void);
154 
161  static const char *GetThreadVersion(void);
162 
169  const char *GetInterfaceName(void) const { return mConfig.mInterfaceName; }
170 
171  static otbrLogLevel ConvertToOtbrLogLevel(otLogLevel aLogLevel);
172 
173 #if OTBR_ENABLE_FEATURE_FLAGS
174 
182  otError ApplyFeatureFlagList(const FeatureFlagList &aFeatureFlagList);
183 
190  const std::string &GetAppliedFeatureFlagListBytes(void) { return mAppliedFeatureFlagListBytes; }
191 #endif
192 
193  ~ControllerOpenThread(void) override;
194 
195 private:
196  static void HandleStateChanged(otChangedFlags aFlags, void *aContext)
197  {
198  static_cast<ControllerOpenThread *>(aContext)->HandleStateChanged(aFlags);
199  }
200  void HandleStateChanged(otChangedFlags aFlags);
201 
202  static void HandleBackboneRouterDomainPrefixEvent(void * aContext,
203  otBackboneRouterDomainPrefixEvent aEvent,
204  const otIp6Prefix * aDomainPrefix);
205  void HandleBackboneRouterDomainPrefixEvent(otBackboneRouterDomainPrefixEvent aEvent,
206  const otIp6Prefix * aDomainPrefix);
207 
208 #if OTBR_ENABLE_DUA_ROUTING
209  static void HandleBackboneRouterNdProxyEvent(void * aContext,
210  otBackboneRouterNdProxyEvent aEvent,
211  const otIp6Address * aAddress);
212  void HandleBackboneRouterNdProxyEvent(otBackboneRouterNdProxyEvent aEvent, const otIp6Address *aAddress);
213 #endif
214 
215  bool IsAutoAttachEnabled(void);
216  void DisableAutoAttach(void);
217 
218  static otLogLevel ConvertToOtLogLevel(otbrLogLevel aLevel);
219 
220  otInstance *mInstance;
221 
222  otPlatformConfig mConfig;
223  std::unique_ptr<otbr::agent::ThreadHelper> mThreadHelper;
224  std::vector<std::function<void(void)>> mResetHandlers;
225  TaskRunner mTaskRunner;
226  std::vector<ThreadStateChangedCallback> mThreadStateChangedCallbacks;
227  bool mEnableAutoAttach = false;
228 #if OTBR_ENABLE_FEATURE_FLAGS
229  // The applied FeatureFlagList in ApplyFeatureFlagList call, used for debugging purpose.
230  std::string mAppliedFeatureFlagListBytes;
231 #endif
232 };
233 
234 } // namespace Ncp
235 } // namespace otbr
236 
237 #endif // OTBR_AGENT_NCP_OPENTHREAD_HPP_
otbr::Ncp::ControllerOpenThread::Update
void Update(MainloopContext &aMainloop) override
Definition: ncp_openthread.cpp:245
otbr::Ncp::ControllerOpenThread::PostTimerTask
void PostTimerTask(Milliseconds aDelay, TaskRunner::Task< void > aTask)
Definition: ncp_openthread.cpp:277
mainloop.hpp
otbr::Ncp::ControllerOpenThread::Init
void Init(void)
Definition: ncp_openthread.cpp:157
otbr::agent::ThreadHelper
Definition: thread_helper.hpp:63
otbr::Ncp::ControllerOpenThread::Deinit
void Deinit(void)
Definition: ncp_openthread.cpp:206
otbr::Ncp::ControllerOpenThread::GetThreadHelper
otbr::agent::ThreadHelper * GetThreadHelper(void)
Definition: ncp_openthread.hpp:115
otbr::Ncp::ControllerOpenThread::GetInstance
otInstance * GetInstance(void)
Definition: ncp_openthread.hpp:103
otbr::Ncp::ControllerOpenThread::AddThreadStateChangedCallback
void AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback)
Definition: ncp_openthread.cpp:287
otbr::Ncp::ControllerOpenThread::Process
void Process(const MainloopContext &aMainloop) override
Definition: ncp_openthread.cpp:255
otbr::MainloopProcessor
Definition: mainloop.hpp:54
otbr::Ncp::ControllerOpenThread::GetInterfaceName
const char * GetInterfaceName(void) const
Definition: ncp_openthread.hpp:169
otbr::TaskRunner::Task
std::function< T(void)> Task
Definition: task_runner.hpp:64
otbr::Ncp::ControllerOpenThread::RegisterResetHandler
void RegisterResetHandler(std::function< void(void)> aHandler)
Definition: ncp_openthread.cpp:282
otbrLogLevel
otbrLogLevel
Definition: logging.hpp:51
otbr::Ncp::ControllerOpenThread::Reset
void Reset(void)
Definition: ncp_openthread.cpp:292
task_runner.hpp
otbr::Ncp::ControllerOpenThread::GetThreadVersion
static const char * GetThreadVersion(void)
Definition: ncp_openthread.cpp:307
thread_helper.hpp
otbr::MainloopContext
otSysMainloopContext MainloopContext
Definition: mainloop.hpp:47
otbr::Ncp::ControllerOpenThread
Definition: ncp_openthread.hpp:64
otbr::Ncp::ControllerOpenThread::ControllerOpenThread
ControllerOpenThread(const char *aInterfaceName, const std::vector< const char * > &aRadioUrls, const char *aBackboneInterfaceName, bool aDryRun, bool aEnableAutoAttach)
Definition: ncp_openthread.cpp:68