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 "host/ncp_host.hpp" 48 #include "host/rcp_host.hpp" 49 #if OTBR_ENABLE_BACKBONE_ROUTER 50 #include "backbone_router/backbone_agent.hpp" 51 #endif 52 #if OTBR_ENABLE_REST_SERVER 53 #include "rest/rest_web_server.hpp" 54 #endif 55 #if OTBR_ENABLE_DBUS_SERVER 56 #include "dbus/server/dbus_agent.hpp" 57 #endif 58 #if OTBR_ENABLE_OPENWRT 59 #include "openwrt/ubus/otubus.hpp" 60 #endif 61 #if OTBR_ENABLE_VENDOR_SERVER 62 #include "agent/vendor.hpp" 63 #endif 64 #if OTBR_ENABLE_DNSSD_PLAT 65 #include "host/posix/dnssd.hpp" 66 #endif 67 #include "utils/infra_link_selector.hpp" 68 69 namespace otbr { 70 71 #if OTBR_ENABLE_VENDOR_SERVER 72 namespace vendor { 73 74 class VendorServer; 75 76 } 77 #endif 78 79 /** 80 * @addtogroup border-router-agent 81 * 82 * @brief 83 * This module includes definition for OTBR application. 84 * 85 * @{ 86 */ 87 88 /** 89 * This class implements OTBR application management. 90 */ 91 class Application : private NonCopyable 92 { 93 public: 94 typedef std::function<otbrError(void)> ErrorCondition; 95 96 /** 97 * This constructor initializes the Application instance. 98 * 99 * @param[in] aHost A reference to the ThreadHost object. 100 * @param[in] aInterfaceName Name of the Thread network interface. 101 * @param[in] aBackboneInterfaceName Name of the backbone network interface. 102 * @param[in] aRestListenAddress Network address to listen on. 103 * @param[in] aRestListenPort Network port to listen on. 104 */ 105 explicit Application(Host::ThreadHost &aHost, 106 const std::string &aInterfaceName, 107 const std::string &aBackboneInterfaceName, 108 const std::string &aRestListenAddress, 109 int aRestListenPort); 110 111 /** 112 * This method initializes the Application instance. 113 */ 114 void Init(void); 115 116 /** 117 * This method de-initializes the Application instance. 118 */ 119 void Deinit(void); 120 121 /** 122 * This method sets an error condition for the application. 123 * 124 * If the error condition returns an error other than 'OTBR_ERROR_NONE', the application will 125 * exit the loop in `Run`. 126 * 127 * @param[in] aErrorCondition The error condition. 128 */ SetErrorCondition(ErrorCondition aErrorCondition)129 void SetErrorCondition(ErrorCondition aErrorCondition) { mErrorCondition = aErrorCondition; } 130 131 /** 132 * This method runs the application until exit. 133 * 134 * @retval OTBR_ERROR_NONE The application exited without any error. 135 * @retval OTBR_ERROR_ERRNO The application exited with some system error. 136 */ 137 otbrError Run(void); 138 139 /** 140 * Get the OpenThread controller object the application is using. 141 * 142 * @returns The OpenThread controller object. 143 */ GetHost(void)144 Host::ThreadHost &GetHost(void) { return mHost; } 145 146 #if OTBR_ENABLE_MDNS 147 /** 148 * Get the Publisher object the application is using. 149 * 150 * @returns The Publisher object. 151 */ GetPublisher(void)152 Mdns::Publisher &GetPublisher(void) 153 { 154 return *mPublisher; 155 } 156 #endif 157 158 #if OTBR_ENABLE_BORDER_AGENT 159 /** 160 * Get the border agent the application is using. 161 * 162 * @returns The border agent. 163 */ GetBorderAgent(void)164 BorderAgent &GetBorderAgent(void) 165 { 166 return *mBorderAgent; 167 } 168 #endif 169 170 #if OTBR_ENABLE_BACKBONE_ROUTER 171 /** 172 * Get the backbone agent the application is using. 173 * 174 * @returns The backbone agent. 175 */ GetBackboneAgent(void)176 BackboneRouter::BackboneAgent &GetBackboneAgent(void) 177 { 178 return *mBackboneAgent; 179 } 180 #endif 181 182 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY 183 /** 184 * Get the advertising proxy the application is using. 185 * 186 * @returns The advertising proxy. 187 */ GetAdvertisingProxy(void)188 AdvertisingProxy &GetAdvertisingProxy(void) 189 { 190 return *mAdvertisingProxy; 191 } 192 #endif 193 194 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY 195 /** 196 * Get the discovery proxy the application is using. 197 * 198 * @returns The discovery proxy. 199 */ GetDiscoveryProxy(void)200 Dnssd::DiscoveryProxy &GetDiscoveryProxy(void) 201 { 202 return *mDiscoveryProxy; 203 } 204 #endif 205 206 #if OTBR_ENABLE_TREL 207 /** 208 * Get the TrelDnssd object the application is using. 209 * 210 * @returns The TrelDnssd. 211 */ GetTrelDnssd(void)212 TrelDnssd::TrelDnssd &GetTrelDnssd(void) 213 { 214 return *mTrelDnssd; 215 } 216 #endif 217 218 #if OTBR_ENABLE_OPENWRT 219 /** 220 * Get the UBus agent the application is using. 221 * 222 * @returns The UBus agent. 223 */ GetUBusAgent(void)224 ubus::UBusAgent &GetUBusAgent(void) 225 { 226 return *mUbusAgent; 227 } 228 #endif 229 230 #if OTBR_ENABLE_REST_SERVER 231 /** 232 * Get the rest web server the application is using. 233 * 234 * @returns The rest web server. 235 */ GetRestWebServer(void)236 rest::RestWebServer &GetRestWebServer(void) 237 { 238 return *mRestWebServer; 239 } 240 #endif 241 242 #if OTBR_ENABLE_DBUS_SERVER 243 /** 244 * Get the DBus agent the application is using. 245 * 246 * @returns The DBus agent. 247 */ GetDBusAgent(void)248 DBus::DBusAgent &GetDBusAgent(void) 249 { 250 return *mDBusAgent; 251 } 252 #endif 253 254 private: 255 // Default poll timeout. 256 static const struct timeval kPollTimeout; 257 258 static void HandleSignal(int aSignal); 259 260 void CreateRcpMode(const std::string &aRestListenAddress, int aRestListenPort); 261 void InitRcpMode(void); 262 void DeinitRcpMode(void); 263 264 void InitNcpMode(void); 265 void DeinitNcpMode(void); 266 267 std::string mInterfaceName; 268 const char *mBackboneInterfaceName; 269 Host::ThreadHost &mHost; 270 #if OTBR_ENABLE_MDNS 271 Mdns::StateSubject mMdnsStateSubject; 272 std::unique_ptr<Mdns::Publisher> mPublisher; 273 #endif 274 #if OTBR_ENABLE_DNSSD_PLAT 275 DnssdPlatform mDnssdPlatform; 276 #endif 277 #if OTBR_ENABLE_BORDER_AGENT 278 std::unique_ptr<BorderAgent> mBorderAgent; 279 #endif 280 #if OTBR_ENABLE_BACKBONE_ROUTER 281 std::unique_ptr<BackboneRouter::BackboneAgent> mBackboneAgent; 282 #endif 283 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY 284 std::unique_ptr<AdvertisingProxy> mAdvertisingProxy; 285 #endif 286 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY 287 std::unique_ptr<Dnssd::DiscoveryProxy> mDiscoveryProxy; 288 #endif 289 #if OTBR_ENABLE_TREL 290 std::unique_ptr<TrelDnssd::TrelDnssd> mTrelDnssd; 291 #endif 292 #if OTBR_ENABLE_OPENWRT 293 std::unique_ptr<ubus::UBusAgent> mUbusAgent; 294 #endif 295 #if OTBR_ENABLE_REST_SERVER 296 std::unique_ptr<rest::RestWebServer> mRestWebServer; 297 #endif 298 #if OTBR_ENABLE_DBUS_SERVER 299 std::unique_ptr<DBus::DBusAgent> mDBusAgent; 300 #endif 301 #if OTBR_ENABLE_VENDOR_SERVER 302 std::shared_ptr<vendor::VendorServer> mVendorServer; 303 #endif 304 305 static std::atomic_bool sShouldTerminate; 306 ErrorCondition mErrorCondition; 307 }; 308 309 /** 310 * @} 311 */ 312 313 } // namespace otbr 314 315 #endif // OTBR_AGENT_APPLICATION_HPP_ 316