1 /* 2 * Copyright (c) 2023, 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 #ifndef OTBR_ANDROID_BINDER_SERVER_HPP_ 30 #define OTBR_ANDROID_BINDER_SERVER_HPP_ 31 32 #include <functional> 33 #include <memory> 34 #include <vector> 35 36 #include <aidl/com/android/server/thread/openthread/BnOtDaemon.h> 37 #include <aidl/com/android/server/thread/openthread/INsdPublisher.h> 38 #include <aidl/com/android/server/thread/openthread/IOtDaemon.h> 39 #include <openthread/instance.h> 40 #include <openthread/ip6.h> 41 42 #include "agent/vendor.hpp" 43 #include "android/mdns_publisher.hpp" 44 #include "common/mainloop.hpp" 45 #include "common/time.hpp" 46 #include "ncp/ncp_openthread.hpp" 47 48 namespace otbr { 49 namespace Android { 50 51 using BinderDeathRecipient = ::ndk::ScopedAIBinder_DeathRecipient; 52 using ScopedFileDescriptor = ::ndk::ScopedFileDescriptor; 53 using Status = ::ndk::ScopedAStatus; 54 using aidl::android::net::thread::ChannelMaxPower; 55 using aidl::com::android::server::thread::openthread::BackboneRouterState; 56 using aidl::com::android::server::thread::openthread::BnOtDaemon; 57 using aidl::com::android::server::thread::openthread::BorderRouterConfigurationParcel; 58 using aidl::com::android::server::thread::openthread::IChannelMasksReceiver; 59 using aidl::com::android::server::thread::openthread::INsdPublisher; 60 using aidl::com::android::server::thread::openthread::IOtDaemon; 61 using aidl::com::android::server::thread::openthread::IOtDaemonCallback; 62 using aidl::com::android::server::thread::openthread::IOtStatusReceiver; 63 using aidl::com::android::server::thread::openthread::Ipv6AddressInfo; 64 using aidl::com::android::server::thread::openthread::MeshcopTxtAttributes; 65 using aidl::com::android::server::thread::openthread::OnMeshPrefixConfig; 66 using aidl::com::android::server::thread::openthread::OtDaemonState; 67 68 class OtDaemonServer : public BnOtDaemon, public MainloopProcessor, public vendor::VendorServer 69 { 70 public: 71 explicit OtDaemonServer(Application &aApplication); 72 virtual ~OtDaemonServer(void) = default; 73 74 // Disallow copy and assign. 75 OtDaemonServer(const OtDaemonServer &) = delete; 76 void operator=(const OtDaemonServer &) = delete; 77 78 // Dump information for debugging. 79 binder_status_t dump(int aFd, const char **aArgs, uint32_t aNumArgs) override; 80 81 private: 82 using LeaveCallback = std::function<void()>; 83 84 otInstance *GetOtInstance(void); 85 86 // Implements vendor::VendorServer 87 88 void Init(void) override; 89 90 // Implements MainloopProcessor 91 92 void Update(MainloopContext &aMainloop) override; 93 void Process(const MainloopContext &aMainloop) override; 94 95 // Implements IOtDaemon.aidl 96 97 Status initialize(const ScopedFileDescriptor &aTunFd, 98 const bool enabled, 99 const std::shared_ptr<INsdPublisher> &aNsdPublisher, 100 const MeshcopTxtAttributes &aMeshcopTxts, 101 const std::shared_ptr<IOtDaemonCallback> &aCallback, 102 const std::string &aCountryCode) override; 103 void initializeInternal(const bool enabled, 104 const std::shared_ptr<INsdPublisher> &aINsdPublisher, 105 const MeshcopTxtAttributes &aMeshcopTxts, 106 const std::shared_ptr<IOtDaemonCallback> &aCallback, 107 const std::string &aCountryCode); 108 Status terminate(void) override; 109 Status setThreadEnabled(const bool enabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver) override; 110 void setThreadEnabledInternal(const bool enabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver); 111 Status registerStateCallback(const std::shared_ptr<IOtDaemonCallback> &aCallback, int64_t listenerId) override; 112 void registerStateCallbackInternal(const std::shared_ptr<IOtDaemonCallback> &aCallback, int64_t listenerId); 113 bool isAttached(void); 114 Status join(const std::vector<uint8_t> &aActiveOpDatasetTlvs, 115 const std::shared_ptr<IOtStatusReceiver> &aReceiver) override; 116 void joinInternal(const std::vector<uint8_t> &aActiveOpDatasetTlvs, 117 const std::shared_ptr<IOtStatusReceiver> &aReceiver); 118 Status leave(const std::shared_ptr<IOtStatusReceiver> &aReceiver) override; 119 void leaveInternal(const std::shared_ptr<IOtStatusReceiver> &aReceiver); 120 Status scheduleMigration(const std::vector<uint8_t> &aPendingOpDatasetTlvs, 121 const std::shared_ptr<IOtStatusReceiver> &aReceiver) override; 122 void scheduleMigrationInternal(const std::vector<uint8_t> &aPendingOpDatasetTlvs, 123 const std::shared_ptr<IOtStatusReceiver> &aReceiver); 124 Status setCountryCode(const std::string &aCountryCode, const std::shared_ptr<IOtStatusReceiver> &aReceiver); 125 void setCountryCodeInternal(const std::string &aCountryCode, const std::shared_ptr<IOtStatusReceiver> &aReceiver); 126 Status setChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers, 127 const std::shared_ptr<IOtStatusReceiver> &aReceiver); 128 Status setChannelMaxPowersInternal(const std::vector<ChannelMaxPower> &aChannelMaxPowers, 129 const std::shared_ptr<IOtStatusReceiver> &aReceiver); 130 Status configureBorderRouter(const BorderRouterConfigurationParcel &aBorderRouterConfiguration, 131 const std::shared_ptr<IOtStatusReceiver> &aReceiver) override; 132 void configureBorderRouterInternal(int aIcmp6SocketFd, 133 const std::string &aInfraInterfaceName, 134 bool aIsBorderRoutingEnabled, 135 bool aIsBorderRouterConfigChanged, 136 const std::shared_ptr<IOtStatusReceiver> &aReceiver); 137 Status getChannelMasks(const std::shared_ptr<IChannelMasksReceiver> &aReceiver) override; 138 void getChannelMasksInternal(const std::shared_ptr<IChannelMasksReceiver> &aReceiver); 139 140 bool RefreshOtDaemonState(otChangedFlags aFlags); 141 void LeaveGracefully(const LeaveCallback &aReceiver); 142 void FinishLeave(const std::shared_ptr<IOtStatusReceiver> &aReceiver); 143 static void DetachGracefullyCallback(void *aBinderServer); 144 void DetachGracefullyCallback(void); 145 static void SendMgmtPendingSetCallback(otError aResult, void *aBinderServer); 146 147 static void BinderDeathCallback(void *aBinderServer); 148 void StateCallback(otChangedFlags aFlags); 149 static void AddressCallback(const otIp6AddressInfo *aAddressInfo, bool aIsAdded, void *aBinderServer); 150 static void ReceiveCallback(otMessage *aMessage, void *aBinderServer); 151 void ReceiveCallback(otMessage *aMessage); 152 void TransmitCallback(void); 153 BackboneRouterState GetBackboneRouterState(void); 154 static void HandleBackboneMulticastListenerEvent(void *aBinderServer, 155 otBackboneRouterMulticastListenerEvent aEvent, 156 const otIp6Address *aAddress); 157 void PushTelemetryIfConditionMatch(); 158 bool RefreshOnMeshPrefixes(); 159 Ipv6AddressInfo ConvertToAddressInfo(const otNetifAddress &aAddress); 160 Ipv6AddressInfo ConvertToAddressInfo(const otNetifMulticastAddress &aAddress); 161 void UpdateThreadEnabledState(const int aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver); 162 void EnableThread(const std::shared_ptr<IOtStatusReceiver> &aReceiver); 163 164 otbr::Application &mApplication; 165 otbr::Ncp::ControllerOpenThread &mNcp; 166 otbr::BorderAgent &mBorderAgent; 167 MdnsPublisher &mMdnsPublisher; 168 std::shared_ptr<INsdPublisher> mINsdPublisher; 169 MeshcopTxtAttributes mMeshcopTxts; 170 TaskRunner mTaskRunner; 171 ScopedFileDescriptor mTunFd; 172 OtDaemonState mState; 173 std::shared_ptr<IOtDaemonCallback> mCallback; 174 BinderDeathRecipient mClientDeathRecipient; 175 std::shared_ptr<IOtStatusReceiver> mJoinReceiver; 176 std::shared_ptr<IOtStatusReceiver> mMigrationReceiver; 177 std::vector<LeaveCallback> mLeaveCallbacks; 178 BorderRouterConfigurationParcel mBorderRouterConfiguration; 179 std::set<OnMeshPrefixConfig> mOnMeshPrefixes; 180 static constexpr Seconds kTelemetryCheckInterval = Seconds(600); // 600 seconds 181 static constexpr Seconds kTelemetryUploadIntervalThreshold = Seconds(60 * 60 * 12); // 12 hours 182 }; 183 184 } // namespace Android 185 } // namespace otbr 186 187 #endif // OTBR_ANDROID_BINDER_SERVER_HPP_ 188