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