[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/wustl/apps/Agilla/components/ContextDiscovery ClusteringI.nc, 1.1, 1.2 NeighborListC.nc, 1.9, 1.10 NeighborListI.nc, 1.3, 1.4 NeighborListM.nc, 1.20, 1.21

Chien-Liang Fok chien-liang at users.sourceforge.net
Wed Apr 5 11:05:15 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/wustl/apps/Agilla/components/ContextDiscovery
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv942/components/ContextDiscovery

Modified Files:
	ClusteringI.nc NeighborListC.nc NeighborListI.nc 
	NeighborListM.nc 
Log Message:


Index: ClusteringI.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/contrib/wustl/apps/Agilla/components/ContextDiscovery/ClusteringI.nc,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ClusteringI.nc	25 Mar 2006 23:27:22 -0000	1.1
--- ClusteringI.nc	5 Apr 2006 18:04:38 -0000	1.2
***************
*** 54,57 ****
--- 54,61 ----
    command result_t isClusterHead();
  
+   /**
+     * Returns SUCCESS if nbr is this nodes cluster head.
+     */
+   //command result_t isNbrClusterHead(uint16_t* nbr);
  
    /**
***************
*** 59,62 ****
     * Returns its own id if it is the cluster head.
     */
!   command uint8_t getClusterHead();
  }
--- 63,74 ----
     * Returns its own id if it is the cluster head.
     */
!   command result_t getClusterHead(uint16_t* chID);
! 
! 
!     /**
!      * Returns the approximate communication range of this node. The communication
!      * range is calculated as the average distance to neighbors.
!      */
!     //command uint8_t getCommRange();
! 
  }

Index: NeighborListC.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/contrib/wustl/apps/Agilla/components/ContextDiscovery/NeighborListC.nc,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** NeighborListC.nc	28 Mar 2006 01:50:53 -0000	1.9
--- NeighborListC.nc	5 Apr 2006 18:04:38 -0000	1.10
***************
*** 99,104 ****
  
    #if DEBUG_CLUSTERING
!   NeighborListM.SendClusterMsg -> Comm.SendMsg[AM_AGILLACLUSTERDEBUGMSG];
!   NeighborListM.RcvClusterMsg -> Comm.ReceiveMsg[AM_AGILLACLUSTERDEBUGMSG];
    #endif
  }
--- 99,104 ----
  
    #if DEBUG_CLUSTERING
!   //NeighborListM.SendClusterMsg -> Comm.SendMsg[AM_AGILLACLUSTERDEBUGMSG];
!   //NeighborListM.RcvClusterMsg -> Comm.ReceiveMsg[AM_AGILLACLUSTERDEBUGMSG];
    #endif
  }

Index: NeighborListI.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/contrib/wustl/apps/Agilla/components/ContextDiscovery/NeighborListI.nc,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** NeighborListI.nc	8 Feb 2006 12:27:37 -0000	1.3
--- NeighborListI.nc	5 Apr 2006 18:04:38 -0000	1.4
***************
*** 2,37 ****
  
  /* Agilla - A middleware for wireless sensor networks.
!  * Copyright (C) 2004, Washington University in Saint Louis 
   * By Chien-Liang Fok.
!  * 
!  * Washington University states that Agilla is free software; 
!  * you can redistribute it and/or modify it under the terms of 
!  * the current version of the GNU Lesser General Public License 
   * as published by the Free Software Foundation.
!  * 
!  * Agilla is distributed in the hope that it will be useful, but 
!  * THERE ARE NO WARRANTIES, WHETHER ORAL OR WRITTEN, EXPRESS OR 
!  * IMPLIED, INCLUDING BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 
   * MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.
   *
!  * YOU UNDERSTAND THAT AGILLA IS PROVIDED "AS IS" FOR WHICH NO 
!  * WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. THERE ARE NO 
!  * WARRANTIES AND NO REPRESENTATION THAT AGILLA IS FREE OF 
!  * INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER 
!  * PROPRIETARY RIGHTS.  THERE ARE NO WARRANTIES THAT SOFTWARE IS 
!  * FREE FROM "BUGS", "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", 
!  * OR OTHER HARMFUL CODE.  
   *
!  * YOU ASSUME THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
!  * ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF 
!  * INFORMATION GENERATED USING SOFTWARE. By using Agilla you agree to 
!  * indemnify, defend, and hold harmless WU, its employees, officers and 
!  * agents from any and all claims, costs, or liabilities, including 
!  * attorneys fees and court costs at both the trial and appellate levels 
!  * for any loss, damage, or injury caused by your actions or actions of 
!  * your officers, servants, agents or third parties acting on behalf or 
!  * under authorization from you, as a result of using Agilla. 
   *
!  * See the GNU Lesser General Public License for more details, which can 
   * be found here: http://www.gnu.org/copyleft/lesser.html
   */
