EVE 1.0
rbuf.h
Go to the documentation of this file.
1 #ifndef EVE_RBUF_H_INCLUDED
2 #define EVE_RBUF_H_INCLUDED
3 
4 /**********************************************************************/
5 /*
6  * Copyright (c) 2008, Swedish Institute of Computer Science.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  *
35  */
36 
37  /**
38  * \file
39  * \brief Header file for ring buffer primitive library
40  *
41  * \author Adam Dunkels <adam@sics.se>
42  * \author SB, Jetro AS
43  */ /******************************************************************/
44 
45 #include <stdint.h>
46 #include <stdbool.h>
47 
48 /**
49  * \defgroup rbuf Ring buffer
50  * \ingroup util
51  * \{
52  */
53 
54 /**
55  * \brief Structure that holds the state of a ring buffer.
56  *
57  * This structure holds the state of a ring buffer. The
58  * actual buffer needs to be defined separately. This
59  * struct is an opaque structure with no user-visible
60  * elements.
61  *
62  */
63 struct rbuf_t {
64  uint8_t *data;
65  uint32_t size;
66  uint32_t put;
67  uint32_t get;
68 };
69 
70 /**
71  * \brief Initialize a ring buffer
72  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
73  * \param a A pointer to an array to hold the data in the buffer
74  * \param size The size of the ring buffer
75  *
76  * This function initiates a ring buffer. The data in the
77  * buffer is stored in an external array, to which a
78  * pointer must be supplied.
79  *
80  */
81 void rbuf_init(struct rbuf_t *r, uint8_t *a, uint32_t size);
82 
83 /**
84  * \brief Get the size of a ring buffer
85  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
86  * \return The size of the buffer.
87  */
88 uint32_t rbuf_size(struct rbuf_t *r);
89 
90 /**
91  * \brief Get the number of bytes currently used in the ring buffer
92  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
93  * \return The number of elements in the buffer.
94  */
95 uint32_t rbuf_used(struct rbuf_t *r);
96 
97 /**
98  * \brief Get the length of a continuous used part of the buffer and a pointer to its beginning
99  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
100  * \param[out] ptr Pointer to the pointer to the beginning of a continuous used part
101  * \return The length of a continuous used part of the buffer
102  */
103 uint32_t rbuf_contused(struct rbuf_t *r, uint8_t** ptr);
104 
105 /**
106  * \brief Advance the read pointer of the buffer after direct read. Note that
107  * length cannot be larger than the value returned by rbuf_contused()
108  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
109  * \param l Number of bytes read
110  */
111 void rbuf_commitread(struct rbuf_t *r, uint32_t l);
112 
113 /**
114  * \brief Get the number of free bytes currently in the ring buffer
115  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
116  * \return The number of free bytes in the buffer.
117  */
118 uint32_t rbuf_free(struct rbuf_t *r);
119 
120 /**
121  * \brief Get the length of a continuous free part of the buffer and a pointer to its beginning
122  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
123  * \param[out] ptr Pointer to the pointer to the beginning of a continuous free part
124  * \return The length of a continuous free part of the buffer
125  */
126 uint32_t rbuf_contfree(struct rbuf_t *r, uint8_t** ptr);
127 
128 /**
129  * \brief Advance the write pointer of the buffer after direct write. Note that
130  * length cannot be larger than the value returned by rbuf_contfree()
131  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
132  * \param l Number of bytes written
133  */
134 void rbuf_commitwrite(struct rbuf_t *r, uint32_t l);
135 
136 /**
137  * \brief Put an array of bytes to the ring buffer
138  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
139  * \param a A pointer to an array to copy data from
140  * \param l Number of bytes to put
141  * \return true on success, false if there is not enough free space in the buffer
142  */
143 bool rbuf_push(struct rbuf_t *r, const uint8_t* a, uint32_t l);
144 
145 /**
146  * \brief Get an array of bytes from the ring buffer
147  * \param r A pointer to a struct rbuf_t to hold the state of the ring buffer
148  * \param a A pointer to an array to copy data to
149  * \param l Number of bytes to get
150  * \return true on success, false if there is not enough bytes in the buffer
151  */
152 bool rbuf_pull(struct rbuf_t *r, uint8_t* a, uint32_t l);
153 
154 /** @} rbuf */
155 
156 #endif /* EVE_RBUF_H_INCLUDED */
Structure that holds the state of a ring buffer.
Definition: rbuf.h:63
void rbuf_commitwrite(struct rbuf_t *r, uint32_t l)
Advance the write pointer of the buffer after direct write. Note that length cannot be larger than th...
uint32_t rbuf_contfree(struct rbuf_t *r, uint8_t **ptr)
Get the length of a continuous free part of the buffer and a pointer to its beginning.
void rbuf_init(struct rbuf_t *r, uint8_t *a, uint32_t size)
Initialize a ring buffer.
uint32_t rbuf_free(struct rbuf_t *r)
Get the number of free bytes currently in the ring buffer.
bool rbuf_pull(struct rbuf_t *r, uint8_t *a, uint32_t l)
Get an array of bytes from the ring buffer.
bool rbuf_push(struct rbuf_t *r, const uint8_t *a, uint32_t l)
Put an array of bytes to the ring buffer.
uint32_t rbuf_size(struct rbuf_t *r)
Get the size of a ring buffer.
void rbuf_commitread(struct rbuf_t *r, uint32_t l)
Advance the read pointer of the buffer after direct read. Note that length cannot be larger than the ...
uint32_t rbuf_used(struct rbuf_t *r)
Get the number of bytes currently used in the ring buffer.
uint32_t rbuf_contused(struct rbuf_t *r, uint8_t **ptr)
Get the length of a continuous used part of the buffer and a pointer to its beginning.