xref: /btstack/src/btstack_ring_buffer.h (revision 1b464e99afd70ddaf6b75be1ba7cc563a5f5dfd8)
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  * Check if ring buffer is empty
69  * @param ring_buffer object
70  * @return TRUE if empty
71  */
72 int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer);
73 
74 /**
75  * Get number of bytes available for read
76  * @param ring_buffer object
77  * @return number of bytes available for read
78  */
79 uint32_t btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer);
80 
81 /**
82  * Get free space available for write
83  * @param ring_buffer object
84  * @return number of bytes available for write
85  */
86 uint32_t btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer);
87 
88 /**
89  * Write bytes into ring buffer
90  * @param ring_buffer object
91  * @param data to store
92  * @param data_length
93  * @return 0 if ok, ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if not enough space in buffer
94  */
95 int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length);
96 
97 /**
98  * Read from ring buffer
99  * @param ring_buffer object
100  * @param buffer to store read data
101  * @param length to read
102  * @param number_of_bytes_read
103  */
104 void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * buffer, uint32_t length, uint32_t * number_of_bytes_read);
105 
106 #if defined __cplusplus
107 }
108 #endif
109 
110 #endif // BTSTACK_RING_BUFFER_H
111