[Tinyos-2-commits] CVS: tinyos-2.x-contrib/stanford-sing/s4-tinyos-2.x/tos/lib/bvr BVR.h, NONE, 1.1 CoordinateTable.h, NONE, 1.1 CoordinateTable.nc, NONE, 1.1 CoordinateTableC.nc, NONE, 1.1 CoordinateTableM.nc, NONE, 1.1 RouteToInterface.nc, NONE, 1.1 S4.h, NONE, 1.1 S4Locator.nc, NONE, 1.1 S4Neighborhood.nc, NONE, 1.1 S4RouterC.nc, NONE, 1.1 S4RouterM.nc, NONE, 1.1 S4StateC.nc, NONE, 1.1 S4StateCommand.nc, NONE, 1.1 S4StateM.nc, NONE, 1.1 S4Topology.nc, NONE, 1.1 S4TopologyC.nc, NONE, 1.1 S4TopologyM.nc, NONE, 1.1 coordinate_table_entry.h, NONE, 1.1 coordinates.h, NONE, 1.1 nexthopinfo.h, NONE, 1.1 topology-old.h, NONE, 1.1 topology.h, NONE, 1.1 topology.sample.testbed.h, NONE, 1.1

Tahir Azim genie1 at users.sourceforge.net
Thu Oct 23 15:22:42 PDT 2008


Update of /cvsroot/tinyos/tinyos-2.x-contrib/stanford-sing/s4-tinyos-2.x/tos/lib/bvr
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv29813/bvr

Added Files:
	BVR.h CoordinateTable.h CoordinateTable.nc CoordinateTableC.nc 
	CoordinateTableM.nc RouteToInterface.nc S4.h S4Locator.nc 
	S4Neighborhood.nc S4RouterC.nc S4RouterM.nc S4StateC.nc 
	S4StateCommand.nc S4StateM.nc S4Topology.nc S4TopologyC.nc 
	S4TopologyM.nc coordinate_table_entry.h coordinates.h 
	nexthopinfo.h topology-old.h topology.h 
	topology.sample.testbed.h 
Log Message:
tos/lib Library files for S4 tinyos 2.x port.


--- NEW FILE: BVR.h ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: BVR.h,v 1.1 2008/10/23 22:22:40 genie1 Exp $

/*
 * "Copyright (c) 2000-2003 The Regents of the University  of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
 * 94704.  Attention:  Intel License Inquiry.
 */

/*
 * Authors:  Rodrigo Fonseca
 * Date Last Modified: 2005/05/26
 */
#ifdef BEACON_ELECTION
#include "CBRouting_unslotted.h"
#endif

#ifndef BVR_ROUTING_H
#define BVR_ROUTING_H

#include "AM.h"
#include "util.h"
#include "topology.h"
#include "LinkEstimator.h"

#ifndef S4_ROUTING_H
#include "S4.h"
#endif

#include "coordinates.h"


enum {
  AM_BVR_APP_MSG    = 55,//0x37
  AM_BVR_APP_P_MSG  = 54,//0x36
  AM_BVR_BEACON_MSG = 56,//0x38
  BVR_UART_ADDR = 57,

};

#ifndef BVR_APP_DATA_LENGTH
#define  BVR_APP_DATA_LENGTH 1
#endif





typedef nx_struct {
  nx_uint8_t valid;
  nx_uint16_t parent;
  nx_uint8_t last_seqno;
  nx_uint8_t hops;
  nx_uint8_t combined_quality; //stores the quality combined quality from the parent up to the root
#ifdef ETX_TOLERANCE
  nx_uint8_t tolerance;
#endif
} BVRRootBeacon;


#include "coordinate_table_entry.h"

enum {
  BVR_APP_MODE_FALLBACK_MASK = 0x80
};



typedef nx_struct {
  nx_uint8_t data[BVR_APP_DATA_LENGTH];
}  BVRData;


/* Used for AM_BVR_APP_MSG, carries application data multihop.
 * BVRRouter uses this structure for storing the multihop routing data
 */

typedef nx_struct {       //for AM_BVR_APP_MSG, carries application data multihop
  nx_uint8_t hopcount;
  Coordinates dest;
  nx_uint16_t dest_id;
  nx_uint16_t  origin;       //the originator of the message
  nx_uint8_t mode;           //most significant bit: fallback? other 7: current mode
  nx_uint16_t fallback_thresh; //the value of the main metric when entering fallback
  nx_uint16_t msg_id;
  BVRData data;
} BVRAppData;

/* This struct is the same as the above but has an extra field - slot - that
   allows parametrization of the interface provided by BVRRouter */
typedef nx_struct {       //for AM_BVR_APP_P_MSG, carries application data multihop
  nx_uint8_t hopcount;
  Coordinates dest;
  nx_uint16_t dest_id;
  nx_uint16_t  origin;       //the originator of the message
  nx_uint8_t mode;           //most significant bit: fallback? other 7: current mode
  nx_uint16_t fallback_thresh; //the value of the main metric when entering fallback
  nx_uint8_t slot;           //added for demultiplexing
  nx_uint16_t msg_id;
  BVRData data;
}  BVRAppPData;




//sizeof = 3 + MAX_ROOT_BEACONS*3. For B=5, 3 + 15 = 18 bytes.
//For B=8, 3+24 = 27, B=12, 3+36 = 39
typedef nx_struct {
  nx_uint16_t seqno;        //the sequence number of my beacon messages
  BeaconInfo beacons[MAX_ROOT_BEACONS] ;
}  BVRBeaconMsgData;


//size: 4 + sizeof(BeaconMsgData). B=5, 22; B=8, 31, B=12, 43
typedef nx_struct BVR_Beacon_Msg {
  LEHeader header;
  BVRBeaconMsgData type_data;
}  BVRBeaconMsg;


typedef nx_struct BVR_Raw_Msg {
  LEHeader header;
} BVRRawMsg;


typedef nx_struct BVR_App_Msg{
  LEHeader header;
  BVRAppData type_data;
}  BVRAppMsg;

typedef nx_struct BVR_App_P_Msg{
  LEHeader header;
  BVRAppPData type_data;
}  BVRAppPMsg;


/***/
#endif  //BVR_ROUTING_H

--- NEW FILE: CoordinateTable.h ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: CoordinateTable.h,v 1.1 2008/10/23 22:22:40 genie1 Exp $
                                    
/*                                                                      
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.             
 *                                  
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *                                  
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *                                  
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *                                  
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.             
 *                                  
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */                                 
                                    
/*                                  
 * Authors:  Rodrigo Fonseca        
 * Date Last Modified: 2005/05/26
 */


#ifndef COORD_T_H
#define COORD_T_H

enum {
  COORD_TABLE_SIZE = N_CACHE_SIZE,
};

#endif

--- NEW FILE: CoordinateTable.nc ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: CoordinateTable.nc,v 1.1 2008/10/23 22:22:40 genie1 Exp $
                                    
/*                                                                      
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.             
 *                                  
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *                                  
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *                                  
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *                                  
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.             
 *                                  
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */                                 
                                    
/*                                  
 * Authors:  Rodrigo Fonseca        
 * Date Last Modified: 2005/05/26
 */



includes BVR;
includes nexthopinfo;

interface CoordinateTable {
  command uint8_t getSize();
  command uint8_t getOccupied();

  /** Get the entry corresponding to address 
   *  @return Pointer to entry NULL if entry not stored 
   */
  command CoordinateTableEntry* getEntry(uint16_t addr);

  /* This is used by the command interface. The interface is such for
     uniformity with other command interfaces */
  command error_t getEntryByIndex(uint8_t idx, CoordinateTableEntry** ce);

  /* Tell the table to not use link addr anymore. This is primarily
     to maintain the consistency with a table of available links,
     such as that provided by LinkEstimator */
  command error_t removeEntry(uint16_t addr);

  command error_t updateQuality(uint16_t addr, uint16_t quality);



  /** Store the entry given by the parameters
   *  This method has to deal with the replacement policy
   *  @return Pointer to entry or NULL if cannot store
   */
  command CoordinateTableEntry* storeEntry(uint16_t addr, uint16_t first_hop, 
                                                           uint8_t seqno, uint8_t quality, 
                                                           Coordinates* coords);

  /* Returns an ordered list of neighbors that are closer than min_dist
   * The list is ordered by distance,quality
   */
  command error_t getNextHops(Coordinates* dest, uint16_t dest_addr, 
                                               uint16_t min_dist, nextHopInfo* next_hops);
                                               

}


--- NEW FILE: CoordinateTableC.nc ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:

// $Id: CoordinateTableC.nc,v 1.1 2008/10/23 22:22:40 genie1 Exp $

                                    

/*                                                                      

 * "Copyright (c) 2000-2003 The Regents of the University  of California.  

 * All rights reserved.             

 *                                  

 * Permission to use, copy, modify, and distribute this software and its

 * documentation for any purpose, without fee, and without written agreement is

 * hereby granted, provided that the above copyright notice, the following

 * two paragraphs and the author appear in all copies of this software.

 *                                  

 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR

 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT

 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF

 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 *                                  

 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,

 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY

 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS

 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO

 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."

 *                                  

 * Copyright (c) 2002-2003 Intel Corporation

 * All rights reserved.             

 *                                  

 * This file is distributed under the terms in the attached INTEL-LICENSE     

 * file. If you do not find these files, copies can be found by writing to

 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 

 * 94704.  Attention:  Intel License Inquiry.

 */                                 

                                    

/*                                  

 * Authors:  Rodrigo Fonseca        

 * Date Last Modified: 2005/05/26

 */



configuration CoordinateTableC {

  provides {

    interface CoordinateTable;

    interface FreezeThaw;

    interface StdControl;

    interface Init;

  }

}

  

implementation {

  components CoordinateTableM

           , new TimerMilliC()

           //, UARTLogger

	   ;

  CoordinateTable = CoordinateTableM;

  FreezeThaw = CoordinateTableM;

  StdControl = CoordinateTableM;

  Init = CoordinateTableM;

 

  //CoordinateTableM.Logger -> UARTLogger;

}


--- NEW FILE: CoordinateTableM.nc ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: CoordinateTableM.nc,v 1.1 2008/10/23 22:22:40 genie1 Exp $
                                    
