MultiHopTtestMStatic.nc
上传用户:joranyuan
上传日期:2022-06-23
资源大小:3306k
文件大小:7k
- includes MultiHopTtestMSG;
- includes DrandInfo;
- module MultiHopTtestM {
- provides {
- interface StdControl;
- }
- uses {
- #ifdef DRAND_ENABLE
- interface StdControl as SubControl;
- #endif
- interface Send;
- interface Intercept;
- interface Random;
- interface Timer as RouteTimer;
- interface Timer as StatTimer;
- interface Timer as SendTimer;
- interface RouteControl;
- interface CC1000Control as RFset;
- #ifdef DRAND_ENABLE
- interface Drand;
- interface SetDrand;
- #endif
- interface SetDrand;
- async command result_t ResetPower();
- async command result_t ResetDataSent();
- async command uint16_t GetPower_tx();
- async command uint16_t GetDataSent();
- async command uint16_t GetPower_sp();
- async command uint16_t GetPower_rx();
- }
- }
- implementation {
- #ifdef APP_DEBUG
- #include "SODebug.h"
- #endif
- TOS_Msg DataPkt;
- uint16_t Rcount;
- uint16_t Scount;
- uint16_t time;
- bool txBusy, gotFrames, startSender;
- uint8_t eachCount[MAX_NODES];
- /*********** StdControl Interface ************/
- command result_t StdControl.init(){
- uint8_t i;
- Rcount = 0;
- Scount = 0;
- call ResetDataSent();
- call ResetPower();
- for(i = 0; i < MAX_NODES; i++)
- eachCount[i] = 0;
- txBusy = startSender = FALSE;
- #ifdef APP_DEBUG
- init_debug();
- #endif
- #ifdef DRAND_ENABLE
- call SubControl.init();
- gotFrames = FALSE;
- #else
- gotFrames = TRUE;
- #endif
- call Random.init();
- return SUCCESS;
- }
- command result_t StdControl.start(){
- uint16_t len;
- multiHopMsg *multiHopMsgData = (multiHopMsg*) (call Send.getBuffer(&DataPkt, &len));
- multiHopMsgData->sendID = TOS_LOCAL_ADDRESS;
- call RFset.SetRFPower(255); //lowest 1~0xFF highest
- call RouteTimer.start(TIMER_ONE_SHOT, (call Random.rand()) % ROUTE_TIME);
- return SUCCESS;
- }
-
- command result_t StdControl.stop(){
- call RouteTimer.stop();
- call StatTimer.stop();
- call SendTimer.stop();
- #ifdef DRAND_ENABLE
- call SubControl.stop();
- #endif
- return SUCCESS;
- }
- event result_t Send.sendDone(TOS_MsgPtr msg, result_t success){
- if(success == SUCCESS){
- #ifdef APP_SEND_DEBUG
- SODbg(DBG_USR2, "Sent packet to address %un", call RouteControl.getParent());
- #endif
- Scount++;
- }
- txBusy = FALSE;
- return SUCCESS;
- }
- event result_t Intercept.intercept(TOS_MsgPtr m, void* payload, uint16_t payloadLen){
- multiHopMsg *hData = (multiHopMsg*)payload;
- if(hData->sendID < MAX_NODES){
- eachCount[hData->sendID]++;
- Rcount++;
- }
- return SUCCESS;
- }
- event result_t SendTimer.fired(){
- if(!txBusy && gotFrames){
- if(call Send.send(&DataPkt, sizeof(multiHopMsg)) == SUCCESS){
- #ifdef APP_SEND_DEBUG
- SODbg(DBG_USR2, "Scheduled sending packet # %u for %u secondsn", Scount, time);
- #endif
- txBusy = TRUE;
- } else{
- #ifdef APP_SEND_DEBUG
- SODbg(DBG_USR2, "Send Failed !!n");
- #endif
- }
- }
- return SUCCESS;
- }
- event result_t StatTimer.fired(){
- uint8_t i;
- time++;
- for(i = 0; i < MAX_NODES; i++){
- if(eachCount[i] != 0)
- SODbg(DBG_USR2, " %u : %un", (uint8_t) i, (uint16_t) eachCount[i]);
- }
- SODbg(DBG_USR2, "Rcount %u Scount %u EnergyTX %u EnergyRX %u EnergySP %un",
- Rcount, call GetDataSent(), call GetPower_tx(), call GetPower_rx(), call GetPower_sp());
- Rcount = 0;
- Scount = 0;
- call ResetPower();
- call ResetDataSent();
- for(i = 0; i < MAX_NODES; i++)
- eachCount[i] = 0;
- return SUCCESS;
- }
- event result_t RouteTimer.fired(){ // routes should be set up by now
- #ifdef DRAND_ENABLE
- call StatTimer.start(TIMER_ONE_SHOT, DRAND_TIME);
- call SubControl.start(); // so start drand
- #else
- switch(TOS_LOCAL_ADDRESS){
- case 0: // sink node
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 0, NULL, 50);
- #endif
- break;
- // fall through for rest of the nodes
- //********************************************
- case 21:
- case 22:
- case 37:
- case 38:
- case 31:
- case 32:
- case 15:
- case 17:
- case 40:
- case 41:
- case 42:
- case 43:
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 0, NULL, 50);
- #endif
- break;
- //********************************************
- case 2:
- case 3:
- case 9:
- case 10:
- case 39:
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 38, NULL, 50);
- #endif
- break;
- //********************************************
- case 24:
- case 27:
- case 28:
- case 8:
- case 20:
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 1, NULL, 50);
- #endif
- break;
- //********************************************
- case 1:
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 10, NULL, 50);
- #endif
- break;
- //********************************************
- case 5:
- case 14:
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 42, NULL, 50);
- #endif
- break;
- //********************************************
- case 18:
- case 19:
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- #ifndef BMAC_ENABLE
- call SetDrand.Set(NULL, 43, NULL, 50);
- #endif
- break;
- }
- call StatTimer.start(TIMER_REPEAT, STAT_TIME);
- #endif
- #ifdef APP_SEND_DEBUG
- // SODbg(DBG_USR2, "Routing finished with parent = %u!n", call RouteControl.getParent());
- #endif
- return SUCCESS;
- }
- #ifdef DRAND_ENABLE
- async event void Drand.gotFrame(){ // Drand over, start sending !!
- #ifdef APP_DEBUG
- nodeInfo *nbrInfo, *myInfo;
- uint8_t i;
- nbrInfo = call Drand.getNeighborInfo();
- for(i = 0; (i < MAX_NBR) && (nbrInfo[i].nodeID != 0xFF); i++)
- 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);
- }
- myInfo = call Drand.getMyInfo();
- SODbg(DBG_USR2, "MyInfo(FRAME) : %u %u %un", myInfo->nodeID, myInfo->slot, myInfo->frame);
- SODbg(DBG_USR2, "Parent = %un", call RouteControl.getParent());
- #endif
- gotFrames = TRUE;
- call SetDrand.Set(call Drand.getMyInfo(), call RouteControl.getParent(), call Drand.getNeighborInfo(),
- 50);
- }
- async event void Drand.helloOver(){}
- #endif
- }//end of implementation