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 encoder. 31*cfb92d14SAndroid Build Coastguard Worker */ 32*cfb92d14SAndroid Build Coastguard Worker 33*cfb92d14SAndroid Build Coastguard Worker #ifndef SPINEL_ENCODER_HPP_ 34*cfb92d14SAndroid Build Coastguard Worker #define SPINEL_ENCODER_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/message.h> 40*cfb92d14SAndroid Build Coastguard Worker #include <openthread/ncp.h> 41*cfb92d14SAndroid Build Coastguard Worker 42*cfb92d14SAndroid Build Coastguard Worker #include "spinel.h" 43*cfb92d14SAndroid Build Coastguard Worker #include "spinel_buffer.hpp" 44*cfb92d14SAndroid Build Coastguard Worker 45*cfb92d14SAndroid Build Coastguard Worker namespace ot { 46*cfb92d14SAndroid Build Coastguard Worker namespace Spinel { 47*cfb92d14SAndroid Build Coastguard Worker 48*cfb92d14SAndroid Build Coastguard Worker /** 49*cfb92d14SAndroid Build Coastguard Worker * Defines a spinel encoder. 50*cfb92d14SAndroid Build Coastguard Worker * 51*cfb92d14SAndroid Build Coastguard Worker */ 52*cfb92d14SAndroid Build Coastguard Worker class Encoder 53*cfb92d14SAndroid Build Coastguard Worker { 54*cfb92d14SAndroid Build Coastguard Worker public: 55*cfb92d14SAndroid Build Coastguard Worker /** 56*cfb92d14SAndroid Build Coastguard Worker * Initializes a `Encoder` object. 57*cfb92d14SAndroid Build Coastguard Worker * 58*cfb92d14SAndroid Build Coastguard Worker * @param[in] aNcpBuffer A reference to a `Spinel::Buffer` where the frames are written. 59*cfb92d14SAndroid Build Coastguard Worker * 60*cfb92d14SAndroid Build Coastguard Worker */ Encoder(Spinel::Buffer & aNcpBuffer)61*cfb92d14SAndroid Build Coastguard Worker explicit Encoder(Spinel::Buffer &aNcpBuffer) 62*cfb92d14SAndroid Build Coastguard Worker : mNcpBuffer(aNcpBuffer) 63*cfb92d14SAndroid Build Coastguard Worker , mNumOpenStructs(0) 64*cfb92d14SAndroid Build Coastguard Worker , mSavedNumOpenStructs(0) 65*cfb92d14SAndroid Build Coastguard Worker { 66*cfb92d14SAndroid Build Coastguard Worker } 67*cfb92d14SAndroid Build Coastguard Worker 68*cfb92d14SAndroid Build Coastguard Worker /** 69*cfb92d14SAndroid Build Coastguard Worker * Begins a new frame to be added/written to the frame buffer. 70*cfb92d14SAndroid Build Coastguard Worker * 71*cfb92d14SAndroid Build Coastguard Worker * If there is a previous frame being written (for which `EndFrame()` has not yet been called), calling 72*cfb92d14SAndroid Build Coastguard Worker * `BeginFrame()` will discard and clear the previous unfinished frame. 73*cfb92d14SAndroid Build Coastguard Worker * 74*cfb92d14SAndroid Build Coastguard Worker * @param[in] aPriority Priority level of the new input frame. 75*cfb92d14SAndroid Build Coastguard Worker * 76*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully started a new frame. 77*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to start a new frame. 78*cfb92d14SAndroid Build Coastguard Worker * 79*cfb92d14SAndroid Build Coastguard Worker */ 80*cfb92d14SAndroid Build Coastguard Worker otError BeginFrame(Spinel::Buffer::Priority aPriority); 81*cfb92d14SAndroid Build Coastguard Worker 82*cfb92d14SAndroid Build Coastguard Worker /** 83*cfb92d14SAndroid Build Coastguard Worker * Begins a new spinel command frame to be added/written to the frame buffer. 84*cfb92d14SAndroid Build Coastguard Worker * 85*cfb92d14SAndroid Build Coastguard Worker * If there is a previous frame being written (for which `EndFrame()` has not yet been called), calling 86*cfb92d14SAndroid Build Coastguard Worker * `BeginFrame()` will discard and clear the previous unfinished frame. 87*cfb92d14SAndroid Build Coastguard Worker * 88*cfb92d14SAndroid Build Coastguard Worker * The spinel transaction ID (TID) in the given spinel header is used to determine the priority level of the new 89*cfb92d14SAndroid Build Coastguard Worker * frame. Non-zero TID value indicates that the frame is a response and therefore it uses higher priority level. 90*cfb92d14SAndroid Build Coastguard Worker * 91*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHeader Spinel header for new the command frame. 92*cfb92d14SAndroid Build Coastguard Worker * @param[in] aCommand Spinel command. 93*cfb92d14SAndroid Build Coastguard Worker * 94*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully started a new frame. 95*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to start a new frame. 96*cfb92d14SAndroid Build Coastguard Worker * 97*cfb92d14SAndroid Build Coastguard Worker */ 98*cfb92d14SAndroid Build Coastguard Worker otError BeginFrame(uint8_t aHeader, unsigned int aCommand); 99*cfb92d14SAndroid Build Coastguard Worker 100*cfb92d14SAndroid Build Coastguard Worker /** 101*cfb92d14SAndroid Build Coastguard Worker * Begins a new spinel property update command frame to be added/written to the frame buffer. 102*cfb92d14SAndroid Build Coastguard Worker * 103*cfb92d14SAndroid Build Coastguard Worker * If there is a previous frame being written (for which `EndFrame()` has not yet been called), calling 104*cfb92d14SAndroid Build Coastguard Worker * `BeginFrame()` will discard and clear the previous unfinished frame. 105*cfb92d14SAndroid Build Coastguard Worker * 106*cfb92d14SAndroid Build Coastguard Worker * The spinel transaction ID (TID) in the given spinel header is used to determine the priority level of the new 107*cfb92d14SAndroid Build Coastguard Worker * frame. Non-zero TID value indicates that the frame is a response and therefore it uses higher priority level. 108*cfb92d14SAndroid Build Coastguard Worker * 109*cfb92d14SAndroid Build Coastguard Worker * Saves the write position before the property key (see also `SavePosition()`) so that if fetching the 110*cfb92d14SAndroid Build Coastguard Worker * property fails and the property key should be switched to `LAST_STATUS` with an error status, the saved 111*cfb92d14SAndroid Build Coastguard Worker * position can be used to update the property key in the frame (see also `OverwriteWithLastStatusError()`) 112*cfb92d14SAndroid Build Coastguard Worker * 113*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHeader Spinel header for new the command frame. 114*cfb92d14SAndroid Build Coastguard Worker * @param[in] aCommand Spinel command. 115*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKey Spinel property key 116*cfb92d14SAndroid Build Coastguard Worker * 117*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully started a new frame. 118*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to start a new frame. 119*cfb92d14SAndroid Build Coastguard Worker * 120*cfb92d14SAndroid Build Coastguard Worker */ 121*cfb92d14SAndroid Build Coastguard Worker otError BeginFrame(uint8_t aHeader, unsigned int aCommand, spinel_prop_key_t aKey); 122*cfb92d14SAndroid Build Coastguard Worker 123*cfb92d14SAndroid Build Coastguard Worker /** 124*cfb92d14SAndroid Build Coastguard Worker * Overwrites the property key with `LAST_STATUS` in a property update command frame. 125*cfb92d14SAndroid Build Coastguard Worker * 126*cfb92d14SAndroid Build Coastguard Worker * Should be only used after a successful `BeginFrame(aHeader, aCommand, aPropertyKey)`, otherwise, its 127*cfb92d14SAndroid Build Coastguard Worker * behavior is undefined. 128*cfb92d14SAndroid Build Coastguard Worker * 129*cfb92d14SAndroid Build Coastguard Worker * Moves the write position back to saved position by `BeginFrame()` and replaces the property key 130*cfb92d14SAndroid Build Coastguard Worker * `SPINEL_PROP_LAST_STATUS` and writes the given spinel status error. 131*cfb92d14SAndroid Build Coastguard Worker * 132*cfb92d14SAndroid Build Coastguard Worker * @param[in] aStatus Spinel error status 133*cfb92d14SAndroid Build Coastguard Worker * 134*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully updated the frame. 135*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to update the frame. 136*cfb92d14SAndroid Build Coastguard Worker * 137*cfb92d14SAndroid Build Coastguard Worker */ 138*cfb92d14SAndroid Build Coastguard Worker otError OverwriteWithLastStatusError(spinel_status_t aStatus); 139*cfb92d14SAndroid Build Coastguard Worker 140*cfb92d14SAndroid Build Coastguard Worker /** 141*cfb92d14SAndroid Build Coastguard Worker * Finalizes/ends the current frame being written to the buffer. 142*cfb92d14SAndroid Build Coastguard Worker * 143*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new frame. Otherwise, this method 144*cfb92d14SAndroid Build Coastguard Worker * does nothing and returns error status `OT_ERROR_INVALID_STATE`. 145*cfb92d14SAndroid Build Coastguard Worker * 146*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the frame and return error status 147*cfb92d14SAndroid Build Coastguard Worker * `OT_ERROR_NO_BUFS`. 148*cfb92d14SAndroid Build Coastguard Worker * 149*cfb92d14SAndroid Build Coastguard Worker * Ensures to close any open structure (previously opened using `OpenStruct()` but not closed using 150*cfb92d14SAndroid Build Coastguard Worker * `CloseStruct()`). 151*cfb92d14SAndroid Build Coastguard Worker * 152*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully ended the input frame. 153*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add message. 154*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 155*cfb92d14SAndroid Build Coastguard Worker * 156*cfb92d14SAndroid Build Coastguard Worker */ 157*cfb92d14SAndroid Build Coastguard Worker otError EndFrame(void); 158*cfb92d14SAndroid Build Coastguard Worker 159*cfb92d14SAndroid Build Coastguard Worker /** 160*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a boolean value to current input frame. 161*cfb92d14SAndroid Build Coastguard Worker * 162*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 163*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 164*cfb92d14SAndroid Build Coastguard Worker * 165*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 166*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 167*cfb92d14SAndroid Build Coastguard Worker * 168*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBool The boolean value to add to input frame. 169*cfb92d14SAndroid Build Coastguard Worker * 170*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given byte to the frame. 171*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 172*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 173*cfb92d14SAndroid Build Coastguard Worker * 174*cfb92d14SAndroid Build Coastguard Worker */ WriteBool(bool aBool)175*cfb92d14SAndroid Build Coastguard Worker otError WriteBool(bool aBool) { return mNcpBuffer.InFrameFeedByte(aBool ? 0x01 : 0x00); } 176*cfb92d14SAndroid Build Coastguard Worker 177*cfb92d14SAndroid Build Coastguard Worker /** 178*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a `uint8_t` value to current input frame. 179*cfb92d14SAndroid Build Coastguard Worker * 180*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 181*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 182*cfb92d14SAndroid Build Coastguard Worker * 183*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 184*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 185*cfb92d14SAndroid Build Coastguard Worker * 186*cfb92d14SAndroid Build Coastguard Worker * @param[in] aUint8 The value to add to input frame. 187*cfb92d14SAndroid Build Coastguard Worker * 188*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 189*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 190*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 191*cfb92d14SAndroid Build Coastguard Worker * 192*cfb92d14SAndroid Build Coastguard Worker */ WriteUint8(uint8_t aUint8)193*cfb92d14SAndroid Build Coastguard Worker otError WriteUint8(uint8_t aUint8) { return mNcpBuffer.InFrameFeedByte(aUint8); } 194*cfb92d14SAndroid Build Coastguard Worker 195*cfb92d14SAndroid Build Coastguard Worker /** 196*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an `int8_t` value to current input frame. 197*cfb92d14SAndroid Build Coastguard Worker * 198*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 199*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 200*cfb92d14SAndroid Build Coastguard Worker * 201*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 202*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 203*cfb92d14SAndroid Build Coastguard Worker * 204*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInt8 The value to add to input frame. 205*cfb92d14SAndroid Build Coastguard Worker * 206*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 207*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 208*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 209*cfb92d14SAndroid Build Coastguard Worker * 210*cfb92d14SAndroid Build Coastguard Worker */ WriteInt8(int8_t aInt8)211*cfb92d14SAndroid Build Coastguard Worker otError WriteInt8(int8_t aInt8) { return WriteUint8(static_cast<uint8_t>(aInt8)); } 212*cfb92d14SAndroid Build Coastguard Worker 213*cfb92d14SAndroid Build Coastguard Worker /** 214*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a `uint16_t` value to current input frame. 215*cfb92d14SAndroid Build Coastguard Worker * 216*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 217*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 218*cfb92d14SAndroid Build Coastguard Worker * 219*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 220*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 221*cfb92d14SAndroid Build Coastguard Worker * 222*cfb92d14SAndroid Build Coastguard Worker * @param[in] aUint16 The value to add to input frame. 223*cfb92d14SAndroid Build Coastguard Worker * 224*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 225*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 226*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 227*cfb92d14SAndroid Build Coastguard Worker * 228*cfb92d14SAndroid Build Coastguard Worker */ 229*cfb92d14SAndroid Build Coastguard Worker otError WriteUint16(uint16_t aUint16); 230*cfb92d14SAndroid Build Coastguard Worker 231*cfb92d14SAndroid Build Coastguard Worker /** 232*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an `int16_t` value to current input frame. 233*cfb92d14SAndroid Build Coastguard Worker * 234*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 235*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 236*cfb92d14SAndroid Build Coastguard Worker * 237*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 238*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 239*cfb92d14SAndroid Build Coastguard Worker * 240*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInt16 The value to add to input frame. 241*cfb92d14SAndroid Build Coastguard Worker * 242*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 243*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 244*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 245*cfb92d14SAndroid Build Coastguard Worker * 246*cfb92d14SAndroid Build Coastguard Worker */ WriteInt16(int16_t aInt16)247*cfb92d14SAndroid Build Coastguard Worker otError WriteInt16(int16_t aInt16) { return WriteUint16(static_cast<uint16_t>(aInt16)); } 248*cfb92d14SAndroid Build Coastguard Worker 249*cfb92d14SAndroid Build Coastguard Worker /** 250*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a `uint32_t` value to current input frame. 251*cfb92d14SAndroid Build Coastguard Worker * 252*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 253*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 254*cfb92d14SAndroid Build Coastguard Worker * 255*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 256*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 257*cfb92d14SAndroid Build Coastguard Worker * 258*cfb92d14SAndroid Build Coastguard Worker * @param[in] aUint32 The value to add to input frame. 259*cfb92d14SAndroid Build Coastguard Worker * 260*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 261*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 262*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 263*cfb92d14SAndroid Build Coastguard Worker * 264*cfb92d14SAndroid Build Coastguard Worker */ 265*cfb92d14SAndroid Build Coastguard Worker otError WriteUint32(uint32_t aUint32); 266*cfb92d14SAndroid Build Coastguard Worker 267*cfb92d14SAndroid Build Coastguard Worker /** 268*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an `int32_t` value to current input frame. 269*cfb92d14SAndroid Build Coastguard Worker * 270*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 271*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 272*cfb92d14SAndroid Build Coastguard Worker * 273*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 274*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 275*cfb92d14SAndroid Build Coastguard Worker * 276*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInt32 The value to add to input frame. 277*cfb92d14SAndroid Build Coastguard Worker * 278*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 279*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 280*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 281*cfb92d14SAndroid Build Coastguard Worker * 282*cfb92d14SAndroid Build Coastguard Worker */ WriteInt32(int32_t aInt32)283*cfb92d14SAndroid Build Coastguard Worker otError WriteInt32(int32_t aInt32) { return WriteUint32(static_cast<uint32_t>(aInt32)); } 284*cfb92d14SAndroid Build Coastguard Worker 285*cfb92d14SAndroid Build Coastguard Worker /** 286*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a `uint64_t` value to current input frame. 287*cfb92d14SAndroid Build Coastguard Worker * 288*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 289*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 290*cfb92d14SAndroid Build Coastguard Worker * 291*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 292*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 293*cfb92d14SAndroid Build Coastguard Worker * 294*cfb92d14SAndroid Build Coastguard Worker * @param[in] aUint64 The value to add to input frame. 295*cfb92d14SAndroid Build Coastguard Worker * 296*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 297*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 298*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 299*cfb92d14SAndroid Build Coastguard Worker * 300*cfb92d14SAndroid Build Coastguard Worker */ 301*cfb92d14SAndroid Build Coastguard Worker otError WriteUint64(uint64_t aUint64); 302*cfb92d14SAndroid Build Coastguard Worker 303*cfb92d14SAndroid Build Coastguard Worker /** 304*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an `int64_t` value to current input frame. 305*cfb92d14SAndroid Build Coastguard Worker * 306*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 307*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 308*cfb92d14SAndroid Build Coastguard Worker * 309*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 310*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 311*cfb92d14SAndroid Build Coastguard Worker * 312*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInt64 The value to add to input frame. 313*cfb92d14SAndroid Build Coastguard Worker * 314*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 315*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 316*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 317*cfb92d14SAndroid Build Coastguard Worker * 318*cfb92d14SAndroid Build Coastguard Worker */ WriteInt64(int64_t aInt64)319*cfb92d14SAndroid Build Coastguard Worker otError WriteInt64(int64_t aInt64) { return WriteUint64(static_cast<uint64_t>(aInt64)); } 320*cfb92d14SAndroid Build Coastguard Worker 321*cfb92d14SAndroid Build Coastguard Worker /** 322*cfb92d14SAndroid Build Coastguard Worker * Encodes (using spinel packed integer format) and writes a value to current input frame. 323*cfb92d14SAndroid Build Coastguard Worker * 324*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 325*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 326*cfb92d14SAndroid Build Coastguard Worker * 327*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 328*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 329*cfb92d14SAndroid Build Coastguard Worker * 330*cfb92d14SAndroid Build Coastguard Worker * @param[in] aUint The value to add to input frame. 331*cfb92d14SAndroid Build Coastguard Worker * 332*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 333*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the value. 334*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 335*cfb92d14SAndroid Build Coastguard Worker * 336*cfb92d14SAndroid Build Coastguard Worker */ 337*cfb92d14SAndroid Build Coastguard Worker otError WriteUintPacked(unsigned int aUint); 338*cfb92d14SAndroid Build Coastguard Worker 339*cfb92d14SAndroid Build Coastguard Worker /** 340*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an IPv6 address to current input frame. 341*cfb92d14SAndroid Build Coastguard Worker * 342*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 343*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 344*cfb92d14SAndroid Build Coastguard Worker * 345*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 346*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 347*cfb92d14SAndroid Build Coastguard Worker * 348*cfb92d14SAndroid Build Coastguard Worker * @param[in] aIp6Addr A reference to the IPv6 address to be added (as `spinel_ipv6addr_t`) 349*cfb92d14SAndroid Build Coastguard Worker * 350*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given address to the frame. 351*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the IP address. 352*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 353*cfb92d14SAndroid Build Coastguard Worker * 354*cfb92d14SAndroid Build Coastguard Worker */ WriteIp6Address(const spinel_ipv6addr_t & aIp6Addr)355*cfb92d14SAndroid Build Coastguard Worker otError WriteIp6Address(const spinel_ipv6addr_t &aIp6Addr) { return WriteIp6Address(aIp6Addr.bytes); } 356*cfb92d14SAndroid Build Coastguard Worker 357*cfb92d14SAndroid Build Coastguard Worker /** 358*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an IPv6 address to current input frame. 359*cfb92d14SAndroid Build Coastguard Worker * 360*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 361*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 362*cfb92d14SAndroid Build Coastguard Worker * 363*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 364*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 365*cfb92d14SAndroid Build Coastguard Worker * 366*cfb92d14SAndroid Build Coastguard Worker * @param[in] aIp6Addr A reference to the IPv6 address to be added (as `otIp6Address`) 367*cfb92d14SAndroid Build Coastguard Worker * 368*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given address to the frame. 369*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the IP address. 370*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 371*cfb92d14SAndroid Build Coastguard Worker * 372*cfb92d14SAndroid Build Coastguard Worker */ WriteIp6Address(const otIp6Address & aIp6Addr)373*cfb92d14SAndroid Build Coastguard Worker otError WriteIp6Address(const otIp6Address &aIp6Addr) { return WriteIp6Address(aIp6Addr.mFields.m8); } 374*cfb92d14SAndroid Build Coastguard Worker 375*cfb92d14SAndroid Build Coastguard Worker /** 376*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an IPv6 address to current input frame. 377*cfb92d14SAndroid Build Coastguard Worker * 378*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 379*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 380*cfb92d14SAndroid Build Coastguard Worker * 381*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 382*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 383*cfb92d14SAndroid Build Coastguard Worker * 384*cfb92d14SAndroid Build Coastguard Worker * @param[in] aIp6AddrBuf A pointer to a buffer containing the IPv6 address. 385*cfb92d14SAndroid Build Coastguard Worker * 386*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given address to the frame. 387*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the IP address. 388*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 389*cfb92d14SAndroid Build Coastguard Worker * 390*cfb92d14SAndroid Build Coastguard Worker */ WriteIp6Address(const uint8_t * aIp6AddrBuf)391*cfb92d14SAndroid Build Coastguard Worker otError WriteIp6Address(const uint8_t *aIp6AddrBuf) { return WriteData(aIp6AddrBuf, sizeof(spinel_ipv6addr_t)); } 392*cfb92d14SAndroid Build Coastguard Worker 393*cfb92d14SAndroid Build Coastguard Worker /** 394*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an EUI64 value to current input frame. 395*cfb92d14SAndroid Build Coastguard Worker * 396*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 397*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 398*cfb92d14SAndroid Build Coastguard Worker * 399*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 400*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 401*cfb92d14SAndroid Build Coastguard Worker * 402*cfb92d14SAndroid Build Coastguard Worker * @param[in] aEui64 A reference to the EUI64 value as a `spinel_eui64_t` type. 403*cfb92d14SAndroid Build Coastguard Worker * 404*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 405*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the EUI64 value. 406*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 407*cfb92d14SAndroid Build Coastguard Worker * 408*cfb92d14SAndroid Build Coastguard Worker */ WriteEui64(const spinel_eui64_t & aEui64)409*cfb92d14SAndroid Build Coastguard Worker otError WriteEui64(const spinel_eui64_t &aEui64) { return WriteEui64(aEui64.bytes); } 410*cfb92d14SAndroid Build Coastguard Worker 411*cfb92d14SAndroid Build Coastguard Worker /** 412*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an EUI64 value to current input frame. 413*cfb92d14SAndroid Build Coastguard Worker * 414*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 415*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 416*cfb92d14SAndroid Build Coastguard Worker * 417*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 418*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 419*cfb92d14SAndroid Build Coastguard Worker * 420*cfb92d14SAndroid Build Coastguard Worker * @param[in] aExtAddress A reference to an `otExtAddress` 421*cfb92d14SAndroid Build Coastguard Worker * 422*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 423*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the EUI64 value. 424*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 425*cfb92d14SAndroid Build Coastguard Worker * 426*cfb92d14SAndroid Build Coastguard Worker */ WriteEui64(const otExtAddress & aExtAddress)427*cfb92d14SAndroid Build Coastguard Worker otError WriteEui64(const otExtAddress &aExtAddress) { return WriteEui64(aExtAddress.m8); } 428*cfb92d14SAndroid Build Coastguard Worker 429*cfb92d14SAndroid Build Coastguard Worker /** 430*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an EUI64 value to current input frame. 431*cfb92d14SAndroid Build Coastguard Worker * 432*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 433*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 434*cfb92d14SAndroid Build Coastguard Worker * 435*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 436*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 437*cfb92d14SAndroid Build Coastguard Worker * 438*cfb92d14SAndroid Build Coastguard Worker * @param[in] aEui64 A pointer to a buffer containing the EUI64 value. 439*cfb92d14SAndroid Build Coastguard Worker * 440*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 441*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the EUI64 value. 442*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 443*cfb92d14SAndroid Build Coastguard Worker * 444*cfb92d14SAndroid Build Coastguard Worker */ WriteEui64(const uint8_t * aEui64)445*cfb92d14SAndroid Build Coastguard Worker otError WriteEui64(const uint8_t *aEui64) { return WriteData(aEui64, sizeof(spinel_eui64_t)); } 446*cfb92d14SAndroid Build Coastguard Worker 447*cfb92d14SAndroid Build Coastguard Worker /** 448*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an EUI48 value to current input frame. 449*cfb92d14SAndroid Build Coastguard Worker * 450*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 451*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 452*cfb92d14SAndroid Build Coastguard Worker * 453*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 454*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 455*cfb92d14SAndroid Build Coastguard Worker * 456*cfb92d14SAndroid Build Coastguard Worker * @param[in] aEui48 A reference to the EUI48 value. 457*cfb92d14SAndroid Build Coastguard Worker * 458*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 459*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the EUI48 value. 460*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 461*cfb92d14SAndroid Build Coastguard Worker * 462*cfb92d14SAndroid Build Coastguard Worker */ WriteEui48(const spinel_eui48_t & aEui48)463*cfb92d14SAndroid Build Coastguard Worker otError WriteEui48(const spinel_eui48_t &aEui48) { return WriteEui48(aEui48.bytes); } 464*cfb92d14SAndroid Build Coastguard Worker 465*cfb92d14SAndroid Build Coastguard Worker /** 466*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes an EUI48 value to current input frame. 467*cfb92d14SAndroid Build Coastguard Worker * 468*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 469*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 470*cfb92d14SAndroid Build Coastguard Worker * 471*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 472*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 473*cfb92d14SAndroid Build Coastguard Worker * 474*cfb92d14SAndroid Build Coastguard Worker * @param[in] aEui48 A pointer to a buffer containing the EUI64 value. 475*cfb92d14SAndroid Build Coastguard Worker * 476*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given value to the frame. 477*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the EUI48 value. 478*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 479*cfb92d14SAndroid Build Coastguard Worker * 480*cfb92d14SAndroid Build Coastguard Worker */ WriteEui48(const uint8_t * aEui48)481*cfb92d14SAndroid Build Coastguard Worker otError WriteEui48(const uint8_t *aEui48) { return WriteData(aEui48, sizeof(spinel_eui48_t)); } 482*cfb92d14SAndroid Build Coastguard Worker 483*cfb92d14SAndroid Build Coastguard Worker /** 484*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a UTF8 string to current input frame. 485*cfb92d14SAndroid Build Coastguard Worker * 486*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 487*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 488*cfb92d14SAndroid Build Coastguard Worker * 489*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 490*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 491*cfb92d14SAndroid Build Coastguard Worker * 492*cfb92d14SAndroid Build Coastguard Worker * @param[in] aUtf8 A const character pointer (C string). 493*cfb92d14SAndroid Build Coastguard Worker * 494*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given string to the frame. 495*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the string. 496*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 497*cfb92d14SAndroid Build Coastguard Worker * 498*cfb92d14SAndroid Build Coastguard Worker */ 499*cfb92d14SAndroid Build Coastguard Worker otError WriteUtf8(const char *aUtf8); 500*cfb92d14SAndroid Build Coastguard Worker 501*cfb92d14SAndroid Build Coastguard Worker /** 502*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a sequence of bytes to current input frame. 503*cfb92d14SAndroid Build Coastguard Worker * 504*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 505*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 506*cfb92d14SAndroid Build Coastguard Worker * 507*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 508*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 509*cfb92d14SAndroid Build Coastguard Worker * 510*cfb92d14SAndroid Build Coastguard Worker * @param[in] aData A pointer to data buffer. 511*cfb92d14SAndroid Build Coastguard Worker * @param[in] aDataLen The length (number of bytes) in the data buffer. 512*cfb92d14SAndroid Build Coastguard Worker * 513*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given data to the frame. 514*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the byte. 515*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 516*cfb92d14SAndroid Build Coastguard Worker * 517*cfb92d14SAndroid Build Coastguard Worker */ WriteData(const uint8_t * aData,uint16_t aDataLen)518*cfb92d14SAndroid Build Coastguard Worker otError WriteData(const uint8_t *aData, uint16_t aDataLen) { return mNcpBuffer.InFrameFeedData(aData, aDataLen); } 519*cfb92d14SAndroid Build Coastguard Worker 520*cfb92d14SAndroid Build Coastguard Worker /** 521*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a data blob (sequence of bytes) with its length prepended before the data. 522*cfb92d14SAndroid Build Coastguard Worker * 523*cfb92d14SAndroid Build Coastguard Worker * The length of the data (in bytes) is prepended (with the length encoded as a `uint16`). The size of the length 524*cfb92d14SAndroid Build Coastguard Worker * field is not included in the length. This is similar to `SPINEL_DATATYPE_DATA_WLEN` type. 525*cfb92d14SAndroid Build Coastguard Worker * 526*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 527*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 528*cfb92d14SAndroid Build Coastguard Worker * 529*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 530*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 531*cfb92d14SAndroid Build Coastguard Worker * 532*cfb92d14SAndroid Build Coastguard Worker * @param[in] aData A pointer to data buffer. 533*cfb92d14SAndroid Build Coastguard Worker * @param[in] aDataLen The length (number of bytes) in the data buffer. 534*cfb92d14SAndroid Build Coastguard Worker * 535*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given data to the frame. 536*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the byte. 537*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 538*cfb92d14SAndroid Build Coastguard Worker * 539*cfb92d14SAndroid Build Coastguard Worker */ 540*cfb92d14SAndroid Build Coastguard Worker otError WriteDataWithLen(const uint8_t *aData, uint16_t aDataLen); 541*cfb92d14SAndroid Build Coastguard Worker 542*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_SPINEL_CONFIG_OPENTHREAD_MESSAGE_ENABLE 543*cfb92d14SAndroid Build Coastguard Worker /** 544*cfb92d14SAndroid Build Coastguard Worker * Adds a message to the current input frame. 545*cfb92d14SAndroid Build Coastguard Worker * 546*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 547*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 548*cfb92d14SAndroid Build Coastguard Worker * 549*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the frame and return error status 550*cfb92d14SAndroid Build Coastguard Worker * `OT_ERROR_NO_BUFS`. 551*cfb92d14SAndroid Build Coastguard Worker * 552*cfb92d14SAndroid Build Coastguard Worker * The ownership of the passed-in message @p aMessage changes to underlying `Spinel::Buffer` ONLY when the entire 553*cfb92d14SAndroid Build Coastguard Worker * frame is successfully finished (i.e., with a successful call to `EndFrame()` for the current frame being 554*cfb92d14SAndroid Build Coastguard Worker * written), and in this case the `otMessage` instance will be freed once the frame is removed from the 555*cfb92d14SAndroid Build Coastguard Worker * `Spinel::Buffer`. However, if the frame gets discarded before it is finished (e.g., running out of buffer space), 556*cfb92d14SAndroid Build Coastguard Worker * the `otMessage` instance remains unchanged. 557*cfb92d14SAndroid Build Coastguard Worker * 558*cfb92d14SAndroid Build Coastguard Worker * @param[in] aMessage A message to be added to current frame. 559*cfb92d14SAndroid Build Coastguard Worker * 560*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added the message to the frame. 561*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the message. 562*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 563*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS If @p aMessage is nullptr. 564*cfb92d14SAndroid Build Coastguard Worker * 565*cfb92d14SAndroid Build Coastguard Worker */ WriteMessage(otMessage * aMessage)566*cfb92d14SAndroid Build Coastguard Worker otError WriteMessage(otMessage *aMessage) { return mNcpBuffer.InFrameFeedMessage(aMessage); } 567*cfb92d14SAndroid Build Coastguard Worker #endif 568*cfb92d14SAndroid Build Coastguard Worker 569*cfb92d14SAndroid Build Coastguard Worker /** 570*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a set of variables to the current input frame using a given spinel packing format 571*cfb92d14SAndroid Build Coastguard Worker * string. 572*cfb92d14SAndroid Build Coastguard Worker * 573*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 574*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 575*cfb92d14SAndroid Build Coastguard Worker * 576*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 577*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 578*cfb92d14SAndroid Build Coastguard Worker * 579*cfb92d14SAndroid Build Coastguard Worker * Note that the encoded buffer should fit in `kPackFormatBufferSize` bytes. 580*cfb92d14SAndroid Build Coastguard Worker * 581*cfb92d14SAndroid Build Coastguard Worker * @param[in] aPackFormat A string giving the spinel packing format. 582*cfb92d14SAndroid Build Coastguard Worker * @param[in] ... Variable arguments corresponding to the types given in @p aPackFormat (see 583*cfb92d14SAndroid Build Coastguard Worker * `spinel_datatype_pack()`). 584*cfb92d14SAndroid Build Coastguard Worker * 585*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given data to the frame. 586*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the byte. 587*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 588*cfb92d14SAndroid Build Coastguard Worker * 589*cfb92d14SAndroid Build Coastguard Worker */ 590*cfb92d14SAndroid Build Coastguard Worker otError WritePacked(const char *aPackFormat, ...); 591*cfb92d14SAndroid Build Coastguard Worker 592*cfb92d14SAndroid Build Coastguard Worker /** 593*cfb92d14SAndroid Build Coastguard Worker * Encodes and writes a set of variables to the current input frame using a given spinel packing format 594*cfb92d14SAndroid Build Coastguard Worker * string. 595*cfb92d14SAndroid Build Coastguard Worker * 596*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 597*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 598*cfb92d14SAndroid Build Coastguard Worker * 599*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the current input frame and return the 600*cfb92d14SAndroid Build Coastguard Worker * error status `OT_ERROR_NO_BUFS`. 601*cfb92d14SAndroid Build Coastguard Worker * 602*cfb92d14SAndroid Build Coastguard Worker * Note that the encoded buffer should fit in `kPackFormatBufferSize` bytes. 603*cfb92d14SAndroid Build Coastguard Worker * 604*cfb92d14SAndroid Build Coastguard Worker * @param[in] aPackFormat A string giving the spinel packing format. 605*cfb92d14SAndroid Build Coastguard Worker * @param[in] aArgs Variable arguments corresponding to the types given in @p aPackFormat (see 606*cfb92d14SAndroid Build Coastguard Worker * `spinel_datatype_pack()`). 607*cfb92d14SAndroid Build Coastguard Worker * 608*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully added given data to the frame. 609*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to add the byte. 610*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 611*cfb92d14SAndroid Build Coastguard Worker * 612*cfb92d14SAndroid Build Coastguard Worker */ 613*cfb92d14SAndroid Build Coastguard Worker otError WriteVPacked(const char *aPackFormat, va_list aArgs); 614*cfb92d14SAndroid Build Coastguard Worker 615*cfb92d14SAndroid Build Coastguard Worker /** 616*cfb92d14SAndroid Build Coastguard Worker * Opens a struct in the current input frame. 617*cfb92d14SAndroid Build Coastguard Worker * 618*cfb92d14SAndroid Build Coastguard Worker * After a successful call to this method, all the subsequent `Write<SomeType>()` methods add the field/value to 619*cfb92d14SAndroid Build Coastguard Worker * the current open struct until the struct is closed using `CloseStruct()` method. Structures can be nested. Up to 620*cfb92d14SAndroid Build Coastguard Worker * `kMaxNestedStructs` nested structs can be opened at the same time. 621*cfb92d14SAndroid Build Coastguard Worker * 622*cfb92d14SAndroid Build Coastguard Worker * Before using this method `BeginFrame()` must be called to start and prepare a new input frame. Otherwise, this 623*cfb92d14SAndroid Build Coastguard Worker * method does nothing and returns error status `OT_ERROR_INVALID_STATE`. 624*cfb92d14SAndroid Build Coastguard Worker * 625*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the frame and return error status 626*cfb92d14SAndroid Build Coastguard Worker * `OT_ERROR_NO_BUFS`. 627*cfb92d14SAndroid Build Coastguard Worker * 628*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully opened the struct. 629*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to open the struct. 630*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame or if we reached 631*cfb92d14SAndroid Build Coastguard Worker * the maximum number of nested open structures. 632*cfb92d14SAndroid Build Coastguard Worker * 633*cfb92d14SAndroid Build Coastguard Worker */ 634*cfb92d14SAndroid Build Coastguard Worker otError OpenStruct(void); 635*cfb92d14SAndroid Build Coastguard Worker 636*cfb92d14SAndroid Build Coastguard Worker /** 637*cfb92d14SAndroid Build Coastguard Worker * Closes the most recently opened struct (using `OpenStruct()`) in the current input frame. 638*cfb92d14SAndroid Build Coastguard Worker * 639*cfb92d14SAndroid Build Coastguard Worker * Each call to `CloseStruct()` must correspond to an earlier successfully opened struct. If a frame is ended using 640*cfb92d14SAndroid Build Coastguard Worker * `EndFrame()` with remaining open structs, the `EndFrame()` method will close all the remaining structs. 641*cfb92d14SAndroid Build Coastguard Worker * 642*cfb92d14SAndroid Build Coastguard Worker * If no buffer space is available, this method will discard and clear the frame and return error status 643*cfb92d14SAndroid Build Coastguard Worker * `OT_ERROR_NO_BUFS`. 644*cfb92d14SAndroid Build Coastguard Worker * 645*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully closed the most recently opened struct. 646*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to open the struct. 647*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame or if there is no 648*cfb92d14SAndroid Build Coastguard Worker * open struct to close 649*cfb92d14SAndroid Build Coastguard Worker */ 650*cfb92d14SAndroid Build Coastguard Worker otError CloseStruct(void); 651*cfb92d14SAndroid Build Coastguard Worker 652*cfb92d14SAndroid Build Coastguard Worker /** 653*cfb92d14SAndroid Build Coastguard Worker * Saves the current write position in the input frame. 654*cfb92d14SAndroid Build Coastguard Worker * 655*cfb92d14SAndroid Build Coastguard Worker * The saved position can later be used to discard a portion of written/encoded frame and move the write pointer 656*cfb92d14SAndroid Build Coastguard Worker * back to the saved position (using `ResetToSaved()`). 657*cfb92d14SAndroid Build Coastguard Worker * 658*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully saved current write position in @p aPosition. 659*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 660*cfb92d14SAndroid Build Coastguard Worker * 661*cfb92d14SAndroid Build Coastguard Worker */ 662*cfb92d14SAndroid Build Coastguard Worker otError SavePosition(void); 663*cfb92d14SAndroid Build Coastguard Worker 664*cfb92d14SAndroid Build Coastguard Worker /** 665*cfb92d14SAndroid Build Coastguard Worker * Resets the write position of input frame back to a previously saved position. Any added content 666*cfb92d14SAndroid Build Coastguard Worker * after the write position is discarded. 667*cfb92d14SAndroid Build Coastguard Worker * 668*cfb92d14SAndroid Build Coastguard Worker * The saved position must belong to the same input frame saved earlier with `SavePosition()`. This method cannot 669*cfb92d14SAndroid Build Coastguard Worker * be used if the input frame has an added `otMessage`. 670*cfb92d14SAndroid Build Coastguard Worker * 671*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully reset the write position of current input frame. 672*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_STATE `BeginFrame()` has not been called earlier to start the frame. 673*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The saved position is not valid (does not belong to same input frame), or 674*cfb92d14SAndroid Build Coastguard Worker * the input frame has an added `otMessage`. 675*cfb92d14SAndroid Build Coastguard Worker * 676*cfb92d14SAndroid Build Coastguard Worker */ 677*cfb92d14SAndroid Build Coastguard Worker otError ResetToSaved(void); 678*cfb92d14SAndroid Build Coastguard Worker 679*cfb92d14SAndroid Build Coastguard Worker /** 680*cfb92d14SAndroid Build Coastguard Worker * Clear NCP buffer on reset command. 681*cfb92d14SAndroid Build Coastguard Worker * 682*cfb92d14SAndroid Build Coastguard Worker */ 683*cfb92d14SAndroid Build Coastguard Worker void ClearNcpBuffer(void); 684*cfb92d14SAndroid Build Coastguard Worker 685*cfb92d14SAndroid Build Coastguard Worker private: 686*cfb92d14SAndroid Build Coastguard Worker enum 687*cfb92d14SAndroid Build Coastguard Worker { 688*cfb92d14SAndroid Build Coastguard Worker kPackFormatBufferSize = 96, ///< Size of buffer used when encoding using `WritePacked()` or `WriteVPacked()`. 689*cfb92d14SAndroid Build Coastguard Worker kMaxNestedStructs = 4, ///< Maximum number of nested structs. 690*cfb92d14SAndroid Build Coastguard Worker }; 691*cfb92d14SAndroid Build Coastguard Worker 692*cfb92d14SAndroid Build Coastguard Worker Spinel::Buffer &mNcpBuffer; 693*cfb92d14SAndroid Build Coastguard Worker Spinel::Buffer::WritePosition mStructPosition[kMaxNestedStructs]; 694*cfb92d14SAndroid Build Coastguard Worker uint8_t mNumOpenStructs; 695*cfb92d14SAndroid Build Coastguard Worker 696*cfb92d14SAndroid Build Coastguard Worker uint8_t mSavedNumOpenStructs; 697*cfb92d14SAndroid Build Coastguard Worker Spinel::Buffer::WritePosition mSavedPosition; 698*cfb92d14SAndroid Build Coastguard Worker }; 699*cfb92d14SAndroid Build Coastguard Worker 700*cfb92d14SAndroid Build Coastguard Worker } // namespace Spinel 701*cfb92d14SAndroid Build Coastguard Worker } // namespace ot 702*cfb92d14SAndroid Build Coastguard Worker 703*cfb92d14SAndroid Build Coastguard Worker #endif // SPINEL_ENCODER_HPP_ 704