Contiki 2.5
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  * $Id: ringbuf.h,v 1.1 2009/03/01 20:23:56 adamdunkels Exp $
45  */
46 
47 /**
48  * \file
49  * Header file for the ring buffer library
50  * \author
51  * Adam Dunkels <adam@sics.se>
52  */
53 
54 #ifndef __RINGBUF_H__
55 #define __RINGBUF_H__
56 
57 #include "contiki-conf.h"
58 
59 /**
60  * \brief Structure that holds the state of a ring buffer.
61  *
62  * This structure holds the state of a ring buffer. The
63  * actual buffer needs to be defined separately. This
64  * struct is an opaque structure with no user-visible
65  * elements.
66  *
67  */
68 struct ringbuf {
69  uint8_t *data;
70  uint8_t mask;
71 
72  /* XXX these must be 8-bit quantities to avoid race conditions. */
73  uint8_t put_ptr, get_ptr;
74 };
75 
76 /**
77  * \brief Initialize a ring buffer
78  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
79  * \param a A pointer to an array to hold the data in the buffer
80  * \param size_power_of_two The size of the ring buffer, which must be a power of two
81  *
82  * This function initiates a ring buffer. The data in the
83  * buffer is stored in an external array, to which a
84  * pointer must be supplied. The size of the ring buffer
85  * must be a power of two and cannot be larger than 128
86  * bytes.
87  *
88  */
89 void ringbuf_init(struct ringbuf *r, uint8_t *a,
90  uint8_t size_power_of_two);
91 
92 /**
93  * \brief Insert a byte into the ring buffer
94  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
95  * \param c The byte to be written to the buffer
96  * \return Non-zero if there data could be written, or zero if the buffer was full.
97  *
98  * This function inserts a byte into the ring buffer. It
99  * is safe to call this function from an interrupt
100  * handler.
101  *
102  */
103 int ringbuf_put(struct ringbuf *r, uint8_t c);
104 
105 
106 /**
107  * \brief Get a byte from the ring buffer
108  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
109  * \return The data from the buffer, or -1 if the buffer was empty
110  *
111  * This function removes a byte from the ring buffer. It
112  * is safe to call this function from an interrupt
113  * handler.
114  *
115  */
116 int ringbuf_get(struct ringbuf *r);
117 
118 /**
119  * \brief Get the size of a ring buffer
120  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
121  * \return The size of the buffer.
122  */
123 int ringbuf_size(struct ringbuf *r);
124 
125 /**
126  * \brief Get the number of elements currently in the ring buffer
127  * \param r A pointer to a struct ringbuf to hold the state of the ring buffer
128  * \return The number of elements in the buffer.
129  */
130 int ringbuf_elements(struct ringbuf *r);
131 
132 #endif /* __RINGBUF_H__ */