Hi,

I wrote a very simple program for my telosb nodes. In this program, each
node keeps boradcasting a message at 50Hz. If any node receives a message
from other nodes, it will print it out thorough the serial port.

However, after I uploading the code into the nodes, I found that nodes were
normal at the beginning several seconds, after that, some nodes would be
frozen. Could you please help me to check what the problem is? My code
seems good to me.

snifferMsg.h

#ifndef SNIFFERMSG_H
#define SNIFFERMSG_H

enum {
        AM_SNIFFERMSG = 7,
        NODE_NUMBER = 4,
        UART_QUEUE_LEN = 12,
        SENDING_RATE = 20,
        TX_POWER = 31,
};


typedef nx_struct snifferMsg {
        nx_uint16_t senderNodeId;
        nx_uint32_t seq;
        nx_uint32_t lastSeq;
        nx_uint32_t lastSendingTimeStamp;
} snifferMsg;

typedef nx_struct uartMsg {
        nx_uint16_t senderNodeId;
        nx_uint32_t seq;
        nx_uint32_t lastSeq;
        nx_uint32_t lastSendingTimeStamp;
        
        nx_uint32_t timeStamp;
        nx_int16_t rssi;
        
        
} uartMsg;


#endif


CSMASnifferC.nc

#include "AM.h"
#include "Serial.h"
#include <Timer.h>
#include "snifferMsg.h"
#include "CC2420.h"

module CSMASnifferC {
        uses {
                interface Boot;
                interface Leds;
                
                interface SplitControl as SerialControl;
                interface SplitControl as RadioControl;

                interface Timer<TMilli> as sendTimer;
                
                interface AMSend;
                interface Receive as AMReceive;

                interface AMSend as UartSend[am_id_t id];
                
                interface Packet;
                interface Packet as RadioPacket;
                interface AMPacket as RadioAMPacket;
                interface CC2420Packet;
                
                interface PacketTimeStamp<TMilli,uint32_t>;
                interface RadioBackoff;
        }
}

implementation
{
        message_t uartQueueBufs[UART_QUEUE_LEN];
        message_t *uartQueue[UART_QUEUE_LEN];
        uint8_t uartIn, uartOut;
        bool uartBusy, uartFull;
        
        snifferMsg* snifferPkt;
        message_t msg_pkt;
        uint8_t lenMsg = sizeof(snifferMsg);
        uint8_t lenUartMsg = sizeof(uartMsg);
        
        bool radioBusy = FALSE;
        uint32_t seq = 1;
        uint32_t lastSeq = 0;
        uint32_t lastSendingTimeStamp = 0;
        
        task void uartSendTask();
        
        void packetize();
        
        void dropBlink() {
        }
        
        void failBlink() {
        }

        event void Boot.booted() {
                uint8_t i;

                for (i = 0; i < UART_QUEUE_LEN; i++){
                        uartQueue[i] = &uartQueueBufs[i];
                }               
                uartIn = uartOut = 0;
                uartBusy = FALSE;
                uartFull = TRUE;

                call RadioControl.start();
                call SerialControl.start();
        }
        
        event void SerialControl.startDone(error_t error) {
                if (error == SUCCESS) {
                        uartFull = FALSE;
                }
                else{
                        call SerialControl.start();
                }
        }

        event void RadioControl.startDone(error_t error) {
                if (error == SUCCESS) {
                        call sendTimer.startPeriodic(SENDING_RATE);
                }
                else{
                        call RadioControl.start();
                }
        }
        
        event void SerialControl.stopDone(error_t error) {}
        event void RadioControl.stopDone(error_t error) {}
        
        event void sendTimer.fired(){
                if (!radioBusy){
                        
                        packetize();
                        if (call AMSend.send(AM_BROADCAST_ADDR, &msg_pkt, 
lenMsg) == SUCCESS) {
                                call Leds.led0Toggle();
                                radioBusy = TRUE;
                        }
                }
        }
        
        void packetize(){
                snifferPkt = (snifferMsg*)(call 
Packet.getPayload(&msg_pkt,lenMsg));
                snifferPkt->senderNodeId = TOS_NODE_ID;
                snifferPkt->seq = seq;
                snifferPkt->lastSeq = lastSeq;
                snifferPkt->lastSendingTimeStamp = lastSendingTimeStamp;
        }
        
        event void AMSend.sendDone(message_t* msg, error_t error){
                radioBusy = FALSE;
                if (error == SUCCESS){
                        lastSeq = seq;
                        lastSendingTimeStamp = call 
PacketTimeStamp.timestamp(msg);
                        seq++;
                }
        }
        
        event message_t *AMReceive.receive(message_t *msg,
                                                    void *payload,
                                                    uint8_t len) {
                uartMsg* uartTempPayload;                                       
                snifferMsg* recPkt = (snifferMsg*) payload;                     
                
                message_t *ret = msg;
                message_t msg_uartPkt;
                
                if (len != lenMsg) {
                        return ret;
                }
                
                call Leds.led1Toggle();
                
                uartTempPayload = (uartMsg*)(call 
Packet.getPayload(&msg_uartPkt,lenUartMsg));
                uartTempPayload->senderNodeId = recPkt->senderNodeId;
                uartTempPayload->seq = recPkt->seq;
                uartTempPayload->lastSeq = recPkt->lastSeq;
                uartTempPayload->lastSendingTimeStamp = 
recPkt->lastSendingTimeStamp;
                uartTempPayload->timeStamp = call 
PacketTimeStamp.timestamp(msg);
                uartTempPayload->rssi = (int16_t) call 
CC2420Packet.getRssi(msg) -45;
                call Packet.setPayloadLength(&msg_uartPkt,lenUartMsg);
                
                
                atomic {
                        if (!uartFull)
                        {
                                ret = uartQueue[uartIn];
                                uartQueue[uartIn] = &msg_uartPkt;
                                //uartQueue[uartIn] = msg;

                                uartIn = (uartIn + 1) % UART_QUEUE_LEN;

                                if (uartIn == uartOut)
                                        uartFull = TRUE;

                                if (!uartBusy)
                                {
                                        post uartSendTask();
                                        uartBusy = TRUE;
                                }
                        }
                        else
                                dropBlink();
                }

                return ret;
        }
        
        uint8_t tmpLen;

        task void uartSendTask() {
                uint8_t len;
                am_id_t id;
                am_addr_t addr;
                message_t* msg;
                atomic
                        if (uartIn == uartOut && !uartFull)
                        {
                                uartBusy = FALSE;
                                return;
                        }

                msg = uartQueue[uartOut];
                tmpLen = len = call RadioPacket.payloadLength(msg);
                
                id = call RadioAMPacket.type(msg);
                addr = call RadioAMPacket.destination(msg);

                if (call UartSend.send[id](addr, uartQueue[uartOut], len) == 
SUCCESS){
                        call Leds.led2Toggle();
                }
                else
                {
                        failBlink();
                        post uartSendTask();
                }
        }

        event void UartSend.sendDone[am_id_t id](message_t* msg, error_t error) 
{
                if (error != SUCCESS)
                        failBlink();
                else
                        atomic
                        if (msg == uartQueue[uartOut])
                        {
                                if (++uartOut >= UART_QUEUE_LEN)
                                        uartOut = 0;
                                if (uartFull)
                                        uartFull = FALSE;
                        }
                post uartSendTask();
        }
        
        async event void RadioBackoff.requestCca(message_t * ONE msg){
                //call RadioBackoff.setCca(FALSE);
        }
        
        async event void RadioBackoff.requestInitialBackoff(message_t * ONE 
msg){
        }
        async event void RadioBackoff.requestCongestionBackoff(message_t * ONE 
msg){
        }

}


