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