xref: /btstack/src/btstack_ring_buffer.h (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * @title Ring Buffer
40  *
41  */
42 
43 #ifndef BTSTACK_RING_BUFFER_H
44 #define BTSTACK_RING_BUFFER_H
45 
46 #if defined __cplusplus
47 extern "C" {
48 #endif
49 
50 #include <stdint.h>
51 
52 typedef struct btstack_ring_buffer {
53     uint8_t  * storage;
54     uint32_t size;
55     uint32_t last_read_index;
56     uint32_t last_written_index;
57     uint8_t  full;
58 } btstack_ring_buffer_t;
59 
60 /* API_START */
61 
62 /**
63  * Init ring buffer
64  * @param ring_buffer object
65  * @param storage
66  * @param storage_size in bytes
67  */
68 void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint32_t storage_size);
69 
70 /**
71  * Reset ring buffer to initial state (empty)
72  * @param ring_buffer object
73  */
74 void btstack_ring_buffer_reset(btstack_ring_buffer_t * ring_buffer);
75 
76 /**
77  * Check if ring buffer is empty
78  * @param ring_buffer object
79  * @return TRUE if empty
80  */
81 int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer);
82 
83 /**
84  * Get number of bytes available for read
85  * @param ring_buffer object
86  * @return number of bytes available for read
87  */
88 uint32_t btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer);
89 
90 /**
91  * Get free space available for write
92  * @param ring_buffer object
93  * @return number of bytes available for write
94  */
95 uint32_t btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer);
96 
97 /**
98  * Write bytes into ring buffer
99  * @param ring_buffer object
100  * @param data to store
101  * @param data_length
102  * @return 0 if ok, ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if not enough space in buffer
103  */
104 int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length);
105 
106 /**
107  * Read from ring buffer
108  * @param ring_buffer object
109  * @param buffer to store read data
110  * @param length to read
111  * @param number_of_bytes_read
112  */
113 void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * buffer, uint32_t length, uint32_t * number_of_bytes_read);
114 
115 /* API_END */
116 
117 #if defined __cplusplus
118 }
119 #endif
120 
121 #endif // BTSTACK_RING_BUFFER_H
122