1 /* 2 * Copyright (c) 2018-2019, Texas Instruments Incorporated 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 7 * are met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of Texas Instruments Incorporated nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef ti_drivers_utils_StructRingBuf__include 34 #define ti_drivers_utils_StructRingBuf__include 35 36 #include <stdint.h> 37 #include <stddef.h> 38 #include <stdbool.h> 39 #include <string.h> 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 typedef struct { 46 uint8_t *buffer; 47 size_t length; 48 size_t count; 49 size_t head; 50 size_t tail; 51 size_t maxCount; 52 size_t structSize; 53 } StructRingBuf_Object, *StructRingBuf_Handle; 54 55 /*! 56 * @brief Initialize circular buffer 57 * 58 * @param object Pointer to a StructRingBuf Object that contains the member variables to 59 * operate a circular buffer. 60 * 61 * @param bufPtr Pointer to data buffer to be used for the circular buffer. The 62 * buffer is NOT stored in StructRingBuf_Object. 63 * 64 * @param bufSize The size of bufPtr in number of structures 65 * 66 * @param structSize The size of a member structure in bytes 67 */ 68 void StructRingBuf_construct(StructRingBuf_Handle object, void *bufPtr, 69 size_t bufSize, size_t structSize); 70 71 /*! 72 * @brief Get an unsigned char from the end of the circular buffer and remove 73 * it. 74 * 75 * @param object Pointer to a StructRingBuf Object that contains the member variables to 76 * operate a circular buffer. 77 * 78 * @param data Pointer to an unsigned char to be filled with the data from the 79 * front of the circular buffer. 80 * 81 * @return Number of unsigned chars on the buffer after taking it out of the 82 * circular buffer. If it returns -1, the circular buffer was already 83 * empty and data is invalid. 84 */ 85 int StructRingBuf_get(StructRingBuf_Handle object, void *data); 86 87 /*! 88 * @brief Get the number of unsigned chars currently stored on the circular 89 * buffer. 90 * 91 * @param object Pointer to a StructRingBuf Object that contains the member variables to 92 * operate a circular buffer. 93 * 94 * @return Number of unsigned chars on the circular buffer. 95 */ 96 int StructRingBuf_getCount(StructRingBuf_Handle object); 97 98 /*! 99 * @brief Function to determine if the circular buffer is full or not. 100 * 101 * @param object Pointer to a StructRingBuf Object that contains the member variables to 102 * operate a circular buffer. 103 * 104 * @return true if circular buffer is full, else false. 105 */ 106 bool StructRingBuf_isFull(StructRingBuf_Handle object); 107 108 /*! 109 * @brief A high-water mark indicating the largest number of unsigned chars 110 * stored on the circular buffer since it was constructed. 111 * 112 * @return Get the largest number of unsigned chars that were at one point in 113 * the circular buffer. 114 */ 115 int StructRingBuf_getMaxCount(StructRingBuf_Handle object); 116 117 /*! 118 * @brief Get an unsigned char from the end of the circular buffer without 119 * remove it. 120 * 121 * @param object Pointer to a StructRingBuf Object that contains the member variables to 122 * operate a circular buffer. 123 * 124 * @param data Pointer to an unsigned char to be filled with the data from the 125 * front of the circular buffer. This function does not remove the data 126 * from the circular buffer. Do not evaluate data if the count returned 127 * is equal to 0. 128 * 129 * @return Number of unsigned chars on the circular buffer. If the number != 0, 130 * then data will contain the unsigned char at the end of the circular 131 * buffer. 132 */ 133 int StructRingBuf_peek(StructRingBuf_Handle object, void **data); 134 135 /*! 136 * @brief Put an unsigned char into the end of the circular buffer. 137 * 138 * @param object Pointer to a StructRingBuf Object that contains the member variables to 139 * operate a circular buffer. 140 * 141 * @param data unsigned char to be placed at the end of the circular buffer. 142 * 143 * @return Number of unsigned chars on the buffer after it was added, or -1 if 144 * it's already full. 145 */ 146 int StructRingBuf_put(StructRingBuf_Handle object, const void *data); 147 148 #ifdef __cplusplus 149 } 150 #endif 151 152 #endif /* ti_drivers_utils_StructRingBuf__include */ 153