1 /* 2 * Copyright (c) 2018, 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 BorderAgent role. 32 */ 33 34 #ifndef BORDER_AGENT_HPP_ 35 #define BORDER_AGENT_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE 40 41 #include <openthread/border_agent.h> 42 43 #include "coap/coap.hpp" 44 #include "common/as_core_type.hpp" 45 #include "common/locator.hpp" 46 #include "common/non_copyable.hpp" 47 #include "common/notifier.hpp" 48 #include "net/udp6.hpp" 49 50 namespace ot { 51 52 namespace MeshCoP { 53 54 class BorderAgent : public InstanceLocator, private NonCopyable 55 { 56 friend class ot::Notifier; 57 58 public: 59 /** 60 * This enumeration defines the Border Agent state. 61 * 62 */ 63 enum State : uint8_t 64 { 65 kStateStopped = OT_BORDER_AGENT_STATE_STOPPED, ///< Border agent is stopped/disabled. 66 kStateStarted = OT_BORDER_AGENT_STATE_STARTED, ///< Border agent is started. 67 kStateActive = OT_BORDER_AGENT_STATE_ACTIVE, ///< Border agent is connected with external commissioner. 68 }; 69 70 /** 71 * This constructor initializes the `BorderAgent` object. 72 * 73 * @param[in] aInstance A reference to the OpenThread instance. 74 * 75 */ 76 explicit BorderAgent(Instance &aInstance); 77 78 /** 79 * This method gets the UDP port of this service. 80 * 81 * @returns UDP port number. 82 * 83 */ 84 uint16_t GetUdpPort(void) const; 85 86 /** 87 * This method starts the Border Agent service. 88 * 89 */ 90 void Start(void); 91 92 /** 93 * This method stops the Border Agent service. 94 * 95 */ 96 void Stop(void); 97 98 /** 99 * This method gets the state of the Border Agent service. 100 * 101 * @returns The state of the Border Agent service. 102 * 103 */ GetState(void) const104 State GetState(void) const { return mState; } 105 106 /** 107 * This method applies the Mesh Local Prefix. 108 * 109 */ 110 void ApplyMeshLocalPrefix(void); 111 112 /** 113 * This method returns the UDP Proxy port to which the commissioner is currently 114 * bound. 115 * 116 * @returns The current UDP Proxy port or 0 if no Proxy Transmit has been received yet. 117 * 118 */ GetUdpProxyPort(void) const119 uint16_t GetUdpProxyPort(void) const { return mUdpProxyPort; } 120 121 private: 122 class ForwardContext : public InstanceLocatorInit 123 { 124 public: 125 void Init(Instance &aInstance, const Coap::Message &aMessage, bool aPetition, bool aSeparate); IsPetition(void) const126 bool IsPetition(void) const { return mPetition; } GetMessageId(void) const127 uint16_t GetMessageId(void) const { return mMessageId; } 128 Error ToHeader(Coap::Message &aMessage, uint8_t aCode); 129 130 private: 131 uint16_t mMessageId; // The CoAP Message ID of the original request. 132 bool mPetition : 1; // Whether the forwarding request is leader petition. 133 bool mSeparate : 1; // Whether the original request expects separate response. 134 uint8_t mTokenLength : 4; // The CoAP Token Length of the original request. 135 uint8_t mType : 2; // The CoAP Type of the original request. 136 uint8_t mToken[Coap::Message::kMaxTokenLength]; // The CoAP Token of the original request. 137 }; 138 139 void HandleNotifierEvents(Events aEvents); 140 141 Coap::Message::Code CoapCodeFromError(Error aError); 142 void SendErrorMessage(ForwardContext &aForwardContext, Error aError); 143 void SendErrorMessage(const Coap::Message &aRequest, bool aSeparate, Error aError); 144 145 static void HandleConnected(bool aConnected, void *aContext); 146 void HandleConnected(bool aConnected); 147 148 template <Coap::Resource BorderAgent::*aResource> 149 static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); 150 151 static void HandleTimeout(Timer &aTimer); 152 void HandleTimeout(void); 153 154 static void HandleCoapResponse(void * aContext, 155 otMessage * aMessage, 156 const otMessageInfo *aMessageInfo, 157 Error aResult); 158 void HandleCoapResponse(ForwardContext &aForwardContext, const Coap::Message *aResponse, Error aResult); 159 160 Error ForwardToLeader(const Coap::Message & aMessage, 161 const Ip6::MessageInfo &aMessageInfo, 162 const char * aPath, 163 bool aPetition, 164 bool aSeparate); 165 Error ForwardToCommissioner(Coap::Message &aForwardMessage, const Message &aMessage); 166 void HandleKeepAlive(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 167 void HandleRelayTransmit(const Coap::Message &aMessage); 168 void HandleRelayReceive(const Coap::Message &aMessage); 169 void HandleProxyTransmit(const Coap::Message &aMessage); HandleUdpReceive(void * aContext,const otMessage * aMessage,const otMessageInfo * aMessageInfo)170 static bool HandleUdpReceive(void *aContext, const otMessage *aMessage, const otMessageInfo *aMessageInfo) 171 { 172 return static_cast<BorderAgent *>(aContext)->HandleUdpReceive(AsCoreType(aMessage), AsCoreType(aMessageInfo)); 173 } 174 bool HandleUdpReceive(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 175 176 static constexpr uint32_t kKeepAliveTimeout = 50 * 1000; // Timeout to reject a commissioner. 177 178 Ip6::MessageInfo mMessageInfo; 179 180 Coap::Resource mCommissionerPetition; 181 Coap::Resource mCommissionerKeepAlive; 182 Coap::Resource mRelayTransmit; 183 Coap::Resource mRelayReceive; 184 Coap::Resource mCommissionerGet; 185 Coap::Resource mCommissionerSet; 186 Coap::Resource mActiveGet; 187 Coap::Resource mActiveSet; 188 Coap::Resource mPendingGet; 189 Coap::Resource mPendingSet; 190 Coap::Resource mProxyTransmit; 191 192 Ip6::Udp::Receiver mUdpReceiver; ///< The UDP receiver to receive packets from external commissioner 193 Ip6::Netif::UnicastAddress mCommissionerAloc; 194 195 TimerMilli mTimer; 196 State mState; 197 uint16_t mUdpProxyPort; 198 }; 199 200 } // namespace MeshCoP 201 202 DefineMapEnum(otBorderAgentState, MeshCoP::BorderAgent::State); 203 204 } // namespace ot 205 206 #endif // OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE 207 208 #endif // BORDER_AGENT_HPP_ 209