xref: /aosp_15_r20/external/ot-br-posix/src/android/otdaemon_server.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker /*
2*4a64e381SAndroid Build Coastguard Worker  *    Copyright (c) 2023, The OpenThread Authors.
3*4a64e381SAndroid Build Coastguard Worker  *    All rights reserved.
4*4a64e381SAndroid Build Coastguard Worker  *
5*4a64e381SAndroid Build Coastguard Worker  *    Redistribution and use in source and binary forms, with or without
6*4a64e381SAndroid Build Coastguard Worker  *    modification, are permitted provided that the following conditions are met:
7*4a64e381SAndroid Build Coastguard Worker  *    1. Redistributions of source code must retain the above copyright
8*4a64e381SAndroid Build Coastguard Worker  *       notice, this list of conditions and the following disclaimer.
9*4a64e381SAndroid Build Coastguard Worker  *    2. Redistributions in binary form must reproduce the above copyright
10*4a64e381SAndroid Build Coastguard Worker  *       notice, this list of conditions and the following disclaimer in the
11*4a64e381SAndroid Build Coastguard Worker  *       documentation and/or other materials provided with the distribution.
12*4a64e381SAndroid Build Coastguard Worker  *    3. Neither the name of the copyright holder nor the
13*4a64e381SAndroid Build Coastguard Worker  *       names of its contributors may be used to endorse or promote products
14*4a64e381SAndroid Build Coastguard Worker  *       derived from this software without specific prior written permission.
15*4a64e381SAndroid Build Coastguard Worker  *
16*4a64e381SAndroid Build Coastguard Worker  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*4a64e381SAndroid Build Coastguard Worker  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*4a64e381SAndroid Build Coastguard Worker  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*4a64e381SAndroid Build Coastguard Worker  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*4a64e381SAndroid Build Coastguard Worker  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*4a64e381SAndroid Build Coastguard Worker  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*4a64e381SAndroid Build Coastguard Worker  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*4a64e381SAndroid Build Coastguard Worker  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*4a64e381SAndroid Build Coastguard Worker  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*4a64e381SAndroid Build Coastguard Worker  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*4a64e381SAndroid Build Coastguard Worker  *    POSSIBILITY OF SUCH DAMAGE.
27*4a64e381SAndroid Build Coastguard Worker  */
28*4a64e381SAndroid Build Coastguard Worker 
29*4a64e381SAndroid Build Coastguard Worker #ifndef OTBR_ANDROID_BINDER_SERVER_HPP_
30*4a64e381SAndroid Build Coastguard Worker #define OTBR_ANDROID_BINDER_SERVER_HPP_
31*4a64e381SAndroid Build Coastguard Worker 
32*4a64e381SAndroid Build Coastguard Worker #include <functional>
33*4a64e381SAndroid Build Coastguard Worker #include <memory>
34*4a64e381SAndroid Build Coastguard Worker #include <vector>
35*4a64e381SAndroid Build Coastguard Worker 
36*4a64e381SAndroid Build Coastguard Worker #include <openthread/instance.h>
37*4a64e381SAndroid Build Coastguard Worker #include <openthread/ip6.h>
38*4a64e381SAndroid Build Coastguard Worker 
39*4a64e381SAndroid Build Coastguard Worker #include "common_utils.hpp"
40*4a64e381SAndroid Build Coastguard Worker #include "agent/vendor.hpp"
41*4a64e381SAndroid Build Coastguard Worker #include "android/android_thread_host.hpp"
42*4a64e381SAndroid Build Coastguard Worker #include "android/common_utils.hpp"
43*4a64e381SAndroid Build Coastguard Worker #include "android/mdns_publisher.hpp"
44*4a64e381SAndroid Build Coastguard Worker #include "common/mainloop.hpp"
45*4a64e381SAndroid Build Coastguard Worker #include "common/time.hpp"
46*4a64e381SAndroid Build Coastguard Worker #include "ncp/rcp_host.hpp"
47*4a64e381SAndroid Build Coastguard Worker 
48*4a64e381SAndroid Build Coastguard Worker namespace otbr {
49*4a64e381SAndroid Build Coastguard Worker namespace Android {
50*4a64e381SAndroid Build Coastguard Worker 
51*4a64e381SAndroid Build Coastguard Worker class OtDaemonServer : public BnOtDaemon, public MainloopProcessor, public vendor::VendorServer
52*4a64e381SAndroid Build Coastguard Worker {
53*4a64e381SAndroid Build Coastguard Worker public:
54*4a64e381SAndroid Build Coastguard Worker     OtDaemonServer(otbr::Ncp::RcpHost    &aRcpHost,
55*4a64e381SAndroid Build Coastguard Worker                    otbr::Mdns::Publisher &aMdnsPublisher,
56*4a64e381SAndroid Build Coastguard Worker                    otbr::BorderAgent     &aBorderAgent);
57*4a64e381SAndroid Build Coastguard Worker     virtual ~OtDaemonServer(void) = default;
58*4a64e381SAndroid Build Coastguard Worker 
59*4a64e381SAndroid Build Coastguard Worker     // Disallow copy and assign.
60*4a64e381SAndroid Build Coastguard Worker     OtDaemonServer(const OtDaemonServer &) = delete;
61*4a64e381SAndroid Build Coastguard Worker     void operator=(const OtDaemonServer &) = delete;
62*4a64e381SAndroid Build Coastguard Worker 
63*4a64e381SAndroid Build Coastguard Worker     // Dump information for debugging.
64*4a64e381SAndroid Build Coastguard Worker     binder_status_t dump(int aFd, const char **aArgs, uint32_t aNumArgs) override;
65*4a64e381SAndroid Build Coastguard Worker 
Get(void)66*4a64e381SAndroid Build Coastguard Worker     static OtDaemonServer *Get(void) { return sOtDaemonServer; }
67*4a64e381SAndroid Build Coastguard Worker 
68*4a64e381SAndroid Build Coastguard Worker private:
69*4a64e381SAndroid Build Coastguard Worker     using LeaveCallback = std::function<void()>;
70*4a64e381SAndroid Build Coastguard Worker 
71*4a64e381SAndroid Build Coastguard Worker     otInstance *GetOtInstance(void);
72*4a64e381SAndroid Build Coastguard Worker 
73*4a64e381SAndroid Build Coastguard Worker     // Implements vendor::VendorServer
74*4a64e381SAndroid Build Coastguard Worker 
75*4a64e381SAndroid Build Coastguard Worker     void Init(void) override;
76*4a64e381SAndroid Build Coastguard Worker 
77*4a64e381SAndroid Build Coastguard Worker     // Implements MainloopProcessor
78*4a64e381SAndroid Build Coastguard Worker 
79*4a64e381SAndroid Build Coastguard Worker     void Update(MainloopContext &aMainloop) override;
80*4a64e381SAndroid Build Coastguard Worker     void Process(const MainloopContext &aMainloop) override;
81*4a64e381SAndroid Build Coastguard Worker 
82*4a64e381SAndroid Build Coastguard Worker     // Creates AndroidThreadHost instance
83*4a64e381SAndroid Build Coastguard Worker     std::unique_ptr<AndroidThreadHost> CreateAndroidHost(void);
84*4a64e381SAndroid Build Coastguard Worker 
85*4a64e381SAndroid Build Coastguard Worker     // Implements IOtDaemon.aidl
86*4a64e381SAndroid Build Coastguard Worker 
87*4a64e381SAndroid Build Coastguard Worker     Status initialize(const bool                                aEnabled,
88*4a64e381SAndroid Build Coastguard Worker                       const OtDaemonConfiguration              &aConfiguration,
89*4a64e381SAndroid Build Coastguard Worker                       const ScopedFileDescriptor               &aTunFd,
90*4a64e381SAndroid Build Coastguard Worker                       const std::shared_ptr<INsdPublisher>     &aINsdPublisher,
91*4a64e381SAndroid Build Coastguard Worker                       const MeshcopTxtAttributes               &aMeshcopTxts,
92*4a64e381SAndroid Build Coastguard Worker                       const std::string                        &aCountryCode,
93*4a64e381SAndroid Build Coastguard Worker                       const bool                                aTrelEnabled,
94*4a64e381SAndroid Build Coastguard Worker                       const std::shared_ptr<IOtDaemonCallback> &aCallback) override;
95*4a64e381SAndroid Build Coastguard Worker     void   initializeInternal(const bool                                aEnabled,
96*4a64e381SAndroid Build Coastguard Worker                               const OtDaemonConfiguration              &aConfiguration,
97*4a64e381SAndroid Build Coastguard Worker                               const std::shared_ptr<INsdPublisher>     &aINsdPublisher,
98*4a64e381SAndroid Build Coastguard Worker                               const MeshcopTxtAttributes               &aMeshcopTxts,
99*4a64e381SAndroid Build Coastguard Worker                               const std::string                        &aCountryCode,
100*4a64e381SAndroid Build Coastguard Worker                               const bool                                aTrelEnabled,
101*4a64e381SAndroid Build Coastguard Worker                               const std::shared_ptr<IOtDaemonCallback> &aCallback);
102*4a64e381SAndroid Build Coastguard Worker     Status terminate(void) override;
103*4a64e381SAndroid Build Coastguard Worker     Status setThreadEnabled(const bool aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
104*4a64e381SAndroid Build Coastguard Worker     void   setThreadEnabledInternal(const bool aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
105*4a64e381SAndroid Build Coastguard Worker     Status registerStateCallback(const std::shared_ptr<IOtDaemonCallback> &aCallback, int64_t aListenerId) override;
106*4a64e381SAndroid Build Coastguard Worker     void   registerStateCallbackInternal(const std::shared_ptr<IOtDaemonCallback> &aCallback, int64_t aListenerId);
107*4a64e381SAndroid Build Coastguard Worker     bool   isAttached(void);
108*4a64e381SAndroid Build Coastguard Worker     Status join(const std::vector<uint8_t>               &aActiveOpDatasetTlvs,
109*4a64e381SAndroid Build Coastguard Worker                 const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
110*4a64e381SAndroid Build Coastguard Worker     void   joinInternal(const std::vector<uint8_t>               &aActiveOpDatasetTlvs,
111*4a64e381SAndroid Build Coastguard Worker                         const std::shared_ptr<IOtStatusReceiver> &aReceiver);
112*4a64e381SAndroid Build Coastguard Worker     Status leave(bool aEraseDataset, const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
113*4a64e381SAndroid Build Coastguard Worker     void   leaveInternal(bool aEraseDataset, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
114*4a64e381SAndroid Build Coastguard Worker     Status scheduleMigration(const std::vector<uint8_t>               &aPendingOpDatasetTlvs,
115*4a64e381SAndroid Build Coastguard Worker                              const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
116*4a64e381SAndroid Build Coastguard Worker     void   scheduleMigrationInternal(const std::vector<uint8_t>               &aPendingOpDatasetTlvs,
117*4a64e381SAndroid Build Coastguard Worker                                      const std::shared_ptr<IOtStatusReceiver> &aReceiver);
118*4a64e381SAndroid Build Coastguard Worker     Status setCountryCode(const std::string &aCountryCode, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
119*4a64e381SAndroid Build Coastguard Worker     void   setCountryCodeInternal(const std::string &aCountryCode, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
120*4a64e381SAndroid Build Coastguard Worker     Status setChannelMaxPowers(const std::vector<ChannelMaxPower>       &aChannelMaxPowers,
121*4a64e381SAndroid Build Coastguard Worker                                const std::shared_ptr<IOtStatusReceiver> &aReceiver);
122*4a64e381SAndroid Build Coastguard Worker     Status setChannelMaxPowersInternal(const std::vector<ChannelMaxPower>       &aChannelMaxPowers,
123*4a64e381SAndroid Build Coastguard Worker                                        const std::shared_ptr<IOtStatusReceiver> &aReceiver);
124*4a64e381SAndroid Build Coastguard Worker     Status setConfiguration(const OtDaemonConfiguration              &aConfiguration,
125*4a64e381SAndroid Build Coastguard Worker                             const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
126*4a64e381SAndroid Build Coastguard Worker     Status setInfraLinkInterfaceName(const std::optional<std::string>         &aInterfaceName,
127*4a64e381SAndroid Build Coastguard Worker                                      const ScopedFileDescriptor               &aIcmp6Socket,
128*4a64e381SAndroid Build Coastguard Worker                                      const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
129*4a64e381SAndroid Build Coastguard Worker     Status setInfraLinkNat64Prefix(const std::optional<std::string>         &aNat64Prefix,
130*4a64e381SAndroid Build Coastguard Worker                                    const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
131*4a64e381SAndroid Build Coastguard Worker     void   setInfraLinkNat64PrefixInternal(const std::string                        &aNat64Prefix,
132*4a64e381SAndroid Build Coastguard Worker                                            const std::shared_ptr<IOtStatusReceiver> &aReceiver);
133*4a64e381SAndroid Build Coastguard Worker     Status setNat64Cidr(const std::optional<std::string>         &aNat64Cidr,
134*4a64e381SAndroid Build Coastguard Worker                         const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
135*4a64e381SAndroid Build Coastguard Worker     void   setNat64CidrInternal(const std::optional<std::string>         &aNat64Cidr,
136*4a64e381SAndroid Build Coastguard Worker                                 const std::shared_ptr<IOtStatusReceiver> &aReceiver);
137*4a64e381SAndroid Build Coastguard Worker     Status setInfraLinkDnsServers(const std::vector<std::string>           &aDnsServers,
138*4a64e381SAndroid Build Coastguard Worker                                   const std::shared_ptr<IOtStatusReceiver> &aReceiver);
139*4a64e381SAndroid Build Coastguard Worker     void   setInfraLinkDnsServersInternal(const std::vector<std::string>           &aDnsServers,
140*4a64e381SAndroid Build Coastguard Worker                                           const std::shared_ptr<IOtStatusReceiver> &aReceiver);
141*4a64e381SAndroid Build Coastguard Worker     Status getChannelMasks(const std::shared_ptr<IChannelMasksReceiver> &aReceiver) override;
142*4a64e381SAndroid Build Coastguard Worker     void   getChannelMasksInternal(const std::shared_ptr<IChannelMasksReceiver> &aReceiver);
143*4a64e381SAndroid Build Coastguard Worker     Status runOtCtlCommand(const std::string                        &aCommand,
144*4a64e381SAndroid Build Coastguard Worker                            const bool                                aIsInteractive,
145*4a64e381SAndroid Build Coastguard Worker                            const std::shared_ptr<IOtOutputReceiver> &aReceiver);
146*4a64e381SAndroid Build Coastguard Worker     void   runOtCtlCommandInternal(const std::string                        &aCommand,
147*4a64e381SAndroid Build Coastguard Worker                                    const bool                                aIsInteractive,
148*4a64e381SAndroid Build Coastguard Worker                                    const std::shared_ptr<IOtOutputReceiver> &aReceiver);
149*4a64e381SAndroid Build Coastguard Worker     Status activateEphemeralKeyMode(const int64_t                             aLifetimeMillis,
150*4a64e381SAndroid Build Coastguard Worker                                     const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
151*4a64e381SAndroid Build Coastguard Worker     void   activateEphemeralKeyModeInternal(const int64_t                             aLifetimeMillis,
152*4a64e381SAndroid Build Coastguard Worker                                             const std::shared_ptr<IOtStatusReceiver> &aReceiver);
153*4a64e381SAndroid Build Coastguard Worker     Status deactivateEphemeralKeyMode(const std::shared_ptr<IOtStatusReceiver> &aReceiver) override;
154*4a64e381SAndroid Build Coastguard Worker     void   deactivateEphemeralKeyModeInternal(const std::shared_ptr<IOtStatusReceiver> &aReceiver);
155*4a64e381SAndroid Build Coastguard Worker 
156*4a64e381SAndroid Build Coastguard Worker     bool        RefreshOtDaemonState(otChangedFlags aFlags);
157*4a64e381SAndroid Build Coastguard Worker     void        LeaveGracefully(const LeaveCallback &aReceiver);
158*4a64e381SAndroid Build Coastguard Worker     void        FinishLeave(bool aEraseDataset, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
159*4a64e381SAndroid Build Coastguard Worker     static void DetachGracefullyCallback(void *aBinderServer);
160*4a64e381SAndroid Build Coastguard Worker     void        DetachGracefullyCallback(void);
161*4a64e381SAndroid Build Coastguard Worker     static void SendMgmtPendingSetCallback(otError aResult, void *aBinderServer);
162*4a64e381SAndroid Build Coastguard Worker 
163*4a64e381SAndroid Build Coastguard Worker     static void         BinderDeathCallback(void *aBinderServer);
164*4a64e381SAndroid Build Coastguard Worker     void                StateCallback(otChangedFlags aFlags);
165*4a64e381SAndroid Build Coastguard Worker     static void         AddressCallback(const otIp6AddressInfo *aAddressInfo, bool aIsAdded, void *aBinderServer);
166*4a64e381SAndroid Build Coastguard Worker     static void         ReceiveCallback(otMessage *aMessage, void *aBinderServer);
167*4a64e381SAndroid Build Coastguard Worker     void                ReceiveCallback(otMessage *aMessage);
168*4a64e381SAndroid Build Coastguard Worker     void                TransmitCallback(void);
169*4a64e381SAndroid Build Coastguard Worker     BackboneRouterState GetBackboneRouterState(void);
170*4a64e381SAndroid Build Coastguard Worker     static void         HandleBackboneMulticastListenerEvent(void                                  *aBinderServer,
171*4a64e381SAndroid Build Coastguard Worker                                                              otBackboneRouterMulticastListenerEvent aEvent,
172*4a64e381SAndroid Build Coastguard Worker                                                              const otIp6Address                    *aAddress);
173*4a64e381SAndroid Build Coastguard Worker     void                PushTelemetryIfConditionMatch();
174*4a64e381SAndroid Build Coastguard Worker     bool                RefreshOnMeshPrefixes();
175*4a64e381SAndroid Build Coastguard Worker     Ipv6AddressInfo     ConvertToAddressInfo(const otNetifAddress &aAddress);
176*4a64e381SAndroid Build Coastguard Worker     Ipv6AddressInfo     ConvertToAddressInfo(const otNetifMulticastAddress &aAddress);
177*4a64e381SAndroid Build Coastguard Worker     void        UpdateThreadEnabledState(const int aEnabled, const std::shared_ptr<IOtStatusReceiver> &aReceiver);
178*4a64e381SAndroid Build Coastguard Worker     void        EnableThread(const std::shared_ptr<IOtStatusReceiver> &aReceiver);
179*4a64e381SAndroid Build Coastguard Worker     static void HandleEpskcStateChanged(void *aBinderServer);
180*4a64e381SAndroid Build Coastguard Worker     void        HandleEpskcStateChanged(void);
181*4a64e381SAndroid Build Coastguard Worker     int         GetEphemeralKeyState(void);
182*4a64e381SAndroid Build Coastguard Worker     void        NotifyStateChanged(int64_t aListenerId);
183*4a64e381SAndroid Build Coastguard Worker 
184*4a64e381SAndroid Build Coastguard Worker     static OtDaemonServer *sOtDaemonServer;
185*4a64e381SAndroid Build Coastguard Worker 
186*4a64e381SAndroid Build Coastguard Worker     otbr::Ncp::RcpHost                &mHost;
187*4a64e381SAndroid Build Coastguard Worker     std::unique_ptr<AndroidThreadHost> mAndroidHost;
188*4a64e381SAndroid Build Coastguard Worker     MdnsPublisher                     &mMdnsPublisher;
189*4a64e381SAndroid Build Coastguard Worker     otbr::BorderAgent                 &mBorderAgent;
190*4a64e381SAndroid Build Coastguard Worker     std::shared_ptr<INsdPublisher>     mINsdPublisher;
191*4a64e381SAndroid Build Coastguard Worker     MeshcopTxtAttributes               mMeshcopTxts;
192*4a64e381SAndroid Build Coastguard Worker     TaskRunner                         mTaskRunner;
193*4a64e381SAndroid Build Coastguard Worker     ScopedFileDescriptor               mTunFd;
194*4a64e381SAndroid Build Coastguard Worker     OtDaemonState                      mState;
195*4a64e381SAndroid Build Coastguard Worker     std::shared_ptr<IOtDaemonCallback> mCallback;
196*4a64e381SAndroid Build Coastguard Worker     BinderDeathRecipient               mClientDeathRecipient;
197*4a64e381SAndroid Build Coastguard Worker     std::shared_ptr<IOtStatusReceiver> mJoinReceiver;
198*4a64e381SAndroid Build Coastguard Worker     std::shared_ptr<IOtStatusReceiver> mMigrationReceiver;
199*4a64e381SAndroid Build Coastguard Worker     std::vector<LeaveCallback>         mLeaveCallbacks;
200*4a64e381SAndroid Build Coastguard Worker     std::set<OnMeshPrefixConfig>       mOnMeshPrefixes;
201*4a64e381SAndroid Build Coastguard Worker     int64_t                            mEphemeralKeyExpiryMillis;
202*4a64e381SAndroid Build Coastguard Worker 
203*4a64e381SAndroid Build Coastguard Worker     static constexpr Seconds kTelemetryCheckInterval           = Seconds(600);          // 600 seconds
204*4a64e381SAndroid Build Coastguard Worker     static constexpr Seconds kTelemetryUploadIntervalThreshold = Seconds(60 * 60 * 12); // 12 hours
205*4a64e381SAndroid Build Coastguard Worker };
206*4a64e381SAndroid Build Coastguard Worker 
207*4a64e381SAndroid Build Coastguard Worker } // namespace Android
208*4a64e381SAndroid Build Coastguard Worker } // namespace otbr
209*4a64e381SAndroid Build Coastguard Worker 
210*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_ANDROID_BINDER_SERVER_HPP_
211