1*cfb92d14SAndroid Build Coastguard Worker /* 2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2017, The OpenThread Authors. 3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved. 4*cfb92d14SAndroid Build Coastguard Worker * 5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met: 7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright 8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright 10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the 11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution. 12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the 13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products 14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission. 15*cfb92d14SAndroid Build Coastguard Worker * 16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17*cfb92d14SAndroid Build Coastguard Worker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18*cfb92d14SAndroid Build Coastguard Worker * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19*cfb92d14SAndroid Build Coastguard Worker * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 20*cfb92d14SAndroid Build Coastguard Worker * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21*cfb92d14SAndroid Build Coastguard Worker * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22*cfb92d14SAndroid Build Coastguard Worker * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23*cfb92d14SAndroid Build Coastguard Worker * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24*cfb92d14SAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25*cfb92d14SAndroid Build Coastguard Worker * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26*cfb92d14SAndroid Build Coastguard Worker */ 27*cfb92d14SAndroid Build Coastguard Worker 28*cfb92d14SAndroid Build Coastguard Worker /** 29*cfb92d14SAndroid Build Coastguard Worker * @file 30*cfb92d14SAndroid Build Coastguard Worker * This file contains the definitions of a spinel decoder. 31*cfb92d14SAndroid Build Coastguard Worker */ 32*cfb92d14SAndroid Build Coastguard Worker 33*cfb92d14SAndroid Build Coastguard Worker #ifndef SPINEL_DECODER_HPP_ 34*cfb92d14SAndroid Build Coastguard Worker #define SPINEL_DECODER_HPP_ 35*cfb92d14SAndroid Build Coastguard Worker 36*cfb92d14SAndroid Build Coastguard Worker #include "openthread-spinel-config.h" 37*cfb92d14SAndroid Build Coastguard Worker 38*cfb92d14SAndroid Build Coastguard Worker #include <openthread/ip6.h> 39*cfb92d14SAndroid Build Coastguard Worker #include <openthread/ncp.h> 40*cfb92d14SAndroid Build Coastguard Worker 41*cfb92d14SAndroid Build Coastguard Worker #include "spinel.h" 42*cfb92d14SAndroid Build Coastguard Worker 43*cfb92d14SAndroid Build Coastguard Worker namespace ot { 44*cfb92d14SAndroid Build Coastguard Worker namespace Spinel { 45*cfb92d14SAndroid Build Coastguard Worker 46*cfb92d14SAndroid Build Coastguard Worker /** 47*cfb92d14SAndroid Build Coastguard Worker * Defines a spinel decoder. 48*cfb92d14SAndroid Build Coastguard Worker * 49*cfb92d14SAndroid Build Coastguard Worker */ 50*cfb92d14SAndroid Build Coastguard Worker class Decoder 51*cfb92d14SAndroid Build Coastguard Worker { 52*cfb92d14SAndroid Build Coastguard Worker public: 53*cfb92d14SAndroid Build Coastguard Worker enum 54*cfb92d14SAndroid Build Coastguard Worker { 55*cfb92d14SAndroid Build Coastguard Worker kMaxNestedStructs = 4, ///< Maximum number of nested structs. 56*cfb92d14SAndroid Build Coastguard Worker }; 57*cfb92d14SAndroid Build Coastguard Worker 58*cfb92d14SAndroid Build Coastguard Worker /** 59*cfb92d14SAndroid Build Coastguard Worker * Initializes a `Decoder` object. 60*cfb92d14SAndroid Build Coastguard Worker * 61*cfb92d14SAndroid Build Coastguard Worker */ 62*cfb92d14SAndroid Build Coastguard Worker Decoder(void); 63*cfb92d14SAndroid Build Coastguard Worker 64*cfb92d14SAndroid Build Coastguard Worker /** 65*cfb92d14SAndroid Build Coastguard Worker * Initializes the decoder to start decoding a new frame. 66*cfb92d14SAndroid Build Coastguard Worker * 67*cfb92d14SAndroid Build Coastguard Worker * It sets the read position to the start of the frame and also erases/voids any saved positions (see 68*cfb92d14SAndroid Build Coastguard Worker * `SavePosition()` and `ResetToSaved()` methods). 69*cfb92d14SAndroid Build Coastguard Worker * 70*cfb92d14SAndroid Build Coastguard Worker * @param[in] aFrame Pointer to the buffer containing the frame to be decoded. 71*cfb92d14SAndroid Build Coastguard Worker * @param[in] aLength Length (number of bytes) of the frame. 72*cfb92d14SAndroid Build Coastguard Worker * 73*cfb92d14SAndroid Build Coastguard Worker */ 74*cfb92d14SAndroid Build Coastguard Worker void Init(const uint8_t *aFrame, uint16_t aLength); 75*cfb92d14SAndroid Build Coastguard Worker 76*cfb92d14SAndroid Build Coastguard Worker /** 77*cfb92d14SAndroid Build Coastguard Worker * Returns the pointer to the start of the frame. 78*cfb92d14SAndroid Build Coastguard Worker * 79*cfb92d14SAndroid Build Coastguard Worker * @returns A pointer to buffer containing current frame being decoded. 80*cfb92d14SAndroid Build Coastguard Worker * 81*cfb92d14SAndroid Build Coastguard Worker */ GetFrame(void) const82*cfb92d14SAndroid Build Coastguard Worker const uint8_t *GetFrame(void) const { return mFrame; } 83*cfb92d14SAndroid Build Coastguard Worker 84*cfb92d14SAndroid Build Coastguard Worker /** 85*cfb92d14SAndroid Build Coastguard Worker * Returns the total length of current frame being decoded. 86*cfb92d14SAndroid Build Coastguard Worker * 87*cfb92d14SAndroid Build Coastguard Worker * @returns The length of current frame being decoded. 88*cfb92d14SAndroid Build Coastguard Worker * 89*cfb92d14SAndroid Build Coastguard Worker */ GetLength(void) const90*cfb92d14SAndroid Build Coastguard Worker uint16_t GetLength(void) const { return mLength; } 91*cfb92d14SAndroid Build Coastguard Worker 92*cfb92d14SAndroid Build Coastguard Worker /** 93*cfb92d14SAndroid Build Coastguard Worker * Returns the number of bytes that are already read/decoded from the frame. 94*cfb92d14SAndroid Build Coastguard Worker * 95*cfb92d14SAndroid Build Coastguard Worker * @returns The number of bytes already read from frame. 96*cfb92d14SAndroid Build Coastguard Worker * 97*cfb92d14SAndroid Build Coastguard Worker */ GetReadLength(void) const98*cfb92d14SAndroid Build Coastguard Worker uint16_t GetReadLength(void) const { return mIndex; } 99*cfb92d14SAndroid Build Coastguard Worker 100*cfb92d14SAndroid Build Coastguard Worker /** 101*cfb92d14SAndroid Build Coastguard Worker * Returns the number of remaining (not yet read/decoded) bytes in the frame. 102*cfb92d14SAndroid Build Coastguard Worker * 103*cfb92d14SAndroid Build Coastguard Worker * @returns The number of remaining unread bytes in the frame. 104*cfb92d14SAndroid Build Coastguard Worker * 105*cfb92d14SAndroid Build Coastguard Worker */ GetRemainingLength(void) const106*cfb92d14SAndroid Build Coastguard Worker uint16_t GetRemainingLength(void) const { return mLength - mIndex; } 107*cfb92d14SAndroid Build Coastguard Worker 108*cfb92d14SAndroid Build Coastguard Worker /** 109*cfb92d14SAndroid Build Coastguard Worker * Indicates whether or not all the bytes in the frame are read. 110*cfb92d14SAndroid Build Coastguard Worker * 111*cfb92d14SAndroid Build Coastguard Worker * @returns TRUE if all the bytes in the buffer are read, FALSE otherwise. 112*cfb92d14SAndroid Build Coastguard Worker * 113*cfb92d14SAndroid Build Coastguard Worker */ IsAllRead(void) const114*cfb92d14SAndroid Build Coastguard Worker bool IsAllRead(void) const { return (mIndex == mLength); } 115*cfb92d14SAndroid Build Coastguard Worker 116*cfb92d14SAndroid Build Coastguard Worker /** 117*cfb92d14SAndroid Build Coastguard Worker * Resets the read position to beginning of frame. It will also void/erase any previously saved 118*cfb92d14SAndroid Build Coastguard Worker * position using `SavePosition()` method. 119*cfb92d14SAndroid Build Coastguard Worker * 120*cfb92d14SAndroid Build Coastguard Worker */ 121*cfb92d14SAndroid Build Coastguard Worker void Reset(void); 122*cfb92d14SAndroid Build Coastguard Worker 123*cfb92d14SAndroid Build Coastguard Worker /** 124*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads a boolean value form the frame. 125*cfb92d14SAndroid Build Coastguard Worker * 126*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 127*cfb92d14SAndroid Build Coastguard Worker * 128*cfb92d14SAndroid Build Coastguard Worker * @param[out] aBool Reference to variable to output the read value. 129*cfb92d14SAndroid Build Coastguard Worker * 130*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 131*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 132*cfb92d14SAndroid Build Coastguard Worker * 133*cfb92d14SAndroid Build Coastguard Worker */ 134*cfb92d14SAndroid Build Coastguard Worker otError ReadBool(bool &aBool); 135*cfb92d14SAndroid Build Coastguard Worker 136*cfb92d14SAndroid Build Coastguard Worker /** 137*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `int8_t` value form the frame. 138*cfb92d14SAndroid Build Coastguard Worker * 139*cfb92d14SAndroid Build Coastguard Worker * On success, the read position get updated. 140*cfb92d14SAndroid Build Coastguard Worker * 141*cfb92d14SAndroid Build Coastguard Worker * @param[out] aInt8 Reference to variable to output the read value. 142*cfb92d14SAndroid Build Coastguard Worker * 143*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 144*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 145*cfb92d14SAndroid Build Coastguard Worker * 146*cfb92d14SAndroid Build Coastguard Worker */ 147*cfb92d14SAndroid Build Coastguard Worker otError ReadInt8(int8_t &aInt8); 148*cfb92d14SAndroid Build Coastguard Worker 149*cfb92d14SAndroid Build Coastguard Worker /** 150*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `uint8_t` value form the frame. 151*cfb92d14SAndroid Build Coastguard Worker * 152*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 153*cfb92d14SAndroid Build Coastguard Worker * 154*cfb92d14SAndroid Build Coastguard Worker * @param[out] aUint8 Reference to variable to output the read value. 155*cfb92d14SAndroid Build Coastguard Worker * 156*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 157*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 158*cfb92d14SAndroid Build Coastguard Worker * 159*cfb92d14SAndroid Build Coastguard Worker */ 160*cfb92d14SAndroid Build Coastguard Worker otError ReadUint8(uint8_t &aUint8); 161*cfb92d14SAndroid Build Coastguard Worker 162*cfb92d14SAndroid Build Coastguard Worker /** 163*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `int16_t` value form the frame. 164*cfb92d14SAndroid Build Coastguard Worker * 165*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 166*cfb92d14SAndroid Build Coastguard Worker * 167*cfb92d14SAndroid Build Coastguard Worker * @param[out] aInt16 Reference to variable to output the read value. 168*cfb92d14SAndroid Build Coastguard Worker * 169*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 170*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 171*cfb92d14SAndroid Build Coastguard Worker * 172*cfb92d14SAndroid Build Coastguard Worker */ 173*cfb92d14SAndroid Build Coastguard Worker otError ReadInt16(int16_t &aInt16); 174*cfb92d14SAndroid Build Coastguard Worker 175*cfb92d14SAndroid Build Coastguard Worker /** 176*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `uint16_t` value form the frame. 177*cfb92d14SAndroid Build Coastguard Worker * 178*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 179*cfb92d14SAndroid Build Coastguard Worker * 180*cfb92d14SAndroid Build Coastguard Worker * @param[out] aUint16 Reference to variable to output the read value. 181*cfb92d14SAndroid Build Coastguard Worker * 182*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 183*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 184*cfb92d14SAndroid Build Coastguard Worker * 185*cfb92d14SAndroid Build Coastguard Worker */ 186*cfb92d14SAndroid Build Coastguard Worker otError ReadUint16(uint16_t &aUint16); 187*cfb92d14SAndroid Build Coastguard Worker 188*cfb92d14SAndroid Build Coastguard Worker /** 189*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `int32_t` value form the frame. 190*cfb92d14SAndroid Build Coastguard Worker * 191*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 192*cfb92d14SAndroid Build Coastguard Worker * 193*cfb92d14SAndroid Build Coastguard Worker * @param[out] aInt32 Reference to variable to output the read value. 194*cfb92d14SAndroid Build Coastguard Worker * 195*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 196*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 197*cfb92d14SAndroid Build Coastguard Worker * 198*cfb92d14SAndroid Build Coastguard Worker */ 199*cfb92d14SAndroid Build Coastguard Worker otError ReadInt32(int32_t &aInt32); 200*cfb92d14SAndroid Build Coastguard Worker 201*cfb92d14SAndroid Build Coastguard Worker /** 202*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `uint32_t` value form the frame. 203*cfb92d14SAndroid Build Coastguard Worker * 204*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 205*cfb92d14SAndroid Build Coastguard Worker * 206*cfb92d14SAndroid Build Coastguard Worker * @param[out] aUint32 Reference to variable to output the read value. 207*cfb92d14SAndroid Build Coastguard Worker * 208*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 209*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 210*cfb92d14SAndroid Build Coastguard Worker * 211*cfb92d14SAndroid Build Coastguard Worker */ 212*cfb92d14SAndroid Build Coastguard Worker otError ReadUint32(uint32_t &aUint32); 213*cfb92d14SAndroid Build Coastguard Worker 214*cfb92d14SAndroid Build Coastguard Worker /** 215*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `int64_t` value form the frame. 216*cfb92d14SAndroid Build Coastguard Worker * 217*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 218*cfb92d14SAndroid Build Coastguard Worker * 219*cfb92d14SAndroid Build Coastguard Worker * @param[out] aInt64 Reference to variable to output the read value. 220*cfb92d14SAndroid Build Coastguard Worker * 221*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 222*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 223*cfb92d14SAndroid Build Coastguard Worker * 224*cfb92d14SAndroid Build Coastguard Worker */ 225*cfb92d14SAndroid Build Coastguard Worker otError ReadInt64(int64_t &aInt64); 226*cfb92d14SAndroid Build Coastguard Worker 227*cfb92d14SAndroid Build Coastguard Worker /** 228*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an `uint64_t` value form the frame. 229*cfb92d14SAndroid Build Coastguard Worker * 230*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 231*cfb92d14SAndroid Build Coastguard Worker * 232*cfb92d14SAndroid Build Coastguard Worker * @param[out] aUint64 Reference to variable to output the read value. 233*cfb92d14SAndroid Build Coastguard Worker * 234*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 235*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 236*cfb92d14SAndroid Build Coastguard Worker * 237*cfb92d14SAndroid Build Coastguard Worker */ 238*cfb92d14SAndroid Build Coastguard Worker otError ReadUint64(uint64_t &aUint64); 239*cfb92d14SAndroid Build Coastguard Worker 240*cfb92d14SAndroid Build Coastguard Worker /** 241*cfb92d14SAndroid Build Coastguard Worker * Decodes (using spinel packed integer format) and reads an unsigned integer value form the frame. 242*cfb92d14SAndroid Build Coastguard Worker * 243*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 244*cfb92d14SAndroid Build Coastguard Worker * 245*cfb92d14SAndroid Build Coastguard Worker * @param[out] aUint Reference to variable to output the read value. 246*cfb92d14SAndroid Build Coastguard Worker * 247*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 248*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 249*cfb92d14SAndroid Build Coastguard Worker * 250*cfb92d14SAndroid Build Coastguard Worker */ 251*cfb92d14SAndroid Build Coastguard Worker otError ReadUintPacked(unsigned int &aUint); 252*cfb92d14SAndroid Build Coastguard Worker 253*cfb92d14SAndroid Build Coastguard Worker /** 254*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an IPv6 address form the frame. 255*cfb92d14SAndroid Build Coastguard Worker * 256*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 257*cfb92d14SAndroid Build Coastguard Worker * 258*cfb92d14SAndroid Build Coastguard Worker * @param[out] aIp6AddrPtr Reference to IPv6 address pointer to output the value (as `spinel_ipv6addr_t`). 259*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 260*cfb92d14SAndroid Build Coastguard Worker * 261*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 262*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 263*cfb92d14SAndroid Build Coastguard Worker * 264*cfb92d14SAndroid Build Coastguard Worker */ ReadIp6Address(const spinel_ipv6addr_t * & aIp6AddrPtr)265*cfb92d14SAndroid Build Coastguard Worker otError ReadIp6Address(const spinel_ipv6addr_t *&aIp6AddrPtr) 266*cfb92d14SAndroid Build Coastguard Worker { 267*cfb92d14SAndroid Build Coastguard Worker return ReadItem(reinterpret_cast<const uint8_t **>(&aIp6AddrPtr), sizeof(spinel_ipv6addr_t)); 268*cfb92d14SAndroid Build Coastguard Worker } 269*cfb92d14SAndroid Build Coastguard Worker 270*cfb92d14SAndroid Build Coastguard Worker /** 271*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an IPv6 address form the frame. 272*cfb92d14SAndroid Build Coastguard Worker * 273*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 274*cfb92d14SAndroid Build Coastguard Worker * 275*cfb92d14SAndroid Build Coastguard Worker * @param[out] aIp6AddrPtr Reference to IPv6 address pointer to output the value (as `otIp6Address`). 276*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 277*cfb92d14SAndroid Build Coastguard Worker * 278*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 279*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 280*cfb92d14SAndroid Build Coastguard Worker * 281*cfb92d14SAndroid Build Coastguard Worker */ ReadIp6Address(const otIp6Address * & aIp6AddrPtr)282*cfb92d14SAndroid Build Coastguard Worker otError ReadIp6Address(const otIp6Address *&aIp6AddrPtr) 283*cfb92d14SAndroid Build Coastguard Worker { 284*cfb92d14SAndroid Build Coastguard Worker return ReadItem(reinterpret_cast<const uint8_t **>(&aIp6AddrPtr), sizeof(spinel_ipv6addr_t)); 285*cfb92d14SAndroid Build Coastguard Worker } 286*cfb92d14SAndroid Build Coastguard Worker 287*cfb92d14SAndroid Build Coastguard Worker /** 288*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an IPv6 address form the frame. 289*cfb92d14SAndroid Build Coastguard Worker * 290*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 291*cfb92d14SAndroid Build Coastguard Worker * 292*cfb92d14SAndroid Build Coastguard Worker * @param[out] aIp6AddrBufPtr Reference to a buffer pointer to output the value. 293*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 294*cfb92d14SAndroid Build Coastguard Worker * 295*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 296*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 297*cfb92d14SAndroid Build Coastguard Worker * 298*cfb92d14SAndroid Build Coastguard Worker */ ReadIp6Address(const uint8_t * & aIp6AddrBufPtr)299*cfb92d14SAndroid Build Coastguard Worker otError ReadIp6Address(const uint8_t *&aIp6AddrBufPtr) 300*cfb92d14SAndroid Build Coastguard Worker { 301*cfb92d14SAndroid Build Coastguard Worker return ReadItem(&aIp6AddrBufPtr, sizeof(spinel_ipv6addr_t)); 302*cfb92d14SAndroid Build Coastguard Worker } 303*cfb92d14SAndroid Build Coastguard Worker 304*cfb92d14SAndroid Build Coastguard Worker /** 305*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an IPv6 address form the frame. 306*cfb92d14SAndroid Build Coastguard Worker * 307*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated and the IP address is copied into the given output variable. 308*cfb92d14SAndroid Build Coastguard Worker * 309*cfb92d14SAndroid Build Coastguard Worker * @param[out] aIp6Addr Reference to IPv6 address variable to output the value (as `spinel_ipv6addr_t`). 310*cfb92d14SAndroid Build Coastguard Worker * On success, the address is copied into the output variable. 311*cfb92d14SAndroid Build Coastguard Worker * 312*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 313*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 314*cfb92d14SAndroid Build Coastguard Worker * 315*cfb92d14SAndroid Build Coastguard Worker */ 316*cfb92d14SAndroid Build Coastguard Worker otError ReadIp6Address(spinel_ipv6addr_t &aIp6Addr); 317*cfb92d14SAndroid Build Coastguard Worker 318*cfb92d14SAndroid Build Coastguard Worker /** 319*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an IPv6 address form the frame. 320*cfb92d14SAndroid Build Coastguard Worker * 321*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated and the IP address is copied into the given output variable. 322*cfb92d14SAndroid Build Coastguard Worker * 323*cfb92d14SAndroid Build Coastguard Worker * @param[out] aIp6Addr Reference to IPv6 address variable to output the value (as `otIp6Address`). 324*cfb92d14SAndroid Build Coastguard Worker * On success, the address is copied into the output variable. 325*cfb92d14SAndroid Build Coastguard Worker * 326*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 327*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 328*cfb92d14SAndroid Build Coastguard Worker * 329*cfb92d14SAndroid Build Coastguard Worker */ 330*cfb92d14SAndroid Build Coastguard Worker otError ReadIp6Address(otIp6Address &aIp6Addr); 331*cfb92d14SAndroid Build Coastguard Worker 332*cfb92d14SAndroid Build Coastguard Worker /** 333*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI64 value form the frame. 334*cfb92d14SAndroid Build Coastguard Worker * 335*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 336*cfb92d14SAndroid Build Coastguard Worker * 337*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui64Ptr Reference to an EUI64 pointer to output the value (as `spinel_eui64_t`). 338*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 339*cfb92d14SAndroid Build Coastguard Worker * 340*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 341*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 342*cfb92d14SAndroid Build Coastguard Worker * 343*cfb92d14SAndroid Build Coastguard Worker */ ReadEui64(const spinel_eui64_t * & aEui64Ptr)344*cfb92d14SAndroid Build Coastguard Worker otError ReadEui64(const spinel_eui64_t *&aEui64Ptr) 345*cfb92d14SAndroid Build Coastguard Worker { 346*cfb92d14SAndroid Build Coastguard Worker return ReadItem(reinterpret_cast<const uint8_t **>(&aEui64Ptr), sizeof(spinel_eui64_t)); 347*cfb92d14SAndroid Build Coastguard Worker } 348*cfb92d14SAndroid Build Coastguard Worker 349*cfb92d14SAndroid Build Coastguard Worker /** 350*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI64 value form the frame. 351*cfb92d14SAndroid Build Coastguard Worker * 352*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 353*cfb92d14SAndroid Build Coastguard Worker * 354*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui64Ptr Reference to an EUI64 pointer to output the value (as `otExtAddress`). 355*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 356*cfb92d14SAndroid Build Coastguard Worker * 357*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 358*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 359*cfb92d14SAndroid Build Coastguard Worker * 360*cfb92d14SAndroid Build Coastguard Worker */ ReadEui64(const otExtAddress * & aEui64Ptr)361*cfb92d14SAndroid Build Coastguard Worker otError ReadEui64(const otExtAddress *&aEui64Ptr) 362*cfb92d14SAndroid Build Coastguard Worker { 363*cfb92d14SAndroid Build Coastguard Worker return ReadItem(reinterpret_cast<const uint8_t **>(&aEui64Ptr), sizeof(spinel_eui64_t)); 364*cfb92d14SAndroid Build Coastguard Worker } 365*cfb92d14SAndroid Build Coastguard Worker 366*cfb92d14SAndroid Build Coastguard Worker /** 367*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI64 value form the frame. 368*cfb92d14SAndroid Build Coastguard Worker * 369*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 370*cfb92d14SAndroid Build Coastguard Worker * 371*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui64BufPtr Reference to a buffer pointer to output the value. 372*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 373*cfb92d14SAndroid Build Coastguard Worker * 374*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 375*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 376*cfb92d14SAndroid Build Coastguard Worker * 377*cfb92d14SAndroid Build Coastguard Worker */ ReadEui64(const uint8_t * & aEui64BufPtr)378*cfb92d14SAndroid Build Coastguard Worker otError ReadEui64(const uint8_t *&aEui64BufPtr) { return ReadItem(&aEui64BufPtr, sizeof(spinel_eui64_t)); } 379*cfb92d14SAndroid Build Coastguard Worker 380*cfb92d14SAndroid Build Coastguard Worker /** 381*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI64 value form the frame. 382*cfb92d14SAndroid Build Coastguard Worker * 383*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated and the EUI64 value is copied into the given output variable. 384*cfb92d14SAndroid Build Coastguard Worker * 385*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui64 Reference to EUI64 variable to output the value (as `spinel_eui64_t`). 386*cfb92d14SAndroid Build Coastguard Worker * On success, the address is copied into the output variable. 387*cfb92d14SAndroid Build Coastguard Worker * 388*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 389*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 390*cfb92d14SAndroid Build Coastguard Worker * 391*cfb92d14SAndroid Build Coastguard Worker */ 392*cfb92d14SAndroid Build Coastguard Worker otError ReadEui64(spinel_eui64_t &aEui64); 393*cfb92d14SAndroid Build Coastguard Worker 394*cfb92d14SAndroid Build Coastguard Worker /** 395*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI64 value form the frame. 396*cfb92d14SAndroid Build Coastguard Worker * 397*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated and the EUI64 value is copied into the given output variable. 398*cfb92d14SAndroid Build Coastguard Worker * 399*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui64 Reference to EUI64 variable to output the value (as `otExtAddress`). 400*cfb92d14SAndroid Build Coastguard Worker * On success, the address is copied into the output variable. 401*cfb92d14SAndroid Build Coastguard Worker * 402*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 403*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 404*cfb92d14SAndroid Build Coastguard Worker * 405*cfb92d14SAndroid Build Coastguard Worker */ 406*cfb92d14SAndroid Build Coastguard Worker otError ReadEui64(otExtAddress &aEui64); 407*cfb92d14SAndroid Build Coastguard Worker 408*cfb92d14SAndroid Build Coastguard Worker /** 409*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI48 value form the frame. 410*cfb92d14SAndroid Build Coastguard Worker * 411*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 412*cfb92d14SAndroid Build Coastguard Worker * 413*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui48Ptr Reference to an EUI48 pointer to output the value (as `spinel_eui48_t`). 414*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 415*cfb92d14SAndroid Build Coastguard Worker * 416*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 417*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 418*cfb92d14SAndroid Build Coastguard Worker * 419*cfb92d14SAndroid Build Coastguard Worker */ ReadEui48(const spinel_eui48_t * & aEui48Ptr)420*cfb92d14SAndroid Build Coastguard Worker otError ReadEui48(const spinel_eui48_t *&aEui48Ptr) 421*cfb92d14SAndroid Build Coastguard Worker { 422*cfb92d14SAndroid Build Coastguard Worker return ReadItem(reinterpret_cast<const uint8_t **>(&aEui48Ptr), sizeof(spinel_eui48_t)); 423*cfb92d14SAndroid Build Coastguard Worker } 424*cfb92d14SAndroid Build Coastguard Worker 425*cfb92d14SAndroid Build Coastguard Worker /** 426*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI48 value form the frame. 427*cfb92d14SAndroid Build Coastguard Worker * 428*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 429*cfb92d14SAndroid Build Coastguard Worker * 430*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui48BufPtr Reference to a buffer pointer to output the value. 431*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 432*cfb92d14SAndroid Build Coastguard Worker * 433*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 434*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 435*cfb92d14SAndroid Build Coastguard Worker * 436*cfb92d14SAndroid Build Coastguard Worker */ ReadEui48(const uint8_t * & aEui48BufPtr)437*cfb92d14SAndroid Build Coastguard Worker otError ReadEui48(const uint8_t *&aEui48BufPtr) { return ReadItem(&aEui48BufPtr, sizeof(spinel_eui48_t)); } 438*cfb92d14SAndroid Build Coastguard Worker 439*cfb92d14SAndroid Build Coastguard Worker /** 440*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads an EUI48 value form the frame. 441*cfb92d14SAndroid Build Coastguard Worker * 442*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated and the EUI48 value is copied into the given output variable. 443*cfb92d14SAndroid Build Coastguard Worker * 444*cfb92d14SAndroid Build Coastguard Worker * @param[out] aEui48 Reference to EUI48 variable to output the value (as `spinel_eui48_t`). 445*cfb92d14SAndroid Build Coastguard Worker * On success, value is copied into the output variable. 446*cfb92d14SAndroid Build Coastguard Worker * 447*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 448*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 449*cfb92d14SAndroid Build Coastguard Worker * 450*cfb92d14SAndroid Build Coastguard Worker */ 451*cfb92d14SAndroid Build Coastguard Worker otError ReadEui48(spinel_eui48_t &aEui48); 452*cfb92d14SAndroid Build Coastguard Worker 453*cfb92d14SAndroid Build Coastguard Worker /** 454*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads a UTF8 string form the frame. 455*cfb92d14SAndroid Build Coastguard Worker * 456*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 457*cfb92d14SAndroid Build Coastguard Worker * 458*cfb92d14SAndroid Build Coastguard Worker * @param[out] aUtf8 Reference to a `char` pointer to output the string. 459*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 460*cfb92d14SAndroid Build Coastguard Worker * 461*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 462*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 463*cfb92d14SAndroid Build Coastguard Worker * 464*cfb92d14SAndroid Build Coastguard Worker */ 465*cfb92d14SAndroid Build Coastguard Worker otError ReadUtf8(const char *&aUtf8); 466*cfb92d14SAndroid Build Coastguard Worker 467*cfb92d14SAndroid Build Coastguard Worker /** 468*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads a data blob (sequence of bytes) form the frame. 469*cfb92d14SAndroid Build Coastguard Worker * 470*cfb92d14SAndroid Build Coastguard Worker * On success, the read position gets updated. 471*cfb92d14SAndroid Build Coastguard Worker * 472*cfb92d14SAndroid Build Coastguard Worker * @param[out] aData Reference to pointer variable to output the data. 473*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 474*cfb92d14SAndroid Build Coastguard Worker * @param[out] aDataLen Reference to variable to output the data length (number of bytes). 475*cfb92d14SAndroid Build Coastguard Worker * 476*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 477*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 478*cfb92d14SAndroid Build Coastguard Worker * 479*cfb92d14SAndroid Build Coastguard Worker */ 480*cfb92d14SAndroid Build Coastguard Worker otError ReadData(const uint8_t *&aData, uint16_t &aDataLen); 481*cfb92d14SAndroid Build Coastguard Worker 482*cfb92d14SAndroid Build Coastguard Worker /** 483*cfb92d14SAndroid Build Coastguard Worker * Decodes and reads a data blob (sequence of bytes) with data length. 484*cfb92d14SAndroid Build Coastguard Worker * 485*cfb92d14SAndroid Build Coastguard Worker * The data length is assumed to be prepended before the data content (encoded as a `uint16_t`). The size of the 486*cfb92d14SAndroid Build Coastguard Worker * length field should not be included in the length value. This method corresponds to `SPINEL_DATATYPE_DATA_WLEN` 487*cfb92d14SAndroid Build Coastguard Worker * type. 488*cfb92d14SAndroid Build Coastguard Worker * 489*cfb92d14SAndroid Build Coastguard Worker * @param[out] aData Reference to pointer variable to output the data. 490*cfb92d14SAndroid Build Coastguard Worker * On success, the pointer variable is updated. 491*cfb92d14SAndroid Build Coastguard Worker * @param[out] aDataLen Reference to variable to out the data length (number of bytes). 492*cfb92d14SAndroid Build Coastguard Worker * 493*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully read the value. 494*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/decode the value. 495*cfb92d14SAndroid Build Coastguard Worker * 496*cfb92d14SAndroid Build Coastguard Worker */ 497*cfb92d14SAndroid Build Coastguard Worker otError ReadDataWithLen(const uint8_t *&aData, uint16_t &aDataLen); 498*cfb92d14SAndroid Build Coastguard Worker 499*cfb92d14SAndroid Build Coastguard Worker /** 500*cfb92d14SAndroid Build Coastguard Worker * Opens a struct in the frame. 501*cfb92d14SAndroid Build Coastguard Worker * 502*cfb92d14SAndroid Build Coastguard Worker * After a successful call to this method, all the subsequent `Read{SomeType}()` methods decode and read the 503*cfb92d14SAndroid Build Coastguard Worker * field/value from the current open struct until the struct is closed using `CloseStruct()` method. Structures can 504*cfb92d14SAndroid Build Coastguard Worker * be nested. Up to `kMaxNestedStructs` nested structs can be opened at the same time. 505*cfb92d14SAndroid Build Coastguard Worker * 506*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully opened a struct. 507*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE Failed to parse/open a struct. 508*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE Already at the maximum number of nested open structures. 509*cfb92d14SAndroid Build Coastguard Worker * 510*cfb92d14SAndroid Build Coastguard Worker */ 511*cfb92d14SAndroid Build Coastguard Worker otError OpenStruct(void); 512*cfb92d14SAndroid Build Coastguard Worker 513*cfb92d14SAndroid Build Coastguard Worker /** 514*cfb92d14SAndroid Build Coastguard Worker * Closes the most recently opened struct (using `OpenStruct()`) in the frame. 515*cfb92d14SAndroid Build Coastguard Worker * 516*cfb92d14SAndroid Build Coastguard Worker * On success, the read position is moved to end of the struct skipping any unread bytes within the struct. 517*cfb92d14SAndroid Build Coastguard Worker * 518*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully closed the struct. 519*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE There is no current open struct to close. 520*cfb92d14SAndroid Build Coastguard Worker * 521*cfb92d14SAndroid Build Coastguard Worker */ 522*cfb92d14SAndroid Build Coastguard Worker otError CloseStruct(void); 523*cfb92d14SAndroid Build Coastguard Worker 524*cfb92d14SAndroid Build Coastguard Worker /** 525*cfb92d14SAndroid Build Coastguard Worker * Returns the number of remaining/unread bytes in the current inner-most open structure. 526*cfb92d14SAndroid Build Coastguard Worker * 527*cfb92d14SAndroid Build Coastguard Worker * If there is no currently open structure the number of remaining bytes in whole frame is returned instead. 528*cfb92d14SAndroid Build Coastguard Worker * 529*cfb92d14SAndroid Build Coastguard Worker * @returns The number of remaining unread bytes in the inner-most open structure. 530*cfb92d14SAndroid Build Coastguard Worker * 531*cfb92d14SAndroid Build Coastguard Worker */ GetRemainingLengthInStruct(void) const532*cfb92d14SAndroid Build Coastguard Worker uint16_t GetRemainingLengthInStruct(void) const { return mEnd - mIndex; } 533*cfb92d14SAndroid Build Coastguard Worker 534*cfb92d14SAndroid Build Coastguard Worker /** 535*cfb92d14SAndroid Build Coastguard Worker * Indicates whether or not all the bytes in inner-most open structure are read. 536*cfb92d14SAndroid Build Coastguard Worker * 537*cfb92d14SAndroid Build Coastguard Worker * If there is no currently open structure, the whole frame is considered instead. 538*cfb92d14SAndroid Build Coastguard Worker * 539*cfb92d14SAndroid Build Coastguard Worker * @returns TRUE if all the bytes are read, FALSE otherwise. 540*cfb92d14SAndroid Build Coastguard Worker * 541*cfb92d14SAndroid Build Coastguard Worker */ IsAllReadInStruct(void) const542*cfb92d14SAndroid Build Coastguard Worker bool IsAllReadInStruct(void) const { return (mIndex == mEnd); } 543*cfb92d14SAndroid Build Coastguard Worker 544*cfb92d14SAndroid Build Coastguard Worker /** 545*cfb92d14SAndroid Build Coastguard Worker * Saves the current read position in the frame. 546*cfb92d14SAndroid Build Coastguard Worker * 547*cfb92d14SAndroid Build Coastguard Worker * A subsequent call to `SavePosition()` will overwrite the previously saved position. The saved position can be 548*cfb92d14SAndroid Build Coastguard Worker * used to move the read position back (using `ResetToSaved()`) and re-read the same content. 549*cfb92d14SAndroid Build Coastguard Worker * 550*cfb92d14SAndroid Build Coastguard Worker * Saved position can be within an open struct, and it remembers its enclosing struct. When the enclosing struct is 551*cfb92d14SAndroid Build Coastguard Worker * closed, the saved position will be voided and can no longer be used. This ensures that we cannot jump back to 552*cfb92d14SAndroid Build Coastguard Worker * middle an already fully decoded/read and closed struct. 553*cfb92d14SAndroid Build Coastguard Worker * 554*cfb92d14SAndroid Build Coastguard Worker */ 555*cfb92d14SAndroid Build Coastguard Worker void SavePosition(void); 556*cfb92d14SAndroid Build Coastguard Worker 557*cfb92d14SAndroid Build Coastguard Worker /** 558*cfb92d14SAndroid Build Coastguard Worker * Resets/moves the read position to a previously saved position. 559*cfb92d14SAndroid Build Coastguard Worker * 560*cfb92d14SAndroid Build Coastguard Worker * The saved position remembers its enclosing structure. When `ResetToSaved()` is called, the current open 561*cfb92d14SAndroid Build Coastguard Worker * structure will be the same as when position was saved. 562*cfb92d14SAndroid Build Coastguard Worker * 563*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully reset the read position. 564*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE The saved position is not valid (there is no saved position or the saved 565*cfb92d14SAndroid Build Coastguard Worker * position was voided since its enclosing struct was closed). 566*cfb92d14SAndroid Build Coastguard Worker * 567*cfb92d14SAndroid Build Coastguard Worker */ 568*cfb92d14SAndroid Build Coastguard Worker otError ResetToSaved(void); 569*cfb92d14SAndroid Build Coastguard Worker 570*cfb92d14SAndroid Build Coastguard Worker private: 571*cfb92d14SAndroid Build Coastguard Worker otError ReadItem(const uint8_t **aPtr, uint16_t aSize); ClearSavedPosition(void)572*cfb92d14SAndroid Build Coastguard Worker void ClearSavedPosition(void) { mSavedIndex = mLength; } IsSavedPositionValid(void) const573*cfb92d14SAndroid Build Coastguard Worker bool IsSavedPositionValid(void) const { return (mSavedIndex < mLength); } 574*cfb92d14SAndroid Build Coastguard Worker 575*cfb92d14SAndroid Build Coastguard Worker const uint8_t *mFrame; // Frame buffer. 576*cfb92d14SAndroid Build Coastguard Worker uint16_t mLength; // Frame length (number of bytes). 577*cfb92d14SAndroid Build Coastguard Worker uint16_t mIndex; // Current read index. 578*cfb92d14SAndroid Build Coastguard Worker uint16_t mEnd; // Current end index (end of struct if in a struct, or end of buffer otherwise). 579*cfb92d14SAndroid Build Coastguard Worker uint8_t mNumOpenStructs; // Number of open structs. 580*cfb92d14SAndroid Build Coastguard Worker 581*cfb92d14SAndroid Build Coastguard Worker uint8_t mSavedNumOpenStructs; // Number of open structs when read position was saved. 582*cfb92d14SAndroid Build Coastguard Worker uint16_t mSavedIndex; // Read index when position was saved. 583*cfb92d14SAndroid Build Coastguard Worker uint16_t mSavedEnd; // End index when position was saved. 584*cfb92d14SAndroid Build Coastguard Worker 585*cfb92d14SAndroid Build Coastguard Worker uint16_t mPrevEnd[kMaxNestedStructs]; 586*cfb92d14SAndroid Build Coastguard Worker }; 587*cfb92d14SAndroid Build Coastguard Worker 588*cfb92d14SAndroid Build Coastguard Worker } // namespace Spinel 589*cfb92d14SAndroid Build Coastguard Worker } // namespace ot 590*cfb92d14SAndroid Build Coastguard Worker 591*cfb92d14SAndroid Build Coastguard Worker #endif // SPINEL_DECODER_HPP_ 592