--- 2,37 ----
  
  /* Agilla - A middleware for wireless sensor networks.
!  * Copyright (C) 2004, Washington University in Saint Louis
   * By Chien-Liang Fok.
!  *
!  * Washington University states that Agilla is free software;
!  * you can redistribute it and/or modify it under the terms of
!  * the current version of the GNU Lesser General Public License
   * as published by the Free Software Foundation.
!  *
!  * Agilla is distributed in the hope that it will be useful, but
!  * THERE ARE NO WARRANTIES, WHETHER ORAL OR WRITTEN, EXPRESS OR
!  * IMPLIED, INCLUDING BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
   * MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.
   *
!  * YOU UNDERSTAND THAT AGILLA IS PROVIDED "AS IS" FOR WHICH NO
!  * WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. THERE ARE NO
!  * WARRANTIES AND NO REPRESENTATION THAT AGILLA IS FREE OF
!  * INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER
!  * PROPRIETARY RIGHTS.  THERE ARE NO WARRANTIES THAT SOFTWARE IS
!  * FREE FROM "BUGS", "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS",
!  * OR OTHER HARMFUL CODE.
   *
!  * YOU ASSUME THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR
!  * ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF
!  * INFORMATION GENERATED USING SOFTWARE. By using Agilla you agree to
!  * indemnify, defend, and hold harmless WU, its employees, officers and
!  * agents from any and all claims, costs, or liabilities, including
!  * attorneys fees and court costs at both the trial and appellate levels
!  * for any loss, damage, or injury caused by your actions or actions of
!  * your officers, servants, agents or third parties acting on behalf or
!  * under authorization from you, as a result of using Agilla.
   *
!  * See the GNU Lesser General Public License for more details, which can
   * be found here: http://www.gnu.org/copyleft/lesser.html
   */
***************
*** 60,64 ****
  
    /**
!    * Fetches the address of the specified neighbor. 
     * Returns SUCCESS if such a neighbor exists, and
     * FALSE otherwise.
--- 60,64 ----
  
    /**
!    * Fetches the address of the specified neighbor.
     * Returns SUCCESS if such a neighbor exists, and
     * FALSE otherwise.
***************
*** 72,80 ****
    command result_t getRandomNeighbor(uint16_t *nbr);
  
!   
    #if ENABLE_GRID_ROUTING
      /**
       * Fetches the address of the closest neighbor to which an agent
!      * should be forwarded do.  Saves the results in the location
       * specified by the nbr parameter.
       *
--- 72,80 ----
    command result_t getRandomNeighbor(uint16_t *nbr);
  
! 
    #if ENABLE_GRID_ROUTING
      /**
       * Fetches the address of the closest neighbor to which an agent
!      * should be forwarded to.  Saves the results in the location
       * specified by the nbr parameter.
       *
***************
*** 87,96 ****
     * Retrieves the address of the gateway node, or the node that is closest
     * to the gateway node.  The gateway node is attached directly to the
!    * base station.  The address is tored in the pointer passed as a parameter.
!    * 
     * @param addr A pointer to store the results.
!    * @return The minimum number of hops to the gateway, or NO_GW if no 
     * gateway is known.
     */
!   command uint8_t getGW(uint16_t* addr);  
  }
--- 87,98 ----
     * Retrieves the address of the gateway node, or the node that is closest
     * to the gateway node.  The gateway node is attached directly to the
!    * base station.  The address is stored in the pointer passed as a parameter.
!    *
     * @param addr A pointer to store the results.
!    * @return The minimum number of hops to the gateway, or NO_GW if no
     * gateway is known.
     */
!   command uint8_t getGW(uint16_t* addr);
! 
! 
  }

Index: NeighborListM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/contrib/wustl/apps/Agilla/components/ContextDiscovery/NeighborListM.nc,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** NeighborListM.nc	28 Mar 2006 01:50:53 -0000	1.20
--- NeighborListM.nc	5 Apr 2006 18:04:38 -0000	1.21
***************
*** 43,46 ****
--- 43,47 ----
   *
   * @author Chien-Liang Fok
+  * @author Sangeeta Bhattacharya
   */
  module NeighborListM {
***************
*** 49,53 ****
      interface NeighborListI;
      #if ENABLE_CLUSTERING
! 	interface ClusteringI;
      #endif
    }
--- 50,54 ----
      interface NeighborListI;
      #if ENABLE_CLUSTERING
!     interface ClusteringI;
      #endif
    }
***************
*** 68,73 ****
      #if DEBUG_CLUSTERING
      // msg sent by cluster member to clusterhead
!     interface SendMsg as SendClusterMsg;
!     interface ReceiveMsg as RcvClusterMsg;
      #endif
  
--- 69,74 ----
      #if DEBUG_CLUSTERING
      // msg sent by cluster member to clusterhead
!     //interface SendMsg as SendClusterMsg;
!     //interface ReceiveMsg as RcvClusterMsg;
      #endif
  
***************
*** 102,110 ****
  
      #if ENABLE_CLUSTERING
!     int16_t chId;			// The id of the cluster head of the cluster
!     						// to which the neighbor belongs; if the neighbor
!     						// is a cluster head, then chId = id;
!     uint16_t linkQuality;	// The quality of the link to the neighbor
!     uint16_t energy;		// The residual energy of the neighbor
      #endif
    } Neighbor;
--- 103,112 ----
  
      #if ENABLE_CLUSTERING
