xref: /aosp_15_r20/external/ot-br-posix/src/agent/application.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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/rcp_host.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 class Application : private NonCopyable
88 {
89 public:
90     /**
91      * This constructor initializes the Application instance.
92      *
93      * @param[in] aInterfaceName         Name of the Thread network interface.
94      * @param[in] aBackboneInterfaceName Name of the backbone network interface.
95      * @param[in] aRadioUrls             The radio URLs (can be IEEE802.15.4 or TREL radio).
96      * @param[in] aEnableAutoAttach      Whether or not to automatically attach to the saved network.
97      * @param[in] aRestListenAddress     Network address to listen on.
98      * @param[in] aRestListenPort        Network port to listen on.
99      */
100     explicit Application(const std::string               &aInterfaceName,
101                          const std::vector<const char *> &aBackboneInterfaceNames,
102                          const std::vector<const char *> &aRadioUrls,
103                          bool                             aEnableAutoAttach,
104                          const std::string               &aRestListenAddress,
105                          int                              aRestListenPort);
106 
107     /**
108      * This method initializes the Application instance.
109      */
110     void Init(void);
111 
112     /**
113      * This method de-initializes the Application instance.
114      */
115     void Deinit(void);
116 
117     /**
118      * This method runs the application until exit.
119      *
120      * @retval OTBR_ERROR_NONE  The application exited without any error.
121      * @retval OTBR_ERROR_ERRNO The application exited with some system error.
122      */
123     otbrError Run(void);
124 
125     /**
126      * Get the OpenThread controller object the application is using.
127      *
128      * @returns The OpenThread controller object.
129      */
GetHost(void)130     Ncp::ThreadHost &GetHost(void) { return *mHost; }
131 
132 #if OTBR_ENABLE_MDNS
133     /**
134      * Get the Publisher object the application is using.
135      *
136      * @returns The Publisher object.
137      */
GetPublisher(void)138     Mdns::Publisher &GetPublisher(void)
139     {
140         return *mPublisher;
141     }
142 #endif
143 
144 #if OTBR_ENABLE_BORDER_AGENT
145     /**
146      * Get the border agent the application is using.
147      *
148      * @returns The border agent.
149      */
GetBorderAgent(void)150     BorderAgent &GetBorderAgent(void)
151     {
152         return *mBorderAgent;
153     }
154 #endif
155 
156 #if OTBR_ENABLE_BACKBONE_ROUTER
157     /**
158      * Get the backbone agent the application is using.
159      *
160      * @returns The backbone agent.
161      */
GetBackboneAgent(void)162     BackboneRouter::BackboneAgent &GetBackboneAgent(void)
163     {
164         return *mBackboneAgent;
165     }
166 #endif
167 
168 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
169     /**
170      * Get the advertising proxy the application is using.
171      *
172      * @returns The advertising proxy.
173      */
GetAdvertisingProxy(void)174     AdvertisingProxy &GetAdvertisingProxy(void)
175     {
176         return *mAdvertisingProxy;
177     }
178 #endif
179 
180 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
181     /**
182      * Get the discovery proxy the application is using.
183      *
184      * @returns The discovery proxy.
185      */
GetDiscoveryProxy(void)186     Dnssd::DiscoveryProxy &GetDiscoveryProxy(void)
187     {
188         return *mDiscoveryProxy;
189     }
190 #endif
191 
192 #if OTBR_ENABLE_TREL
193     /**
194      * Get the TrelDnssd object the application is using.
195      *
196      * @returns The TrelDnssd.
197      */
GetTrelDnssd(void)198     TrelDnssd::TrelDnssd &GetTrelDnssd(void)
199     {
200         return *mTrelDnssd;
201     }
202 #endif
203 
204 #if OTBR_ENABLE_OPENWRT
205     /**
206      * Get the UBus agent the application is using.
207      *
208      * @returns The UBus agent.
209      */
GetUBusAgent(void)210     ubus::UBusAgent &GetUBusAgent(void)
211     {
212         return *mUbusAgent;
213     }
214 #endif
215 
216 #if OTBR_ENABLE_REST_SERVER
217     /**
218      * Get the rest web server the application is using.
219      *
220      * @returns The rest web server.
221      */
GetRestWebServer(void)222     rest::RestWebServer &GetRestWebServer(void)
223     {
224         return *mRestWebServer;
225     }
226 #endif
227 
228 #if OTBR_ENABLE_DBUS_SERVER
229     /**
230      * Get the DBus agent the application is using.
231      *
232      * @returns The DBus agent.
233      */
GetDBusAgent(void)234     DBus::DBusAgent &GetDBusAgent(void)
235     {
236         return *mDBusAgent;
237     }
238 #endif
239 
240     /**
241      * This method handles mDNS publisher's state changes.
242      *
243      * @param[in] aState  The state of mDNS publisher.
244      */
245     void HandleMdnsState(Mdns::Publisher::State aState);
246 
247 private:
248     // Default poll timeout.
249     static const struct timeval kPollTimeout;
250 
251     static void HandleSignal(int aSignal);
252 
253     void CreateRcpMode(const std::string &aRestListenAddress, int aRestListenPort);
254     void InitRcpMode(void);
255     void DeinitRcpMode(void);
256 
257     void InitNcpMode(void);
258     void DeinitNcpMode(void);
259 
260     std::string mInterfaceName;
261 #if __linux__
262     otbr::Utils::InfraLinkSelector mInfraLinkSelector;
263 #endif
264     const char                      *mBackboneInterfaceName;
265     std::unique_ptr<Ncp::ThreadHost> mHost;
266 #if OTBR_ENABLE_MDNS
267     std::unique_ptr<Mdns::Publisher> mPublisher;
268 #endif
269 #if OTBR_ENABLE_BORDER_AGENT
270     std::unique_ptr<BorderAgent> mBorderAgent;
271 #endif
272 #if OTBR_ENABLE_BACKBONE_ROUTER
273     std::unique_ptr<BackboneRouter::BackboneAgent> mBackboneAgent;
274 #endif
275 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
276     std::unique_ptr<AdvertisingProxy> mAdvertisingProxy;
277 #endif
278 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
279     std::unique_ptr<Dnssd::DiscoveryProxy> mDiscoveryProxy;
280 #endif
281 #if OTBR_ENABLE_TREL
282     std::unique_ptr<TrelDnssd::TrelDnssd> mTrelDnssd;
283 #endif
284 #if OTBR_ENABLE_OPENWRT
285     std::unique_ptr<ubus::UBusAgent> mUbusAgent;
286 #endif
287 #if OTBR_ENABLE_REST_SERVER
288     std::unique_ptr<rest::RestWebServer> mRestWebServer;
289 #endif
290 #if OTBR_ENABLE_DBUS_SERVER
291     std::unique_ptr<DBus::DBusAgent> mDBusAgent;
292 #endif
293 #if OTBR_ENABLE_VENDOR_SERVER
294     std::shared_ptr<vendor::VendorServer> mVendorServer;
295 #endif
296 
297     static std::atomic_bool sShouldTerminate;
298 };
299 
300 /**
301  * @}
302  */
303 
304 } // namespace otbr
305 
306 #endif // OTBR_AGENT_APPLICATION_HPP_
307