/****************************************************************************** * @fn isDupSandFFrame * * @brief Have we already stored this frame on behalf of a client? * * input parameters * @param frame - pointer to frame in question * * output parameters * * @return Returns 1 if the frame is a duplicate, otherwise 0. */ uint8_t isDupSandFFrame(mrfiPacket_t *frame) { uint8_t i, plLen = MRFI_GET_PAYLOAD_LEN(frame); frameInfo_t *fiPtr; /* check the input queue for duplicate S&F frame. */ fiPtr = nwk_getQ(INQ); for (i=0; i<SIZE_INFRAME_Q; ++i, fiPtr++) { if (FI_INUSE_UNTIL_FWD == fiPtr->fi_usage) { /* compare everything except the DEVICE INFO byte. */ if (MRFI_GET_PAYLOAD_LEN(&fiPtr->mrfiPkt) == plLen && !memcmp(MRFI_P_DST_ADDR(&fiPtr->mrfiPkt), MRFI_P_DST_ADDR(frame), NET_ADDR_SIZE) && !memcmp(MRFI_P_SRC_ADDR(&fiPtr->mrfiPkt), MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE) && !memcmp(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), MRFI_P_PAYLOAD(frame), 1) && !memcmp(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt)+F_TRACTID_OS, MRFI_P_PAYLOAD(frame)+F_TRACTID_OS, plLen-F_TRACTID_OS) ) { return 1; } } } return 0; }
/****************************************************************************** * @fn nwk_replayFrame * * @brief Deal with hop count on a Range Extender or Access Point replay. * Queue entry usage always left as available when done. * * input parameters * @param pFrameInfo - pointer to frame information structure * * output parameters * * @return void */ void nwk_replayFrame(frameInfo_t *pFrameInfo) { uint8_t hops = GET_FROM_FRAME(MRFI_P_PAYLOAD(&pFrameInfo->mrfiPkt), F_HOP_COUNT); /* if hops are zero, drop frame. othewise send it. */ if (hops--) { PUT_INTO_FRAME(MRFI_P_PAYLOAD(&pFrameInfo->mrfiPkt),F_HOP_COUNT,hops); /* Don't care if the Tx fails because of TO. Either someone else * will retransmit or the application itself will recover. */ #if defined(SMPL_SECURE) /* If the frame was targeted to a NWK port it was decrypted on spec in * the 'dispatchFrame()' method. It must be re-encypted in this case. */ if (GET_FROM_FRAME(MRFI_P_PAYLOAD(&pFrameInfo->mrfiPkt), F_PORT_OS) <= SMPL_PORT_NWK_BCAST) { nwk_setSecureFrame(&pFrameInfo->mrfiPkt, MRFI_GET_PAYLOAD_LEN(&pFrameInfo->mrfiPkt)-F_APP_PAYLOAD_OS, 0); } #endif MRFI_DelayMs(1); nwk_sendFrame(pFrameInfo, MRFI_TX_TYPE_CCA); } else { pFrameInfo->fi_usage = FI_AVAILABLE; } return; }
/****************************************************************************** * @fn MRFI_RxCompleteISR * * @brief This function is called by the ISR of MRFI at RF receive and MUST * be included in all applications. It should be as short as possible * to avoid lengthy processing in the ISR. * * @param none * * @return void */ void MRFI_RxCompleteISR() { uint8 pktType; MRFI_Receive(&pkt); pktType= MRFI_P_PAYLOAD(&pkt)[1]; if (MRFI_GET_PAYLOAD_LEN(&pkt)==2 && pktType==MRFI_LINK_ACK) { fAckRdy= TRUE; } else if (pktType==MRFI_LINK_DATA) { mrfiPktRdy= TRUE; } }
static fhStatus_t handleLinkRequest(mrfiPacket_t *frame) { fhStatus_t rc = FHS_RELEASE; uint8_t isReplySent; if (LINK_LEGACY_MSG_LENGTH == (MRFI_GET_PAYLOAD_LEN(frame) - F_APP_PAYLOAD_OS)) { /* Legacy frame. Spoof a link request */ *(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS) = LINK_REQ_LINK; } switch (*(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS)) { case LINK_REQ_LINK: isReplySent = smpl_send_link_reply(frame); #if !defined(END_DEVICE) /* If I am an AP or RE and not listening I need to replay frame. * The exception is if I am an AP or RE hosting an End Device * object and I just sent a reply frame to a duplicate link frame * for which I was not listening. In this case don't replay. */ if (!sListenActive && (SENT_REPLY != isReplySent)) { rc = FHS_REPLAY; } #endif /* !END_DEVICE */ break; #if defined(EXTENDED_API) case LINK_REQ_UNLINK: smpl_send_unlink_reply(frame); break; #endif default: break; } /* keep compiler happy if I'm compiled as an End Device */ (void) isReplySent; return rc; }
/****************************************************************************** * @fn handleJoinRequest * * @brief Dispatches handler for specfic join request * * input parameters * * @param frame - Join frame received * * output parameters * * @return void */ static void handleJoinRequest(mrfiPacket_t *frame) { if (JOIN_LEGACY_MSG_LENGTH == (MRFI_GET_PAYLOAD_LEN(frame) - F_APP_PAYLOAD_OS)) { /* Legacy frame. Spoof a join request */ *(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS) = JOIN_REQ_JOIN; } switch (*(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS)) { case JOIN_REQ_JOIN: smpl_send_join_reply(frame); break; default: break; } return; }
/****************************************************************************** * @fn mrfiLinkRecv * * @brief Read data from the RX buffer * * @param pBuf - buffer for storage of received data * * @return Number of bytes received * */ uint8 mrfiLinkRecv(uint8 *pBuf) { uint8 n; if (mrfiPktRdy) { uint8 v, pktType; // Process the packet v= halIntLock(); n= MRFI_GET_PAYLOAD_LEN(&pkt)-2; seqRecv= MRFI_P_PAYLOAD(&pkt)[0]; pktType= MRFI_P_PAYLOAD(&pkt)[1]; memcpy(pBuf,MRFI_P_PAYLOAD(&pkt)+2,n); mrfiPktRdy= FALSE; if ( pktType==MRFI_LINK_DATA && n>0) sendAck(); halIntUnlock(v); } else { n= 0; } return n; }
/****************************************************************************** * @fn dispatchFrame * * @brief Received frame looks OK so far. Dispatch to either NWK app by * invoking the handler or the user's app by simply leaving the * frame in the queue and letting the app poll the port. * * input parameters * @param fiPtr - frameInfo_t pointer to received frame * * output parameters * * @return void */ static void dispatchFrame(frameInfo_t *fiPtr) { uint8_t port = GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_PORT_OS); uint8_t nwkAppSize = sizeof(func)/sizeof(func[0]); fhStatus_t rc; linkID_t lid; #if defined(ACCESS_POINT) uint8_t loc; #endif #if !defined(END_DEVICE) uint8_t isForMe; #endif /* be sure it's not an echo... */ if (!memcmp(MRFI_P_SRC_ADDR(&fiPtr->mrfiPkt), sMyAddr, NET_ADDR_SIZE)) { fiPtr->fi_usage = FI_AVAILABLE; return; } /* Make sure encyrption bit conforms to our security support context. */ #if defined(SMPL_SECURE) if (!(GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_ENCRYPT_OS))) { /* Encyrption bit is not on when when it should be */ fiPtr->fi_usage = FI_AVAILABLE; return; } #else if (GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_ENCRYPT_OS)) { /* Encyrption bit is on when when it should not be */ fiPtr->fi_usage = FI_AVAILABLE; return; } #endif /* SMPL_SECURE */ /* If it's a network application port dispatch to service routine. Dispose * of frame depending on return code. */ if (port && (port <= nwkAppSize)) { #if defined(SMPL_SECURE) /* Non-connection-based frame. We can decode here if it was encrypted */ if (!nwk_getSecureFrame(&fiPtr->mrfiPkt, MRFI_GET_PAYLOAD_LEN(&fiPtr->mrfiPkt) - F_SEC_CTR_OS, 0)) { fiPtr->fi_usage = FI_AVAILABLE; return; } #endif rc = func[port-1](&fiPtr->mrfiPkt); if (FHS_KEEP == rc) { fiPtr->fi_usage = FI_INUSE_UNTIL_DEL; } #if !defined(END_DEVICE) else if (FHS_REPLAY == rc) { /* an AP or an RE could be relaying a NWK application frame... */ nwk_replayFrame(fiPtr); } #endif else /* rc == FHS_RELEASE (default...) */ { fiPtr->fi_usage = FI_AVAILABLE; } return; } /* sanity check */ else if ((port != SMPL_PORT_USER_BCAST) && ((port < PORT_BASE_NUMBER) || (port > SMPL_PORT_STATIC_MAX))) { /* bogus port. drop frame */ fiPtr->fi_usage = FI_AVAILABLE; return; } /* At this point we know the target is a user app. If this is an end device * and we got this far save the frame and we're done. If we're an AP there * are 3 cases: it's for us, it's for s store-and-forward client, or we need * to replay the frame. If we're and RE and the frame didn't come from an RE * and it's not for us, replay the frame. */ #if defined(END_DEVICE) /* If we're s polling end device we only accept application frames from * the AP. This prevents duplicate reception if we happen to be on when * a linked peer sends. */ #if defined(RX_POLLS) if (F_TX_DEVICE_ED != GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_TX_DEVICE)) { if (nwk_isConnectionValid(&fiPtr->mrfiPkt, &lid)) { fiPtr->fi_usage = FI_INUSE_UNTIL_DEL; } else { fiPtr->fi_usage = FI_AVAILABLE; } } else { fiPtr->fi_usage = FI_AVAILABLE; } #else /* it's destined for a user app. */ if (nwk_isConnectionValid(&fiPtr->mrfiPkt, &lid)) { fiPtr->fi_usage = FI_INUSE_UNTIL_DEL; if (spCallback && spCallback(lid)) { fiPtr->fi_usage = FI_AVAILABLE; return; } } else { fiPtr->fi_usage = FI_AVAILABLE; } #endif /* RX_POLLS */ #else /* END_DEVICE */ /* We have an issue if the frame is broadcast to the UUD port. The AP (or RE) must * handle this frame as if it were the target in case there is an application * running that is listening on that port. But if it's a broadcast it must also be * replayed. It isn't enough just to test for the UUD port because it could be a * directed frame to another device. We must check explicitly for broadcast * destination address. */ isForMe = !memcmp(sMyAddr, MRFI_P_DST_ADDR(&fiPtr->mrfiPkt), NET_ADDR_SIZE); if (isForMe || ((port == SMPL_PORT_USER_BCAST) && !memcmp(nwk_getBCastAddress(), MRFI_P_DST_ADDR(&fiPtr->mrfiPkt), NET_ADDR_SIZE))) { /* The folllowing test will succeed for the UUD port regardless of the * source address. */ if (nwk_isConnectionValid(&fiPtr->mrfiPkt, &lid)) { /* If this is for the UUD port and we are here then the device is either * an AP or an RE. In either case it must replay the UUD port frame if the * frame is not "for me". But it also must handle it since it could have a * UUD-listening application. Do the reply first and let the subsequent code * correctly set the frame usage state. Note that the routine return can be * from this code block. If not it will drop through to the bottom without * doing a replay. */ /* Do I need to replay it? */ if (!isForMe) { /* must be a broadcast for the UUD port */ nwk_replayFrame(fiPtr); } /* OK. Now I handle it... */ fiPtr->fi_usage = FI_INUSE_UNTIL_DEL; if (spCallback && spCallback(lid)) { fiPtr->fi_usage = FI_AVAILABLE; return; } } else { fiPtr->fi_usage = FI_AVAILABLE; } } #if defined( ACCESS_POINT ) /* Check to see if we need to save this for a S and F client. Otherwise, * if it's not for us, get rid of it. */ else if (nwk_isSandFClient(MRFI_P_DST_ADDR(&fiPtr->mrfiPkt), &loc)) { /* Don't bother if it is a duplicate frame or if it's a forwarded frame * echoed back from an RE. */ if (!isDupSandFFrame(&fiPtr->mrfiPkt) && !(GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_FWD_FRAME)) ) { #if defined(APP_AUTO_ACK) /* Make sure ack request bit is off. Sender will have gone away. */ PUT_INTO_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_ACK_REQ, 0); #endif fiPtr->fi_usage = FI_INUSE_UNTIL_FWD; } else { fiPtr->fi_usage = FI_AVAILABLE; } } else if (GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_TX_DEVICE) == F_TX_DEVICE_AP) { /* I'm an AP and this frame came from an AP. Don't replay. */ fiPtr->fi_usage = FI_AVAILABLE; } #elif defined( RANGE_EXTENDER ) else if (GET_FROM_FRAME(MRFI_P_PAYLOAD(&fiPtr->mrfiPkt), F_TX_DEVICE) == F_TX_DEVICE_RE) { /* I'm an RE and this frame came from an RE. Don't replay. */ fiPtr->fi_usage = FI_AVAILABLE;//////ojooooo descomentar!!! //nwk_replayFrame(fiPtr);//ojoooooooooo borrar!!! } #endif else { /* It's not for me and I'm either an AP or I'm an RE and the frame * didn't come from an RE. Replay the frame. */ nwk_replayFrame(fiPtr); } #endif /* !END_DEVICE */ return; }
/****************************************************************************** * @fn nwk_retrieveFrame * * @brief Retrieve frame from Rx frame queue. Invoked by application-level * code either app through SMPL_Receive() or IOCTL through raw Rx. This * should run in a user thread, not an ISR thread. * * input parameters * @param port - port on which to get a frame * * output parameters * @param msg - pointer to where app payload should be copied. Buffer * allocated should be == MAX_APP_PAYLOAD. * * @param len - pointer to where payload length should be stored. Caller * can check for non-zero when polling the port. initialized * to 0 even if no frame is retrieved. * @param srcAddr - if non-NULL, a pointer to where to copy the source address * of the retrieved message. * @param hopCount - if non-NULL, a pointer to where to copy the hop count of the retrieved message. * * @return SMPL_SUCCESS * SMPL_NO_FRAME - no frame found for specified destination * SMPL_BAD_PARAM - no valid connection info for the Link ID * */ smplStatus_t nwk_retrieveFrame(rcvContext_t *rcv, uint8_t *msg, uint8_t *len, addr_t *srcAddr, uint8_t *hopCount) { frameInfo_t *fPtr; uint8_t done; do { /* look for a frame on requested port. */ *len = 0; done = 1; fPtr = nwk_QfindOldest(INQ, rcv, USAGE_NORMAL); if (fPtr) { connInfo_t *pCInfo = 0; if (RCV_APP_LID == rcv->type) { pCInfo = nwk_getConnInfo(rcv->t.lid); if (!pCInfo) { return SMPL_BAD_PARAM; } #if defined(SMPL_SECURE) /* decrypt here...we have all the context we need. */ { uint32_t ctr = pCInfo->connRxCTR; uint32_t *pctr = &ctr; uint8_t len = MRFI_GET_PAYLOAD_LEN(&fPtr->mrfiPkt) - F_SEC_CTR_OS; if (pCInfo->thisLinkID == SMPL_LINKID_USER_UUD) { pctr = NULL; } #if defined(RX_POLLS) else if ((F_APP_PAYLOAD_OS - F_SEC_CTR_OS) == len) { /* This was an empty poll reply frame generated by the AP. * It uses the single-byte CTR value like network applications. * We do not want to use the application layer counter in this case. */ pctr = NULL; } #endif if (nwk_getSecureFrame(&fPtr->mrfiPkt, len, pctr)) { if (pctr) { /* Update connection's counter. */ pCInfo->connRxCTR = ctr; } } else { /* Frame bogus. Check for another frame. */ done = 0; continue; } } #endif /* SMPL_SECURE */ } /* it's on the requested port. */ *len = MRFI_GET_PAYLOAD_LEN(&fPtr->mrfiPkt) - F_APP_PAYLOAD_OS; memcpy(msg, MRFI_P_PAYLOAD(&fPtr->mrfiPkt)+F_APP_PAYLOAD_OS, *len); /* save signal info */ if (pCInfo) { /* Save Rx metrics... */ pCInfo->sigInfo.rssi = fPtr->mrfiPkt.rxMetrics[MRFI_RX_METRICS_RSSI_OFS]; pCInfo->sigInfo.lqi = fPtr->mrfiPkt.rxMetrics[MRFI_RX_METRICS_CRC_LQI_OFS]; } if (srcAddr) { /* copy source address if requested */ memcpy(srcAddr, MRFI_P_SRC_ADDR(&fPtr->mrfiPkt), NET_ADDR_SIZE); } if (hopCount) { /* copy hop count if requested */ *hopCount = GET_FROM_FRAME(MRFI_P_PAYLOAD(&fPtr->mrfiPkt), F_HOP_COUNT); } /* input frame no longer needed. free it. */ nwk_QadjustOrder(INQ, fPtr->orderStamp); fPtr->fi_usage = FI_AVAILABLE; return SMPL_SUCCESS; } } while (!done); return SMPL_NO_FRAME; }
smplStatus_t nwk_ping(linkID_t lid) { connInfo_t *pCInfo = nwk_getConnInfo(lid); smplStatus_t rc = SMPL_BAD_PARAM; uint8_t done = 0; uint8_t repeatIt = 2; uint8_t msg[MAX_PING_APP_FRAME]; uint8_t radioState = MRFI_GetRadioState(); union { ioctlRawSend_t send; ioctlRawReceive_t recv; } ioctl_info; if (!pCInfo || (SMPL_LINKID_USER_UUD == lid)) { /* either link ID bogus or tried to ping the unconnected user datagram link ID. */ return rc; } do { #if defined(FREQUENCY_AGILITY) && !defined(ACCESS_POINT) uint8_t i, numChan; freqEntry_t channels[NWK_FREQ_TBL_SIZE]; if (repeatIt == 2) { /* If FA enabled, first time through set up so that the 'for' * loop checks the current channel. This saves time (no scan) * and is very likely to succeed. Populate the proper strucure. */ SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_GET, channels); numChan = 1; } else { /* If we get here we must scan for the channel we're now on */ if (!(numChan = nwk_scanForChannels(channels))) { return SMPL_NO_CHANNEL; } } /* Either we scan next time through or we're done */ repeatIt--; /* this loop Pings on each channel (probably only 1) looking * for peer. */ for (i = 0; i < numChan && !done; ++i) { nwk_setChannel(&channels[i]); #else { repeatIt = 0; #endif /* defined(FREQUENCY_AGILITY) && !defined(ACCESS_POINT) */ ioctl_info.send.addr = (addr_t *)pCInfo->peerAddr; ioctl_info.send.msg = msg; ioctl_info.send.len = sizeof(msg); ioctl_info.send.port = SMPL_PORT_PING; /* fill in msg */ msg[PB_REQ_OS] = PING_REQ_PING; msg[PB_TID_OS] = sTid; SMPL_Ioctl(IOCTL_OBJ_RAW_IO, IOCTL_ACT_WRITE, &ioctl_info.send); ioctl_info.recv.port = SMPL_PORT_PING; ioctl_info.recv.msg = msg; ioctl_info.recv.addr = 0; NWK_CHECK_FOR_SETRX(radioState); NWK_REPLY_DELAY(); NWK_CHECK_FOR_RESTORE_STATE(radioState); if (SMPL_SUCCESS == SMPL_Ioctl(IOCTL_OBJ_RAW_IO, IOCTL_ACT_READ, &ioctl_info.recv)) { repeatIt = 0; done = 1; sTid++; /* guard against duplicates */ } } } while (repeatIt); return done ? SMPL_SUCCESS : SMPL_TIMEOUT; } /****************************************************************************** * @fn smpl_send_ping_reply * * @brief Send a reply to a ping request. * * input parameters * @param frame - pointer to frame containing request * * output parameters * * @return void */ static void smpl_send_ping_reply(mrfiPacket_t *frame) { frameInfo_t *pOutFrame; /* Build the reply frame. The application payload is the one included in the * received frame payload. */ if (pOutFrame = nwk_buildFrame(SMPL_PORT_PING, MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS, MRFI_GET_PAYLOAD_LEN(frame) - F_APP_PAYLOAD_OS, MAX_HOPS)) { /* destination address is the source adddress of the received frame. */ memcpy(MRFI_P_DST_ADDR(&pOutFrame->mrfiPkt), MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE); /* turn on the reply bit in the application payload */ *(MRFI_P_PAYLOAD(&pOutFrame->mrfiPkt) + F_APP_PAYLOAD_OS + PB_REQ_OS) |= NWK_APP_REPLY_BIT; #ifdef SMPL_SECURE nwk_setSecureFrame(&pOutFrame->mrfiPkt, MRFI_GET_PAYLOAD_LEN(frame) - F_APP_PAYLOAD_OS, 0); #endif /* SMPL_SECURE */ nwk_sendFrame(pOutFrame, MRFI_TX_TYPE_FORCED); } }
static uint8_t smpl_send_link_reply(mrfiPacket_t *frame) { #if NUM_CONNECTIONS > 0 frameInfo_t *pOutFrame; connInfo_t *pCInfo; uint8_t remotePort; uint8_t msg[LINK_REPLY_FRAME_SIZE]; /* Is this a legacy frame? If so continue. Otherwise check version.*/ if ((MRFI_GET_PAYLOAD_LEN(frame) - F_APP_PAYLOAD_OS) > LINK_LEGACY_MSG_LENGTH) { /* see if protocol version is correct... */ if (*(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + L_PROTOCOL_VERSION_OS) != nwk_getProtocolVersion()) { /* Accommodation of protocol version differences can be noted or accomplished here. * This field was also checked in the join transaction but it is checked again here * because that check may not have occurred if thre is no AP in this topology. * Otherwise, no match and the board goes back */ return SENT_NO_REPLY; } } /* see if token is correct */ { uint32_t lt; nwk_getNumObjectFromMsg(MRFI_P_PAYLOAD( frame) + F_APP_PAYLOAD_OS + L_LINK_TOKEN_OS, <, sizeof(lt)); if (lt != sLinkToken) { return SENT_NO_REPLY; } } /* if we get here the token matched. */ /* is this a duplicate request? */ remotePort = *(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + L_RMT_PORT_OS); if (pCInfo = nwk_isLinkDuplicate(MRFI_P_SRC_ADDR(frame), remotePort)) { /* resend reply */ msg[LB_REQ_OS] = LINK_REQ_LINK | NWK_APP_REPLY_BIT; /* sender's TID */ msg[LB_TID_OS] = *(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + LB_TID_OS); /* Send reply with the local port number so the remote device knows where to * send packets. */ msg[LR_RMT_PORT_OS] = pCInfo->portRx; /* put my Rx type in there. used to know how to set hops when sending back. */ msg[LR_MY_RXTYPE_OS] = nwk_getMyRxType(); # if defined(SMPL_SECURE) /* Set the Tx counter value for peer's Rx counter object */ nwk_putNumObjectIntoMsg((void *)&pCInfo->connTxCTR, (void *)&msg[LR_CTR_OS], 4); /* We also need to save the newly generated Rx counter value. */ nwk_getNumObjectFromMsg((void *)(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + L_CTR_OS), (void *)&pCInfo->connRxCTR, 4); # endif if (pOutFrame = nwk_buildFrame(SMPL_PORT_LINK, msg, sizeof(msg), MAX_HOPS - (GET_FROM_FRAME(MRFI_P_PAYLOAD(frame), F_HOP_COUNT)))) { /* destination address is the source adddress of the received frame. */ memcpy(MRFI_P_DST_ADDR(&pOutFrame->mrfiPkt), MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE); # if defined(SMPL_SECURE) nwk_setSecureFrame(&pOutFrame->mrfiPkt, sizeof(msg), 0); # endif /* SMPL_SECURE */ nwk_sendFrame(pOutFrame, MRFI_TX_TYPE_FORCED); } return SENT_REPLY; } if (!sListenActive) { /* We've checked for duplicate and resent reply. In that case we weren't listening * so just go back`. */ return SENT_NO_REPLY; } /* room to link? */ # if defined(AP_IS_DATA_HUB) pCInfo = nwk_findAlreadyJoined(frame); if (!pCInfo) # endif { pCInfo = nwk_getNextConnection(); } if (pCInfo) { /* yes there's room and it's not a dup. address. */ memcpy(&pCInfo->peerAddr, MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE); if (!nwk_allocateLocalRxPort(LINK_REPLY, pCInfo)) { nwk_freeConnection(pCInfo); /* we're done with the packet */ return SENT_REPLY; } /* The local Rx port is the one returned in the connection structure. The * caller is waiting on this to be set. The code here is running in an ISR * thread so the caller will see this change after RETI. */ if (NUM_CONNECTIONS == sNumLinkers) { /* Something is wrong -- no room to stack Link request */ nwk_freeConnection(pCInfo); /* we're done with the packet */ return SENT_REPLY; } sServiceLinkID[sNumLinkers++] = pCInfo->thisLinkID; /* save the remote Tx port */ pCInfo->portTx = remotePort; /* connection is valid... */ pCInfo->connState = CONNSTATE_CONNECTED; /* Set hop count. If it's a polling device set the count to the * distance to the AP. otherwise, set it to the max less the remaining * which will be the path taken for this frame. It will be no worse * then tha max and probably will be better. */ if (F_RX_TYPE_POLLS == *(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + L_MY_RXTYPE_OS)) { /* It polls. so. we'll be sending to the AP which will store the * frame. The AP is only MAX_HOPS_FROM_AP hops away from us. */ pCInfo->hops2target = MAX_HOPS_FROM_AP; } else { /* Can't really use this trick because the device could move. If the * devices are all static this may work unless the initial reception * was marginal. */ # if defined(DEVICE_DOES_NOT_MOVE) pCInfo->hops2target = MAX_HOPS - GET_FROM_FRAME(MRFI_P_PAYLOAD(frame), F_HOP_COUNT); # else pCInfo->hops2target = MAX_HOPS; # endif } /* Send reply with the local port number so the remote device knows where to * send packets. */ msg[LR_RMT_PORT_OS] = pCInfo->portRx; /* put my Rx type in there. used to know how to set hops when sending back. */ msg[LR_MY_RXTYPE_OS] = nwk_getMyRxType(); msg[LB_REQ_OS] = LINK_REQ_LINK | NWK_APP_REPLY_BIT; /* sender's TID */ msg[LB_TID_OS] = *(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + LB_TID_OS); # if defined(SMPL_SECURE) nwk_getNumObjectFromMsg((void *)(MRFI_P_PAYLOAD(frame) + F_APP_PAYLOAD_OS + L_CTR_OS), (void *)&pCInfo->connRxCTR, 4); pCInfo->connTxCTR = MRFI_RandomByte() | \ ((uint32_t)(MRFI_RandomByte()) << 8) | \ ((uint32_t)(MRFI_RandomByte()) << 16) | \ ((uint32_t)(MRFI_RandomByte()) << 24); nwk_putNumObjectIntoMsg((void *)&pCInfo->connTxCTR, (void *)&msg[LR_CTR_OS], 4); # endif if (pOutFrame = nwk_buildFrame(SMPL_PORT_LINK, msg, sizeof(msg), MAX_HOPS - (GET_FROM_FRAME(MRFI_P_PAYLOAD(frame), F_HOP_COUNT)))) { /* destination address is the source adddress of the received frame. */ memcpy(MRFI_P_DST_ADDR(&pOutFrame->mrfiPkt), MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE); # if defined(SMPL_SECURE) nwk_setSecureFrame(&pOutFrame->mrfiPkt, sizeof(msg), 0); # endif if (SMPL_SUCCESS != nwk_sendFrame(pOutFrame, MRFI_TX_TYPE_FORCED)) { /* better release the connection structure */ nwk_freeConnection(pCInfo); } } else { /* better release the connection structure */ nwk_freeConnection(pCInfo); } } /* we're done with the packet */ return SENT_REPLY; #else return SENT_NO_REPLY; #endif /* NUM_CONNECTIONS */ }
/****************************************************************************** * @fn smpl_send_join_reply * * @brief Send the Join reply. Include the Link token. If the device is * a polling sleeper put it into the list of store-and-forward * clients. * * input parameters * @param frame - join frame for which a reply is needed...maybe * * output parameters * * @return void */ static void smpl_send_join_reply(mrfiPacket_t *frame) { frameInfo_t *pOutFrame; uint8_t msg[JOIN_REPLY_FRAME_SIZE]; /* Is this a legacy frame? If so continue. Otherwise check verion.*/ if ((MRFI_GET_PAYLOAD_LEN(frame) - F_APP_PAYLOAD_OS) > JOIN_LEGACY_MSG_LENGTH) { /* see if protocol version is correct... */ if (*(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS+J_PROTOCOL_VERSION_OS) != nwk_getProtocolVersion()) { /* Accommodation of protocol version differences can be noted or accomplished here. * Otherwise, no match and the board goes back */ return; } } /* see if join token is correct */ { uint32_t jt; nwk_getNumObjectFromMsg(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS+J_JOIN_TOKEN_OS, &jt, sizeof(jt)); if (jt != sJoinToken) { return; } } /* send reply with tid, the link token, and the encryption context */ { uint32_t linkToken; nwk_getLinkToken(&linkToken); nwk_putNumObjectIntoMsg((void *)&linkToken, msg+JR_LINK_TOKEN_OS, sizeof(linkToken)); } msg[JR_CRYPTKEY_SIZE_OS] = SEC_CRYPT_KEY_SIZE; msg[JB_REQ_OS] = JOIN_REQ_JOIN | NWK_APP_REPLY_BIT; /* sender's tid... */ msg[JB_TID_OS] = *(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS+JB_TID_OS); if (pOutFrame = nwk_buildFrame(SMPL_PORT_JOIN, msg, sizeof(msg), MAX_HOPS_FROM_AP)) { /* destination address is the source adddress of the received frame. */ memcpy(MRFI_P_DST_ADDR(&pOutFrame->mrfiPkt), MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE); #ifdef AP_IS_DATA_HUB /* if source device supports ED objects save source address to detect duplicate joins */ if (*(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS+J_NUMCONN_OS)) { if (nwk_saveJoinedDevice(frame) && spCallback) { spCallback(0); } } #endif } else { /* oops -- no room left for Tx frame. Don't send reply. */ return; } /* If this device polls we need to provide store-and-forward support */ if (GET_FROM_FRAME(MRFI_P_PAYLOAD(frame),F_RX_TYPE) == F_RX_TYPE_POLLS) { uint8_t loc; /* Check duplicate status */ if (!nwk_isSandFClient(MRFI_P_SRC_ADDR(frame), &loc)) { uint8_t *pNumc = &spSandFContext->curNumSFClients; sfClientInfo_t *pClient = &spSandFContext->sfClients[*pNumc]; /* It's not a duplicate. Save it if there's room */ if (*pNumc < NUM_STORE_AND_FWD_CLIENTS) { memcpy(pClient->clientAddr.addr, MRFI_P_SRC_ADDR(frame), NET_ADDR_SIZE); *pNumc = *pNumc + 1; } else { /* No room left. Just return and don't send reply. */ return; } } else { /* We get here if it's a duplicate. We drop through and send reply. * Reset the S&F marker in the Management application -- we should * assume that the Client reset so the TID will be random. If this is * simply a duplicate frame it causes no harm. */ nwk_resetSFMarker(loc); } } #ifdef SMPL_SECURE nwk_setSecureFrame(&pOutFrame->mrfiPkt, sizeof(msg), 0); #endif /* SMPL_SECURE */ /* It's not S&F or it is but we're OK to send reply. */ nwk_sendFrame(pOutFrame, MRFI_TX_TYPE_FORCED); return; }