!     int16_t chId;           // The id of the cluster head of the cluster
!                             // to which the neighbor belongs; if the neighbor
!                             // is a cluster head, then chId = id;
!     uint16_t linkQuality;   // The quality of the link to the neighbor
!     uint16_t energy;        // The residual energy of the neighbor
!     //uint16_t range;         // The communication range of the neighbor
      #endif
    } Neighbor;
***************
*** 130,141 ****
  
    #if ENABLE_CLUSTERING
!   int16_t chId;				// id of the cluster head of the cluster to which this node belongs
!   tos_time_t initTime;		// initialization time
!   tos_time_t chSelectTime;	// time when this node selected a clusterhead
    void setCH(uint16_t ch_id);
    #endif
  
    #if DEBUG_CLUSTERING
!   void sendClusterMsg(int16_t dest);
    #endif
  
--- 132,143 ----
  
    #if ENABLE_CLUSTERING
!   int16_t chId;             // id of the cluster head of the cluster to which this node belongs
!   tos_time_t initTime;      // initialization time
!   tos_time_t chSelectTime;  // time when this node selected a clusterhead
    void setCH(uint16_t ch_id);
    #endif
  
    #if DEBUG_CLUSTERING
!   //void sendClusterMsg(int16_t dest);
    #endif
  
***************
*** 170,177 ****
        for (i = 0; i < numNbrs; i++) {
          #if ENABLE_CLUSTERING
!         	dbg(DBG_USR1, "\t%i:\tID = %i\thopsToGW = %i\tlqi = %i\ttimestamp = %i\n", i,
!         				nbrs[i].addr, nbrs[i].hopsToGW, nbrs[i].linkQuality, nbrs[i].timeStamp.low32);
          #else
!         	dbg(DBG_USR1, "\t%i:\tID = %i\thopsToGW = %i\n", i, nbrs[i].addr, nbrs[i].hopsToGW);
          #endif
          //dbg(DBG_USR1,"\n");
--- 172,179 ----
        for (i = 0; i < numNbrs; i++) {
          #if ENABLE_CLUSTERING
!             dbg(DBG_USR1, "\t%i:\tID = %i\thopsToGW = %i\tlqi = %i\ttimestamp = %i\n", i,
!                         nbrs[i].addr, nbrs[i].hopsToGW, nbrs[i].linkQuality, nbrs[i].timeStamp.low32);
          #else
!             dbg(DBG_USR1, "\t%i:\tID = %i\thopsToGW = %i\n", i, nbrs[i].addr, nbrs[i].hopsToGW);
          #endif
          //dbg(DBG_USR1,"\n");
***************
*** 235,246 ****
  
        #if ENABLE_CLUSTERING
!       	if (call AddressMgrI.isGW() && chId == -1){
! 	  		setCH(TOS_LOCAL_ADDRESS);
! 	  		#if DEBUG_CLUSTERING
! 	  		call Leds.greenToggle();
! 	  		#endif
! 	  	}
!       	bmsg->chId = chId;
!       	bmsg->energy = 0;
        #endif
  
--- 237,249 ----
  
        #if ENABLE_CLUSTERING
!         if (call AddressMgrI.isGW() && chId == -1){
!             setCH(TOS_LOCAL_ADDRESS);
!             #if DEBUG_CLUSTERING
!             call Leds.greenToggle();
!             #endif
!         }
!         bmsg->chId = chId;
!         bmsg->energy = 0;
!         //bmsg->range = call ClusteringI.getCommRange();
        #endif
  
***************
*** 250,254 ****
  
        #if DEBUG_CLUSTERING
!       	//dbg(DBG_USR1, "NeighborListM: Send Beacon chId=%i, energy=%i\n", bmsg->chId, bmsg->energy);
        #endif
  
--- 253,257 ----
  
        #if DEBUG_CLUSTERING
!         //dbg(DBG_USR1, "NeighborListM: Send Beacon chId=%i, energy=%i, range=%i\n", bmsg->chId, bmsg->energy, bmsg->range);
        #endif
  
***************
*** 309,317 ****
  
            #if ENABLE_CLUSTERING
!           	// if this node is a clusterhead and the neighbor belongs to
!           	// this nodes cluster, remove neighbor as cluster member
!           	if(chId == TOS_LOCAL_ADDRESS && nbrs[i].chId == chId){
!         	  	call CHDir.removeClusterMember(nbrs[i].addr);
! 			}
            #endif
  
--- 312,320 ----
  
            #if ENABLE_CLUSTERING
!             // if this node is a clusterhead and the neighbor belongs to
!             // this nodes cluster, remove neighbor as cluster member
!             if(chId == TOS_LOCAL_ADDRESS && nbrs[i].chId == chId){
!                 call CHDir.removeClusterMember(nbrs[i].addr);
!             }
            #endif
  
***************
*** 346,356 ****
     * This method is copied from MultiHopLQI to adjust the link quality.
     */
    uint16_t adjustLQI(uint8_t val) {
        uint16_t result = (80 - (val - 40));
        result = (((result * result) >> 3) * result) >> 3;
        return result;
!   }
    #endif
  
    /**
     * Whenever a beacon is recieved, timestamp and store it in the
--- 349,361 ----
     * This method is copied from MultiHopLQI to adjust the link quality.
     */
+ /*
    uint16_t adjustLQI(uint8_t val) {
        uint16_t result = (80 - (val - 40));
        result = (((result * result) >> 3) * result) >> 3;
        return result;
!   }*/
    #endif
  
+ 
    /**
     * Whenever a beacon is recieved, timestamp and store it in the
***************
*** 370,374 ****
      uint8_t switchedCH = 0;
      uint8_t numClusterMembers = 0;
! 	#endif
  
  
--- 375,379 ----
      uint8_t switchedCH = 0;
      uint8_t numClusterMembers = 0;
!     #endif
  
  
***************
*** 409,417 ****
  
      } else if(numNbrs >= AGILLA_MAX_NUM_NEIGHBORS){
! 		dbg(DBG_USR1, "NeighborListM: Error! Failed to insert neighbor: neighbor list maximum reached!\n");
! 		return m;
! 	}
  
! 	if (indx != -1){
        // Update the timestamp and number of hops to the base station.
  
--- 414,422 ----
  
      } else if(numNbrs >= AGILLA_MAX_NUM_NEIGHBORS){
!         dbg(DBG_USR1, "NeighborListM: Error! Failed to insert neighbor: neighbor list maximum reached!\n");
!         return m;
!     }
  
!     if (indx != -1){
        // Update the timestamp and number of hops to the base station.
  
***************
*** 420,437 ****
  
        #if ENABLE_CLUSTERING
! 	   	nbrs[indx].chId = bmsg->chId;
! 	   	nbrs[indx].energy = bmsg->energy;
! 	   	nbrs[indx].linkQuality = m->lqi;
! 	   	//nbrs[indx].linkQuality = 0;
! 	   	if(chId == TOS_LOCAL_ADDRESS && bmsg->chId == chId) {
! 			// this node is a clusterhead and
! 			// neighbor belongs to this nodes cluster
! 			call CHDir.addClusterMember(bmsg->id, 0);
! 		}
        #endif
  
        #if DEBUG_CLUSTERING
! 	  	dbg(DBG_USR1, "NeighborListM: neighbor: %i hopsToGW=%i chId=%i, energy=%i, linkQuality=%i\n",
! 	        					bmsg->id, bmsg->hopsToGW, bmsg->chId, bmsg->energy, nbrs[indx].linkQuality);
        #endif
  
--- 425,443 ----
  
        #if ENABLE_CLUSTERING
!         nbrs[indx].chId = bmsg->chId;
!         nbrs[indx].energy = bmsg->energy;
!         nbrs[indx].linkQuality = m->lqi;
!         //nbrs[indx].range = bmsg->range;
! 
!         if(chId == TOS_LOCAL_ADDRESS && bmsg->chId == chId) {
!             // this node is a clusterhead and
!             // neighbor belongs to this nodes cluster
!             call CHDir.addClusterMember(bmsg->id/*, bmsg->range*/);
!         }
        #endif
  
        #if DEBUG_CLUSTERING
