Contiki-Inga 3.x
ringbuf.h
Go to the documentation of this file.
1 /** \addtogroup lib
2  * @{ */
3 
4 /**
5  * \defgroup ringbuf Ring buffer library
6  * @{
7  *
8  * The ring buffer library implements ring (circular) buffer where
9  * bytes can be read and written independently. A ring buffer is
10  * particularly useful in device drivers where data can come in
11  * through interrupts.
12  *
13  */
14 /*
15  * Copyright (c) 2008, Swedish Institute of Computer Science.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  * notice, this list of conditions and the following disclaimer in the
25  * documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the Institute nor the names of its contributors
27  * may be used to endorse or promote products derived from this software
28  * without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  * This file is part of the Contiki operating system.
43  *
44  */
45 
46 /**
47  * \file
48  * Header file for the ring buffer library
49  * \author
50  * Adam Dunkels <adam@sics.se>
51  */
52 
53 #ifndef RINGBUF_H_
54 #define RINGBUF_H_
55 
56 #include "contiki-conf.h"
57 
58 /**
59  * \brief Structure that holds the state of a ring buffer.
60  *
61  * This structure holds the state of a ring buffer. The
62  * actual buffer needs to be defined separately. This
63  * struct is an opaque structure with no user-visible
64  * elements.
65  *
66  */
67 struct ringbuf {
68  uint8_t *data;
69  uint8_t mask;
70 
71  /* XXX these must be 8-bit quantities to avoid race conditions. */
72  uint8_t put_ptr, get_ptr;
73 };
74 
75 /**
76  * \brief Initialize a ring buffer
77  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
78  * \param a A pointer to an array to hold the data in the buffer
79  * \param size_power_of_two The size of the ring buffer, which must be a power of two
80  *
81  * This function initiates a ring buffer. The data in the
82  * buffer is stored in an external array, to which a
83  * pointer must be supplied. The size of the ring buffer
84  * must be a power of two and cannot be larger than 128
85  * bytes.
86  *
87  */
88 void ringbuf_init(struct ringbuf *r, uint8_t *a,
89  uint8_t size_power_of_two);
90 
91 /**
92  * \brief Insert a byte into the ring buffer
93  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
94  * \param c The byte to be written to the buffer
95  * \return Non-zero if there data could be written, or zero if the buffer was full.
96  *
97  * This function inserts a byte into the ring buffer. It
98  * is safe to call this function from an interrupt
99  * handler.
100  *
101  */
102 int ringbuf_put(struct ringbuf *r, uint8_t c);
103 
104 
105 /**
106  * \brief Get a byte from the ring buffer
107  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
108  * \return The data from the buffer, or -1 if the buffer was empty
109  *
110  * This function removes a byte from the ring buffer. It
111  * is safe to call this function from an interrupt
112  * handler.
113  *
114  */
115 int ringbuf_get(struct ringbuf *r);
116 
117 /**
118  * \brief Get the size of a ring buffer
119  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
120  * \return The size of the buffer.
121  */
122 int ringbuf_size(struct ringbuf *r);
123 
124 /**
125  * \brief Get the number of elements currently in the ring buffer
126  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
127  * \return The number of elements in the buffer.
128  */
129 int ringbuf_elements(struct ringbuf *r);
130 
131 #endif /* RINGBUF_H_ */