1*4a64e381SAndroid Build Coastguard Worker /* 2*4a64e381SAndroid Build Coastguard Worker * Copyright (c) 2024, 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 /** 30*4a64e381SAndroid Build Coastguard Worker * @file 31*4a64e381SAndroid Build Coastguard Worker * This file includes definitions of Thead Controller Interface. 32*4a64e381SAndroid Build Coastguard Worker */ 33*4a64e381SAndroid Build Coastguard Worker 34*4a64e381SAndroid Build Coastguard Worker #ifndef OTBR_AGENT_THREAD_HOST_HPP_ 35*4a64e381SAndroid Build Coastguard Worker #define OTBR_AGENT_THREAD_HOST_HPP_ 36*4a64e381SAndroid Build Coastguard Worker 37*4a64e381SAndroid Build Coastguard Worker #include <functional> 38*4a64e381SAndroid Build Coastguard Worker #include <memory> 39*4a64e381SAndroid Build Coastguard Worker 40*4a64e381SAndroid Build Coastguard Worker #include <openthread/dataset.h> 41*4a64e381SAndroid Build Coastguard Worker #include <openthread/error.h> 42*4a64e381SAndroid Build Coastguard Worker #include <openthread/thread.h> 43*4a64e381SAndroid Build Coastguard Worker 44*4a64e381SAndroid Build Coastguard Worker #include "lib/spinel/coprocessor_type.h" 45*4a64e381SAndroid Build Coastguard Worker 46*4a64e381SAndroid Build Coastguard Worker #include "common/logging.hpp" 47*4a64e381SAndroid Build Coastguard Worker 48*4a64e381SAndroid Build Coastguard Worker namespace otbr { 49*4a64e381SAndroid Build Coastguard Worker namespace Ncp { 50*4a64e381SAndroid Build Coastguard Worker 51*4a64e381SAndroid Build Coastguard Worker /** 52*4a64e381SAndroid Build Coastguard Worker * This interface provides access to some Thread network properties in a sync way. 53*4a64e381SAndroid Build Coastguard Worker * 54*4a64e381SAndroid Build Coastguard Worker * The APIs are unified for both NCP and RCP cases. 55*4a64e381SAndroid Build Coastguard Worker */ 56*4a64e381SAndroid Build Coastguard Worker class NetworkProperties 57*4a64e381SAndroid Build Coastguard Worker { 58*4a64e381SAndroid Build Coastguard Worker public: 59*4a64e381SAndroid Build Coastguard Worker /** 60*4a64e381SAndroid Build Coastguard Worker * Returns the device role. 61*4a64e381SAndroid Build Coastguard Worker * 62*4a64e381SAndroid Build Coastguard Worker * @returns the device role. 63*4a64e381SAndroid Build Coastguard Worker */ 64*4a64e381SAndroid Build Coastguard Worker virtual otDeviceRole GetDeviceRole(void) const = 0; 65*4a64e381SAndroid Build Coastguard Worker 66*4a64e381SAndroid Build Coastguard Worker /** 67*4a64e381SAndroid Build Coastguard Worker * Returns whether or not the IPv6 interface is up. 68*4a64e381SAndroid Build Coastguard Worker * 69*4a64e381SAndroid Build Coastguard Worker * @retval TRUE The IPv6 interface is enabled. 70*4a64e381SAndroid Build Coastguard Worker * @retval FALSE The IPv6 interface is disabled. 71*4a64e381SAndroid Build Coastguard Worker */ 72*4a64e381SAndroid Build Coastguard Worker virtual bool Ip6IsEnabled(void) const = 0; 73*4a64e381SAndroid Build Coastguard Worker 74*4a64e381SAndroid Build Coastguard Worker /** 75*4a64e381SAndroid Build Coastguard Worker * Returns the Partition ID. 76*4a64e381SAndroid Build Coastguard Worker * 77*4a64e381SAndroid Build Coastguard Worker * @returns The Partition ID. 78*4a64e381SAndroid Build Coastguard Worker */ 79*4a64e381SAndroid Build Coastguard Worker virtual uint32_t GetPartitionId(void) const = 0; 80*4a64e381SAndroid Build Coastguard Worker 81*4a64e381SAndroid Build Coastguard Worker /** 82*4a64e381SAndroid Build Coastguard Worker * Returns the active operational dataset tlvs. 83*4a64e381SAndroid Build Coastguard Worker * 84*4a64e381SAndroid Build Coastguard Worker * @param[out] aDatasetTlvs A reference to where the Active Operational Dataset will be placed. 85*4a64e381SAndroid Build Coastguard Worker */ 86*4a64e381SAndroid Build Coastguard Worker virtual void GetDatasetActiveTlvs(otOperationalDatasetTlvs &aDatasetTlvs) const = 0; 87*4a64e381SAndroid Build Coastguard Worker 88*4a64e381SAndroid Build Coastguard Worker /** 89*4a64e381SAndroid Build Coastguard Worker * Returns the pending operational dataset tlvs. 90*4a64e381SAndroid Build Coastguard Worker * 91*4a64e381SAndroid Build Coastguard Worker * @param[out] aDatasetTlvs A reference to where the Pending Operational Dataset will be placed. 92*4a64e381SAndroid Build Coastguard Worker */ 93*4a64e381SAndroid Build Coastguard Worker virtual void GetDatasetPendingTlvs(otOperationalDatasetTlvs &aDatasetTlvs) const = 0; 94*4a64e381SAndroid Build Coastguard Worker 95*4a64e381SAndroid Build Coastguard Worker /** 96*4a64e381SAndroid Build Coastguard Worker * The destructor. 97*4a64e381SAndroid Build Coastguard Worker */ 98*4a64e381SAndroid Build Coastguard Worker virtual ~NetworkProperties(void) = default; 99*4a64e381SAndroid Build Coastguard Worker }; 100*4a64e381SAndroid Build Coastguard Worker 101*4a64e381SAndroid Build Coastguard Worker /** 102*4a64e381SAndroid Build Coastguard Worker * This class is an interface which provides a set of async APIs to control the 103*4a64e381SAndroid Build Coastguard Worker * Thread network. 104*4a64e381SAndroid Build Coastguard Worker * 105*4a64e381SAndroid Build Coastguard Worker * The APIs are unified for both NCP and RCP cases. 106*4a64e381SAndroid Build Coastguard Worker */ 107*4a64e381SAndroid Build Coastguard Worker class ThreadHost : virtual public NetworkProperties 108*4a64e381SAndroid Build Coastguard Worker { 109*4a64e381SAndroid Build Coastguard Worker public: 110*4a64e381SAndroid Build Coastguard Worker using AsyncResultReceiver = std::function<void(otError, const std::string &)>; 111*4a64e381SAndroid Build Coastguard Worker using ChannelMasksReceiver = 112*4a64e381SAndroid Build Coastguard Worker std::function<void(uint32_t /*aSupportedChannelMask*/, uint32_t /*aPreferredChannelMask*/)>; 113*4a64e381SAndroid Build Coastguard Worker using DeviceRoleHandler = std::function<void(otError, otDeviceRole)>; 114*4a64e381SAndroid Build Coastguard Worker using ThreadStateChangedCallback = std::function<void(otChangedFlags aFlags)>; 115*4a64e381SAndroid Build Coastguard Worker 116*4a64e381SAndroid Build Coastguard Worker struct ChannelMaxPower 117*4a64e381SAndroid Build Coastguard Worker { 118*4a64e381SAndroid Build Coastguard Worker uint16_t mChannel; 119*4a64e381SAndroid Build Coastguard Worker int16_t mMaxPower; // INT16_MAX indicates that the corresponding channel is disabled. 120*4a64e381SAndroid Build Coastguard Worker }; 121*4a64e381SAndroid Build Coastguard Worker 122*4a64e381SAndroid Build Coastguard Worker /** 123*4a64e381SAndroid Build Coastguard Worker * Create a Thread Controller Instance. 124*4a64e381SAndroid Build Coastguard Worker * 125*4a64e381SAndroid Build Coastguard Worker * This is a factory method that will decide which implementation class will be created. 126*4a64e381SAndroid Build Coastguard Worker * 127*4a64e381SAndroid Build Coastguard Worker * @param[in] aInterfaceName A string of the Thread interface name. 128*4a64e381SAndroid Build Coastguard Worker * @param[in] aRadioUrls The radio URLs (can be IEEE802.15.4 or TREL radio). 129*4a64e381SAndroid Build Coastguard Worker * @param[in] aBackboneInterfaceName The Backbone network interface name. 130*4a64e381SAndroid Build Coastguard Worker * @param[in] aDryRun TRUE to indicate dry-run mode. FALSE otherwise. 131*4a64e381SAndroid Build Coastguard Worker * @param[in] aEnableAutoAttach Whether or not to automatically attach to the saved network. 132*4a64e381SAndroid Build Coastguard Worker * 133*4a64e381SAndroid Build Coastguard Worker * @returns Non-null OpenThread Controller instance. 134*4a64e381SAndroid Build Coastguard Worker */ 135*4a64e381SAndroid Build Coastguard Worker static std::unique_ptr<ThreadHost> Create(const char *aInterfaceName, 136*4a64e381SAndroid Build Coastguard Worker const std::vector<const char *> &aRadioUrls, 137*4a64e381SAndroid Build Coastguard Worker const char *aBackboneInterfaceName, 138*4a64e381SAndroid Build Coastguard Worker bool aDryRun, 139*4a64e381SAndroid Build Coastguard Worker bool aEnableAutoAttach); 140*4a64e381SAndroid Build Coastguard Worker 141*4a64e381SAndroid Build Coastguard Worker /** 142*4a64e381SAndroid Build Coastguard Worker * This method joins this device to the network specified by @p aActiveOpDatasetTlvs. 143*4a64e381SAndroid Build Coastguard Worker * 144*4a64e381SAndroid Build Coastguard Worker * If there is an ongoing 'Join' operation, no action will be taken and @p aReceiver will be 145*4a64e381SAndroid Build Coastguard Worker * called after the request is completed. The previous @p aReceiver will also be called. 146*4a64e381SAndroid Build Coastguard Worker * 147*4a64e381SAndroid Build Coastguard Worker * @param[in] aActiveOpDatasetTlvs A reference to the active operational dataset of the Thread network. 148*4a64e381SAndroid Build Coastguard Worker * @param[in] aReceiver A receiver to get the async result of this operation. 149*4a64e381SAndroid Build Coastguard Worker */ 150*4a64e381SAndroid Build Coastguard Worker virtual void Join(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs, const AsyncResultReceiver &aRecevier) = 0; 151*4a64e381SAndroid Build Coastguard Worker 152*4a64e381SAndroid Build Coastguard Worker /** 153*4a64e381SAndroid Build Coastguard Worker * This method instructs the device to leave the current network gracefully. 154*4a64e381SAndroid Build Coastguard Worker * 155*4a64e381SAndroid Build Coastguard Worker * 1. If there is already an ongoing 'Leave' operation, no action will be taken and @p aReceiver 156*4a64e381SAndroid Build Coastguard Worker * will be called after the previous request is completed. The previous @p aReceiver will also 157*4a64e381SAndroid Build Coastguard Worker * be called. 158*4a64e381SAndroid Build Coastguard Worker * 2. If this device is not in disabled state, OTBR sends Address Release Notification (i.e. ADDR_REL.ntf) 159*4a64e381SAndroid Build Coastguard Worker * to gracefully detach from the current network and it takes 1 second to finish. 160*4a64e381SAndroid Build Coastguard Worker * 3. Then Operational Dataset will be removed from persistent storage. 161*4a64e381SAndroid Build Coastguard Worker * 4. If everything goes fine, @p aReceiver will be invoked with OT_ERROR_NONE. Otherwise, other errors 162*4a64e381SAndroid Build Coastguard Worker * will be passed to @p aReceiver when the error happens. 163*4a64e381SAndroid Build Coastguard Worker * 164*4a64e381SAndroid Build Coastguard Worker * @param[in] aReceiver A receiver to get the async result of this operation. 165*4a64e381SAndroid Build Coastguard Worker */ 166*4a64e381SAndroid Build Coastguard Worker virtual void Leave(const AsyncResultReceiver &aRecevier) = 0; 167*4a64e381SAndroid Build Coastguard Worker 168*4a64e381SAndroid Build Coastguard Worker /** 169*4a64e381SAndroid Build Coastguard Worker * This method migrates this device to the new network specified by @p aPendingOpDatasetTlvs. 170*4a64e381SAndroid Build Coastguard Worker * 171*4a64e381SAndroid Build Coastguard Worker * @param[in] aPendingOpDatasetTlvs A reference to the pending operational dataset of the Thread network. 172*4a64e381SAndroid Build Coastguard Worker * @param[in] aReceiver A receiver to get the async result of this operation. 173*4a64e381SAndroid Build Coastguard Worker */ 174*4a64e381SAndroid Build Coastguard Worker virtual void ScheduleMigration(const otOperationalDatasetTlvs &aPendingOpDatasetTlvs, 175*4a64e381SAndroid Build Coastguard Worker const AsyncResultReceiver aReceiver) = 0; 176*4a64e381SAndroid Build Coastguard Worker 177*4a64e381SAndroid Build Coastguard Worker /** 178*4a64e381SAndroid Build Coastguard Worker * This method enables/disables the Thread network. 179*4a64e381SAndroid Build Coastguard Worker * 180*4a64e381SAndroid Build Coastguard Worker * 1. If there is an ongoing 'SetThreadEnabled' operation, no action will be taken and @p aReceiver 181*4a64e381SAndroid Build Coastguard Worker * will be invoked with error OT_ERROR_BUSY. 182*4a64e381SAndroid Build Coastguard Worker * 2. If the host hasn't been initialized, @p aReceiver will be invoked with error OT_ERROR_INVALID_STATE. 183*4a64e381SAndroid Build Coastguard Worker * 3. When @p aEnabled is false, this method will first trigger a graceful detach and then disable Thread 184*4a64e381SAndroid Build Coastguard Worker * network interface and the stack. 185*4a64e381SAndroid Build Coastguard Worker * 186*4a64e381SAndroid Build Coastguard Worker * @param[in] aEnabled true to enable and false to disable. 187*4a64e381SAndroid Build Coastguard Worker * @param[in] aReceiver A receiver to get the async result of this operation. 188*4a64e381SAndroid Build Coastguard Worker */ 189*4a64e381SAndroid Build Coastguard Worker virtual void SetThreadEnabled(bool aEnabled, const AsyncResultReceiver aReceiver) = 0; 190*4a64e381SAndroid Build Coastguard Worker 191*4a64e381SAndroid Build Coastguard Worker /** 192*4a64e381SAndroid Build Coastguard Worker * This method sets the country code. 193*4a64e381SAndroid Build Coastguard Worker * 194*4a64e381SAndroid Build Coastguard Worker * The country code refers to the 2-alpha code defined in ISO-3166. 195*4a64e381SAndroid Build Coastguard Worker * 196*4a64e381SAndroid Build Coastguard Worker * 1. If @p aCountryCode isn't valid, @p aReceiver will be invoked with error OT_ERROR_INVALID_ARGS. 197*4a64e381SAndroid Build Coastguard Worker * 2. If the host hasn't been initialized, @p aReceiver will be invoked with error OT_ERROR_INVALID_STATE. 198*4a64e381SAndroid Build Coastguard Worker * 199*4a64e381SAndroid Build Coastguard Worker * @param[in] aCountryCode The country code. 200*4a64e381SAndroid Build Coastguard Worker */ 201*4a64e381SAndroid Build Coastguard Worker virtual void SetCountryCode(const std::string &aCountryCode, const AsyncResultReceiver &aReceiver) = 0; 202*4a64e381SAndroid Build Coastguard Worker 203*4a64e381SAndroid Build Coastguard Worker /** 204*4a64e381SAndroid Build Coastguard Worker * Gets the supported and preferred channel masks. 205*4a64e381SAndroid Build Coastguard Worker * 206*4a64e381SAndroid Build Coastguard Worker * If the operation succeeded, @p aReceiver will be invoked with the supported and preferred channel masks. 207*4a64e381SAndroid Build Coastguard Worker * Otherwise, @p aErrReceiver will be invoked with the error and @p aReceiver won't be invoked in this case. 208*4a64e381SAndroid Build Coastguard Worker * 209*4a64e381SAndroid Build Coastguard Worker * @param aReceiver A receiver to get the channel masks. 210*4a64e381SAndroid Build Coastguard Worker * @param aErrReceiver A receiver to get the error if the operation fails. 211*4a64e381SAndroid Build Coastguard Worker */ 212*4a64e381SAndroid Build Coastguard Worker virtual void GetChannelMasks(const ChannelMasksReceiver &aReceiver, const AsyncResultReceiver &aErrReceiver) = 0; 213*4a64e381SAndroid Build Coastguard Worker 214*4a64e381SAndroid Build Coastguard Worker /** 215*4a64e381SAndroid Build Coastguard Worker * Sets the max power of each channel. 216*4a64e381SAndroid Build Coastguard Worker * 217*4a64e381SAndroid Build Coastguard Worker * 1. If the host hasn't been initialized, @p aReceiver will be invoked with error OT_ERROR_INVALID_STATE. 218*4a64e381SAndroid Build Coastguard Worker * 2. If any value in @p aChannelMaxPowers is invalid, @p aReceiver will be invoked with error 219*4a64e381SAndroid Build Coastguard Worker * OT_ERROR_INVALID_ARGS. 220*4a64e381SAndroid Build Coastguard Worker * 221*4a64e381SAndroid Build Coastguard Worker * @param[in] aChannelMaxPowers A vector of ChannelMaxPower. 222*4a64e381SAndroid Build Coastguard Worker * @param[in] aReceiver A receiver to get the async result of this operation. 223*4a64e381SAndroid Build Coastguard Worker */ 224*4a64e381SAndroid Build Coastguard Worker virtual void SetChannelMaxPowers(const std::vector<ChannelMaxPower> &aChannelMaxPowers, 225*4a64e381SAndroid Build Coastguard Worker const AsyncResultReceiver &aReceiver) = 0; 226*4a64e381SAndroid Build Coastguard Worker 227*4a64e381SAndroid Build Coastguard Worker /** 228*4a64e381SAndroid Build Coastguard Worker * This method adds a event listener for Thread state changes. 229*4a64e381SAndroid Build Coastguard Worker * 230*4a64e381SAndroid Build Coastguard Worker * @param[in] aCallback The callback to receive Thread state changed events. 231*4a64e381SAndroid Build Coastguard Worker */ 232*4a64e381SAndroid Build Coastguard Worker virtual void AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback) = 0; 233*4a64e381SAndroid Build Coastguard Worker 234*4a64e381SAndroid Build Coastguard Worker /** 235*4a64e381SAndroid Build Coastguard Worker * Returns the co-processor type. 236*4a64e381SAndroid Build Coastguard Worker */ 237*4a64e381SAndroid Build Coastguard Worker virtual CoprocessorType GetCoprocessorType(void) = 0; 238*4a64e381SAndroid Build Coastguard Worker 239*4a64e381SAndroid Build Coastguard Worker /** 240*4a64e381SAndroid Build Coastguard Worker * Returns the co-processor version string. 241*4a64e381SAndroid Build Coastguard Worker */ 242*4a64e381SAndroid Build Coastguard Worker virtual const char *GetCoprocessorVersion(void) = 0; 243*4a64e381SAndroid Build Coastguard Worker 244*4a64e381SAndroid Build Coastguard Worker /** 245*4a64e381SAndroid Build Coastguard Worker * This method returns the Thread network interface name. 246*4a64e381SAndroid Build Coastguard Worker * 247*4a64e381SAndroid Build Coastguard Worker * @returns A pointer to the Thread network interface name string. 248*4a64e381SAndroid Build Coastguard Worker */ 249*4a64e381SAndroid Build Coastguard Worker virtual const char *GetInterfaceName(void) const = 0; 250*4a64e381SAndroid Build Coastguard Worker 251*4a64e381SAndroid Build Coastguard Worker /** 252*4a64e381SAndroid Build Coastguard Worker * Initializes the Thread controller. 253*4a64e381SAndroid Build Coastguard Worker */ 254*4a64e381SAndroid Build Coastguard Worker virtual void Init(void) = 0; 255*4a64e381SAndroid Build Coastguard Worker 256*4a64e381SAndroid Build Coastguard Worker /** 257*4a64e381SAndroid Build Coastguard Worker * Deinitializes the Thread controller. 258*4a64e381SAndroid Build Coastguard Worker */ 259*4a64e381SAndroid Build Coastguard Worker virtual void Deinit(void) = 0; 260*4a64e381SAndroid Build Coastguard Worker 261*4a64e381SAndroid Build Coastguard Worker /** 262*4a64e381SAndroid Build Coastguard Worker * The destructor. 263*4a64e381SAndroid Build Coastguard Worker */ 264*4a64e381SAndroid Build Coastguard Worker virtual ~ThreadHost(void) = default; 265*4a64e381SAndroid Build Coastguard Worker }; 266*4a64e381SAndroid Build Coastguard Worker 267*4a64e381SAndroid Build Coastguard Worker } // namespace Ncp 268*4a64e381SAndroid Build Coastguard Worker } // namespace otbr 269*4a64e381SAndroid Build Coastguard Worker 270*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_AGENT_THREAD_HOST_HPP_ 271