📄 multihoplepsm.nc
字号:
// receiveEst may be zero (it's not sending any packets) for (i = 0;i < ROUTE_TABLE_SIZE;i++) { pNbr = &NeighborTbl[i]; if (!(pNbr->flags & NBRFLAG_VALID)) continue; if (pNbr->parent == TOS_LOCAL_ADDRESS) continue; if (pNbr->parent == ROUTE_INVALID) continue; if (pNbr->hop == ROUTE_INVALID) continue; if (pNbr->sendEst < 25) continue; if ((pNbr->hop != 0) && (pNbr->receiveEst < 25)) continue; ulNbrLinkCost = evaluateCost(pNbr->sendEst,pNbr->receiveEst); if ((pNbr->hop != 0) && (ulNbrLinkCost > MAX_ALLOWABLE_LINK_COST)) continue; if ((pNbr->hop < bNewHopCount) || ((pNbr->hop == bNewHopCount) && ulMinLinkCost > ulNbrLinkCost)) { ulMinLinkCost = ulNbrLinkCost; pNewParent = pNbr; bNewHopCount = pNbr->hop; } } if (pNewParent) { atomic { gpCurrentParent = pNewParent; gbCurrentHopCount = bNewHopCount + 1; } } } task void SendRouteTask() { TOS_MHopMsg *pMHMsg = (TOS_MHopMsg *) &routeMsg.data[0]; RoutePacket *pRP = (RoutePacket *)&pMHMsg->data[0]; struct SortEntry sortTbl[ROUTE_TABLE_SIZE]; uint8_t length = offsetof(TOS_MHopMsg,data) + offsetof(RoutePacket,estList); uint8_t maxEstEntries; uint8_t i,j; if (gfSendRouteBusy) { return; } dbg(DBG_ROUTE,"MultiHopLEPSM Sending route update msg.\n"); maxEstEntries = TOSH_DATA_LENGTH - length; maxEstEntries = maxEstEntries / sizeof(RPEstEntry); for (i = 0,j = 0;i < ROUTE_TABLE_SIZE; i++) { if (NeighborTbl[i].flags & NBRFLAG_VALID) { sortTbl[j].id = NeighborTbl[i].id; sortTbl[j].receiveEst = NeighborTbl[i].receiveEst; j++; } } qsort (sortTbl,j,sizeof(struct SortEntry),sortByReceiveEstFcn); pRP->parent = (gpCurrentParent) ? gpCurrentParent->id : ROUTE_INVALID; //pRP->hop = gbCurrentHopCount; //pRP->cost = cost; pRP->estEntries = (j > maxEstEntries) ? maxEstEntries : j; for (i = 0; i < pRP->estEntries; i++) { pRP->estList[i].id = sortTbl[i].id; pRP->estList[i].receiveEst = sortTbl[i].receiveEst; length += sizeof(RPEstEntry); } pMHMsg->sourceaddr = pMHMsg->originaddr = TOS_LOCAL_ADDRESS; pMHMsg->hopcount = gbCurrentHopCount; pMHMsg->seqno = gCurrentSeqNo++; if (call SendMsg.send(TOS_BCAST_ADDR, length, &routeMsg) == SUCCESS) { gfSendRouteBusy = TRUE; } } task void TimerTask() { dbg(DBG_ROUTE,"MultiHopLEPSM timer task.\n"); updateTable();#ifndef NDEBUG { int i; dbg(DBG_ROUTE,"\taddr\tprnt\tmisd\trcvd\tlstS\thop\trEst\tsEst\n"); for (i = 0;i < ROUTE_TABLE_SIZE;i++) { if (NeighborTbl[i].flags) { dbg(DBG_ROUTE,"\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", NeighborTbl[i].id, NeighborTbl[i].parent, NeighborTbl[i].missed, NeighborTbl[i].received, NeighborTbl[i].lastSeqno, NeighborTbl[i].hop, NeighborTbl[i].receiveEst, NeighborTbl[i].sendEst); } } if (gpCurrentParent) { dbg(DBG_ROUTE,"MultiHopLEPSM: Parent = %d\n",gpCurrentParent->id); } }#endif chooseParent(); post SendRouteTask(); } command result_t StdControl.init() { memset((void *)NeighborTbl,0,(sizeof(TableEntry) * ROUTE_TABLE_SIZE)); BaseStation.id = TOS_UART_ADDR; BaseStation.parent = TOS_UART_ADDR; BaseStation.flags = NBRFLAG_VALID; BaseStation.hop = 0; gpCurrentParent = NULL; gbCurrentHopCount = ROUTE_INVALID; gCurrentSeqNo = 0; gwEstTicks = 0; gUpdateInterval = DATA_TO_ROUTE_RATIO * DATA_FREQ; gfSendRouteBusy = FALSE; if (TOS_LOCAL_ADDRESS == BASE_STATION_ADDRESS) { gpCurrentParent = &BaseStation; gbCurrentHopCount = 0; } return SUCCESS; } command result_t StdControl.start() { post TimerTask(); call Timer.start(TIMER_REPEAT,gUpdateInterval);#ifdef USE_WATCHDOG call PoochHandler.start(); call WDT.start(gUpdateInterval * 5);#endif return SUCCESS; } command result_t StdControl.stop() { call Timer.stop(); 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; } 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 Timer.stop(); gUpdateInterval = (Interval * 1024); // * 1024 to make the math simpler Result = call Timer.start(TIMER_REPEAT,gUpdateInterval);#ifdef USE_WATCHDOG call PoochHandler.stop(); call PoochHandler.start(); call WDT.start(gUpdateInterval * 5);#endif return Result; } command result_t RouteControl.manualUpdate() { result_t Result; Result = post TimerTask(); return Result; } event result_t Timer.fired() { post TimerTask(); return SUCCESS; } 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 USE_WATCHDOG call WDT.reset();#endif saddr = pMHMsg->sourceaddr; updateNbrCounters(saddr,pMHMsg->seqno,&iNbr); // iNbr = findPreparedIndex(saddr); NeighborTbl[iNbr].parent = pRP->parent; NeighborTbl[iNbr].hop = pMHMsg->hopcount; // NeighborTbl[iNbr].cost = pRP->cost; // 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++; } } 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; return SUCCESS; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -