DrandM.nc
上传用户:joranyuan
上传日期:2022-06-23
资源大小:3306k
文件大小:47k
- includes DrandInfo;
- module DrandM {
- provides {
- interface StdControl;
- interface Drand;
- }
- uses {
- interface SendMsg as SendHelloMsg;
- interface SendMsg as SendRequestMsg;
- interface SendMsg as SendRejectMsg;
- interface SendMsg as SendReleaseMsg;
- interface SendMsg as SendTwoHopMsg;
- interface SendMsg as SendGrantMsg;
- interface SendMsg as SendReportMsg;
- interface SendMsg as SendFrameMsg;
- interface ReceiveMsg as ReceiveHelloMsg;
- interface ReceiveMsg as ReceiveRequestMsg;
- interface ReceiveMsg as ReceiveRejectMsg;
- interface ReceiveMsg as ReceiveReleaseMsg;
- interface ReceiveMsg as ReceiveTwoHopMsg;
- interface ReceiveMsg as ReceiveGrantMsg;
- interface ReceiveMsg as ReceiveReportMsg;
- interface ReceiveMsg as ReceiveFrameMsg;
- #ifdef LDEBUG
- interface Leds;
- #endif
- interface Timer as roundTimer;
- interface Timer as reXTimer;
- interface Timer as drandRoundTimer;
- interface SClock;
- }
- }
- implementation {
- #include "DrandMSG.h"
- #ifdef DEBUG
- #include "SODebug.h"
- #endif
- struct TOS_Msg DataPkt;
- uint8_t globalState; // global state
- bool setMySlot, gotAllFrames;
- uint8_t OneWayId[MAX_NBR]; // hello world variables
- uint8_t TwoWayId[MAX_NBR];
-
- nodeInfo nbrInfo[MAX_NBR], myInfo;
- uint8_t requestFireCount, grantFireCount, frameFireCount, reportFireCount;
- uint16_t OTT, ROUND_TIME, REQUEST_TIME, GRANT_TIME; // timer variables
- GTime requestStartTime, locTime, drandStartTime, drandRunTime;
- uint16_t shiftReg, mask;
- #ifdef DEBUG
- GTime roundStartTime; //variables for debugging
- uint8_t reqCount, graCount, rejCount, relCount, twoCount, reqXCount, graXCount ;
- bool reqX, graX;
- uint8_t roundNum;
- #endif
- bool sendBusy, drandFinishedFirstTime, reportFinishedFirstTime, sentRelease, firstGrant;
- uint8_t lastReqID;
- GTime timeDiff(GTime A, GTime B){ // returns A-B
- GTime diff;
- if(A.sticks >= B.sticks){
- diff.mticks = (A.mticks - B.mticks);//*568.88; //ms
- diff.sticks = (A.sticks - B.sticks);//*0.00868; //ms
- }
- else{
- uint32_t temp = 65536;
- diff.mticks = (A.mticks - 1 - B.mticks);//*568.88; //ms
- diff.sticks = ((uint16_t)(temp + A.sticks - B.sticks));//*0.00868);
- }
- return diff;
- }
- #ifdef DEBUG
- void printState(){
- SODbg(DBG_USR2, "ID:%u,ST:%u,R:%u,L:%un", TOS_LOCAL_ADDRESS, globalState, roundNum, lastReqID);
- }
- void printMsg(TOS_MsgPtr msg){
- requestMsg *reqPtr = (requestMsg *) msg->data;
- switch(msg->type){
- case AM_GRANT:
- SODbg(DBG_USR2, "MT: Grantn");
- break;
- case AM_REQUEST:
- SODbg(DBG_USR2, "MT: Requestn");
- break;
- case AM_RELEASE:
- SODbg(DBG_USR2, "MT: Releasen");
- break;
- case AM_REJECT:
- SODbg(DBG_USR2, "MT: Rejectn");
- break;
- case AM_TWOHOP:
- SODbg(DBG_USR2, "MT: 2 Hop Reln");
- break;
- }
- SODbg(DBG_USR2, "SID:%u,SS:%u,LID:%u,RD:%un", reqPtr->sendID, reqPtr->myState, reqPtr->lastRequestID,
- reqPtr->roundNum);
- }
- void printNbrInfo(){
- uint8_t i;
- SODbg(DBG_USR2, "*******************************************************n");
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++){
- switch(globalState){
- case IDLE_STATE2:
- if(nbrInfo[i].bitMap & ONE_HOP){
- SODbg(DBG_USR2, "OneHopNodeID(HELLO) : %u %u %un", nbrInfo[i].nodeID, nbrInfo[i].slot,
- nbrInfo[i].frame);
- } else if(nbrInfo[i].bitMap & TWO_HOP){
- SODbg(DBG_USR2, "TwoHopNodeID(HELLO) : %u %u %un", nbrInfo[i].nodeID, nbrInfo[i].slot,
- nbrInfo[i].frame);
- }
- break;
- case RELEASE_STATE:
- if(nbrInfo[i].bitMap & ONE_HOP){
- SODbg(DBG_USR2, "OneHopNodeID(DRAND) : %u %u %un", nbrInfo[i].nodeID, nbrInfo[i].slot,
- nbrInfo[i].frame);
- } else if(nbrInfo[i].bitMap & TWO_HOP){
- SODbg(DBG_USR2, "TwoHopNodeID(DRAND) : %u %u %un", nbrInfo[i].nodeID, nbrInfo[i].slot,
- nbrInfo[i].frame);
- }
- break;
- case FRAME_STATE:
- if(nbrInfo[i].bitMap & ONE_HOP){
- SODbg(DBG_USR2, "OneHopNodeID(FRAME) : %u %u %un", nbrInfo[i].nodeID, nbrInfo[i].slot,
- nbrInfo[i].frame);
- } else if(nbrInfo[i].bitMap & TWO_HOP){
- SODbg(DBG_USR2, "TwoHopNodeID(FRAME) : %u %u %un", nbrInfo[i].nodeID, nbrInfo[i].slot,
- nbrInfo[i].frame);
- }
- break;
- }
- }
- SODbg(DBG_USR2, "*******************************************************n");
- }
- void timeCheck() {
- GTime roundTime;
- call SClock.getTime(&locTime);
- drandRunTime = timeDiff(locTime, drandStartTime);
- roundTime = timeDiff(locTime, roundStartTime);
-
- SODbg(DBG_USR2, "MESSAGE sID %u mCt %u rReq %u rGra %u mTime %u sTime %un",
- TOS_LOCAL_ADDRESS, (reqCount+graCount+rejCount+relCount+twoCount), reqXCount,
- graXCount, (uint16_t) drandRunTime.mticks, (uint16_t) drandRunTime.sticks);
- SODbg(DBG_USR2, "MESSAGE sID %u reqCt %u graCt %u rejCt %u relCt %u twoCt %un",
- TOS_LOCAL_ADDRESS, reqCount, graCount, rejCount, relCount, twoCount);
- SODbg(DBG_USR2, "MESSAGE sID %u Round %u slotNum %u mRound %u sRound %un",
- TOS_LOCAL_ADDRESS, roundNum, myInfo.slot, roundTime.mticks, roundTime.sticks);
- //}
- }
- #endif
- #ifdef LDEBUG
- void printLED(){
- switch(globalState){
- case IDLE_STATE2:
- call Leds.yellowOff();
- call Leds.greenOff();
- call Leds.redOn();
- break;
- case REQUEST_STATE:
- call Leds.yellowOff();
- call Leds.greenOn();
- call Leds.redOff();
- break;
- case RELEASE_STATE:
- call Leds.yellowOn();
- call Leds.greenOff();
- call Leds.redOff();
- break;
- case GRANT_STATE:
- call Leds.yellowOn();
- call Leds.greenOn();
- call Leds.redOn();
- break;
- }
- }
- #endif
- uint64_t Max(uint16_t v1, uint16_t v2){
- if(v1 >= v2)
- return v1;
- else
- return v2;
- }
- uint64_t Min(uint16_t v1, uint16_t v2){
- if(v1 <= v2)
- return v1;
- else
- return v2;
- }
- int isPresentHelloID(uint8_t node, uint8_t *array, uint8_t max){
- // return -1 if not found, else return position
- uint8_t i;
- for(i = 0; i < max; i++)
- if(array[i] == node)
- return i;
- return -1;
- }
- bool checkForRetransGrant(requestMsg *reqMsgPtr){
- if(isPresentHelloID(TOS_LOCAL_ADDRESS, reqMsgPtr->idMap, MAX_NBR) == -1){
- return FALSE;
- }
- else
- return TRUE;
- }
- bool checkForRetransReport(){
- uint8_t i;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].slot == 0xFF){
- #ifdef REPORT_DEBUG
- SODbg(DBG_USR2, "Need to retransmit for node %un", nbrInfo[i].nodeID);
- #endif
- return TRUE;
- }
- return FALSE;
- }
- bool checkForRetransFrame(){
- uint8_t i;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].frame == 0xFF){
- #ifdef FRAME_DEBUG
- SODbg(DBG_USR2, "CheckForRetransmitFrame: Need to retransmit for node %un", nbrInfo[i].nodeID);
- #endif
- return TRUE;
- }
- return FALSE;
- }
- uint8_t getMaxSlot(){
- uint8_t i, maxSlot = myInfo.slot;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].slot > maxSlot)
- maxSlot = nbrInfo[i].slot;
- return maxSlot;
- }
- void addNode(uint8_t nodeID, uint8_t type){
- uint8_t i;
- if((nodeID == TOS_LOCAL_ADDRESS) || (nodeID == 0xFF)) return;
- atomic{
- for(i = 0;(i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF) && (nbrInfo[i].nodeID != nodeID); i++);
- if (i == MAX_NBR){
- #ifdef DEBUG
- SODbg( DBG_USR2, "addNode: No space to add noden");
- #endif
- } else if (nbrInfo[i].nodeID != nodeID){
- nbrInfo[i].nodeID = nodeID;
- nbrInfo[i].bitMap = type;
- nbrInfo[i].slot = 0xFF;
- nbrInfo[i].frame = 0xFF;
- }
- }
- }
- void removeNode(uint8_t nodeID, uint8_t type){
- uint8_t i;
- atomic{
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF) && (nbrInfo[i].nodeID != nodeID); i++);
- if((i != MAX_NBR) && (nbrInfo[i].nodeID == nodeID) && (nbrInfo[i].bitMap & type)){
- for (;i < MAX_NBR - 1; i++)
- nbrInfo[i] = nbrInfo[i+1];
- nbrInfo[MAX_NBR - 1].nodeID = 0xFF;
- }
- }
- }
- void initializeGrantSent(){
- uint8_t i;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].bitMap & ONE_HOP)
- nbrInfo[i].bitMap = (nbrInfo[i].bitMap & ~GRANT_SENT);
- }
- uint8_t numGrantsRemaining(){
- uint8_t i, count;
- for(i = 0, count = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if((nbrInfo[i].bitMap & ONE_HOP) && (!(nbrInfo[i].bitMap & GRANT_SENT))){
- #ifdef DRAND_DEBUG
- SODbg(DBG_USR2, "RequestTimerFired: Need to retransmit for node %un", nbrInfo[i].nodeID);
- #endif
- count++;
- }
- return count;
- }
- uint8_t numOneHop(){
- uint8_t i, count;
- for(i = 0, count = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].bitMap & ONE_HOP)
- count++;
- return count;
- }
- bool updateNeighborInfo(uint8_t nID, uint8_t slot){ // return true if information has changed
- uint8_t i;
- if((slot == 0xFF) || (nID == 0xFF)) return FALSE;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if((nbrInfo[i].nodeID == nID) && (nbrInfo[i].slot == 0xFF)){
- nbrInfo[i].slot = slot;
- return TRUE;
- }
- return FALSE;
- }
- void fillGrantSendInfo(uint8_t *array){
- uint8_t i, j;
- for(i = 0, j = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if((nbrInfo[i].bitMap & ONE_HOP) && (!(nbrInfo[i].bitMap & GRANT_SENT)))
- array[j++] = nbrInfo[i].nodeID;
- for(; j < MAX_NBR; j++)
- array[j] = 0xFF;
- }
- void fillTimeSlotInfo(uint8_t *array){
- uint8_t i;
- for(i = 0; i < MAX_NBR; i++) // first clear array
- array[i] = 0xFF;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++) // put in one hop slot information
- if((nbrInfo[i].slot != 0xFF) && (nbrInfo[i].bitMap & ONE_HOP))
- array[nbrInfo[i].slot] = nbrInfo[i].nodeID;
- if(setMySlot) // put in my slot info
- array[myInfo.slot] = TOS_LOCAL_ADDRESS;
- }
- void fillFrameInfo(frameMsg *pkt){
- uint8_t i, j;
- for(i = 0; i < MAX_ONE_HOP; i++){ // first clear array
- pkt->oneHopNodeID[i] = 0xFF;
- pkt->frameArray[i] = 0xFF;
- }
- for(i = 0, j = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if((nbrInfo[i].bitMap & ONE_HOP) && (nbrInfo[i].frame != 0xFF)){
- pkt->oneHopNodeID[j] = nbrInfo[i].nodeID;
- pkt->frameArray[j] = nbrInfo[i].frame;
- j++;
- }
- }
- void fillTimeSlotInfoForReport(uint8_t *array){
- uint8_t i;
- for(i = 0; i < MAX_NBR; i++) // first clear array
- array[i] = 0xFF;
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].slot != 0xFF)
- array[nbrInfo[i].slot] = nbrInfo[i].nodeID;
- if(setMySlot)
- array[myInfo.slot] = TOS_LOCAL_ADDRESS;
- }
- void addHelloID (uint8_t node, uint8_t *array){
- uint8_t i;
- if(node == TOS_LOCAL_ADDRESS) return;
- atomic{
- for(i = 0; (i < MAX_NBR) && (array[i] != 0xFF) && (array[i] != node); i++);
- if(i == MAX_NBR){
- #ifdef DEBUG
- SODbg( DBG_USR2, "addNode: No space to add noden");
- #endif
- } else if(array[i] != node)
- array[i] = node;
- }
- }
- void removeHelloID(uint8_t node, uint8_t *array){
- uint8_t i;
- atomic{
- for(i = 0; (i < MAX_NBR) && (array[i] != 0xFF) && (array[i] != node); i++);
- if((i != MAX_NBR) && (array[i] == node)){
- for (;i < MAX_NBR - 1; i++)
- array[i]=array[i+1];
- array[MAX_NBR - 1] = 0xFF;
- }
- }
- }
- int isPresent(uint8_t nodeID, uint8_t type){
- uint8_t i;
- if(nodeID == 0xFF) return -1;
- for(i = 0;(i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if((nbrInfo[i].nodeID == nodeID) && (nbrInfo[i].bitMap & type))
- return i;
- return -1;
- }
- uint8_t getContenders(){
- uint8_t i, contenders = 0;
- for(i = 0;(i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].slot == 0xFF)
- contenders++;
- return (contenders + 1); // I am also a contender !
- }
- void removeNodesForRequest(){
- int i;
- for(i = MAX_NBR - 1; i >= 0; i--){
- if(nbrInfo[i].nodeID == 0xFF)
- continue;
- else if((nbrInfo[i].bitMap & ONE_HOP) && !(nbrInfo[i].bitMap & GRANT_SENT)){
- #ifdef DEBUG
- SODbg( DBG_USR2, "RemoveNodesForRequest: Changed %u to Two Hopn", nbrInfo[i].nodeID);
- #endif
- // removeNode(nbrInfo[i].nodeID, nbrInfo[i].bitMap); // TODO - rewrite !!
- nbrInfo[i].bitMap = TWO_HOP;
- }
- }
- }
- void removeNodeForGrant(uint8_t nodeID){
- int i;
- for(i = MAX_NBR - 1; i >= 0; i--){
- if(nbrInfo[i].nodeID == 0xFF)
- continue;
- else if(nbrInfo[i].nodeID == nodeID){
- #ifdef DEBUG
- SODbg( DBG_USR2, "RemoveNodeForGrant: Changed %u to Two Hopn", nbrInfo[i].nodeID);
- #endif
- // removeNode(nbrInfo[i].nodeID, nbrInfo[i].bitMap);
- nbrInfo[i].bitMap = TWO_HOP;
- return;
- }
- }
- }
- void removeNodesForReport(){
- int i;
- for(i = MAX_NBR - 1; i >= 0; i--){
- if(nbrInfo[i].nodeID == 0xFF)
- continue;
- else if(nbrInfo[i].slot == 0xFF){
- #ifdef DEBUG
- SODbg( DBG_USR2, "RemoveNodesForReport: Removed node %un", nbrInfo[i].nodeID);
- #endif
- removeNode(nbrInfo[i].nodeID, nbrInfo[i].bitMap); // TODO - rewrite !!
- }
- }
- }
- void removeNodesForFrame(){
- int i;
- for(i = MAX_NBR - 1; i >= 0; i--){
- if(nbrInfo[i].nodeID == 0xFF)
- continue;
- else if(nbrInfo[i].frame == 0xFF){
- #ifdef DEBUG
- SODbg( DBG_USR2, "RemoveNodesForFrame: Removed node %un", nbrInfo[i].nodeID);
- #endif
- removeNode(nbrInfo[i].nodeID, nbrInfo[i].bitMap); // TODO - rewrite !!
- }
- }
- }
- void updateNeighborInfoForReport(int nIndex, reportMsg *pkt){
- uint8_t i;
- int n;
- #ifdef REPORT_DEBUG
- SODbg(DBG_USR2, "updating for node = %u ***********************n", nbrInfo[nIndex].nodeID);
- #endif
- if(nIndex != -1)
- nbrInfo[nIndex].slot = pkt->slot;
- for(i = 0; i < MAX_NBR; i++)
- if((n = isPresent(pkt->timeSlot[i], ONE_HOP | TWO_HOP)) != -1){
- nbrInfo[n].slot = i;
- #ifdef REPORT_DEBUG
- SODbg(DBG_USR2, "updating for node = %u, Slot = %un", nbrInfo[n].nodeID, nbrInfo[n].slot);
- #endif
- }
- }
- void updateNeighborInfoForFrame(uint8_t nIndex, frameMsg *pkt){
- uint8_t i;
- int n;
- #ifdef FRAME_DEBUG
- SODbg(DBG_USR2, "updating for node = %u ***********************n", nbrInfo[nIndex].nodeID);
- #endif
- nbrInfo[nIndex].frame = pkt->frame;
- for(i = 0; (i < MAX_ONE_HOP) && (pkt->oneHopNodeID[i] != 0xFF); i++)
- if(((n = isPresent(pkt->oneHopNodeID[i], ONE_HOP | TWO_HOP)) != -1)){
- nbrInfo[n].frame = pkt->frameArray[i];
- #ifdef DEBUG
- SODbg(DBG_USR2, "updating for node = %u, Frame = %un", nbrInfo[n].nodeID, nbrInfo[n].frame);
- #endif
- }
- }
- uint16_t randNew() { /* Return the next 16 bit random number */
- bool endbit;
- uint16_t tmpShiftReg;
- atomic {
- tmpShiftReg = shiftReg;
- endbit = ((tmpShiftReg & 0x8000) != 0);
- tmpShiftReg <<= 1;
- if (endbit)
- tmpShiftReg ^= 0x100b;
- tmpShiftReg++;
- shiftReg = tmpShiftReg;
- tmpShiftReg = tmpShiftReg ^ mask;
- }
- return tmpShiftReg;
- }
- command result_t StdControl.init(){
- atomic {
- uint8_t i;
- for(i = 0 ; i<MAX_NBR ; i++){
- nbrInfo[i].nodeID = 0xFF;
- nbrInfo[i].slot = 0xFF;
- nbrInfo[i].frame = 0xFF;
- nbrInfo[i].bitMap = 0x0;
- OneWayId[i] = TwoWayId[i] = 0xFF;
- }
- myInfo.slot = 0xFF;
- myInfo.nodeID = TOS_LOCAL_ADDRESS;
- myInfo.frame = 0xFF;
- myInfo.bitMap = 0xFF;
- globalState = HELLO_STATE; //start this application with hello mode
- setMySlot = gotAllFrames = FALSE;
- lastReqID = 0xFF;
- requestFireCount = grantFireCount = reportFireCount = frameFireCount = 0;
- OTT = 100; // all times in ms (OTT = time for one time transmission of request/grant msg)
- ROUND_TIME = 1500;
- REQUEST_TIME = 1000;
- GRANT_TIME = 1000;
- sendBusy = FALSE;
- drandFinishedFirstTime = reportFinishedFirstTime = TRUE;
- sentRelease = FALSE;
- firstGrant = TRUE;
- call SClock.getTime(&locTime);
- shiftReg = 119 * 119 * (locTime.sticks + TOS_LOCAL_ADDRESS + 1);
- mask = 137 * 29 * (locTime.sticks + TOS_LOCAL_ADDRESS + 1);
- #ifdef DEBUG
- reqCount = graCount = rejCount = relCount = twoCount = reqXCount = graXCount = 0, roundNum = 0;
- reqX = graX = FALSE;
- init_debug();
- #endif
- #ifdef LDEBUG
- call Leds.init();
- #endif
- }
- return SUCCESS;
- }
- command result_t StdControl.start(){
- globalState = HELLO_STATE;
- call reXTimer.start(TIMER_ONE_SHOT, randNew() % HELLO_INTERVAL);
- call roundTimer.start(TIMER_ONE_SHOT, HELLO_PERIOD);
- return SUCCESS;
- }
- command result_t StdControl.stop(){
- call reXTimer.stop();
- call roundTimer.stop();
- call drandRoundTimer.stop();
- return SUCCESS;
- }
- void sendHelloMsg(){
- atomic{
- if(!sendBusy){
- uint8_t i = 0, j = 0;
- helloMsg *helloMsgData = (helloMsg*)DataPkt.data;
- helloMsgData->sendID = TOS_LOCAL_ADDRESS;
- for(i = 0; i < MAX_NBR; i++)
- helloMsgData->OneWayId[i] = 0xFF;
-
- for(i = 0; (i < MAX_NBR) && (OneWayId[i] != 0xFF); i++)
- helloMsgData->OneWayId[i] = OneWayId[i];
- if(i == MAX_NBR){
- #ifdef DEBUG
- SODbg(DBG_USR2, "SendHelloMsg: Too many neighbors, abort !!!!n");
- #endif
- }
- helloMsgData->OneWayLen = i;
- for(j = 0; (j < MAX_NBR) && (i < MAX_NBR) && (TwoWayId[j] != 0xFF); j++, i++)
- helloMsgData->OneWayId[i] = TwoWayId[j];
- if (call SendHelloMsg.send(TOS_BCAST_ADDR, sizeof(helloMsg), &DataPkt) == SUCCESS)
- sendBusy = TRUE;
- else{
- #ifdef HELLO_DEBUG
- SODbg(DBG_USR2, "SendHelloMsg: Something wrong, send failed !n");
- #endif
- call reXTimer.start(TIMER_ONE_SHOT, randNew() % HELLO_INTERVAL);
- }
- } else {
- #ifdef HELLO_DEBUG
- printState();SODbg(DBG_USR2,"sendHelloMsg:Channel busyn");
- #endif
- call reXTimer.start(TIMER_ONE_SHOT, randNew() % HELLO_INTERVAL);
- }
- }
- }
- void sendRequestMsg(){
- atomic{
- if(!sendBusy){
- requestMsg *requestMsgData = (requestMsg *)DataPkt.data;
- requestMsgData->sendID = TOS_LOCAL_ADDRESS;
- call SClock.getTime(&(requestMsgData->timestamp));
- #ifdef DEBUG
- requestMsgData->myState = globalState;
- requestMsgData->roundNum = roundNum;
- requestMsgData->lastRequestID = lastReqID;
- #endif
- fillGrantSendInfo(requestMsgData->idMap);
- requestMsgData->numRemaining = numGrantsRemaining();
- requestMsgData->OTT = OTT;
- if(call SendRequestMsg.send(TOS_BCAST_ADDR, sizeof(requestMsg), &DataPkt) != SUCCESS){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendRequestMsg: Something wrong, send failed.... n");
- #endif
- call reXTimer.start(TIMER_ONE_SHOT, REQUEST_TIME);
- }else{
- sendBusy = TRUE;
- }
- } else {
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendRequestMsg: Channel Busyn");
- #endif
- call reXTimer.start(TIMER_ONE_SHOT, REQUEST_TIME);
- }
- }
- }
- void sendRejectMsg(uint8_t msgADDR){
- atomic{
- if(!sendBusy){
- rejectMsg *rejectMsgData = (rejectMsg*) DataPkt.data;
- rejectMsgData->sendID = TOS_LOCAL_ADDRESS;
- #ifdef DEBUG
- rejectMsgData->myState = globalState;
- rejectMsgData->roundNum = roundNum;
- rejectMsgData->lastRequestID = lastReqID;
- #endif
- if (call SendRejectMsg.send((uint16_t)msgADDR, sizeof(rejectMsg), &DataPkt) == SUCCESS){
- sendBusy = TRUE;
- }
- else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"sendRejectMsg:failedn");
- #endif
- }
- }
- }
- }
- void sendReleaseMsg(uint8_t slot){
- atomic{
- if(!sendBusy){
- releaseMsg *releaseMsgData = (releaseMsg*)DataPkt.data;
- releaseMsgData->sendID = TOS_LOCAL_ADDRESS;
- releaseMsgData->slot = slot;
- #ifdef DEBUG
- releaseMsgData->myState = globalState;
- releaseMsgData->lastRequestID = lastReqID;
- releaseMsgData->roundNum = roundNum;
- #endif
- if (call SendReleaseMsg.send(TOS_BCAST_ADDR, sizeof(releaseMsg), &DataPkt) == SUCCESS){
- sendBusy = TRUE;
- }
- else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"sendReleaseMsg: send failedn");
- #endif
- }
- }
- }
- }
- void setSlot(){
- uint8_t i;
- uint32_t slotField;
- atomic{
- for(i = 0, slotField = 0x0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- if(nbrInfo[i].slot != 0xFF)
- slotField = slotField | (0x1 << nbrInfo[i].slot);
- for(i = 0; i < 32; i++)
- if(!(slotField & (0x1 << i))){
- myInfo.slot = i;
- setMySlot = TRUE;
- break;
- }
- if(!setMySlot){
- #ifdef DEBUG
- SODbg(DBG_USR2, "SetSlot: Could not set slot, something wrong, abort !!!n");
- #endif
- } else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SetSlot:Scheduling release msgn");
- #endif
- globalState = RELEASE_STATE;
- sendReleaseMsg(myInfo.slot);
- call reXTimer.stop();
- }
- }
- }
- void setFrame(){
- uint8_t maxSlot, frameLen;
- removeNodesForReport(); // clear 2hop nodes still without slot information
- call reXTimer.stop();
- maxSlot = getMaxSlot(); // now find nearest power of 2
- frameLen = 0;
- while(maxSlot != 0){
- maxSlot = (int)(maxSlot/2);
- frameLen++;
- }
- myInfo.frame = 0x1 << frameLen;
- #ifdef ES_DEBUG
- SODbg(DBG_USR2, "Local Frame Size = %u for maxSlot = %un", myInfo.frame, getMaxSlot());
- #endif
- }
-
- void sendTwoHopMsg(uint8_t slot, uint8_t sendID){
- atomic{
- if(!sendBusy){
- twoHopMsg *twoHopMsgData = (twoHopMsg*)DataPkt.data;
- twoHopMsgData->sendID = TOS_LOCAL_ADDRESS;
- twoHopMsgData->slotID = sendID;
- twoHopMsgData->slot = slot;
- #ifdef DEBUG
- twoHopMsgData->myState = globalState;
- twoHopMsgData->roundNum = roundNum;
- twoHopMsgData->lastRequestID = lastReqID;
- #endif
- if (call SendTwoHopMsg.send(TOS_BCAST_ADDR, sizeof(twoHopMsg), &DataPkt) == SUCCESS){
- sendBusy = TRUE;
- } else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"sendTwoHopMsg: send failedn");
- #endif
- }
- }
- }
- }
- void sendGrantMsg(uint8_t msgADDR){
- atomic{
- if(!sendBusy){
- grantMsg *grantMsgData = (grantMsg*)DataPkt.data;
- grantMsgData->sendID = TOS_LOCAL_ADDRESS;
- #ifdef DEBUG
- grantMsgData->myState = globalState;
- grantMsgData->lastRequestID = lastReqID;
- grantMsgData->roundNum = roundNum;
- #endif
- grantMsgData->timestamp = requestStartTime;
- fillTimeSlotInfo(grantMsgData->timeSlot);
- if (call SendGrantMsg.send((uint16_t)msgADDR, sizeof(grantMsg), &DataPkt) != SUCCESS){
- call reXTimer.start(TIMER_ONE_SHOT, GRANT_TIME);
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"sendGrantMsg:grant postponed-1n");
- #endif
- }
- else{
- sendBusy = TRUE;
- }
- } else {
- call reXTimer.start(TIMER_ONE_SHOT, GRANT_TIME);
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"sendGrantMsg:grant postponed-2n");
- #endif
- }
- }
- }
- void sendReportMsg(uint8_t type){
- atomic{
- if(!sendBusy){
- reportMsg *reportMsgData = (reportMsg *) DataPkt.data;
- reportMsgData->slot = myInfo.slot;
- reportMsgData->sendID = TOS_LOCAL_ADDRESS;
- reportMsgData->type = type;
- fillTimeSlotInfoForReport(reportMsgData->timeSlot);
- if(call SendReportMsg.send(TOS_BCAST_ADDR, sizeof(reportMsg), &DataPkt) != SUCCESS){
- call reXTimer.start(TIMER_ONE_SHOT, REPORT_XPERIOD + (randNew() % REPORT_INTERVAL));
- #ifdef REPORT_DEBUG
- SODbg(DBG_USR2, "sendReportMsg: Failed to send report msgn");
- #endif
- } else{
- sendBusy = TRUE;
- }
- } else{
- call reXTimer.start(TIMER_ONE_SHOT, REPORT_XPERIOD + (randNew() % REPORT_INTERVAL));
- #ifdef REPORT_DEBUG
- printState();SODbg(DBG_USR2,"sendReportMsg:Channel Busyn");
- #endif
- }
- }
- }
-
- void sendFrameMsg(uint8_t frameType){
- atomic{
- if(!sendBusy){
- frameMsg *frameMsgData = (frameMsg*) DataPkt.data;
- frameMsgData->sendID = TOS_LOCAL_ADDRESS;
- frameMsgData->frame = myInfo.frame;
- frameMsgData->type = frameType;
- fillFrameInfo(frameMsgData);
- if (call SendFrameMsg.send(TOS_BCAST_ADDR, sizeof(frameMsg), &DataPkt) == SUCCESS){
- sendBusy = TRUE;
- } else{
- #ifdef FRAME_DEBUG
- SODbg(DBG_USR2,"SendFrameMsg.SendDone: Failed to Send Frame Messagen");
- #endif
- call reXTimer.start(TIMER_ONE_SHOT, FRAME_XPERIOD + (randNew() % FRAME_INTERVAL));
- }
- } else{
- #ifdef FRAME_DEBUG
- printState();SODbg(DBG_USR2,"sendFrameMsg: Channel Busyn");
- #endif
- call reXTimer.start(TIMER_ONE_SHOT, FRAME_XPERIOD + (randNew() % FRAME_INTERVAL));
- }
- }
- }
- event result_t SendHelloMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic {
- if(sendBusy){
- sendBusy = FALSE;
- #ifdef HELLO_DEBUG
- SODbg(DBG_USR2, "SendHelloMsg.SendDone: Sent Hello Messagen");
- #endif
- if(globalState == HELLO_STATE)
- call reXTimer.start(TIMER_ONE_SHOT, randNew() % HELLO_INTERVAL);
- }
- }
- return SUCCESS;
- }
- event result_t SendRequestMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic{
- if(sendBusy){
- sendBusy = FALSE;
- #ifdef DRAND_DEBUG
- printMsg(msg);
- #endif
- if(success == FAIL){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendRequestMsgSendDone:Postponed Requestn");
- #endif
- } else{
- #ifdef DEBUG
- if(reqX) reqXCount ++;
- else if(!reqX) reqX = TRUE;
- reqCount++;
- #endif
- requestFireCount++;
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendRequestMsgSendDone:sent Requestn");
- printMsg(msg);
- #endif
- }
- if (globalState == REQUEST_STATE)
- call reXTimer.start(TIMER_ONE_SHOT,REQUEST_TIME);
- }
- }
- return SUCCESS;
- }
- event result_t SendRejectMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic{
- if(sendBusy){
- sendBusy = FALSE;
- #ifdef DRAND_DEBUG
- printMsg(msg);
- #endif
- if(success == SUCCESS){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendRejectMsgSendDone:Sent Reject n");
- #endif
- #ifdef DEBUG
- rejCount++;
- #endif
- } else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendRejectMsgSendDone:Reject FAILEDn");
- #endif
- }
- }
- }
- return SUCCESS;
- }
- event result_t SendReleaseMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic{
- if(sendBusy){
- sendBusy = FALSE;
- if(success == SUCCESS){
- #ifdef DEBUG
- relCount++;
- #endif
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendReleaseMsgSendDone:Sent Release n");
- printMsg(msg);
- #endif
- } else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendReleaseMsgSendDone:Release failedn");
- #endif
- }
- }
- }
- return SUCCESS;
- }
- event result_t SendTwoHopMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic {
- if(sendBusy){
- sendBusy = FALSE;
- if(success == SUCCESS){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendTwoHopMsgSendDone: Sent TwoHop n");
- printMsg(msg);
- #endif
- #ifdef DEBUG
- twoCount++;
- #endif
- } else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendTwoHopMsgSendDone:failed n");
- #endif
- }
- }
- }
- return SUCCESS;
- }
- event result_t SendGrantMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic{
- if(sendBusy){
- sendBusy = FALSE;
- #ifdef DRAND_DEBUG
- printMsg(msg);
- #endif
- if(success == FAIL){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendGrantMsgSendDone: grant failed n");
- #endif
- } else{
- #ifdef DEBUG
- if(graX) graXCount ++;
- else if(!graX) graX = TRUE ;
- graCount++;
- grantFireCount++;
- #endif
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"SendGrantMsgSendDone:Sent Grant n");
- printMsg(msg);
- #endif
- }
- }
- if(globalState == GRANT_STATE)
- call reXTimer.start(TIMER_ONE_SHOT, GRANT_TIME);
- }
- return SUCCESS;
- }
- event result_t SendReportMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic{
- #ifdef LDEBUG
- call Leds.redToggle();
- #endif
- if(sendBusy){
- sendBusy = FALSE;
- #ifdef REPORT_DEBUG
- if(success == SUCCESS)
- SODbg(DBG_USR2,"SendReport.SendDone:send report message n");
- #endif
- }
- if(globalState == REPORT_STATE)
- call reXTimer.start(TIMER_ONE_SHOT, REPORT_XPERIOD + (randNew() % REPORT_INTERVAL));
- }
- return SUCCESS;
- }
- event result_t SendFrameMsg.sendDone(TOS_MsgPtr msg, result_t success){
- atomic{
- if(sendBusy){
- sendBusy = FALSE;
- #ifdef FRAME_DEBUG
- if(success == SUCCESS)
- SODbg(DBG_USR2,"SendFrame.SendDone:sent frame message n");
- #endif
- }
- if(globalState == FRAME_STATE)
- call reXTimer.start(TIMER_ONE_SHOT, FRAME_XPERIOD + (randNew() % FRAME_INTERVAL));
- }
- return SUCCESS;
- }
- event TOS_MsgPtr ReceiveHelloMsg.receive(TOS_MsgPtr m){
- if (globalState != HELLO_STATE) return m;
- atomic{
- uint8_t PacketOneWayID[MAX_NBR], PacketTwoWayID[MAX_NBR];
- helloMsg *recvHelloPtr = (helloMsg*)(m->data);
- uint8_t Sender = recvHelloPtr->sendID;
- uint8_t Me = TOS_LOCAL_ADDRESS;
- uint8_t i, j;
- bool SendHelloFlag = FALSE;
- #ifdef HELLO_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveHelloMsg:Receive Hello Msg from %un",
- recvHelloPtr->sendID);
- #endif
- #ifdef LDEBUG
- call Leds.greenToggle();
- #endif
- for(i = 0; i < MAX_NBR; i++)
- PacketOneWayID[i] = PacketTwoWayID[i] = 0xFF;
- for(i = 0; i < recvHelloPtr->OneWayLen; i++)
- PacketOneWayID[i] = recvHelloPtr->OneWayId[i];
- for(j = 0; i < MAX_NBR; i++, j++)
- PacketTwoWayID[j] = recvHelloPtr->OneWayId[i];
- if (isPresentHelloID(Me, PacketOneWayID, MAX_NBR) != -1) {
- if (isPresentHelloID(Sender, OneWayId, MAX_NBR) != -1) {
- addHelloID(Sender, TwoWayId);
- #ifdef LDEBUG
- call Leds.redToggle();
- #endif
- removeHelloID(Sender, OneWayId);
- }
- else if (isPresentHelloID(Sender, TwoWayId, MAX_NBR) != -1) {
- SendHelloFlag = TRUE;
- }
- else {
- addHelloID(Sender, TwoWayId);
- #ifdef LDEBUG
- call Leds.redToggle();
- #endif
- SendHelloFlag = TRUE;
- }
- }
- else if (isPresentHelloID(Me, PacketTwoWayID, MAX_NBR) != -1) {
- if (isPresentHelloID(Sender, OneWayId, MAX_NBR) != -1) {
- addHelloID(Sender, TwoWayId);
- #ifdef LDEBUG
- call Leds.redToggle();
- #endif
- removeHelloID(Sender, OneWayId);
- SendHelloFlag = TRUE;
- }
- else if (isPresentHelloID(Sender, TwoWayId, MAX_NBR) != -1) {}
- else if ((isPresentHelloID(Sender, TwoWayId, MAX_NBR) == -1) &&
- (isPresentHelloID(Sender, OneWayId, MAX_NBR) == -1)) {}
- }
- else if ((isPresentHelloID(Me, PacketOneWayID, MAX_NBR) == -1) &&
- (isPresentHelloID(Me, PacketTwoWayID, MAX_NBR) == -1)) {
- if (isPresentHelloID(Sender, OneWayId, MAX_NBR) == -1) {
- addHelloID(Sender, OneWayId);
- }
- else if (isPresentHelloID(Sender, OneWayId, MAX_NBR) != -1) {
- SendHelloFlag = TRUE;
- }
- else if (isPresentHelloID(Sender, TwoWayId, MAX_NBR) != -1) {}
- }
- if (SendHelloFlag && globalState != HELLO_STATE)
- sendHelloMsg();
- // Update Two Hop ID
- for (i = 0; i < MAX_NBR; i++)
- if (isPresentHelloID(PacketTwoWayID[i], TwoWayId, MAX_NBR) == -1)
- addNode(PacketTwoWayID[i], TWO_HOP);
- // Remove this sender from my two Hop node if it is present
- removeNode(recvHelloPtr->sendID, TWO_HOP);
- }//atomic
- return m;
- }//end of ReceiveHelloMsg.receive(TOS_MsgPtr m)
- event TOS_MsgPtr ReceiveRequestMsg.receive(TOS_MsgPtr m){
- uint8_t i;
- requestMsg *recvRequestPtr = (requestMsg*)(m->data);
- uint16_t reqSentID = recvRequestPtr->sendID;
- if(isPresent(reqSentID, ONE_HOP) == -1) return m;
- atomic{
- requestStartTime = recvRequestPtr->timestamp;
- for(i = 0; (i < MAX_NBR) && (recvRequestPtr->idMap[i] != 0xFF); i++)
- if(isPresent(recvRequestPtr->idMap[i], ONE_HOP) == -1)
- addNode(recvRequestPtr->idMap[i], TWO_HOP);
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveRequestMsg:Receive Request Msgn");
- printMsg(m);
- #endif
- if (( reqSentID != lastReqID ) && (globalState == GRANT_STATE ||
- globalState == REQUEST_STATE)) {
- // This node is not the one that I already sent a grant, so send a reject.
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveRequestMsg:Scheduling rejectn");
- #endif
- sendRejectMsg(reqSentID);
- #ifdef DRAND_DEBUG
- SODbg( DBG_USR2, "sent reject to %un", reqSentID);
- #endif
- } else if((globalState == IDLE_STATE2) || (setMySlot)){
- if(!setMySlot)
- call drandRoundTimer.stop();
- globalState = GRANT_STATE;
- grantFireCount = 0;
- #ifdef LDEBUG
- printLED();
- #endif
- lastReqID = reqSentID ; //to distinguish with request which is because of loss of reject
- GRANT_TIME = ((recvRequestPtr->numRemaining * recvRequestPtr->OTT) + recvRequestPtr->OTT) * 2;
- // subdue grant because this is costly !
- call reXTimer.start(TIMER_ONE_SHOT, randNew() % GRANT_INTERVAL);
- } else if((globalState == GRANT_STATE) && (reqSentID == lastReqID)){
- GRANT_TIME = ((recvRequestPtr->numRemaining * recvRequestPtr->OTT) + recvRequestPtr->OTT) * 2;
- // subdue grant because this is costly !
- if (checkForRetransGrant(recvRequestPtr)){
- call reXTimer.start(TIMER_ONE_SHOT, randNew() % GRANT_INTERVAL);
- } else{
- call reXTimer.start(TIMER_ONE_SHOT, GRANT_TIME);
- grantFireCount = 0;
- }
- }
- }//atomic
- return m;
- }//end of ReceiveRequestMsg.receive(TOS_MsgPtr m)
- event TOS_MsgPtr ReceiveRejectMsg.receive(TOS_MsgPtr m){
- if(globalState != REQUEST_STATE) return m;
- atomic{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveRejectMsg:Received Reject Msgn");
- printMsg(m);
- #endif
- call reXTimer.stop();
- // if(!sentRelease){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2, "ReceivedRejectMsg:Scheduling Releasen");
- #endif
- sendReleaseMsg(0xFF);
- sentRelease = TRUE;
- // }
- initializeGrantSent();
- globalState = IDLE_STATE2; //go back to IDLE_STATE2
- #ifdef DEBUG
- reqX = FALSE;
- #endif
- call drandRoundTimer.start(TIMER_ONE_SHOT, 1000); // time for current round to finish
- #ifdef LDEBUG
- printLED();
- #endif
- }//atomic
- return m;
- }//end of ReceiveRejectMsg.receive(TOS_MsgPtr m)
- event TOS_MsgPtr ReceiveReleaseMsg.receive(TOS_MsgPtr m){
- int nIndex;
- releaseMsg *recvReleasePtr = (releaseMsg*)(m->data);
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveReleaseMsg:Received Release Msgn");
- printMsg(m);
- #endif
- if(((nIndex = isPresent(recvReleasePtr->sendID, ONE_HOP)) == -1) ||
- (globalState != GRANT_STATE) || (lastReqID != recvReleasePtr->sendID))
- return m;
- atomic{
- call reXTimer.stop();
- if(updateNeighborInfo(recvReleasePtr->sendID, recvReleasePtr->slot))
- sendTwoHopMsg(recvReleasePtr->slot, recvReleasePtr->sendID);
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveReleaseMsg:Scheduling two hop releasen");
- #endif
-
- if (setMySlot) { //&&(globalState == GRANT_STATE)
- globalState = RELEASE_STATE;
- #ifdef LDEBUG
- printLED();
- #endif
- } else {
- globalState = IDLE_STATE2;
- call drandRoundTimer.start(TIMER_ONE_SHOT, 100);
- #ifdef LDEBUG
- printLED();
- #endif
- }
- #ifdef DEBUG
- graX = FALSE;
- #endif
- lastReqID = 0xFF;
- }
- return m;
- }//end of ReceiveReleaseMsg.receive(TOS_MsgPtr m)
- event TOS_MsgPtr ReceiveTwoHopMsg.receive(TOS_MsgPtr m){
- int nIndex;
- twoHopMsg *recvTwoHopPtr = (twoHopMsg*)(m->data);
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveTwoHopMsg:Received Two Hopn");
- printMsg(m);
- #endif
- if(((nIndex = isPresent(recvTwoHopPtr->sendID, ONE_HOP)) == -1) ||
- recvTwoHopPtr->slotID == TOS_LOCAL_ADDRESS)
- return m;
- atomic {
- if((globalState == GRANT_STATE) && (recvTwoHopPtr->slotID == lastReqID)){
- recvTwoHopPtr->sendID = recvTwoHopPtr->slotID;
- signal ReceiveReleaseMsg.receive(m);
- }
-
- updateNeighborInfo(recvTwoHopPtr->slotID, recvTwoHopPtr->slot);
- }
- return m;
- }//end of ReceiveTwoHopMsg.receive(TOS_MsgPtr m)
- event TOS_MsgPtr ReceiveGrantMsg.receive(TOS_MsgPtr m){
- uint8_t i;
- GTime rttTime;
- double currOTT;
- bool dup = FALSE;
- grantMsg *recvGrantPtr = (grantMsg*)(m->data);
- int nIndex = isPresent(recvGrantPtr->sendID, ONE_HOP);
- if(nIndex == -1)
- return m;
- atomic{
- for (i = 0; i < MAX_NBR; i++){ // copy sender information
- if(isPresent(recvGrantPtr->timeSlot[i], ONE_HOP) == -1)
- addNode(recvGrantPtr->timeSlot[i], TWO_HOP);
- updateNeighborInfo(recvGrantPtr->timeSlot[i], i);
- }
-
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveGrantMsg:Received Grant Msgn");
- printMsg(m);
- #endif
- if(setMySlot){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveGrantMsg:Scheduling release msgn");
- #endif
- sendReleaseMsg(myInfo.slot);
- } else if((globalState == IDLE_STATE2)|| (globalState == GRANT_STATE)){
- // if(!sentRelease){
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"ReceiveGrantMsg:Scheduling FAIL release msgn");
- #endif
- sendReleaseMsg(0xFF);
- sentRelease = TRUE;
- // }
- } else if(globalState == REQUEST_STATE){
- if(!(nbrInfo[nIndex].bitMap & GRANT_SENT)) // if not duplicate, add to grantSendNode list
- nbrInfo[nIndex].bitMap |= GRANT_SENT;
- else
- dup = TRUE;
- if(firstGrant){ // update timers using info from first grant
- call SClock.getTime(&locTime);
- rttTime = timeDiff(locTime, recvGrantPtr->timestamp);
- currOTT = (rttTime.mticks * 568.88 + rttTime.sticks * 0.00868)/2;
- OTT = (uint16_t) (currOTT * .50 + OTT * .50);
- #ifdef DRAND_DEBUG
- SODbg(DBG_USR2,"ReceiveGrantMsg: Current OTT = %u, average OTT = %un",
- (uint16_t) currOTT, OTT);
- #endif
- ROUND_TIME = (OTT + (OTT * numOneHop()) + OTT) * 2;
- REQUEST_TIME = (2 * OTT) * 2;
- firstGrant = FALSE;
- }
- //check that all one hop node sent grant message and set the time slot. And send release message.
- if ((numGrantsRemaining() == 0) && (!setMySlot)){
- setSlot(); // set slot, change state, send release and schedule report timer
- #ifdef ES_DEBUG
- timeCheck();
- printNbrInfo();
- #endif
- #ifdef LDEBUG
- printLED();
- #endif
- } else if(!dup){
- call reXTimer.start(TIMER_ONE_SHOT, REQUEST_TIME);
- requestFireCount = 0;
- }
- }//else
- }//atomic
- return m;
- }//end of ReceiveGrantMsg.receive(TOS_MsgPtr m)
- void reXFrame(){
- if(frameFireCount > MAX_FRAME)
- removeNodesForFrame();
- else
- frameFireCount++;
- if(!checkForRetransFrame()){
- call reXTimer.stop();
- if(!gotAllFrames){
- signal Drand.gotFrame();
- gotAllFrames = TRUE;
- #ifdef ES_DEBUG
- printNbrInfo();
- #endif
- }
- } else{
- #ifdef FRAME_DEBUG
- SODbg(DBG_USR2,"frameTimer.fired:Broadcast request for Frame againn");
- #endif
- sendFrameMsg(FRAME_REQUEST);
- }
- }
- void reXReport(){
- if(reportFireCount > MAX_REPORT)
- removeNodesForReport();
- else
- reportFireCount++;
-
- if(!checkForRetransReport())
- setFrame();
- else{
- sendReportMsg(REPORT_REQUEST);
- #ifdef REPORT_DEBUG
- SODbg(DBG_USR2, "ReportTimerFired: Need to retransmit with %un", reportFireCount);
- #endif
- }
- }
-
- event TOS_MsgPtr ReceiveReportMsg.receive(TOS_MsgPtr m){
- if(!setMySlot) return m;
- atomic{
- reportMsg *recvReportPtr = (reportMsg*)(m->data);
- int nIndex = isPresent(recvReportPtr->sendID, ONE_HOP);
- #ifdef REPORT_DEBUG
- printState(); SODbg(DBG_USR2,"ReceiveReportMsg.receive:from %u, type %un", recvReportPtr->sendID,
- recvReportPtr->type);
- #endif
- updateNeighborInfoForReport(nIndex, recvReportPtr); // update time slots from packet
- if(globalState == RELEASE_STATE){
- globalState = REPORT_STATE;
- reXReport();
- call roundTimer.start(TIMER_ONE_SHOT, REPORT_PERIOD);
- }
- if(recvReportPtr->type == REPORT_REQUEST){
- sendReportMsg(REPORT_REPLY);
- #ifdef REPORT_DEBUG
- SODbg(DBG_USR2,"ReceiveReportMsg: Sending Replyn");
- #endif
- }
- }
- return m;
- }
-
- event TOS_MsgPtr ReceiveFrameMsg.receive(TOS_MsgPtr m){
- if((globalState != FRAME_STATE) && !gotAllFrames) return m;
- atomic{
- frameMsg *recvFramePtr = (frameMsg*)(m->data);
- int nIndex = isPresent(recvFramePtr->sendID, ONE_HOP);
- #ifdef FRAME_DEBUG
- SODbg(DBG_USR2,"ReceiveFrameMsg:receive frame msg from %u of type %un", recvFramePtr->sendID,
- recvFramePtr->type);
- #endif
- updateNeighborInfoForFrame(nIndex, recvFramePtr);
- if(globalState == REPORT_STATE){
- globalState = FRAME_STATE;
- reXFrame();
- }
- if(recvFramePtr->type == FRAME_REQUEST)
- sendFrameMsg(FRAME_REPLY);
- }
- return m;
- }
- void reXHello(){
- atomic{
- sendHelloMsg();
- }
- }
- void reXRequest(){
- atomic {
- if (requestFireCount > MAX_REQUEST){
- removeNodesForRequest(); // remove nodes who have not sent grant yet
- if((numGrantsRemaining() == 0) && (!setMySlot)){
- setSlot();
- #ifdef ES_DEBUG
- timeCheck();
- printNbrInfo();
- #endif
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"RequestTimerFired:Scheduling release msgn");
- #endif
- #ifdef LDEBUG
- printLED();
- #endif
- }
- } else {
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"requestTimerFired:Scheduling request with %un", requestFireCount);
- #endif
- sendRequestMsg();
- }
- }
- }
- void reXGrant(){
- atomic{
- if (grantFireCount > MAX_GRANT){
- removeNodeForGrant(lastReqID);
- if(setMySlot){
- globalState = RELEASE_STATE;
- #ifdef LDEBUG
- printLED();
- #endif
- } else{
- globalState = IDLE_STATE2;
- call drandRoundTimer.start(TIMER_ONE_SHOT, 100);
- #ifdef LDEBUG
- printLED();
- #endif
- }
- #ifdef DEBUG
- graX = FALSE;
- #endif
- lastReqID = 0xFF;
- } else{
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"grantTimerFired:Scheduling grant with %un", grantFireCount);
- #endif
- sendGrantMsg(lastReqID);
- }
- }
- }
- void helloOver(){ // hello phase over, start drand round
- uint8_t i, randNum, contender;
- signal Drand.helloOver(); // signal aplication that hello is over
- atomic{
- globalState = IDLE_STATE2;
- call reXTimer.stop();
- #ifdef LDEBUG
- printLED();
- #endif
- call SClock.getTime(&drandStartTime); //drand starting time
- for (i = 0; i < MAX_NBR; i++)
- addNode(TwoWayId[i], ONE_HOP);
- #ifdef ES_DEBUG
- printNbrInfo();
- #endif
- contender = getContenders();
- randNum = randNew();
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"HelloTimerFired: trying lottery with contenders = %un", contender);
- SODbg(DBG_USR2,"Random number : %u n", randNum);
- #endif
- if ((randNum % contender) == 0){ // I am the winner
- globalState = REQUEST_STATE;
- requestFireCount = 0;
- #ifdef DEBUG
- call SClock.getTime(&roundStartTime);
- #endif
- #ifdef LDEBUG
- printLED();
- #endif
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"HelloRoundTimerFired:Scheduling requestn");
- #endif
- sendRequestMsg();
- } else
- call drandRoundTimer.start(TIMER_ONE_SHOT, ROUND_TIME);
- }//atomic
- }
- void drandRoundOver(){
- uint8_t randNum, contender;
- atomic{
- sentRelease = FALSE;
- firstGrant = TRUE;
- #ifdef DEBUG
- roundNum++;
- #endif
- randNum = randNew();
- contender = getContenders();
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"DrandRoundTimerFired:running lottery n");
- SODbg(DBG_USR2, "Contender = %un", contender);
- SODbg(DBG_USR2,"Random number : %u n", randNum);
- #endif
- if((randNum % contender) == 0){ // I am the winner
- globalState = REQUEST_STATE;
- #ifdef DEBUG
- call SClock.getTime(&roundStartTime);
- #endif
- requestFireCount = 0;
- #ifdef LDEBUG
- printLED();
- #endif
- #ifdef DRAND_DEBUG
- printState();SODbg(DBG_USR2,"drandRoundTimerFired:WON LOTTERY, Scheduling requestn");
- #endif
- sendRequestMsg();
- } else{
- #ifdef DRAND_DEBUG
- printState();
- SODbg(DBG_USR2,"drandRoundTimerFired:LOST LOTTERY, Scheduling request after %un", ROUND_TIME);
- #endif
- call drandRoundTimer.start(TIMER_ONE_SHOT, ROUND_TIME);
- }
- lastReqID = 0xff;
- }
- }
- event result_t reXTimer.fired(){
- atomic{
- switch(globalState){
- case HELLO_STATE:
- reXHello();
- break;
- case REQUEST_STATE:
- reXRequest();
- break;
- case GRANT_STATE:
- reXGrant();
- break;
- case REPORT_STATE:
- reXReport();
- break;
- case FRAME_STATE:
- reXFrame();
- break;
- }
- }
- return SUCCESS;
- }
- event result_t roundTimer.fired(){
- atomic{
- switch(globalState){
- case HELLO_STATE:
- helloOver();
- call roundTimer.start(TIMER_ONE_SHOT, DRAND_PERIOD);
- break;
- case RELEASE_STATE: // timer expired earlier
- globalState = REPORT_STATE;
- reXReport();
- call roundTimer.start(TIMER_ONE_SHOT, REPORT_PERIOD);
- break;
- case REPORT_STATE: // remove nodes without slot info, and start frame state
- setFrame();
- globalState = FRAME_STATE;
- reXFrame();
- break;
- }
- }
- return SUCCESS;
- }
- event result_t drandRoundTimer.fired(){
- if(globalState == IDLE_STATE2)
- drandRoundOver();
- return SUCCESS;
- }
- /****************** Drand Interface ********************/
- default async event void Drand.gotFrame(){}
- default async event void Drand.helloOver(){}
- command nodeInfo* Drand.getNeighborInfo(){ return nbrInfo; }
- command nodeInfo* Drand.getMyInfo(){ return &myInfo; }
- command void Drand.startDrand(){
- call StdControl.init();
- call StdControl.start();
- }
- command void Drand.stopDrand(){ call StdControl.stop(); }
- /**************SClock Interface********/
- async event result_t SClock.syncDone(){ return SUCCESS; }
- async event result_t SClock.fire(uint16_t mticks){ return SUCCESS; }
- }//end of implementation