• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2021, 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 definition for OTBR Agent.
32  */
33 
34 #ifndef OTBR_AGENT_APPLICATION_HPP_
35 #define OTBR_AGENT_APPLICATION_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #include <atomic>
40 #include <signal.h>
41 #include <stdint.h>
42 #include <vector>
43 
44 #if OTBR_ENABLE_BORDER_AGENT
45 #include "border_agent/border_agent.hpp"
46 #endif
47 #include "ncp/ncp_openthread.hpp"
48 #if OTBR_ENABLE_BACKBONE_ROUTER
49 #include "backbone_router/backbone_agent.hpp"
50 #endif
51 #if OTBR_ENABLE_REST_SERVER
52 #include "rest/rest_web_server.hpp"
53 #endif
54 #if OTBR_ENABLE_DBUS_SERVER
55 #include "dbus/server/dbus_agent.hpp"
56 #endif
57 #if OTBR_ENABLE_OPENWRT
58 #include "openwrt/ubus/otubus.hpp"
59 #endif
60 #if OTBR_ENABLE_VENDOR_SERVER
61 #include "agent/vendor.hpp"
62 #endif
63 #include "utils/infra_link_selector.hpp"
64 
65 namespace otbr {
66 
67 #if OTBR_ENABLE_VENDOR_SERVER
68 namespace vendor {
69 
70 class VendorServer;
71 
72 }
73 #endif
74 
75 /**
76  * @addtogroup border-router-agent
77  *
78  * @brief
79  *   This module includes definition for OTBR application.
80  *
81  * @{
82  */
83 
84 /**
85  * This class implements OTBR application management.
86  *
87  */
88 class Application : private NonCopyable
89 {
90 public:
91     /**
92      * This constructor initializes the Application instance.
93      *
94      * @param[in] aInterfaceName         Name of the Thread network interface.
95      * @param[in] aBackboneInterfaceName Name of the backbone network interface.
96      * @param[in] aRadioUrls             The radio URLs (can be IEEE802.15.4 or TREL radio).
97      * @param[in] aEnableAutoAttach      Whether or not to automatically attach to the saved network.
98      * @param[in] aRestListenAddress     Network address to listen on.
99      * @param[in] aRestListenPort        Network port to listen on.
100      *
101      */
102     explicit Application(const std::string               &aInterfaceName,
103                          const std::vector<const char *> &aBackboneInterfaceNames,
104                          const std::vector<const char *> &aRadioUrls,
105                          bool                             aEnableAutoAttach,
106                          const std::string               &aRestListenAddress,
107                          int                              aRestListenPort);
108 
109     /**
110      * This method initializes the Application instance.
111      *
112      */
113     void Init(void);
114 
115     /**
116      * This method de-initializes the Application instance.
117      *
118      */
119     void Deinit(void);
120 
121     /**
122      * This method runs the application until exit.
123      *
124      * @retval OTBR_ERROR_NONE  The application exited without any error.
125      * @retval OTBR_ERROR_ERRNO The application exited with some system error.
126      *
127      */
128     otbrError Run(void);
129 
130     /**
131      * Get the OpenThread controller object the application is using.
132      *
133      * @returns The OpenThread controller object.
134      */
GetNcp(void)135     Ncp::ControllerOpenThread &GetNcp(void) { return mNcp; }
136 
137 #if OTBR_ENABLE_MDNS
138     /**
139      * Get the Publisher object the application is using.
140      *
141      * @returns The Publisher object.
142      */
GetPublisher(void)143     Mdns::Publisher &GetPublisher(void)
144     {
145         return *mPublisher;
146     }
147 #endif
148 
149 #if OTBR_ENABLE_BORDER_AGENT
150     /**
151      * Get the border agent the application is using.
152      *
153      * @returns The border agent.
154      */
GetBorderAgent(void)155     BorderAgent &GetBorderAgent(void)
156     {
157         return mBorderAgent;
158     }
159 #endif
160 
161 #if OTBR_ENABLE_BACKBONE_ROUTER
162     /**
163      * Get the backbone agent the application is using.
164      *
165      * @returns The backbone agent.
166      */
GetBackboneAgent(void)167     BackboneRouter::BackboneAgent &GetBackboneAgent(void)
168     {
169         return mBackboneAgent;
170     }
171 #endif
172 
173 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
174     /**
175      * Get the advertising proxy the application is using.
176      *
177      * @returns The advertising proxy.
178      */
GetAdvertisingProxy(void)179     AdvertisingProxy &GetAdvertisingProxy(void)
180     {
181         return mAdvertisingProxy;
182     }
183 #endif
184 
185 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
186     /**
187      * Get the discovery proxy the application is using.
188      *
189      * @returns The discovery proxy.
190      */
GetDiscoveryProxy(void)191     Dnssd::DiscoveryProxy &GetDiscoveryProxy(void)
192     {
193         return mDiscoveryProxy;
194     }
195 #endif
196 
197 #if OTBR_ENABLE_TREL
198     /**
199      * Get the TrelDnssd object the application is using.
200      *
201      * @returns The TrelDnssd.
202      */
GetTrelDnssd(void)203     TrelDnssd::TrelDnssd &GetTrelDnssd(void)
204     {
205         return mTrelDnssd;
206     }
207 #endif
208 
209 #if OTBR_ENABLE_OPENWRT
210     /**
211      * Get the UBus agent the application is using.
212      *
213      * @returns The UBus agent.
214      */
GetUBusAgent(void)215     ubus::UBusAgent &GetUBusAgent(void)
216     {
217         return mUbusAgent;
218     }
219 #endif
220 
221 #if OTBR_ENABLE_REST_SERVER
222     /**
223      * Get the rest web server the application is using.
224      *
225      * @returns The rest web server.
226      */
GetRestWebServer(void)227     rest::RestWebServer &GetRestWebServer(void)
228     {
229         return mRestWebServer;
230     }
231 #endif
232 
233 #if OTBR_ENABLE_DBUS_SERVER
234     /**
235      * Get the DBus agent the application is using.
236      *
237      * @returns The DBus agent.
238      */
GetDBusAgent(void)239     DBus::DBusAgent &GetDBusAgent(void)
240     {
241         return mDBusAgent;
242     }
243 #endif
244 
245     /**
246      * This method handles mDNS publisher's state changes.
247      *
248      * @param[in] aState  The state of mDNS publisher.
249      *
250      */
251     void HandleMdnsState(Mdns::Publisher::State aState);
252 
253 private:
254     // Default poll timeout.
255     static const struct timeval kPollTimeout;
256 
257     static void HandleSignal(int aSignal);
258 
259     std::string mInterfaceName;
260 #if __linux__
261     otbr::Utils::InfraLinkSelector mInfraLinkSelector;
262 #endif
263     const char               *mBackboneInterfaceName;
264     Ncp::ControllerOpenThread mNcp;
265 #if OTBR_ENABLE_MDNS
266     std::unique_ptr<Mdns::Publisher> mPublisher;
267 #endif
268 #if OTBR_ENABLE_BORDER_AGENT
269     BorderAgent mBorderAgent;
270 #endif
271 #if OTBR_ENABLE_BACKBONE_ROUTER
272     BackboneRouter::BackboneAgent mBackboneAgent;
273 #endif
274 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
275     AdvertisingProxy mAdvertisingProxy;
276 #endif
277 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
278     Dnssd::DiscoveryProxy mDiscoveryProxy;
279 #endif
280 #if OTBR_ENABLE_TREL
281     TrelDnssd::TrelDnssd mTrelDnssd;
282 #endif
283 #if OTBR_ENABLE_OPENWRT
284     ubus::UBusAgent mUbusAgent;
285 #endif
286 #if OTBR_ENABLE_REST_SERVER
287     rest::RestWebServer mRestWebServer;
288 #endif
289 #if OTBR_ENABLE_DBUS_SERVER
290     DBus::DBusAgent mDBusAgent;
291 #endif
292 #if OTBR_ENABLE_VENDOR_SERVER
293     std::shared_ptr<vendor::VendorServer> mVendorServer;
294 #endif
295 
296     static std::atomic_bool sShouldTerminate;
297 };
298 
299 /**
300  * @}
301  */
302 
303 } // namespace otbr
304 
305 #endif // OTBR_AGENT_APPLICATION_HPP_
306