/*                                                                      
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.             
 *                                  
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *                                  
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *                                  
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *                                  
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.             
 *                                  
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */                                 
                                    
/*                                  
 * Authors:  Rodrigo Fonseca        
 * Date Last Modified: 2005/05/26
 */


//All of the expiration will be taken care of by the Link Estimator module

includes AM;
includes BVR;
includes CoordinateTable;
includes nexthopinfo;


module CoordinateTableM {
  provides {
    interface CoordinateTable;
    interface FreezeThaw;
    interface StdControl;
    interface Init;
  }
  
}

implementation {
  uint8_t num_active;
  CoordinateTableEntry table[COORD_TABLE_SIZE];
  CoordinateTableEntry* tablePtrs[COORD_TABLE_SIZE];

  bool state_is_active;


  command error_t Init.init() {
    int i;
    state_is_active = TRUE;
    dbg("BVR-debug", "CoordinateTableM.init\n");
    for (i = 0; i < COORD_TABLE_SIZE; i++) {      
      CTEntryInit(&table[i]);
    }
    
    dbg("BVR-debug", "CoordinateTableM.init ended\n");
    return SUCCESS;
  }
  command error_t StdControl.start() {
    return SUCCESS;
  }
  command error_t StdControl.stop() {
    return SUCCESS;
  }

  command error_t FreezeThaw.freeze() {
    dbg("BVR-debug","CoordinateTableM$freeze\n");
    state_is_active = FALSE;
    return SUCCESS;
  }
  command error_t FreezeThaw.thaw() {
    dbg("BVR-debug","CoordinateTableM$thaw\n");
    state_is_active = TRUE;
    return SUCCESS;
  }
  command uint8_t CoordinateTable.getSize() {
    return COORD_TABLE_SIZE;
  }

  void printTable() {
    int i;
    for (i = 0; i < num_active; i++) {
      dbg("BVR-debug","CoordinateTable: [%d(%d)] addr:%d through:%d quality:%d age:%d coords: ",
                     i,tablePtrs[i]->pos,tablePtrs[i]->addr,tablePtrs[i]->first_hop,
                     tablePtrs[i]->quality,
                      tablePtrs[i]->age);
      coordinates_print(&tablePtrs[i]->coords);
    }
  }

  /* This sorts the qualifying nextHops and returns the BVR_MAX_NEXT_HOPS best.
   * This is a truncated insertion sort, O(num_active*BVR_MAX_NEXT_HOPS)
   * Update:
   *  - only use nodes that have the components present in the
   *    destination
   *  - this is the first step in using the progressive dropping of
   *    beacons, which will involve adding the distance at each k to
   *    the packet as well.
   */ 
  command error_t CoordinateTable.getNextHops(Coordinates* dest, uint16_t dest_addr, 
                                               uint16_t min_dist, nextHopInfo* next_hops) {
    int i,k;
    uint8_t metric = COORDS_DIST_WEIGHTED;
    uint16_t di = 0;
    bool valid;
#ifdef EXP_PROGRESS
    uint16_t new_exp_progress = 0;
    uint16_t exp_progress[BVR_MAX_NEXT_HOPS];
#endif
    
    if (dest == NULL || next_hops == NULL) {
      return FAIL;
    }
    //return at most BVR_MAX_NEXT_HOPS that have distance smaller than min_dist,
    //sorted by distance and then by quality
    //There is one exception to the ordering: if a node is the destination, it takes
    //precedence over all others
    next_hops->n = 0;
    dbg("BVR","CoordinateTable$getNextHops: min_dist %d max_entries:%d dest_addr:%d\n",
        min_dist,BVR_MAX_NEXT_HOPS,dest_addr);
    for (i = 0; i < num_active; i++) {
      valid = coordinates_has_same_valid_components(&tablePtrs[i]->coords, dest);
      di = coordinates_distance(&tablePtrs[i]->coords, dest, metric);
#ifdef EXP_PROGRESS
      new_exp_progress = (di < min_dist)? (uint16_t)((min_dist - di) * (tablePtrs[i]->quality/255.0)):0;
#endif
      dbg("BVR","CoordinateTable$getNextHops: ct entry: addr %d dist %d qual %d valid %d ",
          tablePtrs[i]->addr, di, tablePtrs[i]->quality, valid);
      coordinates_print(&tablePtrs[i]->coords);
      if (tablePtrs[i]->addr == dest_addr || 
          (valid && di < min_dist)) { 
        /* Add in order to next_hops*/
        /* This is inlined here to save stack space */
        for (k = next_hops->n; k > 0; k--) {
#ifndef EXP_PROGRESS
          if (tablePtrs[i]->addr == dest_addr ||                      //is destination or
              (tablePtrs[next_hops->next_hops[k-1]]->addr != dest_addr &&
              (di <  next_hops->distances[k-1] ||                      //distante is smaller
               (di == next_hops->distances[k-1] &&                      //or distance is equal and
               tablePtrs[i]->quality > 
               tablePtrs[next_hops->next_hops[k-1]]->quality))))        //quality is better
#else
          if (tablePtrs[i]->addr == dest_addr ||                      //is destination or
              (tablePtrs[next_hops->next_hops[k-1]]->addr != dest_addr &&
              (new_exp_progress >  exp_progress[k-1] ||                      //new_exp_progress is larger
               (new_exp_progress == exp_progress[k-1] &&                      //new_exp_progress is is equal and
               di < next_hops->distances[k-1]))))        //distance is better
#endif
          {
            if (k < BVR_MAX_NEXT_HOPS) {                   //shift element at k-1 right
#ifdef EXP_PROGRESS
              dbg("BVR","\tCoordinateTable$getNextHops: shifting entry <i,addr,dist,qual,e_prog> <%d,%d,%d,%d,%d>\n",
                  next_hops->next_hops[k-1],tablePtrs[next_hops->next_hops[k-1]]->addr,next_hops->distances[k-1],
                  tablePtrs[next_hops->next_hops[k-1]]->quality,exp_progress[k-1]);
              exp_progress[k] = exp_progress[k-1];
#else
              dbg("BVR","\tCoordinateTable$getNextHops: shifting entry <i,addr,dist,qual> <%d,%d,%d,%d>\n",
                  next_hops->next_hops[k-1],tablePtrs[next_hops->next_hops[k-1]]->addr,next_hops->distances[k-1],
                  tablePtrs[next_hops->next_hops[k-1]]->quality);
#endif
              next_hops->next_hops[k] = next_hops->next_hops[k-1];
              next_hops->distances[k] = next_hops->distances[k-1];
            } else {
#ifdef EXP_PROGRESS
              dbg("BVR","\tCoordinateTable$getNextHops: replacing entry <i,addr,dist,qual,e_prog> <%d,%d,%d,%d,%d>\n",
                  next_hops->next_hops[k-1],tablePtrs[next_hops->next_hops[k-1]]->addr,next_hops->distances[k-1],
                  tablePtrs[next_hops->next_hops[k-1]]->quality,exp_progress[k-1]);
#else 
              dbg("BVR","\tCoordinateTable$getNextHops: replacing entry <i,addr,dist,qual> <%d,%d,%d,%d>\n",
                  next_hops->next_hops[k-1],tablePtrs[next_hops->next_hops[k-1]]->addr,next_hops->distances[k-1],
                  tablePtrs[next_hops->next_hops[k-1]]->quality);
#endif
            }
          } else 
            break;
        }
        if (k < BVR_MAX_NEXT_HOPS) {                      //insert new element at k
#ifndef EXP_PROGRESS
          dbg("BVR","\tCoordinateTable$getNextHops: * inserting new entry <i,addr,dist,qual> <%d,%d,%d,%d> at k %d\n",
              i,tablePtrs[i]->addr,di,tablePtrs[i]->quality,k);
#else
          dbg("BVR","\tCoordinateTable$getNextHops: * inserting new entry <i,addr,dist,qual,e_prog> <%d,%d,%d,%d,%d> at k %d\n",
              i,tablePtrs[i]->addr,di,tablePtrs[i]->quality,new_exp_progress,k);
          exp_progress[k] = new_exp_progress;
#endif
          next_hops->next_hops[k] = i;
          next_hops->distances[k] = di;
          if (next_hops->n < BVR_MAX_NEXT_HOPS)
            next_hops->n++;
        } else {
          dbg("BVR","\tCoordinateTable$getNextHops: * rejecting new entry <i,addr,dist,qual> <%d,%d,%d,%d> (k %d)\n",
              i,tablePtrs[i]->addr,di,tablePtrs[i]->quality,k);
        }
      }
    }
    /*Convert the stored indices into addresses*/
    for (k = 0; k <next_hops->n ; k++) {
      dbg("BVR","CoordinateTable$getNextHops: nh entry: addr %d dist %d qual %d\n",
          tablePtrs[next_hops->next_hops[k]]->addr, next_hops->distances[k], 
          tablePtrs[next_hops->next_hops[k]]->quality);
      next_hops->next_hops[k] = tablePtrs[next_hops->next_hops[k]]->addr;
    }
    dbg("BVR","coordinateTable$getNextHops: done\n");
    return SUCCESS;
  }


  command uint8_t CoordinateTable.getOccupied() {
    return num_active;
  }

  /*  This function is the same as the provided one, it is here
   *  so that other commands can also use it.
   *  
   *  current_entry is just a hint to speed up the search if
   *  we are looking for the same entry as last time.
   */
  static CoordinateTableEntry* get_entry(uint16_t addr) {
    uint8_t i;
    uint8_t idx = 0;
    static uint8_t current_entry = 0;
    CoordinateTableEntry* e = NULL;

    if (current_entry > num_active) current_entry = 0;

    for (i = 0; i < num_active && e == NULL; i++) {
      idx = (i + current_entry) % num_active;
      if (tablePtrs[idx]->addr == addr)
        e = tablePtrs[idx];
    }
    current_entry = idx;
    dbg("BVR-debug","COORDS: CoordinateTableM$get_entry called w addr: %d returning %p\n",addr,e);
    return e;
  }

  static CoordinateTableEntry* get_free() {
    uint8_t i;
    CoordinateTableEntry* ce = NULL;

    if (num_active == COORD_TABLE_SIZE)
      return NULL;

    for (i = 0; i < COORD_TABLE_SIZE && ce == NULL; i++) {
      if (table[i].valid == FALSE)
        ce = &table[i];
    }
    return ce;
  }
  
  /** Get the entry corresponding to address 
   *  @return Pointer to entry NULL if entry not stored 
   */
  command CoordinateTableEntry* CoordinateTable.getEntry(uint16_t addr) {
    return get_entry(addr);
  }

  /* Remove the entry itself and any entries that have this as the first_hop */
  /* TODO: this is not cleaning up entries that have this as the first_hop! */
  static void removeEntry(uint16_t addr) {
    int i,j,removed;
    removed = 0;
    

    for (i = 0, j=0; j < num_active; i++, j++) {
      while (j < num_active && (tablePtrs[j]->addr == addr || tablePtrs[j]->first_hop == addr)) {
        //clean j
        //call Logger.LogDropNeighbor(tablePtrs[j]->addr);
        tablePtrs[j]->valid = FALSE;
        j++;
        removed++;
      }
      if (i < j && j < num_active) {
        tablePtrs[i] = tablePtrs[j];
        tablePtrs[i]->pos = i;
      }
    }
    if (removed) {
      num_active -= removed;
      dbg("BVR","COORD_TABLE: cleanup\n");
      printTable();
    }
  }
  
 
  command error_t CoordinateTable.removeEntry(uint16_t addr) {
    if (state_is_active) 
      removeEntry(addr);
    return SUCCESS;
  }

  command error_t CoordinateTable.getEntryByIndex(uint8_t i, CoordinateTableEntry ** ce) {
    if (i >= num_active || ce == NULL) 
      return FAIL;
    *ce = tablePtrs[i];
    return SUCCESS;
  }

  /** Store the entry given by the paramters
   *  This method has to deal with the replacement policy
   *  Currently: there is no replacement, but new entries can be added when
   *  old ones expire.
   *  Options: good spread, eliminate repeated entries, etc...
   *  @return Pointer to entry or NULL if cannot store
   */
  command CoordinateTableEntry* CoordinateTable.storeEntry(uint16_t addr, uint16_t first_hop, 
                                                           uint8_t seqno, uint8_t quality, 
                                                           Coordinates* coords)  
  {
    CoordinateTableEntry* e = NULL;
    if (!state_is_active) {
      return e;
    }
    dbg("BVR-debug","CoordinateTable$storeEntry: called with addr: %d first_hop: %d seqno: %d qual: %d \n", 
      addr, first_hop, seqno, quality);
    e = get_entry(addr);
    if (e != NULL) {
      dbg("BVR-debug","CoordinateTable$storeEntry: store called on item that was already stored\n");
    } else {
      //is there space?
      if (num_active < COORD_TABLE_SIZE) {
        //there is space
        tablePtrs[num_active] = get_free();
        if (tablePtrs[num_active] == NULL) 
          dbg("BVR-error","get_free returned null when it shouldn't!!!\n");
        tablePtrs[num_active]->valid = TRUE; 
        tablePtrs[num_active]->first_hop = first_hop;
        tablePtrs[num_active]->last_seqno = seqno;
        tablePtrs[num_active]->addr = addr;
        tablePtrs[num_active]->quality = quality;
        coordinates_copy(coords,&tablePtrs[num_active]->coords);
        tablePtrs[num_active]->age = 0;
        tablePtrs[num_active]->pos = num_active;
        e = tablePtrs[num_active];
        //call Logger.LogAddNeighbor(e);
        num_active++;
      } else {
        //we must replace someone if this guy should really be admitted
        //XXX: will not replace anyone. They will have to expire.
        //I believe that this, and the link quality filtering, will
        //be enough to get us going
        dbg("BVR-debug","CoordinateTable$storeEntry: item not in cache, cache full\n");
      }
    }
    printTable();
    return e;
  }  


  command error_t CoordinateTable.updateQuality(uint16_t addr, uint16_t quality) {
    CoordinateTableEntry *e;

    if (!state_is_active)
      return SUCCESS;

    e = get_entry(addr);
    if (e != NULL) {
      CTEntryUpdateQuality(e,quality);
      //call Logger.LogUpdateNeighbor(e);
    }
    printTable();
    return SUCCESS;
  }
  
    


}
  