!         //dbg(DBG_USR1, "NeighborListM: neighbor: %i hopsToGW=%i chId=%i, energy=%i, linkQuality=%i, range=%i\n",
!         //                        bmsg->id, bmsg->hopsToGW, bmsg->chId, bmsg->energy, nbrs[indx].linkQuality, bmsg->range);
        #endif
  
***************
*** 442,639 ****
      }
      #if NBR_LIST_PRINT_CHANGES || DEBUG_NEIGHBORLIST
! 	 printNbrList();
      #endif
  
      #if ENABLE_CLUSTERING
  
!     	if(bmsg->hopsToGW == NO_GW){
! 			return m;
! 		}
  
!     	// Update clusterhead
!     	// check if the neighbor is a clusterhead
!     	hopsToGW = call NeighborListI.getGW(&addr);
! 		if (!call AddressMgrI.isGW()){
! 				hopsToGW++;	// add one hop to get to the neighbor
! 		}
! 		#if DEBUG_CLUSTERING
! 		dbg(DBG_USR1, "NeighborListM: hopsToGW=%i\n", hopsToGW);
! 		#endif
!     	if(chId == -1){
! 			// cluster head not yet set
! 			// Determine the number of hops to the base station
  
! 			if (!call AddressMgrI.isGW() && hopsToGW != NO_GW){
! 				if(hopsToGW % 2 == 0){
! 				   	setCH(TOS_LOCAL_ADDRESS);
! 				}
! 			}
  
!     		if(hopsToGW != 0 && bmsg->id == bmsg->chId){
! 				// neighbor is a clusterhead
! 				setCH(bmsg->id);
! 			}
  
! 			// if the node has not heard from a clusterhead in a long time
! 			// it should become a clusterhead
! 			if((now.low32 - initTime.low32) > 5*(BEACON_PERIOD+BEACON_RAND)){
! 				setCH(TOS_LOCAL_ADDRESS);
! 			}
  
! 			//for DEBUGING /////////////////
! 			#if DEBUG_CLUSTERING
! 			if(chId == TOS_LOCAL_ADDRESS){
! 				call Leds.greenToggle();
! 			} else if(chId != -1){
! 				sendClusterMsg(chId);
! 			}
! 			if(chId != -1) dbg(DBG_USR1, "NeighborListM: [%i] clusterhead set to %i\n", now.low32, chId);
! 			#endif
! 			/////////////////////////////////
! 		} else if(bmsg->id == bmsg->chId){
! 			// neighbor is a clusterhead
  
! 			if(chId != TOS_LOCAL_ADDRESS){
! 				// This node is not a cluster head
! 				// Check if it should join some other cluster
! 				// The node should change its clusterhead if it has
! 				// not heard from its current cluster head in time T
! 				// OR if the difference in the link quality of the
! 				// node sending the beacon is more than a threshold
  
! 				i = 0;
! 				while (i < numNbrs && pos == -1) {
! 				      if (nbrs[i].addr == chId)
! 				        pos = i;
! 				      i++;
!     			}
  
  
!     			if(pos == -1 ||	((now.low32 - nbrs[pos].timeStamp.low32 ) > 3*(BEACON_PERIOD+BEACON_RAND)) ||
!     						((m->lqi - nbrs[pos].linkQuality) > 10)){
! 					// if clusterhead entry not found
! 					// or if not heard from clusterhead for a while
! 					// or link quality of neighbor is much better
! 					// than link quality of current cluster head
! 					// set neighbor as cluster head
! 					setCH(bmsg->id);
! 					switchedCH = 1;
! 				}
  
! 			} else {
! 				// this node is a clusterhead
! 				if(!call AddressMgrI.isGW()){
! 					//this node is not the GW
! 					if(hopsToGW == 1){
! 						setCH(addr);
! 						switchedCH = 1;
! 						#if DEBUG_CLUSTERING
! 						call Leds.greenToggle();
! 						#endif
! 					} else {
! 						// if after a time period, I see that I don't have any cluster members
! 						// I should stop being a clusterhead and join the neighbor that is one
! 						i = 0;
! 						while (i < numNbrs ) {
! 						  if (nbrs[i].chId == TOS_LOCAL_ADDRESS)
! 							numClusterMembers++;
! 						  i++;
!     					}
! 						if(numClusterMembers == 0 &&
! 								(now.low32 - chSelectTime.low32) > 3*(BEACON_PERIOD+BEACON_RAND)){
  
! 							// check if there is a clusterhead closer to the GW than
! 							// the node sending the beacon msg
! 							// NOTE: not safe to do this; may cause race conditions
! 							//minHops = bmsg->hopsToGW;
! 							//nbrId = bmsg->id;
! 							//i = 0;
! 							//while (i < numNbrs ) {
! 							//  if (nbrs[i].addr == nbrs[i].chId && nbrs[i].hopsToGW < minHops){
! 							//	nbrId = nbrs[i].addr;
! 							//	minHops = nbrs[i].hopsToGW;
! 						  	//  }
! 							//  i++;
!     						//}
!     						setCH(bmsg->id);
! 							switchedCH = 1;
! 							#if DEBUG_CLUSTERING
! 							call Leds.greenToggle();
! 							#endif
! 						}
! 					}
! 				}
! 			}
! 		} else if(bmsg->chId != bmsg->id){
! 			if(bmsg->id == chId){
! 				// it should check if its clusterhead is still a cluster head
! 				// if not, then it should switch clusterhead
! 				// find a neighbor that is a clusterhead and from whom
! 				// this node has heard from recently, and join its cluster
! 				// if no such clusterhead is found, set itself as clusterhead
! 				// NOTE: This may be unsafe and lead to race conditions
  
! 				nbrId = TOS_LOCAL_ADDRESS;
! 				i = 0;
! 				while (i < numNbrs && nbrId != TOS_LOCAL_ADDRESS) {
! 				     if (nbrs[i].addr == nbrs[i].chId &&
! 				     	((now.low32 - nbrs[i].timeStamp.low32 ) <= 2*(BEACON_PERIOD+BEACON_RAND)))
! 				        nbrId = nbrs[i].addr;
! 				     i++;
! 				}
! 				setCH(nbrId);
! 				switchedCH = 1;
! 				#if DEBUG_CLUSTERING
! 				if(nbrId == TOS_LOCAL_ADDRESS) call Leds.greenToggle();
! 				printNbrList();
! 				#endif
! 			} else if(chId != TOS_LOCAL_ADDRESS){
! 				// this node is not a clusterhead
! 				// and receives a neighbor msg from another node that is not a clusterhead
! 				// check if clusterhead is alive
! 				// if not, switch clusterhead
! 				i = 0;
! 				pos = -1;
! 				while (i < numNbrs && pos == -1) {
! 				      if (nbrs[i].addr == chId)
! 					        pos = i;
! 				      i++;
! 				}
  
  
! 				if(pos == -1 ||	((now.low32 - nbrs[pos].timeStamp.low32 ) > 3*(BEACON_PERIOD+BEACON_RAND))){
! 					// if clusterhead entry not found
! 					// or if not heard from clusterhead for a while
! 					// find a neighbor that is a clusterhead and from whom
! 					// this node has heard from recently, and join its cluster
! 					// if no such clusterhead is found, set itself as clusterhead
! 					nbrId = TOS_LOCAL_ADDRESS;
! 					while (i < numNbrs && nbrId != TOS_LOCAL_ADDRESS) {
! 					     if (nbrs[i].addr == nbrs[i].chId &&
! 						     	((now.low32 - nbrs[i].timeStamp.low32 ) <= 2*(BEACON_PERIOD+BEACON_RAND)))
! 					        nbrId = nbrs[i].addr;
! 					     i++;
! 					}
! 					setCH(nbrId);
! 					switchedCH = 1;
! 					#if DEBUG_CLUSTERING
! 					if(nbrId == TOS_LOCAL_ADDRESS) call Leds.greenToggle();
! 					printNbrList();
! 					#endif
! 				}
! 			}
! 		}
! 		//for DEBUGGING/////////////////
! 		#if DEBUG_CLUSTERING
! 		if(switchedCH){
! 			sendClusterMsg(chId);
! 			dbg(DBG_USR1, "NeighborListM: switching clusterhead to %i\n", chId);
! 		}
! 		#endif
! 		///////////////////////////
  
