1 /* 2 * Copyright (c) 2016, 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 for a MeshCoP Leader. 32 */ 33 34 #ifndef MESHCOP_LEADER_HPP_ 35 #define MESHCOP_LEADER_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_FTD 40 41 #include "common/locator.hpp" 42 #include "common/non_copyable.hpp" 43 #include "common/timer.hpp" 44 #include "meshcop/meshcop_tlvs.hpp" 45 #include "net/udp6.hpp" 46 #include "thread/mle.hpp" 47 #include "thread/tmf.hpp" 48 49 namespace ot { 50 namespace MeshCoP { 51 52 class Leader : public InstanceLocator, private NonCopyable 53 { 54 friend class Tmf::Agent; 55 56 public: 57 /** 58 * Initializes the Leader object. 59 * 60 * @param[in] aInstance A reference to the OpenThread instance. 61 */ 62 explicit Leader(Instance &aInstance); 63 64 /** 65 * Sets the session ID. 66 * 67 * @param[in] aSessionId The session ID to use. 68 */ SetSessionId(uint16_t aSessionId)69 void SetSessionId(uint16_t aSessionId) { mSessionId = aSessionId; } 70 71 /** 72 * Sends a MGMT_DATASET_CHANGED message to commissioner. 73 * 74 * @param[in] aAddress The IPv6 address of destination. 75 */ 76 void SendDatasetChanged(const Ip6::Address &aAddress); 77 78 /** 79 * Sets minimal delay timer. 80 * 81 * @param[in] aDelayTimerMinimal The value of minimal delay timer (in ms). 82 * 83 * @retval kErrorNone Successfully set the minimal delay timer. 84 * @retval kErrorInvalidArgs If @p aDelayTimerMinimal is not valid. 85 */ 86 Error SetDelayTimerMinimal(uint32_t aDelayTimerMinimal); 87 88 /** 89 * Gets minimal delay timer. 90 * 91 * @retval the minimal delay timer (in ms). 92 */ GetDelayTimerMinimal(void) const93 uint32_t GetDelayTimerMinimal(void) const { return mDelayTimerMinimal; } 94 95 /** 96 * Sets empty Commissioner Data TLV in the Thread Network Data. 97 */ 98 void SetEmptyCommissionerData(void); 99 100 private: 101 static constexpr uint32_t kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds) 102 103 OT_TOOL_PACKED_BEGIN 104 class CommissioningData 105 { 106 public: 107 void Init(uint16_t aBorderAgentRloc16, uint16_t aSessionId); 108 uint8_t GetLength(void) const; 109 110 private: 111 BorderAgentLocatorTlv mBorderAgentLocatorTlv; 112 CommissionerSessionIdTlv mSessionIdTlv; 113 SteeringDataTlv mSteeringDataTlv; 114 } OT_TOOL_PACKED_END; 115 116 void HandleTimer(void); 117 118 template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 119 120 void SendPetitionResponse(const Coap::Message &aRequest, 121 const Ip6::MessageInfo &aMessageInfo, 122 StateTlv::State aState); 123 124 void SendKeepAliveResponse(const Coap::Message &aRequest, 125 const Ip6::MessageInfo &aMessageInfo, 126 StateTlv::State aState); 127 128 static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); 129 130 void ResignCommissioner(void); 131 132 using LeaderTimer = TimerMilliIn<Leader, &Leader::HandleTimer>; 133 134 LeaderTimer mTimer; 135 136 uint32_t mDelayTimerMinimal; 137 138 CommissionerIdTlv::StringType mCommissionerId; 139 uint16_t mSessionId; 140 }; 141 142 DeclareTmfHandler(Leader, kUriLeaderPetition); 143 DeclareTmfHandler(Leader, kUriLeaderKeepAlive); 144 145 } // namespace MeshCoP 146 } // namespace ot 147 148 #endif // OPENTHREAD_FTD 149 150 #endif // MESHCOP_LEADER_HPP_ 151