--- NEW FILE: RouteToInterface.nc ---
interface RouteToInterface {
    command error_t routeTo(Coordinates * destination, uint16_t addr, uint8_t
		 mode);   
}

--- NEW FILE: S4.h ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: S4.h,v 1.1 2008/10/23 22:22:40 genie1 Exp $

/*
 * "Copyright (c) 2000-2003 The Regents of the University  of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
 * 94704.  Attention:  Intel License Inquiry.
 */

/*
 * Authors:  Rodrigo Fonseca
 * Date Last Modified: 2005/05/26
 */

/*
 * Authors:  Feng Wang, Univ. of Texas at Austin, CS Department
 * Date Last Modified: 2006/03/02
 */

#ifdef BEACON_ELECTION
#include "CBRouting_unslotted.h"
#endif

#ifndef S4_ROUTING_H
#define S4_ROUTING_H

#include "AM.h"
#include "util.h"
#include "topology.h"
#include "LinkEstimator.h"

#include "coordinates.h"


enum {
  AM_S4_APP_MSG    = 55,//0x37
  AM_S4_APP_P_MSG  = 54,//0x36
  AM_S4_BEACON_MSG = 56,//0x38

//added by Feng Wang

#ifdef LOCAL_DV
  // new type for distance vector msg
  AM_DV_MSG = 201,//0xc9
#endif

  // added for MIG
  AM_S4_RAW_MSG    = 202,//0xca

#ifdef FAILURE_RECOVERY
  AM_FRREQ_MSG      = 203,//0xcb
  AM_FRREP_MSG      = 204,//0xcb
#endif
};

#ifndef S4_APP_DATA_LENGTH
#define  S4_APP_DATA_LENGTH 1
#endif

//added by Feng Wang

//maximum number of beacon retransmissions
#ifndef MAX_BEACON_SEND_RETRIES
#define MAX_BEACON_SEND_RETRIES 5
#endif

#ifdef CRROUTING
//maximum cluster size
#ifndef MAX_CLUSTER_SIZE
#ifdef PLATFORM_PC

#if defined(N1000) && defined(B8)
#define MAX_CLUSTER_SIZE 500
#elif defined(N1000) && (defined(B16) || defined(B24))
#define MAX_CLUSTER_SIZE 300
#elif defined(N1000) || defined(N500)
#define MAX_CLUSTER_SIZE 200
#endif
#if defined(N2000)
#define MAX_CLUSTER_SIZE 300
#endif
#if defined(N3000)
#define MAX_CLUSTER_SIZE 400
#endif
#if defined(N4000)
#define MAX_CLUSTER_SIZE 500
#endif
#if defined(N100)
#define MAX_CLUSTER_SIZE 50
#endif

#else
#define MAX_CLUSTER_SIZE 100
#endif

#endif
#endif

#ifdef RELIABLE_BCAST

//maximum number of pending beacon broadcast
#ifndef MAX_PENDING_BEACON
#ifdef PLATFORM_PC
#define MAX_PENDING_BEACON 20
#else
#define MAX_PENDING_BEACON 5
#endif
#endif

//maximum number of pending cluster broadcast
#ifndef MAX_PENDING_DV
#ifdef PLATFORM_PC
#define MAX_PENDING_DV 50
#else
#define MAX_PENDING_DV 10
#endif
#endif

//maximum number of re-broadcasts for reliability
#ifndef MAX_REBCAST
#define MAX_REBCAST 3
#endif

#endif

#ifdef MULTIPLE_BEACON
#ifndef MAX_BEACON_VECTOR_SIZE
#if defined(N500) || defined(N1000) || defined(N2000) || defined(N3000) || defined(N4000)
//maximum size of beacon vector (assume 56 bytes max TOSH_DATA_LENGTH)
#define MAX_BEACON_VECTOR_SIZE 12
//maximum size of beacon vector (assume 130 bytes max TOSH_DATA_LENGTH)
//#define MAX_BEACON_VECTOR_SIZE 30
#else
//maximum size of beacon vector (assume 29 bytes max TOSH_DATA_LENGTH)
#define MAX_BEACON_VECTOR_SIZE 8
#endif
#endif
#endif

#ifdef LOCAL_DV
#ifndef MAX_VECTOR_SIZE
#if defined(N500) || defined(N1000) || defined(N2000) || defined(N3000) || defined(N4000)
//maximum size of distance vector (assume 56 bytes max TOSH_DATA_LENGTH)
#define MAX_VECTOR_SIZE 8
//maximum size of distance vector (assume 130 bytes max TOSH_DATA_LENGTH)
//#define MAX_VECTOR_SIZE 20
#else
//maximum size of distance vector (assume 29 bytes max TOSH_DATA_LENGTH)
#define MAX_VECTOR_SIZE 4
#endif
#endif
#endif

#define SCOPE_LIFETIME 6

#ifdef PLATFORM_PC

#if defined(N100) || defined(N500) || defined(N1000)
#define MAX_BEACON_ROUND 60
#define MAX_DV_ROUND 20
#endif
#ifdef N2000
#define MAX_BEACON_ROUND 80
#define MAX_DV_ROUND 30
#endif
#ifdef N3000
#define MAX_BEACON_ROUND 100
#define MAX_DV_ROUND 40
#endif
#ifdef N4000
#define MAX_BEACON_ROUND 120
#define MAX_DV_ROUND 40
#endif

#else
#define MAX_BEACON_ROUND 60
#define MAX_DV_ROUND 20
#endif

//added by Feng Wang on Oct 4, only accept new seqno if greater by SEQNO_DISTANCE
#define SEQNO_DISTANCE 10

