xref: /aosp_15_r20/external/ot-br-posix/src/ncp/ncp_host.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1 /*
2  *  Copyright (c) 2024, 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 /**
30  * @file
31  *   This file includes definitions of OpenThead Host for NCP.
32  */
33 
34 #ifndef OTBR_AGENT_NCP_HOST_HPP_
35 #define OTBR_AGENT_NCP_HOST_HPP_
36 
37 #include "lib/spinel/coprocessor_type.h"
38 #include "lib/spinel/spinel_driver.hpp"
39 
40 #include "common/mainloop.hpp"
41 #include "ncp/ncp_spinel.hpp"
42 #include "ncp/thread_host.hpp"
43 #include "posix/netif.hpp"
44 
45 namespace otbr {
46 namespace Ncp {
47 
48 /**
49  * This class implements the NetworkProperties under NCP mode.
50  */
51 class NcpNetworkProperties : virtual public NetworkProperties, public PropsObserver
52 {
53 public:
54     /**
55      * Constructor
56      */
57     explicit NcpNetworkProperties(void);
58 
59     // NetworkProperties methods
60     otDeviceRole GetDeviceRole(void) const override;
61     bool         Ip6IsEnabled(void) const override;
62     uint32_t     GetPartitionId(void) const override;
63     void         GetDatasetActiveTlvs(otOperationalDatasetTlvs &aDatasetTlvs) const override;
64     void         GetDatasetPendingTlvs(otOperationalDatasetTlvs &aDatasetTlvs) const override;
65 
66 private:
67     // PropsObserver methods
68     void SetDeviceRole(otDeviceRole aRole) override;
69     void SetDatasetActiveTlvs(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs) override;
70 
71     otDeviceRole             mDeviceRole;
72     otOperationalDatasetTlvs mDatasetActiveTlvs;
73 };
74 
75 class NcpHost : public MainloopProcessor, public ThreadHost, public NcpNetworkProperties
76 {
77 public:
78     /**
79      * Constructor.
80      *
81      * @param[in]   aInterfaceName  A string of the NCP interface name.
82      * @param[in]   aDryRun         TRUE to indicate dry-run mode. FALSE otherwise.
83      */
84     NcpHost(const char *aInterfaceName, bool aDryRun);
85 
86     /**
87      * Destructor.
88      */
89     ~NcpHost(void) override = default;
90 
91     // ThreadHost methods
92     void Join(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs, const AsyncResultReceiver &aReceiver) override;
93     void Leave(const AsyncResultReceiver &aReceiver) override;
94     void ScheduleMigration(const otOperationalDatasetTlvs &aPendingOpDatasetTlvs,
95                            const AsyncResultReceiver       aReceiver) override;
96     void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) override;
97     void SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver) override;
98     void GetChannelMasks(const ChannelMasksReceiver &aReceiver, const AsyncResultReceiver &aErrReceiver) override;
99     void SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers,
100                              const AsyncResultReceiver          &aReceiver) override;
101     void AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback) override;
GetCoprocessorType(void)102     CoprocessorType GetCoprocessorType(void) override { return OT_COPROCESSOR_NCP; }
103     const char     *GetCoprocessorVersion(void) override;
GetInterfaceName(void) const104     const char     *GetInterfaceName(void) const override { return mConfig.mInterfaceName; }
105     void            Init(void) override;
106     void            Deinit(void) override;
107 
108     // MainloopProcessor methods
109     void Update(MainloopContext &aMainloop) override;
110     void Process(const MainloopContext &aMainloop) override;
111 
112 private:
113     ot::Spinel::SpinelDriver &mSpinelDriver;
114     otPlatformConfig          mConfig;
115     NcpSpinel                 mNcpSpinel;
116     TaskRunner                mTaskRunner;
117     Netif                     mNetif;
118 };
119 
120 } // namespace Ncp
121 } // namespace otbr
122 
123 #endif // OTBR_AGENT_NCP_HOST_HPP_
124