Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

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