xref: /btstack/src/btstack_ring_buffer.h (revision 0df3b7185a8b785f7a00234f1a683c77a9b1bbda)
1bc37f7b0SMilanka Ringwald /*
2bc37f7b0SMilanka Ringwald  * Copyright (C) 2016 BlueKitchen GmbH
3bc37f7b0SMilanka Ringwald  *
4bc37f7b0SMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
5bc37f7b0SMilanka Ringwald  * modification, are permitted provided that the following conditions
6bc37f7b0SMilanka Ringwald  * are met:
7bc37f7b0SMilanka Ringwald  *
8bc37f7b0SMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
9bc37f7b0SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
10bc37f7b0SMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bc37f7b0SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bc37f7b0SMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
13bc37f7b0SMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bc37f7b0SMilanka Ringwald  *    contributors may be used to endorse or promote products derived
15bc37f7b0SMilanka Ringwald  *    from this software without specific prior written permission.
16bc37f7b0SMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bc37f7b0SMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
18bc37f7b0SMilanka Ringwald  *    monetary gain.
19bc37f7b0SMilanka Ringwald  *
20bc37f7b0SMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bc37f7b0SMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bc37f7b0SMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bc37f7b0SMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bc37f7b0SMilanka Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bc37f7b0SMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bc37f7b0SMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bc37f7b0SMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bc37f7b0SMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bc37f7b0SMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bc37f7b0SMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bc37f7b0SMilanka Ringwald  * SUCH DAMAGE.
32bc37f7b0SMilanka Ringwald  *
33bc37f7b0SMilanka Ringwald  * Please inquire about commercial licensing options at
34bc37f7b0SMilanka Ringwald  * [email protected]
35bc37f7b0SMilanka Ringwald  *
36bc37f7b0SMilanka Ringwald  */
37bc37f7b0SMilanka Ringwald 
38bc37f7b0SMilanka Ringwald /*
39bc37f7b0SMilanka Ringwald  *  btstack_ring_buffer.h
40bc37f7b0SMilanka Ringwald  */
41bc37f7b0SMilanka Ringwald 
42bc37f7b0SMilanka Ringwald #ifndef __BTSTACK_RING_BUFFER_H
43bc37f7b0SMilanka Ringwald #define __BTSTACK_RING_BUFFER_H
44bc37f7b0SMilanka Ringwald 
45bc37f7b0SMilanka Ringwald #if defined __cplusplus
46bc37f7b0SMilanka Ringwald extern "C" {
47bc37f7b0SMilanka Ringwald #endif
48bc37f7b0SMilanka Ringwald 
49514b7bdcSMatthias Ringwald #include <stdint.h>
50514b7bdcSMatthias Ringwald 
51bc37f7b0SMilanka Ringwald typedef struct btstack_ring_buffer {
52bc37f7b0SMilanka Ringwald     uint8_t  * storage;
5344e9e401SMilanka Ringwald     uint32_t size;
5444e9e401SMilanka Ringwald     uint32_t last_read_index;
5544e9e401SMilanka Ringwald     uint32_t last_written_index;
56bc37f7b0SMilanka Ringwald     uint8_t  full;
57bc37f7b0SMilanka Ringwald } btstack_ring_buffer_t;
58bc37f7b0SMilanka Ringwald 
59514b7bdcSMatthias Ringwald /**
60514b7bdcSMatthias Ringwald  * Init ring buffer
61514b7bdcSMatthias Ringwald  * @param ring_buffer object
62514b7bdcSMatthias Ringwald  * @param storage
63514b7bdcSMatthias Ringwald  * @param storage_size in bytes
64514b7bdcSMatthias Ringwald  */
6544e9e401SMilanka Ringwald void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint32_t storage_size);
66bc37f7b0SMilanka Ringwald 
67514b7bdcSMatthias Ringwald /**
68514b7bdcSMatthias Ringwald  * Check if ring buffer is empty
69514b7bdcSMatthias Ringwald  * @param ring_buffer object
70514b7bdcSMatthias Ringwald  * @return TRUE if empty
71514b7bdcSMatthias Ringwald  */
72bc37f7b0SMilanka Ringwald int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer);
73bc37f7b0SMilanka Ringwald 
74514b7bdcSMatthias Ringwald /**
75514b7bdcSMatthias Ringwald  * Get number of bytes available for read
76514b7bdcSMatthias Ringwald  * @param ring_buffer object
77514b7bdcSMatthias Ringwald  * @return number of bytes available for read
78514b7bdcSMatthias Ringwald  */
79*0df3b718SMatthias Ringwald uint32_t btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer);
80bc37f7b0SMilanka Ringwald 
81514b7bdcSMatthias Ringwald /**
82514b7bdcSMatthias Ringwald  * Get free space available for write
83514b7bdcSMatthias Ringwald  * @param ring_buffer object
84514b7bdcSMatthias Ringwald  * @return number of bytes available for write
85514b7bdcSMatthias Ringwald  */
86*0df3b718SMatthias Ringwald uint32_t btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer);
87bc37f7b0SMilanka Ringwald 
88514b7bdcSMatthias Ringwald /**
89514b7bdcSMatthias Ringwald  * Write bytes into ring buffer
90514b7bdcSMatthias Ringwald  * @param ring_buffer object
91514b7bdcSMatthias Ringwald  * @param data to store
92514b7bdcSMatthias Ringwald  * @param data_length
93514b7bdcSMatthias Ringwald  * @return 0 if ok, ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if not enough space in buffer
94514b7bdcSMatthias Ringwald  */
9544e9e401SMilanka Ringwald int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length);
96bc37f7b0SMilanka Ringwald 
97514b7bdcSMatthias Ringwald /**
98514b7bdcSMatthias Ringwald  * Read from ring buffer
99514b7bdcSMatthias Ringwald  * @param ring_buffer object
100514b7bdcSMatthias Ringwald  * @param buffer to store read data
101514b7bdcSMatthias Ringwald  * @param length to read
102514b7bdcSMatthias Ringwald  * @param number_of_bytes_read
103514b7bdcSMatthias Ringwald  */
104514b7bdcSMatthias Ringwald void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * buffer, uint32_t length, uint32_t * number_of_bytes_read);
105bc37f7b0SMilanka Ringwald 
106bc37f7b0SMilanka Ringwald #if defined __cplusplus
107bc37f7b0SMilanka Ringwald }
108bc37f7b0SMilanka Ringwald #endif
109bc37f7b0SMilanka Ringwald 
110bc37f7b0SMilanka Ringwald #endif // __BTSTACK_RING_BUFFER_H
111