Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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