虽然我使用了接口PacketAcknowledgements,但我仍然发现我不能保证一跳内的通信......在我的实验中,我只使用两个mote,每个节点每1秒发送一个数据包给另一个,然后我发现了命令AMsend.send 总是可以成功调用,但事件 Receive.receive并不总是成功发出信号......它们是否相互冲突?如果命令发送可以抢占事件接收,我该怎么办?非常感谢...
RadioCountToLedsC.nc 文件如下:
module RadioCountToLedsC @safe() {
uses {
interface Leds;
interface Boot;
interface Receive;
interface AMSend;
interface Timer<TMilli> as MilliTimer;
interface SplitControl as AMControl;
interface Packet;
interface PacketAcknowledgements;
interface AMPacket;
}
}
implementation {
message_t packet;
bool locked = FALSE;
uint16_t counter = 0;
event void Boot.booted() {
dbg("Boot","Application booted.\n");
call AMControl.start();
}
task void senddatatask()
{
counter++;
if (locked==TRUE) {
return;
}
else {
radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));
if (rcm == NULL) {
return;
}
rcm->counter = counter;
call PacketAcknowledgements.requestAck(&packet);
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) {
locked = TRUE;
}
}
}
event void AMControl.startDone(error_t err) {
if (err == SUCCESS) {
call MilliTimer.startPeriodic(1000);
}
else {
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err) {
// do nothing
}
event void MilliTimer.fired() {
post senddatatask();
}
event message_t* Receive.receive(message_t* bufPtr,
void* payload, uint8_t len) {
if (len != sizeof(radio_count_msg_t)) {return bufPtr;}
else {
radio_count_msg_t* rcm = (radio_count_msg_t*)payload;
dbg("RadioCountToLedsC", "Received packet of counter %u.\n", rcm->counter);
return bufPtr;
}
}
event void AMSend.sendDone(message_t* bufPtr, error_t error) {
if(&packet == bufPtr && call PacketAcknowledgements.wasAcked(bufPtr)==SUCCESS){
dbg("RadioCountToLedsC", "packet %u sent.\n", counter);
locked = FALSE;
}
else
{
dbg("RadioCountToLedsC", "NOT ACKED!\n");
}
//locked = FALSE;
}
}