]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwIP_130/src/api/netbuf.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwIP_130 / src / api / netbuf.c
1 /**\r
2  * @file\r
3  * Network buffer management\r
4  *\r
5  */\r
6 \r
7 /*\r
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
9  * All rights reserved.\r
10  *\r
11  * Redistribution and use in source and binary forms, with or without modification,\r
12  * are permitted provided that the following conditions are met:\r
13  *\r
14  * 1. Redistributions of source code must retain the above copyright notice,\r
15  *    this list of conditions and the following disclaimer.\r
16  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
17  *    this list of conditions and the following disclaimer in the documentation\r
18  *    and/or other materials provided with the distribution.\r
19  * 3. The name of the author may not be used to endorse or promote products\r
20  *    derived from this software without specific prior written permission.\r
21  *\r
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT\r
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\r
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY\r
31  * OF SUCH DAMAGE.\r
32  *\r
33  * This file is part of the lwIP TCP/IP stack.\r
34  *\r
35  * Author: Adam Dunkels <adam@sics.se>\r
36  *\r
37  */\r
38 \r
39 #include "lwip/opt.h"\r
40 \r
41 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */\r
42 \r
43 #include "lwip/netbuf.h"\r
44 #include "lwip/memp.h"\r
45 \r
46 #include <string.h>\r
47 \r
48 /**\r
49  * Create (allocate) and initialize a new netbuf.\r
50  * The netbuf doesn't yet contain a packet buffer!\r
51  *\r
52  * @return a pointer to a new netbuf\r
53  *         NULL on lack of memory\r
54  */\r
55 struct\r
56 netbuf *netbuf_new(void)\r
57 {\r
58   struct netbuf *buf;\r
59 \r
60   buf = memp_malloc(MEMP_NETBUF);\r
61   if (buf != NULL) {\r
62     buf->p = NULL;\r
63     buf->ptr = NULL;\r
64     buf->addr = NULL;\r
65     return buf;\r
66   } else {\r
67     return NULL;\r
68   }\r
69 }\r
70 \r
71 /**\r
72  * Deallocate a netbuf allocated by netbuf_new().\r
73  *\r
74  * @param buf pointer to a netbuf allocated by netbuf_new()\r
75  */\r
76 void\r
77 netbuf_delete(struct netbuf *buf)\r
78 {\r
79   if (buf != NULL) {\r
80     if (buf->p != NULL) {\r
81       pbuf_free(buf->p);\r
82       buf->p = buf->ptr = NULL;\r
83     }\r
84     memp_free(MEMP_NETBUF, buf);\r
85   }\r
86 }\r
87 \r
88 /**\r
89  * Allocate memory for a packet buffer for a given netbuf.\r
90  *\r
91  * @param buf the netbuf for which to allocate a packet buffer\r
92  * @param size the size of the packet buffer to allocate\r
93  * @return pointer to the allocated memory\r
94  *         NULL if no memory could be allocated\r
95  */\r
96 void *\r
97 netbuf_alloc(struct netbuf *buf, u16_t size)\r
98 {\r
99   LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);\r
100 \r
101   /* Deallocate any previously allocated memory. */\r
102   if (buf->p != NULL) {\r
103     pbuf_free(buf->p);\r
104   }\r
105   buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);\r
106   if (buf->p == NULL) {\r
107      return NULL;\r
108   }\r
109   LWIP_ASSERT("check that first pbuf can hold size",\r
110              (buf->p->len >= size));\r
111   buf->ptr = buf->p;\r
112   return buf->p->payload;\r
113 }\r
114 \r
115 /**\r
116  * Free the packet buffer included in a netbuf\r
117  *\r
118  * @param buf pointer to the netbuf which contains the packet buffer to free\r
119  */\r
120 void\r
121 netbuf_free(struct netbuf *buf)\r
122 {\r
123   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);\r
124   if (buf->p != NULL) {\r
125     pbuf_free(buf->p);\r
126   }\r
127   buf->p = buf->ptr = NULL;\r
128 }\r
129 \r
130 /**\r
131  * Let a netbuf reference existing (non-volatile) data.\r
132  *\r
133  * @param buf netbuf which should reference the data\r
134  * @param dataptr pointer to the data to reference\r
135  * @param size size of the data\r
136  * @return ERR_OK if data is referenced\r
137  *         ERR_MEM if data couldn't be referenced due to lack of memory\r
138  */\r
139 err_t\r
140 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)\r
141 {\r
142   LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);\r
143   if (buf->p != NULL) {\r
144     pbuf_free(buf->p);\r
145   }\r
146   buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);\r
147   if (buf->p == NULL) {\r
148     buf->ptr = NULL;\r
149     return ERR_MEM;\r
150   }\r
151   buf->p->payload = (void*)dataptr;\r
152   buf->p->len = buf->p->tot_len = size;\r
153   buf->ptr = buf->p;\r
154   return ERR_OK;\r
155 }\r
156 \r
157 /**\r
158  * Chain one netbuf to another (@see pbuf_chain)\r
159  *\r
160  * @param head the first netbuf\r
161  * @param tail netbuf to chain after head\r
162  */\r
163 void\r
164 netbuf_chain(struct netbuf *head, struct netbuf *tail)\r
165 {\r
166   LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);\r
167   LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);\r
168   pbuf_chain(head->p, tail->p);\r
169   head->ptr = head->p;\r
170   memp_free(MEMP_NETBUF, tail);\r
171 }\r
172 \r
173 /**\r
174  * Get the data pointer and length of the data inside a netbuf.\r
175  *\r
176  * @param buf netbuf to get the data from\r
177  * @param dataptr pointer to a void pointer where to store the data pointer\r
178  * @param len pointer to an u16_t where the length of the data is stored\r
179  * @return ERR_OK if the information was retreived,\r
180  *         ERR_BUF on error.\r
181  */\r
182 err_t\r
183 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)\r
184 {\r
185   LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);\r
186   LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);\r
187   LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);\r
188 \r
189   if (buf->ptr == NULL) {\r
190     return ERR_BUF;\r
191   }\r
192   *dataptr = buf->ptr->payload;\r
193   *len = buf->ptr->len;\r
194   return ERR_OK;\r
195 }\r
196 \r
197 /**\r
198  * Move the current data pointer of a packet buffer contained in a netbuf\r
199  * to the next part.\r
200  * The packet buffer itself is not modified.\r
201  *\r
202  * @param buf the netbuf to modify\r
203  * @return -1 if there is no next part\r
204  *         1  if moved to the next part but now there is no next part\r
205  *         0  if moved to the next part and there are still more parts\r
206  */\r
207 s8_t\r
208 netbuf_next(struct netbuf *buf)\r
209 {\r
210   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;);\r
211   if (buf->ptr->next == NULL) {\r
212     return -1;\r
213   }\r
214   buf->ptr = buf->ptr->next;\r
215   if (buf->ptr->next == NULL) {\r
216     return 1;\r
217   }\r
218   return 0;\r
219 }\r
220 \r
221 /**\r
222  * Move the current data pointer of a packet buffer contained in a netbuf\r
223  * to the beginning of the packet.\r
224  * The packet buffer itself is not modified.\r
225  *\r
226  * @param buf the netbuf to modify\r
227  */\r
228 void\r
229 netbuf_first(struct netbuf *buf)\r
230 {\r
231   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);\r
232   buf->ptr = buf->p;\r
233 }\r
234 \r
235 #endif /* LWIP_NETCONN */\r