• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  *  ring_buffer.h
17  */
18 
19 #ifndef RING_BUFFER_H
20 #define RING_BUFFER_H
21 
22 #if defined __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <stdint.h>
27 
28 enum {
29     RB_ERR_NONE = 0,
30     RB_ERR_NO_ENOUGH_SPACE,
31     RB_ERR_NO_ENOUGH_DATA,
32     RB_ERR_INVALID_PARAM,
33 };
34 
35 typedef struct cs_ring_buffer {
36     uint8_t  *storage;
37     uint32_t size;
38     uint32_t data_size;
39     uint32_t read;
40     uint32_t write;
41 } ring_buffer_t;
42 
43 /**
44  * Init ring buffer
45  * @param ring_buffer object
46  * @param storage
47  * @param size in bytes
48  * @return RB_ERR_NONE if all params valid
49  */
50 int ring_buffer_init(ring_buffer_t *ring_buffer, uint8_t *storage, uint32_t size);
51 
52 /**
53  * Deinit ring buffer
54  * @param ring_buffer object
55  * @return RB_ERR_NONE if all params valid
56  */
57 int ring_buffer_deinit(ring_buffer_t *ring_buffer);
58 
59 /**
60  * Reset ring buffer
61  * @param ring_buffer object
62  * @return RB_ERR_NONE if all params valid
63  */
64 int ring_buffer_reset(ring_buffer_t *ring_buffer);
65 
66 /**
67  * Check if ring buffer is empty
68  * @param ring_buffer object
69  * @return TRUE if empty
70  */
71 int ring_buffer_empty(ring_buffer_t *ring_buffer);
72 
73 /**
74  * Get number of bytes available for read
75  * @param ring_buffer object
76  * @return number of bytes available for read
77  */
78 uint32_t ring_buffer_bytes_used(ring_buffer_t *ring_buffer);
79 
80 /**
81  * Get free space available for write
82  * @param ring_buffer object
83  * @return number of bytes available for write
84  */
85 uint32_t ring_buffer_bytes_free(ring_buffer_t *ring_buffer);
86 
87 /**
88  * Write bytes into ring buffer
89  * @param ring_buffer object
90  * @param data to store
91  * @param data length
92  * @return RB_ERR_NONE if ok, RB_ERR_NO_ENOUGH_SPACE if not enough space in buffer
93  */
94 int ring_buffer_write(ring_buffer_t *ring_buffer, uint8_t *data, uint32_t length);
95 
96 /**
97  * Read from ring buffer
98  * @param ring_buffer object
99  * @param buffer to store read data
100  * @param length to read
101  * @return RB_ERR_NONE if ok, RB_ERR_NO_ENOUGH_DATA if not enough data in buffer
102  */
103 int ring_buffer_read(ring_buffer_t *ring_buffer, uint8_t *buffer, uint32_t length);
104 
105 /**
106  * Read from ring buffer
107  * @param ring_buffer object
108  * @param length to peek
109  * @param data of 1st trunk
110  * @param length of 1st trunk
111  * @param data of 2nd trunk
112  * @param length of 2nd trunk
113  * @return RB_ERR_NONE if ok, RB_ERR_NO_ENOUGH_DATA if not enough data in buffer
114  */
115 int ring_buffer_peek(ring_buffer_t * ring_buffer, uint32_t length, uint8_t **data1, uint32_t *length1, uint8_t **data2, uint32_t *length2);
116 
117 #if defined __cplusplus
118 }
119 #endif
120 
121 #endif // RING_BUFFER_H
122