//I_ stands for Initial
//Timings
enum {
  I_DELAY_TIMER = 250,  //delay for forwarding beacon messages
  I_DELAY_CMD_TIMER = 25,  //delay for forwarding beacon messages
  I_RADIO_SETTING = 64, //mica2dot testbed:192 is fine. 0x70 for mica2's?
  I_BEACON_INTERVAL = 10000u,
  I_RADIO_CMD = 255, //default power setting for command packets
#ifdef SHORT_INTERVAL
  INTERVAL_MUL = 1,
#endif
#ifdef MEDIUM_INTERVAL
  INTERVAL_MUL = 6,
#endif
#ifdef LONG_INTERVAL
  INTERVAL_MUL = 36,
#endif
  I_BEACON_JITTER = 10000u,
  I_BEACON_START = 10000u,

  I_BEACON_UPDATE_INTERVAL = 10000u,
#ifdef RELIABLE_BCAST
  I_BEACON_RELIABILITY_CHECK_INTERVAL = 20, //!!! in SECONDS, not ms!!!
#endif

#ifdef FAILURE_RECOVERY
  I_WAIT_FR = 250,  //is 200ms long enough?
  FR_CW = 100,
#endif

  //added by Feng Wang on Mar 16
#ifdef CHECK_LINK
  I_CHECK_LINK_INTERVAL = 10000u, //check link condition every 30s
  WAIT_BEACON_ROUNDS = 6,
#endif
};

enum {
  PARENT_SWITCH_THRESHOLD = 15, //20% (was 30)
};

enum {
#ifdef PLATFORM_PC
  NOT_ROOT_BEACON = 65535,
#else
  NOT_ROOT_BEACON = 255,
#endif
};

typedef struct {
  bool valid;
  uint16_t parent;
  uint8_t last_seqno;
  uint8_t hops;
  uint8_t combined_quality; //stores the quality combined quality from the parent up to the root

  uint16_t nodeid;
#ifdef ETX_TOLERANCE
  uint8_t tolerance;
#endif

#ifdef DELETE_OLD_ENTRY
  uint16_t sent_time; //needed to calculate age if we want to delete old entries
#endif

  /* add two flags "updated" & "bcastlost",
   * to specify whether this entry has been updated since last bcast,
   * or whether the bcast is "lost" (i.e. having not overheard
   * re-broadcast from sufficiently many neighbors).
   * Each time BeaconUpdateTimer expires,
   * we only need to broadcast those entries w/ these flags being true,
   * while each time BeaconTimer expires,
   * we broadcast all valid entries.
   */
  bool updated;

#ifdef RELIABLE_BCAST
  bool bcastlost;
#endif

#ifdef SHOW_BEACONID
  uint16_t nodeid;
#endif
} S4RootBeacon;

typedef nx_struct {
  nx_uint16_t parent[MAX_ROOT_BEACONS];
  //nx_uint16_t parent[N_ROOT_BEACONS];
} CoordsParents;

#ifdef FW_COORD_TABLE
#include "coordinate_table_entry.h"
#endif

/*
enum {
  S4_APP_MODE_FALLBACK_MASK = 0x80
};
*/

enum {
  MSG_VALID_RANGE8 = 128      //Valid range for sequence numbers with 8 bits
};

/*Metrics for Routing*/
enum {
  METRIC_CLOSEST_BEACON = 6,
};

typedef nx_struct {
  nx_uint8_t data[S4_APP_DATA_LENGTH];
}  S4Data;


/* Used for AM_S4_APP_MSG, carries application data multihop.
 * S4Router uses this structure for storing the multihop routing data
 */

typedef nx_struct {       //for AM_S4_APP_MSG, carries application data multihop
  nx_uint8_t hopcount;
#ifdef CRROUTING
  nx_uint8_t closest_beacon;
#endif
  nx_uint16_t dest_id;
  nx_uint16_t  origin;       //the originator of the message
  //uint8_t mode;           //most significant bit: fallback? other 7: current mode
  //uint16_t fallback_thresh; //the value of the main metric when entering fallback
  nx_uint16_t msg_id;

  //added by Feng Wang on Sept. 22, to log # of retransmissions
  nx_uint8_t rexmit_count;
  nx_uint8_t tried_hopcount;

  S4Data data;
}  S4AppData;

/* This struct is the same as the above but has an extra field - slot - that
   allows parametrization of the interface provided by S4Router */
typedef nx_struct {       //for AM_S4_APP_P_MSG, carries application data multihop
  nx_uint8_t hopcount;
#ifdef CRROUTING
  nx_uint8_t closest_beacon;
#endif
  //Coordinates dest;
  nx_uint16_t dest_id;
  nx_uint16_t  origin;       //the originator of the message
  //uint8_t mode;           //most significant bit: fallback? other 7: current mode
  //uint16_t fallback_thresh; //the value of the main metric when entering fallback
  nx_uint8_t slot;           //added for demultiplexing
  nx_uint16_t msg_id;
  S4Data data;

  //added by Feng Wang on Sept. 22, to log # of retransmissions
  nx_uint8_t rexmit_count;
  nx_uint8_t tried_hopcount;

}  S4AppPData;



/* New S4BeaconMsg that incorporates both beacon and root beacon
 * messages into one periodic transmission
 * This message will only go 1 hop away */

/* sizeof = 3 */
typedef nx_struct BeaconInfo {

#ifdef MULTIPLE_BEACON
  /* add a field specifying beacon id,
   * since now we allow to send some, not all, beacons
   */
  nx_uint8_t beacon_id;

  nx_uint16_t nodeid;
#endif

  nx_uint8_t hopcount;
  nx_uint8_t seqno;
  nx_uint8_t quality;
}  BeaconInfo;

//sizeof = 3 + MAX_BEACON_VECTOR_SIZE*3.
typedef nx_struct {
  nx_uint16_t seqno;        //the sequence number of my beacon messages
#ifndef MULTIPLE_BEACON
  BeaconInfo beacons[MAX_ROOT_BEACONS];
  //BeaconInfo beacons[N_ROOT_BEACONS];
#else
  BeaconInfo beacons[MAX_BEACON_VECTOR_SIZE];
#endif
}  S4BeaconMsgData;


//size: 4 + sizeof(BeaconMsgData).
typedef nx_struct S4_Beacon_Msg {
  LEHeader header;
  S4BeaconMsgData type_data;
}  S4BeaconMsg;


typedef nx_struct S4_Raw_Msg {
  LEHeader header;
}  S4RawMsg;


typedef nx_struct S4_App_Msg{
  LEHeader header;
  S4AppData type_data;
}  S4AppMsg;

typedef nx_struct S4_App_P_Msg{
  LEHeader header;
  S4AppPData type_data;
}  S4AppPMsg;


//added by Feng Wang

#ifdef RELIABLE_BCAST
//maintain state of pending broadcast sessions, to achieve reliable broadcast
typedef struct {
  bool occupied;  //whether this entry is occupied
  uint8_t beacon_id;
  uint8_t n_rcv;  //have overheard this many neighbors re-broadcast
  uint8_t sendtime; //time of broadcast (in seconds, upper-bounded by timeout)
  uint8_t retries;  //# of retransmissions
} PendingBeacons;
#endif

#ifdef LOCAL_DV

//entry in the cluster routing table
typedef struct {
  bool valid;
#ifdef TOSSIM
  uint16_t dest;  //destination
  uint16_t parent;  //next hop
#else
    uint8_t dest;
    uint8_t parent;
#endif
  uint8_t last_seqno; //can be replaced if we have sent_time
  uint8_t hops;
  uint8_t scope;  //need to store the scope, which is required in DV msg
    //uint8_t combined_quality; //combined quality from the parent up to the root
#ifdef ETX_TOLERANCE
  uint8_t tolerance;
#endif

#ifdef DELETE_OLD_ENTRY
  uint16_t sent_time; //needed to calculate age if we want to delete old entries
#endif

  /* add two flags "updated" & "bcastlost",
   * to specify whether this entry has been updated since last bcast,
   * or whether the bcast is "lost" (i.e. having not overheard
   * re-broadcast from sufficiently many neighbors).
   * Each time ClusterUpdateTimer expires,
   * we only need to broadcast those entries w/ these flags being true,
   * while each time ClusterTimer expires,
   * we broadcast all valid entries.
   */
  bool updated;

#ifdef RELIABLE_BCAST
  bool bcastlost;
#endif
} ClusterMember;

//data structures for distance vectors

/* sizeof = 6 */
typedef nx_struct {
  /* "indx" here is for efficiency of sender,
   * so that do not need to go through the cluster
   * table to find the matching entry;
   * "source" id is still necessary,
   * because "indx" from the received dv just
   * specifies the index in the SENDER's
   * cluster routing table.
   */
  nx_uint8_t indx;
  nx_uint16_t source;  //source of dv
  nx_uint8_t hopcount; //hop count distance to source
  nx_uint8_t scope;  //scope of dv range
  nx_uint8_t seqno;
  //if having sent_time instead, it takes at least one more byte,
  //even 2 bytes probably are not long enough to represent time
  //uint16_t sent_time;
  //uint8_t quality;
}  DVInfo;

//sizeof = [2 +] MAX_VECTOR_SIZE*sizeof(DVInfo).
typedef nx_struct {
  //do we really need a seqno here?
  //a node will check DVInfo.seqno anyway
  //uint16_t seqno;        //the sequence number of my dv advertisement messages
  DVInfo dv_adv[MAX_VECTOR_SIZE]; //dv advertisements
}  DVMsgData;

//size: 4 + sizeof(DVMsgData).
typedef struct DV_Msg {
  LEHeader header;
  DVMsgData type_data;
} __attribute__ ((packed)) DVMsg;

#ifdef RELIABLE_BCAST
//maintain state of pending broadcast sessions, to achieve reliable broadcast
typedef struct {
  bool occupied;  //whether this entry is occupied
  uint8_t indx; //index in cluster routing table of SENDER!!!
  uint8_t n_rcv;  //have overheard this many neighbors re-broadcast
  uint8_t sendtime; //time of broadcast (in seconds, upper-bounded by timeout)
  uint8_t retries;  //# of retransmissions
} PendingDVs;
#endif

#endif

#ifdef FAILURE_RECOVERY
typedef nx_struct {
  //uint16_t requester_id;  //id of requester (who's trying recovery), optional
  nx_uint16_t dest_id;       //id of the destination
  nx_uint8_t closest_beacon; //id of the beacon closest to destination
  nx_uint16_t cur_next_hop;  //id of current next hop (who's failed, supposedly)
  nx_uint8_t cost;           //old cost through cur_next_hop (necessary?)
  nx_uint8_t cost_type;      //0: dest is within local cluster; 1: otherwise
}  FRReqMsgData;

