ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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