Contiki 2.6

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_dag_t *best_dag(rpl_dag_t *, rpl_dag_t *);
00052 static rpl_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
00053 static void update_metric_container(rpl_instance_t *);
00054 
00055 rpl_of_t rpl_of0 = {
00056   reset,
00057   NULL,
00058   best_parent,
00059   best_dag,
00060   calculate_rank,
00061   update_metric_container,
00062   0
00063 };
00064 
00065 #define DEFAULT_RANK_INCREMENT  RPL_MIN_HOPRANKINC
00066 
00067 #define MIN_DIFFERENCE (NEIGHBOR_INFO_ETX_DIVISOR + NEIGHBOR_INFO_ETX_DIVISOR / 2)
00068 
00069 static void
00070 reset(rpl_dag_t *dag)
00071 {
00072   PRINTF("RPL: Resetting OF0\n");
00073 }
00074 
00075 static rpl_rank_t
00076 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)
00077 {
00078   rpl_rank_t increment;
00079   if(base_rank == 0) {
00080     if(p == NULL) {
00081       return INFINITE_RANK;
00082     }
00083     base_rank = p->rank;
00084   }
00085 
00086   increment = p != NULL ?
00087                 p->dag->instance->min_hoprankinc :
00088                 DEFAULT_RANK_INCREMENT;
00089 
00090   if((rpl_rank_t)(base_rank + increment) < base_rank) {
00091     PRINTF("RPL: OF0 rank %d incremented to infinite rank due to wrapping\n",
00092         base_rank);
00093     return INFINITE_RANK;
00094   }
00095   return base_rank + increment;
00096 
00097 }
00098 
00099 static rpl_dag_t *
00100 best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
00101 {
00102   if(d1->grounded) {
00103     if (!d2->grounded) {
00104       return d1;
00105     }
00106   } else if(d2->grounded) {
00107     return d2;
00108   }
00109 
00110   if(d1->preference < d2->preference) {
00111     return d2;
00112   } else {
00113     if(d1->preference > d2->preference) {
00114       return d1;
00115     }
00116   }
00117 
00118   if(d2->rank < d1->rank) {
00119     return d2;
00120   } else {
00121     return d1;
00122   }
00123 }
00124 
00125 static rpl_parent_t *
00126 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
00127 {
00128   rpl_rank_t r1, r2;
00129   rpl_dag_t *dag;
00130   
00131   PRINTF("RPL: Comparing parent ");
00132   PRINT6ADDR(&p1->addr);
00133   PRINTF(" (confidence %d, rank %d) with parent ",
00134         p1->link_metric, p1->rank);
00135   PRINT6ADDR(&p2->addr);
00136   PRINTF(" (confidence %d, rank %d)\n",
00137         p2->link_metric, p2->rank);
00138 
00139 
00140   r1 = DAG_RANK(p1->rank, p1->dag->instance) * NEIGHBOR_INFO_ETX_DIVISOR +
00141          p1->link_metric;
00142   r2 = DAG_RANK(p2->rank, p1->dag->instance) * NEIGHBOR_INFO_ETX_DIVISOR +
00143          p2->link_metric;
00144   /* Compare two parents by looking both and their rank and at the ETX
00145      for that parent. We choose the parent that has the most
00146      favourable combination. */
00147 
00148   dag = (rpl_dag_t *)p1->dag; /* Both parents must be in the same DAG. */
00149   if(r1 < r2 + MIN_DIFFERENCE &&
00150      r1 > r2 - MIN_DIFFERENCE) {
00151     return dag->preferred_parent;
00152   } else if(r1 < r2) {
00153     return p1;
00154   } else {
00155     return p2;
00156   }
00157 }
00158 
00159 static void
00160 update_metric_container(rpl_instance_t *instance)
00161 {
00162   instance->mc.type = RPL_DAG_MC_NONE;
00163 }