xref: /btstack/src/btstack_ring_buffer.c (revision ceed67ffe68ea5103bbc5647a2e000ed0e08b747)
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 BLUEKITCHEN
24  * GMBH 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 #define BTSTACK_FILE__ "btstack_ring_buffer.c"
39 
40 /*
41  *  btstack_ring_buffer.c
42  *
43  */
44 
45 #include <string.h>
46 
47 #include "bluetooth.h"
48 #include "btstack_ring_buffer.h"
49 #include "btstack_util.h"
50 
51 
52 // init ring buffer
53 void btstack_ring_buffer_init(btstack_ring_buffer_t * ring_buffer, uint8_t * storage, uint32_t storage_size){
54     ring_buffer->storage = storage;
55     ring_buffer->size = storage_size;
56     btstack_ring_buffer_reset(ring_buffer);
57 }
58 
59 void btstack_ring_buffer_reset(btstack_ring_buffer_t * ring_buffer){
60     ring_buffer->last_read_index = 0;
61     ring_buffer->last_written_index = 0;
62     ring_buffer->full = 0;
63 }
64 
65 uint32_t btstack_ring_buffer_bytes_available(btstack_ring_buffer_t * ring_buffer){
66     if (ring_buffer->full) return ring_buffer->size;
67     int diff = ring_buffer->last_written_index - ring_buffer->last_read_index;
68     if (diff >= 0) return diff;
69     return diff + ring_buffer->size;
70 }
71 
72 // test if ring buffer is empty
73 int btstack_ring_buffer_empty(btstack_ring_buffer_t * ring_buffer){
74     return btstack_ring_buffer_bytes_available(ring_buffer) == 0u;
75 }
76 
77 //
78 uint32_t btstack_ring_buffer_bytes_free(btstack_ring_buffer_t * ring_buffer){
79     return ring_buffer->size - btstack_ring_buffer_bytes_available(ring_buffer);
80 }
81 
82 // add byte block to ring buffer,
83 int btstack_ring_buffer_write(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length){
84     if (btstack_ring_buffer_bytes_free(ring_buffer) < data_length){
85         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
86     }
87 
88     // simplify logic below by asserting data_length > 0
89     if (data_length == 0u) return 0u;
90 
91     uint32_t  remaining_data_length = data_length;
92     const uint8_t * remaining_data = data;
93 
94     // copy first chunk
95     unsigned int bytes_until_end = ring_buffer->size - ring_buffer->last_written_index;
96     unsigned int bytes_to_copy = btstack_min(bytes_until_end, remaining_data_length);
97     (void)memcpy(&ring_buffer->storage[ring_buffer->last_written_index], remaining_data, bytes_to_copy);
98     remaining_data_length -= bytes_to_copy;
99     remaining_data += bytes_to_copy;
100 
101     // update last written index
102     ring_buffer->last_written_index += bytes_to_copy;
103     if (ring_buffer->last_written_index == ring_buffer->size){
104         ring_buffer->last_written_index = 0;
105     }
106 
107     // copy second chunk
108     if (remaining_data_length != 0) {
109         (void)memcpy(&ring_buffer->storage[0], remaining_data, remaining_data_length);
110         ring_buffer->last_written_index += remaining_data_length;
111     }
112 
113     // mark buffer as full
114     if (ring_buffer->last_written_index == ring_buffer->last_read_index){
115         ring_buffer->full = 1;
116     }
117     return ERROR_CODE_SUCCESS;
118 }
119 
120 // fetch data_length bytes from ring buffer
121 void btstack_ring_buffer_read(btstack_ring_buffer_t * ring_buffer, uint8_t * data, uint32_t data_length, uint32_t * number_of_bytes_read){
122     // limit data to get and report
123     uint32_t remaining_data_length = btstack_min(data_length, btstack_ring_buffer_bytes_available(ring_buffer));
124     *number_of_bytes_read = remaining_data_length;
125 
126     // simplify logic below by asserting remaining_data_length > 0
127     if (remaining_data_length == 0u) return;
128 
129     uint8_t * remaining_data = data;
130 
131     // copy first chunk
132     unsigned int bytes_until_end = ring_buffer->size - ring_buffer->last_read_index;
133     unsigned int bytes_to_copy = btstack_min(bytes_until_end, remaining_data_length);
134     (void)memcpy(remaining_data, &ring_buffer->storage[ring_buffer->last_read_index],
135                  bytes_to_copy);
136     remaining_data_length -= bytes_to_copy;
137     remaining_data += bytes_to_copy;
138 
139     // update last read index
140     ring_buffer->last_read_index += bytes_to_copy;
141     if (ring_buffer->last_read_index == ring_buffer->size){
142         ring_buffer->last_read_index = 0;
143     }
144 
145     // copy second chunk
146     if (remaining_data_length != 0) {
147         (void)memcpy(remaining_data, &ring_buffer->storage[0], remaining_data_length);
148         ring_buffer->last_read_index += remaining_data_length;
149     }
150 
151     // clear full flag
152     ring_buffer->full = 0;
153 }
154 
155