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 */ 69 explicit JoinerRouter(Instance &aInstance); 70 71 /** 72 * Returns the Joiner UDP Port. 73 * 74 * @returns The Joiner UDP Port number. 75 * 76 */ 77 uint16_t GetJoinerUdpPort(void) const; 78 79 /** 80 * Sets the Joiner UDP Port. 81 * 82 * @param[in] aJoinerUdpPort The Joiner UDP Port number. 83 * 84 */ 85 void SetJoinerUdpPort(uint16_t aJoinerUdpPort); 86 87 private: 88 static constexpr uint16_t kDefaultJoinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT; 89 static constexpr uint32_t kJoinerEntrustTxDelay = 50; // in msec 90 91 struct JoinerEntrustMetadata 92 { AppendToot::MeshCoP::JoinerRouter::JoinerEntrustMetadata93 Error AppendTo(Message &aMessage) const { return aMessage.Append(*this); } 94 void ReadFrom(const Message &aMessage); 95 96 Ip6::MessageInfo mMessageInfo; // Message info of the message to send. 97 TimeMilli mSendTime; // Time when the message shall be sent. 98 Kek mKek; // KEK used by MAC layer to encode this message. 99 }; 100 101 void HandleNotifierEvents(Events aEvents); 102 103 static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); 104 void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 105 106 template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 107 108 static void HandleJoinerEntrustResponse(void *aContext, 109 otMessage *aMessage, 110 const otMessageInfo *aMessageInfo, 111 Error aResult); 112 void HandleJoinerEntrustResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult); 113 114 void HandleTimer(void); 115 116 void Start(void); 117 void DelaySendingJoinerEntrust(const Ip6::MessageInfo &aMessageInfo, const Kek &aKek); 118 void SendDelayedJoinerEntrust(void); 119 Error SendJoinerEntrust(const Ip6::MessageInfo &aMessageInfo); 120 Coap::Message *PrepareJoinerEntrustMessage(void); 121 122 using JoinerRouterTimer = TimerMilliIn<JoinerRouter, &JoinerRouter::HandleTimer>; 123 124 Ip6::Udp::Socket mSocket; 125 126 JoinerRouterTimer mTimer; 127 MessageQueue mDelayedJoinEnts; 128 129 uint16_t mJoinerUdpPort; 130 131 bool mIsJoinerPortConfigured : 1; 132 }; 133 134 DeclareTmfHandler(JoinerRouter, kUriRelayTx); 135 136 } // namespace MeshCoP 137 } // namespace ot 138 139 #endif // OPENTHREAD_FTD 140 141 #endif // JOINER_ROUTER_HPP_ 142