xref: /btstack/src/btstack_ring_buffer.h (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH 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 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title Ring Buffer
40fe5a6c4eSMilanka Ringwald  *
41bc37f7b0SMilanka Ringwald  */
42bc37f7b0SMilanka Ringwald 
4380e33422SMatthias Ringwald #ifndef BTSTACK_RING_BUFFER_H
4480e33422SMatthias Ringwald #define BTSTACK_RING_BUFFER_H
45bc37f7b0SMilanka Ringwald 
46bc37f7b0SMilanka Ringwald #if defined __cplusplus
47bc37f7b0SMilanka Ringwald extern "C" {
48bc37f7b0SMilanka Ringwald #endif
49bc37f7b0SMilanka Ringwald 
50514b7bdcSMatthias Ringwald #include <stdint.h>
51514b7bdcSMatthias Ringwald 
52bc37f7b0SMilanka Ringwald typedef struct btstack_ring_buffer {
53bc37f7b0SMilanka Ringwald     uint8_t  * storage;
5444e9e401SMilanka Ringwald     uint32_t size;
5544e9e401SMilanka Ringwald     uint32_t last_read_index;
5644e9e401SMilanka Ringwald     uint32_t last_written_index;
57bc37f7b0SMilanka Ringwald     uint8_t  full;
58bc37f7b0SMilanka Ringwald } btstack_ring_buffer_t;
59bc37f7b0SMilanka Ringwald 
60fe5a6c4eSMilanka Ringwald /* API_START */
61fe5a6c4eSMilanka Ringwald 
62514b7bdcSMatthias Ringwald /**
63514b7bdcSMatthias Ringwald  * Init ring buffer
64514b7bdcSMatthias Ringwald  * @param ring_buffer object
65514b7bdcSMatthias Ringwald  * @param storage
66514b7bdcSMatthias Ringwald  * @param storage_size in bytes
67514b7bdcSMatthias Ringwald  */
6844e9e401SMilanka Ringwald void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint32_t storage_size);
69bc37f7b0SMilanka Ringwald 
70514b7bdcSMatthias Ringwald /**
7140adb62dSMatthias Ringwald  * Reset ring buffer to initial state (empty)
7240adb62dSMatthias Ringwald  * @param ring_buffer object
7340adb62dSMatthias Ringwald  */
7440adb62dSMatthias Ringwald void btstack_ring_buffer_reset(btstack_ring_buffer_t * ring_buffer);
7540adb62dSMatthias Ringwald 
7640adb62dSMatthias Ringwald /**
77514b7bdcSMatthias Ringwald  * Check if ring buffer is empty
78514b7bdcSMatthias Ringwald  * @param ring_buffer object
79514b7bdcSMatthias Ringwald  * @return TRUE if empty
80514b7bdcSMatthias Ringwald  */
81bc37f7b0SMilanka Ringwald int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer);
82bc37f7b0SMilanka Ringwald 
83514b7bdcSMatthias Ringwald /**
84514b7bdcSMatthias Ringwald  * Get number of bytes available for read
85514b7bdcSMatthias Ringwald  * @param ring_buffer object
86514b7bdcSMatthias Ringwald  * @return number of bytes available for read
87514b7bdcSMatthias Ringwald  */
880df3b718SMatthias Ringwald uint32_t btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer);
89bc37f7b0SMilanka Ringwald 
90514b7bdcSMatthias Ringwald /**
91514b7bdcSMatthias Ringwald  * Get free space available for write
92514b7bdcSMatthias Ringwald  * @param ring_buffer object
93514b7bdcSMatthias Ringwald  * @return number of bytes available for write
94514b7bdcSMatthias Ringwald  */
950df3b718SMatthias Ringwald uint32_t btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer);
96bc37f7b0SMilanka Ringwald 
97514b7bdcSMatthias Ringwald /**
98514b7bdcSMatthias Ringwald  * Write bytes into ring buffer
99514b7bdcSMatthias Ringwald  * @param ring_buffer object
100514b7bdcSMatthias Ringwald  * @param data to store
101514b7bdcSMatthias Ringwald  * @param data_length
102514b7bdcSMatthias Ringwald  * @return 0 if ok, ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if not enough space in buffer
103514b7bdcSMatthias Ringwald  */
10444e9e401SMilanka Ringwald int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length);
105bc37f7b0SMilanka Ringwald 
106514b7bdcSMatthias Ringwald /**
107514b7bdcSMatthias Ringwald  * Read from ring buffer
108514b7bdcSMatthias Ringwald  * @param ring_buffer object
109514b7bdcSMatthias Ringwald  * @param buffer to store read data
110514b7bdcSMatthias Ringwald  * @param length to read
111514b7bdcSMatthias Ringwald  * @param number_of_bytes_read
112514b7bdcSMatthias Ringwald  */
113514b7bdcSMatthias Ringwald void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * buffer, uint32_t length, uint32_t * number_of_bytes_read);
114bc37f7b0SMilanka Ringwald 
115fe5a6c4eSMilanka Ringwald /* API_END */
116fe5a6c4eSMilanka Ringwald 
117bc37f7b0SMilanka Ringwald #if defined __cplusplus
118bc37f7b0SMilanka Ringwald }
119bc37f7b0SMilanka Ringwald #endif
120bc37f7b0SMilanka Ringwald 
12180e33422SMatthias Ringwald #endif // BTSTACK_RING_BUFFER_H
122