Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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