xref: /btstack/src/btstack_ring_buffer.c (revision 44e9e4014d9eeb6a68ba5b64b4bf81aa017cfca3)
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.c
40  *
41  */
42 
43 #include <stdlib.h>
44 #include <stdio.h>
45 
46 #include "btstack_ring_buffer.h"
47 
48 #define ERROR_CODE_MEMORY_CAPACITY_EXCEEDED 0x07
49 
50 
51 // init ring buffer
52 void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint32_t storage_size){
53     ring_buffer->storage = storage;
54     ring_buffer->size = storage_size;
55     ring_buffer->last_read_index = 0;
56     ring_buffer->last_written_index = 0;
57     ring_buffer->full = 0;
58 }
59 
60 int btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer){
61     if (ring_buffer->full) return ring_buffer->size;
62     int diff = ring_buffer->last_written_index - ring_buffer->last_read_index;
63     if (diff >= 0) return diff;
64     return diff + ring_buffer->size;
65 }
66 
67 // test if ring buffer is empty
68 int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer){
69     return btstack_ring_buffer_bytes_available(ring_buffer) == 0;
70 }
71 
72 //
73 int btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer){
74     return ring_buffer->size - btstack_ring_buffer_bytes_available(ring_buffer);
75 }
76 
77 // add byte block to ring buffer,
78 int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length){
79     if (btstack_ring_buffer_bytes_free(ring_buffer) < data_length){
80         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
81     }
82     int count = 0;
83     while (count < data_length){
84         if (ring_buffer->last_written_index < ring_buffer->size - 1){
85             ring_buffer->last_written_index++;
86         } else {
87             ring_buffer->last_written_index = 0;
88         }
89         ring_buffer->storage[ring_buffer->last_written_index] = data[count++];
90     }
91     if (ring_buffer->last_written_index == ring_buffer->last_read_index){
92         ring_buffer->full = 1;
93     }
94     return 0;
95 }
96 
97 // fetch data_length bytes from ring buffer
98 void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length, uint32_t * number_of_bytes_read){
99     uint32_t count = 0;
100     while (count < data_length && btstack_ring_buffer_bytes_available(ring_buffer)){
101         if (ring_buffer->last_read_index < ring_buffer->last_written_index ) {
102             ring_buffer->last_read_index++;
103         } else {
104             if (ring_buffer->last_read_index < ring_buffer->size - 1){
105                 ring_buffer->last_read_index++;
106             } else {
107                 ring_buffer->last_read_index = 0;
108             }
109         }
110         ring_buffer->full = 0;
111         data[count++] = ring_buffer->storage[ring_buffer->last_read_index];
112     }
113     *number_of_bytes_read = count;
114 }
115 
116