! 		#if DEBUG_CLUSTERING
! 			//dbg(DBG_USR1, "NeighborListM: current cluster head is %i\n", chId);
! 		#endif
      #endif
  
--- 448,645 ----
      }
      #if NBR_LIST_PRINT_CHANGES || DEBUG_NEIGHBORLIST
!      printNbrList();
      #endif
  
      #if ENABLE_CLUSTERING
  
!         if(bmsg->hopsToGW == NO_GW){
!             return m;
!         }
  
!         // Update clusterhead
!         // check if the neighbor is a clusterhead
!         hopsToGW = call NeighborListI.getGW(&addr);
!         if (!call AddressMgrI.isGW()){
!                 hopsToGW++; // add one hop to get to the neighbor
!         }
!         #if DEBUG_CLUSTERING
!         //dbg(DBG_USR1, "NeighborListM: hopsToGW=%i\n", hopsToGW);
!         #endif
!         if(chId == -1){
!             // cluster head not yet set
!             // Determine the number of hops to the base station
  
!             if (!call AddressMgrI.isGW() && hopsToGW != NO_GW){
!                 if(hopsToGW % 2 == 0){
!                     setCH(TOS_LOCAL_ADDRESS);
!                 }
!             }
  
!             if(hopsToGW != 0 && bmsg->id == bmsg->chId){
!                 // neighbor is a clusterhead
!                 setCH(bmsg->id);
!             }
  
!             // if the node has not heard from a clusterhead in a long time
!             // it should become a clusterhead
!             if((now.low32 - initTime.low32) > 5*(BEACON_PERIOD+BEACON_RAND)){
!                 setCH(TOS_LOCAL_ADDRESS);
!             }
  
!             //for DEBUGING /////////////////
!             #if DEBUG_CLUSTERING
!             if(chId == TOS_LOCAL_ADDRESS){
!                 call Leds.greenToggle();
!             } else if(chId != -1){
!                 //sendClusterMsg(chId);
!             }
!             if(chId != -1) dbg(DBG_USR1, "NeighborListM: [%i] clusterhead set to %i\n", now.low32, chId);
!             #endif
!             /////////////////////////////////
!         } else if(bmsg->id == bmsg->chId){
!             // neighbor is a clusterhead
  
!             if(chId != TOS_LOCAL_ADDRESS){
!                 // This node is not a cluster head
!                 // Check if it should join some other cluster
!                 // The node should change its clusterhead if it has
!                 // not heard from its current cluster head in time T
!                 // OR if the difference in the link quality of the
!                 // node sending the beacon is more than a threshold
  
!                 i = 0;
!                 while (i < numNbrs && pos == -1) {
!                       if (nbrs[i].addr == chId)
!                         pos = i;
!                       i++;
!                 }
  
  
!                 if(pos == -1 || ((now.low32 - nbrs[pos].timeStamp.low32 ) > 3*(BEACON_PERIOD+BEACON_RAND)) ||
!                             ((m->lqi - nbrs[pos].linkQuality) > 10)){
!                     // if clusterhead entry not found
!                     // or if not heard from clusterhead for a while
!                     // or link quality of neighbor is much better
!                     // than link quality of current cluster head
!                     // set neighbor as cluster head
!                     setCH(bmsg->id);
!                     switchedCH = 1;
!                 }
  
!             } else {
!                 // this node is a clusterhead
!                 if(!call AddressMgrI.isGW()){
!                     //this node is not the GW
!                     if(hopsToGW == 1){
!                         setCH(addr);
!                         switchedCH = 1;
!                         #if DEBUG_CLUSTERING
!                         call Leds.greenToggle();
!                         #endif
!                     } else {
!                         // if after a time period, I see that I don't have any cluster members
!                         // I should stop being a clusterhead and join the neighbor that is one
!                         i = 0;
!                         while (i < numNbrs ) {
!                           if (nbrs[i].chId == TOS_LOCAL_ADDRESS)
!                             numClusterMembers++;
!                           i++;
!                         }
!                         if(numClusterMembers == 0 &&
!                                 (now.low32 - chSelectTime.low32) > 3*(BEACON_PERIOD+BEACON_RAND)){
  
!                             // check if there is a clusterhead closer to the GW than
!                             // the node sending the beacon msg
!                             // NOTE: not safe to do this; may cause race conditions
!                             //minHops = bmsg->hopsToGW;
!                             //nbrId = bmsg->id;
!                             //i = 0;
!                             //while (i < numNbrs ) {
!                             //  if (nbrs[i].addr == nbrs[i].chId && nbrs[i].hopsToGW < minHops){
!                             //  nbrId = nbrs[i].addr;
!                             //  minHops = nbrs[i].hopsToGW;
!                             //  }
!                             //  i++;
!                             //}
!                             setCH(bmsg->id);
!                             switchedCH = 1;
!                             #if DEBUG_CLUSTERING
!                             call Leds.greenToggle();
!                             #endif
!                         }
!                     }
!                 }
!             }
!         } else if(bmsg->chId != bmsg->id){
!             if(bmsg->id == chId){
!                 // it should check if its clusterhead is still a cluster head
!                 // if not, then it should switch clusterhead
!                 // find a neighbor that is a clusterhead and from whom
!                 // this node has heard from recently, and join its cluster
!                 // if no such clusterhead is found, set itself as clusterhead
!                 // NOTE: This may be unsafe and lead to race conditions
  
!                 nbrId = TOS_LOCAL_ADDRESS;
!                 i = 0;
!                 while (i < numNbrs && nbrId != TOS_LOCAL_ADDRESS) {
!                      if (nbrs[i].addr == nbrs[i].chId &&
!                         ((now.low32 - nbrs[i].timeStamp.low32 ) <= 2*(BEACON_PERIOD+BEACON_RAND)))
!                         nbrId = nbrs[i].addr;
!                      i++;
!                 }
!                 setCH(nbrId);
!                 switchedCH = 1;
!                 #if DEBUG_CLUSTERING
!                     if(nbrId == TOS_LOCAL_ADDRESS) call Leds.greenToggle();
!                     //printNbrList();
!                 #endif
!             } else if(chId != TOS_LOCAL_ADDRESS){
!                 // this node is not a clusterhead
!                 // and receives a neighbor msg from another node that is not a clusterhead
!                 // check if clusterhead is alive
!                 // if not, switch clusterhead
!                 i = 0;
!                 pos = -1;
!                 while (i < numNbrs && pos == -1) {
!                       if (nbrs[i].addr == chId)
!                             pos = i;
!                       i++;
!                 }
  
  
!                 if(pos == -1 || ((now.low32 - nbrs[pos].timeStamp.low32 ) > 3*(BEACON_PERIOD+BEACON_RAND))){
!                     // if clusterhead entry not found
!                     // or if not heard from clusterhead for a while
!                     // find a neighbor that is a clusterhead and from whom
!                     // this node has heard from recently, and join its cluster
!                     // if no such clusterhead is found, set itself as clusterhead
!                     nbrId = TOS_LOCAL_ADDRESS;
!                     while (i < numNbrs && nbrId != TOS_LOCAL_ADDRESS) {
!                          if (nbrs[i].addr == nbrs[i].chId &&
!                                 ((now.low32 - nbrs[i].timeStamp.low32 ) <= 2*(BEACON_PERIOD+BEACON_RAND)))
!                             nbrId = nbrs[i].addr;
!                          i++;
!                     }
!                     setCH(nbrId);
!                     switchedCH = 1;
!                     #if DEBUG_CLUSTERING
!                         if(nbrId == TOS_LOCAL_ADDRESS) call Leds.greenToggle();
!                         //printNbrList();
!                     #endif
!                 }
!             }
!         }
!         //for DEBUGGING/////////////////
!         #if DEBUG_CLUSTERING
!         if(switchedCH){
!             //sendClusterMsg(chId);
!             dbg(DBG_USR1, "NeighborListM: switching clusterhead to %i\n", chId);
!         }
!         #endif
!         ///////////////////////////
  
