xref: /btstack/src/btstack_ring_buffer.h (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
1*bc37f7b0SMilanka Ringwald /*
2*bc37f7b0SMilanka Ringwald  * Copyright (C) 2016 BlueKitchen GmbH
3*bc37f7b0SMilanka Ringwald  *
4*bc37f7b0SMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
5*bc37f7b0SMilanka Ringwald  * modification, are permitted provided that the following conditions
6*bc37f7b0SMilanka Ringwald  * are met:
7*bc37f7b0SMilanka Ringwald  *
8*bc37f7b0SMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
9*bc37f7b0SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
10*bc37f7b0SMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*bc37f7b0SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*bc37f7b0SMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
13*bc37f7b0SMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*bc37f7b0SMilanka Ringwald  *    contributors may be used to endorse or promote products derived
15*bc37f7b0SMilanka Ringwald  *    from this software without specific prior written permission.
16*bc37f7b0SMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*bc37f7b0SMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
18*bc37f7b0SMilanka Ringwald  *    monetary gain.
19*bc37f7b0SMilanka Ringwald  *
20*bc37f7b0SMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*bc37f7b0SMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*bc37f7b0SMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*bc37f7b0SMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*bc37f7b0SMilanka Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*bc37f7b0SMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*bc37f7b0SMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*bc37f7b0SMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*bc37f7b0SMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*bc37f7b0SMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*bc37f7b0SMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*bc37f7b0SMilanka Ringwald  * SUCH DAMAGE.
32*bc37f7b0SMilanka Ringwald  *
33*bc37f7b0SMilanka Ringwald  * Please inquire about commercial licensing options at
34*bc37f7b0SMilanka Ringwald  * [email protected]
35*bc37f7b0SMilanka Ringwald  *
36*bc37f7b0SMilanka Ringwald  */
37*bc37f7b0SMilanka Ringwald 
38*bc37f7b0SMilanka Ringwald /*
39*bc37f7b0SMilanka Ringwald  *  btstack_ring_buffer.h
40*bc37f7b0SMilanka Ringwald  */
41*bc37f7b0SMilanka Ringwald 
42*bc37f7b0SMilanka Ringwald #ifndef __BTSTACK_RING_BUFFER_H
43*bc37f7b0SMilanka Ringwald #define __BTSTACK_RING_BUFFER_H
44*bc37f7b0SMilanka Ringwald 
45*bc37f7b0SMilanka Ringwald #if defined __cplusplus
46*bc37f7b0SMilanka Ringwald extern "C" {
47*bc37f7b0SMilanka Ringwald #endif
48*bc37f7b0SMilanka Ringwald 
49*bc37f7b0SMilanka Ringwald typedef struct btstack_ring_buffer {
50*bc37f7b0SMilanka Ringwald     uint8_t  * storage;
51*bc37f7b0SMilanka Ringwald     uint16_t size;
52*bc37f7b0SMilanka Ringwald     uint16_t last_read_index;
53*bc37f7b0SMilanka Ringwald     uint16_t last_written_index;
54*bc37f7b0SMilanka Ringwald     uint8_t  full;
55*bc37f7b0SMilanka Ringwald } btstack_ring_buffer_t;
56*bc37f7b0SMilanka Ringwald 
57*bc37f7b0SMilanka Ringwald // init ring buffer
58*bc37f7b0SMilanka Ringwald void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint16_t storage_size);
59*bc37f7b0SMilanka Ringwald 
60*bc37f7b0SMilanka Ringwald // test if ring buffer is empty
61*bc37f7b0SMilanka Ringwald int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer);
62*bc37f7b0SMilanka Ringwald 
63*bc37f7b0SMilanka Ringwald // number of bytes available for read
64*bc37f7b0SMilanka Ringwald int btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer);
65*bc37f7b0SMilanka Ringwald 
66*bc37f7b0SMilanka Ringwald // free space for write given in number of bytes
67*bc37f7b0SMilanka Ringwald int btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer);
68*bc37f7b0SMilanka Ringwald 
69*bc37f7b0SMilanka Ringwald // add byte block to ring buffer,
70*bc37f7b0SMilanka Ringwald int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint16_t data_length);
71*bc37f7b0SMilanka Ringwald 
72*bc37f7b0SMilanka Ringwald // fetch data_length bytes from ring buffer
73*bc37f7b0SMilanka Ringwald void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint16_t data_length, uint16_t * number_of_bytes_read);
74*bc37f7b0SMilanka Ringwald 
75*bc37f7b0SMilanka Ringwald 
76*bc37f7b0SMilanka Ringwald #if defined __cplusplus
77*bc37f7b0SMilanka Ringwald }
78*bc37f7b0SMilanka Ringwald #endif
79*bc37f7b0SMilanka Ringwald 
80*bc37f7b0SMilanka Ringwald #endif // __BTSTACK_RING_BUFFER_H
81