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 */ 63 explicit Leader(Instance &aInstance); 64 65 /** 66 * Sets the session ID. 67 * 68 * @param[in] aSessionId The session ID to use. 69 * 70 */ SetSessionId(uint16_t aSessionId)71 void SetSessionId(uint16_t aSessionId) { mSessionId = aSessionId; } 72 73 /** 74 * Sends a MGMT_DATASET_CHANGED message to commissioner. 75 * 76 * @param[in] aAddress The IPv6 address of destination. 77 * 78 */ 79 void SendDatasetChanged(const Ip6::Address &aAddress); 80 81 /** 82 * Sets minimal delay timer. 83 * 84 * @param[in] aDelayTimerMinimal The value of minimal delay timer (in ms). 85 * 86 * @retval kErrorNone Successfully set the minimal delay timer. 87 * @retval kErrorInvalidArgs If @p aDelayTimerMinimal is not valid. 88 * 89 */ 90 Error SetDelayTimerMinimal(uint32_t aDelayTimerMinimal); 91 92 /** 93 * Gets minimal delay timer. 94 * 95 * @retval the minimal delay timer (in ms). 96 * 97 */ GetDelayTimerMinimal(void) const98 uint32_t GetDelayTimerMinimal(void) const { return mDelayTimerMinimal; } 99 100 /** 101 * Sets empty Commissioner Data TLV in the Thread Network Data. 102 * 103 */ 104 void SetEmptyCommissionerData(void); 105 106 private: 107 static constexpr uint32_t kMinDelayTimer = OPENTHREAD_CONFIG_TMF_PENDING_DATASET_MINIMUM_DELAY; // (msec) 108 static constexpr uint32_t kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds) 109 110 OT_TOOL_PACKED_BEGIN 111 class CommissioningData 112 { 113 public: 114 void Init(uint16_t aBorderAgentRloc16, uint16_t aSessionId); 115 uint8_t GetLength(void) const; 116 117 private: 118 BorderAgentLocatorTlv mBorderAgentLocatorTlv; 119 CommissionerSessionIdTlv mSessionIdTlv; 120 SteeringDataTlv mSteeringDataTlv; 121 } OT_TOOL_PACKED_END; 122 123 void HandleTimer(void); 124 125 template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 126 127 void SendPetitionResponse(const Coap::Message &aRequest, 128 const Ip6::MessageInfo &aMessageInfo, 129 StateTlv::State aState); 130 131 void SendKeepAliveResponse(const Coap::Message &aRequest, 132 const Ip6::MessageInfo &aMessageInfo, 133 StateTlv::State aState); 134 135 static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); 136 137 void ResignCommissioner(void); 138 139 using LeaderTimer = TimerMilliIn<Leader, &Leader::HandleTimer>; 140 141 LeaderTimer mTimer; 142 143 uint32_t mDelayTimerMinimal; 144 145 CommissionerIdTlv::StringType mCommissionerId; 146 uint16_t mSessionId; 147 }; 148 149 DeclareTmfHandler(Leader, kUriLeaderPetition); 150 DeclareTmfHandler(Leader, kUriLeaderKeepAlive); 151 152 } // namespace MeshCoP 153 } // namespace ot 154 155 #endif // OPENTHREAD_FTD 156 157 #endif // MESHCOP_LEADER_HPP_ 158