1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "los_cir_buf.h"
33 #include "los_spinlock.h"
34
35
LOS_CirBufUsedSize(CirBuf * cirbufCB)36 UINT32 LOS_CirBufUsedSize(CirBuf *cirbufCB)
37 {
38 UINT32 size;
39 UINT32 intSave;
40
41 LOS_SpinLockSave(&cirbufCB->lock, &intSave);
42 size = cirbufCB->size - cirbufCB->remain;
43 LOS_SpinUnlockRestore(&cirbufCB->lock, intSave);
44
45 return size;
46 }
47
48 /*
49 * startIdx
50 * |
51 * 0 0 0 0 0 0 0 0 X X X X X X X X 0 0 0 0 0 0
52 * |
53 * endIdx
54 */
OsCirBufWriteLinear(CirBuf * cirbufCB,const CHAR * buf,UINT32 size)55 STATIC UINT32 OsCirBufWriteLinear(CirBuf *cirbufCB, const CHAR *buf, UINT32 size)
56 {
57 UINT32 cpSize;
58 errno_t err;
59
60 cpSize = (cirbufCB->remain < size) ? cirbufCB->remain : size;
61
62 if (cpSize == 0) {
63 return 0;
64 }
65
66 err = memcpy_s(cirbufCB->fifo + cirbufCB->endIdx, cirbufCB->remain, buf, cpSize);
67 if (err != EOK) {
68 return 0;
69 }
70
71 cirbufCB->remain -= cpSize;
72 cirbufCB->endIdx += cpSize;
73
74 return cpSize;
75 }
76
OsCirBufWriteLoop(CirBuf * cirbufCB,const CHAR * buf,UINT32 size)77 STATIC UINT32 OsCirBufWriteLoop(CirBuf *cirbufCB, const CHAR *buf, UINT32 size)
78 {
79 UINT32 right, cpSize;
80 errno_t err;
81
82 right = cirbufCB->size - cirbufCB->endIdx;
83 cpSize = (right < size) ? right : size;
84
85 err = memcpy_s(cirbufCB->fifo + cirbufCB->endIdx, right, buf, cpSize);
86 if (err != EOK) {
87 return 0;
88 }
89
90 cirbufCB->remain -= cpSize;
91 cirbufCB->endIdx += cpSize;
92 if (cirbufCB->endIdx == cirbufCB->size) {
93 cirbufCB->endIdx = 0;
94 }
95
96 if (cpSize == size) {
97 return size;
98 } else {
99 cpSize += OsCirBufWriteLinear(cirbufCB, buf + cpSize, size - cpSize);
100 }
101
102 return cpSize;
103 }
104
LOS_CirBufWrite(CirBuf * cirbufCB,const CHAR * buf,UINT32 size)105 UINT32 LOS_CirBufWrite(CirBuf *cirbufCB, const CHAR *buf, UINT32 size)
106 {
107 UINT32 cpSize = 0;
108 UINT32 intSave;
109
110 if ((cirbufCB == NULL) || (buf == NULL) || (size == 0) || (cirbufCB->status != CBUF_USED)) {
111 return 0;
112 }
113
114 LOS_SpinLockSave(&cirbufCB->lock, &intSave);
115
116 if ((cirbufCB->fifo == NULL) || (cirbufCB->remain == 0)) {
117 goto EXIT;;
118 }
119
120 if (cirbufCB->startIdx <= cirbufCB->endIdx) {
121 cpSize = OsCirBufWriteLoop(cirbufCB, buf, size);
122 } else {
123 cpSize = OsCirBufWriteLinear(cirbufCB, buf, size);
124 }
125
126 EXIT:
127 LOS_SpinUnlockRestore(&cirbufCB->lock, intSave);
128 return cpSize;
129 }
130
OsCirBufReadLinear(CirBuf * cirbufCB,CHAR * buf,UINT32 size)131 STATIC UINT32 OsCirBufReadLinear(CirBuf *cirbufCB, CHAR *buf, UINT32 size)
132 {
133 UINT32 cpSize, remain;
134 errno_t err;
135
136 remain = cirbufCB->endIdx - cirbufCB->startIdx;
137 cpSize = (remain < size) ? remain : size;
138
139 if (cpSize == 0) {
140 return 0;
141 }
142
143 err = memcpy_s(buf, size, cirbufCB->fifo + cirbufCB->startIdx, cpSize);
144 if (err != EOK) {
145 return 0;
146 }
147
148 cirbufCB->remain += cpSize;
149 cirbufCB->startIdx += cpSize;
150
151 return cpSize;
152 }
153
OsCirBufReadLoop(CirBuf * cirbufCB,CHAR * buf,UINT32 size)154 STATIC UINT32 OsCirBufReadLoop(CirBuf *cirbufCB, CHAR *buf, UINT32 size)
155 {
156 UINT32 right, cpSize;
157 errno_t err;
158
159 right = cirbufCB->size - cirbufCB->startIdx;
160 cpSize = (right < size) ? right : size;
161
162 err = memcpy_s(buf, size, cirbufCB->fifo + cirbufCB->startIdx, cpSize);
163 if (err != EOK) {
164 return 0;
165 }
166
167 cirbufCB->remain += cpSize;
168 cirbufCB->startIdx += cpSize;
169 if (cirbufCB->startIdx == cirbufCB->size) {
170 cirbufCB->startIdx = 0;
171 }
172
173 if (cpSize < size) {
174 cpSize += OsCirBufReadLinear(cirbufCB, buf + cpSize, size - cpSize);
175 }
176
177 return cpSize;
178 }
179
LOS_CirBufRead(CirBuf * cirbufCB,CHAR * buf,UINT32 size)180 UINT32 LOS_CirBufRead(CirBuf *cirbufCB, CHAR *buf, UINT32 size)
181 {
182 UINT32 cpSize = 0;
183 UINT32 intSave;
184
185 if ((cirbufCB == NULL) || (buf == NULL) || (size == 0) || (cirbufCB->status != CBUF_USED)) {
186 return 0;
187 }
188
189 LOS_SpinLockSave(&cirbufCB->lock, &intSave);
190
191 if ((cirbufCB->fifo == NULL) || (cirbufCB->remain == cirbufCB->size)) {
192 goto EXIT;
193 }
194
195 if (cirbufCB->startIdx >= cirbufCB->endIdx) {
196 cpSize = OsCirBufReadLoop(cirbufCB, buf, size);
197 } else {
198 cpSize = OsCirBufReadLinear(cirbufCB, buf, size);
199 }
200
201 EXIT:
202 LOS_SpinUnlockRestore(&cirbufCB->lock, intSave);
203 return cpSize;
204 }
205
LOS_CirBufInit(CirBuf * cirbufCB,CHAR * fifo,UINT32 size)206 UINT32 LOS_CirBufInit(CirBuf *cirbufCB, CHAR *fifo, UINT32 size)
207 {
208 if ((cirbufCB == NULL) || (fifo == NULL)) {
209 return LOS_NOK;
210 }
211
212 (VOID)memset_s(cirbufCB, sizeof(CirBuf), 0, sizeof(CirBuf));
213 LOS_SpinInit(&cirbufCB->lock);
214 cirbufCB->size = size;
215 cirbufCB->remain = size;
216 cirbufCB->status = CBUF_USED;
217 cirbufCB->fifo = fifo;
218
219 return LOS_OK;
220 }
221
LOS_CirBufDeinit(CirBuf * cirbufCB)222 VOID LOS_CirBufDeinit(CirBuf *cirbufCB)
223 {
224 (VOID)memset_s(cirbufCB, sizeof(CirBuf), 0, sizeof(CirBuf));
225 }
226
227