!         #if DEBUG_CLUSTERING
!             //dbg(DBG_USR1, "NeighborListM: current cluster head is %i\n", chId);
!         #endif
      #endif
  
***************
*** 706,709 ****
--- 712,719 ----
    command uint8_t NeighborListI.getGW(uint16_t* addr)
    {
+     #if DEBUG_CLUSTERING
+         //dbg(DBG_USR1, "NeighborListI.getGW():\n");
+         //printNbrList();
+     #endif
      if (call AddressMgrI.isGW())
      {
***************
*** 962,997 ****
  
    command result_t ClusteringI.isClusterHead(){
! 	  if(chId == TOS_LOCAL_ADDRESS)
! 	  	return SUCCESS;
! 	  else
! 	  	return FAIL;
    }
  
!   command uint8_t ClusteringI.getClusterHead(){
! 	  return chId;
    }
  
    void setCH(uint16_t ch_id){
! 	  //if(!call AddressMgrI.isGW()){
! 	  	if(chId == TOS_LOCAL_ADDRESS){
! 		  // this node is giving up being a clusterhead
! 		  call CHDirControl.stop();
! 	  	}
! 	  	if(ch_id == TOS_LOCAL_ADDRESS){
! 		  // this node is a new clusterhead
! 		  call CHDirControl.start();
! 	  	}
!   	  //}
!   	  chId = ch_id;
!   	  chSelectTime = call Time.get();
    }
  
    #endif
  
  
!   #if DEBUG_CLUSTERING
  
    event result_t SendClusterMsg.sendDone(TOS_MsgPtr m, result_t success)
!     {
        call MessageBufferI.freeMsg(m);
        return SUCCESS;
--- 972,1027 ----
  
    command result_t ClusteringI.isClusterHead(){
!       if(chId == TOS_LOCAL_ADDRESS)
!         return SUCCESS;
!       else
!         return FAIL;
    }
  
! /*
!   command result_t ClusteringI.isNbrClusterHead(uint16_t* nbr){
!       if(chId == *nbr)
!         return SUCCESS;
!       else
!         return FAIL;
!   }
! */
! 
!   command result_t ClusteringI.getClusterHead(uint16_t* chID){
!       *chID = chId;
!       if(chId == -1 || (chId != TOS_LOCAL_ADDRESS && !call NeighborListI.isNeighbor(chId)))
!       {
!            dbg(DBG_USR1, "NeighborListM: getClusterHead(): ERROR: Cluster head not set or unreachable.\n");
!            return FAIL;
!       }
!       return SUCCESS;
    }
  
    void setCH(uint16_t ch_id){
!       //if(!call AddressMgrI.isGW()){
!         if(chId == TOS_LOCAL_ADDRESS){
!           // this node is giving up being a clusterhead
!           call CHDirControl.stop();
!         }
!         if(ch_id == TOS_LOCAL_ADDRESS){
!           // this node is a new clusterhead
!           call CHDirControl.start();
!         }
!       //}
!       chId = ch_id;
!       chSelectTime = call Time.get();
    }
  
+ /*
+    command uint8_t ClusteringI.getCommRange(){
+        return 1;
+    }
+ */
    #endif
  
  
! /*  #if DEBUG_CLUSTERING
  
    event result_t SendClusterMsg.sendDone(TOS_MsgPtr m, result_t success)
!   {
        call MessageBufferI.freeMsg(m);
        return SUCCESS;
***************
*** 999,1030 ****
  
    event TOS_MsgPtr RcvClusterMsg.receive(TOS_MsgPtr m) {
! 	  AgillaClusterDebugMsg* cmsg = (AgillaClusterDebugMsg *)m->data;
! 	  #if DEBUG_CLUSTERING
! 	  dbg(DBG_USR1, "NeighborListM: received cluster msg at time(%i) from addr(%i)\n", m->time, cmsg->dummy);
! 	  #endif
! 	  return m;
    }
  
    void sendClusterMsg(int16_t dest){
! 	  TOS_MsgPtr msg;
! 	  if(dest == -1) return;
! 	  msg = call MessageBufferI.getMsg();
! 	  if (msg != NULL)
! 	  {
! 		AgillaClusterDebugMsg* cmsg = (AgillaClusterDebugMsg *)msg->data;
! 		cmsg->dummy = TOS_LOCAL_ADDRESS;
! 		#if DEBUG_CLUSTERING
! 		dbg(DBG_USR1, "NeighborListM: sending cluster msg to %i\n", dest);
! 		#endif
  
! 		if (!call SendClusterMsg.send(dest, sizeof(AgillaClusterDebugMsg), msg))
! 		{
! 		  dbg(DBG_USR1, "NeighborListM: ERROR: Unable to send cluster msg.\n");
! 		  call MessageBufferI.freeMsg(msg);
! 		}
! 	  }
    }
  
!   #endif
  
  }
--- 1029,1058 ----
  
    event TOS_MsgPtr RcvClusterMsg.receive(TOS_MsgPtr m) {
!       AgillaClusterDebugMsg* cmsg = (AgillaClusterDebugMsg *)m->data;
!       dbg(DBG_USR1, "NeighborListM: received cluster msg at time(%i) from addr(%i)\n", m->time, cmsg->dummy);
!       return m;
    }
  
+ 
    void sendClusterMsg(int16_t dest){
!       TOS_MsgPtr msg;
!       if(dest == -1) return;
!       msg = call MessageBufferI.getMsg();
!       if (msg != NULL)
!       {
!         AgillaClusterDebugMsg* cmsg = (AgillaClusterDebugMsg *)msg->data;
!         cmsg->dummy = TOS_LOCAL_ADDRESS;
!         dbg(DBG_USR1, "NeighborListM: sending cluster msg to %i\n", dest);
  
!         if (!call SendClusterMsg.send(dest, sizeof(AgillaClusterDebugMsg), msg))
!         {
!           dbg(DBG_USR1, "NeighborListM: ERROR: Unable to send cluster msg.\n");
!           call MessageBufferI.freeMsg(msg);
!         }
!       }
    }
  
! 
!   #endif    */
  
  }



More information about the Tinyos-contrib-commits mailing list