Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / agent / ncp_openthread.hpp
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 <openthread/backbone_router_ftd.h>
41 #include <openthread/cli.h>
42 #include <openthread/instance.h>
43 #include <openthread/openthread-system.h>
44
45 #include "ncp.hpp"
46 #include "agent/thread_helper.hpp"
47
48 namespace otbr {
49 namespace Ncp {
50
51 /**
52  * This interface defines NCP Controller functionality.
53  *
54  */
55 class ControllerOpenThread : public Controller
56 {
57 public:
58     /**
59      * This constructor initializes this object.
60      *
61      * @param[in]   aInterfaceName          A string of the NCP interface name.
62      * @param[in]   aRadioUrl               The URL describes the radio chip.
63      * @param[in]   aBackboneInterfaceName  The Backbone network interface name.
64      *
65      */
66     ControllerOpenThread(const char *aInterfaceName, const char *aRadioUrl, const char *aBackboneInterfaceName);
67
68     /**
69      * This method initalize the NCP controller.
70      *
71      * @retval  OTBR_ERROR_NONE     Successfully initialized NCP controller.
72      *
73      */
74     otbrError Init(void) override;
75
76     /**
77      * This method get mInstance pointer.
78      *
79      * @retval  the pointer of mInstance.
80      *
81      */
82     otInstance *GetInstance(void) { return mInstance; }
83
84     /**
85      * This method gets the thread functionality helper.
86      *
87      * @retval  the pointer to the helper object.
88      *
89      */
90     otbr::agent::ThreadHelper *GetThreadHelper(void) { return mThreadHelper.get(); }
91
92     /**
93      * This method updates the fd_set to poll.
94      *
95      * @param[inout]    aMainloop   A reference to OpenThread mainloop context.
96      *
97      */
98     void UpdateFdSet(otSysMainloopContext &aMainloop) override;
99
100     /**
101      * This method performs the Thread processing.
102      *
103      * @param[in]       aMainloop   A reference to OpenThread mainloop context.
104      *
105      */
106     void Process(const otSysMainloopContext &aMainloop) override;
107
108     /**
109      * This method request the event.
110      *
111      * @param[in]   aEvent  The event id to request.
112      *
113      * @retval  OTBR_ERROR_NONE         Successfully requested the event.
114      * @retval  OTBR_ERROR_ERRNO        Failed to request the event.
115      *
116      */
117     otbrError RequestEvent(int aEvent) override;
118
119     /**
120      * This method posts a task to the timer
121      *
122      * @param[in]   aTimePoint  The timepoint to trigger the task.
123      * @param[in]   aTask       The task function.
124      *
125      */
126     void PostTimerTask(std::chrono::steady_clock::time_point aTimePoint, const std::function<void(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     ~ControllerOpenThread(void) override;
137
138 private:
139     static void HandleStateChanged(otChangedFlags aFlags, void *aContext)
140     {
141         static_cast<ControllerOpenThread *>(aContext)->HandleStateChanged(aFlags);
142     }
143     void HandleStateChanged(otChangedFlags aFlags);
144
145     static void HandleBackboneRouterDomainPrefixEvent(void *                            aContext,
146                                                       otBackboneRouterDomainPrefixEvent aEvent,
147                                                       const otIp6Prefix *               aDomainPrefix);
148     void        HandleBackboneRouterDomainPrefixEvent(otBackboneRouterDomainPrefixEvent aEvent,
149                                                       const otIp6Prefix *               aDomainPrefix);
150
151     static void HandleBackboneRouterNdProxyEvent(void *                       aContext,
152                                                  otBackboneRouterNdProxyEvent aEvent,
153                                                  const otIp6Address *         aAddress);
154     void        HandleBackboneRouterNdProxyEvent(otBackboneRouterNdProxyEvent aEvent, const otIp6Address *aAddress);
155
156     static void HandleBackboneRouterMulticastListenerEvent(void *                                 aContext,
157                                                            otBackboneRouterMulticastListenerEvent aEvent,
158                                                            const otIp6Address *                   aAddress);
159     void        HandleBackboneRouterMulticastListenerEvent(otBackboneRouterMulticastListenerEvent aEvent,
160                                                            const otIp6Address *                   aAddress);
161
162     otInstance *mInstance;
163
164     otPlatformConfig                                                                mConfig;
165     std::unique_ptr<otbr::agent::ThreadHelper>                                      mThreadHelper;
166     std::multimap<std::chrono::steady_clock::time_point, std::function<void(void)>> mTimers;
167     std::vector<std::function<void(void)>>                                          mResetHandlers;
168 };
169
170 } // namespace Ncp
171 } // namespace otbr
172
173 #endif // OTBR_AGENT_NCP_OPENTHREAD_HPP_