Contiki 2.6
|
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 packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 00098 packetbuf_attr(PACKETBUF_ATTR_HOPS) + 1); 00099 nexthop = c->cb->forward(c, &sender, &receiver, 00100 from, packetbuf_attr(PACKETBUF_ATTR_HOPS) - 1); 00101 } 00102 if(nexthop) { 00103 PRINTF("forwarding to %d.%d\n", nexthop->u8[0], nexthop->u8[1]); 00104 unicast_send(&c->c, nexthop); 00105 } 00106 } 00107 } 00108 /*---------------------------------------------------------------------------*/ 00109 static const struct unicast_callbacks data_callbacks = { data_packet_received }; 00110 /*---------------------------------------------------------------------------*/ 00111 void 00112 multihop_open(struct multihop_conn *c, uint16_t channel, 00113 const struct multihop_callbacks *callbacks) 00114 { 00115 unicast_open(&c->c, channel, &data_callbacks); 00116 channel_set_attributes(channel, attributes); 00117 c->cb = callbacks; 00118 } 00119 /*---------------------------------------------------------------------------*/ 00120 void 00121 multihop_close(struct multihop_conn *c) 00122 { 00123 unicast_close(&c->c); 00124 } 00125 /*---------------------------------------------------------------------------*/ 00126 int 00127 multihop_send(struct multihop_conn *c, const rimeaddr_t *to) 00128 { 00129 rimeaddr_t *nexthop; 00130 00131 if(c->cb->forward == NULL) { 00132 return 0; 00133 } 00134 packetbuf_compact(); 00135 packetbuf_set_addr(PACKETBUF_ADDR_ERECEIVER, to); 00136 packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &rimeaddr_node_addr); 00137 packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1); 00138 nexthop = c->cb->forward(c, &rimeaddr_node_addr, to, NULL, 0); 00139 00140 if(nexthop == NULL) { 00141 PRINTF("multihop_send: no route\n"); 00142 return 0; 00143 } else { 00144 PRINTF("multihop_send: sending data towards %d.%d\n", 00145 nexthop->u8[0], nexthop->u8[1]); 00146 unicast_send(&c->c, nexthop); 00147 return 1; 00148 } 00149 } 00150 /*---------------------------------------------------------------------------*/ 00151 void 00152 multihop_resend(struct multihop_conn *c, const rimeaddr_t *nexthop) 00153 { 00154 unicast_send(&c->c, nexthop); 00155 } 00156 /*---------------------------------------------------------------------------*/ 00157 /** @} */