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