0.01.00
commissioner.hpp
Go to the documentation of this file.
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 
34 #ifndef COMMISSIONER_HPP_
35 #define COMMISSIONER_HPP_
36 
37 #include "openthread-core-config.h"
38 
40 
41 #include "coap/coap.hpp"
42 #include "coap/coap_secure.hpp"
43 #include "common/locator.hpp"
44 #include "common/timer.hpp"
45 #include "mac/mac_frame.hpp"
47 #include "meshcop/dtls.hpp"
50 #include "net/udp6.hpp"
51 #include "thread/mle.hpp"
52 
53 namespace ot {
54 
55 class ThreadNetif;
56 
57 namespace MeshCoP {
58 
60 {
61 public:
68  Commissioner(otInstance &aInstance);
69 
76  otError Start(void);
77 
84  otError Stop(void);
85 
90  void ClearJoiners(void);
91 
103  otError AddJoiner(const Mac::ExtAddress *aEui64, const char *aPSKd, uint32_t aTimeout);
104 
115  otError RemoveJoiner(const Mac::ExtAddress *aEui64, uint32_t aDelay);
116 
126  otError SetProvisioningUrl(const char *aProvisioningUrl);
127 
134  uint16_t GetSessionId(void) const;
135 
142  bool IsActive(void) const { return mState == OT_COMMISSIONER_STATE_ACTIVE; }
143 
154  otCommissionerState GetState(void) const;
155 
166  otError SendMgmtCommissionerGetRequest(const uint8_t *aTlvs, uint8_t aLength);
167 
180  const uint8_t *aTlvs, uint8_t aLength);
181 
196  static otError GeneratePSKc(const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId,
197  uint8_t *aPSKc);
198 
205  AnnounceBeginClient &GetAnnounceBeginClient(void) { return mAnnounceBegin; }
206 
213  EnergyScanClient &GetEnergyScanClient(void) { return mEnergyScan; }
214 
221  PanIdQueryClient &GetPanIdQueryClient(void) { return mPanIdQuery; }
222 
223 private:
224  enum
225  {
226  kPetitionAttemptDelay = 5,
227  kPetitionRetryCount = 2,
228  kPetitionRetryDelay = 1,
229  kKeepAliveTimeout = 50,
230  kRemoveJoinerDelay = 20,
231  };
232 
233  void AddCoapResources(void);
234  void RemoveCoapResources(void);
235 
236  static void HandleTimer(Timer &aTimer);
237  void HandleTimer(void);
238 
239  static void HandleJoinerExpirationTimer(Timer &aTimer);
240  void HandleJoinerExpirationTimer(void);
241 
242  void UpdateJoinerExpirationTimer(void);
243 
244  static void HandleMgmtCommissionerSetResponse(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
245  const otMessageInfo *aMessageInfo, otError aResult);
246  void HandleMgmtCommissisonerSetResponse(Coap::Header *aHeader, Message *aMessage,
247  const Ip6::MessageInfo *aMessageInfo, otError aResult);
248  static void HandleMgmtCommissionerGetResponse(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
249  const otMessageInfo *aMessageInfo, otError aResult);
250  void HandleMgmtCommissisonerGetResponse(Coap::Header *aHeader, Message *aMessage,
251  const Ip6::MessageInfo *aMessageInfo, otError aResult);
252  static void HandleLeaderPetitionResponse(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
253  const otMessageInfo *aMessageInfo, otError aResult);
254  void HandleLeaderPetitionResponse(Coap::Header *aHeader, Message *aMessage,
255  const Ip6::MessageInfo *aMessageInfo, otError aResult);
256  static void HandleLeaderKeepAliveResponse(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
257  const otMessageInfo *aMessageInfo, otError aResult);
258  void HandleLeaderKeepAliveResponse(Coap::Header *aHeader, Message *aMessage,
259  const Ip6::MessageInfo *aMessageInfo, otError aResult);
260 
261  static void HandleRelayReceive(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
262  const otMessageInfo *aMessageInfo);
263  void HandleRelayReceive(Coap::Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
264 
265  static void HandleDatasetChanged(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
266  const otMessageInfo *aMessageInfo);
267  void HandleDatasetChanged(Coap::Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
268 
269  static void HandleJoinerFinalize(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
270  const otMessageInfo *aMessageInfo);
271  void HandleJoinerFinalize(Coap::Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
272 
273  void SendJoinFinalizeResponse(const Coap::Header &aRequestHeader, StateTlv::State aState);
274 
275  static otError SendRelayTransmit(void *aContext, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
276  otError SendRelayTransmit(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
277 
278  otError SendCommissionerSet(void);
279  otError SendPetition(void);
280  otError SendKeepAlive(void);
281 
282  static Commissioner &GetOwner(const Context &aContext);
283 
284  otCommissionerState mState;
285 
286  struct Joiner
287  {
288  Mac::ExtAddress mJoinerId;
289  uint32_t mExpirationTime;
290  char mPsk[Dtls::kPskMaxLength + 1];
291  bool mValid : 1;
292  bool mAny : 1;
293  };
294  Joiner mJoiners[OPENTHREAD_CONFIG_MAX_JOINER_ENTRIES];
295 
296  union
297  {
298  uint8_t mJoinerIid[8];
299  uint64_t mJoinerIid64;
300  };
301  uint16_t mJoinerPort;
302  uint16_t mJoinerRloc;
303  TimerMilli mJoinerExpirationTimer;
304 
305  TimerMilli mTimer;
306  uint16_t mSessionId;
307  uint8_t mTransmitAttempts;
308 
309  Coap::Resource mRelayReceive;
310  Coap::Resource mDatasetChanged;
311  Coap::Resource mJoinerFinalize;
312 
313  AnnounceBeginClient mAnnounceBegin;
314  EnergyScanClient mEnergyScan;
315  PanIdQueryClient mPanIdQuery;
316 };
317 
318 } // namespace MeshCoP
319 } // namespace ot
320 
321 #endif // COMMISSIONER_HPP_
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
Definition: cli.cpp:90
EnergyScanClient & GetEnergyScanClient(void)
This method returns a reference to the EnergyScanClient instance.
Definition: commissioner.hpp:213
otError Start(void)
This method starts the Commissioner service.
This file includes definitions for using mbedTLS.
This file includes definitions for MLE functionality required by the Thread Child, Router, and Leader roles.
This class implements CoAP resource handling.
Definition: coap.hpp:200
Definition: commissioner.hpp:59
This class implements handling PANID Query Requests.
Definition: panid_query_client.hpp:54
State
State values.
Definition: meshcop_tlvs.hpp:915
This file includes definitions for CoAP client and server functionality.
This file includes definitions for locator class for OpenThread objects.
This class implements handling PANID Query Requests.
Definition: energy_scan_client.hpp:54
AnnounceBeginClient & GetAnnounceBeginClient(void)
This method returns a reference to the AnnounceBeginClient instance.
Definition: commissioner.hpp:205
Commissioner role is active.
Definition: commissioner.h:63
otError AddJoiner(const Mac::ExtAddress *aEui64, const char *aPSKd, uint32_t aTimeout)
This method adds a Joiner entry.
This class implements message information for an IPv6 message.
Definition: socket.hpp:57
This structure represents a Commissioning Dataset.
Definition: types.h:585
This type points to an OpenThread message buffer.
Definition: types.h:479
This class implements CoAP header generation and parsing.
Definition: coap_header.hpp:72
static otError GeneratePSKc(const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId, uint8_t *aPSKc)
This static method generates PSKc.
PanIdQueryClient & GetPanIdQueryClient(void)
This method returns a reference to the PanIdQueryClient instance.
Definition: commissioner.hpp:221
This class implements a timer.
Definition: timer.hpp:69
This class implements the millisecond timer.
Definition: timer.hpp:145
bool IsActive(void) const
This method indicates whether or not the Commissioner role is active.
Definition: commissioner.hpp:142
This file includes definitions for UDP/IPv6 sockets.
This structure represents an IEEE 802.15.4 Extended Address.
Definition: mac_frame.hpp:84
This file includes definitions for generating and processing IEEE 802.15.4 MAC frames.
otCommissionerState
This enumeration defines the Commissioner State.
Definition: commissioner.h:59
This class implements definitions for maintaining a pointer to arbitrary context information.
Definition: context.hpp:61
This class represents a message.
Definition: message.hpp:195
otError RemoveJoiner(const Mac::ExtAddress *aEui64, uint32_t aDelay)
This method removes a Joiner entry.
otCommissionerState GetState(void) const
This function returns the Commissioner State.
This class implements locator for otInstance object.
Definition: locator.hpp:63
This file includes definitions for responding to PANID Query Requests.
This file includes definitions for responding to Announce Requests.
This class implements handling Announce Begin Requests.
Definition: announce_begin_client.hpp:50
This file includes definitions for the multiplexed timer service.
#define OPENTHREAD_CONFIG_MAX_JOINER_ENTRIES
The maximum number of Joiner entries maintained by the Commissioner.
This structure represents a CoAP header.
Definition: coap.h:172
otError SendMgmtCommissionerGetRequest(const uint8_t *aTlvs, uint8_t aLength)
This method sends MGMT_COMMISSIONER_GET.
otError SetProvisioningUrl(const char *aProvisioningUrl)
This method sets the Provisioning URL.
void ClearJoiners(void)
This method clears all Joiner entries.
This structure represents the local and peer IPv6 socket addresses.
Definition: types.h:436
This file includes definitions for responding to PANID Query Requests.
otError SendMgmtCommissionerSetRequest(const otCommissioningDataset &aDataset, const uint8_t *aTlvs, uint8_t aLength)
This method sends MGMT_COMMISSIONER_SET.
uint16_t GetSessionId(void) const
This method returns the Commissioner Session ID.
This file includes functions for the Thread Commissioner role.
otError Stop(void)
This method stops the Commissioner service.
This file includes definitions for the secure CoAP agent.
Commissioner(otInstance &aInstance)
This constructor initializes the Commissioner object.
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
This file includes compile-time configuration constants for OpenThread.