📄 multihoplepsm.nc
字号:
if (TOS_LOCAL_ADDRESS == BASE_STATION_ADDRESS) {
gpCurrentParent = &BaseStation;
gbCurrentHopCount = 0;
}
/*** HEED: clustering and control ******/
call EnergyControl.initPoints();
#ifdef CLUSTERING_ON
inClusteringProcess = FALSE;
was_CH = FALSE;
call Random.init();
#endif
return SUCCESS;
}
/**************************************************************/
command result_t StdControl.start() {
#ifdef CLUSTERING_ON
if (TOS_LOCAL_ADDRESS == BASE_STATION_ADDRESS) {
CH_type = FINAL_CH;
my_CH = BASE_STATION_ADDRESS;
my_CH_Cost = 0xFFFF;
call CC1000Control.SetRFPower(POW_INTER_CLUSTER);
}
else {
CH_type = NON_CH;
call CC1000Control.SetRFPower(POW_INTRA_CLUSTER);
}
#else
call CC1000Control.SetRFPower(POW_INTER_CLUSTER);
#endif
post Timer1Task();
call Timer1.start(TIMER_REPEAT,gUpdateInterval);
#ifdef CLUSTERING_ON
if (TOS_LOCAL_ADDRESS != BASE_STATION_ADDRESS) {
post Timer2Task();
call Timer2.start(TIMER_REPEAT,OPERATION_INTERVAL);
}
#endif
return SUCCESS;
}
/**************************************************************/
command result_t StdControl.stop() {
call Timer1.stop();
#ifdef CLUSTRING_ON
call Leds.redOff();
if (TOS_LOCAL_ADDRESS != BASE_STATION_ADDRESS)
call Timer2.stop();
#endif
return SUCCESS;
}
/**************************************************************/
command bool RouteSelect.isActive() {
#if 0
bool Result = FALSE;
if (gpCurrentParent != NULL) {
Result = TRUE;
}
return Result;
#endif
return TRUE;
}
/**************************************************************/
command result_t RouteSelect.selectRoute(TOS_MsgPtr Msg, uint8_t id) {
TOS_MHopMsg *pMHMsg = (TOS_MHopMsg *)&Msg->data[0];
uint8_t iNbr;
bool fIsDuplicate;
result_t Result = SUCCESS;
if (gpCurrentParent == NULL) {
// If the msg is locally generated, then send it to the broadcast address
// This is necessary to seed the network.
if ((pMHMsg->sourceaddr == TOS_LOCAL_ADDRESS) &&
(pMHMsg->originaddr == TOS_LOCAL_ADDRESS)) {
pMHMsg->sourceaddr = TOS_LOCAL_ADDRESS;
pMHMsg->hopcount = gbCurrentHopCount;
pMHMsg->seqno = gCurrentSeqNo++;
Msg->addr = TOS_BCAST_ADDR;
return SUCCESS;
}
else {
return FAIL;
}
}
if (gbCurrentHopCount >= pMHMsg->hopcount) {
// Possible cycle??
return FAIL;
}
if ((pMHMsg->sourceaddr == TOS_LOCAL_ADDRESS) &&
(pMHMsg->originaddr == TOS_LOCAL_ADDRESS)) {
fIsDuplicate = FALSE;
}
else {
fIsDuplicate = updateNbrCounters(pMHMsg->sourceaddr,pMHMsg->seqno,&iNbr);
}
if (!fIsDuplicate) {
pMHMsg->sourceaddr = TOS_LOCAL_ADDRESS;
pMHMsg->hopcount = gbCurrentHopCount;
if (gpCurrentParent->id != TOS_UART_ADDR) {
pMHMsg->seqno = gCurrentSeqNo++;
}
Msg->addr = gpCurrentParent->id;
}
else {
Result = FAIL;
}
return Result;
}
/**************************************************************/
command result_t RouteSelect.initializeFields(TOS_MsgPtr Msg, uint8_t id) {
TOS_MHopMsg *pMHMsg = (TOS_MHopMsg *)&Msg->data[0];
pMHMsg->sourceaddr = pMHMsg->originaddr = TOS_LOCAL_ADDRESS;
pMHMsg->hopcount = ROUTE_INVALID;
return SUCCESS;
}
command uint8_t* RouteSelect.getBuffer(TOS_MsgPtr Msg, uint16_t* Len) {
}
command uint16_t RouteControl.getParent() {
uint16_t addr;
addr = (gpCurrentParent != NULL) ? gpCurrentParent->id : 0xffff;
return addr;
}
#ifdef CLUSTERING_ON
command result_t RouteControl.isClusterHead() {
return is_CH();
}
command result_t RouteControl.isInClusteringProcess() {
return inClusteringProcess;
}
command result_t RouteControl.wasCH() {
return (inClusteringProcess && was_CH);
}
#endif
/******* HEED: energy control module ******/
command result_t EnergyControl.initPoints() {
availablePoints = MAX_REM_POWER;
overheadPoints = 0;
return SUCCESS;
}
command uint32_t EnergyControl.getRemainingPoints() {
return availablePoints;
}
command result_t EnergyControl.reducePoints() {
int reg_val = call CC1000Control.GetRFPower();
if (reg_val == POW_INTRA_CLUSTER)
reg_val = POW_COST_INTRA_CLUSTER;
else
reg_val = POW_COST_INTER_CLUSTER;
if (availablePoints > reg_val)
availablePoints -= reg_val;
else
availablePoints = 0;
return SUCCESS;
}
command result_t EnergyControl.addOverhead() {
int reg_val = call CC1000Control.GetRFPower();
if (reg_val == POW_INTRA_CLUSTER)
reg_val = POW_COST_INTRA_CLUSTER;
else
reg_val = POW_COST_INTER_CLUSTER;
overheadPoints += reg_val;
return SUCCESS;
}
command uint32_t EnergyControl.getOverhead() {
return overheadPoints;
}
command result_t EnergyControl.isAlive() {
return (availablePoints > MIN_REM_POWER);
}
/**********************************/
command uint8_t RouteControl.getQuality() {
uint8_t val;
val = (gpCurrentParent != NULL) ? gpCurrentParent->sendEst : 0x00;
return val;
}
command uint8_t RouteControl.getDepth() {
return gbCurrentHopCount;
}
command uint8_t RouteControl.getOccupancy() {
return 0;
}
command uint16_t RouteControl.getSender(TOS_MsgPtr msg) {
TOS_MHopMsg *pMHMsg = (TOS_MHopMsg *)msg->data;
return pMHMsg->sourceaddr;
}
command result_t RouteControl.setUpdateInterval(uint16_t Interval) {
result_t Result;
call Timer1.stop();
gUpdateInterval = (Interval * 1024); // * 1024 to make the math simpler
Result = call Timer1.start(TIMER_REPEAT,gUpdateInterval);
return Result;
}
command result_t RouteControl.manualUpdate() {
result_t Result;
Result = post Timer1Task();
return Result;
}
event result_t Timer1.fired() {
post Timer1Task();
return SUCCESS;
}
#ifdef CLUSTERING_ON
event result_t Timer2.fired() {
if (TOS_LOCAL_ADDRESS == BASE_STATION_ADDRESS)
return SUCCESS;
post Timer2Task();
return SUCCESS;
}
event result_t Timer3.fired() {
post Timer3Task();
return SUCCESS;
}
#endif
/**************************************************************/
event TOS_MsgPtr ReceiveMsg.receive(TOS_MsgPtr Msg) {
TOS_MHopMsg *pMHMsg = (TOS_MHopMsg *)&Msg->data[0];
RoutePacket *pRP = (RoutePacket *)&pMHMsg->data[0];
uint16_t saddr;
uint8_t i, iNbr;
#ifdef CLUSTERING_ON
uint8_t nbr_orig_state;
bool found;
#endif
saddr = pMHMsg->sourceaddr;
updateNbrCounters(saddr,pMHMsg->seqno,&iNbr);
NeighborTbl[iNbr].parent = pRP->parent;
NeighborTbl[iNbr].hop = pMHMsg->hopcount;
// NeighborTbl[iNbr].cost = pRP->cost;
#ifdef CLUSTERING_ON
nbr_orig_state = NeighborTbl[iNbr].CH_type;
NeighborTbl[iNbr].CH_type = pRP->CH_type;
#endif
// find out my address, extract the estimation
for (i = 0; i < pRP->estEntries; i++) {
if (pRP->estList[i].id == TOS_LOCAL_ADDRESS) {
NeighborTbl[iNbr].sendEst = pRP->estList[i].receiveEst;
NeighborTbl[iNbr].liveliness++;
}
}
/*** HEED: energy control ******/
if (!(call EnergyControl.isAlive())) {
turnLedsOff();
call StdControl.stop();
}
#ifdef CLUSTERING_ON
if (saddr == BASE_STATION_ADDRESS)
return Msg;
// populate the tentative or final cluster head variables
// if the cost is similar, choose the smaller ID
if (inClusteringProcess) {
if (pRP->CH_type == TENTATIVE_CH) {
if ((my_tent_CH == NON_CH) || (pRP->my_CH_Cost < my_tent_CH_Cost) ||
(pRP->my_CH_Cost==my_tent_CH_Cost && pRP->my_CH<my_tent_CH)) {
atomic {
my_tent_CH = pRP->my_CH;
my_tent_CH_Cost = pRP->my_CH_Cost;
}
}
}
else if (pRP->CH_type == FINAL_CH) {
i = 0; found = FALSE;
while (i<n_finalCH && !found) {
if (finalCH[i].addr == saddr)
found = TRUE;
else
i++;
}
finalCH[i].addr = saddr;
finalCH[i].cost = pRP->my_CH_Cost;
if (!found)
n_finalCH++;
}
else if (pRP->CH_type == NON_CH && my_tent_CH == (uint16_t)saddr) {
atomic {
my_tent_CH = NON_CH;
my_tent_CH_Cost = 0xFFFF;
}
}
}
else {
// if my cluster head is not one anymore, or if I am a CH and a neighbor
// CH announces it is not one anymore, trigger the clustering process
if (pRP->CH_type == NON_CH) {
if ((my_CH == saddr) || (CH_type == FINAL_CH && nbr_orig_state == FINAL_CH)) {
call Timer2.stop();
post Timer2Task();
call Timer2.start(TIMER_REPEAT,OPERATION_INTERVAL);
}
}
}
#endif
return Msg;
}
/**************************************************************/
event result_t Snoop.intercept[uint8_t id](TOS_MsgPtr Msg, void *Payload, uint16_t Len) {
TOS_MHopMsg *pMHMsg = (TOS_MHopMsg *)&Msg->data[0];
uint8_t iNbr;
updateNbrCounters(pMHMsg->sourceaddr,pMHMsg->seqno,&iNbr);
return SUCCESS;
}
/**************************************************************/
event result_t SendMsg.sendDone(TOS_MsgPtr pMsg, result_t success) {
gfSendRouteBusy = FALSE;
/*** HEED: energy control ******/
if (TOS_LOCAL_ADDRESS != BASE_STATION_ADDRESS) {
atomic {
uint32_t reg_val = call CC1000Control.GetRFPower();
if (reg_val == POW_INTRA_CLUSTER)
reg_val = POW_COST_INTRA_CLUSTER;
else
reg_val = POW_COST_INTER_CLUSTER;
overheadPoints += reg_val;
availablePoints -= reg_val;
}
if (!(call EnergyControl.isAlive())) {
turnLedsOff();
call StdControl.stop();
}
}
return SUCCESS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -