• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 /**
30  * @file
31  *   This file includes definitions for NCP service.
32  */
33 
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 namespace Ncp {
54 
55 /**
56  * This interface defines NCP Controller functionality.
57  *
58  */
59 class ControllerOpenThread : public MainloopProcessor
60 {
61 public:
62     using ThreadStateChangedCallback = std::function<void(otChangedFlags aFlags)>;
63 
64     /**
65      * This constructor initializes this object.
66      *
67      * @param[in]   aInterfaceName          A string of the NCP interface name.
68      * @param[in]   aRadioUrls              The radio URLs (can be IEEE802.15.4 or TREL radio).
69      * @param[in]   aBackboneInterfaceName  The Backbone network interface name.
70      * @param[in]   aDryRun                 TRUE to indicate dry-run mode. FALSE otherwise.
71      * @param[in]   aEnableAutoAttach       Whether or not to automatically attach to the saved network.
72      *
73      */
74     ControllerOpenThread(const char *                     aInterfaceName,
75                          const std::vector<const char *> &aRadioUrls,
76                          const char *                     aBackboneInterfaceName,
77                          bool                             aDryRun,
78                          bool                             aEnableAutoAttach);
79 
80     /**
81      * This method initialize the NCP controller.
82      *
83      */
84     void Init(void);
85 
86     /**
87      * This method deinitialize the NCP controller.
88      *
89      */
90     void Deinit(void);
91 
92     /**
93      * This method get mInstance pointer.
94      *
95      * @retval The pointer of mInstance.
96      *
97      */
GetInstance(void)98     otInstance *GetInstance(void)
99     {
100         assert(mInstance != nullptr);
101         return mInstance;
102     }
103 
104     /**
105      * This method gets the thread functionality helper.
106      *
107      * @retval The pointer to the helper object.
108      *
109      */
GetThreadHelper(void)110     otbr::agent::ThreadHelper *GetThreadHelper(void)
111     {
112         assert(mThreadHelper != nullptr);
113         return mThreadHelper.get();
114     }
115 
116     void Update(MainloopContext &aMainloop) override;
117     void Process(const MainloopContext &aMainloop) override;
118 
119     /**
120      * This method posts a task to the timer
121      *
122      * @param[in] aDelay  The delay in milliseconds before executing the task.
123      * @param[in] aTask   The task function.
124      *
125      */
126     void PostTimerTask(Milliseconds aDelay, TaskRunner::Task<void> aTask);
127 
128     /**
129      * This method registers a reset handler.
130      *
131      * @param[in] aHandler  The handler function.
132      *
133      */
134     void RegisterResetHandler(std::function<void(void)> aHandler);
135 
136     /**
137      * This method adds a event listener for Thread state changes.
138      *
139      * @param[in] aCallback  The callback to receive Thread state changed events.
140      *
141      */
142     void AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback);
143 
144     /**
145      * This method resets the OpenThread instance.
146      *
147      */
148     void Reset(void);
149 
150     /**
151      * This method returns the Thread protocol version as a string.
152      *
153      * @returns A pointer to the Thread version string.
154      *
155      */
156     static const char *GetThreadVersion(void);
157 
158     /**
159      * This method returns the Thread network interface name.
160      *
161      * @returns A pointer to the Thread network interface name string.
162      *
163      */
GetInterfaceName(void) const164     const char *GetInterfaceName(void) const { return mConfig.mInterfaceName; }
165 
166     static otbrLogLevel ConvertToOtbrLogLevel(otLogLevel aLogLevel);
167 
168     ~ControllerOpenThread(void) override;
169 
170 private:
HandleStateChanged(otChangedFlags aFlags,void * aContext)171     static void HandleStateChanged(otChangedFlags aFlags, void *aContext)
172     {
173         static_cast<ControllerOpenThread *>(aContext)->HandleStateChanged(aFlags);
174     }
175     void HandleStateChanged(otChangedFlags aFlags);
176 
177     static void HandleBackboneRouterDomainPrefixEvent(void *                            aContext,
178                                                       otBackboneRouterDomainPrefixEvent aEvent,
179                                                       const otIp6Prefix *               aDomainPrefix);
180     void        HandleBackboneRouterDomainPrefixEvent(otBackboneRouterDomainPrefixEvent aEvent,
181                                                       const otIp6Prefix *               aDomainPrefix);
182 
183 #if OTBR_ENABLE_DUA_ROUTING
184     static void HandleBackboneRouterNdProxyEvent(void *                       aContext,
185                                                  otBackboneRouterNdProxyEvent aEvent,
186                                                  const otIp6Address *         aAddress);
187     void        HandleBackboneRouterNdProxyEvent(otBackboneRouterNdProxyEvent aEvent, const otIp6Address *aAddress);
188 #endif
189 
190     bool IsAutoAttachEnabled(void);
191     void DisableAutoAttach(void);
192 
193     static otLogLevel ConvertToOtLogLevel(otbrLogLevel aLevel);
194 
195     otInstance *mInstance;
196 
197     otPlatformConfig                           mConfig;
198     std::unique_ptr<otbr::agent::ThreadHelper> mThreadHelper;
199     std::vector<std::function<void(void)>>     mResetHandlers;
200     TaskRunner                                 mTaskRunner;
201     std::vector<ThreadStateChangedCallback>    mThreadStateChangedCallbacks;
202     bool                                       mEnableAutoAttach = false;
203 };
204 
205 } // namespace Ncp
206 } // namespace otbr
207 
208 #endif // OTBR_AGENT_NCP_OPENTHREAD_HPP_
209