Forked from STM32F7 internet for nucleo F746ZG
Dependents: Nucleo_F746ZG_Ethernet_MQTT_Ultrasound
Fork of F7_Ethernet by
lwip/core/pbuf.c@2:b4727195c450, 2016-10-24 (annotated)
- Committer:
- EmbeddedSam
- Date:
- Mon Oct 24 12:59:21 2016 +0000
- Revision:
- 2:b4727195c450
- Parent:
- 0:d26c1b55cfca
working with 1 ultrasound sensor approx 10hz update rate on mqtt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 0:d26c1b55cfca | 1 | /** |
DieterGraef | 0:d26c1b55cfca | 2 | * @file |
DieterGraef | 0:d26c1b55cfca | 3 | * Packet buffer management |
DieterGraef | 0:d26c1b55cfca | 4 | * |
DieterGraef | 0:d26c1b55cfca | 5 | * Packets are built from the pbuf data structure. It supports dynamic |
DieterGraef | 0:d26c1b55cfca | 6 | * memory allocation for packet contents or can reference externally |
DieterGraef | 0:d26c1b55cfca | 7 | * managed packet contents both in RAM and ROM. Quick allocation for |
DieterGraef | 0:d26c1b55cfca | 8 | * incoming packets is provided through pools with fixed sized pbufs. |
DieterGraef | 0:d26c1b55cfca | 9 | * |
DieterGraef | 0:d26c1b55cfca | 10 | * A packet may span over multiple pbufs, chained as a singly linked |
DieterGraef | 0:d26c1b55cfca | 11 | * list. This is called a "pbuf chain". |
DieterGraef | 0:d26c1b55cfca | 12 | * |
DieterGraef | 0:d26c1b55cfca | 13 | * Multiple packets may be queued, also using this singly linked list. |
DieterGraef | 0:d26c1b55cfca | 14 | * This is called a "packet queue". |
DieterGraef | 0:d26c1b55cfca | 15 | * |
DieterGraef | 0:d26c1b55cfca | 16 | * So, a packet queue consists of one or more pbuf chains, each of |
DieterGraef | 0:d26c1b55cfca | 17 | * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE |
DieterGraef | 0:d26c1b55cfca | 18 | * NOT SUPPORTED!!! Use helper structs to queue multiple packets. |
DieterGraef | 0:d26c1b55cfca | 19 | * |
DieterGraef | 0:d26c1b55cfca | 20 | * The differences between a pbuf chain and a packet queue are very |
DieterGraef | 0:d26c1b55cfca | 21 | * precise but subtle. |
DieterGraef | 0:d26c1b55cfca | 22 | * |
DieterGraef | 0:d26c1b55cfca | 23 | * The last pbuf of a packet has a ->tot_len field that equals the |
DieterGraef | 0:d26c1b55cfca | 24 | * ->len field. It can be found by traversing the list. If the last |
DieterGraef | 0:d26c1b55cfca | 25 | * pbuf of a packet has a ->next field other than NULL, more packets |
DieterGraef | 0:d26c1b55cfca | 26 | * are on the queue. |
DieterGraef | 0:d26c1b55cfca | 27 | * |
DieterGraef | 0:d26c1b55cfca | 28 | * Therefore, looping through a pbuf of a single packet, has an |
DieterGraef | 0:d26c1b55cfca | 29 | * loop end condition (tot_len == p->len), NOT (next == NULL). |
DieterGraef | 0:d26c1b55cfca | 30 | */ |
DieterGraef | 0:d26c1b55cfca | 31 | |
DieterGraef | 0:d26c1b55cfca | 32 | /* |
DieterGraef | 0:d26c1b55cfca | 33 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
DieterGraef | 0:d26c1b55cfca | 34 | * All rights reserved. |
DieterGraef | 0:d26c1b55cfca | 35 | * |
DieterGraef | 0:d26c1b55cfca | 36 | * Redistribution and use in source and binary forms, with or without modification, |
DieterGraef | 0:d26c1b55cfca | 37 | * are permitted provided that the following conditions are met: |
DieterGraef | 0:d26c1b55cfca | 38 | * |
DieterGraef | 0:d26c1b55cfca | 39 | * 1. Redistributions of source code must retain the above copyright notice, |
DieterGraef | 0:d26c1b55cfca | 40 | * this list of conditions and the following disclaimer. |
DieterGraef | 0:d26c1b55cfca | 41 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
DieterGraef | 0:d26c1b55cfca | 42 | * this list of conditions and the following disclaimer in the documentation |
DieterGraef | 0:d26c1b55cfca | 43 | * and/or other materials provided with the distribution. |
DieterGraef | 0:d26c1b55cfca | 44 | * 3. The name of the author may not be used to endorse or promote products |
DieterGraef | 0:d26c1b55cfca | 45 | * derived from this software without specific prior written permission. |
DieterGraef | 0:d26c1b55cfca | 46 | * |
DieterGraef | 0:d26c1b55cfca | 47 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
DieterGraef | 0:d26c1b55cfca | 48 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
DieterGraef | 0:d26c1b55cfca | 49 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
DieterGraef | 0:d26c1b55cfca | 50 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
DieterGraef | 0:d26c1b55cfca | 51 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
DieterGraef | 0:d26c1b55cfca | 52 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
DieterGraef | 0:d26c1b55cfca | 53 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
DieterGraef | 0:d26c1b55cfca | 54 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
DieterGraef | 0:d26c1b55cfca | 55 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
DieterGraef | 0:d26c1b55cfca | 56 | * OF SUCH DAMAGE. |
DieterGraef | 0:d26c1b55cfca | 57 | * |
DieterGraef | 0:d26c1b55cfca | 58 | * This file is part of the lwIP TCP/IP stack. |
DieterGraef | 0:d26c1b55cfca | 59 | * |
DieterGraef | 0:d26c1b55cfca | 60 | * Author: Adam Dunkels <adam@sics.se> |
DieterGraef | 0:d26c1b55cfca | 61 | * |
DieterGraef | 0:d26c1b55cfca | 62 | */ |
DieterGraef | 0:d26c1b55cfca | 63 | |
DieterGraef | 0:d26c1b55cfca | 64 | #include "lwip/opt.h" |
DieterGraef | 0:d26c1b55cfca | 65 | |
DieterGraef | 0:d26c1b55cfca | 66 | #include "lwip/stats.h" |
DieterGraef | 0:d26c1b55cfca | 67 | #include "lwip/def.h" |
DieterGraef | 0:d26c1b55cfca | 68 | #include "lwip/mem.h" |
DieterGraef | 0:d26c1b55cfca | 69 | #include "lwip/memp.h" |
DieterGraef | 0:d26c1b55cfca | 70 | #include "lwip/pbuf.h" |
DieterGraef | 0:d26c1b55cfca | 71 | #include "lwip/sys.h" |
DieterGraef | 0:d26c1b55cfca | 72 | #include "arch/perf.h" |
DieterGraef | 0:d26c1b55cfca | 73 | #if LWIP_TCP && TCP_QUEUE_OOSEQ |
DieterGraef | 0:d26c1b55cfca | 74 | #include "lwip/tcp_impl.h" |
DieterGraef | 0:d26c1b55cfca | 75 | #endif |
DieterGraef | 0:d26c1b55cfca | 76 | #if LWIP_CHECKSUM_ON_COPY |
DieterGraef | 0:d26c1b55cfca | 77 | #include "lwip/inet_chksum.h" |
DieterGraef | 0:d26c1b55cfca | 78 | #endif |
DieterGraef | 0:d26c1b55cfca | 79 | |
DieterGraef | 0:d26c1b55cfca | 80 | #include <string.h> |
DieterGraef | 0:d26c1b55cfca | 81 | |
DieterGraef | 0:d26c1b55cfca | 82 | #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf)) |
DieterGraef | 0:d26c1b55cfca | 83 | /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically |
DieterGraef | 0:d26c1b55cfca | 84 | aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */ |
DieterGraef | 0:d26c1b55cfca | 85 | #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) |
DieterGraef | 0:d26c1b55cfca | 86 | |
DieterGraef | 0:d26c1b55cfca | 87 | #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ |
DieterGraef | 0:d26c1b55cfca | 88 | #define PBUF_POOL_IS_EMPTY() |
DieterGraef | 0:d26c1b55cfca | 89 | #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */ |
DieterGraef | 0:d26c1b55cfca | 90 | |
DieterGraef | 0:d26c1b55cfca | 91 | #if !NO_SYS |
DieterGraef | 0:d26c1b55cfca | 92 | #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL |
DieterGraef | 0:d26c1b55cfca | 93 | #include "lwip/tcpip.h" |
DieterGraef | 0:d26c1b55cfca | 94 | #define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \ |
DieterGraef | 0:d26c1b55cfca | 95 | if(tcpip_callback_with_block(pbuf_free_ooseq_callback, NULL, 0) != ERR_OK) { \ |
DieterGraef | 0:d26c1b55cfca | 96 | SYS_ARCH_PROTECT(old_level); \ |
DieterGraef | 0:d26c1b55cfca | 97 | pbuf_free_ooseq_pending = 0; \ |
DieterGraef | 0:d26c1b55cfca | 98 | SYS_ARCH_UNPROTECT(old_level); \ |
DieterGraef | 0:d26c1b55cfca | 99 | } } while(0) |
DieterGraef | 0:d26c1b55cfca | 100 | #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ |
DieterGraef | 0:d26c1b55cfca | 101 | #endif /* !NO_SYS */ |
DieterGraef | 0:d26c1b55cfca | 102 | |
DieterGraef | 0:d26c1b55cfca | 103 | volatile u8_t pbuf_free_ooseq_pending; |
DieterGraef | 0:d26c1b55cfca | 104 | #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty() |
DieterGraef | 0:d26c1b55cfca | 105 | |
DieterGraef | 0:d26c1b55cfca | 106 | /** |
DieterGraef | 0:d26c1b55cfca | 107 | * Attempt to reclaim some memory from queued out-of-sequence TCP segments |
DieterGraef | 0:d26c1b55cfca | 108 | * if we run out of pool pbufs. It's better to give priority to new packets |
DieterGraef | 0:d26c1b55cfca | 109 | * if we're running out. |
DieterGraef | 0:d26c1b55cfca | 110 | * |
DieterGraef | 0:d26c1b55cfca | 111 | * This must be done in the correct thread context therefore this function |
DieterGraef | 0:d26c1b55cfca | 112 | * can only be used with NO_SYS=0 and through tcpip_callback. |
DieterGraef | 0:d26c1b55cfca | 113 | */ |
DieterGraef | 0:d26c1b55cfca | 114 | #if !NO_SYS |
DieterGraef | 0:d26c1b55cfca | 115 | static |
DieterGraef | 0:d26c1b55cfca | 116 | #endif /* !NO_SYS */ |
DieterGraef | 0:d26c1b55cfca | 117 | void |
DieterGraef | 0:d26c1b55cfca | 118 | pbuf_free_ooseq(void) |
DieterGraef | 0:d26c1b55cfca | 119 | { |
DieterGraef | 0:d26c1b55cfca | 120 | struct tcp_pcb* pcb; |
DieterGraef | 0:d26c1b55cfca | 121 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 122 | |
DieterGraef | 0:d26c1b55cfca | 123 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 124 | pbuf_free_ooseq_pending = 0; |
DieterGraef | 0:d26c1b55cfca | 125 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 126 | |
DieterGraef | 0:d26c1b55cfca | 127 | for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) { |
DieterGraef | 0:d26c1b55cfca | 128 | if (NULL != pcb->ooseq) { |
DieterGraef | 0:d26c1b55cfca | 129 | /** Free the ooseq pbufs of one PCB only */ |
DieterGraef | 0:d26c1b55cfca | 130 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n")); |
DieterGraef | 0:d26c1b55cfca | 131 | tcp_segs_free(pcb->ooseq); |
DieterGraef | 0:d26c1b55cfca | 132 | pcb->ooseq = NULL; |
DieterGraef | 0:d26c1b55cfca | 133 | return; |
DieterGraef | 0:d26c1b55cfca | 134 | } |
DieterGraef | 0:d26c1b55cfca | 135 | } |
DieterGraef | 0:d26c1b55cfca | 136 | } |
DieterGraef | 0:d26c1b55cfca | 137 | |
DieterGraef | 0:d26c1b55cfca | 138 | #if !NO_SYS |
DieterGraef | 0:d26c1b55cfca | 139 | /** |
DieterGraef | 0:d26c1b55cfca | 140 | * Just a callback function for tcpip_timeout() that calls pbuf_free_ooseq(). |
DieterGraef | 0:d26c1b55cfca | 141 | */ |
DieterGraef | 0:d26c1b55cfca | 142 | static void |
DieterGraef | 0:d26c1b55cfca | 143 | pbuf_free_ooseq_callback(void *arg) |
DieterGraef | 0:d26c1b55cfca | 144 | { |
DieterGraef | 0:d26c1b55cfca | 145 | LWIP_UNUSED_ARG(arg); |
DieterGraef | 0:d26c1b55cfca | 146 | pbuf_free_ooseq(); |
DieterGraef | 0:d26c1b55cfca | 147 | } |
DieterGraef | 0:d26c1b55cfca | 148 | #endif /* !NO_SYS */ |
DieterGraef | 0:d26c1b55cfca | 149 | |
DieterGraef | 0:d26c1b55cfca | 150 | /** Queue a call to pbuf_free_ooseq if not already queued. */ |
DieterGraef | 0:d26c1b55cfca | 151 | static void |
DieterGraef | 0:d26c1b55cfca | 152 | pbuf_pool_is_empty(void) |
DieterGraef | 0:d26c1b55cfca | 153 | { |
DieterGraef | 0:d26c1b55cfca | 154 | #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL |
DieterGraef | 0:d26c1b55cfca | 155 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 156 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 157 | pbuf_free_ooseq_pending = 1; |
DieterGraef | 0:d26c1b55cfca | 158 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 159 | #else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ |
DieterGraef | 0:d26c1b55cfca | 160 | u8_t queued; |
DieterGraef | 0:d26c1b55cfca | 161 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 162 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 163 | queued = pbuf_free_ooseq_pending; |
DieterGraef | 0:d26c1b55cfca | 164 | pbuf_free_ooseq_pending = 1; |
DieterGraef | 0:d26c1b55cfca | 165 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 166 | |
DieterGraef | 0:d26c1b55cfca | 167 | if(!queued) { |
DieterGraef | 0:d26c1b55cfca | 168 | /* queue a call to pbuf_free_ooseq if not already queued */ |
DieterGraef | 0:d26c1b55cfca | 169 | PBUF_POOL_FREE_OOSEQ_QUEUE_CALL(); |
DieterGraef | 0:d26c1b55cfca | 170 | } |
DieterGraef | 0:d26c1b55cfca | 171 | #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */ |
DieterGraef | 0:d26c1b55cfca | 172 | } |
DieterGraef | 0:d26c1b55cfca | 173 | #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */ |
DieterGraef | 0:d26c1b55cfca | 174 | |
DieterGraef | 0:d26c1b55cfca | 175 | /** |
DieterGraef | 0:d26c1b55cfca | 176 | * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type). |
DieterGraef | 0:d26c1b55cfca | 177 | * |
DieterGraef | 0:d26c1b55cfca | 178 | * The actual memory allocated for the pbuf is determined by the |
DieterGraef | 0:d26c1b55cfca | 179 | * layer at which the pbuf is allocated and the requested size |
DieterGraef | 0:d26c1b55cfca | 180 | * (from the size parameter). |
DieterGraef | 0:d26c1b55cfca | 181 | * |
DieterGraef | 0:d26c1b55cfca | 182 | * @param layer flag to define header size |
DieterGraef | 0:d26c1b55cfca | 183 | * @param length size of the pbuf's payload |
DieterGraef | 0:d26c1b55cfca | 184 | * @param type this parameter decides how and where the pbuf |
DieterGraef | 0:d26c1b55cfca | 185 | * should be allocated as follows: |
DieterGraef | 0:d26c1b55cfca | 186 | * |
DieterGraef | 0:d26c1b55cfca | 187 | * - PBUF_RAM: buffer memory for pbuf is allocated as one large |
DieterGraef | 0:d26c1b55cfca | 188 | * chunk. This includes protocol headers as well. |
DieterGraef | 0:d26c1b55cfca | 189 | * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for |
DieterGraef | 0:d26c1b55cfca | 190 | * protocol headers. Additional headers must be prepended |
DieterGraef | 0:d26c1b55cfca | 191 | * by allocating another pbuf and chain in to the front of |
DieterGraef | 0:d26c1b55cfca | 192 | * the ROM pbuf. It is assumed that the memory used is really |
DieterGraef | 0:d26c1b55cfca | 193 | * similar to ROM in that it is immutable and will not be |
DieterGraef | 0:d26c1b55cfca | 194 | * changed. Memory which is dynamic should generally not |
DieterGraef | 0:d26c1b55cfca | 195 | * be attached to PBUF_ROM pbufs. Use PBUF_REF instead. |
DieterGraef | 0:d26c1b55cfca | 196 | * - PBUF_REF: no buffer memory is allocated for the pbuf, even for |
DieterGraef | 0:d26c1b55cfca | 197 | * protocol headers. It is assumed that the pbuf is only |
DieterGraef | 0:d26c1b55cfca | 198 | * being used in a single thread. If the pbuf gets queued, |
DieterGraef | 0:d26c1b55cfca | 199 | * then pbuf_take should be called to copy the buffer. |
DieterGraef | 0:d26c1b55cfca | 200 | * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from |
DieterGraef | 0:d26c1b55cfca | 201 | * the pbuf pool that is allocated during pbuf_init(). |
DieterGraef | 0:d26c1b55cfca | 202 | * |
DieterGraef | 0:d26c1b55cfca | 203 | * @return the allocated pbuf. If multiple pbufs where allocated, this |
DieterGraef | 0:d26c1b55cfca | 204 | * is the first pbuf of a pbuf chain. |
DieterGraef | 0:d26c1b55cfca | 205 | */ |
DieterGraef | 0:d26c1b55cfca | 206 | struct pbuf * |
DieterGraef | 0:d26c1b55cfca | 207 | pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type) |
DieterGraef | 0:d26c1b55cfca | 208 | { |
DieterGraef | 0:d26c1b55cfca | 209 | struct pbuf *p, *q, *r; |
DieterGraef | 0:d26c1b55cfca | 210 | u16_t offset; |
DieterGraef | 0:d26c1b55cfca | 211 | s32_t rem_len; /* remaining length */ |
DieterGraef | 0:d26c1b55cfca | 212 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length)); |
DieterGraef | 0:d26c1b55cfca | 213 | |
DieterGraef | 0:d26c1b55cfca | 214 | /* determine header offset */ |
DieterGraef | 0:d26c1b55cfca | 215 | switch (layer) { |
DieterGraef | 0:d26c1b55cfca | 216 | case PBUF_TRANSPORT: |
DieterGraef | 0:d26c1b55cfca | 217 | /* add room for transport (often TCP) layer header */ |
DieterGraef | 0:d26c1b55cfca | 218 | offset = PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN; |
DieterGraef | 0:d26c1b55cfca | 219 | break; |
DieterGraef | 0:d26c1b55cfca | 220 | case PBUF_IP: |
DieterGraef | 0:d26c1b55cfca | 221 | /* add room for IP layer header */ |
DieterGraef | 0:d26c1b55cfca | 222 | offset = PBUF_LINK_HLEN + PBUF_IP_HLEN; |
DieterGraef | 0:d26c1b55cfca | 223 | break; |
DieterGraef | 0:d26c1b55cfca | 224 | case PBUF_LINK: |
DieterGraef | 0:d26c1b55cfca | 225 | /* add room for link layer header */ |
DieterGraef | 0:d26c1b55cfca | 226 | offset = PBUF_LINK_HLEN; |
DieterGraef | 0:d26c1b55cfca | 227 | break; |
DieterGraef | 0:d26c1b55cfca | 228 | case PBUF_RAW: |
DieterGraef | 0:d26c1b55cfca | 229 | offset = 0; |
DieterGraef | 0:d26c1b55cfca | 230 | break; |
DieterGraef | 0:d26c1b55cfca | 231 | default: |
DieterGraef | 0:d26c1b55cfca | 232 | LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0); |
DieterGraef | 0:d26c1b55cfca | 233 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 234 | } |
DieterGraef | 0:d26c1b55cfca | 235 | |
DieterGraef | 0:d26c1b55cfca | 236 | switch (type) { |
DieterGraef | 0:d26c1b55cfca | 237 | case PBUF_POOL: |
DieterGraef | 0:d26c1b55cfca | 238 | /* allocate head of pbuf chain into p */ |
DieterGraef | 0:d26c1b55cfca | 239 | p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL); |
DieterGraef | 0:d26c1b55cfca | 240 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p)); |
DieterGraef | 0:d26c1b55cfca | 241 | if (p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 242 | PBUF_POOL_IS_EMPTY(); |
DieterGraef | 0:d26c1b55cfca | 243 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 244 | } |
DieterGraef | 0:d26c1b55cfca | 245 | p->type = type; |
DieterGraef | 0:d26c1b55cfca | 246 | p->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 247 | |
DieterGraef | 0:d26c1b55cfca | 248 | /* make the payload pointer point 'offset' bytes into pbuf data memory */ |
DieterGraef | 0:d26c1b55cfca | 249 | p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset))); |
DieterGraef | 0:d26c1b55cfca | 250 | LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned", |
DieterGraef | 0:d26c1b55cfca | 251 | ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); |
DieterGraef | 0:d26c1b55cfca | 252 | /* the total length of the pbuf chain is the requested size */ |
DieterGraef | 0:d26c1b55cfca | 253 | p->tot_len = length; |
DieterGraef | 0:d26c1b55cfca | 254 | /* set the length of the first pbuf in the chain */ |
DieterGraef | 0:d26c1b55cfca | 255 | p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)); |
DieterGraef | 0:d26c1b55cfca | 256 | LWIP_ASSERT("check p->payload + p->len does not overflow pbuf", |
DieterGraef | 0:d26c1b55cfca | 257 | ((u8_t*)p->payload + p->len <= |
DieterGraef | 0:d26c1b55cfca | 258 | (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED)); |
DieterGraef | 0:d26c1b55cfca | 259 | LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT", |
DieterGraef | 0:d26c1b55cfca | 260 | (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 ); |
DieterGraef | 0:d26c1b55cfca | 261 | /* set reference count (needed here in case we fail) */ |
DieterGraef | 0:d26c1b55cfca | 262 | p->ref = 1; |
DieterGraef | 0:d26c1b55cfca | 263 | |
DieterGraef | 0:d26c1b55cfca | 264 | /* now allocate the tail of the pbuf chain */ |
DieterGraef | 0:d26c1b55cfca | 265 | |
DieterGraef | 0:d26c1b55cfca | 266 | /* remember first pbuf for linkage in next iteration */ |
DieterGraef | 0:d26c1b55cfca | 267 | r = p; |
DieterGraef | 0:d26c1b55cfca | 268 | /* remaining length to be allocated */ |
DieterGraef | 0:d26c1b55cfca | 269 | rem_len = length - p->len; |
DieterGraef | 0:d26c1b55cfca | 270 | /* any remaining pbufs to be allocated? */ |
DieterGraef | 0:d26c1b55cfca | 271 | while (rem_len > 0) { |
DieterGraef | 0:d26c1b55cfca | 272 | q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL); |
DieterGraef | 0:d26c1b55cfca | 273 | if (q == NULL) { |
DieterGraef | 0:d26c1b55cfca | 274 | PBUF_POOL_IS_EMPTY(); |
DieterGraef | 0:d26c1b55cfca | 275 | /* free chain so far allocated */ |
DieterGraef | 0:d26c1b55cfca | 276 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 277 | /* bail out unsuccesfully */ |
DieterGraef | 0:d26c1b55cfca | 278 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 279 | } |
DieterGraef | 0:d26c1b55cfca | 280 | q->type = type; |
DieterGraef | 0:d26c1b55cfca | 281 | q->flags = 0; |
DieterGraef | 0:d26c1b55cfca | 282 | q->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 283 | /* make previous pbuf point to this pbuf */ |
DieterGraef | 0:d26c1b55cfca | 284 | r->next = q; |
DieterGraef | 0:d26c1b55cfca | 285 | /* set total length of this pbuf and next in chain */ |
DieterGraef | 0:d26c1b55cfca | 286 | LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff); |
DieterGraef | 0:d26c1b55cfca | 287 | q->tot_len = (u16_t)rem_len; |
DieterGraef | 0:d26c1b55cfca | 288 | /* this pbuf length is pool size, unless smaller sized tail */ |
DieterGraef | 0:d26c1b55cfca | 289 | q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED); |
DieterGraef | 0:d26c1b55cfca | 290 | q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF); |
DieterGraef | 0:d26c1b55cfca | 291 | LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned", |
DieterGraef | 0:d26c1b55cfca | 292 | ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0); |
DieterGraef | 0:d26c1b55cfca | 293 | LWIP_ASSERT("check p->payload + p->len does not overflow pbuf", |
DieterGraef | 0:d26c1b55cfca | 294 | ((u8_t*)p->payload + p->len <= |
DieterGraef | 0:d26c1b55cfca | 295 | (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED)); |
DieterGraef | 0:d26c1b55cfca | 296 | q->ref = 1; |
DieterGraef | 0:d26c1b55cfca | 297 | /* calculate remaining length to be allocated */ |
DieterGraef | 0:d26c1b55cfca | 298 | rem_len -= q->len; |
DieterGraef | 0:d26c1b55cfca | 299 | /* remember this pbuf for linkage in next iteration */ |
DieterGraef | 0:d26c1b55cfca | 300 | r = q; |
DieterGraef | 0:d26c1b55cfca | 301 | } |
DieterGraef | 0:d26c1b55cfca | 302 | /* end of chain */ |
DieterGraef | 0:d26c1b55cfca | 303 | /*r->next = NULL;*/ |
DieterGraef | 0:d26c1b55cfca | 304 | |
DieterGraef | 0:d26c1b55cfca | 305 | break; |
DieterGraef | 0:d26c1b55cfca | 306 | case PBUF_RAM: |
DieterGraef | 0:d26c1b55cfca | 307 | /* If pbuf is to be allocated in RAM, allocate memory for it. */ |
DieterGraef | 0:d26c1b55cfca | 308 | p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length)); |
DieterGraef | 0:d26c1b55cfca | 309 | if (p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 310 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 311 | } |
DieterGraef | 0:d26c1b55cfca | 312 | /* Set up internal structure of the pbuf. */ |
DieterGraef | 0:d26c1b55cfca | 313 | p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)); |
DieterGraef | 0:d26c1b55cfca | 314 | p->len = p->tot_len = length; |
DieterGraef | 0:d26c1b55cfca | 315 | p->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 316 | p->type = type; |
DieterGraef | 0:d26c1b55cfca | 317 | |
DieterGraef | 0:d26c1b55cfca | 318 | LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned", |
DieterGraef | 0:d26c1b55cfca | 319 | ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); |
DieterGraef | 0:d26c1b55cfca | 320 | break; |
DieterGraef | 0:d26c1b55cfca | 321 | /* pbuf references existing (non-volatile static constant) ROM payload? */ |
DieterGraef | 0:d26c1b55cfca | 322 | case PBUF_ROM: |
DieterGraef | 0:d26c1b55cfca | 323 | /* pbuf references existing (externally allocated) RAM payload? */ |
DieterGraef | 0:d26c1b55cfca | 324 | case PBUF_REF: |
DieterGraef | 0:d26c1b55cfca | 325 | /* only allocate memory for the pbuf structure */ |
DieterGraef | 0:d26c1b55cfca | 326 | p = (struct pbuf *)memp_malloc(MEMP_PBUF); |
DieterGraef | 0:d26c1b55cfca | 327 | if (p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 328 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
DieterGraef | 0:d26c1b55cfca | 329 | ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", |
DieterGraef | 0:d26c1b55cfca | 330 | (type == PBUF_ROM) ? "ROM" : "REF")); |
DieterGraef | 0:d26c1b55cfca | 331 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 332 | } |
DieterGraef | 0:d26c1b55cfca | 333 | /* caller must set this field properly, afterwards */ |
DieterGraef | 0:d26c1b55cfca | 334 | p->payload = NULL; |
DieterGraef | 0:d26c1b55cfca | 335 | p->len = p->tot_len = length; |
DieterGraef | 0:d26c1b55cfca | 336 | p->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 337 | p->type = type; |
DieterGraef | 0:d26c1b55cfca | 338 | break; |
DieterGraef | 0:d26c1b55cfca | 339 | default: |
DieterGraef | 0:d26c1b55cfca | 340 | LWIP_ASSERT("pbuf_alloc: erroneous type", 0); |
DieterGraef | 0:d26c1b55cfca | 341 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 342 | } |
DieterGraef | 0:d26c1b55cfca | 343 | /* set reference count */ |
DieterGraef | 0:d26c1b55cfca | 344 | p->ref = 1; |
DieterGraef | 0:d26c1b55cfca | 345 | /* set flags */ |
DieterGraef | 0:d26c1b55cfca | 346 | p->flags = 0; |
DieterGraef | 0:d26c1b55cfca | 347 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p)); |
DieterGraef | 0:d26c1b55cfca | 348 | return p; |
DieterGraef | 0:d26c1b55cfca | 349 | } |
DieterGraef | 0:d26c1b55cfca | 350 | |
DieterGraef | 0:d26c1b55cfca | 351 | #if LWIP_SUPPORT_CUSTOM_PBUF |
DieterGraef | 0:d26c1b55cfca | 352 | /** Initialize a custom pbuf (already allocated). |
DieterGraef | 0:d26c1b55cfca | 353 | * |
DieterGraef | 0:d26c1b55cfca | 354 | * @param layer flag to define header size |
DieterGraef | 0:d26c1b55cfca | 355 | * @param length size of the pbuf's payload |
DieterGraef | 0:d26c1b55cfca | 356 | * @param type type of the pbuf (only used to treat the pbuf accordingly, as |
DieterGraef | 0:d26c1b55cfca | 357 | * this function allocates no memory) |
DieterGraef | 0:d26c1b55cfca | 358 | * @param p pointer to the custom pbuf to initialize (already allocated) |
DieterGraef | 0:d26c1b55cfca | 359 | * @param payload_mem pointer to the buffer that is used for payload and headers, |
DieterGraef | 0:d26c1b55cfca | 360 | * must be at least big enough to hold 'length' plus the header size, |
DieterGraef | 0:d26c1b55cfca | 361 | * may be NULL if set later. |
DieterGraef | 0:d26c1b55cfca | 362 | * ATTENTION: The caller is responsible for correct alignment of this buffer!! |
DieterGraef | 0:d26c1b55cfca | 363 | * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least |
DieterGraef | 0:d26c1b55cfca | 364 | * big enough to hold 'length' plus the header size |
DieterGraef | 0:d26c1b55cfca | 365 | */ |
DieterGraef | 0:d26c1b55cfca | 366 | struct pbuf* |
DieterGraef | 0:d26c1b55cfca | 367 | pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p, |
DieterGraef | 0:d26c1b55cfca | 368 | void *payload_mem, u16_t payload_mem_len) |
DieterGraef | 0:d26c1b55cfca | 369 | { |
DieterGraef | 0:d26c1b55cfca | 370 | u16_t offset; |
DieterGraef | 0:d26c1b55cfca | 371 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length)); |
DieterGraef | 0:d26c1b55cfca | 372 | |
DieterGraef | 0:d26c1b55cfca | 373 | /* determine header offset */ |
DieterGraef | 0:d26c1b55cfca | 374 | switch (l) { |
DieterGraef | 0:d26c1b55cfca | 375 | case PBUF_TRANSPORT: |
DieterGraef | 0:d26c1b55cfca | 376 | /* add room for transport (often TCP) layer header */ |
DieterGraef | 0:d26c1b55cfca | 377 | offset = PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN; |
DieterGraef | 0:d26c1b55cfca | 378 | break; |
DieterGraef | 0:d26c1b55cfca | 379 | case PBUF_IP: |
DieterGraef | 0:d26c1b55cfca | 380 | /* add room for IP layer header */ |
DieterGraef | 0:d26c1b55cfca | 381 | offset = PBUF_LINK_HLEN + PBUF_IP_HLEN; |
DieterGraef | 0:d26c1b55cfca | 382 | break; |
DieterGraef | 0:d26c1b55cfca | 383 | case PBUF_LINK: |
DieterGraef | 0:d26c1b55cfca | 384 | /* add room for link layer header */ |
DieterGraef | 0:d26c1b55cfca | 385 | offset = PBUF_LINK_HLEN; |
DieterGraef | 0:d26c1b55cfca | 386 | break; |
DieterGraef | 0:d26c1b55cfca | 387 | case PBUF_RAW: |
DieterGraef | 0:d26c1b55cfca | 388 | offset = 0; |
DieterGraef | 0:d26c1b55cfca | 389 | break; |
DieterGraef | 0:d26c1b55cfca | 390 | default: |
DieterGraef | 0:d26c1b55cfca | 391 | LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0); |
DieterGraef | 0:d26c1b55cfca | 392 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 393 | } |
DieterGraef | 0:d26c1b55cfca | 394 | |
DieterGraef | 0:d26c1b55cfca | 395 | if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) { |
DieterGraef | 0:d26c1b55cfca | 396 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length)); |
DieterGraef | 0:d26c1b55cfca | 397 | return NULL; |
DieterGraef | 0:d26c1b55cfca | 398 | } |
DieterGraef | 0:d26c1b55cfca | 399 | |
DieterGraef | 0:d26c1b55cfca | 400 | p->pbuf.next = NULL; |
DieterGraef | 0:d26c1b55cfca | 401 | if (payload_mem != NULL) { |
DieterGraef | 0:d26c1b55cfca | 402 | p->pbuf.payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset); |
DieterGraef | 0:d26c1b55cfca | 403 | } else { |
DieterGraef | 0:d26c1b55cfca | 404 | p->pbuf.payload = NULL; |
DieterGraef | 0:d26c1b55cfca | 405 | } |
DieterGraef | 0:d26c1b55cfca | 406 | p->pbuf.flags = PBUF_FLAG_IS_CUSTOM; |
DieterGraef | 0:d26c1b55cfca | 407 | p->pbuf.len = p->pbuf.tot_len = length; |
DieterGraef | 0:d26c1b55cfca | 408 | p->pbuf.type = type; |
DieterGraef | 0:d26c1b55cfca | 409 | p->pbuf.ref = 1; |
DieterGraef | 0:d26c1b55cfca | 410 | return &p->pbuf; |
DieterGraef | 0:d26c1b55cfca | 411 | } |
DieterGraef | 0:d26c1b55cfca | 412 | #endif /* LWIP_SUPPORT_CUSTOM_PBUF */ |
DieterGraef | 0:d26c1b55cfca | 413 | |
DieterGraef | 0:d26c1b55cfca | 414 | /** |
DieterGraef | 0:d26c1b55cfca | 415 | * Shrink a pbuf chain to a desired length. |
DieterGraef | 0:d26c1b55cfca | 416 | * |
DieterGraef | 0:d26c1b55cfca | 417 | * @param p pbuf to shrink. |
DieterGraef | 0:d26c1b55cfca | 418 | * @param new_len desired new length of pbuf chain |
DieterGraef | 0:d26c1b55cfca | 419 | * |
DieterGraef | 0:d26c1b55cfca | 420 | * Depending on the desired length, the first few pbufs in a chain might |
DieterGraef | 0:d26c1b55cfca | 421 | * be skipped and left unchanged. The new last pbuf in the chain will be |
DieterGraef | 0:d26c1b55cfca | 422 | * resized, and any remaining pbufs will be freed. |
DieterGraef | 0:d26c1b55cfca | 423 | * |
DieterGraef | 0:d26c1b55cfca | 424 | * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted. |
DieterGraef | 0:d26c1b55cfca | 425 | * @note May not be called on a packet queue. |
DieterGraef | 0:d26c1b55cfca | 426 | * |
DieterGraef | 0:d26c1b55cfca | 427 | * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain). |
DieterGraef | 0:d26c1b55cfca | 428 | */ |
DieterGraef | 0:d26c1b55cfca | 429 | void |
DieterGraef | 0:d26c1b55cfca | 430 | pbuf_realloc(struct pbuf *p, u16_t new_len) |
DieterGraef | 0:d26c1b55cfca | 431 | { |
DieterGraef | 0:d26c1b55cfca | 432 | struct pbuf *q; |
DieterGraef | 0:d26c1b55cfca | 433 | u16_t rem_len; /* remaining length */ |
DieterGraef | 0:d26c1b55cfca | 434 | s32_t grow; |
DieterGraef | 0:d26c1b55cfca | 435 | |
DieterGraef | 0:d26c1b55cfca | 436 | LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL); |
DieterGraef | 0:d26c1b55cfca | 437 | LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL || |
DieterGraef | 0:d26c1b55cfca | 438 | p->type == PBUF_ROM || |
DieterGraef | 0:d26c1b55cfca | 439 | p->type == PBUF_RAM || |
DieterGraef | 0:d26c1b55cfca | 440 | p->type == PBUF_REF); |
DieterGraef | 0:d26c1b55cfca | 441 | |
DieterGraef | 0:d26c1b55cfca | 442 | /* desired length larger than current length? */ |
DieterGraef | 0:d26c1b55cfca | 443 | if (new_len >= p->tot_len) { |
DieterGraef | 0:d26c1b55cfca | 444 | /* enlarging not yet supported */ |
DieterGraef | 0:d26c1b55cfca | 445 | return; |
DieterGraef | 0:d26c1b55cfca | 446 | } |
DieterGraef | 0:d26c1b55cfca | 447 | |
DieterGraef | 0:d26c1b55cfca | 448 | /* the pbuf chain grows by (new_len - p->tot_len) bytes |
DieterGraef | 0:d26c1b55cfca | 449 | * (which may be negative in case of shrinking) */ |
DieterGraef | 0:d26c1b55cfca | 450 | grow = new_len - p->tot_len; |
DieterGraef | 0:d26c1b55cfca | 451 | |
DieterGraef | 0:d26c1b55cfca | 452 | /* first, step over any pbufs that should remain in the chain */ |
DieterGraef | 0:d26c1b55cfca | 453 | rem_len = new_len; |
DieterGraef | 0:d26c1b55cfca | 454 | q = p; |
DieterGraef | 0:d26c1b55cfca | 455 | /* should this pbuf be kept? */ |
DieterGraef | 0:d26c1b55cfca | 456 | while (rem_len > q->len) { |
DieterGraef | 0:d26c1b55cfca | 457 | /* decrease remaining length by pbuf length */ |
DieterGraef | 0:d26c1b55cfca | 458 | rem_len -= q->len; |
DieterGraef | 0:d26c1b55cfca | 459 | /* decrease total length indicator */ |
DieterGraef | 0:d26c1b55cfca | 460 | LWIP_ASSERT("grow < max_u16_t", grow < 0xffff); |
DieterGraef | 0:d26c1b55cfca | 461 | q->tot_len += (u16_t)grow; |
DieterGraef | 0:d26c1b55cfca | 462 | /* proceed to next pbuf in chain */ |
DieterGraef | 0:d26c1b55cfca | 463 | q = q->next; |
DieterGraef | 0:d26c1b55cfca | 464 | LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL); |
DieterGraef | 0:d26c1b55cfca | 465 | } |
DieterGraef | 0:d26c1b55cfca | 466 | /* we have now reached the new last pbuf (in q) */ |
DieterGraef | 0:d26c1b55cfca | 467 | /* rem_len == desired length for pbuf q */ |
DieterGraef | 0:d26c1b55cfca | 468 | |
DieterGraef | 0:d26c1b55cfca | 469 | /* shrink allocated memory for PBUF_RAM */ |
DieterGraef | 0:d26c1b55cfca | 470 | /* (other types merely adjust their length fields */ |
DieterGraef | 0:d26c1b55cfca | 471 | if ((q->type == PBUF_RAM) && (rem_len != q->len)) { |
DieterGraef | 0:d26c1b55cfca | 472 | /* reallocate and adjust the length of the pbuf that will be split */ |
DieterGraef | 0:d26c1b55cfca | 473 | q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len); |
DieterGraef | 0:d26c1b55cfca | 474 | LWIP_ASSERT("mem_trim returned q == NULL", q != NULL); |
DieterGraef | 0:d26c1b55cfca | 475 | } |
DieterGraef | 0:d26c1b55cfca | 476 | /* adjust length fields for new last pbuf */ |
DieterGraef | 0:d26c1b55cfca | 477 | q->len = rem_len; |
DieterGraef | 0:d26c1b55cfca | 478 | q->tot_len = q->len; |
DieterGraef | 0:d26c1b55cfca | 479 | |
DieterGraef | 0:d26c1b55cfca | 480 | /* any remaining pbufs in chain? */ |
DieterGraef | 0:d26c1b55cfca | 481 | if (q->next != NULL) { |
DieterGraef | 0:d26c1b55cfca | 482 | /* free remaining pbufs in chain */ |
DieterGraef | 0:d26c1b55cfca | 483 | pbuf_free(q->next); |
DieterGraef | 0:d26c1b55cfca | 484 | } |
DieterGraef | 0:d26c1b55cfca | 485 | /* q is last packet in chain */ |
DieterGraef | 0:d26c1b55cfca | 486 | q->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 487 | |
DieterGraef | 0:d26c1b55cfca | 488 | } |
DieterGraef | 0:d26c1b55cfca | 489 | |
DieterGraef | 0:d26c1b55cfca | 490 | /** |
DieterGraef | 0:d26c1b55cfca | 491 | * Adjusts the payload pointer to hide or reveal headers in the payload. |
DieterGraef | 0:d26c1b55cfca | 492 | * |
DieterGraef | 0:d26c1b55cfca | 493 | * Adjusts the ->payload pointer so that space for a header |
DieterGraef | 0:d26c1b55cfca | 494 | * (dis)appears in the pbuf payload. |
DieterGraef | 0:d26c1b55cfca | 495 | * |
DieterGraef | 0:d26c1b55cfca | 496 | * The ->payload, ->tot_len and ->len fields are adjusted. |
DieterGraef | 0:d26c1b55cfca | 497 | * |
DieterGraef | 0:d26c1b55cfca | 498 | * @param p pbuf to change the header size. |
DieterGraef | 0:d26c1b55cfca | 499 | * @param header_size_increment Number of bytes to increment header size which |
DieterGraef | 0:d26c1b55cfca | 500 | * increases the size of the pbuf. New space is on the front. |
DieterGraef | 0:d26c1b55cfca | 501 | * (Using a negative value decreases the header size.) |
DieterGraef | 0:d26c1b55cfca | 502 | * If hdr_size_inc is 0, this function does nothing and returns succesful. |
DieterGraef | 0:d26c1b55cfca | 503 | * |
DieterGraef | 0:d26c1b55cfca | 504 | * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so |
DieterGraef | 0:d26c1b55cfca | 505 | * the call will fail. A check is made that the increase in header size does |
DieterGraef | 0:d26c1b55cfca | 506 | * not move the payload pointer in front of the start of the buffer. |
DieterGraef | 0:d26c1b55cfca | 507 | * @return non-zero on failure, zero on success. |
DieterGraef | 0:d26c1b55cfca | 508 | * |
DieterGraef | 0:d26c1b55cfca | 509 | */ |
DieterGraef | 0:d26c1b55cfca | 510 | u8_t |
DieterGraef | 0:d26c1b55cfca | 511 | pbuf_header(struct pbuf *p, s16_t header_size_increment) |
DieterGraef | 0:d26c1b55cfca | 512 | { |
DieterGraef | 0:d26c1b55cfca | 513 | u16_t type; |
DieterGraef | 0:d26c1b55cfca | 514 | void *payload; |
DieterGraef | 0:d26c1b55cfca | 515 | u16_t increment_magnitude; |
DieterGraef | 0:d26c1b55cfca | 516 | |
DieterGraef | 0:d26c1b55cfca | 517 | LWIP_ASSERT("p != NULL", p != NULL); |
DieterGraef | 0:d26c1b55cfca | 518 | if ((header_size_increment == 0) || (p == NULL)) { |
DieterGraef | 0:d26c1b55cfca | 519 | return 0; |
DieterGraef | 0:d26c1b55cfca | 520 | } |
DieterGraef | 0:d26c1b55cfca | 521 | |
DieterGraef | 0:d26c1b55cfca | 522 | if (header_size_increment < 0){ |
DieterGraef | 0:d26c1b55cfca | 523 | increment_magnitude = -header_size_increment; |
DieterGraef | 0:d26c1b55cfca | 524 | /* Check that we aren't going to move off the end of the pbuf */ |
DieterGraef | 0:d26c1b55cfca | 525 | LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;); |
DieterGraef | 0:d26c1b55cfca | 526 | } else { |
DieterGraef | 0:d26c1b55cfca | 527 | increment_magnitude = header_size_increment; |
DieterGraef | 0:d26c1b55cfca | 528 | #if 0 |
DieterGraef | 0:d26c1b55cfca | 529 | /* Can't assert these as some callers speculatively call |
DieterGraef | 0:d26c1b55cfca | 530 | pbuf_header() to see if it's OK. Will return 1 below instead. */ |
DieterGraef | 0:d26c1b55cfca | 531 | /* Check that we've got the correct type of pbuf to work with */ |
DieterGraef | 0:d26c1b55cfca | 532 | LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL", |
DieterGraef | 0:d26c1b55cfca | 533 | p->type == PBUF_RAM || p->type == PBUF_POOL); |
DieterGraef | 0:d26c1b55cfca | 534 | /* Check that we aren't going to move off the beginning of the pbuf */ |
DieterGraef | 0:d26c1b55cfca | 535 | LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF", |
DieterGraef | 0:d26c1b55cfca | 536 | (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF); |
DieterGraef | 0:d26c1b55cfca | 537 | #endif |
DieterGraef | 0:d26c1b55cfca | 538 | } |
DieterGraef | 0:d26c1b55cfca | 539 | |
DieterGraef | 0:d26c1b55cfca | 540 | type = p->type; |
DieterGraef | 0:d26c1b55cfca | 541 | /* remember current payload pointer */ |
DieterGraef | 0:d26c1b55cfca | 542 | payload = p->payload; |
DieterGraef | 0:d26c1b55cfca | 543 | |
DieterGraef | 0:d26c1b55cfca | 544 | /* pbuf types containing payloads? */ |
DieterGraef | 0:d26c1b55cfca | 545 | if (type == PBUF_RAM || type == PBUF_POOL) { |
DieterGraef | 0:d26c1b55cfca | 546 | /* set new payload pointer */ |
DieterGraef | 0:d26c1b55cfca | 547 | p->payload = (u8_t *)p->payload - header_size_increment; |
DieterGraef | 0:d26c1b55cfca | 548 | /* boundary check fails? */ |
DieterGraef | 0:d26c1b55cfca | 549 | if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) { |
DieterGraef | 0:d26c1b55cfca | 550 | LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
DieterGraef | 0:d26c1b55cfca | 551 | ("pbuf_header: failed as %p < %p (not enough space for new header size)\n", |
DieterGraef | 0:d26c1b55cfca | 552 | (void *)p->payload, (void *)(p + 1))); |
DieterGraef | 0:d26c1b55cfca | 553 | /* restore old payload pointer */ |
DieterGraef | 0:d26c1b55cfca | 554 | p->payload = payload; |
DieterGraef | 0:d26c1b55cfca | 555 | /* bail out unsuccesfully */ |
DieterGraef | 0:d26c1b55cfca | 556 | return 1; |
DieterGraef | 0:d26c1b55cfca | 557 | } |
DieterGraef | 0:d26c1b55cfca | 558 | /* pbuf types refering to external payloads? */ |
DieterGraef | 0:d26c1b55cfca | 559 | } else if (type == PBUF_REF || type == PBUF_ROM) { |
DieterGraef | 0:d26c1b55cfca | 560 | /* hide a header in the payload? */ |
DieterGraef | 0:d26c1b55cfca | 561 | if ((header_size_increment < 0) && (increment_magnitude <= p->len)) { |
DieterGraef | 0:d26c1b55cfca | 562 | /* increase payload pointer */ |
DieterGraef | 0:d26c1b55cfca | 563 | p->payload = (u8_t *)p->payload - header_size_increment; |
DieterGraef | 0:d26c1b55cfca | 564 | } else { |
DieterGraef | 0:d26c1b55cfca | 565 | /* cannot expand payload to front (yet!) |
DieterGraef | 0:d26c1b55cfca | 566 | * bail out unsuccesfully */ |
DieterGraef | 0:d26c1b55cfca | 567 | return 1; |
DieterGraef | 0:d26c1b55cfca | 568 | } |
DieterGraef | 0:d26c1b55cfca | 569 | } else { |
DieterGraef | 0:d26c1b55cfca | 570 | /* Unknown type */ |
DieterGraef | 0:d26c1b55cfca | 571 | LWIP_ASSERT("bad pbuf type", 0); |
DieterGraef | 0:d26c1b55cfca | 572 | return 1; |
DieterGraef | 0:d26c1b55cfca | 573 | } |
DieterGraef | 0:d26c1b55cfca | 574 | /* modify pbuf length fields */ |
DieterGraef | 0:d26c1b55cfca | 575 | p->len += header_size_increment; |
DieterGraef | 0:d26c1b55cfca | 576 | p->tot_len += header_size_increment; |
DieterGraef | 0:d26c1b55cfca | 577 | |
DieterGraef | 0:d26c1b55cfca | 578 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n", |
DieterGraef | 0:d26c1b55cfca | 579 | (void *)payload, (void *)p->payload, header_size_increment)); |
DieterGraef | 0:d26c1b55cfca | 580 | |
DieterGraef | 0:d26c1b55cfca | 581 | return 0; |
DieterGraef | 0:d26c1b55cfca | 582 | } |
DieterGraef | 0:d26c1b55cfca | 583 | |
DieterGraef | 0:d26c1b55cfca | 584 | /** |
DieterGraef | 0:d26c1b55cfca | 585 | * Dereference a pbuf chain or queue and deallocate any no-longer-used |
DieterGraef | 0:d26c1b55cfca | 586 | * pbufs at the head of this chain or queue. |
DieterGraef | 0:d26c1b55cfca | 587 | * |
DieterGraef | 0:d26c1b55cfca | 588 | * Decrements the pbuf reference count. If it reaches zero, the pbuf is |
DieterGraef | 0:d26c1b55cfca | 589 | * deallocated. |
DieterGraef | 0:d26c1b55cfca | 590 | * |
DieterGraef | 0:d26c1b55cfca | 591 | * For a pbuf chain, this is repeated for each pbuf in the chain, |
DieterGraef | 0:d26c1b55cfca | 592 | * up to the first pbuf which has a non-zero reference count after |
DieterGraef | 0:d26c1b55cfca | 593 | * decrementing. So, when all reference counts are one, the whole |
DieterGraef | 0:d26c1b55cfca | 594 | * chain is free'd. |
DieterGraef | 0:d26c1b55cfca | 595 | * |
DieterGraef | 0:d26c1b55cfca | 596 | * @param p The pbuf (chain) to be dereferenced. |
DieterGraef | 0:d26c1b55cfca | 597 | * |
DieterGraef | 0:d26c1b55cfca | 598 | * @return the number of pbufs that were de-allocated |
DieterGraef | 0:d26c1b55cfca | 599 | * from the head of the chain. |
DieterGraef | 0:d26c1b55cfca | 600 | * |
DieterGraef | 0:d26c1b55cfca | 601 | * @note MUST NOT be called on a packet queue (Not verified to work yet). |
DieterGraef | 0:d26c1b55cfca | 602 | * @note the reference counter of a pbuf equals the number of pointers |
DieterGraef | 0:d26c1b55cfca | 603 | * that refer to the pbuf (or into the pbuf). |
DieterGraef | 0:d26c1b55cfca | 604 | * |
DieterGraef | 0:d26c1b55cfca | 605 | * @internal examples: |
DieterGraef | 0:d26c1b55cfca | 606 | * |
DieterGraef | 0:d26c1b55cfca | 607 | * Assuming existing chains a->b->c with the following reference |
DieterGraef | 0:d26c1b55cfca | 608 | * counts, calling pbuf_free(a) results in: |
DieterGraef | 0:d26c1b55cfca | 609 | * |
DieterGraef | 0:d26c1b55cfca | 610 | * 1->2->3 becomes ...1->3 |
DieterGraef | 0:d26c1b55cfca | 611 | * 3->3->3 becomes 2->3->3 |
DieterGraef | 0:d26c1b55cfca | 612 | * 1->1->2 becomes ......1 |
DieterGraef | 0:d26c1b55cfca | 613 | * 2->1->1 becomes 1->1->1 |
DieterGraef | 0:d26c1b55cfca | 614 | * 1->1->1 becomes ....... |
DieterGraef | 0:d26c1b55cfca | 615 | * |
DieterGraef | 0:d26c1b55cfca | 616 | */ |
DieterGraef | 0:d26c1b55cfca | 617 | u8_t |
DieterGraef | 0:d26c1b55cfca | 618 | pbuf_free(struct pbuf *p) |
DieterGraef | 0:d26c1b55cfca | 619 | { |
DieterGraef | 0:d26c1b55cfca | 620 | u16_t type; |
DieterGraef | 0:d26c1b55cfca | 621 | struct pbuf *q; |
DieterGraef | 0:d26c1b55cfca | 622 | u8_t count; |
DieterGraef | 0:d26c1b55cfca | 623 | |
DieterGraef | 0:d26c1b55cfca | 624 | if (p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 625 | LWIP_ASSERT("p != NULL", p != NULL); |
DieterGraef | 0:d26c1b55cfca | 626 | /* if assertions are disabled, proceed with debug output */ |
DieterGraef | 0:d26c1b55cfca | 627 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
DieterGraef | 0:d26c1b55cfca | 628 | ("pbuf_free(p == NULL) was called.\n")); |
DieterGraef | 0:d26c1b55cfca | 629 | return 0; |
DieterGraef | 0:d26c1b55cfca | 630 | } |
DieterGraef | 0:d26c1b55cfca | 631 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p)); |
DieterGraef | 0:d26c1b55cfca | 632 | |
DieterGraef | 0:d26c1b55cfca | 633 | PERF_START; |
DieterGraef | 0:d26c1b55cfca | 634 | |
DieterGraef | 0:d26c1b55cfca | 635 | LWIP_ASSERT("pbuf_free: sane type", |
DieterGraef | 0:d26c1b55cfca | 636 | p->type == PBUF_RAM || p->type == PBUF_ROM || |
DieterGraef | 0:d26c1b55cfca | 637 | p->type == PBUF_REF || p->type == PBUF_POOL); |
DieterGraef | 0:d26c1b55cfca | 638 | |
DieterGraef | 0:d26c1b55cfca | 639 | count = 0; |
DieterGraef | 0:d26c1b55cfca | 640 | /* de-allocate all consecutive pbufs from the head of the chain that |
DieterGraef | 0:d26c1b55cfca | 641 | * obtain a zero reference count after decrementing*/ |
DieterGraef | 0:d26c1b55cfca | 642 | while (p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 643 | u16_t ref; |
DieterGraef | 0:d26c1b55cfca | 644 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 645 | /* Since decrementing ref cannot be guaranteed to be a single machine operation |
DieterGraef | 0:d26c1b55cfca | 646 | * we must protect it. We put the new ref into a local variable to prevent |
DieterGraef | 0:d26c1b55cfca | 647 | * further protection. */ |
DieterGraef | 0:d26c1b55cfca | 648 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 649 | /* all pbufs in a chain are referenced at least once */ |
DieterGraef | 0:d26c1b55cfca | 650 | LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0); |
DieterGraef | 0:d26c1b55cfca | 651 | /* decrease reference count (number of pointers to pbuf) */ |
DieterGraef | 0:d26c1b55cfca | 652 | ref = --(p->ref); |
DieterGraef | 0:d26c1b55cfca | 653 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 654 | /* this pbuf is no longer referenced to? */ |
DieterGraef | 0:d26c1b55cfca | 655 | if (ref == 0) { |
DieterGraef | 0:d26c1b55cfca | 656 | /* remember next pbuf in chain for next iteration */ |
DieterGraef | 0:d26c1b55cfca | 657 | q = p->next; |
DieterGraef | 0:d26c1b55cfca | 658 | LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p)); |
DieterGraef | 0:d26c1b55cfca | 659 | type = p->type; |
DieterGraef | 0:d26c1b55cfca | 660 | #if LWIP_SUPPORT_CUSTOM_PBUF |
DieterGraef | 0:d26c1b55cfca | 661 | /* is this a custom pbuf? */ |
DieterGraef | 0:d26c1b55cfca | 662 | if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) { |
DieterGraef | 0:d26c1b55cfca | 663 | struct pbuf_custom *pc = (struct pbuf_custom*)p; |
DieterGraef | 0:d26c1b55cfca | 664 | LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL); |
DieterGraef | 0:d26c1b55cfca | 665 | pc->custom_free_function(p); |
DieterGraef | 0:d26c1b55cfca | 666 | } else |
DieterGraef | 0:d26c1b55cfca | 667 | #endif /* LWIP_SUPPORT_CUSTOM_PBUF */ |
DieterGraef | 0:d26c1b55cfca | 668 | { |
DieterGraef | 0:d26c1b55cfca | 669 | /* is this a pbuf from the pool? */ |
DieterGraef | 0:d26c1b55cfca | 670 | if (type == PBUF_POOL) { |
DieterGraef | 0:d26c1b55cfca | 671 | memp_free(MEMP_PBUF_POOL, p); |
DieterGraef | 0:d26c1b55cfca | 672 | /* is this a ROM or RAM referencing pbuf? */ |
DieterGraef | 0:d26c1b55cfca | 673 | } else if (type == PBUF_ROM || type == PBUF_REF) { |
DieterGraef | 0:d26c1b55cfca | 674 | memp_free(MEMP_PBUF, p); |
DieterGraef | 0:d26c1b55cfca | 675 | /* type == PBUF_RAM */ |
DieterGraef | 0:d26c1b55cfca | 676 | } else { |
DieterGraef | 0:d26c1b55cfca | 677 | mem_free(p); |
DieterGraef | 0:d26c1b55cfca | 678 | } |
DieterGraef | 0:d26c1b55cfca | 679 | } |
DieterGraef | 0:d26c1b55cfca | 680 | count++; |
DieterGraef | 0:d26c1b55cfca | 681 | /* proceed to next pbuf */ |
DieterGraef | 0:d26c1b55cfca | 682 | p = q; |
DieterGraef | 0:d26c1b55cfca | 683 | /* p->ref > 0, this pbuf is still referenced to */ |
DieterGraef | 0:d26c1b55cfca | 684 | /* (and so the remaining pbufs in chain as well) */ |
DieterGraef | 0:d26c1b55cfca | 685 | } else { |
DieterGraef | 0:d26c1b55cfca | 686 | LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref)); |
DieterGraef | 0:d26c1b55cfca | 687 | /* stop walking through the chain */ |
DieterGraef | 0:d26c1b55cfca | 688 | p = NULL; |
DieterGraef | 0:d26c1b55cfca | 689 | } |
DieterGraef | 0:d26c1b55cfca | 690 | } |
DieterGraef | 0:d26c1b55cfca | 691 | PERF_STOP("pbuf_free"); |
DieterGraef | 0:d26c1b55cfca | 692 | /* return number of de-allocated pbufs */ |
DieterGraef | 0:d26c1b55cfca | 693 | return count; |
DieterGraef | 0:d26c1b55cfca | 694 | } |
DieterGraef | 0:d26c1b55cfca | 695 | |
DieterGraef | 0:d26c1b55cfca | 696 | /** |
DieterGraef | 0:d26c1b55cfca | 697 | * Count number of pbufs in a chain |
DieterGraef | 0:d26c1b55cfca | 698 | * |
DieterGraef | 0:d26c1b55cfca | 699 | * @param p first pbuf of chain |
DieterGraef | 0:d26c1b55cfca | 700 | * @return the number of pbufs in a chain |
DieterGraef | 0:d26c1b55cfca | 701 | */ |
DieterGraef | 0:d26c1b55cfca | 702 | |
DieterGraef | 0:d26c1b55cfca | 703 | u8_t |
DieterGraef | 0:d26c1b55cfca | 704 | pbuf_clen(struct pbuf *p) |
DieterGraef | 0:d26c1b55cfca | 705 | { |
DieterGraef | 0:d26c1b55cfca | 706 | u8_t len; |
DieterGraef | 0:d26c1b55cfca | 707 | |
DieterGraef | 0:d26c1b55cfca | 708 | len = 0; |
DieterGraef | 0:d26c1b55cfca | 709 | while (p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 710 | ++len; |
DieterGraef | 0:d26c1b55cfca | 711 | p = p->next; |
DieterGraef | 0:d26c1b55cfca | 712 | } |
DieterGraef | 0:d26c1b55cfca | 713 | return len; |
DieterGraef | 0:d26c1b55cfca | 714 | } |
DieterGraef | 0:d26c1b55cfca | 715 | |
DieterGraef | 0:d26c1b55cfca | 716 | /** |
DieterGraef | 0:d26c1b55cfca | 717 | * Increment the reference count of the pbuf. |
DieterGraef | 0:d26c1b55cfca | 718 | * |
DieterGraef | 0:d26c1b55cfca | 719 | * @param p pbuf to increase reference counter of |
DieterGraef | 0:d26c1b55cfca | 720 | * |
DieterGraef | 0:d26c1b55cfca | 721 | */ |
DieterGraef | 0:d26c1b55cfca | 722 | void |
DieterGraef | 0:d26c1b55cfca | 723 | pbuf_ref(struct pbuf *p) |
DieterGraef | 0:d26c1b55cfca | 724 | { |
DieterGraef | 0:d26c1b55cfca | 725 | SYS_ARCH_DECL_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 726 | /* pbuf given? */ |
DieterGraef | 0:d26c1b55cfca | 727 | if (p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 728 | SYS_ARCH_PROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 729 | ++(p->ref); |
DieterGraef | 0:d26c1b55cfca | 730 | SYS_ARCH_UNPROTECT(old_level); |
DieterGraef | 0:d26c1b55cfca | 731 | } |
DieterGraef | 0:d26c1b55cfca | 732 | } |
DieterGraef | 0:d26c1b55cfca | 733 | |
DieterGraef | 0:d26c1b55cfca | 734 | /** |
DieterGraef | 0:d26c1b55cfca | 735 | * Concatenate two pbufs (each may be a pbuf chain) and take over |
DieterGraef | 0:d26c1b55cfca | 736 | * the caller's reference of the tail pbuf. |
DieterGraef | 0:d26c1b55cfca | 737 | * |
DieterGraef | 0:d26c1b55cfca | 738 | * @note The caller MAY NOT reference the tail pbuf afterwards. |
DieterGraef | 0:d26c1b55cfca | 739 | * Use pbuf_chain() for that purpose. |
DieterGraef | 0:d26c1b55cfca | 740 | * |
DieterGraef | 0:d26c1b55cfca | 741 | * @see pbuf_chain() |
DieterGraef | 0:d26c1b55cfca | 742 | */ |
DieterGraef | 0:d26c1b55cfca | 743 | |
DieterGraef | 0:d26c1b55cfca | 744 | void |
DieterGraef | 0:d26c1b55cfca | 745 | pbuf_cat(struct pbuf *h, struct pbuf *t) |
DieterGraef | 0:d26c1b55cfca | 746 | { |
DieterGraef | 0:d26c1b55cfca | 747 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 748 | |
DieterGraef | 0:d26c1b55cfca | 749 | LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)", |
DieterGraef | 0:d26c1b55cfca | 750 | ((h != NULL) && (t != NULL)), return;); |
DieterGraef | 0:d26c1b55cfca | 751 | |
DieterGraef | 0:d26c1b55cfca | 752 | /* proceed to last pbuf of chain */ |
DieterGraef | 0:d26c1b55cfca | 753 | for (p = h; p->next != NULL; p = p->next) { |
DieterGraef | 0:d26c1b55cfca | 754 | /* add total length of second chain to all totals of first chain */ |
DieterGraef | 0:d26c1b55cfca | 755 | p->tot_len += t->tot_len; |
DieterGraef | 0:d26c1b55cfca | 756 | } |
DieterGraef | 0:d26c1b55cfca | 757 | /* { p is last pbuf of first h chain, p->next == NULL } */ |
DieterGraef | 0:d26c1b55cfca | 758 | LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len); |
DieterGraef | 0:d26c1b55cfca | 759 | LWIP_ASSERT("p->next == NULL", p->next == NULL); |
DieterGraef | 0:d26c1b55cfca | 760 | /* add total length of second chain to last pbuf total of first chain */ |
DieterGraef | 0:d26c1b55cfca | 761 | p->tot_len += t->tot_len; |
DieterGraef | 0:d26c1b55cfca | 762 | /* chain last pbuf of head (p) with first of tail (t) */ |
DieterGraef | 0:d26c1b55cfca | 763 | p->next = t; |
DieterGraef | 0:d26c1b55cfca | 764 | /* p->next now references t, but the caller will drop its reference to t, |
DieterGraef | 0:d26c1b55cfca | 765 | * so netto there is no change to the reference count of t. |
DieterGraef | 0:d26c1b55cfca | 766 | */ |
DieterGraef | 0:d26c1b55cfca | 767 | } |
DieterGraef | 0:d26c1b55cfca | 768 | |
DieterGraef | 0:d26c1b55cfca | 769 | /** |
DieterGraef | 0:d26c1b55cfca | 770 | * Chain two pbufs (or pbuf chains) together. |
DieterGraef | 0:d26c1b55cfca | 771 | * |
DieterGraef | 0:d26c1b55cfca | 772 | * The caller MUST call pbuf_free(t) once it has stopped |
DieterGraef | 0:d26c1b55cfca | 773 | * using it. Use pbuf_cat() instead if you no longer use t. |
DieterGraef | 0:d26c1b55cfca | 774 | * |
DieterGraef | 0:d26c1b55cfca | 775 | * @param h head pbuf (chain) |
DieterGraef | 0:d26c1b55cfca | 776 | * @param t tail pbuf (chain) |
DieterGraef | 0:d26c1b55cfca | 777 | * @note The pbufs MUST belong to the same packet. |
DieterGraef | 0:d26c1b55cfca | 778 | * @note MAY NOT be called on a packet queue. |
DieterGraef | 0:d26c1b55cfca | 779 | * |
DieterGraef | 0:d26c1b55cfca | 780 | * The ->tot_len fields of all pbufs of the head chain are adjusted. |
DieterGraef | 0:d26c1b55cfca | 781 | * The ->next field of the last pbuf of the head chain is adjusted. |
DieterGraef | 0:d26c1b55cfca | 782 | * The ->ref field of the first pbuf of the tail chain is adjusted. |
DieterGraef | 0:d26c1b55cfca | 783 | * |
DieterGraef | 0:d26c1b55cfca | 784 | */ |
DieterGraef | 0:d26c1b55cfca | 785 | void |
DieterGraef | 0:d26c1b55cfca | 786 | pbuf_chain(struct pbuf *h, struct pbuf *t) |
DieterGraef | 0:d26c1b55cfca | 787 | { |
DieterGraef | 0:d26c1b55cfca | 788 | pbuf_cat(h, t); |
DieterGraef | 0:d26c1b55cfca | 789 | /* t is now referenced by h */ |
DieterGraef | 0:d26c1b55cfca | 790 | pbuf_ref(t); |
DieterGraef | 0:d26c1b55cfca | 791 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t)); |
DieterGraef | 0:d26c1b55cfca | 792 | } |
DieterGraef | 0:d26c1b55cfca | 793 | |
DieterGraef | 0:d26c1b55cfca | 794 | /** |
DieterGraef | 0:d26c1b55cfca | 795 | * Dechains the first pbuf from its succeeding pbufs in the chain. |
DieterGraef | 0:d26c1b55cfca | 796 | * |
DieterGraef | 0:d26c1b55cfca | 797 | * Makes p->tot_len field equal to p->len. |
DieterGraef | 0:d26c1b55cfca | 798 | * @param p pbuf to dechain |
DieterGraef | 0:d26c1b55cfca | 799 | * @return remainder of the pbuf chain, or NULL if it was de-allocated. |
DieterGraef | 0:d26c1b55cfca | 800 | * @note May not be called on a packet queue. |
DieterGraef | 0:d26c1b55cfca | 801 | */ |
DieterGraef | 0:d26c1b55cfca | 802 | struct pbuf * |
DieterGraef | 0:d26c1b55cfca | 803 | pbuf_dechain(struct pbuf *p) |
DieterGraef | 0:d26c1b55cfca | 804 | { |
DieterGraef | 0:d26c1b55cfca | 805 | struct pbuf *q; |
DieterGraef | 0:d26c1b55cfca | 806 | u8_t tail_gone = 1; |
DieterGraef | 0:d26c1b55cfca | 807 | /* tail */ |
DieterGraef | 0:d26c1b55cfca | 808 | q = p->next; |
DieterGraef | 0:d26c1b55cfca | 809 | /* pbuf has successor in chain? */ |
DieterGraef | 0:d26c1b55cfca | 810 | if (q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 811 | /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */ |
DieterGraef | 0:d26c1b55cfca | 812 | LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len); |
DieterGraef | 0:d26c1b55cfca | 813 | /* enforce invariant if assertion is disabled */ |
DieterGraef | 0:d26c1b55cfca | 814 | q->tot_len = p->tot_len - p->len; |
DieterGraef | 0:d26c1b55cfca | 815 | /* decouple pbuf from remainder */ |
DieterGraef | 0:d26c1b55cfca | 816 | p->next = NULL; |
DieterGraef | 0:d26c1b55cfca | 817 | /* total length of pbuf p is its own length only */ |
DieterGraef | 0:d26c1b55cfca | 818 | p->tot_len = p->len; |
DieterGraef | 0:d26c1b55cfca | 819 | /* q is no longer referenced by p, free it */ |
DieterGraef | 0:d26c1b55cfca | 820 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q)); |
DieterGraef | 0:d26c1b55cfca | 821 | tail_gone = pbuf_free(q); |
DieterGraef | 0:d26c1b55cfca | 822 | if (tail_gone > 0) { |
DieterGraef | 0:d26c1b55cfca | 823 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, |
DieterGraef | 0:d26c1b55cfca | 824 | ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q)); |
DieterGraef | 0:d26c1b55cfca | 825 | } |
DieterGraef | 0:d26c1b55cfca | 826 | /* return remaining tail or NULL if deallocated */ |
DieterGraef | 0:d26c1b55cfca | 827 | } |
DieterGraef | 0:d26c1b55cfca | 828 | /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */ |
DieterGraef | 0:d26c1b55cfca | 829 | LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len); |
DieterGraef | 0:d26c1b55cfca | 830 | return ((tail_gone > 0) ? NULL : q); |
DieterGraef | 0:d26c1b55cfca | 831 | } |
DieterGraef | 0:d26c1b55cfca | 832 | |
DieterGraef | 0:d26c1b55cfca | 833 | /** |
DieterGraef | 0:d26c1b55cfca | 834 | * |
DieterGraef | 0:d26c1b55cfca | 835 | * Create PBUF_RAM copies of pbufs. |
DieterGraef | 0:d26c1b55cfca | 836 | * |
DieterGraef | 0:d26c1b55cfca | 837 | * Used to queue packets on behalf of the lwIP stack, such as |
DieterGraef | 0:d26c1b55cfca | 838 | * ARP based queueing. |
DieterGraef | 0:d26c1b55cfca | 839 | * |
DieterGraef | 0:d26c1b55cfca | 840 | * @note You MUST explicitly use p = pbuf_take(p); |
DieterGraef | 0:d26c1b55cfca | 841 | * |
DieterGraef | 0:d26c1b55cfca | 842 | * @note Only one packet is copied, no packet queue! |
DieterGraef | 0:d26c1b55cfca | 843 | * |
DieterGraef | 0:d26c1b55cfca | 844 | * @param p_to pbuf destination of the copy |
DieterGraef | 0:d26c1b55cfca | 845 | * @param p_from pbuf source of the copy |
DieterGraef | 0:d26c1b55cfca | 846 | * |
DieterGraef | 0:d26c1b55cfca | 847 | * @return ERR_OK if pbuf was copied |
DieterGraef | 0:d26c1b55cfca | 848 | * ERR_ARG if one of the pbufs is NULL or p_to is not big |
DieterGraef | 0:d26c1b55cfca | 849 | * enough to hold p_from |
DieterGraef | 0:d26c1b55cfca | 850 | */ |
DieterGraef | 0:d26c1b55cfca | 851 | err_t |
DieterGraef | 0:d26c1b55cfca | 852 | pbuf_copy(struct pbuf *p_to, struct pbuf *p_from) |
DieterGraef | 0:d26c1b55cfca | 853 | { |
DieterGraef | 0:d26c1b55cfca | 854 | u16_t offset_to=0, offset_from=0, len; |
DieterGraef | 0:d26c1b55cfca | 855 | |
DieterGraef | 0:d26c1b55cfca | 856 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n", |
DieterGraef | 0:d26c1b55cfca | 857 | (void*)p_to, (void*)p_from)); |
DieterGraef | 0:d26c1b55cfca | 858 | |
DieterGraef | 0:d26c1b55cfca | 859 | /* is the target big enough to hold the source? */ |
DieterGraef | 0:d26c1b55cfca | 860 | LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) && |
DieterGraef | 0:d26c1b55cfca | 861 | (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;); |
DieterGraef | 0:d26c1b55cfca | 862 | |
DieterGraef | 0:d26c1b55cfca | 863 | /* iterate through pbuf chain */ |
DieterGraef | 0:d26c1b55cfca | 864 | do |
DieterGraef | 0:d26c1b55cfca | 865 | { |
DieterGraef | 0:d26c1b55cfca | 866 | /* copy one part of the original chain */ |
DieterGraef | 0:d26c1b55cfca | 867 | if ((p_to->len - offset_to) >= (p_from->len - offset_from)) { |
DieterGraef | 0:d26c1b55cfca | 868 | /* complete current p_from fits into current p_to */ |
DieterGraef | 0:d26c1b55cfca | 869 | len = p_from->len - offset_from; |
DieterGraef | 0:d26c1b55cfca | 870 | } else { |
DieterGraef | 0:d26c1b55cfca | 871 | /* current p_from does not fit into current p_to */ |
DieterGraef | 0:d26c1b55cfca | 872 | len = p_to->len - offset_to; |
DieterGraef | 0:d26c1b55cfca | 873 | } |
DieterGraef | 0:d26c1b55cfca | 874 | MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len); |
DieterGraef | 0:d26c1b55cfca | 875 | offset_to += len; |
DieterGraef | 0:d26c1b55cfca | 876 | offset_from += len; |
DieterGraef | 0:d26c1b55cfca | 877 | LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len); |
DieterGraef | 0:d26c1b55cfca | 878 | LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len); |
DieterGraef | 0:d26c1b55cfca | 879 | if (offset_from >= p_from->len) { |
DieterGraef | 0:d26c1b55cfca | 880 | /* on to next p_from (if any) */ |
DieterGraef | 0:d26c1b55cfca | 881 | offset_from = 0; |
DieterGraef | 0:d26c1b55cfca | 882 | p_from = p_from->next; |
DieterGraef | 0:d26c1b55cfca | 883 | } |
DieterGraef | 0:d26c1b55cfca | 884 | if (offset_to == p_to->len) { |
DieterGraef | 0:d26c1b55cfca | 885 | /* on to next p_to (if any) */ |
DieterGraef | 0:d26c1b55cfca | 886 | offset_to = 0; |
DieterGraef | 0:d26c1b55cfca | 887 | p_to = p_to->next; |
DieterGraef | 0:d26c1b55cfca | 888 | LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL) , return ERR_ARG;); |
DieterGraef | 0:d26c1b55cfca | 889 | } |
DieterGraef | 0:d26c1b55cfca | 890 | |
DieterGraef | 0:d26c1b55cfca | 891 | if((p_from != NULL) && (p_from->len == p_from->tot_len)) { |
DieterGraef | 0:d26c1b55cfca | 892 | /* don't copy more than one packet! */ |
DieterGraef | 0:d26c1b55cfca | 893 | LWIP_ERROR("pbuf_copy() does not allow packet queues!\n", |
DieterGraef | 0:d26c1b55cfca | 894 | (p_from->next == NULL), return ERR_VAL;); |
DieterGraef | 0:d26c1b55cfca | 895 | } |
DieterGraef | 0:d26c1b55cfca | 896 | if((p_to != NULL) && (p_to->len == p_to->tot_len)) { |
DieterGraef | 0:d26c1b55cfca | 897 | /* don't copy more than one packet! */ |
DieterGraef | 0:d26c1b55cfca | 898 | LWIP_ERROR("pbuf_copy() does not allow packet queues!\n", |
DieterGraef | 0:d26c1b55cfca | 899 | (p_to->next == NULL), return ERR_VAL;); |
DieterGraef | 0:d26c1b55cfca | 900 | } |
DieterGraef | 0:d26c1b55cfca | 901 | } while (p_from); |
DieterGraef | 0:d26c1b55cfca | 902 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n")); |
DieterGraef | 0:d26c1b55cfca | 903 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 904 | } |
DieterGraef | 0:d26c1b55cfca | 905 | |
DieterGraef | 0:d26c1b55cfca | 906 | /** |
DieterGraef | 0:d26c1b55cfca | 907 | * Copy (part of) the contents of a packet buffer |
DieterGraef | 0:d26c1b55cfca | 908 | * to an application supplied buffer. |
DieterGraef | 0:d26c1b55cfca | 909 | * |
DieterGraef | 0:d26c1b55cfca | 910 | * @param buf the pbuf from which to copy data |
DieterGraef | 0:d26c1b55cfca | 911 | * @param dataptr the application supplied buffer |
DieterGraef | 0:d26c1b55cfca | 912 | * @param len length of data to copy (dataptr must be big enough). No more |
DieterGraef | 0:d26c1b55cfca | 913 | * than buf->tot_len will be copied, irrespective of len |
DieterGraef | 0:d26c1b55cfca | 914 | * @param offset offset into the packet buffer from where to begin copying len bytes |
DieterGraef | 0:d26c1b55cfca | 915 | * @return the number of bytes copied, or 0 on failure |
DieterGraef | 0:d26c1b55cfca | 916 | */ |
DieterGraef | 0:d26c1b55cfca | 917 | u16_t |
DieterGraef | 0:d26c1b55cfca | 918 | pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset) |
DieterGraef | 0:d26c1b55cfca | 919 | { |
DieterGraef | 0:d26c1b55cfca | 920 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 921 | u16_t left; |
DieterGraef | 0:d26c1b55cfca | 922 | u16_t buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 923 | u16_t copied_total = 0; |
DieterGraef | 0:d26c1b55cfca | 924 | |
DieterGraef | 0:d26c1b55cfca | 925 | LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;); |
DieterGraef | 0:d26c1b55cfca | 926 | LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;); |
DieterGraef | 0:d26c1b55cfca | 927 | |
DieterGraef | 0:d26c1b55cfca | 928 | left = 0; |
DieterGraef | 0:d26c1b55cfca | 929 | |
DieterGraef | 0:d26c1b55cfca | 930 | if((buf == NULL) || (dataptr == NULL)) { |
DieterGraef | 0:d26c1b55cfca | 931 | return 0; |
DieterGraef | 0:d26c1b55cfca | 932 | } |
DieterGraef | 0:d26c1b55cfca | 933 | |
DieterGraef | 0:d26c1b55cfca | 934 | /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ |
DieterGraef | 0:d26c1b55cfca | 935 | for(p = buf; len != 0 && p != NULL; p = p->next) { |
DieterGraef | 0:d26c1b55cfca | 936 | if ((offset != 0) && (offset >= p->len)) { |
DieterGraef | 0:d26c1b55cfca | 937 | /* don't copy from this buffer -> on to the next */ |
DieterGraef | 0:d26c1b55cfca | 938 | offset -= p->len; |
DieterGraef | 0:d26c1b55cfca | 939 | } else { |
DieterGraef | 0:d26c1b55cfca | 940 | /* copy from this buffer. maybe only partially. */ |
DieterGraef | 0:d26c1b55cfca | 941 | buf_copy_len = p->len - offset; |
DieterGraef | 0:d26c1b55cfca | 942 | if (buf_copy_len > len) |
DieterGraef | 0:d26c1b55cfca | 943 | buf_copy_len = len; |
DieterGraef | 0:d26c1b55cfca | 944 | /* copy the necessary parts of the buffer */ |
DieterGraef | 0:d26c1b55cfca | 945 | MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len); |
DieterGraef | 0:d26c1b55cfca | 946 | copied_total += buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 947 | left += buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 948 | len -= buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 949 | offset = 0; |
DieterGraef | 0:d26c1b55cfca | 950 | } |
DieterGraef | 0:d26c1b55cfca | 951 | } |
DieterGraef | 0:d26c1b55cfca | 952 | return copied_total; |
DieterGraef | 0:d26c1b55cfca | 953 | } |
DieterGraef | 0:d26c1b55cfca | 954 | |
DieterGraef | 0:d26c1b55cfca | 955 | /** |
DieterGraef | 0:d26c1b55cfca | 956 | * Copy application supplied data into a pbuf. |
DieterGraef | 0:d26c1b55cfca | 957 | * This function can only be used to copy the equivalent of buf->tot_len data. |
DieterGraef | 0:d26c1b55cfca | 958 | * |
DieterGraef | 0:d26c1b55cfca | 959 | * @param buf pbuf to fill with data |
DieterGraef | 0:d26c1b55cfca | 960 | * @param dataptr application supplied data buffer |
DieterGraef | 0:d26c1b55cfca | 961 | * @param len length of the application supplied data buffer |
DieterGraef | 0:d26c1b55cfca | 962 | * |
DieterGraef | 0:d26c1b55cfca | 963 | * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough |
DieterGraef | 0:d26c1b55cfca | 964 | */ |
DieterGraef | 0:d26c1b55cfca | 965 | err_t |
DieterGraef | 0:d26c1b55cfca | 966 | pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) |
DieterGraef | 0:d26c1b55cfca | 967 | { |
DieterGraef | 0:d26c1b55cfca | 968 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 969 | u16_t buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 970 | u16_t total_copy_len = len; |
DieterGraef | 0:d26c1b55cfca | 971 | u16_t copied_total = 0; |
DieterGraef | 0:d26c1b55cfca | 972 | |
DieterGraef | 0:d26c1b55cfca | 973 | LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;); |
DieterGraef | 0:d26c1b55cfca | 974 | LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;); |
DieterGraef | 0:d26c1b55cfca | 975 | |
DieterGraef | 0:d26c1b55cfca | 976 | if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) { |
DieterGraef | 0:d26c1b55cfca | 977 | return ERR_ARG; |
DieterGraef | 0:d26c1b55cfca | 978 | } |
DieterGraef | 0:d26c1b55cfca | 979 | |
DieterGraef | 0:d26c1b55cfca | 980 | /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ |
DieterGraef | 0:d26c1b55cfca | 981 | for(p = buf; total_copy_len != 0; p = p->next) { |
DieterGraef | 0:d26c1b55cfca | 982 | LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL); |
DieterGraef | 0:d26c1b55cfca | 983 | buf_copy_len = total_copy_len; |
DieterGraef | 0:d26c1b55cfca | 984 | if (buf_copy_len > p->len) { |
DieterGraef | 0:d26c1b55cfca | 985 | /* this pbuf cannot hold all remaining data */ |
DieterGraef | 0:d26c1b55cfca | 986 | buf_copy_len = p->len; |
DieterGraef | 0:d26c1b55cfca | 987 | } |
DieterGraef | 0:d26c1b55cfca | 988 | /* copy the necessary parts of the buffer */ |
DieterGraef | 0:d26c1b55cfca | 989 | MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len); |
DieterGraef | 0:d26c1b55cfca | 990 | total_copy_len -= buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 991 | copied_total += buf_copy_len; |
DieterGraef | 0:d26c1b55cfca | 992 | } |
DieterGraef | 0:d26c1b55cfca | 993 | LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len); |
DieterGraef | 0:d26c1b55cfca | 994 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 995 | } |
DieterGraef | 0:d26c1b55cfca | 996 | |
DieterGraef | 0:d26c1b55cfca | 997 | /** |
DieterGraef | 0:d26c1b55cfca | 998 | * Creates a single pbuf out of a queue of pbufs. |
DieterGraef | 0:d26c1b55cfca | 999 | * |
DieterGraef | 0:d26c1b55cfca | 1000 | * @remark: Either the source pbuf 'p' is freed by this function or the original |
DieterGraef | 0:d26c1b55cfca | 1001 | * pbuf 'p' is returned, therefore the caller has to check the result! |
DieterGraef | 0:d26c1b55cfca | 1002 | * |
DieterGraef | 0:d26c1b55cfca | 1003 | * @param p the source pbuf |
DieterGraef | 0:d26c1b55cfca | 1004 | * @param layer pbuf_layer of the new pbuf |
DieterGraef | 0:d26c1b55cfca | 1005 | * |
DieterGraef | 0:d26c1b55cfca | 1006 | * @return a new, single pbuf (p->next is NULL) |
DieterGraef | 0:d26c1b55cfca | 1007 | * or the old pbuf if allocation fails |
DieterGraef | 0:d26c1b55cfca | 1008 | */ |
DieterGraef | 0:d26c1b55cfca | 1009 | struct pbuf* |
DieterGraef | 0:d26c1b55cfca | 1010 | pbuf_coalesce(struct pbuf *p, pbuf_layer layer) |
DieterGraef | 0:d26c1b55cfca | 1011 | { |
DieterGraef | 0:d26c1b55cfca | 1012 | struct pbuf *q; |
DieterGraef | 0:d26c1b55cfca | 1013 | err_t err; |
DieterGraef | 0:d26c1b55cfca | 1014 | if (p->next == NULL) { |
DieterGraef | 0:d26c1b55cfca | 1015 | return p; |
DieterGraef | 0:d26c1b55cfca | 1016 | } |
DieterGraef | 0:d26c1b55cfca | 1017 | q = pbuf_alloc(layer, p->tot_len, PBUF_RAM); |
DieterGraef | 0:d26c1b55cfca | 1018 | if (q == NULL) { |
DieterGraef | 0:d26c1b55cfca | 1019 | /* @todo: what do we do now? */ |
DieterGraef | 0:d26c1b55cfca | 1020 | return p; |
DieterGraef | 0:d26c1b55cfca | 1021 | } |
DieterGraef | 0:d26c1b55cfca | 1022 | err = pbuf_copy(q, p); |
DieterGraef | 0:d26c1b55cfca | 1023 | LWIP_ASSERT("pbuf_copy failed", err == ERR_OK); |
DieterGraef | 0:d26c1b55cfca | 1024 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 1025 | return q; |
DieterGraef | 0:d26c1b55cfca | 1026 | } |
DieterGraef | 0:d26c1b55cfca | 1027 | |
DieterGraef | 0:d26c1b55cfca | 1028 | #if LWIP_CHECKSUM_ON_COPY |
DieterGraef | 0:d26c1b55cfca | 1029 | /** |
DieterGraef | 0:d26c1b55cfca | 1030 | * Copies data into a single pbuf (*not* into a pbuf queue!) and updates |
DieterGraef | 0:d26c1b55cfca | 1031 | * the checksum while copying |
DieterGraef | 0:d26c1b55cfca | 1032 | * |
DieterGraef | 0:d26c1b55cfca | 1033 | * @param p the pbuf to copy data into |
DieterGraef | 0:d26c1b55cfca | 1034 | * @param start_offset offset of p->payload where to copy the data to |
DieterGraef | 0:d26c1b55cfca | 1035 | * @param dataptr data to copy into the pbuf |
DieterGraef | 0:d26c1b55cfca | 1036 | * @param len length of data to copy into the pbuf |
DieterGraef | 0:d26c1b55cfca | 1037 | * @param chksum pointer to the checksum which is updated |
DieterGraef | 0:d26c1b55cfca | 1038 | * @return ERR_OK if successful, another error if the data does not fit |
DieterGraef | 0:d26c1b55cfca | 1039 | * within the (first) pbuf (no pbuf queues!) |
DieterGraef | 0:d26c1b55cfca | 1040 | */ |
DieterGraef | 0:d26c1b55cfca | 1041 | err_t |
DieterGraef | 0:d26c1b55cfca | 1042 | pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr, |
DieterGraef | 0:d26c1b55cfca | 1043 | u16_t len, u16_t *chksum) |
DieterGraef | 0:d26c1b55cfca | 1044 | { |
DieterGraef | 0:d26c1b55cfca | 1045 | u32_t acc; |
DieterGraef | 0:d26c1b55cfca | 1046 | u16_t copy_chksum; |
DieterGraef | 0:d26c1b55cfca | 1047 | char *dst_ptr; |
DieterGraef | 0:d26c1b55cfca | 1048 | LWIP_ASSERT("p != NULL", p != NULL); |
DieterGraef | 0:d26c1b55cfca | 1049 | LWIP_ASSERT("dataptr != NULL", dataptr != NULL); |
DieterGraef | 0:d26c1b55cfca | 1050 | LWIP_ASSERT("chksum != NULL", chksum != NULL); |
DieterGraef | 0:d26c1b55cfca | 1051 | LWIP_ASSERT("len != 0", len != 0); |
DieterGraef | 0:d26c1b55cfca | 1052 | |
DieterGraef | 0:d26c1b55cfca | 1053 | if ((start_offset >= p->len) || (start_offset + len > p->len)) { |
DieterGraef | 0:d26c1b55cfca | 1054 | return ERR_ARG; |
DieterGraef | 0:d26c1b55cfca | 1055 | } |
DieterGraef | 0:d26c1b55cfca | 1056 | |
DieterGraef | 0:d26c1b55cfca | 1057 | dst_ptr = ((char*)p->payload) + start_offset; |
DieterGraef | 0:d26c1b55cfca | 1058 | copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len); |
DieterGraef | 0:d26c1b55cfca | 1059 | if ((start_offset & 1) != 0) { |
DieterGraef | 0:d26c1b55cfca | 1060 | copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum); |
DieterGraef | 0:d26c1b55cfca | 1061 | } |
DieterGraef | 0:d26c1b55cfca | 1062 | acc = *chksum; |
DieterGraef | 0:d26c1b55cfca | 1063 | acc += copy_chksum; |
DieterGraef | 0:d26c1b55cfca | 1064 | *chksum = FOLD_U32T(acc); |
DieterGraef | 0:d26c1b55cfca | 1065 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1066 | } |
DieterGraef | 0:d26c1b55cfca | 1067 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
DieterGraef | 0:d26c1b55cfca | 1068 | |
DieterGraef | 0:d26c1b55cfca | 1069 | /** Get one byte from the specified position in a pbuf |
DieterGraef | 0:d26c1b55cfca | 1070 | * WARNING: returns zero for offset >= p->tot_len |
DieterGraef | 0:d26c1b55cfca | 1071 | * |
DieterGraef | 0:d26c1b55cfca | 1072 | * @param p pbuf to parse |
DieterGraef | 0:d26c1b55cfca | 1073 | * @param offset offset into p of the byte to return |
DieterGraef | 0:d26c1b55cfca | 1074 | * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len |
DieterGraef | 0:d26c1b55cfca | 1075 | */ |
DieterGraef | 0:d26c1b55cfca | 1076 | u8_t |
DieterGraef | 0:d26c1b55cfca | 1077 | pbuf_get_at(struct pbuf* p, u16_t offset) |
DieterGraef | 0:d26c1b55cfca | 1078 | { |
DieterGraef | 0:d26c1b55cfca | 1079 | u16_t copy_from = offset; |
DieterGraef | 0:d26c1b55cfca | 1080 | struct pbuf* q = p; |
DieterGraef | 0:d26c1b55cfca | 1081 | |
DieterGraef | 0:d26c1b55cfca | 1082 | /* get the correct pbuf */ |
DieterGraef | 0:d26c1b55cfca | 1083 | while ((q != NULL) && (q->len <= copy_from)) { |
DieterGraef | 0:d26c1b55cfca | 1084 | copy_from -= q->len; |
DieterGraef | 0:d26c1b55cfca | 1085 | q = q->next; |
DieterGraef | 0:d26c1b55cfca | 1086 | } |
DieterGraef | 0:d26c1b55cfca | 1087 | /* return requested data if pbuf is OK */ |
DieterGraef | 0:d26c1b55cfca | 1088 | if ((q != NULL) && (q->len > copy_from)) { |
DieterGraef | 0:d26c1b55cfca | 1089 | return ((u8_t*)q->payload)[copy_from]; |
DieterGraef | 0:d26c1b55cfca | 1090 | } |
DieterGraef | 0:d26c1b55cfca | 1091 | return 0; |
DieterGraef | 0:d26c1b55cfca | 1092 | } |
DieterGraef | 0:d26c1b55cfca | 1093 | |
DieterGraef | 0:d26c1b55cfca | 1094 | /** Compare pbuf contents at specified offset with memory s2, both of length n |
DieterGraef | 0:d26c1b55cfca | 1095 | * |
DieterGraef | 0:d26c1b55cfca | 1096 | * @param p pbuf to compare |
DieterGraef | 0:d26c1b55cfca | 1097 | * @param offset offset into p at wich to start comparing |
DieterGraef | 0:d26c1b55cfca | 1098 | * @param s2 buffer to compare |
DieterGraef | 0:d26c1b55cfca | 1099 | * @param n length of buffer to compare |
DieterGraef | 0:d26c1b55cfca | 1100 | * @return zero if equal, nonzero otherwise |
DieterGraef | 0:d26c1b55cfca | 1101 | * (0xffff if p is too short, diffoffset+1 otherwise) |
DieterGraef | 0:d26c1b55cfca | 1102 | */ |
DieterGraef | 0:d26c1b55cfca | 1103 | u16_t |
DieterGraef | 0:d26c1b55cfca | 1104 | pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n) |
DieterGraef | 0:d26c1b55cfca | 1105 | { |
DieterGraef | 0:d26c1b55cfca | 1106 | u16_t start = offset; |
DieterGraef | 0:d26c1b55cfca | 1107 | struct pbuf* q = p; |
DieterGraef | 0:d26c1b55cfca | 1108 | |
DieterGraef | 0:d26c1b55cfca | 1109 | /* get the correct pbuf */ |
DieterGraef | 0:d26c1b55cfca | 1110 | while ((q != NULL) && (q->len <= start)) { |
DieterGraef | 0:d26c1b55cfca | 1111 | start -= q->len; |
DieterGraef | 0:d26c1b55cfca | 1112 | q = q->next; |
DieterGraef | 0:d26c1b55cfca | 1113 | } |
DieterGraef | 0:d26c1b55cfca | 1114 | /* return requested data if pbuf is OK */ |
DieterGraef | 0:d26c1b55cfca | 1115 | if ((q != NULL) && (q->len > start)) { |
DieterGraef | 0:d26c1b55cfca | 1116 | u16_t i; |
DieterGraef | 0:d26c1b55cfca | 1117 | for(i = 0; i < n; i++) { |
DieterGraef | 0:d26c1b55cfca | 1118 | u8_t a = pbuf_get_at(q, start + i); |
DieterGraef | 0:d26c1b55cfca | 1119 | u8_t b = ((u8_t*)s2)[i]; |
DieterGraef | 0:d26c1b55cfca | 1120 | if (a != b) { |
DieterGraef | 0:d26c1b55cfca | 1121 | return i+1; |
DieterGraef | 0:d26c1b55cfca | 1122 | } |
DieterGraef | 0:d26c1b55cfca | 1123 | } |
DieterGraef | 0:d26c1b55cfca | 1124 | return 0; |
DieterGraef | 0:d26c1b55cfca | 1125 | } |
DieterGraef | 0:d26c1b55cfca | 1126 | return 0xffff; |
DieterGraef | 0:d26c1b55cfca | 1127 | } |
DieterGraef | 0:d26c1b55cfca | 1128 | |
DieterGraef | 0:d26c1b55cfca | 1129 | /** Find occurrence of mem (with length mem_len) in pbuf p, starting at offset |
DieterGraef | 0:d26c1b55cfca | 1130 | * start_offset. |
DieterGraef | 0:d26c1b55cfca | 1131 | * |
DieterGraef | 0:d26c1b55cfca | 1132 | * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as |
DieterGraef | 0:d26c1b55cfca | 1133 | * return value 'not found' |
DieterGraef | 0:d26c1b55cfca | 1134 | * @param mem search for the contents of this buffer |
DieterGraef | 0:d26c1b55cfca | 1135 | * @param mem_len length of 'mem' |
DieterGraef | 0:d26c1b55cfca | 1136 | * @param start_offset offset into p at which to start searching |
DieterGraef | 0:d26c1b55cfca | 1137 | * @return 0xFFFF if substr was not found in p or the index where it was found |
DieterGraef | 0:d26c1b55cfca | 1138 | */ |
DieterGraef | 0:d26c1b55cfca | 1139 | u16_t |
DieterGraef | 0:d26c1b55cfca | 1140 | pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset) |
DieterGraef | 0:d26c1b55cfca | 1141 | { |
DieterGraef | 0:d26c1b55cfca | 1142 | u16_t i; |
DieterGraef | 0:d26c1b55cfca | 1143 | u16_t max = p->tot_len - mem_len; |
DieterGraef | 0:d26c1b55cfca | 1144 | if (p->tot_len >= mem_len + start_offset) { |
DieterGraef | 0:d26c1b55cfca | 1145 | for(i = start_offset; i <= max; ) { |
DieterGraef | 0:d26c1b55cfca | 1146 | u16_t plus = pbuf_memcmp(p, i, mem, mem_len); |
DieterGraef | 0:d26c1b55cfca | 1147 | if (plus == 0) { |
DieterGraef | 0:d26c1b55cfca | 1148 | return i; |
DieterGraef | 0:d26c1b55cfca | 1149 | } else { |
DieterGraef | 0:d26c1b55cfca | 1150 | i += plus; |
DieterGraef | 0:d26c1b55cfca | 1151 | } |
DieterGraef | 0:d26c1b55cfca | 1152 | } |
DieterGraef | 0:d26c1b55cfca | 1153 | } |
DieterGraef | 0:d26c1b55cfca | 1154 | return 0xFFFF; |
DieterGraef | 0:d26c1b55cfca | 1155 | } |
DieterGraef | 0:d26c1b55cfca | 1156 | |
DieterGraef | 0:d26c1b55cfca | 1157 | /** Find occurrence of substr with length substr_len in pbuf p, start at offset |
DieterGraef | 0:d26c1b55cfca | 1158 | * start_offset |
DieterGraef | 0:d26c1b55cfca | 1159 | * WARNING: in contrast to strstr(), this one does not stop at the first \0 in |
DieterGraef | 0:d26c1b55cfca | 1160 | * the pbuf/source string! |
DieterGraef | 0:d26c1b55cfca | 1161 | * |
DieterGraef | 0:d26c1b55cfca | 1162 | * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as |
DieterGraef | 0:d26c1b55cfca | 1163 | * return value 'not found' |
DieterGraef | 0:d26c1b55cfca | 1164 | * @param substr string to search for in p, maximum length is 0xFFFE |
DieterGraef | 0:d26c1b55cfca | 1165 | * @return 0xFFFF if substr was not found in p or the index where it was found |
DieterGraef | 0:d26c1b55cfca | 1166 | */ |
DieterGraef | 0:d26c1b55cfca | 1167 | u16_t |
DieterGraef | 0:d26c1b55cfca | 1168 | pbuf_strstr(struct pbuf* p, const char* substr) |
DieterGraef | 0:d26c1b55cfca | 1169 | { |
DieterGraef | 0:d26c1b55cfca | 1170 | size_t substr_len; |
DieterGraef | 0:d26c1b55cfca | 1171 | if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) { |
DieterGraef | 0:d26c1b55cfca | 1172 | return 0xFFFF; |
DieterGraef | 0:d26c1b55cfca | 1173 | } |
DieterGraef | 0:d26c1b55cfca | 1174 | substr_len = strlen(substr); |
DieterGraef | 0:d26c1b55cfca | 1175 | if (substr_len >= 0xFFFF) { |
DieterGraef | 0:d26c1b55cfca | 1176 | return 0xFFFF; |
DieterGraef | 0:d26c1b55cfca | 1177 | } |
DieterGraef | 0:d26c1b55cfca | 1178 | return pbuf_memfind(p, substr, (u16_t)substr_len, 0); |
DieterGraef | 0:d26c1b55cfca | 1179 | } |