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

📄 bcastsenderp.nc

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

/**
 * @author zhangjiwen
 */
#include "Bcast.h"
module BcastSenderP {
  provides {
    //interface AMSend;
    interface Send;
  }
  uses {
    interface AMSend as subSend;
    interface Packet;
    interface AMPacket;
    interface Receive;
  }
}

implementation {
	int16_t BcastSeqno = 0;
	//TOS_BcastMsg buf;
	//message_t msg_sendbuf;
	message_t msg_receivebuf;
	uint8_t length = offsetof(TOS_BcastMsg,data);
  command error_t Send.send(message_t* msg, uint8_t len){
  	TOS_BcastMsg* temp;
    if(len > call Send.maxPayloadLength())
      return FAIL;
    BcastSeqno ++ ;
    /*
    buf.seqno = BcastSeqno ;
    buf.isSource = TRUE;
    memcpy(buf.data ,call Packet.getPayload(msg ,len),len);
    memcpy(call Packet.getPayload(&msg_sendbuf, len +length),&buf, len + length);
    */
    temp = (TOS_BcastMsg *)call Packet.getPayload(msg,len);
    temp -> isSource = TRUE;
    temp -> seqno = BcastSeqno;
    return call subSend.send(AM_BROADCAST_ADDR,msg ,len + length);
    //return call subSend.send(AM_BROADCAST_ADDR,&msg_sendbuf ,len + length);
    }
  command error_t Send.cancel(message_t* msg){
    return call subSend.cancel(msg);
  	}
  event void subSend.sendDone(message_t* msg, error_t error){
    if(error != SUCCESS)
      BcastSeqno --;
    signal Send.sendDone(msg, error);
  	}
  command uint8_t Send.maxPayloadLength(){
    return (call subSend. maxPayloadLength()-length);
  	}
  command void* Send.getPayload(message_t* msg, uint8_t len){
    return (call subSend.getPayload(msg,len+length) + length); 
    //call subSend.getPayload(msg,len);
  	}
  	
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 - BcastSeqno) > 0) {
      //BcastSeqno++;
      BcastSeqno = proposed;
      return TRUE;
    } else {
      return FALSE;
    }
  }
  event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) {
    TOS_BcastMsg *pBCMsg = (TOS_BcastMsg *)payload;
    //call Leds.led1Toggle();
    if (newBcast(pBCMsg->seqno)) {
    	memcpy( call Packet.getPayload(&msg_receivebuf ,len - length),pBCMsg->data,len-length);
      call Send.send(&msg_receivebuf ,len-length);
    }
    return msg;
  }
}

⌨️ 快捷键说明

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