Hi all,
I have a problem using two timers under tinyos-2.x with micaz modes. I have my
code ín one file and tried it. I use two timers, one, to start a function
periodic and one to timeout this function and use the leds for debugging. For
the Timeout timer I use the red led and another led for the other timer.
Unfortunately the red led is trigerred, before the timer after my opinion is
called, as it goes on with my periodic timer, but should fire some time after
that. I give you part of my code in the following and hope that somebody can
tell me why this is.
Nicole Neureiter
module ConvergecastAuthM {
provides interface ConvergecastAuth;
uses {
interface Leds;
[..]
interface Timer<TMilli> as MilliTimer;
interface Timer<TMilli> as Timer3;
}
}
implementation {
/*****************************************************************************
*
* AGGREGATOR + NODE: process upflow msg, e.g. save upflow-data
*
**********************************************************/
void processUpflowInit() {
// extract nodecount and nonce from msg
CcAuthUpflowInitMsg_t *recvMsg = (CcAuthUpflowInitMsg_t*)
incPacketQueue[nextIncPacket].payload;
myRndNonce = recvMsg->rndNonce;
myNodeCount = recvMsg->nodeCount;
call Leds.led2Toggle();//gelb sollte angehen an allen knoten, die dies
ausführen
while(UsedRndNonce[count] != myRndNonce)
{
verified[count] = FALSE;
count++;
//sets count to the actual roundnumber
}
if (count == MAX_RNDNONCES)
{
//call Leds.led0On();
dbg("Nonce wurde nicht gefunden, Fehler !!!");
return;
}
// init aggregator
else {if(I_AM_AGGREGATOR) {
// intitialize stuff & timer
nodeCounter = 0;
statFlags.timeOutOccured = FALSE;
verified[count] = FALSE;
// timeout timer
call MilliTimer.startOneShot(TIMEOUT);
// seed list
idList[count][0] = TOS_NODE_ID;
}
// read sensor-data
call Read.read();
}
}
void processNonces()
{ //uint8_t i;
CcAuthSendNonces_t* recMsg = (CcAuthSendNonces_t*) lastPayload;
// for(i = 0; i < MAX_RNDNONCES; i++)
memcpy(UsedRndNonce, recMsg->RndNonces, MAX_RNDNONCES);
//call Leds.led1On();
if(I_AM_AGGREGATOR)
call Timer3.startPeriodic(61440);
}
// compute next packet
task void processNextPacket() {
// decide what to do.. and start corresponding function
dbg("AM", "Received Packet (packets in queue=%i)\n\tProcessing NOW:\n",
incPacketsLeft);
switch (incPacketQueue[nextIncPacket].msgType) {
case MSG_SEND_NONCES:
fsmState = IDLE;
processNonces();
break;
case MSG_UPFLOW_INIT:
fsmState = UPFLOW;
processUpflowInit();
break;
case MSG_UPFLOW_ANSWER: //should only be received and handled by the
aggregator
if ((I_AM_AGGREGATOR) && (fsmState == UPFLOW))
{
// call Leds.led1On();//grün sollte an gehen am aggregator
processInitialSensorData();}
break;
}
// advance to next packet
nextIncPacket = nextIncPacket+1 % PACKET_QUEUE_SIZE;
incPacketsLeft--;
}
task void errorHandler() {
dbg("SYS", "\n\n**** SINK REPORTS CRITICAL ERROR *****\n\n");
}
/*********************************************************
*
* The aggregator send this message every 10 to 15 min to start th execution
of the upflow
* phase
*
***************************************/
void send_MSG_UPFLOW_INIT() {
message_t myPacket;
CcAuthUpflowInitMsg_t *myMsg = (CcAuthUpflowInitMsg_t*) call
Packet.getPayload(&myPacket,NULL);
// construct msg
// myRndNonce = UsedRndNonce[count];
myMsg->msgHdr.msgType = MSG_UPFLOW_INIT;// this msg will kick off the
upflow
myMsg->rndNonce = UsedRndNonce[count]; // random nonce
myMsg->nodeCount = CLUSTER_SIZE-1; // expected number of nodes
dbg("SYS","BROADCASTING: MSG_UPFLOW_INIT\n");
sendPacket(AM_BROADCAST_ADDR, sizeof(CcAuthUpflowInitMsg_t),myPacket);
}
/*****************************************
*
* Timer functions for the aggregator
*
******************************************/
event void Timer3.fired()//aggregator starts one run of upflow phase
{ if(count < MAX_RNDNONCES)
{
fsmState = UPFLOW;
call Leds.led1Toggle();//grün sollte an und ausgehen
send_MSG_UPFLOW_INIT();
processUpflowInit();
}
}
// just set timeOut=TRUE, used by aggregator only
event void MilliTimer.fired()
{
statFlags.timeOutOccured = TRUE;
call Leds.led0Toggle();//rot sollte angehen
}
/********************************
*
* Initialization of the motes
*
*****************************/
event void Boot.booted()//boots the motes
{
call AMControl.start(); //is done for all nodes
}
/**************************************
*
* receive incoming messages, store them in global buffer and post the
corresponding tasks
*
*********************************/
event message_t* AMReceive.receive(message_t* bufPtr, void* payload, uint8_t
len) {
uint8_t nextIdx;
MsgHdr_t* recMsg = (MsgHdr_t*) payload;
// store information on received packet in incoming-packetqueue
nextIdx = nextIncPacket+incPacketsLeft % PACKET_QUEUE_SIZE;
incPacketQueue[nextIdx].msgType = recMsg->msgType;
incPacketQueue[nextIdx].srcAddr = call AMPacket.source(bufPtr);
incPacketQueue[nextIdx].msgLen = len;
if (incPacketQueue[nextIdx].payload != NULL)
incPacketQueue[nextIdx].payload = malloc(len);
else
incPacketQueue[nextIdx].payload =
realloc(incPacketQueue[nextIdx].payload, len);
memcpy(incPacketQueue[nextIdx].payload, payload, len);
// pass control to packet-handler
incPacketsLeft++;
post processNextPacket();
return bufPtr;
}
event void Send.sendDone(message_t* bufPtr, error_t error) {
// in case we wanted an acknowledgement and we did not receive it, we just
resend the last packet.
if (!call
PacketAcknowledgements.wasAcked(&outPacketQueue[nextOutPacket].msg) &&
outPacketQueue[nextOutPacket].destAddr != AM_BROADCAST_ADDR) {
dbg("AM", "MSG to [%i] was not acknowledged. Retrying..\n",
outPacketQueue[nextOutPacket].destAddr);
sendCurrentPacket();
} else if (call
PacketAcknowledgements.wasAcked(&outPacketQueue[nextOutPacket].msg) &&
outPacketQueue[nextOutPacket].destAddr != AM_BROADCAST_ADDR) {
outPacketsLeft--;
dbg("AM","MSG to [%i] successfully sent, packets in queue = %i\n",
outPacketQueue[nextOutPacket].destAddr, outPacketsLeft);
// if there are still packets in the queue
if (outPacketsLeft > 0) {
nextOutPacket = nextOutPacket+1 % PACKET_QUEUE_SIZE;
dbg("AM", "Sending next message in QUEUE to [%i].\n",
outPacketQueue[nextOutPacket].destAddr);
sendCurrentPacket();
}
}
}
/*********************************
*
* event is fired by sensor-interface when value-read is done..
*
*************************************/
event void Read.readDone(error_t result, SensorData_t data) {
#ifdef TOSSIM
mySensorValue[count] = TOS_NODE_ID;
#else
mySensorValue[count] = data;
#endif
dbg("SYS", "--> MY SENSOR VAL: %i\n", mySensorValue);
if (I_AM_AGGREGATOR)
valueList[count][0] = mySensorValue[count];
else
post sendInitialSensorData();
}
event void AMControl.startDone(error_t err) { }
event void AMControl.stopDone(error_t err) { }
}
--
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help