CSMASnifferAppC.nc

#include "snifferMsg.h"

configuration CSMASnifferAppC {
}
implementation {
        components MainC;
        components CSMASnifferC;
        components LedsC;
        
        components CC2420ActiveMessageC as Radio;
        components SerialActiveMessageC as Serial;
        
        components new TimerMilliC() as sendTimer;

        components new AMSenderC(AM_SNIFFERMSG);
        components new AMReceiverC(AM_SNIFFERMSG);
        
        components CC2420PacketC;
        
        
        CSMASnifferC.Boot -> MainC;
        CSMASnifferC.Leds -> LedsC;
        
        CSMASnifferC.RadioControl -> Radio;
        CSMASnifferC.SerialControl -> Serial;

        CSMASnifferC.sendTimer -> sendTimer;
        
        CSMASnifferC.AMSend -> AMSenderC;
        CSMASnifferC.AMReceive -> AMReceiverC;
        
        
        CSMASnifferC.UartSend -> Serial;
        
        CSMASnifferC.Packet -> AMSenderC;
        CSMASnifferC.RadioPacket -> Radio;
        CSMASnifferC.RadioAMPacket -> Radio;
        CSMASnifferC.CC2420Packet -> CC2420PacketC;
        
        CSMASnifferC.PacketTimeStamp -> CC2420PacketC.PacketTimeStampMilli;
        CSMASnifferC.RadioBackoff -> Radio.RadioBackoff[AM_SNIFFERMSG];
}

Makefile

COMPONENT=CSMASnifferAppC
CFLAGS += -DCC2420_NO_ACKNOWLEDGEMENTS
CFLAGS += -DCC2420_NO_ADDRESS_RECOGNITION
CFLAGS += -DTOSH_DATA_LENGTH=42
#CFLAGS += -I$(TOSDIR)/lib/printf

BUILD_EXTRA_DEPS=SnifferMsg.class
CLEAN_EXTRA = *.class SnifferMsg.java


SnifferMsg.class: $(wildcard *.java) SnifferMsg.java
        javac *.java

SnifferMsg.java:
        mig java -target=null -java-classname=SnifferMsg snifferMsg.h 
snifferMsg -o $@


include $(MAKERULES)




-- 
Zhiheng Xie
_______________________________________________
Tinyos-help mailing list
Tinyos-help@millennium.berkeley.edu
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to