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 kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds)
108 
109     OT_TOOL_PACKED_BEGIN
110     class CommissioningData
111     {
112     public:
113         void    Init(uint16_t aBorderAgentRloc16, uint16_t aSessionId);
114         uint8_t GetLength(void) const;
115 
116     private:
117         BorderAgentLocatorTlv    mBorderAgentLocatorTlv;
118         CommissionerSessionIdTlv mSessionIdTlv;
119         SteeringDataTlv          mSteeringDataTlv;
120     } OT_TOOL_PACKED_END;
121 
122     void HandleTimer(void);
123 
124     template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
125 
126     void SendPetitionResponse(const Coap::Message    &aRequest,
127                               const Ip6::MessageInfo &aMessageInfo,
128                               StateTlv::State         aState);
129 
130     void SendKeepAliveResponse(const Coap::Message    &aRequest,
131                                const Ip6::MessageInfo &aMessageInfo,
132                                StateTlv::State         aState);
133 
134     static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
135 
136     void ResignCommissioner(void);
137 
138     using LeaderTimer = TimerMilliIn<Leader, &Leader::HandleTimer>;
139 
140     LeaderTimer mTimer;
141 
142     uint32_t mDelayTimerMinimal;
143 
144     CommissionerIdTlv::StringType mCommissionerId;
145     uint16_t                      mSessionId;
146 };
147 
148 DeclareTmfHandler(Leader, kUriLeaderPetition);
149 DeclareTmfHandler(Leader, kUriLeaderKeepAlive);
150 
151 } // namespace MeshCoP
152 } // namespace ot
153 
154 #endif // OPENTHREAD_FTD
155 
156 #endif // MESHCOP_LEADER_HPP_
157