为了学习如何使用PacketAcknowledgements接口,我尝试修改apps/RadioCountToLeds中的示例,但是出现了一些问题,无法完成数据的传输,并且似乎没有回复给发送者。
文件 RadioCountToLedsC 如下:
#include "Timer.h"
#include "RadioCountToLeds.h"
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();
}
event void AMControl.startDone(error_t err) {
if (err == SUCCESS) {
if(TOS_NODE_ID==0)
{
call MilliTimer.startPeriodic(1000);
}
}
else {
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err) {
// do nothing
}
event void MilliTimer.fired() {
counter++;
dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", 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) {
dbg("RadioCountToLedsC", "RadioCountToLedsC: packet sent.\n", counter);
locked = TRUE;
}
}
}
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);
if (rcm->counter & 0x1) {
call Leds.led0On();
}
else {
call Leds.led0Off();
}
if (rcm->counter & 0x2) {
call Leds.led1On();
}
else {
call Leds.led1Off();
}
if (rcm->counter & 0x4) {
call Leds.led2On();
}
else {
call Leds.led2Off();
}
return bufPtr;
}
}
event void AMSend.sendDone(message_t* bufPtr, error_t error) {
if(call PacketAcknowledgements.wasAcked(bufPtr)){
dbg("RadioCountToLedsC", "ACKED!\n");
locked = FALSE;
}
else
{
dbg("RadioCountToLedsC", "NOT ACKED!\n");
}
}
}
我想知道错误在哪里。顺便说一句,谁能给我一个关于如何使用接口 PacketAcknowledgements的例子?非常感谢。
配置文件如下:
configuration RadioCountToLedsAppC {}
implementation {
components MainC, RadioCountToLedsC as App, LedsC;
components new AMSenderC(AM_RADIO_COUNT_MSG);
components new AMReceiverC(AM_RADIO_COUNT_MSG);
components new TimerMilliC();
components ActiveMessageC;
App.Boot -> MainC.Boot;
App.PacketAcknowledgements ->AMSenderC.Acks;
App.Receive -> AMReceiverC;
App.AMSend -> AMSenderC;
App.AMControl -> ActiveMessageC;
App.Leds -> LedsC;
App.MilliTimer -> TimerMilliC;
App.Packet -> AMSenderC;
App.AMPacket ->ActiveMessageC;
}