Forked from STM32F7 internet for nucleo F746ZG

Dependents:   Nucleo_F746ZG_Ethernet_MQTT_Ultrasound

Fork of F7_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sun Jun 19 16:23:40 2016 +0000
Revision:
0:d26c1b55cfca
Ethernet Library for Nucleo stm32f746ZG and Disco stm32f746NG  works under arm and gcc environment

Who changed what in which revision?

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