//size: 4 + sizeof(FRReqMsgData).
typedef nx_struct FRReq_Msg {
  LEHeader header;
  FRReqMsgData type_data;
}  FRReqMsg;

typedef nx_struct {
  nx_uint16_t requester_id;  //id of requester
  nx_uint16_t replier_id;  //id of replier (who's claiming to recover), optional
  nx_uint8_t cost_type;    // 0: cost to closest_beacon; 1: cost to destination
  nx_uint8_t cost;           //new cost through this replier
}  FRRepMsgData;

//size: 4 + sizeof(FRRepMsgData).
typedef nx_struct FRRep_Msg {
  LEHeader header;
  FRRepMsgData type_data;
}  FRRepMsg;
#endif

typedef struct {
	uint16_t addr;
    uint16_t nextHop;
    uint8_t quality;
} NextHopTableEntry;

/***/
#endif  //S4_ROUTING_H

--- NEW FILE: S4Locator.nc ---

includes S4;

/*Interface that a locator may provide, for retrieving the local node's 
  coordinates*/

/* Currently it is not safe in that the application could change the
   buffer, but it saves memory, keeping only one copy of the coordinates.*/


interface S4Locator {
   /** 
    * Provides the coordinates of the local node
    *
    * @param coords Pointer to coordinates
    * @return SUCCESS if coordinates are valid, FAIL otherwise
    */
   command error_t getCoordinates(Coordinates * coords);

   /**
    * Signals when the coordinates have changed, so that the user 
    * is advised to request them again when needed.
    * 
    * @return Should always return SUCCESS
    */
   event error_t statusChanged();
   
  /* Returns my distance to the dest */
  
  
  /*command error_t getDistance(Coordinates * dest, uint16_t * distance);*/  //Qasim Mansoor 1/15/2007
 
}

--- NEW FILE: S4Neighborhood.nc ---

includes S4;
includes nexthopinfo;

interface S4Neighborhood {
  /* This function returns a set of next hop candidates to dest which have
   * distance smaller than min_dist.
   * The next_hop(s) in fallback mode are returned as well
   * The entries in nextHopInfo are ordered:
   *   - by distance
   *   - by quality
   * The fallback entries are located in the same list, after the normal mode
   * entries. 
   * The first fallback entry is TOS_LOCAL_ADDR if and only if we are the closest
   * node to the destination
   */
  
  
  



  #ifdef CRROUTING
  
  command error_t getNextHops( uint16_t dest_addr, uint8_t closest_beacon, uint16_t* next_hop);

  #ifdef FAILURE_RECOVERY
  
  command error_t FR_getNextHops(Coordinates* dest, uint16_t dest_addr, uint16_t min_dist, nextHopInfo* next_hops);

  #endif
  #endif
  
  command am_addr_t getClosestBeaconAddr(uint16_t addr);
  
    command error_t addNextHopEntry(uint16_t addr, uint16_t nextHop);
      
    command error_t removeNextHopEntry(uint16_t addr);
      
    command error_t removeAllNextHopEntries();


  
}


--- NEW FILE: S4RouterC.nc ---

configuration S4RouterC {
  provides {
    interface StdControl;
    interface S4Send;
    interface S4Receive;
  }
  
}
implementation {
  components  S4RouterM   //StdControl provided
            , S4StateC    //StdControl here
            , S4CommStack //StdControl here 
            //, S4CommandC  //StdControl here
#if defined(PLATFORM_MICA2) || defined(PLATFORM_MICA2DOT)
            , CC1000RadioC
#endif
            
            , RandomLfsrC as Random
            , new TimerMilliC() as Timer
            , new TimerMilliC() as FRSendReplyTimer
            , new TimerMilliC() as FRWaitReplyTimer
            , new TimerMilliC() as PeriodicTimer
            , ActiveMessageC      
            , S4ActiveMessageC 
            
            ;

  #ifdef FAILURE_RECOVERY ////////////// Qasim
     ,LinkEstimatorC
  #endif
  
  components S4TopologyC;

  
  //external
  S4Send = S4RouterM;
  S4Receive = S4RouterM;

  //RouteToInterface = S4CommandC;

  //internal
  
  //StdControl = Timer;  //////////// Qasim
  StdControl = S4CommStack;
  StdControl = S4StateC;
  //StdControl = S4CommandC;
  
  StdControl = S4RouterM;
  
  S4RouterM.S4Topology -> S4TopologyC;
  
  S4RouterM.Neighborhood -> S4StateC.S4Neighborhood;
  S4RouterM.Locator -> S4StateC.S4Locator;
  S4RouterM.AMSend -> S4CommStack.AMSend[AM_S4_APP_MSG];
  S4RouterM.Receive -> S4CommStack.Receive[AM_S4_APP_MSG];

  S4RouterM.ForwardDelayTimer -> Timer;
  S4RouterM.Random -> Random;
  S4RouterM.PeriodicTimer -> PeriodicTimer;

  S4RouterM.Packet -> S4ActiveMessageC;
  S4RouterM.AMPacket -> S4ActiveMessageC;
  
  
#ifdef SERIAL_LOGGING  
  components new SerialAMSenderC(AM_SERIALPACKET);
  components new SerialAMSenderC(AM_BEACONSLISTPACKET) as BeaconsListAMSenderC;  
  components SerialActiveMessageC;
  
  S4RouterM.SerialAMSend ->SerialAMSenderC;
  S4RouterM.SerialAMControl -> SerialActiveMessageC;
  S4RouterM.SerialPacketInterface ->SerialAMSenderC;
  
  S4RouterM.BeaconsAMSend -> BeaconsListAMSenderC;  
  S4RouterM.BeaconsPacketInterface -> BeaconsListAMSenderC;
#endif
  
  
  
  #ifdef FAILURE_RECOVERY  ////////////////  Qasim
  
  S4RouterM.FRReqSendMsg->S4CommStack.SendMsg[AM_FRREQ_MSG];
  S4RouterM.FRReqReceiveMsg->S4CommStack.ReceiveMsg[AM_FRREQ_MSG];
  S4RouterM.FRRepSendMsg->S4CommStack.SendMsg[AM_FRREQ_MSG];
  S4RouterM.FRRepReveiveMsg->S4CommStack.ReceiveMsg[AM_FRREQ_MSG];
  S4RouterM.FRSendReplyTimer->FRSendReplyTimer;
  S4RouterM.FRWaitReplyTimer->FRWaitReplyTimer;
  S4RouterM.LinkEstimator->LinkEstimatorC;
  
  
  /////////////////////////////////////////
  #endif
 
  
}

--- NEW FILE: S4RouterM.nc ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: S4RouterM.nc,v 1.1 2008/10/23 22:22:40 genie1 Exp $

/*                                                                      
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
[...1107 lines suppressed...]
      event void SerialAMSend.sendDone(message_t* bufPtr, error_t error) {
        if (bufPtr == &loggingPacket)
          serialPortLocked = FALSE;
      }
    
      event void BeaconsAMSend.sendDone(message_t* bufPtr, error_t error) {
          if (bufPtr == &loggingPacket)
            serialPortLocked = FALSE;
      }
#endif
      
      event void PeriodicTimer.fired() {
          if (call Locator.getCoordinates(&my_coords) == SUCCESS) {          
          }
          else {          
          }          
      }
    
}
      

--- NEW FILE: S4StateC.nc ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: S4StateC.nc,v 1.1 2008/10/23 22:22:40 genie1 Exp $
                                    
/*                                                                      
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.             
 *                                  
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *                                  
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *                                  
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *                                  
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.             
 *                                  
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */                                 
                                    
/*                                  
 * Authors:  Rodrigo Fonseca        
 * Date Last Modified: 2005/05/26
 */

/*                                  
 * Authors:  Feng Wang, Univ. of Texas at Austin, CS Department        
 * Date Last Modified: 2006/02/04
 */

configuration S4StateC {
  provides {
    interface Init;
    interface StdControl;
    interface S4Neighborhood;
    interface S4Locator;
    interface S4StateCommand;
    interface FreezeThaw;
    interface RoutingTable;
  }
}
implementation {
  components S4StateM as S4State 
           , S4CommStack as Comm //assumes StdControl elsewhere
#ifdef FW_COORD_TABLE
           , CoordinateTableC     //StdControl here
#endif
           , LinkEstimatorC       //assumes StdControl elsewhere
           

           
#ifdef PLATFORM_MICA2
           , CC1000RadioC
           , LogicalTime
#endif

           , RandomLfsrC as Random
           , ActiveMessageC
           , LedsC, S4TopologyC;          //assumes StdControl elsewhere
  
  components new TimerMilliC() as BeaconTimer;
  components new TimerMilliC() as BeaconRetransmitTimer;
  components new TimerMilliC() as BeaconUpdateTimer;
  components new TimerMilliC() as CheckLinkTimer;
  components new TimerMilliC() as ClusterTimer;
  components new TimerMilliC() as ClusterRetransmitTimer;
  components new TimerMilliC() as ClusterUpdateTimer;
  
#ifdef TOSSIM
  components new TimerMilliC() as BeaconDisplayTimer;
#endif
  
  //provided
  Init = S4State;
  StdControl = S4State;
  S4Neighborhood = S4State;
  S4Locator = S4State;
  S4StateCommand = S4State;
  FreezeThaw = S4State;

    //added by Yun Mao
    RoutingTable = S4State;

  //wiring
  //  S4StateM
  S4State.BeaconTimer -> BeaconTimer;
  S4State.BeaconRetransmitTimer -> BeaconRetransmitTimer;
  S4State.Random -> Random;
  S4State.AMPacket -> ActiveMessageC;

  //S4State.Logger -> UARTLogger;

#ifdef FW_COORD_TABLE
  S4State.CoordinateTable -> CoordinateTableC.CoordinateTable;
  S4State.CoordinateTableControl -> CoordinateTableC.StdControl;
  
#endif

  S4State.CoordinateTableControlInit -> CoordinateTableC.Init;

  S4State.LinkEstimator -> LinkEstimatorC;
  S4State.LinkEstimatorSynch -> LinkEstimatorC;

  S4State.S4StateAMSend -> Comm.AMSend[AM_S4_BEACON_MSG];
  S4State.S4StateReceive -> Comm.Receive[AM_S4_BEACON_MSG];

  //S4State.UARTLoggerInit -> UARTLogger.Init;


  //added by Feng Wang

  S4State.BeaconUpdateTimer -> BeaconUpdateTimer;
#ifdef CHECK_LINK
  S4State.CheckLinkTimer -> CheckLinkTimer;
#endif

  S4State.Leds -> LedsC;
#ifdef PLATFORM_MICA2
  S4State.CC1000Control -> CC1000RadioC.CC1000Control;
  S4State.Time -> LogicalTime.Time;
#endif

#ifdef CRROUTING

#ifdef LOCAL_DV
  S4State.ClusterAMSend -> Comm.AMSend[AM_DV_MSG];
  S4State.ClusterReceive -> Comm.Receive[AM_DV_MSG];
#endif

  S4State.ClusterTimer -> ClusterTimer;
  S4State.ClusterRetransmitTimer -> ClusterRetransmitTimer;

#ifdef LOCAL_DV
  S4State.ClusterUpdateTimer -> ClusterUpdateTimer;
#endif

#endif
  S4State.Leds -> LedsC;
  
  S4State.S4Topology -> S4TopologyC;
  S4State.S4TopologyInit -> S4TopologyC;

#ifdef TOSSIM
  S4State.BeaconDisplayTimer -> BeaconDisplayTimer;
#endif

}

