ybk1996
ybk1996

Reputation: 1

questions about PacketAcknowledgements in nesC,TinyOS

In order to learn how to use the interface PacketAcknowledgements,i try to modify the example in apps/RadioCountToLeds,but there are some problem,the transmit of data can't be done and it seems there is no ack back to the sender.

the file RadioCountToLedsC is as follows:

#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");
    }
  }

}

i want to know where the error is. by the way,can anyone give me an example about how to use the interface PacketAcknowledgements? thanks very much.

the configuration file are as follows:

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;
}

Upvotes: 0

Views: 276

Answers (0)

Related Questions