openthread-br  0.3.0-72c0388
thread_helper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, 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 OTBR_THREAD_HELPER_HPP_
35 #define OTBR_THREAD_HELPER_HPP_
36 
37 #include <chrono>
38 #include <functional>
39 #include <map>
40 #include <random>
41 #include <string>
42 #include <vector>
43 
44 #include <openthread/instance.h>
45 #include <openthread/ip6.h>
46 #include <openthread/jam_detection.h>
47 #include <openthread/joiner.h>
48 #include <openthread/netdata.h>
49 #include <openthread/thread.h>
50 
51 namespace otbr {
52 namespace Ncp {
53 class ControllerOpenThread;
54 }
55 } // namespace otbr
56 
57 namespace otbr {
58 namespace agent {
59 
64 {
65 public:
66  using DeviceRoleHandler = std::function<void(otDeviceRole)>;
67  using ScanHandler = std::function<void(otError, const std::vector<otActiveScanResult> &)>;
68  using EnergyScanHandler = std::function<void(otError, const std::vector<otEnergyScanResult> &)>;
69  using ResultHandler = std::function<void(otError)>;
70  using AttachHandler = std::function<void(otError, int64_t)>;
71  using UpdateMeshCopTxtHandler = std::function<void(std::map<std::string, std::vector<uint8_t>>)>;
72  using DatasetChangeHandler = std::function<void(const otOperationalDatasetTlvs &)>;
73 
81  ThreadHelper(otInstance *aInstance, otbr::Ncp::ControllerOpenThread *aNcp);
82 
89  void AddDeviceRoleHandler(DeviceRoleHandler aHandler);
90 
96  void AddActiveDatasetChangeHandler(DatasetChangeHandler aHandler);
97 
107  otError PermitUnsecureJoin(uint16_t aPort, uint32_t aSeconds);
108 
115  void Scan(ScanHandler aHandler);
116 
124  void EnergyScan(uint32_t aScanDuration, EnergyScanHandler aHandler);
125 
140  void Attach(const std::string & aNetworkName,
141  uint16_t aPanId,
142  uint64_t aExtPanId,
143  const std::vector<uint8_t> &aNetworkKey,
144  const std::vector<uint8_t> &aPSKc,
145  uint32_t aChannelMask,
146  AttachHandler aHandler);
147 
154  otError Detach(void);
155 
165  void Attach(AttachHandler aHandler);
166 
174  void AttachAllNodesTo(const std::vector<uint8_t> &aDatasetTlvs, AttachHandler aHandler);
175 
182  otError Reset(void);
183 
198  void JoinerStart(const std::string &aPskd,
199  const std::string &aProvisioningUrl,
200  const std::string &aVendorName,
201  const std::string &aVendorModel,
202  const std::string &aVendorSwVersion,
203  const std::string &aVendorData,
204  ResultHandler aHandler);
205 
212  otError TryResumeNetwork(void);
213 
220  otInstance *GetInstance(void) { return mInstance; }
221 
228  void StateChangedCallback(otChangedFlags aFlags);
229 
230 #if OTBR_ENABLE_DBUS_SERVER
231 
237  void SetUpdateMeshCopTxtHandler(UpdateMeshCopTxtHandler aHandler)
238  {
239  mUpdateMeshCopTxtHandler = std::move(aHandler);
240  }
241 
248  void OnUpdateMeshCopTxt(std::map<std::string, std::vector<uint8_t>> aUpdate);
249 #endif
250 
251  void DetachGracefully(ResultHandler aHandler);
252 
260  static void LogOpenThreadResult(const char *aAction, otError aError);
261 
262 private:
263  static void ActiveScanHandler(otActiveScanResult *aResult, void *aThreadHelper);
264  void ActiveScanHandler(otActiveScanResult *aResult);
265 
266  static void EnergyScanCallback(otEnergyScanResult *aResult, void *aThreadHelper);
267  void EnergyScanCallback(otEnergyScanResult *aResult);
268 
269  static void JoinerCallback(otError aError, void *aThreadHelper);
270  void JoinerCallback(otError aResult);
271 
272  static void MgmtSetResponseHandler(otError aResult, void *aContext);
273  void MgmtSetResponseHandler(otError aResult);
274 
275  static void DetachGracefullyCallback(void *aContext);
276  void DetachGracefullyCallback(void);
277 
278  void RandomFill(void *aBuf, size_t size);
279  uint8_t RandomChannelFromChannelMask(uint32_t aChannelMask);
280 
281  void ActiveDatasetChangedCallback(void);
282 
283  otInstance *mInstance;
284 
286 
287  ScanHandler mScanHandler;
288  std::vector<otActiveScanResult> mScanResults;
289  EnergyScanHandler mEnergyScanHandler;
290  std::vector<otEnergyScanResult> mEnergyScanResults;
291 
292  std::vector<DeviceRoleHandler> mDeviceRoleHandlers;
293  std::vector<DatasetChangeHandler> mActiveDatasetChangeHandlers;
294 
295  std::map<uint16_t, size_t> mUnsecurePortRefCounter;
296 
297  int64_t mAttachDelayMs = 0;
298  AttachHandler mAttachHandler;
299  ResultHandler mJoinerHandler;
300 
301  ResultHandler mDetachGracefullyHandler = nullptr;
302 
303  otOperationalDatasetTlvs mAttachPendingDatasetTlvs = {};
304 
305  std::random_device mRandomDevice;
306 
307 #if OTBR_ENABLE_DBUS_SERVER
308  UpdateMeshCopTxtHandler mUpdateMeshCopTxtHandler;
309 #endif
310 };
311 
312 } // namespace agent
313 } // namespace otbr
314 
315 #endif // OTBR_THREAD_HELPER_HPP_
otbr::agent::ThreadHelper::JoinerStart
void JoinerStart(const std::string &aPskd, const std::string &aProvisioningUrl, const std::string &aVendorName, const std::string &aVendorModel, const std::string &aVendorSwVersion, const std::string &aVendorData, ResultHandler aHandler)
Definition: thread_helper.cpp:440
otbr::agent::ThreadHelper::Detach
otError Detach(void)
Definition: thread_helper.cpp:421
otbr::agent::ThreadHelper::AddActiveDatasetChangeHandler
void AddActiveDatasetChangeHandler(DatasetChangeHandler aHandler)
Definition: thread_helper.cpp:719
otbr::agent::ThreadHelper
Definition: thread_helper.hpp:63
otbr::agent::ThreadHelper::Scan
void Scan(ScanHandler aHandler)
Definition: thread_helper.cpp:170
otbr::agent::ThreadHelper::ThreadHelper
ThreadHelper(otInstance *aInstance, otbr::Ncp::ControllerOpenThread *aNcp)
Definition: thread_helper.cpp:74
otbr::agent::ThreadHelper::GetInstance
otInstance * GetInstance(void)
Definition: thread_helper.hpp:220
otbr::agent::ThreadHelper::Attach
void Attach(const std::string &aNetworkName, uint16_t aPanId, uint64_t aExtPanId, const std::vector< uint8_t > &aNetworkKey, const std::vector< uint8_t > &aPSKc, uint32_t aChannelMask, AttachHandler aHandler)
Definition: thread_helper.cpp:302
otbr::agent::ThreadHelper::StateChangedCallback
void StateChangedCallback(otChangedFlags aFlags)
Definition: thread_helper.cpp:80
otbr::agent::ThreadHelper::AddDeviceRoleHandler
void AddDeviceRoleHandler(DeviceRoleHandler aHandler)
Definition: thread_helper.cpp:165
otbr::agent::ThreadHelper::Reset
otError Reset(void)
Definition: thread_helper.cpp:432
otbr::agent::ThreadHelper::PermitUnsecureJoin
otError PermitUnsecureJoin(uint16_t aPort, uint32_t aSeconds)
otbr::agent::ThreadHelper::AttachAllNodesTo
void AttachAllNodesTo(const std::vector< uint8_t > &aDatasetTlvs, AttachHandler aHandler)
Definition: thread_helper.cpp:527
otbr::agent::ThreadHelper::LogOpenThreadResult
static void LogOpenThreadResult(const char *aAction, otError aError)
Definition: thread_helper.cpp:515
otbr::agent::ThreadHelper::EnergyScan
void EnergyScan(uint32_t aScanDuration, EnergyScanHandler aHandler)
Definition: thread_helper.cpp:192
otbr::Ncp::ControllerOpenThread
Definition: ncp_openthread.hpp:64
otbr::agent::ThreadHelper::TryResumeNetwork
otError TryResumeNetwork(void)
Definition: thread_helper.cpp:493