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

📄 bcastp.nc

📁 tinyos2.0版本驱动
💻 NC
字号:
/*
 * Authors:	zhangjiwen
 * $Revision: 1.0 $
 *
 */

/**
 * @author zhangjiwen
 */

#include "Bcast.h"

module BcastP {
  provides {
    interface Receive;
  }
  uses {
    interface Receive as ReceiveMsg;
    interface AMSend as subSend;
    interface AMPacket;
    interface Packet;
    interface Pool<message_t>;
    interface Queue<message_t *>;
    //interface Leds;
  }
}

implementation {

  int16_t localSeqno = 0;
  bool radiobusy = FALSE;
	uint8_t length = offsetof(TOS_BcastMsg,data);
  /***********************************************************************
   * Internal functions
   ***********************************************************************/

bool newBcast(int16_t proposed) {
    /*	This handles sequence space wrap-around. Overlow/Underflow makes
     * the result below correct ( -, 0, + ) for any a, b in the sequence
     * space. Results:	result	implies
     *			  - 	 a < b
     *			  0 	 a = b
     *			  + 	 a > b
     */
    if ((proposed - localSeqno) > 0) {
      localSeqno++;
      //localSeqno = proposed;
      return TRUE;
    } else {
      return FALSE;
    }
  }
  
 task void FwdBcastSendTask() {
    uint8_t len;
    message_t* msg; 
    am_addr_t addr;	
    TOS_BcastMsg *BcastMsg_buf ;
   if (call Queue.empty()) {
     return;
   }
   else if (!radiobusy) {
   	msg = call Queue.dequeue();
   	len = call Packet.payloadLength(msg);
   	BcastMsg_buf = call Packet.getPayload(msg,len);
    if(BcastMsg_buf->isSource == TRUE) {
    	addr = call AMPacket.source(msg);
    	BcastMsg_buf -> seqno = localSeqno;
      if (call subSend.send(addr, msg, len) == SUCCESS)
   		  radiobusy = TRUE;
   	}
    else if (call subSend.send(AM_BROADCAST_ADDR, msg, len) == SUCCESS){
   		radiobusy = TRUE;
   	}
  }
 }
  event message_t* ReceiveMsg.receive(message_t* msg, void* payload, uint8_t len) {
    TOS_BcastMsg *pBCMsg = (TOS_BcastMsg *)payload;
    uint16_t Len = len - offsetof(TOS_BcastMsg,data);
    message_t* tmp;
    bool isnewBcast = newBcast(pBCMsg->seqno);
    //call Leds.led1Toggle();
    if (isnewBcast || (!isnewBcast && pBCMsg -> isSource == TRUE)) {
      if(isnewBcast){
        signal Receive.receive(msg,&pBCMsg->data[0],Len);
        pBCMsg -> isSource = FALSE;
        //call Leds.led1Toggle();
      }
      if (!call Pool.empty() && call Queue.size() < call Queue.maxSize()) {
        //call Leds.led0Toggle();
    	  tmp = call Pool.get();
        call Queue.enqueue(msg);
        if (!radiobusy) {
          post FwdBcastSendTask();
        }
        return tmp;
      }    	
    }
    else 
      //call Leds.led0Toggle();
    return msg;
  }


  event void subSend.sendDone(message_t *msg, error_t error) {
  	if( error == SUCCESS )
      //call Leds.led1Toggle();
    radiobusy = FALSE;
    call Pool.put(msg);
    if (!call Queue.empty()) {
      post FwdBcastSendTask();
    } 
  }
}



⌨️ 快捷键说明

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