xref: /btstack/src/btstack_ring_buffer.h (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
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 MATTHIAS
24  * RINGWALD 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  *  btstack_ring_buffer.h
40  */
41 
42 #ifndef BTSTACK_RING_BUFFER_H
43 #define BTSTACK_RING_BUFFER_H
44 
45 #if defined __cplusplus
46 extern "C" {
47 #endif
48 
49 #include <stdint.h>
50 
51 typedef struct btstack_ring_buffer {
52     uint8_t  * storage;
53     uint32_t size;
54     uint32_t last_read_index;
55     uint32_t last_written_index;
56     uint8_t  full;
57 } btstack_ring_buffer_t;
58 
59 /**
60  * Init ring buffer
61  * @param ring_buffer object
62  * @param storage
63  * @param storage_size in bytes
64  */
65 void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint32_t storage_size);
66 
67 /**
68  * Reset ring buffer to initial state (empty)
69  * @param ring_buffer object
70  */
71 void btstack_ring_buffer_reset(btstack_ring_buffer_t * ring_buffer);
72 
73 /**
74  * Check if ring buffer is empty
75  * @param ring_buffer object
76  * @return TRUE if empty
77  */
78 int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer);
79 
80 /**
81  * Get number of bytes available for read
82  * @param ring_buffer object
83  * @return number of bytes available for read
84  */
85 uint32_t btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer);
86 
87 /**
88  * Get free space available for write
89  * @param ring_buffer object
90  * @return number of bytes available for write
91  */
92 uint32_t btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer);
93 
94 /**
95  * Write bytes into ring buffer
96  * @param ring_buffer object
97  * @param data to store
98  * @param data_length
99  * @return 0 if ok, ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if not enough space in buffer
100  */
101 int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length);
102 
103 /**
104  * Read from ring buffer
105  * @param ring_buffer object
106  * @param buffer to store read data
107  * @param length to read
108  * @param number_of_bytes_read
109  */
110 void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * buffer, uint32_t length, uint32_t * number_of_bytes_read);
111 
112 #if defined __cplusplus
113 }
114 #endif
115 
116 #endif // BTSTACK_RING_BUFFER_H
117