Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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