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 OT_TOOL_PACKED_BEGIN
53 class CommissioningData
54 {
55 public:
GetLength(void) const56     uint8_t GetLength(void) const
57     {
58         return sizeof(Tlv) + mBorderAgentLocator.GetLength() + sizeof(Tlv) + mCommissionerSessionId.GetLength() +
59                sizeof(Tlv) + mSteeringData.GetLength();
60     }
61 
62     BorderAgentLocatorTlv    mBorderAgentLocator;
63     CommissionerSessionIdTlv mCommissionerSessionId;
64     SteeringDataTlv          mSteeringData;
65 } OT_TOOL_PACKED_END;
66 
67 class Leader : public InstanceLocator, private NonCopyable
68 {
69     friend class Tmf::Agent;
70 
71 public:
72     /**
73      * This constructor initializes the Leader object.
74      *
75      * @param[in]  aInstance     A reference to the OpenThread instance.
76      *
77      */
78     explicit Leader(Instance &aInstance);
79 
80     /**
81      * This method sends a MGMT_DATASET_CHANGED message to commissioner.
82      *
83      * @param[in]  aAddress   The IPv6 address of destination.
84      *
85      */
86     void SendDatasetChanged(const Ip6::Address &aAddress);
87 
88     /**
89      * This method sets minimal delay timer.
90      *
91      * @param[in]  aDelayTimerMinimal The value of minimal delay timer (in ms).
92      *
93      * @retval  kErrorNone         Successfully set the minimal delay timer.
94      * @retval  kErrorInvalidArgs  If @p aDelayTimerMinimal is not valid.
95      *
96      */
97     Error SetDelayTimerMinimal(uint32_t aDelayTimerMinimal);
98 
99     /**
100      * This method gets minimal delay timer.
101      *
102      * @retval the minimal delay timer (in ms).
103      *
104      */
105     uint32_t GetDelayTimerMinimal(void) const;
106 
107     /**
108      * This method sets empty Commissioner Data TLV in the Thread Network Data.
109      *
110      */
111     void SetEmptyCommissionerData(void);
112 
113 private:
114     static constexpr uint32_t kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds)
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 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