Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2017, 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" AND |
| 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * @file |
| 30 | * This file implements minimal thread device required Spinel interface to the OpenThread stack. |
| 31 | */ |
| 32 | |
Jonathan Hui | 512d233 | 2019-07-12 14:57:15 -0700 | [diff] [blame] | 33 | #include "openthread-core-config.h" |
Yakun Xu | 96ed7cc | 2018-04-17 00:28:00 +0800 | [diff] [blame] | 34 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 35 | #include "ncp_base.hpp" |
| 36 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 37 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 38 | #include <openthread/border_router.h> |
| 39 | #endif |
Jonathan Hui | 01ebc18 | 2019-07-12 15:21:49 -0700 | [diff] [blame] | 40 | #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 41 | #include <openthread/channel_monitor.h> |
| 42 | #endif |
Jonathan Hui | aa82391 | 2019-07-15 12:03:36 -0700 | [diff] [blame] | 43 | #if OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE |
Abtin Keshavarzian | 1ca81fb | 2018-08-02 15:21:11 -0700 | [diff] [blame] | 44 | #include <openthread/child_supervision.h> |
| 45 | #endif |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 46 | #include <openthread/diag.h> |
| 47 | #include <openthread/icmp6.h> |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 48 | #if OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 49 | #include <openthread/jam_detection.h> |
| 50 | #endif |
| 51 | #include <openthread/ncp.h> |
Jonathan Hui | 926b9e8 | 2019-07-15 21:16:37 -0700 | [diff] [blame] | 52 | #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE |
Shu Chen | e69019f | 2018-06-27 02:46:32 +0800 | [diff] [blame] | 53 | #include <openthread/network_time.h> |
| 54 | #endif |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 55 | #include <openthread/platform/misc.h> |
| 56 | #include <openthread/platform/radio.h> |
| 57 | #if OPENTHREAD_FTD |
| 58 | #include <openthread/thread_ftd.h> |
| 59 | #endif |
Jonathan Hui | 149640d | 2019-07-16 11:06:03 -0700 | [diff] [blame] | 60 | #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 61 | #include <openthread/server.h> |
| 62 | #endif |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 63 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 64 | #include "common/code_utils.hpp" |
| 65 | #include "common/debug.hpp" |
Abtin Keshavarzian | 02c876e | 2017-11-13 08:27:57 -0800 | [diff] [blame] | 66 | #include "common/instance.hpp" |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 67 | #include "net/ip6.hpp" |
| 68 | |
| 69 | #if OPENTHREAD_MTD || OPENTHREAD_FTD |
| 70 | |
| 71 | namespace ot { |
| 72 | namespace Ncp { |
| 73 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 74 | static uint8_t BorderRouterConfigToFlagByte(const otBorderRouterConfig &aConfig) |
| 75 | { |
| 76 | uint8_t flags(0); |
| 77 | |
| 78 | if (aConfig.mPreferred) |
| 79 | { |
| 80 | flags |= SPINEL_NET_FLAG_PREFERRED; |
| 81 | } |
| 82 | |
| 83 | if (aConfig.mSlaac) |
| 84 | { |
| 85 | flags |= SPINEL_NET_FLAG_SLAAC; |
| 86 | } |
| 87 | |
| 88 | if (aConfig.mDhcp) |
| 89 | { |
| 90 | flags |= SPINEL_NET_FLAG_DHCP; |
| 91 | } |
| 92 | |
| 93 | if (aConfig.mDefaultRoute) |
| 94 | { |
| 95 | flags |= SPINEL_NET_FLAG_DEFAULT_ROUTE; |
| 96 | } |
| 97 | |
| 98 | if (aConfig.mConfigure) |
| 99 | { |
| 100 | flags |= SPINEL_NET_FLAG_CONFIGURE; |
| 101 | } |
| 102 | |
| 103 | if (aConfig.mOnMesh) |
| 104 | { |
| 105 | flags |= SPINEL_NET_FLAG_ON_MESH; |
| 106 | } |
| 107 | |
Jonathan Hui | 70cb48f | 2019-01-28 08:39:02 -0800 | [diff] [blame] | 108 | flags |= (static_cast<uint8_t>(aConfig.mPreference) << SPINEL_NET_FLAG_PREFERENCE_OFFSET); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 109 | |
| 110 | return flags; |
| 111 | } |
| 112 | |
| 113 | static uint8_t ExternalRoutePreferenceToFlagByte(int aPreference) |
| 114 | { |
| 115 | uint8_t flags; |
| 116 | |
| 117 | switch (aPreference) |
| 118 | { |
| 119 | case OT_ROUTE_PREFERENCE_LOW: |
| 120 | flags = SPINEL_ROUTE_PREFERENCE_LOW; |
| 121 | break; |
| 122 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 123 | case OT_ROUTE_PREFERENCE_HIGH: |
| 124 | flags = SPINEL_ROUTE_PREFERENCE_HIGH; |
| 125 | break; |
| 126 | |
Yakun Xu | 26694be | 2020-01-23 02:18:54 +0800 | [diff] [blame] | 127 | case OT_ROUTE_PREFERENCE_MED: |
| 128 | // fall through |
| 129 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 130 | default: |
| 131 | flags = SPINEL_ROUTE_PREFERENCE_MEDIUM; |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | return flags; |
| 136 | } |
| 137 | |
Jonathan Hui | dc9b032 | 2020-09-29 15:30:37 -0700 | [diff] [blame^] | 138 | uint8_t NcpBase::LinkFlagsToFlagByte(bool aRxOnWhenIdle, bool aDeviceType, bool aNetworkData) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 139 | { |
| 140 | uint8_t flags(0); |
| 141 | |
| 142 | if (aRxOnWhenIdle) |
| 143 | { |
| 144 | flags |= SPINEL_THREAD_MODE_RX_ON_WHEN_IDLE; |
| 145 | } |
| 146 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 147 | if (aDeviceType) |
| 148 | { |
Jonathan Hui | 5a48983 | 2018-09-27 09:33:03 -0700 | [diff] [blame] | 149 | flags |= SPINEL_THREAD_MODE_FULL_THREAD_DEV; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (aNetworkData) |
| 153 | { |
| 154 | flags |= SPINEL_THREAD_MODE_FULL_NETWORK_DATA; |
| 155 | } |
| 156 | |
| 157 | return flags; |
| 158 | } |
| 159 | |
Abtin Keshavarzian | a1b2d35 | 2019-05-19 00:05:21 -0700 | [diff] [blame] | 160 | otError NcpBase::EncodeNeighborInfo(const otNeighborInfo &aNeighborInfo) |
| 161 | { |
| 162 | otError error; |
| 163 | uint8_t modeFlags; |
| 164 | |
Jonathan Hui | dc9b032 | 2020-09-29 15:30:37 -0700 | [diff] [blame^] | 165 | modeFlags = LinkFlagsToFlagByte(aNeighborInfo.mRxOnWhenIdle, aNeighborInfo.mFullThreadDevice, |
| 166 | aNeighborInfo.mFullNetworkData); |
Abtin Keshavarzian | a1b2d35 | 2019-05-19 00:05:21 -0700 | [diff] [blame] | 167 | |
| 168 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 169 | |
| 170 | SuccessOrExit(error = mEncoder.WriteEui64(aNeighborInfo.mExtAddress)); |
| 171 | SuccessOrExit(error = mEncoder.WriteUint16(aNeighborInfo.mRloc16)); |
| 172 | SuccessOrExit(error = mEncoder.WriteUint32(aNeighborInfo.mAge)); |
| 173 | SuccessOrExit(error = mEncoder.WriteUint8(aNeighborInfo.mLinkQualityIn)); |
| 174 | SuccessOrExit(error = mEncoder.WriteInt8(aNeighborInfo.mAverageRssi)); |
| 175 | SuccessOrExit(error = mEncoder.WriteUint8(modeFlags)); |
| 176 | SuccessOrExit(error = mEncoder.WriteBool(aNeighborInfo.mIsChild)); |
| 177 | SuccessOrExit(error = mEncoder.WriteUint32(aNeighborInfo.mLinkFrameCounter)); |
| 178 | SuccessOrExit(error = mEncoder.WriteUint32(aNeighborInfo.mMleFrameCounter)); |
| 179 | SuccessOrExit(error = mEncoder.WriteInt8(aNeighborInfo.mLastRssi)); |
| 180 | |
| 181 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 182 | |
| 183 | exit: |
| 184 | return error; |
| 185 | } |
| 186 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 187 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_DATA_POLL_PERIOD>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 188 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 189 | return mEncoder.WriteUint32(otLinkGetPollPeriod(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 192 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_DATA_POLL_PERIOD>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 193 | { |
| 194 | uint32_t pollPeriod; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 195 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 196 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 197 | SuccessOrExit(error = mDecoder.ReadUint32(pollPeriod)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 198 | |
rongli | 3282398 | 2018-12-28 14:35:57 +0800 | [diff] [blame] | 199 | error = otLinkSetPollPeriod(mInstance, pollPeriod); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 200 | |
| 201 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 202 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 205 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_EXTENDED_ADDR>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 206 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 207 | return mEncoder.WriteEui64(*otLinkGetExtendedAddress(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Rafał Kuźnia | 5a7e190 | 2019-10-29 17:04:26 +0100 | [diff] [blame] | 210 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_MAX_RETRY_NUMBER_DIRECT>(void) |
| 211 | { |
| 212 | return mEncoder.WriteUint8(otLinkGetMaxFrameRetriesDirect(mInstance)); |
| 213 | } |
| 214 | |
| 215 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_MAX_RETRY_NUMBER_DIRECT>(void) |
| 216 | { |
| 217 | uint8_t maxFrameRetriesDirect; |
| 218 | otError error = OT_ERROR_NONE; |
| 219 | |
| 220 | SuccessOrExit(error = mDecoder.ReadUint8(maxFrameRetriesDirect)); |
| 221 | otLinkSetMaxFrameRetriesDirect(mInstance, maxFrameRetriesDirect); |
| 222 | |
| 223 | exit: |
| 224 | return error; |
| 225 | } |
| 226 | |
Abtin Keshavarzian | 31698ec | 2018-07-02 08:43:45 -0700 | [diff] [blame] | 227 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_PHY_CHAN_SUPPORTED>(void) |
| 228 | { |
| 229 | uint32_t newMask = 0; |
| 230 | otError error = OT_ERROR_NONE; |
| 231 | |
| 232 | SuccessOrExit(error = DecodeChannelMask(newMask)); |
| 233 | error = otLinkSetSupportedChannelMask(mInstance, newMask); |
| 234 | |
| 235 | exit: |
| 236 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 239 | otError NcpBase::CommandHandler_NET_CLEAR(uint8_t aHeader) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 240 | { |
Abtin Keshavarzian | 9ce57b2 | 2017-12-05 18:33:53 -0800 | [diff] [blame] | 241 | return PrepareLastStatusResponse(aHeader, ThreadErrorToSpinelStatus(otInstanceErasePersistentInfo(mInstance))); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 244 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_SAVED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 245 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 246 | return mEncoder.WriteBool(otDatasetIsCommissioned(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 249 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_IF_UP>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 250 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 251 | return mEncoder.WriteBool(otIp6IsEnabled(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 254 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_IF_UP>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 255 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 256 | bool enabled = false; |
| 257 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 258 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 259 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 260 | |
| 261 | error = otIp6SetEnabled(mInstance, enabled); |
| 262 | |
| 263 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 264 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 267 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_STACK_UP>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 268 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 269 | return mEncoder.WriteBool(otThreadGetDeviceRole(mInstance) != OT_DEVICE_ROLE_DISABLED); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 270 | } |
| 271 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 272 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_STACK_UP>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 273 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 274 | bool enabled = false; |
| 275 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 276 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 277 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 278 | |
| 279 | // If the value has changed... |
| 280 | if ((enabled != false) != (otThreadGetDeviceRole(mInstance) != OT_DEVICE_ROLE_DISABLED)) |
| 281 | { |
| 282 | if (enabled != false) |
| 283 | { |
| 284 | error = otThreadSetEnabled(mInstance, true); |
Abtin Keshavarzian | ecd2845 | 2018-04-05 10:20:47 -0700 | [diff] [blame] | 285 | StartLegacy(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
| 289 | error = otThreadSetEnabled(mInstance, false); |
Abtin Keshavarzian | ecd2845 | 2018-04-05 10:20:47 -0700 | [diff] [blame] | 290 | StopLegacy(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 295 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 296 | } |
| 297 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 298 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_ROLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 299 | { |
| 300 | spinel_net_role_t role(SPINEL_NET_ROLE_DETACHED); |
| 301 | |
| 302 | switch (otThreadGetDeviceRole(mInstance)) |
| 303 | { |
| 304 | case OT_DEVICE_ROLE_DISABLED: |
| 305 | case OT_DEVICE_ROLE_DETACHED: |
| 306 | role = SPINEL_NET_ROLE_DETACHED; |
| 307 | break; |
| 308 | |
| 309 | case OT_DEVICE_ROLE_CHILD: |
| 310 | role = SPINEL_NET_ROLE_CHILD; |
| 311 | break; |
| 312 | |
| 313 | case OT_DEVICE_ROLE_ROUTER: |
| 314 | role = SPINEL_NET_ROLE_ROUTER; |
| 315 | break; |
| 316 | |
| 317 | case OT_DEVICE_ROLE_LEADER: |
| 318 | role = SPINEL_NET_ROLE_LEADER; |
| 319 | break; |
| 320 | } |
| 321 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 322 | return mEncoder.WriteUint8(role); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 323 | } |
| 324 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 325 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_ROLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 326 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 327 | unsigned int role = 0; |
| 328 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 329 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 330 | SuccessOrExit(error = mDecoder.ReadUintPacked(role)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 331 | |
| 332 | switch (role) |
| 333 | { |
| 334 | case SPINEL_NET_ROLE_DETACHED: |
| 335 | error = otThreadBecomeDetached(mInstance); |
| 336 | break; |
| 337 | |
| 338 | #if OPENTHREAD_FTD |
| 339 | case SPINEL_NET_ROLE_ROUTER: |
| 340 | error = otThreadBecomeRouter(mInstance); |
| 341 | break; |
| 342 | |
| 343 | case SPINEL_NET_ROLE_LEADER: |
| 344 | error = otThreadBecomeLeader(mInstance); |
| 345 | break; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 346 | #endif // OPENTHREAD_FTD |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 347 | |
| 348 | case SPINEL_NET_ROLE_CHILD: |
| 349 | error = otThreadBecomeChild(mInstance); |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 354 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 355 | } |
| 356 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 357 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_NETWORK_NAME>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 358 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 359 | return mEncoder.WriteUtf8(otThreadGetNetworkName(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 362 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_NETWORK_NAME>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 363 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 364 | const char *string = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 365 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 366 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 367 | SuccessOrExit(mDecoder.ReadUtf8(string)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 368 | |
| 369 | error = otThreadSetNetworkName(mInstance, string); |
| 370 | |
| 371 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 372 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 373 | } |
| 374 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 375 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_XPANID>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 376 | { |
Jonathan Hui | 9201973 | 2018-08-15 10:12:49 -0700 | [diff] [blame] | 377 | return mEncoder.WriteData(otThreadGetExtendedPanId(mInstance)->m8, sizeof(spinel_net_xpanid_t)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 378 | } |
| 379 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 380 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_XPANID>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 381 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 382 | const uint8_t *ptr = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 383 | uint16_t len; |
| 384 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 385 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 386 | SuccessOrExit(error = mDecoder.ReadData(ptr, len)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 387 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 388 | VerifyOrExit(len == sizeof(spinel_net_xpanid_t), error = OT_ERROR_PARSE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 389 | |
Jonathan Hui | 9201973 | 2018-08-15 10:12:49 -0700 | [diff] [blame] | 390 | error = otThreadSetExtendedPanId(mInstance, reinterpret_cast<const otExtendedPanId *>(ptr)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 391 | |
| 392 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 393 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 396 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_MASTER_KEY>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 397 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 398 | return mEncoder.WriteData(otThreadGetMasterKey(mInstance)->m8, OT_MASTER_KEY_SIZE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 401 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_MASTER_KEY>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 402 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 403 | const uint8_t *ptr = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 404 | uint16_t len; |
| 405 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 406 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 407 | SuccessOrExit(error = mDecoder.ReadData(ptr, len)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 408 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 409 | VerifyOrExit(len == OT_MASTER_KEY_SIZE, error = OT_ERROR_PARSE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 410 | |
| 411 | error = otThreadSetMasterKey(mInstance, reinterpret_cast<const otMasterKey *>(ptr)); |
| 412 | |
| 413 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 414 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 415 | } |
| 416 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 417 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_KEY_SEQUENCE_COUNTER>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 418 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 419 | return mEncoder.WriteUint32(otThreadGetKeySequenceCounter(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 420 | } |
| 421 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 422 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_KEY_SEQUENCE_COUNTER>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 423 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 424 | uint32_t keySeqCounter; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 425 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 426 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 427 | SuccessOrExit(error = mDecoder.ReadUint32(keySeqCounter)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 428 | |
| 429 | otThreadSetKeySequenceCounter(mInstance, keySeqCounter); |
| 430 | |
| 431 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 432 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 433 | } |
| 434 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 435 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_PARTITION_ID>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 436 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 437 | return mEncoder.WriteUint32(otThreadGetPartitionId(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 440 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_KEY_SWITCH_GUARDTIME>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 441 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 442 | return mEncoder.WriteUint32(otThreadGetKeySwitchGuardTime(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 443 | } |
| 444 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 445 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_KEY_SWITCH_GUARDTIME>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 446 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 447 | uint32_t keyGuardTime; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 448 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 449 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 450 | SuccessOrExit(error = mDecoder.ReadUint32(keyGuardTime)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 451 | |
| 452 | otThreadSetKeySwitchGuardTime(mInstance, keyGuardTime); |
| 453 | |
| 454 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 455 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 456 | } |
| 457 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 458 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_NETWORK_DATA_VERSION>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 459 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 460 | return mEncoder.WriteUint8(otNetDataGetVersion(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 461 | } |
| 462 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 463 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_STABLE_NETWORK_DATA_VERSION>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 464 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 465 | return mEncoder.WriteUint8(otNetDataGetStableVersion(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 466 | } |
| 467 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 468 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 469 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_NETWORK_DATA>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 470 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 471 | uint8_t networkData[255]; |
| 472 | uint8_t networkDataLen = 255; |
| 473 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 474 | IgnoreError(otBorderRouterGetNetData(mInstance, |
| 475 | false, // Stable? |
| 476 | networkData, &networkDataLen)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 477 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 478 | return mEncoder.WriteData(networkData, networkDataLen); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 481 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_STABLE_NETWORK_DATA>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 482 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 483 | uint8_t networkData[255]; |
| 484 | uint8_t networkDataLen = 255; |
| 485 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 486 | IgnoreError(otBorderRouterGetNetData(mInstance, |
| 487 | true, // Stable? |
| 488 | networkData, &networkDataLen)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 489 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 490 | return mEncoder.WriteData(networkData, networkDataLen); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 491 | } |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 492 | #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 493 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 494 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_LEADER_NETWORK_DATA>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 495 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 496 | uint8_t networkData[255]; |
| 497 | uint8_t networkDataLen = 255; |
| 498 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 499 | IgnoreError(otNetDataGet(mInstance, |
| 500 | false, // Stable? |
| 501 | networkData, &networkDataLen)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 502 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 503 | return mEncoder.WriteData(networkData, networkDataLen); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 504 | } |
| 505 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 506 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_STABLE_LEADER_NETWORK_DATA>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 507 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 508 | uint8_t networkData[255]; |
| 509 | uint8_t networkDataLen = 255; |
| 510 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 511 | IgnoreError(otNetDataGet(mInstance, |
| 512 | true, // Stable? |
| 513 | networkData, &networkDataLen)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 514 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 515 | return mEncoder.WriteData(networkData, networkDataLen); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 516 | } |
| 517 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 518 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_LEADER_RID>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 519 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 520 | return mEncoder.WriteUint8(otThreadGetLeaderRouterId(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 521 | } |
| 522 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 523 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_LEADER_ADDR>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 524 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 525 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 526 | otIp6Address address; |
| 527 | |
| 528 | error = otThreadGetLeaderRloc(mInstance, &address); |
| 529 | |
| 530 | if (error == OT_ERROR_NONE) |
| 531 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 532 | error = mEncoder.WriteIp6Address(address); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 533 | } |
| 534 | else |
| 535 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 536 | error = mEncoder.OverwriteWithLastStatusError(ThreadErrorToSpinelStatus(error)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | return error; |
| 540 | } |
| 541 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 542 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_PARENT>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 543 | { |
| 544 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 545 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 546 | otRouterInfo parentInfo; |
| 547 | |
| 548 | error = otThreadGetParentInfo(mInstance, &parentInfo); |
| 549 | |
| 550 | if (error == OT_ERROR_NONE) |
| 551 | { |
Abtin Keshavarzian | 52354e7 | 2018-07-04 13:40:40 -0700 | [diff] [blame] | 552 | if (parentInfo.mLinkEstablished) |
| 553 | { |
| 554 | int8_t averageRssi; |
| 555 | int8_t lastRssi; |
| 556 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 557 | IgnoreError(otThreadGetParentAverageRssi(mInstance, &averageRssi)); |
| 558 | IgnoreError(otThreadGetParentLastRssi(mInstance, &lastRssi)); |
Abtin Keshavarzian | 52354e7 | 2018-07-04 13:40:40 -0700 | [diff] [blame] | 559 | |
| 560 | SuccessOrExit(error = mEncoder.WriteEui64(parentInfo.mExtAddress)); |
| 561 | SuccessOrExit(error = mEncoder.WriteUint16(parentInfo.mRloc16)); |
| 562 | SuccessOrExit(error = mEncoder.WriteUint32(parentInfo.mAge)); |
| 563 | SuccessOrExit(error = mEncoder.WriteInt8(averageRssi)); |
| 564 | SuccessOrExit(error = mEncoder.WriteInt8(lastRssi)); |
| 565 | SuccessOrExit(error = mEncoder.WriteUint8(parentInfo.mLinkQualityIn)); |
| 566 | SuccessOrExit(error = mEncoder.WriteUint8(parentInfo.mLinkQualityOut)); |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | SuccessOrExit(error = mEncoder.OverwriteWithLastStatusError(SPINEL_STATUS_ITEM_NOT_FOUND)); |
| 571 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 572 | } |
| 573 | else |
| 574 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 575 | error = mEncoder.OverwriteWithLastStatusError(ThreadErrorToSpinelStatus(error)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 576 | } |
| 577 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 578 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 579 | return error; |
| 580 | } |
| 581 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 582 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_NEIGHBOR_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 583 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 584 | otError error = OT_ERROR_NONE; |
| 585 | otNeighborInfoIterator iter = OT_NEIGHBOR_INFO_ITERATOR_INIT; |
| 586 | otNeighborInfo neighInfo; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 587 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 588 | while (otThreadGetNextNeighborInfo(mInstance, &iter, &neighInfo) == OT_ERROR_NONE) |
| 589 | { |
Abtin Keshavarzian | a1b2d35 | 2019-05-19 00:05:21 -0700 | [diff] [blame] | 590 | SuccessOrExit(error = EncodeNeighborInfo(neighInfo)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 591 | } |
| 592 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 593 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 594 | return error; |
| 595 | } |
| 596 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 597 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_NEIGHBOR_TABLE_ERROR_RATES>(void) |
Abtin Keshavarzian | 10f6c4f | 2018-02-02 10:29:52 -0800 | [diff] [blame] | 598 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 599 | otError error = OT_ERROR_NONE; |
| 600 | otNeighborInfoIterator iter = OT_NEIGHBOR_INFO_ITERATOR_INIT; |
| 601 | otNeighborInfo neighInfo; |
Abtin Keshavarzian | 10f6c4f | 2018-02-02 10:29:52 -0800 | [diff] [blame] | 602 | |
| 603 | while (otThreadGetNextNeighborInfo(mInstance, &iter, &neighInfo) == OT_ERROR_NONE) |
| 604 | { |
| 605 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 606 | |
| 607 | SuccessOrExit(error = mEncoder.WriteEui64(neighInfo.mExtAddress)); |
| 608 | SuccessOrExit(error = mEncoder.WriteUint16(neighInfo.mRloc16)); |
| 609 | SuccessOrExit(error = mEncoder.WriteUint16(neighInfo.mFrameErrorRate)); |
| 610 | SuccessOrExit(error = mEncoder.WriteUint16(neighInfo.mMessageErrorRate)); |
| 611 | SuccessOrExit(error = mEncoder.WriteInt8(neighInfo.mAverageRssi)); |
| 612 | SuccessOrExit(error = mEncoder.WriteInt8(neighInfo.mLastRssi)); |
| 613 | |
| 614 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 615 | } |
| 616 | |
| 617 | exit: |
| 618 | return error; |
| 619 | } |
| 620 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 621 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_ASSISTING_PORTS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 622 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 623 | otError error = OT_ERROR_NONE; |
| 624 | uint8_t numEntries = 0; |
| 625 | const uint16_t *ports = otIp6GetUnsecurePorts(mInstance, &numEntries); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 626 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 627 | for (; numEntries != 0; ports++, numEntries--) |
| 628 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 629 | SuccessOrExit(error = mEncoder.WriteUint16(*ports)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 630 | } |
| 631 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 632 | exit: |
| 633 | return error; |
| 634 | } |
| 635 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 636 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_ASSISTING_PORTS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 637 | { |
Zhanglong Xia | c573daf | 2018-09-11 08:39:06 +0800 | [diff] [blame] | 638 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 639 | |
| 640 | // First, we need to remove all of the current assisting ports. |
Zhanglong Xia | c573daf | 2018-09-11 08:39:06 +0800 | [diff] [blame] | 641 | otIp6RemoveAllUnsecurePorts(mInstance); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 642 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 643 | while (mDecoder.GetRemainingLengthInStruct() >= sizeof(uint16_t)) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 644 | { |
| 645 | uint16_t port; |
| 646 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 647 | SuccessOrExit(mDecoder.ReadUint16(port)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 648 | |
| 649 | SuccessOrExit(error = otIp6AddUnsecurePort(mInstance, port)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 650 | } |
| 651 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 652 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 653 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 654 | if (error != OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 655 | { |
| 656 | // We had an error, but we've actually changed |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 657 | // the state of these ports, so we need to report |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 658 | // those incomplete changes via an asynchronous |
| 659 | // change event. |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 660 | IgnoreError( |
| 661 | WritePropertyValueIsFrame(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, SPINEL_PROP_THREAD_ASSISTING_PORTS)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | return error; |
| 665 | } |
| 666 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 667 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_ALLOW_LOCAL_NET_DATA_CHANGE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 668 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 669 | return mEncoder.WriteBool(mAllowLocalNetworkDataChange); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 670 | } |
| 671 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 672 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 673 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_ALLOW_LOCAL_NET_DATA_CHANGE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 674 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 675 | bool value = false; |
| 676 | otError error = OT_ERROR_NONE; |
| 677 | bool shouldRegisterWithLeader = false; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 678 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 679 | SuccessOrExit(error = mDecoder.ReadBool(value)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 680 | |
| 681 | // Register any net data changes on transition from `true` to `false`. |
| 682 | shouldRegisterWithLeader = (mAllowLocalNetworkDataChange == true) && (value == false); |
| 683 | |
| 684 | mAllowLocalNetworkDataChange = value; |
| 685 | |
| 686 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 687 | |
| 688 | if (shouldRegisterWithLeader) |
| 689 | { |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 690 | IgnoreError(otBorderRouterRegister(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return error; |
| 694 | } |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 695 | #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 696 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 697 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_ON_MESH_NETS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 698 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 699 | otError error = OT_ERROR_NONE; |
| 700 | otBorderRouterConfig borderRouterConfig; |
Abtin Keshavarzian | 2c8f50b | 2017-09-01 21:27:58 -0700 | [diff] [blame] | 701 | otNetworkDataIterator iter = OT_NETWORK_DATA_ITERATOR_INIT; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 702 | |
| 703 | // Fill from non-local network data first |
Abtin Keshavarzian | 2c8f50b | 2017-09-01 21:27:58 -0700 | [diff] [blame] | 704 | while (otNetDataGetNextOnMeshPrefix(mInstance, &iter, &borderRouterConfig) == OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 705 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 706 | SuccessOrExit(error = mEncoder.OpenStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 707 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 708 | SuccessOrExit(error = mEncoder.WriteIp6Address(borderRouterConfig.mPrefix.mPrefix)); |
| 709 | SuccessOrExit(error = mEncoder.WriteUint8(borderRouterConfig.mPrefix.mLength)); |
| 710 | SuccessOrExit(error = mEncoder.WriteBool(borderRouterConfig.mStable)); |
| 711 | SuccessOrExit(error = mEncoder.WriteUint8(BorderRouterConfigToFlagByte(borderRouterConfig))); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 712 | SuccessOrExit(error = mEncoder.WriteBool(false)); // isLocal |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 713 | SuccessOrExit(error = mEncoder.WriteUint16(borderRouterConfig.mRloc16)); |
| 714 | |
| 715 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 716 | } |
| 717 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 718 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Abtin Keshavarzian | 2c8f50b | 2017-09-01 21:27:58 -0700 | [diff] [blame] | 719 | |
| 720 | iter = OT_NETWORK_DATA_ITERATOR_INIT; |
| 721 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 722 | // Fill from local network data last |
Abtin Keshavarzian | 2c8f50b | 2017-09-01 21:27:58 -0700 | [diff] [blame] | 723 | while (otBorderRouterGetNextOnMeshPrefix(mInstance, &iter, &borderRouterConfig) == OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 724 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 725 | SuccessOrExit(error = mEncoder.OpenStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 726 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 727 | SuccessOrExit(error = mEncoder.WriteIp6Address(borderRouterConfig.mPrefix.mPrefix)); |
| 728 | SuccessOrExit(error = mEncoder.WriteUint8(borderRouterConfig.mPrefix.mLength)); |
| 729 | SuccessOrExit(error = mEncoder.WriteBool(borderRouterConfig.mStable)); |
| 730 | SuccessOrExit(error = mEncoder.WriteUint8(BorderRouterConfigToFlagByte(borderRouterConfig))); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 731 | SuccessOrExit(error = mEncoder.WriteBool(true)); // isLocal |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 732 | SuccessOrExit(error = mEncoder.WriteUint16(borderRouterConfig.mRloc16)); |
| 733 | |
| 734 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 735 | } |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 736 | #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 737 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 738 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 739 | return error; |
| 740 | } |
| 741 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 742 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 743 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_THREAD_ON_MESH_NETS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 744 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 745 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 746 | otBorderRouterConfig borderRouterConfig; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 747 | bool stable = false; |
| 748 | uint8_t flags = 0; |
| 749 | uint8_t prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 750 | |
| 751 | memset(&borderRouterConfig, 0, sizeof(otBorderRouterConfig)); |
| 752 | |
Abtin Keshavarzian | 31ce3d4 | 2019-08-01 10:30:08 -0700 | [diff] [blame] | 753 | VerifyOrExit(mAllowLocalNetworkDataChange, error = OT_ERROR_INVALID_STATE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 754 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 755 | SuccessOrExit(error = mDecoder.ReadIp6Address(borderRouterConfig.mPrefix.mPrefix)); |
Łukasz Duda | 7b084ec | 2017-10-25 17:54:17 +0200 | [diff] [blame] | 756 | SuccessOrExit(error = mDecoder.ReadUint8(prefixLength)); |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 757 | SuccessOrExit(error = mDecoder.ReadBool(stable)); |
| 758 | SuccessOrExit(error = mDecoder.ReadUint8(flags)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 759 | |
Łukasz Duda | 7b084ec | 2017-10-25 17:54:17 +0200 | [diff] [blame] | 760 | borderRouterConfig.mPrefix.mLength = prefixLength; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 761 | borderRouterConfig.mStable = stable; |
| 762 | borderRouterConfig.mPreference = ((flags & SPINEL_NET_FLAG_PREFERENCE_MASK) >> SPINEL_NET_FLAG_PREFERENCE_OFFSET); |
| 763 | borderRouterConfig.mPreferred = ((flags & SPINEL_NET_FLAG_PREFERRED) != 0); |
| 764 | borderRouterConfig.mSlaac = ((flags & SPINEL_NET_FLAG_SLAAC) != 0); |
| 765 | borderRouterConfig.mDhcp = ((flags & SPINEL_NET_FLAG_DHCP) != 0); |
| 766 | borderRouterConfig.mConfigure = ((flags & SPINEL_NET_FLAG_CONFIGURE) != 0); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 767 | borderRouterConfig.mDefaultRoute = ((flags & SPINEL_NET_FLAG_DEFAULT_ROUTE) != 0); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 768 | borderRouterConfig.mOnMesh = ((flags & SPINEL_NET_FLAG_ON_MESH) != 0); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 769 | |
| 770 | error = otBorderRouterAddOnMeshPrefix(mInstance, &borderRouterConfig); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 771 | |
| 772 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 773 | return error; |
| 774 | } |
| 775 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 776 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_THREAD_ON_MESH_NETS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 777 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 778 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 779 | otIp6Prefix ip6Prefix; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 780 | uint8_t prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 781 | |
| 782 | memset(&ip6Prefix, 0, sizeof(otIp6Prefix)); |
| 783 | |
Abtin Keshavarzian | 31ce3d4 | 2019-08-01 10:30:08 -0700 | [diff] [blame] | 784 | VerifyOrExit(mAllowLocalNetworkDataChange, error = OT_ERROR_INVALID_STATE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 785 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 786 | SuccessOrExit(error = mDecoder.ReadIp6Address(ip6Prefix.mPrefix)); |
Łukasz Duda | 7b084ec | 2017-10-25 17:54:17 +0200 | [diff] [blame] | 787 | SuccessOrExit(error = mDecoder.ReadUint8(prefixLength)); |
| 788 | |
| 789 | ip6Prefix.mLength = prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 790 | |
| 791 | error = otBorderRouterRemoveOnMeshPrefix(mInstance, &ip6Prefix); |
| 792 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 793 | // If prefix was not on the list, "remove" command can be considred |
| 794 | // successful. |
Abtin Keshavarzian | 02bdc69 | 2017-08-15 09:11:19 -0700 | [diff] [blame] | 795 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 796 | if (error == OT_ERROR_NOT_FOUND) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 797 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 798 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 799 | } |
| 800 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 801 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 802 | return error; |
| 803 | } |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 804 | #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 805 | |
Jonathan Hui | 149640d | 2019-07-16 11:06:03 -0700 | [diff] [blame] | 806 | #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 807 | |
| 808 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_SERVER_ALLOW_LOCAL_DATA_CHANGE>(void) |
| 809 | { |
| 810 | return mEncoder.WriteBool(mAllowLocalServerDataChange); |
| 811 | } |
| 812 | |
| 813 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_SERVER_ALLOW_LOCAL_DATA_CHANGE>(void) |
| 814 | { |
| 815 | bool value = false; |
| 816 | otError error = OT_ERROR_NONE; |
| 817 | bool shouldRegisterWithLeader = false; |
| 818 | |
| 819 | SuccessOrExit(error = mDecoder.ReadBool(value)); |
| 820 | |
| 821 | // Register any server data changes on transition from `true` to `false`. |
| 822 | shouldRegisterWithLeader = (mAllowLocalServerDataChange == true) && (value == false); |
| 823 | |
| 824 | mAllowLocalServerDataChange = value; |
| 825 | |
| 826 | exit: |
| 827 | |
| 828 | if (shouldRegisterWithLeader) |
| 829 | { |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 830 | IgnoreError(otServerRegister(mInstance)); |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | return error; |
| 834 | } |
| 835 | |
| 836 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_SERVER_SERVICES>(void) |
| 837 | { |
| 838 | otError error = OT_ERROR_NONE; |
| 839 | otServiceConfig cfg; |
| 840 | bool stable; |
| 841 | const uint8_t * data; |
| 842 | uint16_t dataLen; |
| 843 | |
Abtin Keshavarzian | 31ce3d4 | 2019-08-01 10:30:08 -0700 | [diff] [blame] | 844 | VerifyOrExit(mAllowLocalServerDataChange, error = OT_ERROR_INVALID_STATE); |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 845 | |
| 846 | SuccessOrExit(error = mDecoder.ReadUint32(cfg.mEnterpriseNumber)); |
| 847 | SuccessOrExit(error = mDecoder.ReadDataWithLen(data, dataLen)); |
| 848 | |
| 849 | VerifyOrExit((dataLen <= sizeof(cfg.mServiceData)), error = OT_ERROR_INVALID_ARGS); |
| 850 | memcpy(cfg.mServiceData, data, dataLen); |
| 851 | |
Abtin Keshavarzian | 53d23b5 | 2020-06-20 18:09:14 -0700 | [diff] [blame] | 852 | static_assert((sizeof(cfg.mServiceData) <= UINT8_MAX), "Cannot handle full range of buffer length"); |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 853 | cfg.mServiceDataLength = static_cast<uint8_t>(dataLen); |
| 854 | |
| 855 | SuccessOrExit(error = mDecoder.ReadBool(stable)); |
| 856 | cfg.mServerConfig.mStable = stable; |
| 857 | SuccessOrExit(error = mDecoder.ReadDataWithLen(data, dataLen)); |
| 858 | |
| 859 | VerifyOrExit((dataLen <= sizeof(cfg.mServerConfig.mServerData)), error = OT_ERROR_INVALID_ARGS); |
| 860 | memcpy(cfg.mServerConfig.mServerData, data, dataLen); |
| 861 | |
Abtin Keshavarzian | 53d23b5 | 2020-06-20 18:09:14 -0700 | [diff] [blame] | 862 | static_assert((sizeof(cfg.mServerConfig.mServerData) <= UINT8_MAX), "Cannot handle full range of buffer length"); |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 863 | cfg.mServerConfig.mServerDataLength = static_cast<uint8_t>(dataLen); |
| 864 | |
| 865 | SuccessOrExit(error = otServerAddService(mInstance, &cfg)); |
| 866 | exit: |
| 867 | return error; |
| 868 | } |
| 869 | |
| 870 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_SERVER_SERVICES>(void) |
| 871 | { |
| 872 | otError error = OT_ERROR_NONE; |
| 873 | |
| 874 | uint32_t enterpriseNumber; |
| 875 | const uint8_t *serviceData; |
| 876 | uint16_t serviceDataLength; |
| 877 | |
Abtin Keshavarzian | 31ce3d4 | 2019-08-01 10:30:08 -0700 | [diff] [blame] | 878 | VerifyOrExit(mAllowLocalServerDataChange, error = OT_ERROR_INVALID_STATE); |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 879 | |
| 880 | SuccessOrExit(error = mDecoder.ReadUint32(enterpriseNumber)); |
| 881 | SuccessOrExit(error = mDecoder.ReadDataWithLen(serviceData, serviceDataLength)); |
| 882 | |
| 883 | VerifyOrExit(serviceDataLength <= UINT8_MAX, error = OT_ERROR_INVALID_ARGS); |
| 884 | |
| 885 | SuccessOrExit(error = otServerRemoveService(mInstance, enterpriseNumber, serviceData, |
| 886 | static_cast<uint8_t>(serviceDataLength))); |
| 887 | exit: |
| 888 | return error; |
| 889 | } |
| 890 | |
| 891 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_SERVER_SERVICES>(void) |
| 892 | { |
| 893 | otError error = OT_ERROR_NONE; |
| 894 | otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT; |
| 895 | otServiceConfig cfg; |
| 896 | |
| 897 | while (otServerGetNextService(mInstance, &iterator, &cfg) == OT_ERROR_NONE) |
| 898 | { |
| 899 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 900 | |
| 901 | SuccessOrExit(error = mEncoder.WriteUint32(cfg.mEnterpriseNumber)); |
| 902 | SuccessOrExit(error = mEncoder.WriteDataWithLen(cfg.mServiceData, cfg.mServiceDataLength)); |
| 903 | SuccessOrExit(error = mEncoder.WriteBool(cfg.mServerConfig.mStable)); |
| 904 | SuccessOrExit( |
| 905 | error = mEncoder.WriteDataWithLen(cfg.mServerConfig.mServerData, cfg.mServerConfig.mServerDataLength)); |
| 906 | SuccessOrExit(error = mEncoder.WriteUint16(cfg.mServerConfig.mRloc16)); |
| 907 | |
| 908 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 909 | } |
| 910 | exit: |
| 911 | return error; |
| 912 | } |
Rongli Sun | e860009 | 2019-12-03 00:42:46 +0800 | [diff] [blame] | 913 | #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 914 | |
| 915 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_SERVER_LEADER_SERVICES>(void) |
| 916 | { |
| 917 | otError error = OT_ERROR_NONE; |
| 918 | otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT; |
| 919 | otServiceConfig cfg; |
| 920 | |
Rongli Sun | e860009 | 2019-12-03 00:42:46 +0800 | [diff] [blame] | 921 | while (otNetDataGetNextService(mInstance, &iterator, &cfg) == OT_ERROR_NONE) |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 922 | { |
| 923 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 924 | |
Rongli Sun | 315a27b | 2020-03-26 23:54:23 +0800 | [diff] [blame] | 925 | SuccessOrExit(error = mEncoder.WriteUint8(cfg.mServiceId)); |
Joseph Newman | ecad717 | 2019-03-29 05:29:25 +0000 | [diff] [blame] | 926 | SuccessOrExit(error = mEncoder.WriteUint32(cfg.mEnterpriseNumber)); |
| 927 | SuccessOrExit(error = mEncoder.WriteDataWithLen(cfg.mServiceData, cfg.mServiceDataLength)); |
| 928 | SuccessOrExit(error = mEncoder.WriteBool(cfg.mServerConfig.mStable)); |
| 929 | SuccessOrExit( |
| 930 | error = mEncoder.WriteDataWithLen(cfg.mServerConfig.mServerData, cfg.mServerConfig.mServerDataLength)); |
| 931 | SuccessOrExit(error = mEncoder.WriteUint16(cfg.mServerConfig.mRloc16)); |
| 932 | |
| 933 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 934 | } |
| 935 | exit: |
| 936 | return error; |
| 937 | } |
| 938 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 939 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_DISCOVERY_SCAN_JOINER_FLAG>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 940 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 941 | return mEncoder.WriteBool(mDiscoveryScanJoinerFlag); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 942 | } |
| 943 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 944 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_DISCOVERY_SCAN_JOINER_FLAG>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 945 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 946 | return mDecoder.ReadBool(mDiscoveryScanJoinerFlag); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 947 | } |
| 948 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 949 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_DISCOVERY_SCAN_ENABLE_FILTERING>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 950 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 951 | return mEncoder.WriteBool(mDiscoveryScanEnableFiltering); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 952 | } |
| 953 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 954 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_DISCOVERY_SCAN_ENABLE_FILTERING>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 955 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 956 | return mDecoder.ReadBool(mDiscoveryScanEnableFiltering); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 957 | } |
| 958 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 959 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_DISCOVERY_SCAN_PANID>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 960 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 961 | return mEncoder.WriteUint16(mDiscoveryScanPanId); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 962 | } |
| 963 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 964 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_DISCOVERY_SCAN_PANID>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 965 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 966 | return mDecoder.ReadUint16(mDiscoveryScanPanId); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 969 | otError NcpBase::EncodeOperationalDataset(const otOperationalDataset &aDataset) |
| 970 | { |
| 971 | otError error = OT_ERROR_NONE; |
| 972 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 973 | if (aDataset.mComponents.mIsActiveTimestampPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 974 | { |
| 975 | SuccessOrExit(mEncoder.OpenStruct()); |
| 976 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_DATASET_ACTIVE_TIMESTAMP)); |
| 977 | SuccessOrExit(mEncoder.WriteUint64(aDataset.mActiveTimestamp)); |
| 978 | SuccessOrExit(mEncoder.CloseStruct()); |
| 979 | } |
| 980 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 981 | if (aDataset.mComponents.mIsPendingTimestampPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 982 | { |
| 983 | SuccessOrExit(mEncoder.OpenStruct()); |
| 984 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_DATASET_PENDING_TIMESTAMP)); |
| 985 | SuccessOrExit(mEncoder.WriteUint64(aDataset.mPendingTimestamp)); |
| 986 | SuccessOrExit(mEncoder.CloseStruct()); |
| 987 | } |
| 988 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 989 | if (aDataset.mComponents.mIsMasterKeyPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 990 | { |
| 991 | SuccessOrExit(mEncoder.OpenStruct()); |
| 992 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_NET_MASTER_KEY)); |
| 993 | SuccessOrExit(mEncoder.WriteData(aDataset.mMasterKey.m8, OT_MASTER_KEY_SIZE)); |
| 994 | SuccessOrExit(mEncoder.CloseStruct()); |
| 995 | } |
| 996 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 997 | if (aDataset.mComponents.mIsNetworkNamePresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 998 | { |
| 999 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1000 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_NET_NETWORK_NAME)); |
| 1001 | SuccessOrExit(mEncoder.WriteUtf8(aDataset.mNetworkName.m8)); |
| 1002 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1003 | } |
| 1004 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 1005 | if (aDataset.mComponents.mIsExtendedPanIdPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1006 | { |
| 1007 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1008 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_NET_XPANID)); |
| 1009 | SuccessOrExit(mEncoder.WriteData(aDataset.mExtendedPanId.m8, OT_EXT_PAN_ID_SIZE)); |
| 1010 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1011 | } |
| 1012 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 1013 | if (aDataset.mComponents.mIsMeshLocalPrefixPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1014 | { |
| 1015 | otIp6Address addr; |
| 1016 | |
| 1017 | memcpy(addr.mFields.m8, aDataset.mMeshLocalPrefix.m8, 8); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1018 | memset(addr.mFields.m8 + 8, 0, 8); // Zero out the last 8 bytes. |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1019 | |
| 1020 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1021 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_IPV6_ML_PREFIX)); |
Jintao Lin | 8788cf5 | 2020-01-10 10:15:23 +0800 | [diff] [blame] | 1022 | SuccessOrExit(error = mEncoder.WriteIp6Address(addr)); // Mesh local prefix |
| 1023 | SuccessOrExit(error = mEncoder.WriteUint8(OT_IP6_PREFIX_BITSIZE)); // Prefix length (in bits) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1024 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1025 | } |
| 1026 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 1027 | if (aDataset.mComponents.mIsDelayPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1028 | { |
| 1029 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1030 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_DATASET_DELAY_TIMER)); |
| 1031 | SuccessOrExit(mEncoder.WriteUint32(aDataset.mDelay)); |
| 1032 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1033 | } |
| 1034 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 1035 | if (aDataset.mComponents.mIsPanIdPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1036 | { |
| 1037 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1038 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_MAC_15_4_PANID)); |
| 1039 | SuccessOrExit(mEncoder.WriteUint16(aDataset.mPanId)); |
| 1040 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1041 | } |
| 1042 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 1043 | if (aDataset.mComponents.mIsChannelPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1044 | { |
| 1045 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1046 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_PHY_CHAN)); |
| 1047 | |
| 1048 | // The channel is stored in Dataset as `uint16_t` (to accommodate |
| 1049 | // larger number of channels in sub-GHz band), however the current |
| 1050 | // definition of `SPINEL_PROP_PHY_CHAN` property limits the channel |
| 1051 | // to a `uint8_t`. |
| 1052 | |
| 1053 | SuccessOrExit(mEncoder.WriteUint8(static_cast<uint8_t>(aDataset.mChannel))); |
| 1054 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1055 | } |
| 1056 | |
Abtin Keshavarzian | 1555b21 | 2019-09-18 19:11:07 -0700 | [diff] [blame] | 1057 | if (aDataset.mComponents.mIsPskcPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1058 | { |
| 1059 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1060 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_NET_PSKC)); |
Abtin Keshavarzian | 1555b21 | 2019-09-18 19:11:07 -0700 | [diff] [blame] | 1061 | SuccessOrExit(mEncoder.WriteData(aDataset.mPskc.m8, sizeof(spinel_net_pskc_t))); |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1062 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1063 | } |
| 1064 | |
Abtin Keshavarzian | 758e647 | 2018-07-19 14:33:49 -0700 | [diff] [blame] | 1065 | if (aDataset.mComponents.mIsSecurityPolicyPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1066 | { |
| 1067 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1068 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_DATASET_SECURITY_POLICY)); |
| 1069 | SuccessOrExit(mEncoder.WriteUint16(aDataset.mSecurityPolicy.mRotationTime)); |
| 1070 | SuccessOrExit(mEncoder.WriteUint8(aDataset.mSecurityPolicy.mFlags)); |
| 1071 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1072 | } |
| 1073 | |
Zhanglong Xia | ce24abb | 2019-03-13 22:49:25 +0800 | [diff] [blame] | 1074 | if (aDataset.mComponents.mIsChannelMaskPresent) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1075 | { |
| 1076 | SuccessOrExit(mEncoder.OpenStruct()); |
| 1077 | SuccessOrExit(mEncoder.WriteUintPacked(SPINEL_PROP_PHY_CHAN_SUPPORTED)); |
Zhanglong Xia | ce24abb | 2019-03-13 22:49:25 +0800 | [diff] [blame] | 1078 | SuccessOrExit(EncodeChannelMask(aDataset.mChannelMask)); |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1079 | SuccessOrExit(mEncoder.CloseStruct()); |
| 1080 | } |
| 1081 | |
| 1082 | exit: |
| 1083 | return error; |
| 1084 | } |
| 1085 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1086 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_ACTIVE_DATASET>(void) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1087 | { |
| 1088 | otOperationalDataset dataset; |
| 1089 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 1090 | IgnoreError(otDatasetGetActive(mInstance, &dataset)); |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1091 | return EncodeOperationalDataset(dataset); |
| 1092 | } |
| 1093 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1094 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_PENDING_DATASET>(void) |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1095 | { |
| 1096 | otOperationalDataset dataset; |
| 1097 | |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 1098 | IgnoreError(otDatasetGetPending(mInstance, &dataset)); |
Abtin Keshavarzian | 4e060e4 | 2017-11-16 05:43:13 -0800 | [diff] [blame] | 1099 | return EncodeOperationalDataset(dataset); |
| 1100 | } |
| 1101 | |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1102 | otError NcpBase::DecodeOperationalDataset(otOperationalDataset &aDataset, |
| 1103 | const uint8_t ** aTlvs, |
| 1104 | uint8_t * aTlvsLength, |
| 1105 | const otIp6Address ** aDestIpAddress, |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1106 | bool aAllowEmptyValues) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1107 | { |
| 1108 | otError error = OT_ERROR_NONE; |
| 1109 | |
| 1110 | memset(&aDataset, 0, sizeof(otOperationalDataset)); |
| 1111 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1112 | if (aTlvs != nullptr) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1113 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1114 | *aTlvs = nullptr; |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1117 | if (aTlvsLength != nullptr) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1118 | { |
| 1119 | *aTlvsLength = 0; |
| 1120 | } |
| 1121 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1122 | if (aDestIpAddress != nullptr) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1123 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1124 | *aDestIpAddress = nullptr; |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | while (!mDecoder.IsAllReadInStruct()) |
| 1128 | { |
| 1129 | unsigned int propKey; |
| 1130 | |
| 1131 | SuccessOrExit(error = mDecoder.OpenStruct()); |
| 1132 | SuccessOrExit(error = mDecoder.ReadUintPacked(propKey)); |
| 1133 | |
| 1134 | switch (static_cast<spinel_prop_key_t>(propKey)) |
| 1135 | { |
| 1136 | case SPINEL_PROP_DATASET_ACTIVE_TIMESTAMP: |
| 1137 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1138 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1139 | { |
| 1140 | SuccessOrExit(error = mDecoder.ReadUint64(aDataset.mActiveTimestamp)); |
| 1141 | } |
| 1142 | |
| 1143 | aDataset.mComponents.mIsActiveTimestampPresent = true; |
| 1144 | break; |
| 1145 | |
| 1146 | case SPINEL_PROP_DATASET_PENDING_TIMESTAMP: |
| 1147 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1148 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1149 | { |
| 1150 | SuccessOrExit(error = mDecoder.ReadUint64(aDataset.mPendingTimestamp)); |
| 1151 | } |
| 1152 | |
| 1153 | aDataset.mComponents.mIsPendingTimestampPresent = true; |
| 1154 | break; |
| 1155 | |
| 1156 | case SPINEL_PROP_NET_MASTER_KEY: |
| 1157 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1158 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1159 | { |
| 1160 | const uint8_t *key; |
| 1161 | uint16_t len; |
| 1162 | |
| 1163 | SuccessOrExit(error = mDecoder.ReadData(key, len)); |
| 1164 | VerifyOrExit(len == OT_MASTER_KEY_SIZE, error = OT_ERROR_INVALID_ARGS); |
| 1165 | memcpy(aDataset.mMasterKey.m8, key, len); |
| 1166 | } |
| 1167 | |
| 1168 | aDataset.mComponents.mIsMasterKeyPresent = true; |
| 1169 | break; |
| 1170 | |
| 1171 | case SPINEL_PROP_NET_NETWORK_NAME: |
| 1172 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1173 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1174 | { |
| 1175 | const char *name; |
| 1176 | size_t len; |
| 1177 | |
| 1178 | SuccessOrExit(error = mDecoder.ReadUtf8(name)); |
| 1179 | len = strlen(name); |
| 1180 | VerifyOrExit(len <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS); |
| 1181 | memcpy(aDataset.mNetworkName.m8, name, len + 1); |
| 1182 | } |
| 1183 | |
| 1184 | aDataset.mComponents.mIsNetworkNamePresent = true; |
| 1185 | break; |
| 1186 | |
| 1187 | case SPINEL_PROP_NET_XPANID: |
| 1188 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1189 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1190 | { |
| 1191 | const uint8_t *xpanid; |
| 1192 | uint16_t len; |
| 1193 | |
| 1194 | SuccessOrExit(error = mDecoder.ReadData(xpanid, len)); |
| 1195 | VerifyOrExit(len == OT_EXT_PAN_ID_SIZE, error = OT_ERROR_INVALID_ARGS); |
| 1196 | memcpy(aDataset.mExtendedPanId.m8, xpanid, len); |
| 1197 | } |
| 1198 | |
| 1199 | aDataset.mComponents.mIsExtendedPanIdPresent = true; |
| 1200 | break; |
| 1201 | |
| 1202 | case SPINEL_PROP_IPV6_ML_PREFIX: |
| 1203 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1204 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1205 | { |
| 1206 | const otIp6Address *addr; |
| 1207 | uint8_t prefixLen; |
| 1208 | |
| 1209 | SuccessOrExit(error = mDecoder.ReadIp6Address(addr)); |
| 1210 | SuccessOrExit(error = mDecoder.ReadUint8(prefixLen)); |
Jintao Lin | 8788cf5 | 2020-01-10 10:15:23 +0800 | [diff] [blame] | 1211 | VerifyOrExit(prefixLen == OT_IP6_PREFIX_BITSIZE, error = OT_ERROR_INVALID_ARGS); |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1212 | memcpy(aDataset.mMeshLocalPrefix.m8, addr, OT_MESH_LOCAL_PREFIX_SIZE); |
| 1213 | } |
| 1214 | |
| 1215 | aDataset.mComponents.mIsMeshLocalPrefixPresent = true; |
| 1216 | break; |
| 1217 | |
| 1218 | case SPINEL_PROP_DATASET_DELAY_TIMER: |
| 1219 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1220 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1221 | { |
| 1222 | SuccessOrExit(error = mDecoder.ReadUint32(aDataset.mDelay)); |
| 1223 | } |
| 1224 | |
| 1225 | aDataset.mComponents.mIsDelayPresent = true; |
| 1226 | break; |
| 1227 | |
| 1228 | case SPINEL_PROP_MAC_15_4_PANID: |
| 1229 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1230 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1231 | { |
| 1232 | SuccessOrExit(error = mDecoder.ReadUint16(aDataset.mPanId)); |
| 1233 | } |
| 1234 | |
| 1235 | aDataset.mComponents.mIsPanIdPresent = true; |
| 1236 | break; |
| 1237 | |
| 1238 | case SPINEL_PROP_PHY_CHAN: |
| 1239 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1240 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1241 | { |
| 1242 | uint8_t channel; |
| 1243 | |
| 1244 | SuccessOrExit(error = mDecoder.ReadUint8(channel)); |
| 1245 | aDataset.mChannel = channel; |
| 1246 | } |
| 1247 | |
| 1248 | aDataset.mComponents.mIsChannelPresent = true; |
| 1249 | break; |
| 1250 | |
| 1251 | case SPINEL_PROP_NET_PSKC: |
| 1252 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1253 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1254 | { |
| 1255 | const uint8_t *psk; |
| 1256 | uint16_t len; |
| 1257 | |
| 1258 | SuccessOrExit(error = mDecoder.ReadData(psk, len)); |
| 1259 | VerifyOrExit(len == OT_PSKC_MAX_SIZE, error = OT_ERROR_INVALID_ARGS); |
Abtin Keshavarzian | 1555b21 | 2019-09-18 19:11:07 -0700 | [diff] [blame] | 1260 | memcpy(aDataset.mPskc.m8, psk, OT_PSKC_MAX_SIZE); |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Abtin Keshavarzian | 1555b21 | 2019-09-18 19:11:07 -0700 | [diff] [blame] | 1263 | aDataset.mComponents.mIsPskcPresent = true; |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1264 | break; |
| 1265 | |
| 1266 | case SPINEL_PROP_DATASET_SECURITY_POLICY: |
| 1267 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1268 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1269 | { |
| 1270 | SuccessOrExit(error = mDecoder.ReadUint16(aDataset.mSecurityPolicy.mRotationTime)); |
| 1271 | SuccessOrExit(error = mDecoder.ReadUint8(aDataset.mSecurityPolicy.mFlags)); |
| 1272 | } |
| 1273 | |
| 1274 | aDataset.mComponents.mIsSecurityPolicyPresent = true; |
| 1275 | break; |
| 1276 | |
| 1277 | case SPINEL_PROP_PHY_CHAN_SUPPORTED: |
| 1278 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1279 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1280 | { |
| 1281 | uint8_t channel; |
| 1282 | |
Zhanglong Xia | ce24abb | 2019-03-13 22:49:25 +0800 | [diff] [blame] | 1283 | aDataset.mChannelMask = 0; |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1284 | |
| 1285 | while (!mDecoder.IsAllReadInStruct()) |
| 1286 | { |
| 1287 | SuccessOrExit(error = mDecoder.ReadUint8(channel)); |
| 1288 | VerifyOrExit(channel <= 31, error = OT_ERROR_INVALID_ARGS); |
Zhanglong Xia | ce24abb | 2019-03-13 22:49:25 +0800 | [diff] [blame] | 1289 | aDataset.mChannelMask |= (1UL << channel); |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Zhanglong Xia | ce24abb | 2019-03-13 22:49:25 +0800 | [diff] [blame] | 1293 | aDataset.mComponents.mIsChannelMaskPresent = true; |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1294 | break; |
| 1295 | |
| 1296 | case SPINEL_PROP_DATASET_RAW_TLVS: |
| 1297 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1298 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1299 | { |
| 1300 | const uint8_t *tlvs; |
| 1301 | uint16_t len; |
| 1302 | |
| 1303 | SuccessOrExit(error = mDecoder.ReadData(tlvs, len)); |
| 1304 | VerifyOrExit(len <= 255, error = OT_ERROR_INVALID_ARGS); |
| 1305 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1306 | if (aTlvs != nullptr) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1307 | { |
| 1308 | *aTlvs = tlvs; |
| 1309 | } |
| 1310 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1311 | if (aTlvsLength != nullptr) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1312 | { |
| 1313 | *aTlvsLength = static_cast<uint8_t>(len); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | break; |
| 1318 | |
| 1319 | case SPINEL_PROP_DATASET_DEST_ADDRESS: |
| 1320 | |
Jonathan Hui | 49c4b63 | 2019-03-21 09:52:32 -0700 | [diff] [blame] | 1321 | if (!aAllowEmptyValues || !mDecoder.IsAllReadInStruct()) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1322 | { |
| 1323 | const otIp6Address *addr; |
| 1324 | |
| 1325 | SuccessOrExit(error = mDecoder.ReadIp6Address(addr)); |
| 1326 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1327 | if (aDestIpAddress != nullptr) |
rongli | 1253bec | 2018-12-04 13:28:09 +0800 | [diff] [blame] | 1328 | { |
| 1329 | *aDestIpAddress = addr; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | break; |
| 1334 | |
| 1335 | default: |
| 1336 | break; |
| 1337 | } |
| 1338 | |
| 1339 | SuccessOrExit(error = mDecoder.CloseStruct()); |
| 1340 | } |
| 1341 | |
| 1342 | exit: |
| 1343 | return error; |
| 1344 | } |
| 1345 | |
| 1346 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_ACTIVE_DATASET>(void) |
| 1347 | { |
| 1348 | otError error = OT_ERROR_NONE; |
| 1349 | otOperationalDataset dataset; |
| 1350 | |
| 1351 | SuccessOrExit(error = DecodeOperationalDataset(dataset)); |
| 1352 | error = otDatasetSetActive(mInstance, &dataset); |
| 1353 | |
| 1354 | exit: |
| 1355 | return error; |
| 1356 | } |
| 1357 | |
| 1358 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_PENDING_DATASET>(void) |
| 1359 | { |
| 1360 | otError error = OT_ERROR_NONE; |
| 1361 | otOperationalDataset dataset; |
| 1362 | |
| 1363 | SuccessOrExit(error = DecodeOperationalDataset(dataset)); |
| 1364 | error = otDatasetSetPending(mInstance, &dataset); |
| 1365 | |
| 1366 | exit: |
| 1367 | return error; |
| 1368 | } |
| 1369 | |
| 1370 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_MGMT_SET_ACTIVE_DATASET>(void) |
| 1371 | { |
| 1372 | otError error = OT_ERROR_NONE; |
| 1373 | otOperationalDataset dataset; |
| 1374 | const uint8_t * extraTlvs; |
| 1375 | uint8_t extraTlvsLength; |
| 1376 | |
| 1377 | SuccessOrExit(error = DecodeOperationalDataset(dataset, &extraTlvs, &extraTlvsLength)); |
| 1378 | error = otDatasetSendMgmtActiveSet(mInstance, &dataset, extraTlvs, extraTlvsLength); |
| 1379 | |
| 1380 | exit: |
| 1381 | return error; |
| 1382 | } |
| 1383 | |
| 1384 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_MGMT_SET_PENDING_DATASET>(void) |
| 1385 | { |
| 1386 | otError error = OT_ERROR_NONE; |
| 1387 | otOperationalDataset dataset; |
| 1388 | const uint8_t * extraTlvs; |
| 1389 | uint8_t extraTlvsLength; |
| 1390 | |
| 1391 | SuccessOrExit(error = DecodeOperationalDataset(dataset, &extraTlvs, &extraTlvsLength)); |
| 1392 | error = otDatasetSendMgmtPendingSet(mInstance, &dataset, extraTlvs, extraTlvsLength); |
| 1393 | |
| 1394 | exit: |
| 1395 | return error; |
| 1396 | } |
| 1397 | |
| 1398 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_MGMT_GET_ACTIVE_DATASET>(void) |
| 1399 | { |
| 1400 | otError error = OT_ERROR_NONE; |
| 1401 | otOperationalDataset dataset; |
| 1402 | const uint8_t * extraTlvs; |
| 1403 | uint8_t extraTlvsLength; |
| 1404 | const otIp6Address * destIpAddress; |
| 1405 | |
| 1406 | SuccessOrExit(error = DecodeOperationalDataset(dataset, &extraTlvs, &extraTlvsLength, &destIpAddress, true)); |
| 1407 | error = otDatasetSendMgmtActiveGet(mInstance, &dataset.mComponents, extraTlvs, extraTlvsLength, destIpAddress); |
| 1408 | |
| 1409 | exit: |
| 1410 | return error; |
| 1411 | } |
| 1412 | |
| 1413 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_MGMT_GET_PENDING_DATASET>(void) |
| 1414 | { |
| 1415 | otError error = OT_ERROR_NONE; |
| 1416 | otOperationalDataset dataset; |
| 1417 | const uint8_t * extraTlvs; |
| 1418 | uint8_t extraTlvsLength; |
| 1419 | const otIp6Address * destIpAddress; |
| 1420 | |
| 1421 | SuccessOrExit(error = DecodeOperationalDataset(dataset, &extraTlvs, &extraTlvsLength, &destIpAddress, true)); |
| 1422 | error = otDatasetSendMgmtPendingGet(mInstance, &dataset.mComponents, extraTlvs, extraTlvsLength, destIpAddress); |
| 1423 | |
| 1424 | exit: |
| 1425 | return error; |
| 1426 | } |
Jonathan Hui | 31ffd16 | 2019-07-16 12:01:36 -0700 | [diff] [blame] | 1427 | #if OPENTHREAD_CONFIG_JOINER_ENABLE |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1428 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MESHCOP_JOINER_STATE>(void) |
| 1429 | { |
Abtin Keshavarzian | 99f5790 | 2019-04-22 10:04:54 -0700 | [diff] [blame] | 1430 | spinel_meshcop_joiner_state_t state = SPINEL_MESHCOP_JOINER_STATE_IDLE; |
| 1431 | |
| 1432 | switch (otJoinerGetState(mInstance)) |
| 1433 | { |
| 1434 | case OT_JOINER_STATE_IDLE: |
| 1435 | state = SPINEL_MESHCOP_JOINER_STATE_IDLE; |
| 1436 | break; |
| 1437 | case OT_JOINER_STATE_DISCOVER: |
| 1438 | state = SPINEL_MESHCOP_JOINER_STATE_DISCOVER; |
| 1439 | break; |
| 1440 | case OT_JOINER_STATE_CONNECT: |
| 1441 | state = SPINEL_MESHCOP_JOINER_STATE_CONNECTING; |
| 1442 | break; |
| 1443 | case OT_JOINER_STATE_CONNECTED: |
| 1444 | state = SPINEL_MESHCOP_JOINER_STATE_CONNECTED; |
| 1445 | break; |
| 1446 | case OT_JOINER_STATE_ENTRUST: |
| 1447 | state = SPINEL_MESHCOP_JOINER_STATE_ENTRUST; |
| 1448 | break; |
| 1449 | case OT_JOINER_STATE_JOINED: |
| 1450 | state = SPINEL_MESHCOP_JOINER_STATE_JOINED; |
| 1451 | break; |
| 1452 | } |
| 1453 | |
| 1454 | return mEncoder.WriteUint8(state); |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MESHCOP_JOINER_COMMISSIONING>(void) |
| 1458 | { |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1459 | otError error = OT_ERROR_NONE; |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1460 | bool action = false; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1461 | const char *psk = nullptr; |
| 1462 | const char *provisioningUrl = nullptr; |
| 1463 | const char *vendorName = nullptr; |
| 1464 | const char *vendorModel = nullptr; |
| 1465 | const char *vendorSwVersion = nullptr; |
| 1466 | const char *vendorData = nullptr; |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1467 | |
| 1468 | SuccessOrExit(error = mDecoder.ReadBool(action)); |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1469 | |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1470 | if (action == false) |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1471 | { |
Yakun Xu | 957d79d | 2019-09-10 23:42:02 +0800 | [diff] [blame] | 1472 | otJoinerStop(mInstance); |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1473 | ExitNow(); |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1474 | } |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1475 | |
| 1476 | SuccessOrExit(error = mDecoder.ReadUtf8(psk)); |
| 1477 | |
| 1478 | // Parse optional fields |
| 1479 | |
| 1480 | if (!mDecoder.IsAllReadInStruct()) |
| 1481 | { |
| 1482 | SuccessOrExit(error = mDecoder.ReadUtf8(provisioningUrl)); |
| 1483 | } |
| 1484 | |
| 1485 | if (!mDecoder.IsAllReadInStruct()) |
| 1486 | { |
| 1487 | SuccessOrExit(error = mDecoder.ReadUtf8(vendorName)); |
| 1488 | } |
| 1489 | |
| 1490 | if (!mDecoder.IsAllReadInStruct()) |
| 1491 | { |
| 1492 | SuccessOrExit(error = mDecoder.ReadUtf8(vendorModel)); |
| 1493 | } |
| 1494 | |
| 1495 | if (!mDecoder.IsAllReadInStruct()) |
| 1496 | { |
| 1497 | SuccessOrExit(error = mDecoder.ReadUtf8(vendorSwVersion)); |
| 1498 | } |
| 1499 | |
| 1500 | if (!mDecoder.IsAllReadInStruct()) |
| 1501 | { |
| 1502 | SuccessOrExit(error = mDecoder.ReadUtf8(vendorData)); |
| 1503 | } |
| 1504 | |
| 1505 | // Use OpenThread default values for vendor name, mode, sw version if |
| 1506 | // not specified or an empty string is given. |
| 1507 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1508 | if ((vendorName == nullptr) || (vendorName[0] == 0)) |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1509 | { |
| 1510 | vendorName = PACKAGE_NAME; |
| 1511 | } |
| 1512 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1513 | if ((vendorModel == nullptr) || (vendorModel[0] == 0)) |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1514 | { |
| 1515 | vendorModel = OPENTHREAD_CONFIG_PLATFORM_INFO; |
| 1516 | } |
| 1517 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1518 | if ((vendorSwVersion == nullptr) || (vendorSwVersion[0] == 0)) |
Abtin Keshavarzian | 987e96b | 2019-04-17 19:08:11 -0700 | [diff] [blame] | 1519 | { |
| 1520 | vendorSwVersion = PACKAGE_VERSION; |
| 1521 | } |
| 1522 | |
| 1523 | error = otJoinerStart(mInstance, psk, provisioningUrl, vendorName, vendorModel, vendorSwVersion, vendorData, |
| 1524 | &NcpBase::HandleJoinerCallback_Jump, this); |
| 1525 | |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1526 | exit: |
| 1527 | return error; |
| 1528 | } |
| 1529 | |
Abtin Keshavarzian | a7415b5 | 2020-06-20 00:35:57 -0700 | [diff] [blame] | 1530 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MESHCOP_JOINER_DISCERNER>(void) |
| 1531 | { |
| 1532 | otError error; |
| 1533 | const otJoinerDiscerner *discerner = otJoinerGetDiscerner(mInstance); |
| 1534 | |
| 1535 | if (discerner == nullptr) |
| 1536 | { |
| 1537 | SuccessOrExit(error = mEncoder.WriteUint8(0)); |
| 1538 | } |
| 1539 | else |
| 1540 | { |
| 1541 | SuccessOrExit(error = mEncoder.WriteUint8(discerner->mLength)); |
| 1542 | SuccessOrExit(error = mEncoder.WriteUint64(discerner->mValue)); |
| 1543 | } |
| 1544 | |
| 1545 | exit: |
| 1546 | return error; |
| 1547 | } |
| 1548 | |
| 1549 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MESHCOP_JOINER_DISCERNER>(void) |
| 1550 | { |
| 1551 | otError error = OT_ERROR_NONE; |
| 1552 | otJoinerDiscerner discerner; |
| 1553 | |
| 1554 | SuccessOrExit(error = mDecoder.ReadUint8(discerner.mLength)); |
| 1555 | |
| 1556 | if (discerner.mLength == 0) |
| 1557 | { |
| 1558 | // Clearing any previously set Joiner Discerner |
| 1559 | error = otJoinerSetDiscerner(mInstance, nullptr); |
| 1560 | ExitNow(); |
| 1561 | } |
| 1562 | |
| 1563 | SuccessOrExit(error = mDecoder.ReadUint64(discerner.mValue)); |
| 1564 | error = otJoinerSetDiscerner(mInstance, &discerner); |
| 1565 | |
| 1566 | exit: |
| 1567 | return error; |
| 1568 | } |
| 1569 | |
| 1570 | #endif // OPENTHREAD_CONFIG_JOINER_ENABLE |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 1571 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1572 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_ML_PREFIX>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1573 | { |
Jonathan Hui | c116441 | 2018-08-17 08:59:37 -0700 | [diff] [blame] | 1574 | otError error = OT_ERROR_NONE; |
| 1575 | const otMeshLocalPrefix *mlPrefix = otThreadGetMeshLocalPrefix(mInstance); |
| 1576 | otIp6Address addr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1577 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1578 | VerifyOrExit(mlPrefix != nullptr, OT_NOOP); // If `mlPrefix` is nullptr send empty response. |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1579 | |
Jonathan Hui | c116441 | 2018-08-17 08:59:37 -0700 | [diff] [blame] | 1580 | memcpy(addr.mFields.m8, mlPrefix->m8, 8); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1581 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1582 | // Zero out the last 8 bytes. |
| 1583 | memset(addr.mFields.m8 + 8, 0, 8); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1584 | |
Jintao Lin | 8788cf5 | 2020-01-10 10:15:23 +0800 | [diff] [blame] | 1585 | SuccessOrExit(error = mEncoder.WriteIp6Address(addr)); // Mesh local prefix |
| 1586 | SuccessOrExit(error = mEncoder.WriteUint8(OT_IP6_PREFIX_BITSIZE)); // Prefix length (in bits) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1587 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1588 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1589 | return error; |
| 1590 | } |
| 1591 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1592 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_IPV6_ML_PREFIX>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1593 | { |
Jonathan Hui | c116441 | 2018-08-17 08:59:37 -0700 | [diff] [blame] | 1594 | otError error = OT_ERROR_NONE; |
| 1595 | const otIp6Address *meshLocalPrefix; |
| 1596 | uint8_t prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1597 | |
rongli | 11f3260 | 2018-03-20 23:55:41 +0800 | [diff] [blame] | 1598 | SuccessOrExit(error = mDecoder.ReadIp6Address(meshLocalPrefix)); |
| 1599 | SuccessOrExit(error = mDecoder.ReadUint8(prefixLength)); |
Jintao Lin | 8788cf5 | 2020-01-10 10:15:23 +0800 | [diff] [blame] | 1600 | VerifyOrExit(prefixLength == OT_IP6_PREFIX_BITSIZE, error = OT_ERROR_INVALID_ARGS); |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1601 | |
Jonathan Hui | c116441 | 2018-08-17 08:59:37 -0700 | [diff] [blame] | 1602 | error = otThreadSetMeshLocalPrefix(mInstance, reinterpret_cast<const otMeshLocalPrefix *>(meshLocalPrefix)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1603 | |
| 1604 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1605 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1606 | } |
| 1607 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1608 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_ML_ADDR>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1609 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1610 | otError error = OT_ERROR_NONE; |
| 1611 | const otIp6Address *ml64 = otThreadGetMeshLocalEid(mInstance); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1612 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1613 | VerifyOrExit(ml64 != nullptr, OT_NOOP); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1614 | SuccessOrExit(error = mEncoder.WriteIp6Address(*ml64)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1615 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1616 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1617 | return error; |
| 1618 | } |
| 1619 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1620 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_LL_ADDR>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1621 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1622 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1623 | const otIp6Address *address = otThreadGetLinkLocalIp6Address(mInstance); |
| 1624 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1625 | VerifyOrExit(address != nullptr, OT_NOOP); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1626 | SuccessOrExit(error = mEncoder.WriteIp6Address(*address)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1627 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1628 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1629 | return error; |
| 1630 | } |
| 1631 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1632 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_ADDRESS_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1633 | { |
| 1634 | otError error = OT_ERROR_NONE; |
| 1635 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1636 | for (const otNetifAddress *address = otIp6GetUnicastAddresses(mInstance); address; address = address->mNext) |
| 1637 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1638 | SuccessOrExit(error = mEncoder.OpenStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1639 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1640 | SuccessOrExit(error = mEncoder.WriteIp6Address(address->mAddress)); |
| 1641 | SuccessOrExit(error = mEncoder.WriteUint8(address->mPrefixLength)); |
| 1642 | SuccessOrExit(error = mEncoder.WriteUint32(address->mPreferred ? 0xffffffff : 0)); |
| 1643 | SuccessOrExit(error = mEncoder.WriteUint32(address->mValid ? 0xffffffff : 0)); |
| 1644 | |
| 1645 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1646 | } |
| 1647 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1648 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1649 | return error; |
| 1650 | } |
| 1651 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1652 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_IPV6_ADDRESS_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1653 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1654 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1655 | otNetifAddress netifAddr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1656 | uint32_t preferredLifetime; |
| 1657 | uint32_t validLifetime; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1658 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1659 | SuccessOrExit(error = mDecoder.ReadIp6Address(netifAddr.mAddress)); |
| 1660 | SuccessOrExit(error = mDecoder.ReadUint8(netifAddr.mPrefixLength)); |
| 1661 | SuccessOrExit(error = mDecoder.ReadUint32(preferredLifetime)); |
| 1662 | SuccessOrExit(error = mDecoder.ReadUint32(validLifetime)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1663 | |
Markus Becker | bad53a2 | 2020-06-16 03:24:14 +0200 | [diff] [blame] | 1664 | netifAddr.mAddressOrigin = OT_ADDRESS_ORIGIN_MANUAL; |
| 1665 | netifAddr.mPreferred = (preferredLifetime != 0); |
| 1666 | netifAddr.mValid = (validLifetime != 0); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1667 | |
| 1668 | error = otIp6AddUnicastAddress(mInstance, &netifAddr); |
| 1669 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1670 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1671 | return error; |
| 1672 | } |
| 1673 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1674 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_IPV6_ADDRESS_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1675 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1676 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1677 | const otIp6Address *addrPtr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1678 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1679 | SuccessOrExit(error = mDecoder.ReadIp6Address(addrPtr)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1680 | |
| 1681 | error = otIp6RemoveUnicastAddress(mInstance, addrPtr); |
Abtin Keshavarzian | 02bdc69 | 2017-08-15 09:11:19 -0700 | [diff] [blame] | 1682 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1683 | // If address was not on the list, "remove" command is successful. |
| 1684 | if (error == OT_ERROR_NOT_FOUND) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1685 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1686 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1687 | } |
| 1688 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1689 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1690 | return error; |
| 1691 | } |
| 1692 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1693 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_ROUTE_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1694 | { |
| 1695 | // TODO: Implement get route table |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1696 | return mEncoder.OverwriteWithLastStatusError(SPINEL_STATUS_UNIMPLEMENTED); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1697 | } |
| 1698 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1699 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_ICMP_PING_OFFLOAD>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1700 | { |
Jonathan Hui | f602dcd | 2018-03-13 18:10:28 +0000 | [diff] [blame] | 1701 | return mEncoder.WriteBool(otIcmp6GetEchoMode(mInstance) != OT_ICMP6_ECHO_HANDLER_DISABLED); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1704 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_IPV6_ICMP_PING_OFFLOAD>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1705 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1706 | bool enabled = false; |
| 1707 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1708 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1709 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1710 | |
Jonathan Hui | f602dcd | 2018-03-13 18:10:28 +0000 | [diff] [blame] | 1711 | otIcmp6SetEchoMode(mInstance, enabled ? OT_ICMP6_ECHO_HANDLER_ALL : OT_ICMP6_ECHO_HANDLER_DISABLED); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1712 | |
| 1713 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1714 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1715 | } |
| 1716 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1717 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1718 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1719 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1720 | const otNetifMulticastAddress *address; |
| 1721 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1722 | for (address = otIp6GetMulticastAddresses(mInstance); address; address = address->mNext) |
| 1723 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1724 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 1725 | SuccessOrExit(error = mEncoder.WriteIp6Address(address->mAddress)); |
| 1726 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1727 | } |
| 1728 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1729 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1730 | return error; |
| 1731 | } |
| 1732 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1733 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1734 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1735 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1736 | const otIp6Address *addrPtr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1737 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1738 | SuccessOrExit(error = mDecoder.ReadIp6Address(addrPtr)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1739 | |
| 1740 | error = otIp6SubscribeMulticastAddress(mInstance, addrPtr); |
Abtin Keshavarzian | 02bdc69 | 2017-08-15 09:11:19 -0700 | [diff] [blame] | 1741 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1742 | if (error == OT_ERROR_ALREADY) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1743 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1744 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1745 | } |
| 1746 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1747 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1748 | return error; |
| 1749 | } |
| 1750 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1751 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1752 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1753 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1754 | const otIp6Address *addrPtr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1755 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1756 | SuccessOrExit(error = mDecoder.ReadIp6Address(addrPtr)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1757 | |
| 1758 | error = otIp6UnsubscribeMulticastAddress(mInstance, addrPtr); |
Abtin Keshavarzian | 02bdc69 | 2017-08-15 09:11:19 -0700 | [diff] [blame] | 1759 | |
| 1760 | // If the address was not on the list, "remove" command is successful, |
| 1761 | // and we respond with a `SPINEL_STATUS_OK` status. |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1762 | if (error == OT_ERROR_NOT_FOUND) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1763 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1764 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1765 | } |
| 1766 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1767 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1768 | return error; |
| 1769 | } |
| 1770 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1771 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_IPV6_ICMP_PING_OFFLOAD_MODE>(void) |
Jonathan Hui | f602dcd | 2018-03-13 18:10:28 +0000 | [diff] [blame] | 1772 | { |
| 1773 | spinel_ipv6_icmp_ping_offload_mode_t mode = SPINEL_IPV6_ICMP_PING_OFFLOAD_DISABLED; |
| 1774 | |
| 1775 | switch (otIcmp6GetEchoMode(mInstance)) |
| 1776 | { |
| 1777 | case OT_ICMP6_ECHO_HANDLER_DISABLED: |
| 1778 | mode = SPINEL_IPV6_ICMP_PING_OFFLOAD_DISABLED; |
| 1779 | break; |
| 1780 | case OT_ICMP6_ECHO_HANDLER_UNICAST_ONLY: |
| 1781 | mode = SPINEL_IPV6_ICMP_PING_OFFLOAD_UNICAST_ONLY; |
| 1782 | break; |
| 1783 | case OT_ICMP6_ECHO_HANDLER_MULTICAST_ONLY: |
| 1784 | mode = SPINEL_IPV6_ICMP_PING_OFFLOAD_MULTICAST_ONLY; |
| 1785 | break; |
| 1786 | case OT_ICMP6_ECHO_HANDLER_ALL: |
| 1787 | mode = SPINEL_IPV6_ICMP_PING_OFFLOAD_ALL; |
| 1788 | break; |
| 1789 | }; |
| 1790 | |
| 1791 | return mEncoder.WriteUint8(mode); |
| 1792 | } |
| 1793 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1794 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_IPV6_ICMP_PING_OFFLOAD_MODE>(void) |
Jonathan Hui | f602dcd | 2018-03-13 18:10:28 +0000 | [diff] [blame] | 1795 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1796 | otError error = OT_ERROR_NONE; |
| 1797 | otIcmp6EchoMode mode = OT_ICMP6_ECHO_HANDLER_DISABLED; |
| 1798 | uint8_t spinelMode; |
Jonathan Hui | f602dcd | 2018-03-13 18:10:28 +0000 | [diff] [blame] | 1799 | |
| 1800 | SuccessOrExit(error = mDecoder.ReadUint8(spinelMode)); |
| 1801 | |
| 1802 | switch (spinelMode) |
| 1803 | { |
| 1804 | case SPINEL_IPV6_ICMP_PING_OFFLOAD_DISABLED: |
| 1805 | mode = OT_ICMP6_ECHO_HANDLER_DISABLED; |
| 1806 | break; |
| 1807 | case SPINEL_IPV6_ICMP_PING_OFFLOAD_UNICAST_ONLY: |
| 1808 | mode = OT_ICMP6_ECHO_HANDLER_UNICAST_ONLY; |
| 1809 | break; |
| 1810 | case SPINEL_IPV6_ICMP_PING_OFFLOAD_MULTICAST_ONLY: |
| 1811 | mode = OT_ICMP6_ECHO_HANDLER_MULTICAST_ONLY; |
| 1812 | break; |
| 1813 | case SPINEL_IPV6_ICMP_PING_OFFLOAD_ALL: |
| 1814 | mode = OT_ICMP6_ECHO_HANDLER_ALL; |
| 1815 | break; |
| 1816 | }; |
| 1817 | |
| 1818 | otIcmp6SetEchoMode(mInstance, mode); |
| 1819 | |
| 1820 | exit: |
| 1821 | return error; |
| 1822 | } |
| 1823 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1824 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_RLOC16_DEBUG_PASSTHRU>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1825 | { |
| 1826 | // Note reverse logic: passthru enabled = filter disabled |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1827 | return mEncoder.WriteBool(!otIp6IsReceiveFilterEnabled(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1828 | } |
| 1829 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1830 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_RLOC16_DEBUG_PASSTHRU>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1831 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1832 | bool enabled = false; |
| 1833 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1834 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1835 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1836 | |
| 1837 | // Note reverse logic: passthru enabled = filter disabled |
| 1838 | otIp6SetReceiveFilterEnabled(mInstance, !enabled); |
| 1839 | |
| 1840 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1841 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1842 | } |
| 1843 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1844 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_OFF_MESH_ROUTES>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1845 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1846 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1847 | otExternalRouteConfig routeConfig; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1848 | otNetworkDataIterator iter = OT_NETWORK_DATA_ITERATOR_INIT; |
| 1849 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1850 | while (otNetDataGetNextRoute(mInstance, &iter, &routeConfig) == OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1851 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1852 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 1853 | |
| 1854 | SuccessOrExit(error = mEncoder.WriteIp6Address(routeConfig.mPrefix.mPrefix)); |
| 1855 | SuccessOrExit(error = mEncoder.WriteUint8(routeConfig.mPrefix.mLength)); |
| 1856 | SuccessOrExit(error = mEncoder.WriteBool(routeConfig.mStable)); |
| 1857 | SuccessOrExit(error = mEncoder.WriteUint8(ExternalRoutePreferenceToFlagByte(routeConfig.mPreference))); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1858 | SuccessOrExit(error = mEncoder.WriteBool(false)); // IsLocal |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1859 | SuccessOrExit(error = mEncoder.WriteBool(routeConfig.mNextHopIsThisDevice)); |
| 1860 | SuccessOrExit(error = mEncoder.WriteUint16(routeConfig.mRloc16)); |
| 1861 | |
| 1862 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1863 | } |
| 1864 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 1865 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Abtin Keshavarzian | 2c8f50b | 2017-09-01 21:27:58 -0700 | [diff] [blame] | 1866 | |
| 1867 | iter = OT_NETWORK_DATA_ITERATOR_INIT; |
| 1868 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1869 | while (otBorderRouterGetNextRoute(mInstance, &iter, &routeConfig) == OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1870 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1871 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 1872 | |
| 1873 | SuccessOrExit(error = mEncoder.WriteIp6Address(routeConfig.mPrefix.mPrefix)); |
| 1874 | SuccessOrExit(error = mEncoder.WriteUint8(routeConfig.mPrefix.mLength)); |
| 1875 | SuccessOrExit(error = mEncoder.WriteBool(routeConfig.mStable)); |
| 1876 | SuccessOrExit(error = mEncoder.WriteUint8(ExternalRoutePreferenceToFlagByte(routeConfig.mPreference))); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1877 | SuccessOrExit(error = mEncoder.WriteBool(true)); // IsLocal |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1878 | SuccessOrExit(error = mEncoder.WriteBool(routeConfig.mNextHopIsThisDevice)); |
| 1879 | SuccessOrExit(error = mEncoder.WriteUint16(routeConfig.mRloc16)); |
| 1880 | |
| 1881 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1882 | } |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 1883 | #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1884 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1885 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1886 | return error; |
| 1887 | } |
| 1888 | |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 1889 | #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1890 | static int FlagByteToExternalRoutePreference(uint8_t aFlags) |
| 1891 | { |
| 1892 | int route_preference = 0; |
| 1893 | |
| 1894 | switch (aFlags & SPINEL_NET_FLAG_PREFERENCE_MASK) |
| 1895 | { |
| 1896 | case SPINEL_ROUTE_PREFERENCE_HIGH: |
| 1897 | route_preference = OT_ROUTE_PREFERENCE_HIGH; |
| 1898 | break; |
| 1899 | |
| 1900 | case SPINEL_ROUTE_PREFERENCE_MEDIUM: |
| 1901 | route_preference = OT_ROUTE_PREFERENCE_MED; |
| 1902 | break; |
| 1903 | |
| 1904 | case SPINEL_ROUTE_PREFERENCE_LOW: |
| 1905 | route_preference = OT_ROUTE_PREFERENCE_LOW; |
| 1906 | break; |
| 1907 | } |
| 1908 | |
| 1909 | return route_preference; |
| 1910 | } |
| 1911 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1912 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_THREAD_OFF_MESH_ROUTES>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1913 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1914 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1915 | otExternalRouteConfig routeConfig; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1916 | bool stable = false; |
| 1917 | uint8_t flags = 0; |
| 1918 | uint8_t prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1919 | |
| 1920 | memset(&routeConfig, 0, sizeof(otExternalRouteConfig)); |
| 1921 | |
Abtin Keshavarzian | 31ce3d4 | 2019-08-01 10:30:08 -0700 | [diff] [blame] | 1922 | VerifyOrExit(mAllowLocalNetworkDataChange, error = OT_ERROR_INVALID_STATE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1923 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1924 | SuccessOrExit(error = mDecoder.ReadIp6Address(routeConfig.mPrefix.mPrefix)); |
Łukasz Duda | 7b084ec | 2017-10-25 17:54:17 +0200 | [diff] [blame] | 1925 | SuccessOrExit(error = mDecoder.ReadUint8(prefixLength)); |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1926 | SuccessOrExit(error = mDecoder.ReadBool(stable)); |
| 1927 | SuccessOrExit(error = mDecoder.ReadUint8(flags)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1928 | |
Łukasz Duda | 7b084ec | 2017-10-25 17:54:17 +0200 | [diff] [blame] | 1929 | routeConfig.mPrefix.mLength = prefixLength; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1930 | routeConfig.mStable = stable; |
| 1931 | routeConfig.mPreference = FlagByteToExternalRoutePreference(flags); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1932 | |
| 1933 | error = otBorderRouterAddRoute(mInstance, &routeConfig); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1934 | |
| 1935 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1936 | return error; |
| 1937 | } |
| 1938 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1939 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_THREAD_OFF_MESH_ROUTES>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1940 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1941 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1942 | otIp6Prefix ip6Prefix; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1943 | uint8_t prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1944 | |
| 1945 | memset(&ip6Prefix, 0, sizeof(otIp6Prefix)); |
| 1946 | |
Abtin Keshavarzian | 31ce3d4 | 2019-08-01 10:30:08 -0700 | [diff] [blame] | 1947 | VerifyOrExit(mAllowLocalNetworkDataChange, error = OT_ERROR_INVALID_STATE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1948 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1949 | SuccessOrExit(error = mDecoder.ReadIp6Address(ip6Prefix.mPrefix)); |
Łukasz Duda | 7b084ec | 2017-10-25 17:54:17 +0200 | [diff] [blame] | 1950 | SuccessOrExit(error = mDecoder.ReadUint8(prefixLength)); |
| 1951 | |
| 1952 | ip6Prefix.mLength = prefixLength; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1953 | |
| 1954 | error = otBorderRouterRemoveRoute(mInstance, &ip6Prefix); |
Abtin Keshavarzian | 02bdc69 | 2017-08-15 09:11:19 -0700 | [diff] [blame] | 1955 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1956 | // If the route prefix was not on the list, "remove" command is successful. |
| 1957 | if (error == OT_ERROR_NOT_FOUND) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1958 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1959 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 1962 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1963 | return error; |
| 1964 | } |
Jonathan Hui | f184973 | 2019-07-16 12:31:11 -0700 | [diff] [blame] | 1965 | #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1966 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 1967 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_STREAM_NET>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1968 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1969 | const uint8_t *framePtr = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1970 | uint16_t frameLen = 0; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1971 | const uint8_t *metaPtr = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1972 | uint16_t metaLen = 0; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1973 | otMessage * message = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 1974 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1975 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 1976 | SuccessOrExit(error = mDecoder.ReadDataWithLen(framePtr, frameLen)); |
| 1977 | SuccessOrExit(error = mDecoder.ReadData(metaPtr, metaLen)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1978 | |
| 1979 | // We ignore metadata for now. |
| 1980 | // May later include TX power, allow retransmits, etc... |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1981 | |
Zhanglong Xia | 19f2533 | 2018-12-20 05:55:26 +0800 | [diff] [blame] | 1982 | // STREAM_NET requires layer 2 security. |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 1983 | message = otIp6NewMessageFromBuffer(mInstance, framePtr, frameLen, nullptr); |
| 1984 | VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1985 | |
| 1986 | error = otIp6Send(mInstance, message); |
| 1987 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1988 | exit: |
| 1989 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1990 | if (error == OT_ERROR_NONE) |
| 1991 | { |
| 1992 | mInboundSecureIpFrameCounter++; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1993 | } |
| 1994 | else |
| 1995 | { |
| 1996 | mDroppedInboundIpFrameCounter++; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1997 | } |
| 1998 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 1999 | return error; |
| 2000 | } |
| 2001 | |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 2002 | #if OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2003 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2004 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_JAM_DETECT_ENABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2005 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2006 | return mEncoder.WriteBool(otJamDetectionIsEnabled(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2007 | } |
| 2008 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2009 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_JAM_DETECTED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2010 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2011 | return mEncoder.WriteBool(otJamDetectionGetState(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2012 | } |
| 2013 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2014 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_JAM_DETECT_RSSI_THRESHOLD>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2015 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2016 | return mEncoder.WriteInt8(otJamDetectionGetRssiThreshold(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2017 | } |
| 2018 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2019 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_JAM_DETECT_WINDOW>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2020 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2021 | return mEncoder.WriteUint8(otJamDetectionGetWindow(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2022 | } |
| 2023 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2024 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_JAM_DETECT_BUSY>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2025 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2026 | return mEncoder.WriteUint8(otJamDetectionGetBusyPeriod(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2029 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_JAM_DETECT_HISTORY_BITMAP>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2030 | { |
Abtin Keshavarzian | 07f4ae1 | 2017-12-07 10:22:24 -0800 | [diff] [blame] | 2031 | return mEncoder.WriteUint64(otJamDetectionGetHistoryBitmap(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2032 | } |
| 2033 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2034 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_JAM_DETECT_ENABLE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2035 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2036 | bool enabled; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2037 | otError error = OT_ERROR_NONE; |
| 2038 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2039 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2040 | |
| 2041 | if (enabled) |
| 2042 | { |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 2043 | IgnoreError(otJamDetectionStart(mInstance, &NcpBase::HandleJamStateChange_Jump, this)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2044 | } |
| 2045 | else |
| 2046 | { |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 2047 | IgnoreError(otJamDetectionStop(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2051 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2052 | } |
| 2053 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2054 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_JAM_DETECT_RSSI_THRESHOLD>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2055 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2056 | int8_t threshold = 0; |
| 2057 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2058 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2059 | SuccessOrExit(error = mDecoder.ReadInt8(threshold)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2060 | |
| 2061 | error = otJamDetectionSetRssiThreshold(mInstance, threshold); |
| 2062 | |
| 2063 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2064 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2065 | } |
| 2066 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2067 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_JAM_DETECT_WINDOW>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2068 | { |
| 2069 | uint8_t window = 0; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2070 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2071 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2072 | SuccessOrExit(error = mDecoder.ReadUint8(window)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2073 | |
| 2074 | error = otJamDetectionSetWindow(mInstance, window); |
| 2075 | |
| 2076 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2077 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2078 | } |
| 2079 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2080 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_JAM_DETECT_BUSY>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2081 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2082 | uint8_t busy = 0; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2083 | otError error = OT_ERROR_NONE; |
| 2084 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2085 | SuccessOrExit(error = mDecoder.ReadUint8(busy)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2086 | |
| 2087 | error = otJamDetectionSetBusyPeriod(mInstance, busy); |
| 2088 | |
| 2089 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2090 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | void NcpBase::HandleJamStateChange_Jump(bool aJamState, void *aContext) |
| 2094 | { |
| 2095 | static_cast<NcpBase *>(aContext)->HandleJamStateChange(aJamState); |
| 2096 | } |
| 2097 | |
| 2098 | void NcpBase::HandleJamStateChange(bool aJamState) |
| 2099 | { |
| 2100 | OT_UNUSED_VARIABLE(aJamState); |
| 2101 | |
| 2102 | mChangedPropsSet.AddProperty(SPINEL_PROP_JAM_DETECTED); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 2103 | mUpdateChangedPropsTask.Post(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2104 | } |
| 2105 | |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 2106 | #endif // OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2107 | |
Jonathan Hui | aa82391 | 2019-07-15 12:03:36 -0700 | [diff] [blame] | 2108 | #if OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE |
Abtin Keshavarzian | 1ca81fb | 2018-08-02 15:21:11 -0700 | [diff] [blame] | 2109 | |
| 2110 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHILD_SUPERVISION_CHECK_TIMEOUT>(void) |
| 2111 | { |
| 2112 | return mEncoder.WriteUint16(otChildSupervisionGetCheckTimeout(mInstance)); |
| 2113 | } |
| 2114 | |
| 2115 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_CHILD_SUPERVISION_CHECK_TIMEOUT>(void) |
| 2116 | { |
| 2117 | otError error = OT_ERROR_NONE; |
| 2118 | uint16_t timeout; |
| 2119 | |
| 2120 | SuccessOrExit(error = mDecoder.ReadUint16(timeout)); |
| 2121 | otChildSupervisionSetCheckTimeout(mInstance, timeout); |
| 2122 | |
| 2123 | exit: |
| 2124 | return error; |
| 2125 | } |
| 2126 | |
Jonathan Hui | aa82391 | 2019-07-15 12:03:36 -0700 | [diff] [blame] | 2127 | #endif // OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE |
Abtin Keshavarzian | 1ca81fb | 2018-08-02 15:21:11 -0700 | [diff] [blame] | 2128 | |
Jonathan Hui | 01ebc18 | 2019-07-12 15:21:49 -0700 | [diff] [blame] | 2129 | #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2130 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2131 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHANNEL_MONITOR_SAMPLE_INTERVAL>(void) |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2132 | { |
| 2133 | return mEncoder.WriteUint32(otChannelMonitorGetSampleInterval(mInstance)); |
| 2134 | } |
| 2135 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2136 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHANNEL_MONITOR_RSSI_THRESHOLD>(void) |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2137 | { |
| 2138 | return mEncoder.WriteInt8(otChannelMonitorGetRssiThreshold(mInstance)); |
| 2139 | } |
| 2140 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2141 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHANNEL_MONITOR_SAMPLE_WINDOW>(void) |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2142 | { |
| 2143 | return mEncoder.WriteUint32(otChannelMonitorGetSampleWindow(mInstance)); |
| 2144 | } |
| 2145 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2146 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHANNEL_MONITOR_SAMPLE_COUNT>(void) |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2147 | { |
| 2148 | return mEncoder.WriteUint32(otChannelMonitorGetSampleCount(mInstance)); |
| 2149 | } |
| 2150 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2151 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHANNEL_MONITOR_CHANNEL_OCCUPANCY>(void) |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2152 | { |
Shu Chen | a43a263 | 2019-04-09 12:25:16 +0800 | [diff] [blame] | 2153 | otError error = OT_ERROR_NONE; |
| 2154 | uint32_t channelMask = otLinkGetSupportedChannelMask(mInstance); |
| 2155 | uint8_t channelNum = sizeof(channelMask) * CHAR_BIT; |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2156 | |
Shu Chen | a43a263 | 2019-04-09 12:25:16 +0800 | [diff] [blame] | 2157 | for (uint8_t channel = 0; channel < channelNum; channel++) |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2158 | { |
Shu Chen | a43a263 | 2019-04-09 12:25:16 +0800 | [diff] [blame] | 2159 | if (!((1UL << channel) & channelMask)) |
| 2160 | { |
| 2161 | continue; |
| 2162 | } |
| 2163 | |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2164 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2165 | |
| 2166 | SuccessOrExit(error = mEncoder.WriteUint8(channel)); |
Abtin Keshavarzian | df45240 | 2018-04-16 11:17:09 -0700 | [diff] [blame] | 2167 | SuccessOrExit(error = mEncoder.WriteUint16(otChannelMonitorGetChannelOccupancy(mInstance, channel))); |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2168 | |
| 2169 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2170 | } |
| 2171 | |
| 2172 | exit: |
| 2173 | return error; |
| 2174 | } |
| 2175 | |
Jonathan Hui | 01ebc18 | 2019-07-12 15:21:49 -0700 | [diff] [blame] | 2176 | #endif // OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE |
Abtin Keshavarzian | ba5317a | 2018-01-13 13:37:04 -0800 | [diff] [blame] | 2177 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2178 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_CCA_FAILURE_RATE>(void) |
Abtin Keshavarzian | 171c951 | 2018-01-23 09:10:13 -0800 | [diff] [blame] | 2179 | { |
| 2180 | return mEncoder.WriteUint16(otLinkGetCcaFailureRate(mInstance)); |
| 2181 | } |
| 2182 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2183 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_TOTAL>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2184 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2185 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxTotal); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2186 | } |
| 2187 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2188 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_ACK_REQ>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2189 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2190 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxAckRequested); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2191 | } |
| 2192 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2193 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_ACKED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2194 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2195 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxAcked); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2196 | } |
| 2197 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2198 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_NO_ACK_REQ>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2199 | { |
| 2200 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxNoAckRequested); |
| 2201 | } |
| 2202 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2203 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_DATA>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2204 | { |
| 2205 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxData); |
| 2206 | } |
| 2207 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2208 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_DATA_POLL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2209 | { |
| 2210 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxDataPoll); |
| 2211 | } |
| 2212 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2213 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_BEACON>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2214 | { |
| 2215 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxBeacon); |
| 2216 | } |
| 2217 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2218 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_BEACON_REQ>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2219 | { |
| 2220 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxBeaconRequest); |
| 2221 | } |
| 2222 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2223 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_OTHER>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2224 | { |
| 2225 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxOther); |
| 2226 | } |
| 2227 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2228 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_RETRY>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2229 | { |
| 2230 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxRetry); |
| 2231 | } |
| 2232 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2233 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_ERR_CCA>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2234 | { |
| 2235 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxErrCca); |
| 2236 | } |
| 2237 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2238 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_UNICAST>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2239 | { |
| 2240 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxUnicast); |
| 2241 | } |
| 2242 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2243 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_PKT_BROADCAST>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2244 | { |
| 2245 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxBroadcast); |
| 2246 | } |
| 2247 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2248 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_ERR_ABORT>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2249 | { |
| 2250 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mTxErrAbort); |
| 2251 | } |
| 2252 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2253 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2254 | { |
| 2255 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxTotal); |
| 2256 | } |
| 2257 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2258 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_DATA>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2259 | { |
| 2260 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxData); |
| 2261 | } |
| 2262 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2263 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_DATA_POLL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2264 | { |
| 2265 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxDataPoll); |
| 2266 | } |
| 2267 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2268 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_BEACON>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2269 | { |
| 2270 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxBeacon); |
| 2271 | } |
| 2272 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2273 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_BEACON_REQ>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2274 | { |
| 2275 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxBeaconRequest); |
| 2276 | } |
| 2277 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2278 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_OTHER>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2279 | { |
| 2280 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxOther); |
| 2281 | } |
| 2282 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2283 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_FILT_WL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2284 | { |
rongli | b1baa7d | 2017-10-14 00:33:23 +0800 | [diff] [blame] | 2285 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxAddressFiltered); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2288 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_FILT_DA>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2289 | { |
| 2290 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxDestAddrFiltered); |
| 2291 | } |
| 2292 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2293 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_DUP>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2294 | { |
| 2295 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxDuplicated); |
| 2296 | } |
| 2297 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2298 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_UNICAST>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2299 | { |
| 2300 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxUnicast); |
| 2301 | } |
| 2302 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2303 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_PKT_BROADCAST>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2304 | { |
| 2305 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxBroadcast); |
| 2306 | } |
| 2307 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2308 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_ERR_EMPTY>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2309 | { |
| 2310 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxErrNoFrame); |
| 2311 | } |
| 2312 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2313 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_ERR_UKWN_NBR>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2314 | { |
| 2315 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxErrUnknownNeighbor); |
| 2316 | } |
| 2317 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2318 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_ERR_NVLD_SADDR>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2319 | { |
| 2320 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxErrInvalidSrcAddr); |
| 2321 | } |
| 2322 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2323 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_ERR_SECURITY>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2324 | { |
| 2325 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxErrSec); |
| 2326 | } |
| 2327 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2328 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_ERR_BAD_FCS>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2329 | { |
| 2330 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxErrFcs); |
| 2331 | } |
| 2332 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2333 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_ERR_OTHER>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2334 | { |
| 2335 | return mEncoder.WriteUint32(otLinkGetCounters(mInstance)->mRxErrOther); |
| 2336 | } |
| 2337 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2338 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_IP_SEC_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2339 | { |
| 2340 | return mEncoder.WriteUint32(mInboundSecureIpFrameCounter); |
| 2341 | } |
| 2342 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2343 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_IP_INSEC_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2344 | { |
| 2345 | return mEncoder.WriteUint32(mInboundInsecureIpFrameCounter); |
| 2346 | } |
| 2347 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2348 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_IP_DROPPED>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2349 | { |
| 2350 | return mEncoder.WriteUint32(mDroppedInboundIpFrameCounter); |
| 2351 | } |
| 2352 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2353 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_IP_SEC_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2354 | { |
| 2355 | return mEncoder.WriteUint32(mOutboundSecureIpFrameCounter); |
| 2356 | } |
| 2357 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2358 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_IP_INSEC_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2359 | { |
| 2360 | return mEncoder.WriteUint32(mOutboundInsecureIpFrameCounter); |
| 2361 | } |
| 2362 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2363 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_IP_DROPPED>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2364 | { |
| 2365 | return mEncoder.WriteUint32(mDroppedOutboundIpFrameCounter); |
| 2366 | } |
| 2367 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2368 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_TX_SPINEL_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2369 | { |
| 2370 | return mEncoder.WriteUint32(mTxSpinelFrameCounter); |
| 2371 | } |
| 2372 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2373 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_SPINEL_TOTAL>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2374 | { |
| 2375 | return mEncoder.WriteUint32(mRxSpinelFrameCounter); |
| 2376 | } |
| 2377 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2378 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_SPINEL_OUT_OF_ORDER_TID>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2379 | { |
| 2380 | return mEncoder.WriteUint32(mRxSpinelOutOfOrderTidCounter); |
| 2381 | } |
| 2382 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2383 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_RX_SPINEL_ERR>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2384 | { |
| 2385 | return mEncoder.WriteUint32(mFramingErrorCounter); |
| 2386 | } |
| 2387 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2388 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_IP_TX_SUCCESS>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2389 | { |
| 2390 | return mEncoder.WriteUint32(otThreadGetIp6Counters(mInstance)->mTxSuccess); |
| 2391 | } |
| 2392 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2393 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_IP_RX_SUCCESS>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2394 | { |
| 2395 | return mEncoder.WriteUint32(otThreadGetIp6Counters(mInstance)->mRxSuccess); |
| 2396 | } |
| 2397 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2398 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_IP_TX_FAILURE>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2399 | { |
| 2400 | return mEncoder.WriteUint32(otThreadGetIp6Counters(mInstance)->mTxFailure); |
| 2401 | } |
| 2402 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2403 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_IP_RX_FAILURE>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2404 | { |
| 2405 | return mEncoder.WriteUint32(otThreadGetIp6Counters(mInstance)->mRxFailure); |
| 2406 | } |
| 2407 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2408 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MSG_BUFFER_COUNTERS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2409 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2410 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2411 | otBufferInfo bufferInfo; |
| 2412 | |
| 2413 | otMessageGetBufferInfo(mInstance, &bufferInfo); |
| 2414 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2415 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mTotalBuffers)); |
| 2416 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mFreeBuffers)); |
| 2417 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.m6loSendMessages)); |
| 2418 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.m6loSendBuffers)); |
| 2419 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.m6loReassemblyMessages)); |
| 2420 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.m6loReassemblyBuffers)); |
| 2421 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mIp6Messages)); |
| 2422 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mIp6Buffers)); |
| 2423 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mMplMessages)); |
| 2424 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mMplBuffers)); |
| 2425 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mMleMessages)); |
| 2426 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mMleBuffers)); |
| 2427 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mArpMessages)); |
| 2428 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mArpBuffers)); |
| 2429 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mCoapMessages)); |
| 2430 | SuccessOrExit(mEncoder.WriteUint16(bufferInfo.mCoapBuffers)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2431 | |
| 2432 | exit: |
| 2433 | return error; |
| 2434 | } |
| 2435 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2436 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_ALL_MAC_COUNTERS>(void) |
Abtin Keshavarzian | 23069bb | 2018-01-23 09:00:27 -0800 | [diff] [blame] | 2437 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2438 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 23069bb | 2018-01-23 09:00:27 -0800 | [diff] [blame] | 2439 | const otMacCounters *counters = otLinkGetCounters(mInstance); |
| 2440 | |
Abtin Keshavarzian | 23069bb | 2018-01-23 09:00:27 -0800 | [diff] [blame] | 2441 | // Encode Tx related counters |
| 2442 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2443 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxTotal)); |
| 2444 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxUnicast)); |
| 2445 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxBroadcast)); |
| 2446 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxAckRequested)); |
| 2447 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxAcked)); |
| 2448 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxNoAckRequested)); |
| 2449 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxData)); |
| 2450 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxDataPoll)); |
| 2451 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxBeacon)); |
| 2452 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxBeaconRequest)); |
| 2453 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxOther)); |
| 2454 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxRetry)); |
| 2455 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxErrCca)); |
| 2456 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxErrAbort)); |
| 2457 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxErrBusyChannel)); |
Paweł Wańczyk | c646e6f | 2019-12-18 19:23:43 +0100 | [diff] [blame] | 2458 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxDirectMaxRetryExpiry)); |
| 2459 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxIndirectMaxRetryExpiry)); |
Abtin Keshavarzian | 23069bb | 2018-01-23 09:00:27 -0800 | [diff] [blame] | 2460 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2461 | |
| 2462 | // Encode Rx related counters |
| 2463 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2464 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxTotal)); |
| 2465 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxUnicast)); |
| 2466 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxBroadcast)); |
| 2467 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxData)); |
| 2468 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxDataPoll)); |
| 2469 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxBeacon)); |
| 2470 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxBeaconRequest)); |
| 2471 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxOther)); |
| 2472 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxAddressFiltered)); |
| 2473 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxDestAddrFiltered)); |
| 2474 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxDuplicated)); |
| 2475 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxErrNoFrame)); |
| 2476 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxErrUnknownNeighbor)); |
| 2477 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxErrInvalidSrcAddr)); |
| 2478 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxErrSec)); |
| 2479 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxErrFcs)); |
| 2480 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxErrOther)); |
| 2481 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2482 | |
| 2483 | exit: |
| 2484 | return error; |
| 2485 | } |
| 2486 | |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2487 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_CNTR_ALL_MAC_COUNTERS>(void) |
| 2488 | { |
| 2489 | otLinkResetCounters(mInstance); |
| 2490 | |
| 2491 | return OT_ERROR_NONE; |
| 2492 | } |
| 2493 | |
Abtin Keshavarzian | bd4ca81 | 2019-02-06 08:41:50 -0800 | [diff] [blame] | 2494 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_MLE_COUNTERS>(void) |
| 2495 | { |
| 2496 | otError error = OT_ERROR_NONE; |
| 2497 | const otMleCounters *counters = otThreadGetMleCounters(mInstance); |
| 2498 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2499 | OT_ASSERT(counters != nullptr); |
Abtin Keshavarzian | bd4ca81 | 2019-02-06 08:41:50 -0800 | [diff] [blame] | 2500 | |
| 2501 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mDisabledRole)); |
| 2502 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mDetachedRole)); |
| 2503 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mChildRole)); |
| 2504 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mRouterRole)); |
| 2505 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mLeaderRole)); |
| 2506 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mAttachAttempts)); |
| 2507 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mPartitionIdChanges)); |
| 2508 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mBetterPartitionAttachAttempts)); |
| 2509 | SuccessOrExit(error = mEncoder.WriteUint16(counters->mParentChanges)); |
| 2510 | |
| 2511 | exit: |
| 2512 | return error; |
| 2513 | } |
| 2514 | |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2515 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_CNTR_MLE_COUNTERS>(void) |
| 2516 | { |
| 2517 | otThreadResetMleCounters(mInstance); |
| 2518 | |
| 2519 | return OT_ERROR_NONE; |
| 2520 | } |
| 2521 | |
| 2522 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_ALL_IP_COUNTERS>(void) |
| 2523 | { |
| 2524 | otError error = OT_ERROR_NONE; |
| 2525 | const otIpCounters *counters = otThreadGetIp6Counters(mInstance); |
| 2526 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2527 | OT_ASSERT(counters != nullptr); |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2528 | |
| 2529 | // Encode Tx related counters |
| 2530 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2531 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxSuccess)); |
| 2532 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mTxFailure)); |
| 2533 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2534 | |
| 2535 | // Encode Rx related counters |
| 2536 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2537 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxSuccess)); |
| 2538 | SuccessOrExit(error = mEncoder.WriteUint32(counters->mRxFailure)); |
| 2539 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2540 | |
| 2541 | exit: |
| 2542 | return error; |
| 2543 | } |
| 2544 | |
Paweł Wańczyk | c646e6f | 2019-12-18 19:23:43 +0100 | [diff] [blame] | 2545 | #if OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE |
| 2546 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CNTR_MAC_RETRY_HISTOGRAM>(void) |
| 2547 | { |
| 2548 | otError error = OT_ERROR_NONE; |
| 2549 | const uint32_t *histogramDirect; |
| 2550 | const uint32_t *histogramIndirect; |
| 2551 | uint8_t histogramDirectEntries; |
| 2552 | uint8_t histogramIndirectEntries; |
| 2553 | |
| 2554 | histogramDirect = otLinkGetTxDirectRetrySuccessHistogram(mInstance, &histogramDirectEntries); |
| 2555 | histogramIndirect = otLinkGetTxIndirectRetrySuccessHistogram(mInstance, &histogramIndirectEntries); |
| 2556 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2557 | OT_ASSERT((histogramDirectEntries == 0) || (histogramDirect != nullptr)); |
| 2558 | OT_ASSERT((histogramIndirectEntries == 0) || (histogramIndirect != nullptr)); |
Paweł Wańczyk | c646e6f | 2019-12-18 19:23:43 +0100 | [diff] [blame] | 2559 | |
| 2560 | // Encode direct message retries histogram |
| 2561 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2562 | for (uint8_t i = 0; i < histogramDirectEntries; i++) |
| 2563 | { |
| 2564 | SuccessOrExit(error = mEncoder.WriteUint32(histogramDirect[i])); |
| 2565 | } |
| 2566 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2567 | |
| 2568 | // Encode indirect message retries histogram |
| 2569 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2570 | for (uint8_t i = 0; i < histogramIndirectEntries; i++) |
| 2571 | { |
| 2572 | SuccessOrExit(error = mEncoder.WriteUint32(histogramIndirect[i])); |
| 2573 | } |
| 2574 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 2575 | |
| 2576 | exit: |
| 2577 | return error; |
| 2578 | } |
| 2579 | |
| 2580 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_CNTR_MAC_RETRY_HISTOGRAM>(void) |
| 2581 | { |
| 2582 | otLinkResetTxRetrySuccessHistogram(mInstance); |
| 2583 | |
| 2584 | return OT_ERROR_NONE; |
| 2585 | } |
| 2586 | #endif // OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE |
| 2587 | |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2588 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_CNTR_ALL_IP_COUNTERS>(void) |
| 2589 | { |
| 2590 | otThreadResetIp6Counters(mInstance); |
| 2591 | |
| 2592 | return OT_ERROR_NONE; |
| 2593 | } |
| 2594 | |
Jonathan Hui | 0baaf1a | 2019-07-15 12:57:32 -0700 | [diff] [blame] | 2595 | #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2596 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2597 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_ALLOWLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2598 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2599 | otMacFilterEntry entry; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2600 | otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2601 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2602 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2603 | while (otLinkFilterGetNextAddress(mInstance, &iterator, &entry) == OT_ERROR_NONE) |
| 2604 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2605 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2606 | |
| 2607 | SuccessOrExit(error = mEncoder.WriteEui64(entry.mExtAddress)); |
| 2608 | SuccessOrExit(error = mEncoder.WriteInt8(entry.mRssIn)); |
| 2609 | |
| 2610 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2611 | } |
| 2612 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2613 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2614 | return error; |
| 2615 | } |
| 2616 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2617 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_ALLOWLIST_ENABLED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2618 | { |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2619 | return mEncoder.WriteBool(otLinkFilterGetAddressMode(mInstance) == OT_MAC_FILTER_ADDRESS_MODE_ALLOWLIST); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2620 | } |
| 2621 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2622 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_DENYLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2623 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2624 | otMacFilterEntry entry; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2625 | otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2626 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2627 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2628 | while (otLinkFilterGetNextAddress(mInstance, &iterator, &entry) == OT_ERROR_NONE) |
| 2629 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2630 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2631 | SuccessOrExit(error = mEncoder.WriteEui64(entry.mExtAddress)); |
| 2632 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2633 | } |
| 2634 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2635 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2636 | return error; |
| 2637 | } |
| 2638 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2639 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_DENYLIST_ENABLED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2640 | { |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2641 | return mEncoder.WriteBool(otLinkFilterGetAddressMode(mInstance) == OT_MAC_FILTER_ADDRESS_MODE_DENYLIST); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2642 | } |
| 2643 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2644 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MAC_FIXED_RSS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2645 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2646 | otMacFilterEntry entry; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2647 | otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2648 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2649 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2650 | while (otLinkFilterGetNextRssIn(mInstance, &iterator, &entry) == OT_ERROR_NONE) |
| 2651 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2652 | SuccessOrExit(error = mEncoder.OpenStruct()); |
| 2653 | |
| 2654 | SuccessOrExit(error = mEncoder.WriteEui64(entry.mExtAddress)); |
| 2655 | SuccessOrExit(error = mEncoder.WriteInt8(entry.mRssIn)); |
| 2656 | |
| 2657 | SuccessOrExit(error = mEncoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2658 | } |
| 2659 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2660 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2661 | return error; |
| 2662 | } |
| 2663 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2664 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_ALLOWLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2665 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2666 | otError error = OT_ERROR_NONE; |
| 2667 | |
| 2668 | // First, clear the address filter entries. |
| 2669 | otLinkFilterClearAddresses(mInstance); |
| 2670 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2671 | while (mDecoder.GetRemainingLengthInStruct() > 0) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2672 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2673 | const otExtAddress *extAddress = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2674 | int8_t rss; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2675 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2676 | SuccessOrExit(error = mDecoder.OpenStruct()); |
| 2677 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2678 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2679 | if (!mDecoder.IsAllReadInStruct()) |
| 2680 | { |
| 2681 | SuccessOrExit(error = mDecoder.ReadInt8(rss)); |
| 2682 | } |
| 2683 | else |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2684 | { |
| 2685 | rss = OT_MAC_FILTER_FIXED_RSS_DISABLED; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2686 | } |
| 2687 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2688 | SuccessOrExit(error = mDecoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2689 | |
| 2690 | error = otLinkFilterAddAddress(mInstance, extAddress); |
| 2691 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2692 | if (error == OT_ERROR_ALREADY) |
| 2693 | { |
| 2694 | error = OT_ERROR_NONE; |
| 2695 | } |
| 2696 | |
| 2697 | SuccessOrExit(error); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2698 | |
| 2699 | if (rss != OT_MAC_FILTER_FIXED_RSS_DISABLED) |
| 2700 | { |
| 2701 | SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, extAddress, rss)); |
| 2702 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | exit: |
| 2706 | // If we had an error, we may have actually changed |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2707 | // the state of the allowlist, so we need to report |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2708 | // those incomplete changes via an asynchronous |
| 2709 | // change event. |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2710 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2711 | if (error != OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2712 | { |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2713 | IgnoreError(WritePropertyValueIsFrame(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, SPINEL_PROP_MAC_ALLOWLIST)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | return error; |
| 2717 | } |
| 2718 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2719 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_ALLOWLIST_ENABLED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2720 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2721 | bool enabled; |
| 2722 | otError error = OT_ERROR_NONE; |
| 2723 | otMacFilterAddressMode mode = OT_MAC_FILTER_ADDRESS_MODE_DISABLED; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2724 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2725 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2726 | |
| 2727 | if (enabled) |
| 2728 | { |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2729 | mode = OT_MAC_FILTER_ADDRESS_MODE_ALLOWLIST; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2730 | } |
| 2731 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 2732 | otLinkFilterSetAddressMode(mInstance, mode); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2733 | |
| 2734 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2735 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2736 | } |
| 2737 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2738 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_DENYLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2739 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2740 | otError error = OT_ERROR_NONE; |
| 2741 | |
| 2742 | // First, clear the address filter entries. |
| 2743 | otLinkFilterClearAddresses(mInstance); |
| 2744 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2745 | while (mDecoder.GetRemainingLengthInStruct() > 0) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2746 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2747 | const otExtAddress *extAddress = nullptr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2748 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2749 | SuccessOrExit(error = mDecoder.OpenStruct()); |
| 2750 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
| 2751 | SuccessOrExit(error = mDecoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2752 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 2753 | otLinkFilterRemoveAddress(mInstance, extAddress); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | exit: |
| 2757 | // If we had an error, we may have actually changed |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2758 | // the state of the denylist, so we need to report |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2759 | // those incomplete changes via an asynchronous |
| 2760 | // change event. |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2761 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2762 | if (error != OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2763 | { |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2764 | IgnoreError(WritePropertyValueIsFrame(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, SPINEL_PROP_MAC_DENYLIST)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | return error; |
| 2768 | } |
| 2769 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2770 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_DENYLIST_ENABLED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2771 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2772 | bool enabled; |
| 2773 | otError error = OT_ERROR_NONE; |
| 2774 | otMacFilterAddressMode mode = OT_MAC_FILTER_ADDRESS_MODE_DISABLED; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2775 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2776 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2777 | |
| 2778 | if (enabled) |
| 2779 | { |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2780 | mode = OT_MAC_FILTER_ADDRESS_MODE_DENYLIST; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2781 | } |
| 2782 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 2783 | otLinkFilterSetAddressMode(mInstance, mode); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2784 | |
| 2785 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2786 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2787 | } |
| 2788 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2789 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MAC_FIXED_RSS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2790 | { |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2791 | otError error = OT_ERROR_NONE; |
| 2792 | |
| 2793 | // First, clear the address filter entries. |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 2794 | otLinkFilterClearAllRssIn(mInstance); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2795 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2796 | while (mDecoder.GetRemainingLengthInStruct() > 0) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2797 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2798 | const otExtAddress *extAddress; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2799 | int8_t rss; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2800 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2801 | SuccessOrExit(error = mDecoder.OpenStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2802 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2803 | if (mDecoder.GetRemainingLengthInStruct() > sizeof(otExtAddress)) |
| 2804 | { |
| 2805 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
| 2806 | } |
| 2807 | else |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2808 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2809 | extAddress = nullptr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2810 | } |
| 2811 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2812 | SuccessOrExit(error = mDecoder.ReadInt8(rss)); |
| 2813 | |
| 2814 | SuccessOrExit(error = mDecoder.CloseStruct()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2815 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 2816 | if (extAddress != nullptr) |
| 2817 | { |
| 2818 | SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, extAddress, rss)); |
| 2819 | } |
| 2820 | else |
| 2821 | { |
| 2822 | otLinkFilterSetDefaultRssIn(mInstance, rss); |
| 2823 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | exit: |
| 2827 | // If we had an error, we may have actually changed |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2828 | // the state of the RssIn filter, so we need to report |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2829 | // those incomplete changes via an asynchronous |
| 2830 | // change event. |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2831 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2832 | if (error != OT_ERROR_NONE) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2833 | { |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 2834 | IgnoreError(WritePropertyValueIsFrame(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, SPINEL_PROP_MAC_FIXED_RSS)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | return error; |
| 2838 | } |
Jonathan Hui | 0baaf1a | 2019-07-15 12:57:32 -0700 | [diff] [blame] | 2839 | #endif // OPENTHREAD_CONFIG_MAC_FILTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2840 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2841 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_MODE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2842 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2843 | uint8_t numericMode; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2844 | otLinkModeConfig modeConfig = otThreadGetLinkMode(mInstance); |
| 2845 | |
Jonathan Hui | dc9b032 | 2020-09-29 15:30:37 -0700 | [diff] [blame^] | 2846 | numericMode = LinkFlagsToFlagByte(modeConfig.mRxOnWhenIdle, modeConfig.mDeviceType, modeConfig.mNetworkData); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2847 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2848 | return mEncoder.WriteUint8(numericMode); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2849 | } |
| 2850 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2851 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_MODE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2852 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2853 | uint8_t numericMode = 0; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2854 | otLinkModeConfig modeConfig; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2855 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2856 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2857 | SuccessOrExit(mDecoder.ReadUint8(numericMode)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2858 | |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2859 | modeConfig.mRxOnWhenIdle = |
| 2860 | ((numericMode & SPINEL_THREAD_MODE_RX_ON_WHEN_IDLE) == SPINEL_THREAD_MODE_RX_ON_WHEN_IDLE); |
Jonathan Hui | 5a48983 | 2018-09-27 09:33:03 -0700 | [diff] [blame] | 2861 | modeConfig.mDeviceType = ((numericMode & SPINEL_THREAD_MODE_FULL_THREAD_DEV) == SPINEL_THREAD_MODE_FULL_THREAD_DEV); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2862 | modeConfig.mNetworkData = |
| 2863 | ((numericMode & SPINEL_THREAD_MODE_FULL_NETWORK_DATA) == SPINEL_THREAD_MODE_FULL_NETWORK_DATA); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2864 | |
| 2865 | error = otThreadSetLinkMode(mInstance, modeConfig); |
| 2866 | |
| 2867 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2868 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2869 | } |
| 2870 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2871 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_CHILD_TIMEOUT>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2872 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2873 | return mEncoder.WriteUint32(otThreadGetChildTimeout(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2874 | } |
| 2875 | |
Abtin Keshavarzian | d67c4e2 | 2018-08-01 22:55:35 -0700 | [diff] [blame] | 2876 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_CHILD_TIMEOUT>(void) |
| 2877 | { |
| 2878 | uint32_t timeout = 0; |
| 2879 | otError error = OT_ERROR_NONE; |
| 2880 | |
| 2881 | SuccessOrExit(error = mDecoder.ReadUint32(timeout)); |
| 2882 | |
| 2883 | otThreadSetChildTimeout(mInstance, timeout); |
| 2884 | |
| 2885 | exit: |
| 2886 | return error; |
| 2887 | } |
| 2888 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2889 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_RLOC16>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2890 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2891 | return mEncoder.WriteUint16(otThreadGetRloc16(mInstance)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2892 | } |
| 2893 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2894 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NET_REQUIRE_JOIN_EXISTING>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2895 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2896 | return mEncoder.WriteBool(mRequireJoinExistingNetwork); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2897 | } |
| 2898 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2899 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NET_REQUIRE_JOIN_EXISTING>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2900 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2901 | return mDecoder.ReadBool(mRequireJoinExistingNetwork); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2902 | } |
| 2903 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2904 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_STREAM_NET_INSECURE>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2905 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2906 | const uint8_t * framePtr = nullptr; |
Zhanglong Xia | f890959 | 2018-11-06 04:48:39 +0800 | [diff] [blame] | 2907 | uint16_t frameLen = 0; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2908 | const uint8_t * metaPtr = nullptr; |
Zhanglong Xia | f890959 | 2018-11-06 04:48:39 +0800 | [diff] [blame] | 2909 | uint16_t metaLen = 0; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2910 | otMessage * message = nullptr; |
Zhanglong Xia | f890959 | 2018-11-06 04:48:39 +0800 | [diff] [blame] | 2911 | otError error = OT_ERROR_NONE; |
| 2912 | otMessageSettings msgSettings = {false, OT_MESSAGE_PRIORITY_NORMAL}; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2913 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2914 | SuccessOrExit(mDecoder.ReadDataWithLen(framePtr, frameLen)); |
| 2915 | SuccessOrExit(mDecoder.ReadData(metaPtr, metaLen)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2916 | |
| 2917 | // We ignore metadata for now. |
| 2918 | // May later include TX power, allow retransmits, etc... |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2919 | |
Zhanglong Xia | 19f2533 | 2018-12-20 05:55:26 +0800 | [diff] [blame] | 2920 | // STREAM_NET_INSECURE packets are not secured at layer 2. |
| 2921 | message = otIp6NewMessageFromBuffer(mInstance, framePtr, frameLen, &msgSettings); |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 2922 | VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2923 | |
| 2924 | // Ensure the insecure message is forwarded using direct transmission. |
| 2925 | otMessageSetDirectTransmission(message, true); |
| 2926 | |
| 2927 | error = otIp6Send(mInstance, message); |
| 2928 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2929 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2930 | if (error == OT_ERROR_NONE) |
| 2931 | { |
| 2932 | mInboundInsecureIpFrameCounter++; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2933 | } |
| 2934 | else |
| 2935 | { |
| 2936 | mDroppedInboundIpFrameCounter++; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2937 | } |
| 2938 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2939 | return error; |
| 2940 | } |
| 2941 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2942 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_CNTR_RESET>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2943 | { |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2944 | otLinkResetCounters(mInstance); |
Paweł Wańczyk | c646e6f | 2019-12-18 19:23:43 +0100 | [diff] [blame] | 2945 | #if OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE |
| 2946 | otLinkResetTxRetrySuccessHistogram(mInstance); |
| 2947 | #endif |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2948 | otThreadResetIp6Counters(mInstance); |
| 2949 | otThreadResetMleCounters(mInstance); |
| 2950 | ResetCounters(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2951 | |
Abtin Keshavarzian | 112d1bb | 2019-10-29 15:34:03 -0700 | [diff] [blame] | 2952 | return OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2953 | } |
| 2954 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 2955 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_THREAD_ASSISTING_PORTS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2956 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 2957 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2958 | uint16_t port; |
| 2959 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2960 | SuccessOrExit(error = mDecoder.ReadUint16(port)); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2961 | |
| 2962 | error = otIp6AddUnsecurePort(mInstance, port); |
| 2963 | exit: |
| 2964 | return error; |
| 2965 | } |
| 2966 | |
Jonathan Hui | 0baaf1a | 2019-07-15 12:57:32 -0700 | [diff] [blame] | 2967 | #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2968 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 2969 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_MAC_ALLOWLIST>(void) |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2970 | { |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 2971 | otError error = OT_ERROR_NONE; |
| 2972 | const otExtAddress *extAddress; |
| 2973 | int8_t rss = OT_MAC_FILTER_FIXED_RSS_DISABLED; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2974 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2975 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2976 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 2977 | if (!mDecoder.IsAllRead()) |
| 2978 | { |
| 2979 | SuccessOrExit(error = mDecoder.ReadInt8(rss)); |
| 2980 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2981 | |
| 2982 | error = otLinkFilterAddAddress(mInstance, extAddress); |
| 2983 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 2984 | if (error == OT_ERROR_ALREADY) |
| 2985 | { |
| 2986 | error = OT_ERROR_NONE; |
| 2987 | } |
| 2988 | |
| 2989 | SuccessOrExit(error); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2990 | |
| 2991 | if (rss != OT_MAC_FILTER_FIXED_RSS_DISABLED) |
| 2992 | { |
| 2993 | error = otLinkFilterAddRssIn(mInstance, extAddress, rss); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2994 | } |
| 2995 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2996 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 2997 | return error; |
| 2998 | } |
| 2999 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 3000 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_MAC_DENYLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3001 | { |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 3002 | otError error = OT_ERROR_NONE; |
| 3003 | const otExtAddress *extAddress; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3004 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3005 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3006 | |
| 3007 | error = otLinkFilterAddAddress(mInstance, extAddress); |
| 3008 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3009 | if (error == OT_ERROR_ALREADY) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3010 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3011 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3012 | } |
| 3013 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3014 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3015 | return error; |
| 3016 | } |
| 3017 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3018 | template <> otError NcpBase::HandlePropertyInsert<SPINEL_PROP_MAC_FIXED_RSS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3019 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3020 | otError error = OT_ERROR_NONE; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3021 | const otExtAddress *extAddress = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3022 | int8_t rss = OT_MAC_FILTER_FIXED_RSS_DISABLED; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3023 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3024 | if (mDecoder.GetRemainingLength() > sizeof(int8_t)) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3025 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3026 | SuccessOrExit(mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3027 | } |
| 3028 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3029 | SuccessOrExit(mDecoder.ReadInt8(rss)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3030 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 3031 | if (extAddress != nullptr) |
| 3032 | { |
| 3033 | error = otLinkFilterAddRssIn(mInstance, extAddress, rss); |
| 3034 | } |
| 3035 | else |
| 3036 | { |
| 3037 | otLinkFilterSetDefaultRssIn(mInstance, rss); |
| 3038 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3039 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3040 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3041 | return error; |
| 3042 | } |
| 3043 | |
Jonathan Hui | 0baaf1a | 2019-07-15 12:57:32 -0700 | [diff] [blame] | 3044 | #endif // OPENTHREAD_CONFIG_MAC_FILTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3045 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3046 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_THREAD_ASSISTING_PORTS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3047 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3048 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3049 | uint16_t port; |
| 3050 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3051 | SuccessOrExit(error = mDecoder.ReadUint16(port)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3052 | |
| 3053 | error = otIp6RemoveUnsecurePort(mInstance, port); |
Abtin Keshavarzian | 02bdc69 | 2017-08-15 09:11:19 -0700 | [diff] [blame] | 3054 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3055 | // If unsecure port was not on the list, "remove" command is successful. |
| 3056 | if (error == OT_ERROR_NOT_FOUND) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3057 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3058 | error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3059 | } |
| 3060 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3061 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3062 | return error; |
| 3063 | } |
| 3064 | |
Jonathan Hui | 0baaf1a | 2019-07-15 12:57:32 -0700 | [diff] [blame] | 3065 | #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3066 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 3067 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_MAC_ALLOWLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3068 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3069 | otError error = OT_ERROR_NONE; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3070 | const otExtAddress *extAddress = nullptr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3071 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3072 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3073 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 3074 | otLinkFilterRemoveAddress(mInstance, extAddress); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3075 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3076 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3077 | return error; |
| 3078 | } |
| 3079 | |
Jonathan Hui | 5bffd8b | 2020-09-11 16:03:39 -0700 | [diff] [blame] | 3080 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_MAC_DENYLIST>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3081 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3082 | otError error = OT_ERROR_NONE; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3083 | const otExtAddress *extAddress = nullptr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3084 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3085 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3086 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 3087 | otLinkFilterRemoveAddress(mInstance, extAddress); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3088 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3089 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3090 | return error; |
| 3091 | } |
| 3092 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3093 | template <> otError NcpBase::HandlePropertyRemove<SPINEL_PROP_MAC_FIXED_RSS>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3094 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3095 | otError error = OT_ERROR_NONE; |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3096 | const otExtAddress *extAddress = nullptr; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3097 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3098 | if (mDecoder.GetRemainingLength() > 0) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3099 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3100 | SuccessOrExit(error = mDecoder.ReadEui64(extAddress)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3101 | } |
| 3102 | |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 3103 | if (extAddress != nullptr) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3104 | { |
Abtin Keshavarzian | 6f1fc2e | 2020-06-29 08:59:34 -0700 | [diff] [blame] | 3105 | otLinkFilterRemoveRssIn(mInstance, extAddress); |
| 3106 | } |
| 3107 | else |
| 3108 | { |
| 3109 | otLinkFilterClearDefaultRssIn(mInstance); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3110 | } |
| 3111 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3112 | exit: |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3113 | return error; |
| 3114 | } |
| 3115 | |
Jonathan Hui | 0baaf1a | 2019-07-15 12:57:32 -0700 | [diff] [blame] | 3116 | #endif // OPENTHREAD_CONFIG_MAC_FILTER_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3117 | |
Yakun Xu | 6537459 | 2020-03-12 02:42:30 +0800 | [diff] [blame] | 3118 | #if OPENTHREAD_PLATFORM_POSIX |
Abtin Keshavarzian | 7c8a372 | 2018-10-12 10:40:57 -0700 | [diff] [blame] | 3119 | |
| 3120 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_RCP_VERSION>(void) |
| 3121 | { |
| 3122 | return mEncoder.WriteUtf8(otGetRadioVersionString(mInstance)); |
| 3123 | } |
| 3124 | |
Abtin Keshavarzian | 6e718a1 | 2019-04-04 17:39:22 -0700 | [diff] [blame] | 3125 | #endif |
Abtin Keshavarzian | 7c8a372 | 2018-10-12 10:40:57 -0700 | [diff] [blame] | 3126 | |
Jonathan Hui | 4a475d5 | 2019-07-15 22:13:11 -0700 | [diff] [blame] | 3127 | #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE |
Abtin Keshavarzian | 89396ab | 2019-03-11 14:59:43 -0700 | [diff] [blame] | 3128 | |
| 3129 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_SLAAC_ENABLED>(void) |
| 3130 | { |
| 3131 | return mEncoder.WriteBool(otIp6IsSlaacEnabled(mInstance)); |
| 3132 | } |
| 3133 | |
| 3134 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_SLAAC_ENABLED>(void) |
| 3135 | { |
| 3136 | otError error = OT_ERROR_NONE; |
| 3137 | bool enabled; |
| 3138 | |
| 3139 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
| 3140 | otIp6SetSlaacEnabled(mInstance, enabled); |
| 3141 | |
| 3142 | exit: |
| 3143 | return error; |
| 3144 | } |
| 3145 | |
Jonathan Hui | 4a475d5 | 2019-07-15 22:13:11 -0700 | [diff] [blame] | 3146 | #endif // OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE |
Abtin Keshavarzian | 89396ab | 2019-03-11 14:59:43 -0700 | [diff] [blame] | 3147 | |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3148 | #if OPENTHREAD_CONFIG_LEGACY_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3149 | |
| 3150 | void NcpBase::RegisterLegacyHandlers(const otNcpLegacyHandlers *aHandlers) |
| 3151 | { |
| 3152 | mLegacyHandlers = aHandlers; |
| 3153 | bool isEnabled; |
| 3154 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3155 | VerifyOrExit(mLegacyHandlers != nullptr, OT_NOOP); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3156 | |
| 3157 | isEnabled = (otThreadGetDeviceRole(mInstance) != OT_DEVICE_ROLE_DISABLED); |
| 3158 | |
| 3159 | if (isEnabled) |
| 3160 | { |
| 3161 | if (mLegacyHandlers->mStartLegacy) |
| 3162 | { |
| 3163 | mLegacyHandlers->mStartLegacy(); |
| 3164 | } |
| 3165 | } |
| 3166 | else |
| 3167 | { |
| 3168 | if (mLegacyHandlers->mStopLegacy) |
| 3169 | { |
| 3170 | mLegacyHandlers->mStopLegacy(); |
| 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | if (mLegacyHandlers->mSetLegacyUlaPrefix) |
| 3175 | { |
| 3176 | mLegacyHandlers->mSetLegacyUlaPrefix(mLegacyUlaPrefix); |
| 3177 | } |
| 3178 | |
| 3179 | exit: |
| 3180 | return; |
| 3181 | } |
| 3182 | |
| 3183 | void NcpBase::HandleDidReceiveNewLegacyUlaPrefix(const uint8_t *aUlaPrefix) |
| 3184 | { |
| 3185 | memcpy(mLegacyUlaPrefix, aUlaPrefix, OT_NCP_LEGACY_ULA_PREFIX_LENGTH); |
| 3186 | mChangedPropsSet.AddProperty(SPINEL_PROP_NEST_LEGACY_ULA_PREFIX); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3187 | mUpdateChangedPropsTask.Post(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | void NcpBase::HandleLegacyNodeDidJoin(const otExtAddress *aExtAddr) |
| 3191 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3192 | mLegacyNodeDidJoin = true; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3193 | mLegacyLastJoinedNode = *aExtAddr; |
| 3194 | mChangedPropsSet.AddProperty(SPINEL_PROP_NEST_LEGACY_LAST_NODE_JOINED); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3195 | mUpdateChangedPropsTask.Post(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3196 | } |
| 3197 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3198 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NEST_LEGACY_ULA_PREFIX>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3199 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3200 | return mEncoder.WriteData(mLegacyUlaPrefix, sizeof(mLegacyUlaPrefix)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3201 | } |
| 3202 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3203 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_NEST_LEGACY_ULA_PREFIX>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3204 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3205 | const uint8_t *ptr = nullptr; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3206 | uint16_t len; |
| 3207 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3208 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3209 | SuccessOrExit(error = mDecoder.ReadData(ptr, len)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3210 | |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3211 | VerifyOrExit(len <= sizeof(mLegacyUlaPrefix), error = OT_ERROR_PARSE); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3212 | |
| 3213 | memset(mLegacyUlaPrefix, 0, sizeof(mLegacyUlaPrefix)); |
| 3214 | memcpy(mLegacyUlaPrefix, ptr, len); |
| 3215 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3216 | if ((mLegacyHandlers != nullptr) && (mLegacyHandlers->mSetLegacyUlaPrefix != nullptr)) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3217 | { |
| 3218 | mLegacyHandlers->mSetLegacyUlaPrefix(mLegacyUlaPrefix); |
| 3219 | } |
| 3220 | |
| 3221 | exit: |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3222 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3223 | } |
| 3224 | |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3225 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_NEST_LEGACY_LAST_NODE_JOINED>(void) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3226 | { |
| 3227 | if (!mLegacyNodeDidJoin) |
| 3228 | { |
| 3229 | memset(&mLegacyLastJoinedNode, 0, sizeof(mLegacyLastJoinedNode)); |
| 3230 | } |
| 3231 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3232 | return mEncoder.WriteEui64(mLegacyLastJoinedNode); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3233 | } |
Abtin Keshavarzian | ecd2845 | 2018-04-05 10:20:47 -0700 | [diff] [blame] | 3234 | |
| 3235 | void NcpBase::StartLegacy(void) |
| 3236 | { |
| 3237 | mLegacyNodeDidJoin = false; |
| 3238 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3239 | if ((mLegacyHandlers != nullptr) && (mLegacyHandlers->mStartLegacy != nullptr)) |
Abtin Keshavarzian | ecd2845 | 2018-04-05 10:20:47 -0700 | [diff] [blame] | 3240 | { |
| 3241 | mLegacyHandlers->mStartLegacy(); |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | void NcpBase::StopLegacy(void) |
| 3246 | { |
| 3247 | mLegacyNodeDidJoin = false; |
| 3248 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3249 | if ((mLegacyHandlers != nullptr) && (mLegacyHandlers->mStopLegacy != nullptr)) |
Abtin Keshavarzian | ecd2845 | 2018-04-05 10:20:47 -0700 | [diff] [blame] | 3250 | { |
| 3251 | mLegacyHandlers->mStopLegacy(); |
| 3252 | } |
| 3253 | } |
| 3254 | |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3255 | #endif // OPENTHREAD_CONFIG_LEGACY_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3256 | |
Jonathan Hui | 926b9e8 | 2019-07-15 21:16:37 -0700 | [diff] [blame] | 3257 | #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE |
Yakun Xu | 8ccf138 | 2018-06-29 00:26:31 +0800 | [diff] [blame] | 3258 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_NETWORK_TIME>(void) |
Shu Chen | e69019f | 2018-06-27 02:46:32 +0800 | [diff] [blame] | 3259 | { |
| 3260 | otError error = OT_ERROR_NONE; |
| 3261 | otNetworkTimeStatus networkTimeStatus; |
| 3262 | uint64_t time; |
| 3263 | |
Jonathan Hui | fa395d7 | 2019-05-20 08:50:43 -0700 | [diff] [blame] | 3264 | networkTimeStatus = otNetworkTimeGet(mInstance, &time); |
Shu Chen | e69019f | 2018-06-27 02:46:32 +0800 | [diff] [blame] | 3265 | |
| 3266 | SuccessOrExit(error = mEncoder.WriteUint64(time)); |
| 3267 | SuccessOrExit(error = mEncoder.WriteInt8((int8_t)networkTimeStatus)); |
| 3268 | |
| 3269 | exit: |
| 3270 | return error; |
| 3271 | } |
Joseph Newman | 4f99bcf | 2018-11-08 06:32:31 +0000 | [diff] [blame] | 3272 | |
| 3273 | void NcpBase::HandleTimeSyncUpdate(void *aContext) |
| 3274 | { |
| 3275 | static_cast<NcpBase *>(aContext)->HandleTimeSyncUpdate(); |
| 3276 | } |
| 3277 | |
| 3278 | void NcpBase::HandleTimeSyncUpdate(void) |
| 3279 | { |
| 3280 | mChangedPropsSet.AddProperty(SPINEL_PROP_THREAD_NETWORK_TIME); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3281 | mUpdateChangedPropsTask.Post(); |
Joseph Newman | 4f99bcf | 2018-11-08 06:32:31 +0000 | [diff] [blame] | 3282 | } |
Jonathan Hui | 926b9e8 | 2019-07-15 21:16:37 -0700 | [diff] [blame] | 3283 | #endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE |
Shu Chen | e69019f | 2018-06-27 02:46:32 +0800 | [diff] [blame] | 3284 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3285 | void NcpBase::HandleActiveScanResult_Jump(otActiveScanResult *aResult, void *aContext) |
| 3286 | { |
| 3287 | static_cast<NcpBase *>(aContext)->HandleActiveScanResult(aResult); |
| 3288 | } |
| 3289 | |
| 3290 | // ---------------------------------------------------------------------------- |
| 3291 | // MARK: Scan Results Glue |
| 3292 | // ---------------------------------------------------------------------------- |
| 3293 | |
| 3294 | void NcpBase::HandleActiveScanResult(otActiveScanResult *aResult) |
| 3295 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3296 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3297 | |
| 3298 | if (aResult) |
| 3299 | { |
| 3300 | uint8_t flags = static_cast<uint8_t>(aResult->mVersion << SPINEL_BEACON_THREAD_FLAG_VERSION_SHIFT); |
| 3301 | |
| 3302 | if (aResult->mIsJoinable) |
| 3303 | { |
| 3304 | flags |= SPINEL_BEACON_THREAD_FLAG_JOINABLE; |
| 3305 | } |
| 3306 | |
| 3307 | if (aResult->mIsNative) |
| 3308 | { |
| 3309 | flags |= SPINEL_BEACON_THREAD_FLAG_NATIVE; |
| 3310 | } |
| 3311 | |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3312 | SuccessOrExit(error = mEncoder.BeginFrame(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, |
| 3313 | SPINEL_CMD_PROP_VALUE_INSERTED, SPINEL_PROP_MAC_SCAN_BEACON)); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3314 | SuccessOrExit(error = mEncoder.WriteUint8(aResult->mChannel)); |
| 3315 | SuccessOrExit(error = mEncoder.WriteInt8(aResult->mRssi)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3316 | |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3317 | SuccessOrExit(error = mEncoder.OpenStruct()); // "mac-layer data" |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3318 | SuccessOrExit(error = mEncoder.WriteEui64(aResult->mExtAddress)); |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3319 | SuccessOrExit(error = mEncoder.WriteUint16(0xffff)); // short address, not given |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3320 | SuccessOrExit(error = mEncoder.WriteUint16(aResult->mPanId)); |
| 3321 | SuccessOrExit(error = mEncoder.WriteUint8(aResult->mLqi)); |
| 3322 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 3323 | |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3324 | SuccessOrExit(error = mEncoder.OpenStruct()); // "net-layer data" |
| 3325 | SuccessOrExit(error = mEncoder.WriteUintPacked(SPINEL_PROTOCOL_TYPE_THREAD)); // type |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3326 | SuccessOrExit(error = mEncoder.WriteUint8(flags)); |
| 3327 | SuccessOrExit(error = mEncoder.WriteUtf8(aResult->mNetworkName.m8)); |
| 3328 | SuccessOrExit(error = mEncoder.WriteDataWithLen(aResult->mExtendedPanId.m8, OT_EXT_PAN_ID_SIZE)); |
| 3329 | SuccessOrExit(error = mEncoder.WriteDataWithLen(aResult->mSteeringData.m8, aResult->mSteeringData.mLength)); |
| 3330 | SuccessOrExit(error = mEncoder.CloseStruct()); |
| 3331 | |
| 3332 | SuccessOrExit(error = mEncoder.EndFrame()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3333 | } |
| 3334 | else |
| 3335 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3336 | // We are finished with the scan, send an unsolicited |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3337 | // scan state update. |
| 3338 | mChangedPropsSet.AddProperty(SPINEL_PROP_MAC_SCAN_STATE); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3339 | mUpdateChangedPropsTask.Post(); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3340 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3341 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3342 | exit: |
| 3343 | |
| 3344 | if (error != OT_ERROR_NONE) |
| 3345 | { |
| 3346 | // We ran out of buffer adding a scan result so remember to send |
| 3347 | // an async `LAST_STATUS(NOMEM)` when buffer space becomes |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3348 | // available. |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3349 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_NOMEM); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3350 | mUpdateChangedPropsTask.Post(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | void NcpBase::HandleEnergyScanResult_Jump(otEnergyScanResult *aResult, void *aContext) |
| 3355 | { |
| 3356 | static_cast<NcpBase *>(aContext)->HandleEnergyScanResult(aResult); |
| 3357 | } |
| 3358 | |
| 3359 | void NcpBase::HandleEnergyScanResult(otEnergyScanResult *aResult) |
| 3360 | { |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3361 | otError error = OT_ERROR_NONE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3362 | |
| 3363 | if (aResult) |
| 3364 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3365 | SuccessOrExit(error = mEncoder.BeginFrame(SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, |
| 3366 | SPINEL_CMD_PROP_VALUE_INSERTED, SPINEL_PROP_MAC_ENERGY_SCAN_RESULT)); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3367 | SuccessOrExit(error = mEncoder.WriteUint8(aResult->mChannel)); |
| 3368 | SuccessOrExit(error = mEncoder.WriteInt8(aResult->mMaxRssi)); |
| 3369 | SuccessOrExit(error = mEncoder.EndFrame()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3370 | } |
| 3371 | else |
| 3372 | { |
Abtin Keshavarzian | 92ba27a | 2017-10-13 09:18:37 -0700 | [diff] [blame] | 3373 | // We are finished with the scan, send an unsolicited |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3374 | // scan state update. |
| 3375 | mChangedPropsSet.AddProperty(SPINEL_PROP_MAC_SCAN_STATE); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3376 | mUpdateChangedPropsTask.Post(); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3377 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3378 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3379 | exit: |
| 3380 | |
| 3381 | if (error != OT_ERROR_NONE) |
| 3382 | { |
| 3383 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_NOMEM); |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3384 | mUpdateChangedPropsTask.Post(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3385 | } |
| 3386 | } |
| 3387 | |
Jonathan Hui | 31ffd16 | 2019-07-16 12:01:36 -0700 | [diff] [blame] | 3388 | #if OPENTHREAD_CONFIG_JOINER_ENABLE |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 3389 | void NcpBase::HandleJoinerCallback_Jump(otError aError, void *aContext) |
| 3390 | { |
| 3391 | static_cast<NcpBase *>(aContext)->HandleJoinerCallback(aError); |
| 3392 | } |
| 3393 | |
| 3394 | void NcpBase::HandleJoinerCallback(otError aError) |
| 3395 | { |
| 3396 | switch (aError) |
| 3397 | { |
| 3398 | case OT_ERROR_NONE: |
| 3399 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_JOIN_SUCCESS); |
| 3400 | break; |
| 3401 | case OT_ERROR_SECURITY: |
| 3402 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_JOIN_SECURITY); |
| 3403 | break; |
| 3404 | case OT_ERROR_NOT_FOUND: |
| 3405 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_JOIN_NO_PEERS); |
| 3406 | break; |
| 3407 | case OT_ERROR_RESPONSE_TIMEOUT: |
| 3408 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_JOIN_RSP_TIMEOUT); |
| 3409 | break; |
| 3410 | default: |
| 3411 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_JOIN_FAILURE); |
| 3412 | break; |
| 3413 | } |
| 3414 | |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3415 | mUpdateChangedPropsTask.Post(); |
rongli | 90ee0a4 | 2018-08-09 02:18:53 +0800 | [diff] [blame] | 3416 | } |
| 3417 | #endif |
| 3418 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3419 | // ---------------------------------------------------------------------------- |
| 3420 | // MARK: Outbound Datagram Handling |
| 3421 | // ---------------------------------------------------------------------------- |
| 3422 | |
| 3423 | void NcpBase::HandleDatagramFromStack(otMessage *aMessage, void *aContext) |
| 3424 | { |
| 3425 | static_cast<NcpBase *>(aContext)->HandleDatagramFromStack(aMessage); |
| 3426 | } |
| 3427 | |
| 3428 | void NcpBase::HandleDatagramFromStack(otMessage *aMessage) |
| 3429 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3430 | VerifyOrExit(aMessage != nullptr, OT_NOOP); |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3431 | |
Duda, Lukasz | 9aa7340 | 2020-05-08 22:46:00 +0200 | [diff] [blame] | 3432 | // Do not forward frames larger than SPINEL payload size. |
| 3433 | VerifyOrExit(otMessageGetLength(aMessage) <= SPINEL_FRAME_MAX_COMMAND_PAYLOAD_SIZE, otMessageFree(aMessage)); |
| 3434 | |
Jonathan Hui | 7540758 | 2020-05-08 20:43:40 -0700 | [diff] [blame] | 3435 | otMessageQueueEnqueue(&mMessageQueue, aMessage); |
Abtin Keshavarzian | 9ce57b2 | 2017-12-05 18:33:53 -0800 | [diff] [blame] | 3436 | |
| 3437 | // If there is no queued spinel command response, try to write/send |
| 3438 | // the datagram message immediately. If there is a queued response |
| 3439 | // or if currently out of buffer space, the IPv6 datagram message |
| 3440 | // will be sent from `HandleFrameRemovedFromNcpBuffer()` when buffer |
| 3441 | // space becomes available and after any pending spinel command |
| 3442 | // response. |
| 3443 | |
| 3444 | if (IsResponseQueueEmpty()) |
| 3445 | { |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 3446 | IgnoreError(SendQueuedDatagramMessages()); |
Abtin Keshavarzian | 9ce57b2 | 2017-12-05 18:33:53 -0800 | [diff] [blame] | 3447 | } |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3448 | |
| 3449 | exit: |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3450 | return; |
| 3451 | } |
| 3452 | |
| 3453 | otError NcpBase::SendDatagramMessage(otMessage *aMessage) |
| 3454 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3455 | otError error = OT_ERROR_NONE; |
| 3456 | uint8_t header = SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0; |
| 3457 | bool isSecure = otMessageIsLinkSecurityEnabled(aMessage); |
| 3458 | spinel_prop_key_t propKey = isSecure ? SPINEL_PROP_STREAM_NET : SPINEL_PROP_STREAM_NET_INSECURE; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3459 | |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3460 | SuccessOrExit(error = mEncoder.BeginFrame(header, SPINEL_CMD_PROP_VALUE_IS, propKey)); |
| 3461 | SuccessOrExit(error = mEncoder.WriteUint16(otMessageGetLength(aMessage))); |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3462 | SuccessOrExit(error = mEncoder.WriteMessage(aMessage)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3463 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3464 | // Append any metadata (rssi, lqi, channel, etc) here! |
| 3465 | |
Abtin Keshavarzian | 192df6c | 2017-09-27 23:04:53 -0700 | [diff] [blame] | 3466 | SuccessOrExit(error = mEncoder.EndFrame()); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3467 | |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3468 | if (isSecure) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3469 | { |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3470 | mOutboundSecureIpFrameCounter++; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3471 | } |
| 3472 | else |
| 3473 | { |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3474 | mOutboundInsecureIpFrameCounter++; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3475 | } |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3476 | |
| 3477 | exit: |
| 3478 | return error; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3479 | } |
| 3480 | |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3481 | otError NcpBase::SendQueuedDatagramMessages(void) |
| 3482 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3483 | otError error = OT_ERROR_NONE; |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3484 | otMessage *message; |
| 3485 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3486 | while ((message = otMessageQueueGetHead(&mMessageQueue)) != nullptr) |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3487 | { |
| 3488 | // Since an `otMessage` instance can be in one queue at a time, |
| 3489 | // it is first dequeued from `mMessageQueue` before attempting |
| 3490 | // to include it in a spinel frame by calling `SendDatagramMessage()` |
| 3491 | // If forming of the spinel frame fails, the message is enqueued |
| 3492 | // back at the front of `mMessageQueue`. |
| 3493 | |
Jonathan Hui | cf57110 | 2020-05-08 21:42:11 -0700 | [diff] [blame] | 3494 | otMessageQueueDequeue(&mMessageQueue, message); |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3495 | |
| 3496 | error = SendDatagramMessage(message); |
| 3497 | |
| 3498 | if (error != OT_ERROR_NONE) |
| 3499 | { |
Jonathan Hui | 7540758 | 2020-05-08 20:43:40 -0700 | [diff] [blame] | 3500 | otMessageQueueEnqueueAtHead(&mMessageQueue, message); |
Abtin Keshavarzian | 54fa0f1 | 2017-10-13 09:29:18 -0700 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | SuccessOrExit(error); |
| 3504 | } |
| 3505 | |
| 3506 | exit: |
| 3507 | return error; |
| 3508 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3509 | |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3510 | #if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3511 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_THREAD_UDP_FORWARD_STREAM>(void) |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3512 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3513 | const uint8_t * framePtr = nullptr; |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3514 | uint16_t frameLen = 0; |
| 3515 | const otIp6Address *peerAddr; |
| 3516 | uint16_t peerPort; |
| 3517 | uint16_t sockPort; |
| 3518 | otMessage * message; |
Zhanglong Xia | f890959 | 2018-11-06 04:48:39 +0800 | [diff] [blame] | 3519 | otError error = OT_ERROR_NONE; |
| 3520 | otMessageSettings msgSettings = {false, OT_MESSAGE_PRIORITY_NORMAL}; |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3521 | |
Zhanglong Xia | f890959 | 2018-11-06 04:48:39 +0800 | [diff] [blame] | 3522 | message = otIp6NewMessage(mInstance, &msgSettings); |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3523 | VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3524 | |
| 3525 | SuccessOrExit(error = mDecoder.ReadDataWithLen(framePtr, frameLen)); |
| 3526 | SuccessOrExit(error = mDecoder.ReadUint16(peerPort)); |
| 3527 | SuccessOrExit(error = mDecoder.ReadIp6Address(peerAddr)); |
| 3528 | SuccessOrExit(error = mDecoder.ReadUint16(sockPort)); |
| 3529 | |
| 3530 | SuccessOrExit(error = otMessageAppend(message, framePtr, static_cast<uint16_t>(frameLen))); |
| 3531 | |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3532 | otUdpForwardReceive(mInstance, message, peerPort, peerAddr, sockPort); |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3533 | |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3534 | // `otUdpForwardReceive()` takes ownership of `message` (in both success |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3535 | // or failure cases). `message` is set to nullptr so it is not freed at |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3536 | // exit. |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3537 | message = nullptr; |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3538 | |
| 3539 | exit: |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3540 | if (message != nullptr) |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3541 | { |
| 3542 | otMessageFree(message); |
| 3543 | } |
| 3544 | |
| 3545 | return error; |
| 3546 | } |
| 3547 | |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3548 | void NcpBase::HandleUdpForwardStream(otMessage * aMessage, |
| 3549 | uint16_t aPeerPort, |
| 3550 | otIp6Address *aPeerAddr, |
| 3551 | uint16_t aSockPort, |
| 3552 | void * aContext) |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3553 | { |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3554 | static_cast<NcpBase *>(aContext)->HandleUdpForwardStream(aMessage, aPeerPort, *aPeerAddr, aSockPort); |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3555 | } |
| 3556 | |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3557 | void NcpBase::HandleUdpForwardStream(otMessage *aMessage, uint16_t aPeerPort, otIp6Address &aPeerAddr, uint16_t aPort) |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3558 | { |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3559 | uint16_t length = otMessageGetLength(aMessage); |
| 3560 | uint8_t header = SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0; |
| 3561 | |
Yakun Xu | b9b57cc | 2018-11-06 12:48:08 +0800 | [diff] [blame] | 3562 | SuccessOrExit(mEncoder.BeginFrame(header, SPINEL_CMD_PROP_VALUE_IS, SPINEL_PROP_THREAD_UDP_FORWARD_STREAM)); |
Kamil Sroka | d7f8a5b | 2018-07-13 18:35:47 +0200 | [diff] [blame] | 3563 | SuccessOrExit(mEncoder.WriteUint16(length)); |
| 3564 | SuccessOrExit(mEncoder.WriteMessage(aMessage)); |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3565 | |
Kamil Sroka | d7f8a5b | 2018-07-13 18:35:47 +0200 | [diff] [blame] | 3566 | SuccessOrExit(mEncoder.WriteUint16(aPeerPort)); |
| 3567 | SuccessOrExit(mEncoder.WriteIp6Address(aPeerAddr)); |
| 3568 | SuccessOrExit(mEncoder.WriteUint16(aPort)); |
| 3569 | SuccessOrExit(mEncoder.EndFrame()); |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3570 | |
| 3571 | // The `aMessage` is owned by the outbound frame and NCP buffer |
| 3572 | // after frame was finished/ended successfully. It will be freed |
| 3573 | // when the frame is successfully sent and removed. |
| 3574 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3575 | aMessage = nullptr; |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3576 | |
| 3577 | exit: |
| 3578 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3579 | if (aMessage != nullptr) |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3580 | { |
| 3581 | otMessageFree(aMessage); |
| 3582 | } |
| 3583 | } |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3584 | #endif // OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE |
Yakun Xu | 96a7e2d | 2018-07-10 04:25:23 +0800 | [diff] [blame] | 3585 | |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3586 | // ---------------------------------------------------------------------------- |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3587 | // MARK: Pcap frame handling |
| 3588 | // ---------------------------------------------------------------------------- |
| 3589 | |
Yakun Xu | 504331e | 2019-01-29 04:31:15 +0800 | [diff] [blame] | 3590 | void NcpBase::HandlePcapFrame(const otRadioFrame *aFrame, bool aIsTx, void *aContext) |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3591 | { |
Yakun Xu | 504331e | 2019-01-29 04:31:15 +0800 | [diff] [blame] | 3592 | static_cast<NcpBase *>(aContext)->HandlePcapFrame(aFrame, aIsTx); |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3593 | } |
| 3594 | |
Yakun Xu | 504331e | 2019-01-29 04:31:15 +0800 | [diff] [blame] | 3595 | void NcpBase::HandlePcapFrame(const otRadioFrame *aFrame, bool aIsTx) |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3596 | { |
| 3597 | uint16_t flags = 0; |
| 3598 | uint8_t header = SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0; |
| 3599 | |
Jonathan Hui | a459223 | 2020-04-08 15:22:25 -0700 | [diff] [blame] | 3600 | VerifyOrExit(mPcapEnabled, OT_NOOP); |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3601 | |
Yakun Xu | 504331e | 2019-01-29 04:31:15 +0800 | [diff] [blame] | 3602 | if (aIsTx) |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3603 | { |
| 3604 | flags |= SPINEL_MD_FLAG_TX; |
| 3605 | } |
| 3606 | |
| 3607 | SuccessOrExit(mEncoder.BeginFrame(header, SPINEL_CMD_PROP_VALUE_IS, SPINEL_PROP_STREAM_RAW)); |
| 3608 | SuccessOrExit(mEncoder.WriteUint16(aFrame->mLength)); |
| 3609 | |
| 3610 | SuccessOrExit(mEncoder.WriteData(aFrame->mPsdu, aFrame->mLength)); |
| 3611 | |
| 3612 | // Append metadata (rssi, etc) |
| 3613 | SuccessOrExit(mEncoder.WriteInt8(aFrame->mInfo.mRxInfo.mRssi)); // RSSI |
| 3614 | SuccessOrExit(mEncoder.WriteInt8(-128)); // Noise floor (Currently unused) |
| 3615 | SuccessOrExit(mEncoder.WriteUint16(flags)); // Flags |
| 3616 | |
| 3617 | SuccessOrExit(mEncoder.OpenStruct()); // PHY-data |
| 3618 | // Empty for now |
| 3619 | SuccessOrExit(mEncoder.CloseStruct()); |
| 3620 | |
| 3621 | SuccessOrExit(mEncoder.OpenStruct()); // Vendor-data |
| 3622 | // Empty for now |
| 3623 | SuccessOrExit(mEncoder.CloseStruct()); |
| 3624 | |
| 3625 | SuccessOrExit(mEncoder.EndFrame()); |
| 3626 | |
| 3627 | exit: |
| 3628 | return; |
| 3629 | } |
| 3630 | |
| 3631 | template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_PHY_PCAP_ENABLED>(void) |
| 3632 | { |
| 3633 | return mEncoder.WriteBool(mPcapEnabled); |
| 3634 | } |
| 3635 | |
| 3636 | template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_PHY_PCAP_ENABLED>(void) |
| 3637 | { |
| 3638 | otError error = OT_ERROR_NONE; |
| 3639 | bool enabled; |
| 3640 | |
| 3641 | SuccessOrExit(error = mDecoder.ReadBool(enabled)); |
Jonathan Hui | a459223 | 2020-04-08 15:22:25 -0700 | [diff] [blame] | 3642 | VerifyOrExit(enabled != mPcapEnabled, OT_NOOP); |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3643 | |
| 3644 | mPcapEnabled = enabled; |
| 3645 | |
| 3646 | if (mPcapEnabled) |
| 3647 | { |
| 3648 | otLinkSetPcapCallback(mInstance, &NcpBase::HandlePcapFrame, static_cast<void *>(this)); |
| 3649 | } |
| 3650 | else |
| 3651 | { |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3652 | otLinkSetPcapCallback(mInstance, nullptr, nullptr); |
Abtin Keshavarzian | 765c45c | 2018-11-09 08:59:20 -0800 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | exit: |
| 3656 | return error; |
| 3657 | } |
| 3658 | |
| 3659 | // ---------------------------------------------------------------------------- |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3660 | // MARK: Property/Status Changed |
| 3661 | // ---------------------------------------------------------------------------- |
| 3662 | |
Abtin Keshavarzian | 9ce38e0 | 2018-06-22 15:09:18 -0700 | [diff] [blame] | 3663 | void NcpBase::HandleStateChanged(otChangedFlags aFlags, void *aContext) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3664 | { |
| 3665 | NcpBase *ncp = static_cast<NcpBase *>(aContext); |
| 3666 | |
| 3667 | ncp->mThreadChangedFlags |= aFlags; |
Abtin Keshavarzian | a1adaac | 2020-05-11 10:24:58 -0700 | [diff] [blame] | 3668 | ncp->mUpdateChangedPropsTask.Post(); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | void NcpBase::ProcessThreadChangedFlags(void) |
| 3672 | { |
| 3673 | static const struct |
| 3674 | { |
Abtin Keshavarzian | 9ce38e0 | 2018-06-22 15:09:18 -0700 | [diff] [blame] | 3675 | otChangedFlags mThreadFlag; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3676 | spinel_prop_key_t mPropKey; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3677 | } kFlags[] = { |
| 3678 | {OT_CHANGED_IP6_ADDRESS_ADDED, SPINEL_PROP_IPV6_ADDRESS_TABLE}, |
| 3679 | {OT_CHANGED_IP6_ADDRESS_REMOVED, SPINEL_PROP_IPV6_ADDRESS_TABLE}, |
| 3680 | {OT_CHANGED_THREAD_ROLE, SPINEL_PROP_NET_ROLE}, |
| 3681 | {OT_CHANGED_THREAD_LL_ADDR, SPINEL_PROP_IPV6_LL_ADDR}, |
| 3682 | {OT_CHANGED_THREAD_ML_ADDR, SPINEL_PROP_IPV6_ML_ADDR}, |
| 3683 | {OT_CHANGED_THREAD_PARTITION_ID, SPINEL_PROP_NET_PARTITION_ID}, |
| 3684 | {OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER, SPINEL_PROP_NET_KEY_SEQUENCE_COUNTER}, |
| 3685 | {OT_CHANGED_THREAD_NETDATA, SPINEL_PROP_THREAD_LEADER_NETWORK_DATA}, |
| 3686 | {OT_CHANGED_THREAD_CHILD_ADDED, SPINEL_PROP_THREAD_CHILD_TABLE}, |
| 3687 | {OT_CHANGED_THREAD_CHILD_REMOVED, SPINEL_PROP_THREAD_CHILD_TABLE}, |
Jonathan Hui | 98fb9df | 2019-10-09 15:01:32 -0700 | [diff] [blame] | 3688 | {OT_CHANGED_IP6_MULTICAST_SUBSCRIBED, SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE}, |
| 3689 | {OT_CHANGED_IP6_MULTICAST_UNSUBSCRIBED, SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE}, |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3690 | {OT_CHANGED_THREAD_CHANNEL, SPINEL_PROP_PHY_CHAN}, |
| 3691 | {OT_CHANGED_THREAD_PANID, SPINEL_PROP_MAC_15_4_PANID}, |
| 3692 | {OT_CHANGED_THREAD_NETWORK_NAME, SPINEL_PROP_NET_NETWORK_NAME}, |
| 3693 | {OT_CHANGED_THREAD_EXT_PANID, SPINEL_PROP_NET_XPANID}, |
Simon Lin | b5b97ae | 2020-03-10 23:55:25 +0800 | [diff] [blame] | 3694 | {OT_CHANGED_THREAD_RLOC_ADDED, SPINEL_PROP_IPV6_ADDRESS_TABLE}, |
| 3695 | {OT_CHANGED_THREAD_RLOC_REMOVED, SPINEL_PROP_IPV6_ADDRESS_TABLE}, |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3696 | {OT_CHANGED_MASTER_KEY, SPINEL_PROP_NET_MASTER_KEY}, |
| 3697 | {OT_CHANGED_PSKC, SPINEL_PROP_NET_PSKC}, |
| 3698 | {OT_CHANGED_CHANNEL_MANAGER_NEW_CHANNEL, SPINEL_PROP_CHANNEL_MANAGER_NEW_CHANNEL}, |
Abtin Keshavarzian | 31698ec | 2018-07-02 08:43:45 -0700 | [diff] [blame] | 3699 | {OT_CHANGED_SUPPORTED_CHANNEL_MASK, SPINEL_PROP_PHY_CHAN_SUPPORTED}, |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3700 | }; |
| 3701 | |
Jonathan Hui | a459223 | 2020-04-08 15:22:25 -0700 | [diff] [blame] | 3702 | VerifyOrExit(mThreadChangedFlags != 0, OT_NOOP); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3703 | |
| 3704 | // If thread role has changed, check for possible "join" error. |
| 3705 | |
| 3706 | if ((mThreadChangedFlags & OT_CHANGED_THREAD_ROLE) != 0) |
| 3707 | { |
| 3708 | if (mRequireJoinExistingNetwork) |
| 3709 | { |
| 3710 | switch (otThreadGetDeviceRole(mInstance)) |
| 3711 | { |
| 3712 | case OT_DEVICE_ROLE_DETACHED: |
| 3713 | case OT_DEVICE_ROLE_DISABLED: |
| 3714 | break; |
| 3715 | |
| 3716 | default: |
| 3717 | mRequireJoinExistingNetwork = false; |
| 3718 | mChangedPropsSet.AddProperty(SPINEL_PROP_NET_REQUIRE_JOIN_EXISTING); |
| 3719 | break; |
| 3720 | } |
| 3721 | |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3722 | if ((otThreadGetDeviceRole(mInstance) == OT_DEVICE_ROLE_LEADER) && otThreadIsSingleton(mInstance) |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3723 | #if OPENTHREAD_CONFIG_LEGACY_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3724 | && !mLegacyNodeDidJoin |
| 3725 | #endif |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3726 | ) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3727 | { |
| 3728 | mThreadChangedFlags &= ~static_cast<uint32_t>(OT_CHANGED_THREAD_PARTITION_ID); |
kangping | 49f36f8 | 2020-05-07 02:17:58 +0800 | [diff] [blame] | 3729 | IgnoreError(otThreadSetEnabled(mInstance, false)); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3730 | |
| 3731 | mChangedPropsSet.AddProperty(SPINEL_PROP_NET_STACK_UP); |
| 3732 | mChangedPropsSet.AddLastStatus(SPINEL_STATUS_JOIN_FAILURE); |
| 3733 | } |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | // Convert OT_CHANGED flags to corresponding NCP property update. |
| 3738 | |
Simon Lin | 5e9b281 | 2020-07-22 00:41:40 +0800 | [diff] [blame] | 3739 | for (auto flag : kFlags) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3740 | { |
Simon Lin | 5e9b281 | 2020-07-22 00:41:40 +0800 | [diff] [blame] | 3741 | uint32_t threadFlag = flag.mThreadFlag; |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3742 | |
| 3743 | if (mThreadChangedFlags & threadFlag) |
| 3744 | { |
Simon Lin | 5e9b281 | 2020-07-22 00:41:40 +0800 | [diff] [blame] | 3745 | spinel_prop_key_t propKey = flag.mPropKey; |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3746 | bool shouldAddProperty = true; |
Abtin Keshavarzian | 7392200 | 2017-10-31 14:15:04 -0700 | [diff] [blame] | 3747 | |
| 3748 | // Child table changes are reported using the `HandleChildAdded()` and |
| 3749 | // `HandleChildRemoved()` callbacks emitting spinel `VALUE_INSERTED` and |
| 3750 | // `VALUE_REMOVED` async spinel frames. If the spinel frames could not be |
| 3751 | // added (e.g., out of NCP buffer) from the above callbacks, the flag |
| 3752 | // `mShouldEmitChildTableUpdate` is set to `true` so that the entire |
| 3753 | // child table is emitted as an unsolicited `VALUE_IS` update. |
| 3754 | |
| 3755 | if (propKey == SPINEL_PROP_THREAD_CHILD_TABLE) |
| 3756 | { |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3757 | shouldAddProperty = mShouldEmitChildTableUpdate; |
Abtin Keshavarzian | 7392200 | 2017-10-31 14:15:04 -0700 | [diff] [blame] | 3758 | mShouldEmitChildTableUpdate = false; |
| 3759 | } |
| 3760 | |
| 3761 | if (shouldAddProperty) |
| 3762 | { |
| 3763 | mChangedPropsSet.AddProperty(propKey); |
| 3764 | } |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3765 | |
| 3766 | if (threadFlag == OT_CHANGED_THREAD_NETDATA) |
| 3767 | { |
| 3768 | mChangedPropsSet.AddProperty(SPINEL_PROP_THREAD_ON_MESH_NETS); |
| 3769 | mChangedPropsSet.AddProperty(SPINEL_PROP_THREAD_OFF_MESH_ROUTES); |
| 3770 | } |
| 3771 | |
| 3772 | mThreadChangedFlags &= ~threadFlag; |
Jonathan Hui | a459223 | 2020-04-08 15:22:25 -0700 | [diff] [blame] | 3773 | VerifyOrExit(mThreadChangedFlags != 0, OT_NOOP); |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | // Clear any remaining ThreadFlag that has no matching |
Simon Lin | b5b97ae | 2020-03-10 23:55:25 +0800 | [diff] [blame] | 3778 | // NCP property update (e.g., OT_CHANGED_SECURITY_POLICY) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3779 | |
| 3780 | mThreadChangedFlags = 0; |
| 3781 | |
| 3782 | exit: |
| 3783 | return; |
| 3784 | } |
| 3785 | |
Yakun Xu | b833373 | 2018-06-21 04:15:13 +0800 | [diff] [blame] | 3786 | } // namespace Ncp |
| 3787 | } // namespace ot |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3788 | |
| 3789 | // ---------------------------------------------------------------------------- |
| 3790 | // MARK: Legacy network APIs |
| 3791 | // ---------------------------------------------------------------------------- |
| 3792 | |
| 3793 | void otNcpRegisterLegacyHandlers(const otNcpLegacyHandlers *aHandlers) |
| 3794 | { |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3795 | #if OPENTHREAD_CONFIG_LEGACY_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3796 | ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance(); |
| 3797 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3798 | if (ncp != nullptr) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3799 | { |
| 3800 | ncp->RegisterLegacyHandlers(aHandlers); |
| 3801 | } |
| 3802 | |
| 3803 | #else |
| 3804 | OT_UNUSED_VARIABLE(aHandlers); |
| 3805 | #endif |
| 3806 | } |
| 3807 | |
| 3808 | void otNcpHandleDidReceiveNewLegacyUlaPrefix(const uint8_t *aUlaPrefix) |
| 3809 | { |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3810 | #if OPENTHREAD_CONFIG_LEGACY_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3811 | ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance(); |
| 3812 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3813 | if (ncp != nullptr) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3814 | { |
| 3815 | ncp->HandleDidReceiveNewLegacyUlaPrefix(aUlaPrefix); |
| 3816 | } |
| 3817 | |
| 3818 | #else |
| 3819 | OT_UNUSED_VARIABLE(aUlaPrefix); |
| 3820 | #endif |
| 3821 | } |
| 3822 | |
| 3823 | void otNcpHandleLegacyNodeDidJoin(const otExtAddress *aExtAddr) |
| 3824 | { |
Jonathan Hui | 02da206 | 2019-07-16 12:18:34 -0700 | [diff] [blame] | 3825 | #if OPENTHREAD_CONFIG_LEGACY_ENABLE |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3826 | ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance(); |
| 3827 | |
Jonathan Hui | 1326d64 | 2020-06-17 22:44:54 -0700 | [diff] [blame] | 3828 | if (ncp != nullptr) |
Buke Po | 818998e | 2017-08-04 01:19:07 +0800 | [diff] [blame] | 3829 | { |
| 3830 | ncp->HandleLegacyNodeDidJoin(aExtAddr); |
| 3831 | } |
| 3832 | |
| 3833 | #else |
| 3834 | OT_UNUSED_VARIABLE(aExtAddr); |
| 3835 | #endif |
| 3836 | } |
| 3837 | |
| 3838 | #endif // OPENTHREAD_MTD || OPENTHREAD_FTD |