--- NEW FILE: S4StateCommand.nc ---

includes AM;
includes S4;

interface S4StateCommand {
    /* Set this node's coordinates do the value pointed to by coords
     * The coordinates should be copied, coords must not be assumed safe,
     * and may be changed.
     */
    command error_t setCoordinates(Coordinates * coords);

    /* Get this node's coordinates. coords is made to point to this nodes
     * coords, so that the caller should not change the value pointed to
     * by coords.
     */
    command error_t getCoordinates(Coordinates ** coords);

    /**/
    command error_t startRootBeacon();
    command error_t stopRootBeacon();
    
    #ifdef FW_ROOTBEACON CMD  
    ///////////// *******Qasim
    
    command error_t setRootBeacon(uint8_t n);
    command error_t isRootBeacon(bool *value);
    
    #endif  
    ///////////// ****Qasim

    command error_t getRootInfo(uint8_t n , S4RootBeacon **r);
    command error_t getNumNeighbors(uint8_t *n);


    ////////////// *********** Qasim
     command uint16_t get_routing_state();
     command uint16_t get_sent_bv();
     command uint16_t get_sent_dv();
    
    ////////////////////////////////


}

--- NEW FILE: S4StateM.nc ---


/* This module takes care of maintaining the S4 state
   This includes the beacon flooding, coordinate flooding
   It has both end points for AM messages of type AM_S4_BEACON_MSG
   Differently from previous versions, here there is no separate
   beacon flood: beacons just state that their coordinates are
   0 for themselves, and send the normal coordinate update. 
 ******************
 */

/* Comment on BEACON_ETX:
   BEACON_ETX defines another way of comparing the parent trees, based on ETX.
   ETX is the expected number of transmissions along the path, counting the
retransmissions at each hop. If p_i is the prob. of success at hop i,

   ETX = sum{1/p_i}

   q_i is the bidirectional quality of link i, and 1/pi = 255/qi
[...3404 lines suppressed...]
          return SUCCESS;
        }
        
        command error_t S4Neighborhood.removeAllNextHopEntries() {
          int i=0;
      
          for (i=0; i < N_NODES / MAX_ROOT_BEACONS; i++ ) {      
            nextHopTable[i].addr = INVALID_NODE_ID;      
            nextHopTable[i].quality = 0;
          }
      
          return SUCCESS;
        }


	
	
  
  } // end of implementation
   

--- NEW FILE: S4Topology.nc ---
interface S4Topology{

    command error_t getRootBeaconIDs(uint8_t* array);

    

    command error_t setRootBeaconID(uint16_t pos, uint8_t val);

    

    command uint16_t getRootNodesCount();

    

    command void setRootNodesCount(uint16_t nodesCount);



    command uint8_t* getRootBeaconIDsPtr() ;



}


--- NEW FILE: S4TopologyC.nc ---



configuration S4TopologyC {
  provides {
    interface S4Topology;
    interface Init; 
  }
  
}
implementation {
  components  S4TopologyM   //StdControl provided            
            ;

  S4Topology = S4TopologyM;
  Init = S4TopologyM;
  
}

--- NEW FILE: S4TopologyM.nc ---
module S4TopologyM{
  provides interface S4Topology;
      
  provides interface Init;
}
implementation {

#ifdef TOSSIM
    uint8_t root_beacon_id[N_NODES];
#else
    uint8_t* root_beacon_id;
#endif

    uint8_t n_root_beacons;
    
    command error_t Init.init() {
      int i =0 ;
      n_root_beacons = 0;
  
      dbg("S4-beacon", "S4TopologyM - Init.init\n");
      
#ifdef TOSSIM   

      for (i = 0; i< N_NODES; i++) {
        root_beacon_id[i] = hc_root_beacon_id[i];
        
        if (root_beacon_id[i] != INVALID_BEACON_ID)  {
          n_root_beacons++;
        }
      }
      
      dbg("S4-beacon" , "S4TopologyM; Number of beacons: %d\n", n_root_beacons);
      copy_hc_root_beacon_id[TOS_NODE_ID] = root_beacon_id;      
      
#else

      root_beacon_id = hc_root_beacon_id;
      
#endif

      return SUCCESS;
    }

    command error_t S4Topology.getRootBeaconIDs(uint8_t* array) {
      uint16_t i =0 ;      
    
      for (i = 0; i< N_NODES; i++) {
        array[i] = root_beacon_id[i];
      } 
      
      return SUCCESS; 
    }

    command uint8_t* S4Topology.getRootBeaconIDsPtr() {
      return root_beacon_id;
    }
    
    command error_t S4Topology.setRootBeaconID(uint16_t pos, uint8_t val) {
      if (pos >= N_NODES)
        return FAIL;
     
      root_beacon_id[pos] = val;
      
      return SUCCESS;
    }

    command uint16_t S4Topology.getRootNodesCount() {
      return n_root_beacons;
    }


    command void S4Topology.setRootNodesCount(uint16_t nodesCount){
      n_root_beacons = nodesCount;
    }

}

--- NEW FILE: coordinate_table_entry.h ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: coordinate_table_entry.h,v 1.1 2008/10/23 22:22:40 genie1 Exp $

/*
 * "Copyright (c) 2000-2003 The Regents of the University  of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
 * 94704.  Attention:  Intel License Inquiry.
 */

/*
 * Authors:  Rodrigo Fonseca
 * Date Last Modified: 2005/05/26
 */

#ifndef COORD_TABLE_ENTRY_H
#define COORD_TABLE_ENTRY_H

enum {
  MAX_AGE = 255,
  EXPIRATION_AGE_THRESHOLD = 5,
};

typedef nx_struct {
  nx_uint8_t valid;
  nx_uint16_t first_hop;
  nx_uint8_t last_seqno;
  nx_uint16_t addr;
  Coordinates coords;
  nx_uint8_t quality;           //quality is used when this is a 2-hop neighbor+
  nx_uint8_t age;
  nx_uint8_t pos;               //position of this neighbor in the coordinate table
}  CoordinateTableEntry;

/* + in the case of a one hop neighbor, quality is to be taken directly from
 *   the link estimator. */

inline error_t CTEntryInit(CoordinateTableEntry* e) {
  if (e == NULL)
    return FAIL;

  e->valid = FALSE;
  e->first_hop = 0;
  e->last_seqno = 0;
  e->addr = 0;
  e->quality = 0;
  coordinates_init(&e->coords);
  e->pos = 0;
  return SUCCESS;
}

inline error_t CTEntryTouch(CoordinateTableEntry* e) {
  if (e == NULL)
    return FAIL;
  e->age = 0;
  return SUCCESS;
}
inline error_t CTEntryUpdate(CoordinateTableEntry* e,
		uint8_t last_seqno, uint16_t first_hop, uint8_t quality,
		Coordinates* coords) {
  if (e == NULL || e->valid == FALSE)
    return FAIL;
  e->first_hop = first_hop;
  e->last_seqno = last_seqno;
  e->quality = quality;
  coordinates_copy(coords, &e->coords);
  return SUCCESS;
}

inline error_t CTEntryUpdateFirstHop(CoordinateTableEntry* e, uint16_t first_hop)
{
  if (e == NULL)
    return FAIL;
  e->first_hop = first_hop;
  return SUCCESS;
}

inline error_t CTEntryUpdateCoordinates(CoordinateTableEntry* e, Coordinates* c) {
  if (e == NULL || c == NULL)
    return FAIL;
  return coordinates_copy(c, &e->coords);
}

inline error_t CTEntryUpdateSeqno(CoordinateTableEntry* e, uint16_t seqno) {
  if (e == NULL)
    return FAIL;
  e->last_seqno = seqno;
  return SUCCESS;
}

inline error_t CTEntryUpdateQuality(CoordinateTableEntry* e, uint8_t quality) {
  if (e == NULL)
    return FAIL;
  e->quality = quality;
  return SUCCESS;
}
#endif



--- NEW FILE: coordinates.h ---

// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: coordinates.h,v 1.1 2008/10/23 22:22:40 genie1 Exp $

/*
 * "Copyright (c) 2000-2003 The Regents of the University  of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
 * 94704.  Attention:  Intel License Inquiry.
 */

/*
 * Authors:  Rodrigo Fonseca
 * Date Last Modified: 2005/05/26
 */

/* This file defines coordinates to be a fixed sized array of
 * 8 bit numbers. 255 indicates that a given component is not
 * valid.
 * IMPORTANT: Beware that when changing the number of components, it may be
 * necessary to change the size of message_t*/

#ifndef COORDS_H
#define COORDS_H

#include "util.h"
#include "topology.h"

