Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
GordonSin
Date:
Fri May 31 04:09:54 2013 +0000
Revision:
0:0ed2a7c7190c
31/5/2013;

Who changed what in which revision?

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