• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 the Joiner Router role.
32  */
33 
34 #ifndef JOINER_ROUTER_HPP_
35 #define JOINER_ROUTER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_FTD
40 
41 #include "coap/coap_message.hpp"
42 #include "common/locator.hpp"
43 #include "common/message.hpp"
44 #include "common/non_copyable.hpp"
45 #include "common/notifier.hpp"
46 #include "common/timer.hpp"
47 #include "mac/mac_types.hpp"
48 #include "meshcop/meshcop_tlvs.hpp"
49 #include "net/udp6.hpp"
50 #include "thread/key_manager.hpp"
51 #include "thread/tmf.hpp"
52 
53 namespace ot {
54 
55 namespace MeshCoP {
56 
57 class JoinerRouter : public InstanceLocator, private NonCopyable
58 {
59     friend class ot::Notifier;
60     friend class Tmf::Agent;
61 
62 public:
63     /**
64      * Initializes the Joiner Router object.
65      *
66      * @param[in]  aInstance     A reference to the OpenThread instance.
67      */
68     explicit JoinerRouter(Instance &aInstance);
69 
70     /**
71      * Returns the Joiner UDP Port.
72      *
73      * @returns The Joiner UDP Port number.
74      */
75     uint16_t GetJoinerUdpPort(void) const;
76 
77     /**
78      * Sets the Joiner UDP Port.
79      *
80      * @param[in]  aJoinerUdpPort  The Joiner UDP Port number.
81      */
82     void SetJoinerUdpPort(uint16_t aJoinerUdpPort);
83 
84 private:
85     static constexpr uint16_t kDefaultJoinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
86     static constexpr uint32_t kJoinerEntrustTxDelay = 50; // in msec
87 
88     struct JoinerEntrustMetadata : public Message::FooterData<JoinerEntrustMetadata>
89     {
90         Ip6::MessageInfo mMessageInfo; // Message info of the message to send.
91         TimeMilli        mSendTime;    // Time when the message shall be sent.
92         Kek              mKek;         // KEK used by MAC layer to encode this message.
93     };
94 
95     void HandleNotifierEvents(Events aEvents);
96 
97     void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
98 
99     template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
100 
101     static void HandleJoinerEntrustResponse(void                *aContext,
102                                             otMessage           *aMessage,
103                                             const otMessageInfo *aMessageInfo,
104                                             otError              aResult);
105     void HandleJoinerEntrustResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
106 
107     void HandleTimer(void);
108 
109     void           Start(void);
110     void           DelaySendingJoinerEntrust(const Ip6::MessageInfo &aMessageInfo, const Kek &aKek);
111     void           SendDelayedJoinerEntrust(void);
112     Error          SendJoinerEntrust(const Ip6::MessageInfo &aMessageInfo);
113     Coap::Message *PrepareJoinerEntrustMessage(void);
114 
115     using JoinerRouterTimer = TimerMilliIn<JoinerRouter, &JoinerRouter::HandleTimer>;
116     using JoinerSocket      = Ip6::Udp::SocketIn<JoinerRouter, &JoinerRouter::HandleUdpReceive>;
117 
118     JoinerSocket mSocket;
119 
120     JoinerRouterTimer mTimer;
121     MessageQueue      mDelayedJoinEnts;
122 
123     uint16_t mJoinerUdpPort;
124 
125     bool mIsJoinerPortConfigured : 1;
126 };
127 
128 DeclareTmfHandler(JoinerRouter, kUriRelayTx);
129 
130 } // namespace MeshCoP
131 } // namespace ot
132 
133 #endif // OPENTHREAD_FTD
134 
135 #endif // JOINER_ROUTER_HPP_
136