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 "coap/coap.hpp"
42 #include "common/locator.hpp"
43 #include "common/non_copyable.hpp"
44 #include "common/timer.hpp"
45 #include "meshcop/meshcop_tlvs.hpp"
46 #include "net/udp6.hpp"
47 #include "thread/mle.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 public:
70     /**
71      * This constructor initializes the Leader object.
72      *
73      * @param[in]  aInstance     A reference to the OpenThread instance.
74      *
75      */
76     explicit Leader(Instance &aInstance);
77 
78     /**
79      * This method sends a MGMT_DATASET_CHANGED message to commissioner.
80      *
81      * @param[in]  aAddress   The IPv6 address of destination.
82      *
83      */
84     void SendDatasetChanged(const Ip6::Address &aAddress);
85 
86     /**
87      * This method sets minimal delay timer.
88      *
89      * @param[in]  aDelayTimerMinimal The value of minimal delay timer (in ms).
90      *
91      * @retval  kErrorNone         Successfully set the minimal delay timer.
92      * @retval  kErrorInvalidArgs  If @p aDelayTimerMinimal is not valid.
93      *
94      */
95     Error SetDelayTimerMinimal(uint32_t aDelayTimerMinimal);
96 
97     /**
98      * This method gets minimal delay timer.
99      *
100      * @retval the minimal delay timer (in ms).
101      *
102      */
103     uint32_t GetDelayTimerMinimal(void) const;
104 
105     /**
106      * This method sets empty Commissioner Data TLV in the Thread Network Data.
107      *
108      */
109     void SetEmptyCommissionerData(void);
110 
111 private:
112     static constexpr uint32_t kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds)
113 
114     static void HandleTimer(Timer &aTimer);
115     void        HandleTimer(void);
116 
117     static void HandlePetition(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
118     void        HandlePetition(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
119     void        SendPetitionResponse(const Coap::Message &   aRequest,
120                                      const Ip6::MessageInfo &aMessageInfo,
121                                      StateTlv::State         aState);
122 
123     static void HandleKeepAlive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
124     void        HandleKeepAlive(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
125     void        SendKeepAliveResponse(const Coap::Message &   aRequest,
126                                       const Ip6::MessageInfo &aMessageInfo,
127                                       StateTlv::State         aState);
128 
129     static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
130 
131     void ResignCommissioner(void);
132 
133     Coap::Resource mPetition;
134     Coap::Resource mKeepAlive;
135     TimerMilli     mTimer;
136 
137     uint32_t mDelayTimerMinimal;
138 
139     CommissionerIdTlv mCommissionerId;
140     uint16_t          mSessionId;
141 };
142 
143 } // namespace MeshCoP
144 } // namespace ot
145 
146 #endif // OPENTHREAD_FTD
147 
148 #endif // MESHCOP_LEADER_HPP_
149