diff options
author | Simon Lin <simonlin@google.com> | 2021-12-15 13:14:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-14 21:14:20 -0800 |
commit | 975ace0e93d72fa020d7f310f2695817d5d59c87 (patch) | |
tree | 3d2a86549b62dea6eb4f45a00b2135709386dbf1 /src/agent | |
parent | 174f29265a0392e1a4729a17215891685ed2e708 (diff) | |
download | ot-br-posix-975ace0e93d72fa020d7f310f2695817d5d59c87.tar.gz |
[main] add `Application` class (#1146)
This commit adds the `Application` class for better managing different
program components.
Diffstat (limited to 'src/agent')
-rw-r--r-- | src/agent/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/agent/application.cpp | 125 | ||||
-rw-r--r-- | src/agent/application.hpp | 127 | ||||
-rw-r--r-- | src/agent/main.cpp | 98 |
4 files changed, 259 insertions, 92 deletions
diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt index d204a1e4..45c0aceb 100644 --- a/src/agent/CMakeLists.txt +++ b/src/agent/CMakeLists.txt @@ -27,6 +27,7 @@ # add_executable(otbr-agent + application.cpp main.cpp uris.hpp instance_params.cpp diff --git a/src/agent/application.cpp b/src/agent/application.cpp new file mode 100644 index 00000000..a25002b7 --- /dev/null +++ b/src/agent/application.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * The file implements the OTBR Agent. + */ + +#define OTBR_LOG_TAG "APP" + +#include "agent/application.hpp" +#include "common/code_utils.hpp" +#include "common/mainloop_manager.hpp" + +namespace otbr { + +bool Application::sShouldTerminate = false; +const struct timeval Application::kPollTimeout = {10, 0}; + +Application::Application(Ncp::ControllerOpenThread &aOpenThread) + : mBorderAgent(aOpenThread) +#if OTBR_ENABLE_OPENWRT + , mUbusAgent(aOpenThread) +#endif +#if OTBR_ENABLE_REST_SERVER + , mRestWebServer(aOpenThread) +#endif +#if OTBR_ENABLE_DBUS_SERVER + , mDBusAgent(aOpenThread) +#endif +#if OTBR_ENABLE_VENDOR_SERVER + , mVendorServer(aOpenThread) +#endif +{ +} + +void Application::Init(void) +{ + mBorderAgent.Init(); +#if OTBR_ENABLE_OPENWRT + mUbusAgent.Init(); +#endif +#if OTBR_ENABLE_REST_SERVER + mRestWebServer.Init(); +#endif +#if OTBR_ENABLE_DBUS_SERVER + mDBusAgent.Init(); +#endif +#if OTBR_ENABLE_VENDOR_SERVER + mVendorServer.Init(); +#endif +} + +otbrError Application::Run(void) +{ + otbrError error = OTBR_ERROR_NONE; + + otbrLogInfo("Border router agent started."); + // allow quitting elegantly + signal(SIGTERM, HandleSignal); + + while (!sShouldTerminate) + { + otbr::MainloopContext mainloop; + int rval; + + mainloop.mMaxFd = -1; + mainloop.mTimeout = kPollTimeout; + + FD_ZERO(&mainloop.mReadFdSet); + FD_ZERO(&mainloop.mWriteFdSet); + FD_ZERO(&mainloop.mErrorFdSet); + + MainloopManager::GetInstance().Update(mainloop); + + rval = select(mainloop.mMaxFd + 1, &mainloop.mReadFdSet, &mainloop.mWriteFdSet, &mainloop.mErrorFdSet, + &mainloop.mTimeout); + + if (rval >= 0) + { + MainloopManager::GetInstance().Process(mainloop); + } + else if (errno != EINTR) + { + error = OTBR_ERROR_ERRNO; + otbrLogErr("select() failed: %s", strerror(errno)); + break; + } + } + + return error; +} + +void Application::HandleSignal(int aSignal) +{ + sShouldTerminate = true; + signal(aSignal, SIG_DFL); +} + +} // namespace otbr diff --git a/src/agent/application.hpp b/src/agent/application.hpp new file mode 100644 index 00000000..80f10327 --- /dev/null +++ b/src/agent/application.hpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2021, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes definition for OTBR Agent. + */ + +#ifndef OTBR_AGENT_APPLICATION_HPP_ +#define OTBR_AGENT_APPLICATION_HPP_ + +#include <signal.h> +#include <stdint.h> +#include <vector> + +#include "border_agent/border_agent.hpp" +#include "ncp/ncp_openthread.hpp" + +#if OTBR_ENABLE_REST_SERVER +#include "rest/rest_web_server.hpp" +#endif +#if OTBR_ENABLE_DBUS_SERVER +#include "dbus/server/dbus_agent.hpp" +#endif +#if OTBR_ENABLE_OPENWRT +#include "openwrt/ubus/otubus.hpp" +#endif +#if OTBR_ENABLE_VENDOR_SERVER +#include "agent/vendor.hpp" +#endif + +namespace otbr { + +/** + * @addtogroup border-router-agent + * + * @brief + * This module includes definition for OTBR application. + * + * @{ + */ + +/** + * This class implements OTBR application management. + * + */ +class Application +{ +public: + /** + * This constructor initializes the Application instance. + * + * @param[in] aOpenThread A reference to the OpenThread instance. + * + */ + explicit Application(Ncp::ControllerOpenThread &aOpenThread); + + /** + * This method initializes the Application instance. + * + */ + void Init(void); + + /** + * This method runs the application until exit. + * + * @retval OTBR_ERROR_NONE The application exited without any error. + * @retval OTBR_ERROR_ERRNO The application exited with some system error. + * + */ + otbrError Run(void); + +private: + // Default poll timeout. + static const struct timeval kPollTimeout; + + static void HandleSignal(int aSignal); + + BorderAgent mBorderAgent; +#if OTBR_ENABLE_OPENWRT + ubus::UBusAgent mUbusAgent; +#endif +#if OTBR_ENABLE_REST_SERVER + rest::RestWebServer mRestWebServer; +#endif +#if OTBR_ENABLE_DBUS_SERVER + DBus::DBusAgent mDBusAgent; +#endif +#if OTBR_ENABLE_VENDOR_SERVER + vendor::VendorServer mVendorServer; +#endif + + static bool sShouldTerminate; +}; + +/** + * @} + */ + +} // namespace otbr + +#endif // OTBR_AGENT_APPLICATION_HPP_ diff --git a/src/agent/main.cpp b/src/agent/main.cpp index 5ec22df9..0d055080 100644 --- a/src/agent/main.cpp +++ b/src/agent/main.cpp @@ -31,11 +31,8 @@ #include <fstream> #include <mutex> #include <sstream> -#include <thread> -#include <errno.h> #include <getopt.h> -#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -49,29 +46,13 @@ #include <cutils/properties.h> #endif +#include "agent/application.hpp" #include "border_agent/border_agent.hpp" #include "common/code_utils.hpp" #include "common/logging.hpp" #include "common/mainloop.hpp" -#include "common/mainloop_manager.hpp" #include "common/types.hpp" #include "ncp/ncp_openthread.hpp" -#if OTBR_ENABLE_REST_SERVER -#include "rest/rest_web_server.hpp" -using otbr::rest::RestWebServer; -#endif -#if OTBR_ENABLE_DBUS_SERVER -#include "dbus/server/dbus_agent.hpp" -using otbr::DBus::DBusAgent; -#endif -#if OTBR_ENABLE_OPENWRT -#include "openwrt/ubus/otubus.hpp" -using otbr::ubus::UBusAgent; -#endif -#if OTBR_ENABLE_VENDOR_SERVER -#include "agent/vendor.hpp" -using otbr::vendor::VendorServer; -#endif static const char kSyslogIdent[] = "otbr-agent"; static const char kDefaultInterfaceName[] = "wpan0"; @@ -88,16 +69,10 @@ enum OTBR_OPT_RADIO_VERSION, }; -using otbr::MainloopManager; - static jmp_buf sResetJump; -static bool sShouldTerminate = false; - -void __gcov_flush(); -// Default poll timeout. -static const struct timeval kPollTimeout = {10, 0}; -static const struct option kOptions[] = { +void __gcov_flush(); +static const struct option kOptions[] = { {"backbone-ifname", required_argument, nullptr, OTBR_OPT_BACKBONE_INTERFACE_NAME}, {"debug-level", required_argument, nullptr, OTBR_OPT_DEBUG_LEVEL}, {"help", no_argument, nullptr, OTBR_OPT_HELP}, @@ -107,74 +82,13 @@ static const struct option kOptions[] = { {"radio-version", no_argument, nullptr, OTBR_OPT_RADIO_VERSION}, {0, 0, 0, 0}}; -static void HandleSignal(int aSignal) -{ - sShouldTerminate = true; - signal(aSignal, SIG_DFL); -} - static int Mainloop(otbr::Ncp::ControllerOpenThread &aOpenThread) { - int error = EXIT_SUCCESS; - - otbr::BorderAgent borderAgent{aOpenThread}; - - borderAgent.Init(); - -#if OTBR_ENABLE_OPENWRT - UBusAgent ubusAgent{aOpenThread}; - ubusAgent.Init(); -#endif - -#if OTBR_ENABLE_REST_SERVER - RestWebServer restWebServer{aOpenThread}; - restWebServer.Init(); -#endif - -#if OTBR_ENABLE_DBUS_SERVER - DBusAgent dbusAgent{aOpenThread}; - dbusAgent.Init(); -#endif - -#if OTBR_ENABLE_VENDOR_SERVER - VendorServer vendorServer{aOpenThread}; - vendorServer.Init(); -#endif - - otbrLogInfo("Border router agent started."); - // allow quitting elegantly - signal(SIGTERM, HandleSignal); - - while (!sShouldTerminate) - { - otbr::MainloopContext mainloop; - int rval; - - mainloop.mMaxFd = -1; - mainloop.mTimeout = kPollTimeout; - - FD_ZERO(&mainloop.mReadFdSet); - FD_ZERO(&mainloop.mWriteFdSet); - FD_ZERO(&mainloop.mErrorFdSet); + otbr::Application app(aOpenThread); - MainloopManager::GetInstance().Update(mainloop); - - rval = select(mainloop.mMaxFd + 1, &mainloop.mReadFdSet, &mainloop.mWriteFdSet, &mainloop.mErrorFdSet, - &mainloop.mTimeout); - - if (rval >= 0) - { - MainloopManager::GetInstance().Process(mainloop); - } - else if (errno != EINTR) - { - error = OTBR_ERROR_ERRNO; - otbrLogErr("select() failed: %s", strerror(errno)); - break; - } - } + app.Init(); - return error; + return app.Run(); } static void PrintHelp(const char *aProgramName) |