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 "host/ncp_spinel.hpp" 42 #include "host/thread_host.hpp" 43 #include "posix/netif.hpp" 44 45 namespace otbr { 46 namespace Host { 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, 76 public ThreadHost, 77 public NcpNetworkProperties 78 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY 79 , 80 public Mdns::StateObserver 81 #endif 82 { 83 public: 84 /** 85 * Constructor. 86 * 87 * @param[in] aInterfaceName A string of the NCP interface name. 88 * @param[in] aBackboneInterfaceName A string of the backbone interface name. 89 * @param[in] aDryRun TRUE to indicate dry-run mode. FALSE otherwise. 90 */ 91 NcpHost(const char *aInterfaceName, const char *aBackboneInterfaceName, bool aDryRun); 92 93 /** 94 * Destructor. 95 */ 96 ~NcpHost(void) override = default; 97 98 // ThreadHost methods 99 void Join(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs, const AsyncResultReceiver &aReceiver) override; 100 void Leave(bool aEraseDataset, const AsyncResultReceiver &aReceiver) override; 101 void ScheduleMigration(const otOperationalDatasetTlvs &aPendingOpDatasetTlvs, 102 const AsyncResultReceiver aReceiver) override; 103 void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) override; 104 void SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver) override; 105 void GetChannelMasks(const ChannelMasksReceiver &aReceiver, const AsyncResultReceiver &aErrReceiver) override; 106 #if OTBR_ENABLE_POWER_CALIBRATION 107 void SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers, 108 const AsyncResultReceiver &aReceiver) override; 109 #endif 110 void AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback) override; 111 void AddThreadEnabledStateChangedCallback(ThreadEnabledStateCallback aCallback) override; GetCoprocessorType(void)112 CoprocessorType GetCoprocessorType(void) override 113 { 114 return OT_COPROCESSOR_NCP; 115 } 116 const char *GetCoprocessorVersion(void) override; GetInterfaceName(void) const117 const char *GetInterfaceName(void) const override 118 { 119 return mConfig.mInterfaceName; 120 } 121 void Init(void) override; 122 void Deinit(void) override; 123 124 // MainloopProcessor methods 125 void Update(MainloopContext &aMainloop) override; 126 void Process(const MainloopContext &aMainloop) override; 127 128 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY 129 void SetMdnsPublisher(Mdns::Publisher *aPublisher); 130 #endif 131 132 private: 133 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY 134 void HandleMdnsState(Mdns::Publisher::State aState) override; 135 #endif 136 137 ot::Spinel::SpinelDriver &mSpinelDriver; 138 otPlatformConfig mConfig; 139 NcpSpinel mNcpSpinel; 140 TaskRunner mTaskRunner; 141 Netif mNetif; 142 InfraIf mInfraIf; 143 }; 144 145 } // namespace Host 146 } // namespace otbr 147 148 #endif // OTBR_AGENT_NCP_HOST_HPP_ 149