Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

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