⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 anycastm.nc

📁 主要用于无线传感网络的编写的书籍.对于初学者有着很大的用处
💻 NC
字号:
/* Anycast Implmentation. *//* Written by Kin Sun Ho (ksho@cse) *//* Last Modified: 07 September 2005 *//* History:   15/07/05: Created File   18/07/05: Implmented the handling of JOIN and the routing table   19/07/05: Started Testing, Received empty broadcast from monitor   20/07/05: Fixed the sending of packets from the monitor   21/07/05: Editing the header file (MN.h) for changes to Anycast   22/07/05: Implmented Sensor Data Packets             Done some testing, AnycastM seems to be fine	     UART is not printing the message correctly   25/07/05: Rewritten ACBase from TOSBase, it works now!             Minor Bug fix in AnycastM due to message buffers	     Implemented multihop forwarding   26/07/05: Testing multihop... however, it may not work very well...   27/07/05: Worked on exp1: refer to it.             Wen Hu points out:             1. the routing table may need an atomic to prevent preempt	     2. the renewal of sequence # will result in network hang after a	        while	     => tried to add atomic on point 1 but network performance 	     decrease sharply	     => fixing the second point with force update if seq# <= 10   29/07/05: Done experiments, the maximum hop achieved is 3, although it is             rare. But it is proved that a multihop of 2 would work   05/07/05: Done experiments with Tatiana. Removed ADC from Anycast.             Wen points out table entries are still accessed after invalid             => fixed             added atomic statements within update of table   05/08/05: Modified doData for forwarding packets   07/09/05: Fixed possible synchronization problem in doJoin   LEDS:      green:  deliver packet to parent   yellow: forward packet from child   red:    broadcast from mote/base received*/includes Sasha;module AnycastM{  provides interface StdControl;  uses {    interface Timer as Table_Timer;    interface Timer as Sensor_Timer;    interface Timer as Sleep_Timer;    interface Leds;    interface StdControl as CommControl;    interface ReceiveMsg;    interface SendMsg;  }}implementation{  TOS_Msg msg[2];  // data msg unicast buffer  TOS_Msg jmsg[2]; // join msg rebroadcast buffer  TOS_Msg fmsg[2]; // forwarding msg unicast buffer  uint8_t currentMsg; // select between one and zero  uint8_t jcurrentMsg;  uint8_t fcurrentMsg;  struct Mrouting *rtable[ROUTING_TABLE]; // routing table  bool sleeping;  uint8_t sequences;    uint8_t find_entry();    /**   * Used to initialize this component.   */  command result_t StdControl.init() {        uint8_t i;        call Leds.init();             // initalize Leds    call CommControl.init();      // initalize Radio    sleeping = FALSE;        atomic {    for(i=0; i<ROUTING_TABLE;i++) {      rtable[i] = (struct Mrouting*) malloc(sizeof(struct Mrouting));      rtable[i]->empty = TRUE;      rtable[i]->sinkID = -1;    }    currentMsg = 0;    jcurrentMsg = 0;    fcurrentMsg = 0;    sequences=0;    }    dbg(DBG_BOOT, "Anycast initialized\n");    return SUCCESS;  }    /**   * Starts the SensorControl and CommControl components.   * @return Always returns SUCCESS.   */  command result_t StdControl.start() {    call CommControl.start();    call Table_Timer.start(TIMER_REPEAT, TABLE_TIMER);    call Sensor_Timer.start(TIMER_REPEAT, SENSOR_TIMER);    return SUCCESS;  }    /**   * Stops the SensorControl and CommControl components.   * @return Always returns SUCCESS.   */  command result_t StdControl.stop() {    call CommControl.stop();    call Table_Timer.stop();    return SUCCESS;  }    /**   * Signalled when the clock ticks.   * @return Always returns SUCCESS.   */  event result_t Table_Timer.fired() {    int8_t i;        //check for routing table timeout and time--    for(i=0; i<ROUTING_TABLE; i++) {      atomic {	rtable[i]->timer--;	if(rtable[i]->timer == 0 && rtable[i]->empty == FALSE) {	  rtable[i]->empty = TRUE;	}      }    }        return SUCCESS;  }    /* awake from sleep */  event result_t Sleep_Timer.fired() {    sleeping = FALSE;    call StdControl.start();    call Sleep_Timer.stop();    return SUCCESS;  }  /* request for sensor data */    event result_t Sensor_Timer.fired() {    struct Anycast *sendt;    int8_t use_entry;    atomic {      sendt = (struct Anycast *)msg[currentMsg].data;    }        use_entry = find_entry();    if(use_entry == -1) {      return SUCCESS;    }    atomic {	sendt->type   = DATA;	sendt->seqNum = sequences++;	sendt->src    = TOS_LOCAL_ADDRESS;	sendt->parent = rtable[use_entry]->parent;	sendt->hop    = rtable[use_entry]->hop;	sendt->from    = TOS_LOCAL_ADDRESS;		msg[currentMsg].length = TOSH_DATA_LENGTH;	msg[currentMsg].group  = TOS_AM_GROUP;	msg[currentMsg].addr   = sendt->parent;			  	  	  		/* send the message to the parent */	if (call SendMsg.send(sendt->parent,TOSH_DATA_LENGTH,			      &msg[currentMsg])){	  currentMsg ^= 0x1;	  call Leds.greenToggle();	}    }    return SUCCESS;  }    event result_t SendMsg.sendDone(TOS_MsgPtr sent, result_t success) {    return SUCCESS;  }  /* This will sleep the mote */  void doSleep() {    sleeping = TRUE;    call StdControl.stop();    call Sleep_Timer.start(TIMER_ONE_SHOT, SLEEP_TIMER);  }    /* Operation to be done when the mote receive a Join from the monitor */  void doJoin(TOS_MsgPtr rmsg) {    uint8_t i;    uint8_t t_seq;    struct Anycast *join;    struct Anycast *sendt;    join = (struct Anycast *)rmsg->data;        atomic {      /* check if sink exist */      for(i=0; i<ROUTING_TABLE; i++) {	if(rtable[i]->sinkID == join->src && rtable[i]->empty == FALSE)	  // do not set sinkID = 2^16 = -1	  break;      }      t_seq = 0;      /* if sink does exist */      if(i < ROUTING_TABLE)	t_seq = rtable[i]->seqNum;     }    /* check seq number: maximum 240 */    if(join->seqNum <= t_seq && t_seq < MAX_SEQ && i < ROUTING_TABLE) {      // drop the rmsg      return;    }    /* update the table */    else {      atomic {	rtable[i]->sinkID = join->src;	rtable[i]->parent = join->parent;	rtable[i]->seqNum = join->seqNum;	rtable[i]->hop    = join->hop;	rtable[i]->timer  = TABLE_EXIST;	rtable[i]->empty  = FALSE;      }    }    /* if sink does not exist */    if(i >= ROUTING_TABLE) {      atomic {	/* check if any entry empty */	for(i=0; i<ROUTING_TABLE; i++) {	  if(rtable[i]->empty == TRUE)	    break;	}	/* if there is entry empty */	if(i < ROUTING_TABLE) {	  /* add sink */	  rtable[i]->sinkID = join->src;	  rtable[i]->parent = join->parent;	  rtable[i]->hop  = join->hop;	  rtable[i]->seqNum = join->seqNum;	  rtable[i]->timer  = TABLE_EXIST;	  rtable[i]->empty = FALSE;	}	else {	  uint8_t depth = rtable[0]->hop;	  uint8_t max   = 0;	  	  /* find the entry with max hop count */	  for(i=1; i<ROUTING_TABLE; i++) {	    if(rtable[i]->hop > depth && rtable[i]->empty == FALSE) {	      depth = rtable[i]->hop;	      max = i;		    }	  }	  	  /* replace sink with max hop count */	  rtable[i]->sinkID = join->src;	  rtable[i]->hop    = join->hop;	  rtable[i]->seqNum = join->seqNum;	  rtable[i]->timer  = TABLE_EXIST;	  rtable[i]->parent = join->parent;	  rtable[i]->empty  = FALSE;	}      }    }        atomic {      sendt         = (struct Anycast *)jmsg[jcurrentMsg].data;      sendt->type   = JOIN;      sendt->src    = join->src; // sinkID      sendt->parent = TOS_LOCAL_ADDRESS;      sendt->seqNum = join->seqNum;      sendt->hop    = join->hop+1;      jmsg[jcurrentMsg].length = TOSH_DATA_LENGTH;      jmsg[jcurrentMsg].group  = TOS_AM_GROUP;      jmsg[jcurrentMsg].addr   = TOS_BCAST_ADDR;             /* rebroadcast the message */      if(call SendMsg.send(TOS_BCAST_ADDR,TOSH_DATA_LENGTH,&jmsg[jcurrentMsg]))	{	  jcurrentMsg ^= 0x1;	  call Leds.redToggle();	}	    }  }    /* find a shortest path sink */  uint8_t find_entry() {        uint8_t i,j;    uint8_t min, min_i;    // find first valid entry    for(i=0; i<ROUTING_TABLE; i++) {      if(rtable[i]->empty == FALSE)	break;    }    if(i == ROUTING_TABLE)      return -1;    min = rtable[i]->hop;    min_i = i;    for(j=i; j<ROUTING_TABLE; j++) {      if(rtable[j]->hop < min && rtable[i]->empty == FALSE) {	min = rtable[j]->hop;	min_i = j;	      }    }    return min_i;  }  void doData(TOS_MsgPtr rmsg) {    int8_t i;    int16_t parent;    TOS_MsgPtr fmsgpt;    /*Find the shortes path*/    int8_t use_entry = find_entry();        if(use_entry == -1) {      return; // if there is no route to parent, there is nothing we can do    }    atomic {      parent = rtable[use_entry]->parent;      fmsgpt = &fmsg[fcurrentMsg];      for(i=0; i<TOSH_DATA_LENGTH; i++) {	fmsgpt->data[i] = rmsg->data[i];      }      fmsg[fcurrentMsg].length = TOSH_DATA_LENGTH;      fmsg[fcurrentMsg].group  = TOS_AM_GROUP;      fmsg[fcurrentMsg].addr   = parent;            /* forward the message to the parent */      if (call SendMsg.send(parent,TOSH_DATA_LENGTH,&fmsg[fcurrentMsg])){	fcurrentMsg ^= 0x1;	call Leds.yellowToggle();      }    }  }    event TOS_MsgPtr ReceiveMsg.receive(TOS_MsgPtr rmsg){        switch (rmsg->data[0]) {          case(JOIN):      doJoin(rmsg);      break;    case(DATA):      doData(rmsg);      break;    default:      break;    }    return rmsg;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -