This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:f30e2135b0db 1 /**
Yann 0:f30e2135b0db 2 * @file
Yann 0:f30e2135b0db 3 * Implementation of raw protocol PCBs for low-level handling of
Yann 0:f30e2135b0db 4 * different types of protocols besides (or overriding) those
Yann 0:f30e2135b0db 5 * already available in lwIP.
Yann 0:f30e2135b0db 6 *
Yann 0:f30e2135b0db 7 */
Yann 0:f30e2135b0db 8
Yann 0:f30e2135b0db 9 /*
Yann 0:f30e2135b0db 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
Yann 0:f30e2135b0db 11 * All rights reserved.
Yann 0:f30e2135b0db 12 *
Yann 0:f30e2135b0db 13 * Redistribution and use in source and binary forms, with or without modification,
Yann 0:f30e2135b0db 14 * are permitted provided that the following conditions are met:
Yann 0:f30e2135b0db 15 *
Yann 0:f30e2135b0db 16 * 1. Redistributions of source code must retain the above copyright notice,
Yann 0:f30e2135b0db 17 * this list of conditions and the following disclaimer.
Yann 0:f30e2135b0db 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Yann 0:f30e2135b0db 19 * this list of conditions and the following disclaimer in the documentation
Yann 0:f30e2135b0db 20 * and/or other materials provided with the distribution.
Yann 0:f30e2135b0db 21 * 3. The name of the author may not be used to endorse or promote products
Yann 0:f30e2135b0db 22 * derived from this software without specific prior written permission.
Yann 0:f30e2135b0db 23 *
Yann 0:f30e2135b0db 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
Yann 0:f30e2135b0db 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Yann 0:f30e2135b0db 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
Yann 0:f30e2135b0db 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Yann 0:f30e2135b0db 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
Yann 0:f30e2135b0db 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Yann 0:f30e2135b0db 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Yann 0:f30e2135b0db 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
Yann 0:f30e2135b0db 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
Yann 0:f30e2135b0db 33 * OF SUCH DAMAGE.
Yann 0:f30e2135b0db 34 *
Yann 0:f30e2135b0db 35 * This file is part of the lwIP TCP/IP stack.
Yann 0:f30e2135b0db 36 *
Yann 0:f30e2135b0db 37 * Author: Adam Dunkels <adam@sics.se>
Yann 0:f30e2135b0db 38 *
Yann 0:f30e2135b0db 39 */
Yann 0:f30e2135b0db 40
Yann 0:f30e2135b0db 41 #include "lwip/opt.h"
Yann 0:f30e2135b0db 42
Yann 0:f30e2135b0db 43 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
Yann 0:f30e2135b0db 44
Yann 0:f30e2135b0db 45 #include "lwip/def.h"
Yann 0:f30e2135b0db 46 #include "lwip/memp.h"
Yann 0:f30e2135b0db 47 #include "lwip/ip_addr.h"
Yann 0:f30e2135b0db 48 #include "lwip/netif.h"
Yann 0:f30e2135b0db 49 #include "lwip/raw.h"
Yann 0:f30e2135b0db 50 #include "lwip/stats.h"
Yann 0:f30e2135b0db 51 #include "arch/perf.h"
Yann 0:f30e2135b0db 52
Yann 0:f30e2135b0db 53 #include <string.h>
Yann 0:f30e2135b0db 54
Yann 0:f30e2135b0db 55 /** The list of RAW PCBs */
Yann 0:f30e2135b0db 56 static struct raw_pcb *raw_pcbs;
Yann 0:f30e2135b0db 57
Yann 0:f30e2135b0db 58 /**
Yann 0:f30e2135b0db 59 * Determine if in incoming IP packet is covered by a RAW PCB
Yann 0:f30e2135b0db 60 * and if so, pass it to a user-provided receive callback function.
Yann 0:f30e2135b0db 61 *
Yann 0:f30e2135b0db 62 * Given an incoming IP datagram (as a chain of pbufs) this function
Yann 0:f30e2135b0db 63 * finds a corresponding RAW PCB and calls the corresponding receive
Yann 0:f30e2135b0db 64 * callback function.
Yann 0:f30e2135b0db 65 *
Yann 0:f30e2135b0db 66 * @param p pbuf to be demultiplexed to a RAW PCB.
Yann 0:f30e2135b0db 67 * @param inp network interface on which the datagram was received.
Yann 0:f30e2135b0db 68 * @return - 1 if the packet has been eaten by a RAW PCB receive
Yann 0:f30e2135b0db 69 * callback function. The caller MAY NOT not reference the
Yann 0:f30e2135b0db 70 * packet any longer, and MAY NOT call pbuf_free().
Yann 0:f30e2135b0db 71 * @return - 0 if packet is not eaten (pbuf is still referenced by the
Yann 0:f30e2135b0db 72 * caller).
Yann 0:f30e2135b0db 73 *
Yann 0:f30e2135b0db 74 */
Yann 0:f30e2135b0db 75 u8_t
Yann 0:f30e2135b0db 76 raw_input(struct pbuf *p, struct netif *inp)
Yann 0:f30e2135b0db 77 {
Yann 0:f30e2135b0db 78 struct raw_pcb *pcb, *prev;
Yann 0:f30e2135b0db 79 struct ip_hdr *iphdr;
Yann 0:f30e2135b0db 80 s16_t proto;
Yann 0:f30e2135b0db 81 u8_t eaten = 0;
Yann 0:f30e2135b0db 82
Yann 0:f30e2135b0db 83 LWIP_UNUSED_ARG(inp);
Yann 0:f30e2135b0db 84
Yann 0:f30e2135b0db 85 iphdr = (struct ip_hdr *)p->payload;
Yann 0:f30e2135b0db 86 proto = IPH_PROTO(iphdr);
Yann 0:f30e2135b0db 87
Yann 0:f30e2135b0db 88 prev = NULL;
Yann 0:f30e2135b0db 89 pcb = raw_pcbs;
Yann 0:f30e2135b0db 90 /* loop through all raw pcbs until the packet is eaten by one */
Yann 0:f30e2135b0db 91 /* this allows multiple pcbs to match against the packet by design */
Yann 0:f30e2135b0db 92 while ((eaten == 0) && (pcb != NULL)) {
Yann 0:f30e2135b0db 93 if ((pcb->protocol == proto) &&
Yann 0:f30e2135b0db 94 (ip_addr_isany(&pcb->local_ip) ||
Yann 0:f30e2135b0db 95 ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest))) {
Yann 0:f30e2135b0db 96 #if IP_SOF_BROADCAST_RECV
Yann 0:f30e2135b0db 97 /* broadcast filter? */
Yann 0:f30e2135b0db 98 if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(&current_iphdr_dest, inp))
Yann 0:f30e2135b0db 99 #endif /* IP_SOF_BROADCAST_RECV */
Yann 0:f30e2135b0db 100 {
Yann 0:f30e2135b0db 101 /* receive callback function available? */
Yann 0:f30e2135b0db 102 if (pcb->recv != NULL) {
Yann 0:f30e2135b0db 103 /* the receive callback function did not eat the packet? */
Yann 0:f30e2135b0db 104 if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()) != 0) {
Yann 0:f30e2135b0db 105 /* receive function ate the packet */
Yann 0:f30e2135b0db 106 p = NULL;
Yann 0:f30e2135b0db 107 eaten = 1;
Yann 0:f30e2135b0db 108 if (prev != NULL) {
Yann 0:f30e2135b0db 109 /* move the pcb to the front of raw_pcbs so that is
Yann 0:f30e2135b0db 110 found faster next time */
Yann 0:f30e2135b0db 111 prev->next = pcb->next;
Yann 0:f30e2135b0db 112 pcb->next = raw_pcbs;
Yann 0:f30e2135b0db 113 raw_pcbs = pcb;
Yann 0:f30e2135b0db 114 }
Yann 0:f30e2135b0db 115 }
Yann 0:f30e2135b0db 116 }
Yann 0:f30e2135b0db 117 /* no receive callback function was set for this raw PCB */
Yann 0:f30e2135b0db 118 }
Yann 0:f30e2135b0db 119 /* drop the packet */
Yann 0:f30e2135b0db 120 }
Yann 0:f30e2135b0db 121 prev = pcb;
Yann 0:f30e2135b0db 122 pcb = pcb->next;
Yann 0:f30e2135b0db 123 }
Yann 0:f30e2135b0db 124 return eaten;
Yann 0:f30e2135b0db 125 }
Yann 0:f30e2135b0db 126
Yann 0:f30e2135b0db 127 /**
Yann 0:f30e2135b0db 128 * Bind a RAW PCB.
Yann 0:f30e2135b0db 129 *
Yann 0:f30e2135b0db 130 * @param pcb RAW PCB to be bound with a local address ipaddr.
Yann 0:f30e2135b0db 131 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
Yann 0:f30e2135b0db 132 * bind to all local interfaces.
Yann 0:f30e2135b0db 133 *
Yann 0:f30e2135b0db 134 * @return lwIP error code.
Yann 0:f30e2135b0db 135 * - ERR_OK. Successful. No error occured.
Yann 0:f30e2135b0db 136 * - ERR_USE. The specified IP address is already bound to by
Yann 0:f30e2135b0db 137 * another RAW PCB.
Yann 0:f30e2135b0db 138 *
Yann 0:f30e2135b0db 139 * @see raw_disconnect()
Yann 0:f30e2135b0db 140 */
Yann 0:f30e2135b0db 141 err_t
Yann 0:f30e2135b0db 142 raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
Yann 0:f30e2135b0db 143 {
Yann 0:f30e2135b0db 144 ip_addr_set(&pcb->local_ip, ipaddr);
Yann 0:f30e2135b0db 145 return ERR_OK;
Yann 0:f30e2135b0db 146 }
Yann 0:f30e2135b0db 147
Yann 0:f30e2135b0db 148 /**
Yann 0:f30e2135b0db 149 * Connect an RAW PCB. This function is required by upper layers
Yann 0:f30e2135b0db 150 * of lwip. Using the raw api you could use raw_sendto() instead
Yann 0:f30e2135b0db 151 *
Yann 0:f30e2135b0db 152 * This will associate the RAW PCB with the remote address.
Yann 0:f30e2135b0db 153 *
Yann 0:f30e2135b0db 154 * @param pcb RAW PCB to be connected with remote address ipaddr and port.
Yann 0:f30e2135b0db 155 * @param ipaddr remote IP address to connect with.
Yann 0:f30e2135b0db 156 *
Yann 0:f30e2135b0db 157 * @return lwIP error code
Yann 0:f30e2135b0db 158 *
Yann 0:f30e2135b0db 159 * @see raw_disconnect() and raw_sendto()
Yann 0:f30e2135b0db 160 */
Yann 0:f30e2135b0db 161 err_t
Yann 0:f30e2135b0db 162 raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
Yann 0:f30e2135b0db 163 {
Yann 0:f30e2135b0db 164 ip_addr_set(&pcb->remote_ip, ipaddr);
Yann 0:f30e2135b0db 165 return ERR_OK;
Yann 0:f30e2135b0db 166 }
Yann 0:f30e2135b0db 167
Yann 0:f30e2135b0db 168
Yann 0:f30e2135b0db 169 /**
Yann 0:f30e2135b0db 170 * Set the callback function for received packets that match the
Yann 0:f30e2135b0db 171 * raw PCB's protocol and binding.
Yann 0:f30e2135b0db 172 *
Yann 0:f30e2135b0db 173 * The callback function MUST either
Yann 0:f30e2135b0db 174 * - eat the packet by calling pbuf_free() and returning non-zero. The
Yann 0:f30e2135b0db 175 * packet will not be passed to other raw PCBs or other protocol layers.
Yann 0:f30e2135b0db 176 * - not free the packet, and return zero. The packet will be matched
Yann 0:f30e2135b0db 177 * against further PCBs and/or forwarded to another protocol layers.
Yann 0:f30e2135b0db 178 *
Yann 0:f30e2135b0db 179 * @return non-zero if the packet was free()d, zero if the packet remains
Yann 0:f30e2135b0db 180 * available for others.
Yann 0:f30e2135b0db 181 */
Yann 0:f30e2135b0db 182 void
Yann 0:f30e2135b0db 183 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
Yann 0:f30e2135b0db 184 {
Yann 0:f30e2135b0db 185 /* remember recv() callback and user data */
Yann 0:f30e2135b0db 186 pcb->recv = recv;
Yann 0:f30e2135b0db 187 pcb->recv_arg = recv_arg;
Yann 0:f30e2135b0db 188 }
Yann 0:f30e2135b0db 189
Yann 0:f30e2135b0db 190 /**
Yann 0:f30e2135b0db 191 * Send the raw IP packet to the given address. Note that actually you cannot
Yann 0:f30e2135b0db 192 * modify the IP headers (this is inconsistent with the receive callback where
Yann 0:f30e2135b0db 193 * you actually get the IP headers), you can only specify the IP payload here.
Yann 0:f30e2135b0db 194 * It requires some more changes in lwIP. (there will be a raw_send() function
Yann 0:f30e2135b0db 195 * then.)
Yann 0:f30e2135b0db 196 *
Yann 0:f30e2135b0db 197 * @param pcb the raw pcb which to send
Yann 0:f30e2135b0db 198 * @param p the IP payload to send
Yann 0:f30e2135b0db 199 * @param ipaddr the destination address of the IP packet
Yann 0:f30e2135b0db 200 *
Yann 0:f30e2135b0db 201 */
Yann 0:f30e2135b0db 202 err_t
Yann 0:f30e2135b0db 203 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
Yann 0:f30e2135b0db 204 {
Yann 0:f30e2135b0db 205 err_t err;
Yann 0:f30e2135b0db 206 struct netif *netif;
Yann 0:f30e2135b0db 207 ip_addr_t *src_ip;
Yann 0:f30e2135b0db 208 struct pbuf *q; /* q will be sent down the stack */
Yann 0:f30e2135b0db 209
Yann 0:f30e2135b0db 210 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
Yann 0:f30e2135b0db 211
Yann 0:f30e2135b0db 212 /* not enough space to add an IP header to first pbuf in given p chain? */
Yann 0:f30e2135b0db 213 if (pbuf_header(p, IP_HLEN)) {
Yann 0:f30e2135b0db 214 /* allocate header in new pbuf */
Yann 0:f30e2135b0db 215 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
Yann 0:f30e2135b0db 216 /* new header pbuf could not be allocated? */
Yann 0:f30e2135b0db 217 if (q == NULL) {
Yann 0:f30e2135b0db 218 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
Yann 0:f30e2135b0db 219 return ERR_MEM;
Yann 0:f30e2135b0db 220 }
Yann 0:f30e2135b0db 221 /* chain header q in front of given pbuf p */
Yann 0:f30e2135b0db 222 pbuf_chain(q, p);
Yann 0:f30e2135b0db 223 /* { first pbuf q points to header pbuf } */
Yann 0:f30e2135b0db 224 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
Yann 0:f30e2135b0db 225 } else {
Yann 0:f30e2135b0db 226 /* first pbuf q equals given pbuf */
Yann 0:f30e2135b0db 227 q = p;
Yann 0:f30e2135b0db 228 if(pbuf_header(q, -IP_HLEN)) {
Yann 0:f30e2135b0db 229 LWIP_ASSERT("Can't restore header we just removed!", 0);
Yann 0:f30e2135b0db 230 return ERR_MEM;
Yann 0:f30e2135b0db 231 }
Yann 0:f30e2135b0db 232 }
Yann 0:f30e2135b0db 233
Yann 0:f30e2135b0db 234 if ((netif = ip_route(ipaddr)) == NULL) {
Yann 0:f30e2135b0db 235 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
Yann 0:f30e2135b0db 236 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
Yann 0:f30e2135b0db 237 /* free any temporary header pbuf allocated by pbuf_header() */
Yann 0:f30e2135b0db 238 if (q != p) {
Yann 0:f30e2135b0db 239 pbuf_free(q);
Yann 0:f30e2135b0db 240 }
Yann 0:f30e2135b0db 241 return ERR_RTE;
Yann 0:f30e2135b0db 242 }
Yann 0:f30e2135b0db 243
Yann 0:f30e2135b0db 244 #if IP_SOF_BROADCAST
Yann 0:f30e2135b0db 245 /* broadcast filter? */
Yann 0:f30e2135b0db 246 if (((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif)) {
Yann 0:f30e2135b0db 247 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
Yann 0:f30e2135b0db 248 /* free any temporary header pbuf allocated by pbuf_header() */
Yann 0:f30e2135b0db 249 if (q != p) {
Yann 0:f30e2135b0db 250 pbuf_free(q);
Yann 0:f30e2135b0db 251 }
Yann 0:f30e2135b0db 252 return ERR_VAL;
Yann 0:f30e2135b0db 253 }
Yann 0:f30e2135b0db 254 #endif /* IP_SOF_BROADCAST */
Yann 0:f30e2135b0db 255
Yann 0:f30e2135b0db 256 if (ip_addr_isany(&pcb->local_ip)) {
Yann 0:f30e2135b0db 257 /* use outgoing network interface IP address as source address */
Yann 0:f30e2135b0db 258 src_ip = &(netif->ip_addr);
Yann 0:f30e2135b0db 259 } else {
Yann 0:f30e2135b0db 260 /* use RAW PCB local IP address as source address */
Yann 0:f30e2135b0db 261 src_ip = &(pcb->local_ip);
Yann 0:f30e2135b0db 262 }
Yann 0:f30e2135b0db 263
Yann 0:f30e2135b0db 264 #if LWIP_NETIF_HWADDRHINT
Yann 0:f30e2135b0db 265 netif->addr_hint = &(pcb->addr_hint);
Yann 0:f30e2135b0db 266 #endif /* LWIP_NETIF_HWADDRHINT*/
Yann 0:f30e2135b0db 267 err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
Yann 0:f30e2135b0db 268 #if LWIP_NETIF_HWADDRHINT
Yann 0:f30e2135b0db 269 netif->addr_hint = NULL;
Yann 0:f30e2135b0db 270 #endif /* LWIP_NETIF_HWADDRHINT*/
Yann 0:f30e2135b0db 271
Yann 0:f30e2135b0db 272 /* did we chain a header earlier? */
Yann 0:f30e2135b0db 273 if (q != p) {
Yann 0:f30e2135b0db 274 /* free the header */
Yann 0:f30e2135b0db 275 pbuf_free(q);
Yann 0:f30e2135b0db 276 }
Yann 0:f30e2135b0db 277 return err;
Yann 0:f30e2135b0db 278 }
Yann 0:f30e2135b0db 279
Yann 0:f30e2135b0db 280 /**
Yann 0:f30e2135b0db 281 * Send the raw IP packet to the address given by raw_connect()
Yann 0:f30e2135b0db 282 *
Yann 0:f30e2135b0db 283 * @param pcb the raw pcb which to send
Yann 0:f30e2135b0db 284 * @param p the IP payload to send
Yann 0:f30e2135b0db 285 *
Yann 0:f30e2135b0db 286 */
Yann 0:f30e2135b0db 287 err_t
Yann 0:f30e2135b0db 288 raw_send(struct raw_pcb *pcb, struct pbuf *p)
Yann 0:f30e2135b0db 289 {
Yann 0:f30e2135b0db 290 return raw_sendto(pcb, p, &pcb->remote_ip);
Yann 0:f30e2135b0db 291 }
Yann 0:f30e2135b0db 292
Yann 0:f30e2135b0db 293 /**
Yann 0:f30e2135b0db 294 * Remove an RAW PCB.
Yann 0:f30e2135b0db 295 *
Yann 0:f30e2135b0db 296 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
Yann 0:f30e2135b0db 297 * RAW PCB's and the data structure is freed from memory.
Yann 0:f30e2135b0db 298 *
Yann 0:f30e2135b0db 299 * @see raw_new()
Yann 0:f30e2135b0db 300 */
Yann 0:f30e2135b0db 301 void
Yann 0:f30e2135b0db 302 raw_remove(struct raw_pcb *pcb)
Yann 0:f30e2135b0db 303 {
Yann 0:f30e2135b0db 304 struct raw_pcb *pcb2;
Yann 0:f30e2135b0db 305 /* pcb to be removed is first in list? */
Yann 0:f30e2135b0db 306 if (raw_pcbs == pcb) {
Yann 0:f30e2135b0db 307 /* make list start at 2nd pcb */
Yann 0:f30e2135b0db 308 raw_pcbs = raw_pcbs->next;
Yann 0:f30e2135b0db 309 /* pcb not 1st in list */
Yann 0:f30e2135b0db 310 } else {
Yann 0:f30e2135b0db 311 for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
Yann 0:f30e2135b0db 312 /* find pcb in raw_pcbs list */
Yann 0:f30e2135b0db 313 if (pcb2->next != NULL && pcb2->next == pcb) {
Yann 0:f30e2135b0db 314 /* remove pcb from list */
Yann 0:f30e2135b0db 315 pcb2->next = pcb->next;
Yann 0:f30e2135b0db 316 }
Yann 0:f30e2135b0db 317 }
Yann 0:f30e2135b0db 318 }
Yann 0:f30e2135b0db 319 memp_free(MEMP_RAW_PCB, pcb);
Yann 0:f30e2135b0db 320 }
Yann 0:f30e2135b0db 321
Yann 0:f30e2135b0db 322 /**
Yann 0:f30e2135b0db 323 * Create a RAW PCB.
Yann 0:f30e2135b0db 324 *
Yann 0:f30e2135b0db 325 * @return The RAW PCB which was created. NULL if the PCB data structure
Yann 0:f30e2135b0db 326 * could not be allocated.
Yann 0:f30e2135b0db 327 *
Yann 0:f30e2135b0db 328 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
Yann 0:f30e2135b0db 329 *
Yann 0:f30e2135b0db 330 * @see raw_remove()
Yann 0:f30e2135b0db 331 */
Yann 0:f30e2135b0db 332 struct raw_pcb *
Yann 0:f30e2135b0db 333 raw_new(u8_t proto)
Yann 0:f30e2135b0db 334 {
Yann 0:f30e2135b0db 335 struct raw_pcb *pcb;
Yann 0:f30e2135b0db 336
Yann 0:f30e2135b0db 337 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
Yann 0:f30e2135b0db 338
Yann 0:f30e2135b0db 339 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
Yann 0:f30e2135b0db 340 /* could allocate RAW PCB? */
Yann 0:f30e2135b0db 341 if (pcb != NULL) {
Yann 0:f30e2135b0db 342 /* initialize PCB to all zeroes */
Yann 0:f30e2135b0db 343 memset(pcb, 0, sizeof(struct raw_pcb));
Yann 0:f30e2135b0db 344 pcb->protocol = proto;
Yann 0:f30e2135b0db 345 pcb->ttl = RAW_TTL;
Yann 0:f30e2135b0db 346 pcb->next = raw_pcbs;
Yann 0:f30e2135b0db 347 raw_pcbs = pcb;
Yann 0:f30e2135b0db 348 }
Yann 0:f30e2135b0db 349 return pcb;
Yann 0:f30e2135b0db 350 }
Yann 0:f30e2135b0db 351
Yann 0:f30e2135b0db 352 #endif /* LWIP_RAW */