void appMain(void) { macProtocol.recvCb = macRecv; MosAddr dst; uint8_t counter = 0,i; uint16_t neighbor; // XXX: hardcoded addresses if (localAddress == 0x0236) { intToAddr(dst, 0x5b5a); } else { intToAddr(dst, 0x0236); } neighbor = addNeighbor(dst.shortAddr); while (true) { for (i=0;i<10;i++) { sendBuffer[0] = '0' + counter++; if (counter >= 10) counter = 0; int8_t result = macSend(&dst, sendBuffer, sizeof(sendBuffer)); if (result < 0) { PRINTF("mac send failed: %s\n", strerror(-result)); } else if (result < sizeof(sendBuffer)) { PRINTF("mac send: not all sent! (%u vs %u)\n", result, sizeof(sendBuffer)); } mdelay(1000); } PRINTF("sent:%d akcepted:%d => %d%%\t", linq[neighbor].sent, linq[neighbor].sentAck, (100*linq[neighbor].sentAck / linq[neighbor].sent)); PRINTF("recv:%d akcepted:%d => %d%%\n", linq[neighbor].recv, linq[neighbor].recvAck, (100*linq[neighbor].recvAck / linq[neighbor].recv)); } }
// send smth to address 'addr', port 'port' void networkingForwardData(MacInfo_t *macInfo, uint8_t *data, uint16_t len) { // PRINTF("commForwardData, len=%u\n", len); switch (routePacket(macInfo)) { case RD_DROP: // PRINTF("RD_DROP\n"); if (IS_LOCAL(macInfo)){ INC_NETSTAT(NETSTAT_PACKETS_DROPPED_TX, EMPTY_ADDR); } else{ INC_NETSTAT(NETSTAT_PACKETS_DROPPED_RX, EMPTY_ADDR); } break; case RD_LOCAL: // PRINTF("RD_LOCAL\n"); socketInputData(macInfo, data, len); break; case RD_UNICAST: // PRINTF("RD_UNICAST\n"); // force header rebuild macInfo->macHeaderLen = 0; #if PLATFORM_SADMOTE if (!IS_LOCAL(macInfo) && isDuplicate(macInfo, data, len)) { PRINTF("not forwarding, duplicate...\n"); break; } #endif if (IS_LOCAL(macInfo)) { INC_NETSTAT(NETSTAT_PACKETS_SENT, macInfo->originalDst.shortAddr); } macSendEx(macInfo, data, len); break; case RD_BROADCAST: // PRINTF("RD_BROADCAST\n"); if (!IS_LOCAL(macInfo)) { socketInputData(macInfo, data, len); } // and forward to all intToAddr(macInfo->originalDst, OSW_ADDR_BROADCAST); // force header rebuild macInfo->macHeaderLen = 0; INC_NETSTAT(NETSTAT_PACKETS_SENT, EMPTY_ADDR); macSendEx(macInfo, data, len); break; } }
RoutingDecision_e routePacket(MacInfo_t *info) { MosAddr *dst = &info->originalDst; // PRINTF("route: dst address=0x%04x, nexthop=0x%04x\n", dst->shortAddr, info->immedDst.shortAddr); // PRINTF(" localAddress=0x%04x\n", localAddress); if (IS_LOCAL(info)) { // fix root address if we are sending it to the root if (dst->shortAddr == MOS_ADDR_ROOT) { intToAddr(info->originalDst, rootAddress); // info->hoplimit = hopCountToRoot; info->hoplimit = MAX_HOP_COUNT; } } else { // PRINTF("route packet, addr=0x%04x port=%02x\n", dst->shortAddr, info->dstPort); uint8_t index = markAsSeen(info->immedSrc.shortAddr, false); if (index != 0xff) { uint32_t expectedTimeStart = 4000ul + MOTE_TIME_FULL * index + MOTE_TIME + (IS_ODD_COLLECTOR ? MOTE_TIME_FULL * MAX_MOTES : 0); uint32_t expectedTimeEnd = expectedTimeStart + MOTE_TIME; uint32_t now = timeSinceFrameStart(); if (now < expectedTimeStart || now > expectedTimeEnd) { TPRINTF("*** mote %#04x sends out of its time: expected at %lu (+0-512 ms), now %lu!\n", motes[index].address, expectedTimeStart, now); } } else { if (timeSinceFrameStart() > 4000) { TPRINTF("*** unregistered sender!\n"); } } } if (isLocalAddress(dst)) { INC_NETSTAT(NETSTAT_PACKETS_RECV, info->originalSrc.shortAddr); return RD_LOCAL; } if (isBroadcast(dst)) { fillLocalAddress(&info->immedSrc); // XXX if (!IS_LOCAL(info)){ INC_NETSTAT(NETSTAT_PACKETS_RECV, info->originalSrc.shortAddr); } // don't forward broadcast packets return IS_LOCAL(info) ? RD_BROADCAST : RD_LOCAL; } // check if hop limit allows the packet to be forwarded if (!checkHoplimit(info)) { PRINTF("hoplimit reached!\n"); return RD_DROP; } // fill address: may forward it fillLocalAddress(&info->immedSrc); if (dst->shortAddr == rootAddress) { if (isRoutingInfoValid()) { //PRINTF("using 0x%04x as nexthop to root\n", nexthopToRoot); if (!IS_LOCAL(info)) { #if PRECONFIGURED_NH_TO_ROOT if (info->immedDst.shortAddr != localAddress) { TPRINTF("Dropping, I'm not a nexthop for sender %#04x\n", info->originalSrc.shortAddr); INC_NETSTAT(NETSTAT_PACKETS_DROPPED_RX, EMPTY_ADDR); return RD_DROP; } #endif // if (info->hoplimit < hopCountToRoot){ // PRINTF("Dropping, can't reach host with left hopCounts\n"); // return RD_DROP; // } TPRINTF("****** Forwarding a packet to root for %#04x!\n", info->originalSrc.shortAddr); INC_NETSTAT(NETSTAT_PACKETS_FWD, nexthopToRoot); // delay a bit info->timeWhenSend = getSyncTimeMs() + randomNumberBounded(150); } else{ //INC_NETSTAT(NETSTAT_PACKETS_SENT, nexthopToRoot); // Done @ comm.c } info->immedDst.shortAddr = nexthopToRoot; return RD_UNICAST; } else { // PRINTF("root routing info not present or expired!\n"); TPRINTF("DROP\n"); return RD_DROP; } } if (IS_LOCAL(info)) { //INC_NETSTAT(NETSTAT_PACKETS_SENT, dst->shortAddr); // Done @ comm.c // send out even with an unknown nexthop, makes sense? return RD_UNICAST; } return RD_DROP; }