• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @brief
32  *   This file includes functions for the Thread Border Agent role.
33  */
34 
35 #ifndef OPENTHREAD_BORDER_AGENT_H_
36 #define OPENTHREAD_BORDER_AGENT_H_
37 
38 #include <openthread/instance.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @addtogroup api-border-agent
46  *
47  * @brief
48  *   This module includes functions for the Thread Border Agent role.
49  *
50  * @{
51  *
52  */
53 
54 /**
55  * The length of Border Agent/Router ID in bytes.
56  *
57  */
58 #define OT_BORDER_AGENT_ID_LENGTH (16)
59 
60 /**
61  * Minimum length of the ephemeral key string.
62  *
63  */
64 #define OT_BORDER_AGENT_MIN_EPHEMERAL_KEY_LENGTH (6)
65 
66 /**
67  * Maximum length of the ephemeral key string.
68  *
69  */
70 #define OT_BORDER_AGENT_MAX_EPHEMERAL_KEY_LENGTH (32)
71 
72 /**
73  * Default ephemeral key timeout interval in milliseconds.
74  *
75  */
76 #define OT_BORDER_AGENT_DEFAULT_EPHEMERAL_KEY_TIMEOUT (2 * 60 * 1000u)
77 
78 /**
79  * Maximum ephemeral key timeout interval in milliseconds.
80  *
81  */
82 #define OT_BORDER_AGENT_MAX_EPHEMERAL_KEY_TIMEOUT (10 * 60 * 1000u)
83 
84 /**
85  * @struct otBorderAgentId
86  *
87  * Represents a Border Agent ID.
88  *
89  */
90 OT_TOOL_PACKED_BEGIN
91 struct otBorderAgentId
92 {
93     uint8_t mId[OT_BORDER_AGENT_ID_LENGTH];
94 } OT_TOOL_PACKED_END;
95 
96 /**
97  * Represents a Border Agent ID.
98  *
99  */
100 typedef struct otBorderAgentId otBorderAgentId;
101 
102 /**
103  * Defines the Border Agent state.
104  *
105  */
106 typedef enum otBorderAgentState
107 {
108     OT_BORDER_AGENT_STATE_STOPPED = 0, ///< Border agent role is disabled.
109     OT_BORDER_AGENT_STATE_STARTED = 1, ///< Border agent is started.
110     OT_BORDER_AGENT_STATE_ACTIVE  = 2, ///< Border agent is connected with external commissioner.
111 } otBorderAgentState;
112 
113 /**
114  * Gets the #otBorderAgentState of the Thread Border Agent role.
115  *
116  * @param[in]  aInstance  A pointer to an OpenThread instance.
117  *
118  * @returns The current #otBorderAgentState of the Border Agent.
119  *
120  */
121 otBorderAgentState otBorderAgentGetState(otInstance *aInstance);
122 
123 /**
124  * Gets the UDP port of the Thread Border Agent service.
125  *
126  * @param[in]  aInstance  A pointer to an OpenThread instance.
127  *
128  * @returns UDP port of the Border Agent.
129  *
130  */
131 uint16_t otBorderAgentGetUdpPort(otInstance *aInstance);
132 
133 /**
134  * Gets the randomly generated Border Agent ID.
135  *
136  * Requires `OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE`.
137  *
138  * The ID is saved in persistent storage and survives reboots. The typical use case of the ID is to
139  * be published in the MeshCoP mDNS service as the `id` TXT value for the client to identify this
140  * Border Router/Agent device.
141  *
142  * @param[in]    aInstance  A pointer to an OpenThread instance.
143  * @param[out]   aId        A pointer to buffer to receive the ID.
144  *
145  * @retval OT_ERROR_NONE  If successfully retrieved the Border Agent ID.
146  * @retval ...            If failed to retrieve the Border Agent ID.
147  *
148  * @sa otBorderAgentSetId
149  *
150  */
151 otError otBorderAgentGetId(otInstance *aInstance, otBorderAgentId *aId);
152 
153 /**
154  * Sets the Border Agent ID.
155  *
156  * Requires `OPENTHREAD_CONFIG_BORDER_AGENT_ID_ENABLE`.
157  *
158  * The Border Agent ID will be saved in persistent storage and survive reboots. It's required to
159  * set the ID only once after factory reset. If the ID has never been set by calling this function,
160  * a random ID will be generated and returned when `otBorderAgentGetId` is called.
161  *
162  * @param[in]    aInstance  A pointer to an OpenThread instance.
163  * @param[out]   aId        A pointer to the Border Agent ID.
164  *
165  * @retval OT_ERROR_NONE  If successfully set the Border Agent ID.
166  * @retval ...            If failed to set the Border Agent ID.
167  *
168  * @sa otBorderAgentGetId
169  *
170  */
171 otError otBorderAgentSetId(otInstance *aInstance, const otBorderAgentId *aId);
172 
173 /**
174  * Sets the ephemeral key for a given timeout duration.
175  *
176  * Requires `OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE`.
177  *
178  * The ephemeral key can be set when the Border Agent is already running and is not currently connected to any external
179  * commissioner (i.e., it is in `OT_BORDER_AGENT_STATE_STARTED` state). Otherwise `OT_ERROR_INVALID_STATE` is returned.
180  *
181  * The given @p aKeyString is directly used as the ephemeral PSK (excluding the trailing null `\0` character ).
182  * The @p aKeyString length must be between `OT_BORDER_AGENT_MIN_EPHEMERAL_KEY_LENGTH` and
183  * `OT_BORDER_AGENT_MAX_EPHEMERAL_KEY_LENGTH`, inclusive.
184  *
185  * Setting the ephemeral key again before a previously set key has timed out will replace the previously set key and
186  * reset the timeout.
187  *
188  * While the timeout interval is in effect, the ephemeral key can be used only once by an external commissioner to
189  * connect. Once the commissioner disconnects, the ephemeral key is cleared, and the Border Agent reverts to using
190  * PSKc.
191  *
192  * @param[in] aInstance    The OpenThread instance.
193  * @param[in] aKeyString   The ephemeral key string (used as PSK excluding the trailing null `\0` character).
194  * @param[in] aTimeout     The timeout duration in milliseconds to use the ephemeral key.
195  *                         If zero, the default `OT_BORDER_AGENT_DEFAULT_EPHEMERAL_KEY_TIMEOUT` value will be used.
196  *                         If the given timeout value is larger than `OT_BORDER_AGENT_MAX_EPHEMERAL_KEY_TIMEOUT`, the
197  *                         max value `OT_BORDER_AGENT_MAX_EPHEMERAL_KEY_TIMEOUT` will be used instead.
198  * @param[in] aUdpPort     The UDP port to use with ephemeral key. If zero, an ephemeral port will be used.
199  *                         `otBorderAgentGetUdpPort()` will return the current UDP port being used.
200  *
201  * @retval OT_ERROR_NONE           Successfully set the ephemeral key.
202  * @retval OT_ERROR_INVALID_STATE  Border Agent is not running or it is connected to an external commissioner.
203  * @retval OT_ERROR_INVALID_ARGS   The given @p aKeyString is not valid (too short or too long).
204  * @retval OT_ERROR_FAILED         Failed to set the key (e.g., could not bind to UDP port).
205 
206  *
207  */
208 otError otBorderAgentSetEphemeralKey(otInstance *aInstance,
209                                      const char *aKeyString,
210                                      uint32_t    aTimeout,
211                                      uint16_t    aUdpPort);
212 
213 /**
214  * Cancels the ephemeral key that is in use.
215  *
216  * Requires `OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE`.
217  *
218  * Can be used to cancel a previously set ephemeral key before it times out. If the Border Agent is not running or
219  * there is no ephemeral key in use, calling this function has no effect.
220  *
221  * If a commissioner is connected using the ephemeral key and is currently active, calling this function does not
222  * change its state. In this case the `otBorderAgentIsEphemeralKeyActive()` will continue to return `TRUE` until the
223  * commissioner disconnects.
224  *
225  * @param[in] aInstance    The OpenThread instance.
226  *
227  */
228 void otBorderAgentClearEphemeralKey(otInstance *aInstance);
229 
230 /**
231  * Indicates whether or not an ephemeral key is currently active.
232  *
233  * Requires `OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE`.
234  *
235  * @param[in] aInstance    The OpenThread instance.
236  *
237  * @retval TRUE    An ephemeral key is active.
238  * @retval FALSE   No ephemeral key is active.
239  *
240  */
241 bool otBorderAgentIsEphemeralKeyActive(otInstance *aInstance);
242 
243 /**
244  * Callback function pointer to signal changes related to the Border Agent's ephemeral key.
245  *
246  * This callback is invoked whenever:
247  *
248  * - The Border Agent starts using an ephemeral key.
249  * - Any parameter related to the ephemeral key, such as the port number, changes.
250  * - The Border Agent stops using the ephemeral key due to:
251  *   - A direct call to `otBorderAgentClearEphemeralKey()`.
252  *   - The ephemeral key timing out.
253  *   - An external commissioner successfully using the key to connect and then disconnecting.
254  *   - Reaching the maximum number of allowed failed connection attempts.
255  *
256  * Any OpenThread API, including `otBorderAgent` APIs, can be safely called from this callback.
257  *
258  * @param[in] aContext   A pointer to an arbitrary context (provided when callback is set).
259  *
260  */
261 typedef void (*otBorderAgentEphemeralKeyCallback)(void *aContext);
262 
263 /**
264  * Sets the callback function used by the Border Agent to notify any changes related to use of ephemeral key.
265  *
266  * Requires `OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE`.
267  *
268  * A subsequent call to this function will replace any previously set callback.
269  *
270  * @param[in] aInstance    The OpenThread instance.
271  * @param[in] aCallback    The callback function pointer.
272  * @param[in] aContext     The arbitrary context to use with callback.
273  *
274  */
275 void otBorderAgentSetEphemeralKeyCallback(otInstance                       *aInstance,
276                                           otBorderAgentEphemeralKeyCallback aCallback,
277                                           void                             *aContext);
278 
279 /**
280  * @}
281  *
282  */
283 
284 #ifdef __cplusplus
285 } // end of extern "C"
286 #endif
287 
288 #endif // OPENTHREAD_BORDER_AGENT_H_
289