2 * Copyright (c) 2020, The OpenThread Authors.
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.
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.
31 * This file includes definitions for Thread helper.
34 #ifndef OTBR_THREAD_HELPER_HPP_
35 #define OTBR_THREAD_HELPER_HPP_
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>
51 #include "common/logging.hpp"
55 class ControllerOpenThread;
63 * This class implements Thread helper.
68 using DeviceRoleHandler = std::function<void(otDeviceRole)>;
69 using ScanHandler = std::function<void(otError, const std::vector<otActiveScanResult> &)>;
70 using ResultHandler = std::function<void(otError)>;
73 * The constructor of a Thread helper.
75 * @param[in] aInstance The Thread instance.
76 * @param[in] aNcp The ncp controller.
79 ThreadHelper(otInstance *aInstance, otbr::Ncp::ControllerOpenThread *aNcp);
82 * This method adds a callback for device role change.
84 * @param[in] aHandler The device role handler.
87 void AddDeviceRoleHandler(DeviceRoleHandler aHandler);
90 * This method permits unsecure join on port.
92 * @param[in] aPort The port number.
93 * @param[in] aSeconds The timeout to close the port, 0 for never close.
95 * @returns The error value of underlying OpenThread api calls.
98 otError PermitUnsecureJoin(uint16_t aPort, uint32_t aSeconds);
101 * This method performs a Thread network scan.
103 * @param[in] aHandler The scan result handler.
106 void Scan(ScanHandler aHandler);
109 * This method attaches the device to the Thread network.
111 * @note The joiner start and the attach proccesses are exclusive
113 * @param[in] aNetworkName The network name.
114 * @param[in] aPanId The pan id, UINT16_MAX for random.
115 * @param[in] aExtPanId The extended pan id, UINT64_MAX for random.
116 * @param[in] aMasterKey The master key, empty for random.
117 * @param[in] aPSKc The pre-shared commissioner key, empty for random.
118 * @param[in] aChannelMask A bitmask for valid channels, will random select one.
119 * @param[in] aHandler The attach result handler.
122 void Attach(const std::string & aNetworkName,
125 const std::vector<uint8_t> &aMasterKey,
126 const std::vector<uint8_t> &aPSKc,
127 uint32_t aChannelMask,
128 ResultHandler aHandler);
131 * This method attaches the device to the Thread network.
133 * @note The joiner start and the attach proccesses are exclusive, and the
134 * network parameter will be set through the active dataset.
136 * @param[in] aHandler The attach result handler.
139 void Attach(ResultHandler aHandler);
142 * This method resets the OpenThread stack.
144 * @returns The error value of underlying OpenThread api calls.
150 * This method triggers a thread join process.
152 * @note The joiner start and the attach proccesses are exclusive
154 * @param[in] aPskd The pre-shared key for device.
155 * @param[in] aProvisioningUrl The provision url.
156 * @param[in] aVendorName The vendor name.
157 * @param[in] aVendorModel The vendor model.
158 * @param[in] aVendorSwVersion The vendor software version.
159 * @param[in] aVendorData The vendor custom data.
160 * @param[in] aHandler The join result handler.
163 void JoinerStart(const std::string &aPskd,
164 const std::string &aProvisioningUrl,
165 const std::string &aVendorName,
166 const std::string &aVendorModel,
167 const std::string &aVendorSwVersion,
168 const std::string &aVendorData,
169 ResultHandler aHandler);
172 * This method tries to restore the network after reboot
174 * @returns The error value of underlying OpenThread api calls.
177 otError TryResumeNetwork(void);
180 * This method returns the underlying OpenThread instance.
182 * @returns The underlying instance.
185 otInstance *GetInstance(void) { return mInstance; }
188 * This method handles OpenThread state changed notification.
190 * @param[in] aFlags A bit-field indicating specific state that has changed. See `OT_CHANGED_*` definitions.
193 void StateChangedCallback(otChangedFlags aFlags);
196 * This method logs OpenThread action result.
198 * @param[in] aAction The action OpenThread performs.
199 * @param[in] aError The action result.
202 static void LogOpenThreadResult(const char *aAction, otError aError)
204 otbrLog((aError == OT_ERROR_NONE ? OTBR_LOG_INFO : OTBR_LOG_WARNING), "%s: %s", aAction,
205 otThreadErrorToString(aError));
209 static void sActiveScanHandler(otActiveScanResult *aResult, void *aThreadHelper);
210 void ActiveScanHandler(otActiveScanResult *aResult);
212 static void sJoinerCallback(otError aError, void *aThreadHelper);
213 void JoinerCallback(otError aResult);
215 void RandomFill(void *aBuf, size_t size);
216 uint8_t RandomChannelFromChannelMask(uint32_t aChannelMask);
218 otInstance *mInstance;
220 otbr::Ncp::ControllerOpenThread *mNcp;
222 ScanHandler mScanHandler;
223 std::vector<otActiveScanResult> mScanResults;
225 std::vector<DeviceRoleHandler> mDeviceRoleHandlers;
227 std::map<uint16_t, std::chrono::steady_clock::time_point> mUnsecurePortCloseTime;
229 ResultHandler mAttachHandler;
230 ResultHandler mJoinerHandler;
232 std::random_device mRandomDevice;
238 #endif // OTBR_THREAD_HELPER_HPP_