blob: 6e56633ec307f7f34a790c2c8dd72cc1f7e20eef [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/**
2 *
3 * @file ringbuf.c
4 * @brief
5 * This file is part of tools.
6 * ring buffer
7 *
8 * @details
9 * @author Tools Team.
10 * @email
11 * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
12 * @warning
13 * @date 2019/02/02
14 * @version 1.1
15 * @pre
16 * @post
17 *
18 * @par
19 * Change History :
20 * ---------------------------------------------------------------------------
21 * date version author description
22 * ---------------------------------------------------------------------------
23 * 2013/01/21 1.0 lu.xieji Create file
24 * 2019/02/02 1.1 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
25 * ---------------------------------------------------------------------------
26 *
27 *
28 */
29
30#include "ringbuf.h"
31#include <linux/uaccess.h>
32
33#define SYMBOL_VALUE ZCAT_IPC_ESTABLISHED
34
35/**
36 * @brief Åжϻº³åÇøÊÇ·ñÓÐЧ
37 * @param[in] ringBuf »·Ðλº³å
38 * @return ÊÇ·µ»ØTRUE,·ñ·µ»ØFALSE
39 * @note
40 * @see
41 */
42BOOL IsRingBufferValid(T_RINGBUFFER *ringBuf)
43{
44 return (ringBuf != NULL && ringBuf->symbol == SYMBOL_VALUE);
45}
46
47/**
48 * @brief ´´½¨ÐèÒªµÄ»º³åÇø,²¢ÇÒÖ¸¶¨±ê¼ÇλµÄÖµ
49 * Èç¹ûbuf²»ÎªNULL£¬ÔòÔÚÖ¸¶¨µØÖ·´´½¨»º³åÇø£»·ñÔò£¬ÉêÇëÒ»¿éÄÚ´æ×÷Ϊ»º³åÇø
50 * @param[in] buf »º³åÇøµØÖ·
51 * @param[in] bufSize »º³åÇø´óС
52 * @param[in] bufSize ±ê¼Çλ
53 * @return ³É¹¦·µ»ØÖ¸Ïò»º³åÇø½á¹¹ÌåµÄÖ¸Õ룬·ñÔò·µ»ØNULL
54 * @note
55 * @see
56 */
57T_RINGBUFFER *CreateRingBufferWithSymbol(UINT8 *buf, UINT32 bufSize, UINT32 symbol)
58{
59 T_RINGBUFFER *ringBuf = NULL;
60
61 if (bufSize <= sizeof(T_RINGBUFFER))
62 {
63 return NULL;
64 }
65
66 if (buf == NULL)
67 {
68 //ringBuf = (T_RINGBUFFER *)kmalloc(ALIGNED_SIZE(bufSize, 3), GFP_KERNEL);
69
70 //if (ringBuf == NULL)
71 {
72 return NULL;
73 }
74 }
75 else
76 {
77 ringBuf = (T_RINGBUFFER *)buf;
78 }
79
80 ringBuf->capacity = bufSize - sizeof(T_RINGBUFFER);
81 ringBuf->readPoint = 0;
82 ringBuf->writePoint = 0;
83 ringBuf->symbol = symbol;
84
85 return ringBuf;
86}
87
88
89/**
90 * @brief ´´½¨ÐèÒªµÄ»º³åÇø
91 * Èç¹ûbuf²»ÎªNULL£¬ÔòÔÚÖ¸¶¨µØÖ·´´½¨»º³åÇø£»·ñÔò£¬ÉêÇëÒ»¿éÄÚ´æ×÷Ϊ»º³åÇø
92 * @param[in] buf »º³åÇøµØÖ·
93 * @param[in] bufSize »º³åÇø´óС
94 * @return ³É¹¦·µ»ØÖ¸Ïò»º³åÇø½á¹¹ÌåµÄÖ¸Õ룬·ñÔò·µ»ØNULL
95 * @note
96 * @see
97 */
98T_RINGBUFFER *CreateRingBuffer(UINT8 *buf, UINT32 bufSize)
99{
100 T_RINGBUFFER *ringBuf = NULL;
101
102 if (bufSize <= sizeof(T_RINGBUFFER))
103 {
104 return NULL;
105 }
106
107 if (buf == NULL)
108 {
109 //ringBuf = (T_RINGBUFFER *)kmalloc(ALIGNED_SIZE(bufSize, 3), GFP_KERNEL);
110
111 //if (ringBuf == NULL)
112 {
113 return NULL;
114 }
115 }
116 else
117 {
118 ringBuf = (T_RINGBUFFER *)buf;
119 }
120
121 ringBuf->capacity = bufSize - sizeof(T_RINGBUFFER);
122 ringBuf->readPoint = 0;
123 ringBuf->writePoint = 0;
124 ringBuf->symbol = SYMBOL_VALUE;
125
126 return ringBuf;
127}
128
129
130/**
131* @brief ½«Êý¾ÝдÈë»·Ðλº³å
132* @param[in] ringBuf »·Ðλº³åÖ¸Õë
133* @param[in] buf Êý¾Ý»º´æÖ¸Õë
134* @param[in] len Êý¾Ý³¤¶È
135* @param[in] flags Êý¾Ý»º´æÄÚ´æÀàÐÍ£º0:ÄÚºË̬,1:Óû§Ì¬
136* @return ·µ»ØÐ´ÈëµÄʵ¼Ê³¤¶È
137* @note
138* @see
139*/
140UINT32 WriteRingBuffer(T_RINGBUFFER *ringBuf, UINT8 *buf, UINT32 len, UINT32 flags)
141{
142 UINT32 writeLen = 0;
143 UINT32 writePoint = ringBuf->writePoint;
144 UINT32 readPoint = ringBuf->readPoint;
145
146 /* Èë²ÎÅÐ¶Ï */
147 if (!IsRingBufferValid(ringBuf) || buf == NULL || len == 0)
148 {
149 return 0;
150 }
151 if( writePoint >= ringBuf->capacity || readPoint >= ringBuf->capacity )
152 {
153 return 0;
154 }
155
156 if (writePoint >= readPoint)
157 {
158 if (ringBuf->capacity - writePoint > len)
159 {
160 if (ZCAT_MEM_TYPE_USER == flags) { copy_from_user(ringBuf->buf + writePoint, buf, len); }
161 else { memcpy(ringBuf->buf + writePoint, buf, len); }
162 ringBuf->writePoint += len;
163 writeLen = len;
164 }
165 else if ((ringBuf->capacity - writePoint + readPoint - 1) >= len)
166 {
167 UINT32 copyLen = ringBuf->capacity - writePoint;
168 if (ZCAT_MEM_TYPE_USER == flags) { copy_from_user(ringBuf->buf + writePoint, buf, copyLen); }
169 else { memcpy(ringBuf->buf + writePoint, buf, copyLen); }
170
171 copyLen = len - copyLen;
172
173 if (copyLen > 0)
174 {
175 if (ZCAT_MEM_TYPE_USER == flags) { copy_from_user(ringBuf->buf, buf + (len - copyLen), copyLen); }
176 else { memcpy(ringBuf->buf, buf + (len - copyLen), copyLen); }
177 }
178
179 ringBuf->writePoint = copyLen;
180 writeLen = len;
181 }
182 }
183 else
184 {
185 if (readPoint - writePoint - 1 >= len)
186 {
187 if (ZCAT_MEM_TYPE_USER == flags) { copy_from_user(ringBuf->buf + writePoint, buf, len); }
188 else { memcpy(ringBuf->buf + writePoint, buf, len); }
189 ringBuf->writePoint += len;
190 writeLen = len;
191 }
192 }
193
194 return writeLen;
195}
196
197/**
198* @brief ¶Áȡһ¶Î»·Ðλº³åÇøµÄÊý¾Ýµ½Ä¿±ê»º´æ
199* @param[in] ringBuf »·Ðλº³åÇøÖ¸Õë
200* @param[in] buf Ä¿±ê»º´æÖ¸Õë
201* @param[in] len Ä¿±ê»º´æ³¤¶È
202* @param[in] flags Ä¿±ê»º´æÄÚ´æÀàÐÍ£º0:ÄÚºË̬,1:Óû§Ì¬
203* @return ·µ»Ø¶ÁÈ¡³¤¶È
204* @note
205* @see
206*/
207UINT32 ReadRingBuffer(T_RINGBUFFER *ringBuf, UINT8 *buf, UINT32 len, UINT32 flags)
208{
209 UINT32 readLen = 0;
210 UINT32 writePoint = ringBuf->writePoint;
211 UINT32 readPoint = ringBuf->readPoint;
212
213 /* ²ÎÊý¼ì²é */
214 if (!IsRingBufferValid(ringBuf) || buf == NULL || len == 0)
215 {
216 return 0;
217 }
218 if( writePoint >= ringBuf->capacity || readPoint >= ringBuf->capacity )
219 {
220 return 0;
221 }
222 if (writePoint > readPoint)
223 {
224 readLen = writePoint - readPoint;
225 readLen = (readLen >= len) ? len : readLen;
226
227 if (ZCAT_MEM_TYPE_USER == flags) { copy_to_user(buf, ringBuf->buf + readPoint, readLen); }
228 else { memcpy(buf, ringBuf->buf + readPoint, readLen); }
229 ringBuf->readPoint += readLen;
230 }
231 else if (writePoint < readPoint)
232 {
233 readLen = ringBuf->capacity - readPoint + writePoint;
234 readLen = (readLen >= len) ? len : readLen;
235
236 if (ringBuf->capacity - readPoint >= readLen)
237 {
238 if (ZCAT_MEM_TYPE_USER == flags) { copy_to_user(buf, ringBuf->buf + readPoint, readLen); }
239 else { memcpy(buf, ringBuf->buf + readPoint, readLen); }
240 ringBuf->readPoint = (ringBuf->readPoint + readLen) % ringBuf->capacity;
241 }
242 else
243 {
244 UINT32 copyLen = ringBuf->capacity - readPoint;
245 if (ZCAT_MEM_TYPE_USER == flags) { copy_to_user(buf, ringBuf->buf + readPoint, copyLen); }
246 else { memcpy(buf, ringBuf->buf + readPoint, copyLen); }
247
248 copyLen = readLen - copyLen;
249 if (ZCAT_MEM_TYPE_USER == flags) { copy_to_user(buf + (readLen - copyLen), ringBuf->buf, copyLen); }
250 else { memcpy(buf + (readLen - copyLen), ringBuf->buf, copyLen);}
251
252 ringBuf->readPoint = copyLen;
253 }
254 }
255
256 return readLen;
257}
258
259/**
260* @brief »ñÈ¡µ±Ç°Ring BufferÖпÕÏÐÇøµÄ´óС
261* @param[in] ringBuf »·Ðλº³åÇøÖ¸Õë
262* @return ·µ»Ø×Ö½ÚÊý
263* @note
264* @see
265*/
266UINT32 GetRingBufferSize(T_RINGBUFFER *ringBuf)
267{
268 UINT32 writePoint = ringBuf->writePoint;
269 UINT32 readPoint = ringBuf->readPoint;
270 UINT32 bufSize = 0;
271
272 if (!IsRingBufferValid(ringBuf))
273 {
274 return 0;
275 }
276
277 if (writePoint > readPoint)
278 {
279 bufSize = writePoint - readPoint;
280 }
281 else if (writePoint < readPoint)
282 {
283 bufSize = ringBuf->capacity - readPoint + writePoint;
284 }
285
286 return bufSize;
287}
288
289/**
290* @brief ½«»º³åÇøÖÿÕ
291* @param[in] ringBuf »·Ðλº³åÇøÖ¸Õë
292* @return void
293* @note
294* @see
295*/
296VOID EmptyRingBuffer(T_RINGBUFFER *ringBuf)
297{
298 if (IsRingBufferValid(ringBuf))
299 {
300 ringBuf->readPoint = ringBuf->writePoint;
301 }
302}
303