multihop.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rimemh
00003  * @{
00004  */
00005 
00006 /*
00007  * Copyright (c) 2007, Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of the Institute nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * This file is part of the Contiki operating system.
00035  *
00036  * $Id: multihop.c,v 1.7 2009/11/08 19:40:17 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Multihop forwarding
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include "contiki.h"
00047 #include "net/rime.h"
00048 #include "net/rime/multihop.h"
00049 #include "net/rime/route.h"
00050 
00051 #include <string.h>
00052 
00053 static const struct packetbuf_attrlist attributes[] =
00054   {
00055     MULTIHOP_ATTRIBUTES
00056     PACKETBUF_ATTR_LAST
00057   };
00058 
00059 #define DEBUG 0
00060 #if DEBUG
00061 #include <stdio.h>
00062 #define PRINTF(...) printf(__VA_ARGS__)
00063 #else
00064 #define PRINTF(...)
00065 #endif
00066 
00067 /*---------------------------------------------------------------------------*/
00068 void
00069 data_packet_received(struct unicast_conn *uc, const rimeaddr_t *from)
00070 {
00071   struct multihop_conn *c = (struct multihop_conn *)uc;
00072   rimeaddr_t *nexthop;
00073   rimeaddr_t sender, receiver;
00074 
00075   /* Copy the packet attributes to avoid them being overwritten or
00076      cleared by an application program that uses the packet buffer for
00077      its own needs. */
00078   rimeaddr_copy(&sender, packetbuf_addr(PACKETBUF_ADDR_ESENDER));
00079   rimeaddr_copy(&receiver, packetbuf_addr(PACKETBUF_ADDR_ERECEIVER));
00080 
00081   PRINTF("data_packet_received from %d.%d towards %d.%d len %d\n",
00082          from->u8[0], from->u8[1],
00083          packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)->u8[0],
00084          packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)->u8[1],
00085          packetbuf_datalen());
00086 
00087   if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_ERECEIVER),
00088                                  &rimeaddr_node_addr)) {
00089     PRINTF("for us!\n");
00090     if(c->cb->recv) {
00091       c->cb->recv(c, &sender, from,
00092                   packetbuf_attr(PACKETBUF_ATTR_HOPS));
00093     }
00094   } else {
00095     nexthop = NULL;
00096     if(c->cb->forward) {
00097       nexthop = c->cb->forward(c, &sender, &receiver,
00098                                from, packetbuf_attr(PACKETBUF_ATTR_HOPS));
00099 
00100       packetbuf_set_attr(PACKETBUF_ATTR_HOPS,
00101                          packetbuf_attr(PACKETBUF_ATTR_HOPS) + 1);
00102     }
00103     if(nexthop) {
00104       PRINTF("forwarding to %d.%d\n", nexthop->u8[0], nexthop->u8[1]);
00105       unicast_send(&c->c, nexthop);
00106     }
00107   }
00108 }
00109 /*---------------------------------------------------------------------------*/
00110 static const struct unicast_callbacks data_callbacks = { data_packet_received };
00111 /*---------------------------------------------------------------------------*/
00112 void
00113 multihop_open(struct multihop_conn *c, uint16_t channel,
00114         const struct multihop_callbacks *callbacks)
00115 {
00116   unicast_open(&c->c, channel, &data_callbacks);
00117   channel_set_attributes(channel, attributes);
00118   c->cb = callbacks;
00119 }
00120 /*---------------------------------------------------------------------------*/
00121 void
00122 multihop_close(struct multihop_conn *c)
00123 {
00124   unicast_close(&c->c);
00125 }
00126 /*---------------------------------------------------------------------------*/
00127 int
00128 multihop_send(struct multihop_conn *c, const rimeaddr_t *to)
00129 {
00130   rimeaddr_t *nexthop;
00131 
00132   if(c->cb->forward == NULL) {
00133     return 0;
00134   }
00135   packetbuf_compact();
00136   nexthop = c->cb->forward(c, &rimeaddr_node_addr, to, NULL, 0);
00137   
00138   if(nexthop == NULL) {
00139     PRINTF("multihop_send: no route\n");
00140     return 0;
00141   } else {
00142     PRINTF("multihop_send: sending data towards %d.%d\n",
00143            nexthop->u8[0], nexthop->u8[1]);
00144     packetbuf_set_addr(PACKETBUF_ADDR_ERECEIVER, to);
00145     packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &rimeaddr_node_addr);
00146     packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
00147     unicast_send(&c->c, nexthop);
00148     return 1;
00149   }
00150 }
00151 /*---------------------------------------------------------------------------*/
00152 /** @} */

Generated on Mon Apr 11 14:23:30 2011 for Contiki 2.5 by  doxygen 1.6.1