mesh.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rimemesh
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: mesh.c,v 1.20 2009/12/18 14:57:15 nvt-se Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         A mesh routing protocol
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include "contiki.h"
00047 #include "net/rime.h"
00048 #include "net/rime/route.h"
00049 #include "net/rime/mesh.h"
00050 
00051 #include <stddef.h> /* For offsetof */
00052 
00053 #define PACKET_TIMEOUT (CLOCK_SECOND * 10)
00054 
00055 #define DEBUG 0
00056 #if DEBUG
00057 #include <stdio.h>
00058 #define PRINTF(...) printf(__VA_ARGS__)
00059 #else
00060 #define PRINTF(...)
00061 #endif
00062 
00063 /*---------------------------------------------------------------------------*/
00064 static void
00065 data_packet_received(struct multihop_conn *multihop,
00066                      const rimeaddr_t *from,
00067                      const rimeaddr_t *prevhop, uint8_t hops)
00068 {
00069   struct mesh_conn *c = (struct mesh_conn *)
00070     ((char *)multihop - offsetof(struct mesh_conn, multihop));
00071 
00072   struct route_entry *rt;
00073 
00074   /* Refresh the route when we hear a packet from a neighbor. */
00075   rt = route_lookup(from);
00076   if(rt != NULL) {
00077     route_refresh(rt);
00078   }
00079   
00080   if(c->cb->recv) {
00081     c->cb->recv(c, from, hops);
00082   }
00083 }
00084 /*---------------------------------------------------------------------------*/
00085 static rimeaddr_t *
00086 data_packet_forward(struct multihop_conn *multihop,
00087                     const rimeaddr_t *originator,
00088                     const rimeaddr_t *dest,
00089                     const rimeaddr_t *prevhop, uint8_t hops)
00090 {
00091   struct route_entry *rt;
00092 
00093   rt = route_lookup(dest);
00094   if(rt == NULL) {
00095     return NULL;
00096   }
00097   
00098   return &rt->nexthop;
00099 }
00100 /*---------------------------------------------------------------------------*/
00101 static void
00102 found_route(struct route_discovery_conn *rdc, const rimeaddr_t *dest)
00103 {
00104   struct mesh_conn *c = (struct mesh_conn *)
00105     ((char *)rdc - offsetof(struct mesh_conn, route_discovery_conn));
00106 
00107   if(c->queued_data != NULL &&
00108      rimeaddr_cmp(dest, &c->queued_data_dest)) {
00109     queuebuf_to_packetbuf(c->queued_data);
00110     queuebuf_free(c->queued_data);
00111     c->queued_data = NULL;
00112     if(multihop_send(&c->multihop, dest)) {
00113       c->cb->sent(c);
00114     } else {
00115       c->cb->timedout(c);
00116     }
00117   }
00118 }
00119 /*---------------------------------------------------------------------------*/
00120 static void
00121 route_timed_out(struct route_discovery_conn *rdc)
00122 {
00123   struct mesh_conn *c = (struct mesh_conn *)
00124     ((char *)rdc - offsetof(struct mesh_conn, route_discovery_conn));
00125 
00126   if(c->queued_data != NULL) {
00127     queuebuf_free(c->queued_data);
00128     c->queued_data = NULL;
00129   }
00130 
00131   if(c->cb->timedout) {
00132     c->cb->timedout(c);
00133   }
00134 }
00135 /*---------------------------------------------------------------------------*/
00136 static const struct multihop_callbacks data_callbacks = { data_packet_received,
00137                                                     data_packet_forward };
00138 static const struct route_discovery_callbacks route_discovery_callbacks =
00139   { found_route, route_timed_out };
00140 /*---------------------------------------------------------------------------*/
00141 void
00142 mesh_open(struct mesh_conn *c, uint16_t channels,
00143           const struct mesh_callbacks *callbacks)
00144 {
00145   route_init();
00146   multihop_open(&c->multihop, channels, &data_callbacks);
00147   route_discovery_open(&c->route_discovery_conn,
00148                        CLOCK_SECOND * 2,
00149                        channels + 1,
00150                        &route_discovery_callbacks);
00151   c->cb = callbacks;
00152 }
00153 /*---------------------------------------------------------------------------*/
00154 void
00155 mesh_close(struct mesh_conn *c)
00156 {
00157   multihop_close(&c->multihop);
00158   route_discovery_close(&c->route_discovery_conn);
00159 }
00160 /*---------------------------------------------------------------------------*/
00161 int
00162 mesh_send(struct mesh_conn *c, const rimeaddr_t *to)
00163 {
00164   int could_send;
00165 
00166   PRINTF("%d.%d: mesh_send to %d.%d\n",
00167          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00168          to->u8[0], to->u8[1]);
00169   
00170   could_send = multihop_send(&c->multihop, to);
00171 
00172   if(!could_send) {
00173     if(c->queued_data != NULL) {
00174       queuebuf_free(c->queued_data);
00175     }
00176 
00177     PRINTF("mesh_send: queueing data, sending rreq\n");
00178     c->queued_data = queuebuf_new_from_packetbuf();
00179     rimeaddr_copy(&c->queued_data_dest, to);
00180     route_discovery_discover(&c->route_discovery_conn, to,
00181                              PACKET_TIMEOUT);
00182     return 0;
00183   }
00184   c->cb->sent(c);
00185   return 1;
00186 }
00187 /*---------------------------------------------------------------------------*/
00188 /** @} */

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