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/locator.hpp"
45 #include "common/non_copyable.hpp"
46 #include "common/notifier.hpp"
47 #include "net/udp6.hpp"
48 
49 namespace ot {
50 
51 namespace MeshCoP {
52 
53 class BorderAgent : public InstanceLocator, private NonCopyable
54 {
55     friend class ot::Notifier;
56 
57 public:
58     /**
59      * This enumeration defines the Border Agent state.
60      *
61      */
62     enum State : uint8_t
63     {
64         kStateStopped = OT_BORDER_AGENT_STATE_STOPPED, ///< Border agent is stopped/disabled.
65         kStateStarted = OT_BORDER_AGENT_STATE_STARTED, ///< Border agent is started.
66         kStateActive  = OT_BORDER_AGENT_STATE_ACTIVE,  ///< Border agent is connected with external commissioner.
67     };
68 
69     /**
70      * This constructor initializes the `BorderAgent` object.
71      *
72      * @param[in]  aInstance     A reference to the OpenThread instance.
73      *
74      */
75     explicit BorderAgent(Instance &aInstance);
76 
77     /**
78      * This method gets the UDP port of this service.
79      *
80      * @returns  UDP port number.
81      *
82      */
83     uint16_t GetUdpPort(void) const;
84 
85     /**
86      * This method starts the Border Agent service.
87      *
88      */
89     void Start(void);
90 
91     /**
92      * This method stops the Border Agent service.
93      *
94      */
95     void Stop(void);
96 
97     /**
98      * This method gets the state of the Border Agent service.
99      *
100      * @returns The state of the Border Agent service.
101      *
102      */
GetState(void) const103     State GetState(void) const { return mState; }
104 
105     /**
106      * This method applies the Mesh Local Prefix.
107      *
108      */
109     void ApplyMeshLocalPrefix(void);
110 
111     /**
112      * This method returns the UDP Proxy port to which the commissioner is currently
113      * bound.
114      *
115      * @returns  The current UDP Proxy port or 0 if no Proxy Transmit has been received yet.
116      *
117      */
GetUdpProxyPort(void) const118     uint16_t GetUdpProxyPort(void) const { return mUdpProxyPort; }
119 
120 private:
121     class ForwardContext : public InstanceLocatorInit
122     {
123     public:
124         void     Init(Instance &aInstance, const Coap::Message &aMessage, bool aPetition, bool aSeparate);
IsPetition(void) const125         bool     IsPetition(void) const { return mPetition; }
GetMessageId(void) const126         uint16_t GetMessageId(void) const { return mMessageId; }
127         Error    ToHeader(Coap::Message &aMessage, uint8_t aCode);
128 
129     private:
130         uint16_t mMessageId;                             // The CoAP Message ID of the original request.
131         bool     mPetition : 1;                          // Whether the forwarding request is leader petition.
132         bool     mSeparate : 1;                          // Whether the original request expects separate response.
133         uint8_t  mTokenLength : 4;                       // The CoAP Token Length of the original request.
134         uint8_t  mType : 2;                              // The CoAP Type of the original request.
135         uint8_t  mToken[Coap::Message::kMaxTokenLength]; // The CoAP Token of the original request.
136     };
137 
138     void HandleNotifierEvents(Events aEvents);
139 
140     Coap::Message::Code CoapCodeFromError(Error aError);
141     void                SendErrorMessage(ForwardContext &aForwardContext, Error aError);
142     void                SendErrorMessage(const Coap::Message &aRequest, bool aSeparate, Error aError);
143 
144     static void HandleConnected(bool aConnected, void *aContext);
145     void        HandleConnected(bool aConnected);
146 
147     template <Coap::Resource BorderAgent::*aResource>
148     static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
149 
150     static void HandleTimeout(Timer &aTimer);
151     void        HandleTimeout(void);
152 
153     static void HandleCoapResponse(void *               aContext,
154                                    otMessage *          aMessage,
155                                    const otMessageInfo *aMessageInfo,
156                                    Error                aResult);
157     void        HandleCoapResponse(ForwardContext &aForwardContext, const Coap::Message *aResponse, Error aResult);
158 
159     Error       ForwardToLeader(const Coap::Message &   aMessage,
160                                 const Ip6::MessageInfo &aMessageInfo,
161                                 const char *            aPath,
162                                 bool                    aPetition,
163                                 bool                    aSeparate);
164     Error       ForwardToCommissioner(Coap::Message &aForwardMessage, const Message &aMessage);
165     void        HandleKeepAlive(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
166     void        HandleRelayTransmit(const Coap::Message &aMessage);
167     void        HandleRelayReceive(const Coap::Message &aMessage);
168     void        HandleProxyTransmit(const Coap::Message &aMessage);
HandleUdpReceive(void * aContext,const otMessage * aMessage,const otMessageInfo * aMessageInfo)169     static bool HandleUdpReceive(void *aContext, const otMessage *aMessage, const otMessageInfo *aMessageInfo)
170     {
171         return static_cast<BorderAgent *>(aContext)->HandleUdpReceive(
172             *static_cast<const Message *>(aMessage), *static_cast<const Ip6::MessageInfo *>(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 } // namespace ot
202 
203 #endif // OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
204 
205 #endif // BORDER_AGENT_HPP_
206