• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <openthread/instance.h>
37 #include <openthread/ip6.h>
38 
39 #include "common_utils.hpp"
40 #include "agent/vendor.hpp"
41 #include "android/android_thread_host.hpp"
42 #include "android/common_utils.hpp"
43 #include "android/mdns_publisher.hpp"
44 #include "common/mainloop.hpp"
45 #include "common/time.hpp"
46 #include "host/rcp_host.hpp"
47 #include "sdp_proxy/advertising_proxy.hpp"
48 
49 namespace otbr {
50 namespace Android {
51 
52 class OtDaemonServer : public BnOtDaemon, public MainloopProcessor, public vendor::VendorServer
53 {
54 public:
55     using ResetThreadHandler = std::function<void()>;
56 
57     OtDaemonServer(otbr::Host::RcpHost    &aRcpHost,
58                    otbr::Mdns::Publisher  &aMdnsPublisher,
59                    otbr::BorderAgent      &aBorderAgent,
60                    otbr::AdvertisingProxy &aAdvProxy,
61                    ResetThreadHandler      aResetThreadHandler);
62     virtual ~OtDaemonServer(void) = default;
63 
64     // Disallow copy and assign.
65     OtDaemonServer(const OtDaemonServer &) = delete;
66     void operator=(const OtDaemonServer &) = delete;
67 
68     // Dump information for debugging.
69     binder_status_t dump(int aFd, const char **aArgs, uint32_t aNumArgs) override;
70 
Get(void)71     static OtDaemonServer *Get(void) { return sOtDaemonServer; }
72 
73 private:
74     using LeaveCallback = std::function<void()>;
75 
76     otInstance *GetOtInstance(void);
77 
78     // Implements vendor::VendorServer
79 
80     void Init(void) override;
81 
82     // Implements MainloopProcessor
83 
84     void Update(MainloopContext &aMainloop) override;
85     void Process(const MainloopContext &aMainloop) override;
86 
87     // Creates AndroidThreadHost instance
88     std::unique_ptr<AndroidThreadHost> CreateAndroidHost(void);
89 
90     // Implements IOtDaemon.aidl
91 
92     Status initialize(const bool                                aEnabled,
93                       const OtDaemonConfiguration              &aConfiguration,
94                       const ScopedFileDescriptor               &aTunFd,
95                       const std::shared_ptr<INsdPublisher>     &aINsdPublisher,
96                       const MeshcopTxtAttributes               &aMeshcopTxts,
97                       const std::string                        &aCountryCode,
98                       const bool                                aTrelEnabled,
99                       const std::shared_ptr<IOtDaemonCallback> &aCallback) override;
100     void   initializeInternal(const bool                                aEnabled,
101                               const OtDaemonConfiguration              &aConfiguration,
102                               const std::shared_ptr<INsdPublisher>     &aINsdPublisher,
103                               const MeshcopTxtAttributes               &aMeshcopTxts,
104                               const std::string                        &aCountryCode,
105                               const bool                                aTrelEnabled,
106                               const std::shared_ptr<IOtDaemonCallback> &aCallback);
107     Status terminate(void) override;
108     Status setThreadEnabled(const bool aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
109     void   setThreadEnabledInternal(const bool aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
110     Status registerStateCallback(const std::shared_ptr<IOtDaemonCallback> &aCallback, int64_t aListenerId) override;
111     void   registerStateCallbackInternal(const std::shared_ptr<IOtDaemonCallback> &aCallback, int64_t aListenerId);
112     bool   isAttached(void);
113     Status join(const std::vector<uint8_t>               &aActiveOpDatasetTlvs,
114                 const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
115     void   joinInternal(const std::vector<uint8_t>               &aActiveOpDatasetTlvs,
116                         const std::shared_ptr<IOtStatusReceiver> &aReceiver);
117 
118     Status leave(bool aEraseDataset, const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
119     void   leaveInternal(bool aEraseDataset, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
120     void   LeaveGracefully(bool aEraseDataset, const std::string &aCallerTag, const LeaveCallback &aReceiver);
121     void   ResetRuntimeStatesAfterLeave();
122 
123     Status scheduleMigration(const std::vector<uint8_t>               &aPendingOpDatasetTlvs,
124                              const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
125     void   scheduleMigrationInternal(const std::vector<uint8_t>               &aPendingOpDatasetTlvs,
126                                      const std::shared_ptr<IOtStatusReceiver> &aReceiver);
127     Status setCountryCode(const std::string &aCountryCode, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
128     void   setCountryCodeInternal(const std::string &aCountryCode, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
129     Status setChannelMaxPowers(const std::vector<ChannelMaxPower>       &aChannelMaxPowers,
130                                const std::shared_ptr<IOtStatusReceiver> &aReceiver);
131     Status setChannelMaxPowersInternal(const std::vector<ChannelMaxPower>       &aChannelMaxPowers,
132                                        const std::shared_ptr<IOtStatusReceiver> &aReceiver);
133     Status setConfiguration(const OtDaemonConfiguration              &aConfiguration,
134                             const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
135     Status setInfraLinkInterfaceName(const std::optional<std::string>         &aInterfaceName,
136                                      const ScopedFileDescriptor               &aIcmp6Socket,
137                                      const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
138     Status setInfraLinkNat64Prefix(const std::optional<std::string>         &aNat64Prefix,
139                                    const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
140     void   setInfraLinkNat64PrefixInternal(const std::string                        &aNat64Prefix,
141                                            const std::shared_ptr<IOtStatusReceiver> &aReceiver);
142     Status setNat64Cidr(const std::optional<std::string>         &aNat64Cidr,
143                         const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
144     void   setNat64CidrInternal(const std::optional<std::string>         &aNat64Cidr,
145                                 const std::shared_ptr<IOtStatusReceiver> &aReceiver);
146     Status setInfraLinkDnsServers(const std::vector<std::string>           &aDnsServers,
147                                   const std::shared_ptr<IOtStatusReceiver> &aReceiver);
148     void   setInfraLinkDnsServersInternal(const std::vector<std::string>           &aDnsServers,
149                                           const std::shared_ptr<IOtStatusReceiver> &aReceiver);
150     Status getChannelMasks(const std::shared_ptr<IChannelMasksReceiver> &aReceiver) override;
151     void   getChannelMasksInternal(const std::shared_ptr<IChannelMasksReceiver> &aReceiver);
152     Status runOtCtlCommand(const std::string                        &aCommand,
153                            const bool                                aIsInteractive,
154                            const std::shared_ptr<IOtOutputReceiver> &aReceiver);
155     void   runOtCtlCommandInternal(const std::string                        &aCommand,
156                                    const bool                                aIsInteractive,
157                                    const std::shared_ptr<IOtOutputReceiver> &aReceiver);
158     Status activateEphemeralKeyMode(const int64_t                             aLifetimeMillis,
159                                     const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
160     void   activateEphemeralKeyModeInternal(const int64_t                             aLifetimeMillis,
161                                             const std::shared_ptr<IOtStatusReceiver> &aReceiver);
162     Status deactivateEphemeralKeyMode(const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
163     void   deactivateEphemeralKeyModeInternal(const std::shared_ptr<IOtStatusReceiver> &aReceiver);
164 
165     bool        RefreshOtDaemonState(otChangedFlags aFlags);
166     static void DetachGracefullyCallback(void *aBinderServer);
167     void        DetachGracefullyCallback(void);
168     static void SendMgmtPendingSetCallback(otError aResult, void *aBinderServer);
169 
170     static void         BinderDeathCallback(void *aBinderServer);
171     void                StateCallback(otChangedFlags aFlags);
172     static void         AddressCallback(const otIp6AddressInfo *aAddressInfo, bool aIsAdded, void *aBinderServer);
173     static void         ReceiveCallback(otMessage *aMessage, void *aBinderServer);
174     void                ReceiveCallback(otMessage *aMessage);
175     void                TransmitCallback(void);
176     BackboneRouterState GetBackboneRouterState(void);
177     static void         HandleBackboneMulticastListenerEvent(void                                  *aBinderServer,
178                                                              otBackboneRouterMulticastListenerEvent aEvent,
179                                                              const otIp6Address                    *aAddress);
180     void                PushTelemetryIfConditionMatch();
181     bool                RefreshOnMeshPrefixes();
182     Ipv6AddressInfo     ConvertToAddressInfo(const otNetifAddress &aAddress);
183     Ipv6AddressInfo     ConvertToAddressInfo(const otNetifMulticastAddress &aAddress);
184     void        UpdateThreadEnabledState(const int aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
185     void        EnableThread(const std::shared_ptr<IOtStatusReceiver> &aReceiver);
186     static void HandleEpskcStateChanged(void *aBinderServer);
187     void        HandleEpskcStateChanged(void);
188     int         GetEphemeralKeyState(void);
189     void        NotifyStateChanged(int64_t aListenerId);
190 
191     static OtDaemonServer *sOtDaemonServer;
192 
193     // Class dependencies
194     otbr::Host::RcpHost               &mHost;
195     std::unique_ptr<AndroidThreadHost> mAndroidHost;
196     MdnsPublisher                     &mMdnsPublisher;
197     otbr::BorderAgent                 &mBorderAgent;
198     otbr::AdvertisingProxy            &mAdvProxy;
199     ResetThreadHandler                 mResetThreadHandler;
200     TaskRunner                         mTaskRunner;
201 
202     // States initialized in initialize()
203     ScopedFileDescriptor               mTunFd;
204     std::shared_ptr<INsdPublisher>     mINsdPublisher;
205     MeshcopTxtAttributes               mMeshcopTxts;
206     std::string                        mCountryCode;
207     bool                               mTrelEnabled = false;
208     std::shared_ptr<IOtDaemonCallback> mCallback;
209     BinderDeathRecipient               mClientDeathRecipient;
210 
211     // Runtime states
212     std::shared_ptr<IOtStatusReceiver> mJoinReceiver;
213     std::shared_ptr<IOtStatusReceiver> mMigrationReceiver;
214     std::vector<LeaveCallback>         mLeaveCallbacks;
215     OtDaemonState                      mState;
216     std::set<OnMeshPrefixConfig>       mOnMeshPrefixes;
217     int64_t                            mEphemeralKeyExpiryMillis;
218 
219     static constexpr Seconds kTelemetryCheckInterval           = Seconds(600);          // 600 seconds
220     static constexpr Seconds kTelemetryUploadIntervalThreshold = Seconds(60 * 60 * 12); // 12 hours
221 };
222 
223 } // namespace Android
224 } // namespace otbr
225 
226 #endif // OTBR_ANDROID_BINDER_SERVER_HPP_
227