#ifdef TOSSIM
uint8_t* copy_hc_root_beacon_id[N_NODES];
#endif

/* interface:
These are "exported" from this file. The implementation is subject to change.
 result_t coordinates_init(Coordinates* c)
 result_t coordinates_copy(Coordinates* source, Coordinates* dest)
 uint8_t coordinates_copy_k_closest(Coordinates* source, Coordinates* dest, uint8_t K)
 result_t coordinates_set_component(Coordinates* c, uint8_t pos, uint8_t hopcount)
 uint16_t coordinates_distance(Coordinates* c1, Coordinates* c2)
 uint8_t coordinates_get_closest_beacon(Coordinates* c) {
 void coordinates_print(Coordinates* c)
 bool coordinates_is_valid_component(Coordinates* c, uint8_t pos)
 bool coordinates_has_same_valid_components(Coordinates *q, Coordinates *c) {
 uint8_t coordinates_count_valid_components(Coordinates* c)
*/


#ifndef MAX_ROOT_BEACONS
#define MAX_ROOT_BEACONS 8
#endif

enum {
  INVALID_DISTANCE = 65535u,
  MAX_COORD_DISTANCE = 16384u,
  MAX_COMPONENT_VALUE = 254,
  INVALID_COMPONENT = 255,
};

#ifndef INVALID_NODE_ID
#ifdef TOSSIM
#define INVALID_NODE_ID 65535
#else
#define INVALID_NODE_ID 255
#endif
#endif

typedef nx_struct Coordinates {
  nx_uint8_t comps[MAX_ROOT_BEACONS];
}  Coordinates, *Coordinates_ptr;


/* declaration */
//#ifdef PLATFORM_PC
 void coordinates_print(Coordinates* c);
//#else
//#define coordinates_print(b)
//#endif


error_t coordinates_init(Coordinates* c) {
  int i;
  if (c == NULL)
    return FAIL;
  for (i = 0; i < MAX_ROOT_BEACONS; c->comps[i] = INVALID_COMPONENT, i++);
  return SUCCESS;
}

bool coordinates_is_valid_component(Coordinates* c, uint8_t pos) {
  if (c == NULL || pos >= MAX_ROOT_BEACONS)
    return FALSE;
  return !(c->comps[pos] == INVALID_COMPONENT);
}



uint8_t coordinates_count_valid_components(Coordinates* c) {
  uint8_t i;
  uint8_t count = 0;
  if (c == NULL)
    return 0;
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c, i))
      count++;
  }
  return count;
}

error_t coordinates_copy(Coordinates* source, Coordinates* dest) {
  int i;
  if (source == NULL || dest == NULL)
    return FAIL;
  for (i = 0; i < MAX_ROOT_BEACONS; i++)
    dest->comps[i] = source->comps[i];
  return SUCCESS;
}

/** Get the k smallest components of source and copies them to dest.
 *  If there are less valid components than k in source, the rest are set to
 *  invalid in dest. The return is the number of components copied.
 */
 uint8_t coordinates_copy_k_closest(Coordinates* source, Coordinates* dest, uint8_t K) {
  int i,j;
  uint8_t count = 0;
  uint8_t order[MAX_ROOT_BEACONS];

  if (source == NULL || dest == NULL)  {
    dbg("BVR", "CK: null pointer passed (source:%p, dest:%p)\n",source, dest);
    return 0;
  }

  if (K >= MAX_ROOT_BEACONS) {
    for (i = 0; i < MAX_ROOT_BEACONS; i++) {
      dest->comps[i] = source->comps[i];
      if (source->comps[i] != INVALID_COMPONENT)
        count++;
    }
    return count;
  }
  //insert in order at most k indices into order
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (source->comps[i] != INVALID_COMPONENT) {
      for (j = count ; j > 0; j--) {
        if (source->comps[i] < source->comps[order[j-1]]) {
          if (j < K)  { //shift right the element at j-1
            order[j] = order[j-1];
          //} else {      //replace element at j-1 (do nothing)
          }
        } else
         break;
      }
      if (j < K) {      //insert new element at j
        order[j] = i;
        if (count < K)
          count++;
      //} else {          //ignoring new element (do nothing)
      }
    }
  }
  // now go through order and copy the corresponding entries
  coordinates_init(dest);
  for (i = 0; i < count; i++) {
    dest->comps[order[i]] = source->comps[order[i]];
  }

  dbg("BVR","CK: getting the smallest %d components of ",K);
  coordinates_print(source);
  dbg("BVR","CK: result: ");
  coordinates_print(dest);
  return count;
}

error_t coordinates_set_component(Coordinates* c, uint8_t pos, uint8_t hopcount) {
  dbg("BVR","coordinates_set_component\n");
  if (c == NULL || pos >= MAX_ROOT_BEACONS)
    return FAIL;
  c->comps[pos] = hopcount;
  return SUCCESS;
}

uint8_t coordinates_get_component(Coordinates* c, uint8_t pos) {
  if (c == NULL || pos > MAX_ROOT_BEACONS)
    return INVALID_COMPONENT;
  return c->comps[pos];
}

 uint16_t coordinates_distance_original(Coordinates* c1, Coordinates* c2) {
  uint16_t distance = 0;
  int i,compared;
  compared = 0;
  if (c1 == NULL || c2 == NULL) {
    dbg("BVR","coordinates_distance: %p or %p is null...\n",c1,c2);
    return INVALID_DISTANCE;
  }
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c1,i) &&
        coordinates_is_valid_component(c2,i)) {
      compared++;
      distance += int_abs(c1->comps[i] - c2->comps[i]);
    }
  }
  if (compared > 0)
    return distance;
  else
    return MAX_COORD_DISTANCE;
}


/* Lower bound of the true hop distance between 2 coordinates */
 uint16_t coordinates_distance_L(Coordinates* c1, Coordinates* c2) {
  uint16_t d = 0;
  uint16_t max = 0;
  int i,compared;
  compared = 0;
  if (c1 == NULL || c2 == NULL) {
    dbg("BVR","coordinates_distance: %p or %p is null...\n",c1,c2);
    return INVALID_DISTANCE;
  }
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c1,i) &&
        coordinates_is_valid_component(c2,i)) {
      compared++;
      d = int_abs(c1->comps[i] - c2->comps[i]);
      if (d > max) max = d;
    }
  }
  if (compared > 0)
    return max;
  else
    return MAX_COORD_DISTANCE;
}

/* Upper bound to the true distance between the two coordinates */
 uint16_t coordinates_distance_U(Coordinates* c1, Coordinates* c2) {
  uint16_t d = 0;
  uint16_t min = MAX_COORD_DISTANCE;
  int i,compared;
  compared = 0;
  if (c1 == NULL || c2 == NULL) {
    dbg("BVR","coordinates_distance: %p or %p is null...\n",c1,c2);
    return INVALID_DISTANCE;
  }
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c1,i) &&
        coordinates_is_valid_component(c2,i)) {
      compared++;
      d = c1->comps[i] + c2->comps[i];
      if (d < min) min = d;
    }
  }
  if (compared > 0)
    return min;
  else
    return MAX_COORD_DISTANCE;
}

/* This is used to move towards the beacon which is closest to the destination.
   It is not symmetric: it returns the component of c1 that corresponds to the
   smallest component in c2. If c2 is (10,3), then it returns the second component
   of c1.
   In this case c2 should be the destination for this metric to make sense for
   routing */
uint16_t coordinates_distance_closest(Coordinates *c1, Coordinates *c2) {
  uint8_t i,smallest;
  uint8_t closest_beacon;

  if (c1 == NULL || c2 == NULL) {
    dbg("BVR","coordinates_distance: %p or %p is null...\n",c1,c2);
    return INVALID_DISTANCE;
  }
  //Find the smallest coordinate in c2
  if (coordinates_count_valid_components(c2) == 0)
    return INVALID_DISTANCE;
  smallest = (uint8_t)-1;
  closest_beacon = (uint8_t)-1;
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c2,i)) {
      if (c2->comps[i] < smallest) {
        smallest = c2->comps[i];
        closest_beacon = i;
      }
    }
  }
  if (closest_beacon == (uint8_t)-1)
    return INVALID_DISTANCE;
  if (coordinates_is_valid_component(c1,closest_beacon))
    return c1->comps[closest_beacon];
  else
    return INVALID_DISTANCE;
}

enum {
  COORDS_WEIGHT = 10,
};

/* This is the orginial metric, except that it weights the positive differences
   more than the negative differences. The reasoning is that positive differences
   should be more meaningful in guiding you towards the destination.
   The weight is currently defined at compile time. */

uint16_t coordinates_distance_weighted(Coordinates *c1, Coordinates *c2) {
  uint16_t distance = 0;
  int i,compared;
  uint16_t weight = 1;
  compared = 0;
  if (c1 == NULL || c2 == NULL) {
    dbg("BVR","coordinates_distance: %p or %p is null...\n",c1,c2);
    return INVALID_DISTANCE;
  }
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c1,i) &&
        coordinates_is_valid_component(c2,i)) {
      compared++;
      if (c1->comps[i] > c2->comps[i])  //positive difference
        weight = COORDS_WEIGHT;
      else
        weight = 1;
      distance += weight * int_abs(c1->comps[i] - c2->comps[i]);
    }
  }
  if (compared > 0)
    return distance;
  else
    return MAX_COORD_DISTANCE;
}

enum {
  COORDS_DIST_NORMAL = 0,
  COORDS_DIST_L = 1,
  COORDS_DIST_U = 2,
  COORDS_DIST_UL = 3,
  COORDS_DIST_WEIGHTED = 5,
  COORDS_DIST_BEACON = 6
};

uint16_t coordinates_distance(Coordinates *c1, Coordinates *c2, uint8_t metric) {
  switch(metric) {
    case 0: //normal (original) distance
      return coordinates_distance_original(c1,c2);
    case 1: //L (lower bound, max(|i-j|)
      return coordinates_distance_L(c1,c2);
    case 2: //U (upper bound, min(|i+j|)
      return coordinates_distance_U(c1,c2);
    case 3: //U+L
      return coordinates_distance_U(c1,c2)+coordinates_distance_L(c1,c2);
    case 5: //10:1 weighted original distance
      return coordinates_distance_weighted(c1,c2);
    case 6: //closest beacon
      return coordinates_distance_closest(c1,c2);
  }
  return INVALID_DISTANCE;
}

