Hello!

I am trying to run some tests on tmotes using tinyos 2. As it's uneasy
to re-program a whole set of motes by plugging/unplugging from usb, i
started learning TOSSIM.

Now, I am a beginner at this, I've only read the tutorial (parts) and
ran some basic tests on the motes.

I'm using 4 motes: 1 is an initiator that makes the others start an
activity. The program assigns initiator title to mote with id 0, right
at boot (boot event).
The initiator just sends a special packet to everyone else. 

To achieve this I start the radio, and when that is done (startDone),
the node will only send an initiator packet if it is the initiator. So
the startDone event looks like this:

event void SplitControl.startDone(error_t err){
        dbg("Boot", "Radio started\n");
        if (err == SUCCESS){
                if (iAmInitiator){
                        MSPacket *initPacket = (MSPacket *)
                                (call Packet.getPayload(&radio_packet, NULL));
                buildInitPacket(initPacket);
                if (SEND == SUCCESS){
                        dbg("MS", "Packet sent to radio\n");
                        radioBusy = TRUE;
                }
                call Leds.led1On();
        }
        }else{
                dbg("Boot", "Unable to start radio\n");
                call SplitControl.start();
        }
}

The only problem is that the sendDone event is never called.

I have tried changing the if (iAmInitiator) to if (TRUE) and it works.
So this means that it doesn't work when the node can have different
behavior depending on a state variable. 

Is this true, or is there something in my code? Here's all of it. (the
module part).

Best regards,
Victor Cionca

#include "MSH.h"

#define SEND (call AMSend.send(AM_BROADCAST_ADDR, &radio_packet,
sizeof(MSPacket)))
#define SEND_TO(X) (call AMSend.send((X), &radio_packet,
sizeof(MSPacket)))

module MSModC{
        uses interface Boot;

        uses interface Packet;
        uses interface AMPacket;
        uses interface AMSend;
        uses interface Receive;
        uses interface SplitControl;

        uses interface Leds;

        //uses interface RadioBackoff;
}

implementation{
        bool numberSet;
        uint8_t number;
        bool iAmInitiator;
        message_t radio_packet;
        bool radioBusy = FALSE;

        void buildInitPacket(MSPacket*);
        void buildBeaconPacket(MSPacket *packet, bool isMaster);
        void buildCorrectionPacket(MSPacket *packet, uint8_t number_t);
        
        event void Boot.booted(){
                if (TOS_NODE_ID == 0) iAmInitiator = TRUE;
                else iAmInitiator = FALSE;
                numberSet = FALSE;
                number = 1;

                call Leds.led0Off();
                call Leds.led1Off();
                call Leds.led2Off();

                //start radio
                call SplitControl.start();

                dbg("Boot", "Booted as node %d role = %s\n", TOS_NODE_ID,
((iAmInitiator)?"initiator":"regular"));
        }

        void send_init_packet(){
                if (iAmInitiator){
                        MSPacket *initPacket = (MSPacket *)
                                (call Packet.getPayload(&radio_packet, NULL));
                        buildInitPacket(initPacket);
                        if (SEND == SUCCESS){
                                dbg("MS", "Packet sent to radio\n");
                                radioBusy = TRUE;
                        }

                        call Leds.led1On();
                }
        }

        event void SplitControl.startDone(error_t err){
                dbg("Boot", "Radio started\n");
                if (err == SUCCESS){
                        send_init_packet();
                }else{
                        dbg("Boot", "Unable to start radio\n");
                        call SplitControl.start();
                }
        }

        event void SplitControl.stopDone(error_t err){
        }

        event void AMSend.sendDone(message_t *msg, error_t err){
                dbg("MS", "Packet sent\n");
                if (msg == &radio_packet){
                        MSPacket *packet;
                        radioBusy = FALSE;

                        packet = (MSPacket*)(call 
Packet.getPayload(&radio_packet, NULL));
                        if (packet->type == BEACON){
                               numberSet = TRUE;
                               call Leds.set(number);
                               dbg("MS", "Beacon packet sent. Number set\n");
                        }
                }
        }

        void receivedInitPacket(MSPacket *packet){
                //if receive init packet, activate (turn leds on)
                //call Leds.set(TOS_NODE_ID);           

                //if receive init packet switch to beacon mode
                MSPacket *beacon;

                //send beacon packet
                beacon = (MSPacket*)(call Packet.getPayload(&radio_packet, 
NULL));
                buildBeaconPacket(beacon, number);
                if (SEND == SUCCESS){
                        radioBusy = TRUE;
                }

                dbg("MS", "Received init packet\n");

        }

        void receivedBeaconPacket(message_t *msg, MSPacket *packet){
                dbg("MS", "Received beacon packet. Number is %s\n",
((numberSet)?"set":"not set"));
                if (numberSet == FALSE){
                        MSPacket *crtNumber;

                        //increment number
                        number++;

                        //build beacon packet
                        crtNumber = (MSPacket*)(call 
Packet.getPayload(&radio_packet, NULL));

                        buildBeaconPacket(crtNumber, number);

                        //send it
                        if (SEND == SUCCESS){
                                radioBusy = TRUE;
                        }
                }else{//on the else side treat corrections in node discovery
                        if (packet->number <= number){
                                am_addr_t source_add;
                                MSPacket *crtNumber;
                                crtNumber = (MSPacket*)(call 
Packet.getPayload(&radio_packet,
NULL));
                                buildCorrectionPacket(crtNumber, number);

                                source_add = call AMPacket.source(msg);
                                if (SEND_TO(source_add) == SUCCESS){
                                        radioBusy = TRUE;
                                }
                        }
                }
        }

        void receivedCorrectionPacket(MSPacket *packet){
                dbg("MS", "Received correction packet number %d\n", 
packet->number);
                if (packet->number >= number) number = packet->number + 1;
                call Leds.set(number);
        }

        void buildInitPacket(MSPacket *packet){
                packet->mote_id = TOS_NODE_ID;
                packet->type = INIT;
        }

        void buildBeaconPacket(MSPacket *packet, uint8_t nr){
                packet->mote_id = TOS_NODE_ID;
                packet->type = BEACON;
                packet->number = nr;
        }

        void buildCorrectionPacket(MSPacket *packet, uint8_t nr){
                packet->mote_id = TOS_NODE_ID;
                packet->type = CORRECTION;
                packet->number = nr;
        }

        event message_t *Receive.receive(message_t *msg, void *payload, uint8_t
len){
                MSPacket *packet;
                dbg("MS", "Received packet\n");
                if (len != sizeof(MSPacket)) return msg; //just ignore

                //get payload
                packet = (MSPacket*)payload;

                switch (packet->type){
                        /**
                         * Hack for node discovery:
                         * if node settled on number will not reinit
                         */
                        case INIT:{
                                          if (numberSet == FALSE){
                                                  receivedInitPacket(packet);
                                          }
                                          break;
                                  }
                        case BEACON:{ 
                                            //initiator ignores beacon messages
                                            if (iAmInitiator) break;
                                            receivedBeaconPacket(msg, packet); 
break;
                                    }
                        case CORRECTION:{
                                                if (call AMPacket.isForMe(msg)){
                                                        
receivedCorrectionPacket(packet);
                                                }
                                                break; //upon new node
                                        }
                        default: break;
                }
                return msg;
        }

}

_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to