MultiHopTtestM.nc
上传用户:joranyuan
上传日期:2022-06-23
资源大小:3306k
文件大小:6k
- 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;
- #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, drandFirstTime;
- 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
- drandFirstTime = FALSE;
- 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(){
- if(!drandFirstTime && gotFrames){
- 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;
- } else if(drandFirstTime && gotFrames){ // drand just finished, start sending
- if(TOS_LOCAL_ADDRESS != 0){
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- }
- call StatTimer.start(TIMER_REPEAT, STAT_TIME);
- drandFirstTime = FALSE;
- }
- return SUCCESS;
- }
- event result_t RouteTimer.fired(){ // routes should be set up by now
- #ifdef DRAND_ENABLE // wait for drand
- call StatTimer.start(TIMER_ONE_SHOT, DRAND_TIME);
- call SubControl.start(); // so start drand
- drandFirstTime = TRUE;
- #else // start sending at once
- drandFirstTime = FALSE;
- if(TOS_LOCAL_ADDRESS == 0){
- call SetDrand.Set(NULL, 0, NULL, 50);
- } else{
- call SendTimer.start(TIMER_REPEAT, SEND_RATE);
- call SetDrand.Set(NULL, call RouteControl.getParent(), NULL, 50);
- }
- 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