/* Given coords, returns the beacon that is closest to it, i.e., the
   index of the smallest component */
 uint8_t coordinates_get_closest_beacon(Coordinates* c) {
  uint8_t i;
  uint8_t closest = INVALID_COMPONENT;
  uint8_t closest_distance = INVALID_COMPONENT;
  if (c == NULL)
    return INVALID_COMPONENT;
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    if (coordinates_is_valid_component(c, i)) {
      if (c->comps[i] < closest_distance) {
        closest = i;
        closest_distance = c->comps[i];
      }
    }
  }
  return closest;
}

/* Returns true if the set of valid components of c is a subset of the
 * valid components in q, i.e., if q can route to c */
 bool coordinates_has_same_valid_components(Coordinates *q, Coordinates *c) {
  bool result;
  int i;
  if (c == NULL || q == NULL) {
    dbg("BVR", "COORDS: coordinates_has_same_valid_components called with a NULL coordinate!\n");
    return 0;
  }
  result = 1;
  for (i = 0; i < MAX_ROOT_BEACONS; i++) {
    result &= (!coordinates_is_valid_component(c,i) ||
               (coordinates_is_valid_component(c,i) && coordinates_is_valid_component(q,i)));
  }
  dbg("BVR", "COORDS: q has all components in c?  %s\n",(result)?"yes":"no");
  dbg("BVR", "COORDS: q "); coordinates_print(q);
  dbg("BVR", "COORDS: c "); coordinates_print(c);
  return result;
}


 void coordinates_print(Coordinates* c) {
#ifdef TOSSIM
  int i;
  dbg_clear("BVR-debug","%d",coordinates_is_valid_component(c,0)?(c->comps[0]):-1);
  for (i = 1; i < MAX_ROOT_BEACONS; i++) {
    dbg_clear("BVR-debug",", %d",coordinates_is_valid_component(c,i)?(c->comps[i]):-1);
  }
  dbg_clear("BVR-debug","\n");
#endif
}


void coordinates_print2(char* mode, Coordinates* c) {
#ifdef TOSSIM
  int i;
  dbg_clear("TestBVR","%d",coordinates_is_valid_component(c,0)?(c->comps[0]):-1);
  for (i = 1; i < MAX_ROOT_BEACONS; i++) {
    dbg_clear("TestBVR",", %d",coordinates_is_valid_component(c,i)?(c->comps[i]):-1);
  }
  dbg_clear("TestBVR","\n");
#endif
}

uint16_t search(uint8_t* array, int size, uint8_t val) {
    uint16_t i;

    for (i=0; i<size; i++)
      if (array[i] == val) { return i; }

    return INVALID_NODE_ID;
}

#endif

--- NEW FILE: nexthopinfo.h ---
// ex: set tabstop=2 shiftwidth=2 expandtab syn=c:
// $Id: nexthopinfo.h,v 1.1 2008/10/23 22:22:40 genie1 Exp $

/*
 * "Copyright (c) 2000-2003 The Regents of the University  of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
 * 94704.  Attention:  Intel License Inquiry.
 */

/*
 * Authors:  Rodrigo Fonseca
 * Date Last Modified: 2005/05/26
 */

#ifndef _NEXT_HOP_INFO_H
#define _NEXT_HOP_INFO_H
enum {
  BVR_MAX_NEXT_HOPS = 6,
  BVR_MAX_FALLBACK_NEXT_HOPS = 1,
};


typedef nx_struct {
  nx_uint16_t next_hops[BVR_MAX_NEXT_HOPS + BVR_MAX_FALLBACK_NEXT_HOPS];
  nx_uint16_t  distances[BVR_MAX_NEXT_HOPS];
  nx_uint8_t  n;
  nx_uint8_t  f;
  nx_uint8_t  index;
}  nextHopInfo;

void inline nextHopInfoInit(nextHopInfo *this) {
  this->n = 0;
  this->f = 0;
  this->index = 0;
}

/* Inserts the tuple <addr,distance> into nextHopInfo in the right place.
 * The current implementation will find the right place and shift the rest
 * to the right.
 */
error_t nextHopInfoAddOrdered(nextHopInfo* this, uint16_t addr,
                                  uint16_t distance, uint8_t quality) {
  int i;
  if (this == NULL || this->n == BVR_MAX_NEXT_HOPS) {
    return FAIL;
  }
}

/************************/
/* forwardRoutingBuffer */
/************************/

typedef struct {
  bool busy;
  message_t* msg;
  uint16_t min_dist;     //hold the min(incoming pkt dist, dist at this node)
  nextHopInfo next_hops;

#ifdef CRROUTING
    uint16_t next_hop;
#endif
}  forwardRoutingBuffer;

static inline void forwardRoutingBufferFree(forwardRoutingBuffer *this) {
  this->busy = FALSE;
  nextHopInfoInit(&this->next_hops);
}

static inline void forwardRoutingBufferInit(forwardRoutingBuffer *this, message_t* msg) {
  forwardRoutingBufferFree(this);
  this->msg = msg;
}



#endif

--- NEW FILE: topology-old.h ---
/* Topology file. Ids 1 to 5 are beacons.
 * Parameters: n:256 nodes
 * Author: Rodrigo Fonseca
 */
#ifndef TOPOLOGY_H
#define TOPOLOGY_H
enum {
  N_NODES = 256,
  N_ROOT_BEACONS = 5,
  DIAMETER = 10,
  INVALID_BEACON_ID = 0xff,
};

uint8_t hc_root_beacon_id[N_NODES] = {
  0xff,    0,    1,    2,    3,    4, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff 
};

#endif

--- NEW FILE: topology.h ---
/* This file autogenerated by generate_topology.pl. Do not edit
 * Parameters: n:200 density:12 roots:5 root placement:edges seed:123
 * Author: Rodrigo Fonseca
 */
#ifndef TOPOLOGY_H
#define TOPOLOGY_H

uint16_t TOS_LOCAL_ADDRESS=1;

enum {
  N_NODES = 225,
  N_ROOT_BEACONS = 8,
  DIAMETER = 15,
  INVALID_BEACON_ID = 0xff,
    SQRT_N_NODES = 20,

};

int NUM_DEST_NODES = 5;
am_addr_t destinationNodes[] = {
								   //45, 164, 20 ,150, 47, 156,9, 163, 113, 118,
								   //98,147,43,116,102,89,81,34,179,107
								   //148,97,100,30,149,103,99,24,150,29,136,125,4,168,73,143,139,177,57,159,
								   //33,66,124,17,41, //117,67,88,119,120,11,161,92,55,163,109,23,164,51,35,171,174,68,60,94,149,153,22,93,71,58,123,131,135,91,157,129,70,102,106,25,72,182,82,69,65,21,7,9,50,75,148,181,96,10,155,36,59,165,137,49,127,140,144,130,6,128,13,19,76,
                                    //1, 19, 399
                                   //306,  289, 201, 384, 307,
                                   //327,  386, 369, 342,  366 ,
                                   //287 , 202,267 , 248,367,
                                   //389, 324, 209, 280,300
                                   78, 177, 94, 163, 57, 129, 143, 174, 101, 106,

                                   137,  117, 167, 39,  12,
                                   36, 157, 150, 182, 173,
                                   //158, 32, 153, 171, 172
                                   //400 node grid random
                                   //31,297,52,116,47,3,395,161,121,15,72,235,41,399,368,94,312,46,316,195,198,330,84,311,369,50,299,96,31,322,394,58,137,180,177,383,157,313,220,167,64,148,19,74,293,249,310,395,282,172,251,391,346,11,190,200,398,369,315,141,98,262,105,33,143,71,328,197,194,40,87,126,60,207,301,173,217,26,160,263,
                                   //228,216,331,250,4,226,340,180,101,102,80,110,309,375,260,127,202,153,150,82,140,67,258,191,141,109,200,261,340,50,136,286,358,230,254,119,247,222,196,68,323,285,23,24,368,18,30,131,328,68,20,188,289,308,392,173,375,190,105,238,216,276,76,204,291,331,343,125,38,52,235,383,189,144,229,21,377,236,141,273
                                   // stride select 5 on Motelab
									//1, 36, 71, 106, 143
									//37 ,  46 ,  55 ,  64 ,  72
								    //stride select 10 on Motelab
								    //1, 19, 36, 54, 71, 89, 106, 124, 143, 161
									//stride select 20 on Motelab
									//1 ,  10 ,  19 ,  28 ,  36 ,  45 ,  54 ,  63 ,  71 ,  81 ,  89 ,  98 ,  106 ,  115 ,  124 ,  133 ,  143 ,  152 ,  161 ,  184
									//3 ,  11 ,  20 ,  29 ,  37 ,  46 ,  55 ,  64 ,  72 ,  82 ,  90 ,  99 ,  107 ,  116 ,  125 ,  134 ,  144 ,  153 ,  162 ,  182




                                  };


uint8_t hc_root_beacon_id[N_NODES] = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,    7, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    1, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,    0, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    3, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff,    4, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff,    6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff
};


/*
uint8_t hc_root_beacon_id[N_NODES] = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    1,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    2,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    3,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,    0, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    7,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    6,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    5,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    4,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
*/

//Motelab
/*
uint8_t hc_root_beacon_id[N_NODES] = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,    1, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ,0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff,    0, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff,    2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff,    3, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff
}; 
*/
#endif


--- NEW FILE: topology.sample.testbed.h ---
/* Topology file. Ids 1 to 5 are beacons.
 * Parameters: n:256 nodes
 * Author: Rodrigo Fonseca
 */
#ifndef TOPOLOGY_H
#define TOPOLOGY_H

uint16_t TOS_LOCAL_ADDRESS=1;

enum {
  N_NODES = 256,
  N_ROOT_BEACONS = 5,
  DIAMETER = 10,
  INVALID_BEACON_ID = 0xff,
};

uint8_t hc_root_beacon_id[N_NODES] = {
  0xff,    0,    1,    2,    3,    4, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

#endif



More information about the Tinyos-2-commits mailing list