rpl-of0.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup uip6
00003  * @{
00004  */
00005 /*
00006  * Copyright (c) 2010, Swedish Institute of Computer Science.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. Neither the name of the Institute nor the names of its contributors
00018  *    may be used to endorse or promote products derived from this software
00019  *    without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * This file is part of the Contiki operating system.
00034  */
00035 /**
00036  * \file
00037  *         An implementation of RPL's objective function 0.
00038  *
00039  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
00040  */
00041 
00042 #include "net/rpl/rpl-private.h"
00043 
00044 #define DEBUG DEBUG_NONE
00045 #include "net/uip-debug.h"
00046 
00047 #include "net/neighbor-info.h"
00048 
00049 static void reset(rpl_dag_t *);
00050 static rpl_parent_t *best_parent(rpl_parent_t *, rpl_parent_t *);
00051 static rpl_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
00052 static void update_metric_container(rpl_dag_t *);
00053 
00054 rpl_of_t rpl_of0 = {
00055   reset,
00056   NULL,
00057   best_parent,
00058   calculate_rank,
00059   update_metric_container,
00060   0
00061 };
00062 
00063 #define DEFAULT_RANK_INCREMENT  DEFAULT_MIN_HOPRANKINC
00064 
00065 #define MIN_DIFFERENCE (NEIGHBOR_INFO_ETX_DIVISOR + NEIGHBOR_INFO_ETX_DIVISOR / 2)
00066 
00067 static void
00068 reset(rpl_dag_t *dag)
00069 {
00070   PRINTF("RPL: Resetting OF0\n");
00071 }
00072 
00073 static rpl_rank_t
00074 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)
00075 {
00076   rpl_rank_t increment;
00077   if(base_rank == 0) {
00078     if(p == NULL) {
00079       return INFINITE_RANK;
00080     }
00081     base_rank = p->rank;
00082   }
00083 
00084   increment = p != NULL ? p->dag->min_hoprankinc : DEFAULT_RANK_INCREMENT;
00085 
00086   if((rpl_rank_t)(base_rank + increment) < base_rank) {
00087     PRINTF("RPL: OF0 rank %d incremented to infinite rank due to wrapping\n",
00088         base_rank);
00089     return INFINITE_RANK;
00090   }
00091   return base_rank + increment;
00092 
00093 }
00094 
00095 static rpl_parent_t *
00096 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
00097 {
00098   rpl_rank_t r1, r2;
00099   rpl_dag_t *dag;
00100   
00101   PRINTF("RPL: Comparing parent ");
00102   PRINT6ADDR(&p1->addr);
00103   PRINTF(" (confidence %d, rank %d) with parent ",
00104         p1->link_metric, p1->rank);
00105   PRINT6ADDR(&p2->addr);
00106   PRINTF(" (confidence %d, rank %d)\n",
00107         p2->link_metric, p2->rank);
00108 
00109 
00110   r1 = DAG_RANK(p1->rank, (rpl_dag_t *)p1->dag) * NEIGHBOR_INFO_ETX_DIVISOR +
00111     p1->link_metric;
00112   r2 = DAG_RANK(p2->rank, (rpl_dag_t *)p1->dag) * NEIGHBOR_INFO_ETX_DIVISOR +
00113     p2->link_metric;
00114   /* Compare two parents by looking both and their rank and at the ETX
00115      for that parent. We choose the parent that has the most
00116      favourable combination. */
00117 
00118   dag = (rpl_dag_t *)p1->dag; /* Both parents must be in the same DAG. */
00119   if(r1 < r2 + MIN_DIFFERENCE &&
00120      r1 > r2 - MIN_DIFFERENCE) {
00121     return dag->preferred_parent;
00122   } else if(r1 < r2) {
00123     return p1;
00124   } else {
00125     return p2;
00126   }
00127 }
00128 
00129 static void
00130 update_metric_container(rpl_dag_t *dag)
00131 {
00132   dag->mc.type = RPL_DAG_MC_NONE;
00133 }

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