1 /* 2 * Copyright (c) 2021, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for `Heap::Data` (heap allocated data). 32 */ 33 34 #ifndef HEAP_DATA_HPP_ 35 #define HEAP_DATA_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include "common/data.hpp" 40 #include "common/heap.hpp" 41 #include "common/message.hpp" 42 43 namespace ot { 44 namespace Heap { 45 46 /** 47 * Represents a heap allocated data. 48 */ 49 class Data 50 { 51 public: 52 /** 53 * Initializes the `Heap::Data` as empty. 54 */ Data(void)55 Data(void) { mData.Init(nullptr, 0); } 56 57 /** 58 * This is the move constructor for `Heap::Data`. 59 * 60 * `Heap::Data` is non-copyable (copy constructor is deleted) but move constructor is provided to allow it to to be 61 * used as return type (return by value) from functions/methods (which will then use move semantics). 62 * 63 * @param[in] aData An rvalue reference to another `Heap::Data` to move from. 64 */ Data(Data && aData)65 Data(Data &&aData) { TakeFrom(aData); } 66 67 /** 68 * This is the destructor for `Heap::Data` object. 69 */ ~Data(void)70 ~Data(void) { Free(); } 71 72 /** 73 * Indicates whether or not the `Heap::Data` is null (i.e., it was never successfully set or it was 74 * freed). 75 * 76 * @retval TRUE The `Heap::Data` is null. 77 * @retval FALSE The `Heap::Data` is not null. 78 */ IsNull(void) const79 bool IsNull(void) const { return (mData.GetBytes() == nullptr); } 80 81 /** 82 * Returns a pointer to the `Heap::Data` bytes buffer. 83 * 84 * @returns A pointer to data buffer or `nullptr` if the `Heap::Data` is null (never set or freed). 85 */ GetBytes(void) const86 const uint8_t *GetBytes(void) const { return mData.GetBytes(); } 87 88 /** 89 * Returns the `Heap::Data` length. 90 * 91 * @returns The data length (number of bytes) or zero if the `HeapData` is null. 92 */ GetLength(void) const93 uint16_t GetLength(void) const { return mData.GetLength(); } 94 95 /** 96 * Sets the `Heap::Data` from the content of a given buffer. 97 * 98 * @param[in] aBuffer The buffer to copy bytes from. 99 * @param[in] aLength The buffer length (number of bytes). 100 * 101 * @retval kErrorNone Successfully set the `Heap::Data`. 102 * @retval kErrorNoBufs Failed to allocate buffer. 103 */ 104 Error SetFrom(const uint8_t *aBuffer, uint16_t aLength); 105 106 /** 107 * Sets the `Heap::Data` from the content of a given message. 108 * 109 * The bytes are copied from current offset in @p aMessage till the end of the message. 110 * 111 * @param[in] aMessage The message to copy bytes from (starting from offset till the end of message). 112 * 113 * @retval kErrorNone Successfully set the `Heap::Data`. 114 * @retval kErrorNoBufs Failed to allocate buffer. 115 */ 116 Error SetFrom(const Message &aMessage); 117 118 /** 119 * Sets the `Heap::Data` from the content of a given message read at a given offset up to a given 120 * length. 121 * 122 * @param[in] aMessage The message to read and copy bytes from. 123 * @param[in] aOffset The offset into the message to start reading the bytes from. 124 * @param[in] aLength The number of bytes to read from message. If @p aMessage contains fewer bytes than 125 * requested, then `Heap::Data` is cleared and this method returns `kErrorParse`. 126 * 127 * @retval kErrorNone Successfully set the `Heap::Data`. 128 * @retval kErrorNoBufs Failed to allocate buffer. 129 * @retval kErrorParse Not enough bytes in @p aMessage to read the requested @p aLength bytes. 130 */ 131 Error SetFrom(const Message &aMessage, uint16_t aOffset, uint16_t aLength); 132 133 /** 134 * Sets the `Heap::Data` from another one (move semantics). 135 * 136 * @param[in] aData The other `Heap::Data` to set from (rvalue reference). 137 */ 138 void SetFrom(Data &&aData); 139 140 /** 141 * Appends the bytes from `Heap::Data` to a given message. 142 * 143 * @param[in] aMessage The message to append the bytes into. 144 * 145 * @retval kErrorNone Successfully copied the bytes from `Heap::Data` into @p aMessage. 146 * @retval kErrorNoBufs Failed to allocate buffer. 147 */ CopyBytesTo(Message & aMessage) const148 Error CopyBytesTo(Message &aMessage) const { return aMessage.AppendBytes(mData.GetBytes(), mData.GetLength()); } 149 150 /** 151 * Copies the bytes from `Heap::Data` into a given buffer. 152 * 153 * It is up to the caller to ensure that @p aBuffer has enough space for the current data length. 154 * 155 * @param[in] aBuffer A pointer to buffer to copy the bytes into. 156 */ CopyBytesTo(uint8_t * aBuffer) const157 void CopyBytesTo(uint8_t *aBuffer) const { return mData.CopyBytesTo(aBuffer); } 158 159 /** 160 * Compares the `Data` content with the bytes from a given buffer. 161 * 162 * @param[in] aBuffer A pointer to a buffer to compare with the data. 163 * @param[in] aLength The length of @p aBuffer. 164 * 165 * @retval TRUE The `Data` content matches the bytes in @p aBuffer. 166 * @retval FALSE The `Data` content does not match the byes in @p aBuffer. 167 */ 168 bool Matches(const uint8_t *aBuffer, uint16_t aLength) const; 169 170 /** 171 * Overloads operator `==` to compare the `Data` content with the content from another one. 172 * 173 * @param[in] aOtherData The other `Data` to compare with. 174 * 175 * @retval TRUE The two `Data` instances have matching content (same length and same bytes). 176 * @retval FALSE The two `Data` instances do not have matching content. 177 */ operator ==(const Data & aOtherData) const178 bool operator==(const Data &aOtherData) const { return mData == aOtherData.mData; } 179 180 /** 181 * Frees any buffer allocated by the `Heap::Data`. 182 * 183 * The `Heap::Data` destructor will automatically call `Free()`. This method allows caller to free the buffer 184 * explicitly. 185 */ 186 void Free(void); 187 188 Data(const Data &) = delete; 189 Data &operator=(const Data &) = delete; 190 191 private: 192 Error UpdateBuffer(uint16_t aNewLength); 193 void TakeFrom(Data &aData); 194 195 MutableData<kWithUint16Length> mData; 196 }; 197 198 } // namespace Heap 199 } // namespace ot 200 201 #endif // HEAP_DATA_HPP_ 202