/****************************************************************************** * @fn nwk_freqControl * * @brief Handle application requests received through IOCTL interface. * * input parameters * @param action - requested action * @param val - pointer to parameters required/returned for action * * output parameters * @param val - populated values if action was a retrieval action. * * @return status of operation. */ smplStatus_t nwk_freqControl(ioctlAction_t action, void *val) { smplStatus_t rc; switch (action) { case IOCTL_ACT_SET: #ifdef ACCESS_POINT broadcast_channel_change(((freqEntry_t *)val)->logicalChan); #endif /* ACCESS_POINT */ rc = nwk_setChannel((freqEntry_t *)val); break; case IOCTL_ACT_GET: nwk_getChannel((freqEntry_t *)val); rc = SMPL_SUCCESS; break; case IOCTL_ACT_SCAN: { ioctlScanChan_t *sc = (ioctlScanChan_t *)val; sc->numChan = nwk_scanForChannels(sc->freq); rc = SMPL_SUCCESS; } break; default: rc = SMPL_BAD_PARAM; break; } return rc; }
/*********************************************************************************** * @fn SMPL_Init * * @brief Initialize the SimpliciTI stack. * * input parameters * @param f - Pointer to call back function. Function called by NWK when * user application frame received. The callback is done in the * ISR thread. Argument is Link ID associated with frame. Function * returns 0 if frame is to be kept by NWK, otherwise 1. Frame * should be kept if application will do a SMPL_Receive() in the * user thread (recommended). Pointer may be NULL. * * output parameters * * @return Status of operation: * SMPL_SUCCESS * SMPL_NO_JOIN No Join reply. AP possibly not yet up. * SMPL_NO_CHANNEL Only if Frequency Agility enabled. Channel scan * failed. AP possibly not yet up. */ smplStatus_t SMPL_Init(uint8_t (*f)(linkID_t)) { smplStatus_t rc; if (!sInit_done) { /* set up radio. */ MRFI_Init(); /* initialize network */ if ((rc=nwk_nwkInit(f)) != SMPL_SUCCESS) { return rc; } MRFI_WakeUp(); #if defined( FREQUENCY_AGILITY ) { freqEntry_t chan; chan.logicalChan = 0; /* ok to set default channel explicitly now that MRFI initialized. */ nwk_setChannel(&chan); } #endif /* don't turn Rx on if we're an end device that isn't always on. */ /* but do turn the radio on for PLL operations */ #if !defined( END_DEVICE ) || defined( NWK_PLL ) MRFI_RxOn(); #endif #if defined( END_DEVICE ) /* All except End Devices are in promiscuous mode */ MRFI_SetRxAddrFilter((uint8_t *)nwk_getMyAddress()); MRFI_EnableRxAddrFilter(); #endif } sInit_done = 1; #ifdef NWK_PLL /* If the PLL is enabled then it must get running before the join * request or the system may lock up in the join request becuase * PLL is not locked in. */ // turn on the PLL SMPL_Ioctl(IOCTL_OBJ_PLL, IOCTL_ACT_ON, NULL); // reference clocks are by definition always locked. #ifndef NWK_PLL_REFERENCE_CLOCK // wait for a 5ms failure rate to be achieved while( nwk_pllIsLocked( 0 ) == false ) nwk_pllBackgrounder( false ); #endif #endif /* Join. if no AP or Join fails that status is returned. */ rc = nwk_join(); return rc; }
/******************************************************************************** * @fn change_channel_cmd * * @brief Change to channel specified in received frame. Polling devices * might be awake when the broadcast occurs but we want the channel * change recovery to occur in a disciplined manner using the * polling code. Also, with certain Test sceanrios in which a * sleeping device is emulated we want to emulate 'missing' the * broadcast change-channel command. * * input parameters * @param frame - pointer to frame containing target logical channel. * * @return none. */ static void change_channel_cmd(mrfiPacket_t *frame) { #if !defined( RX_POLLS ) freqEntry_t chan; chan.logicalChan = *(MRFI_P_PAYLOAD(frame)+F_APP_PAYLOAD_OS+F_CHAN_OS); nwk_setChannel(&chan); #endif return; }
/*********************************************************************************** * @fn SMPL_Init * * @brief Initialize the SimpliciTI stack. * * input parameters * @param f - Pointer to call back function. Function called by NWK when * user application frame received. The callback is done in the * ISR thread. Argument is Link ID associated with frame. Function * returns 0 if frame is to be kept by NWK, otherwise 1. Frame * should be kept if application will do a SMPL_Receive() in the * user thread (recommended). Pointer may be NULL. * * output parameters * * @return Status of operation: * SMPL_SUCCESS * SMPL_NO_JOIN No Join reply. AP possibly not yet up. * SMPL_NO_CHANNEL Only if Frequency Agility enabled. Channel scan * failed. AP possibly not yet up. */ smplStatus_t SMPL_Init(uint8_t (*f)(linkID_t)) { smplStatus_t rc; if (!sInit_done) { /* set up radio. */ MRFI_Init(); /* initialize network */ if ((rc=nwk_nwkInit(f)) != SMPL_SUCCESS) { return rc; } MRFI_WakeUp(); #if defined( FREQUENCY_AGILITY ) { freqEntry_t chan; chan.logicalChan = 0; /* ok to set default channel explicitly now that MRFI initialized. */ nwk_setChannel(&chan); } #endif /* don't turn Rx on if we're an end device that isn't always on. */ #if !defined( END_DEVICE ) MRFI_RxOn(); #endif #if defined( END_DEVICE ) /* All except End Devices are in promiscuous mode */ MRFI_SetRxAddrFilter((uint8_t *)nwk_getMyAddress()); MRFI_EnableRxAddrFilter(); #endif } sInit_done = 1; /* Join. if no AP or Join fails that status is returned. */ rc = nwk_join(); return rc; }
/****************************************************************************** * @fn nwk_scanForChannels * * @brief Scan for channels by sending a ping frame on each channel in the * channel table and listen for a reply. * * input parameters * @param channels - pointer to area to receive list of channels from which * ping replies were received. * * output parameters * @param channels - populated list of channels. * * @return statuis of operation.. */ uint8_t nwk_scanForChannels(freqEntry_t *channels) { uint8_t msg[FREQ_REQ_PING_FRAME_SIZE], i, num=0, notBcast = 1; addr_t *apAddr, retAddr; uint8_t radioState = MRFI_GetRadioState(); freqEntry_t chan; freqEntry_t curChan; union { ioctlRawSend_t send; ioctlRawReceive_t recv; } ioctl_info; nwk_getChannel(&curChan); /* send to AP. If we don't know AP address, broadcast. */ apAddr = (addr_t *)nwk_getAPAddress(); if (!apAddr) { apAddr = (addr_t *)nwk_getBCastAddress(); notBcast = 0; } for (i=0; i<NWK_FREQ_TBL_SIZE; ++i) { chan.logicalChan = i; nwk_setChannel(&chan); ioctl_info.send.addr = apAddr; ioctl_info.send.msg = msg; ioctl_info.send.len = sizeof(msg); ioctl_info.send.port = SMPL_PORT_FREQ; msg[FB_APP_INFO_OS] = FREQ_REQ_PING; msg[FB_TID_OS] = sTid; SMPL_Ioctl(IOCTL_OBJ_RAW_IO, IOCTL_ACT_WRITE, &ioctl_info.send); ioctl_info.recv.port = SMPL_PORT_FREQ; ioctl_info.recv.msg = msg; ioctl_info.recv.addr = &retAddr; 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)) { /* Once we know the Access Point we're related to we only accept * ping replies from that one. */ if (!notBcast || (notBcast && !memcmp(&retAddr, apAddr, NET_ADDR_SIZE))) { channels[num++].logicalChan = i; } } sTid++; if (num && notBcast) { /* we're done...only one possible channel if we know the AP address. */ break; } /* TODO: process encryption stuff */ } /* reset original channel */ nwk_setChannel(&curChan); return num; }
/************************************************************************************** * @fn SMPL_Receive * * @brief Receive a message from a peer application. * * input parameters * @param lid - Link ID (port) from application * * * output parameters * @param msg - pointer to where received message should be copied. * buffer should be of size == MAX_APP_PAYLOAD * @param len - pointer to receive length of received message * * @return Status of operation. * Caller should not use the value returned in 'len' to decide * whether there is a frame or not. It could be useful to the * Caller to distinguish between no frame and a frame with no data. * For example, in the polling case a frame with no application payload * is the way the AP conveys that there are no frames waiting. * * SMPL_SUCCESS * * SMPL_BAD_PARAM No valid Connection Table entry for Link ID * Data in Connection Table entry bad * SMPL_NO_FRAME No frame received. * SMPL_NO_PAYLOAD Frame received with no payload (not necessarily * an error and could be deduced by application * because the returned length will be 0) * * Polling device only: * * SMPL_TIMEOUT No response from Access Point * SMPL_NO_AP_ADDRESS Access Point address unknown * SMPL_TX_CCA_FAIL Could not send poll frame * SMPL_NOMEM No memory in output frame queue * SMPL_NO_CHANNEL Frequency Agility enabled and could not find channel */ smplStatus_t SMPL_Receive(linkID_t lid, uint8_t *msg, uint8_t *len) { connInfo_t *pCInfo = nwk_getConnInfo(lid); smplStatus_t rc = SMPL_BAD_PARAM; rcvContext_t rcv; if (!pCInfo || ((rc=nwk_checkConnInfo(pCInfo, CHK_RX)) != SMPL_SUCCESS)) { return rc; } rcv.type = RCV_APP_LID; rcv.t.lid = lid; #if defined(RX_POLLS) { uint8_t numChans = 1; #if defined(FREQUENCY_AGILITY) freqEntry_t chans[NWK_FREQ_TBL_SIZE]; uint8_t scannedB4 = 0; #endif do { uint8_t radioState = MRFI_GetRadioState(); /* I'm polling. Do the poll to stimulate the sending of a frame. If the * frame has application length of 0 it means there were no frames. If * no reply is received infer that the channel is changed. We then need * to scan and then retry the poll on each channel returned. */ if (SMPL_SUCCESS != (rc=nwk_poll(pCInfo->portRx, pCInfo->peerAddr))) { /* for some reason couldn't send the poll out. */ return rc; } /* do this before code block below which may reset it. */ numChans--; /* Wait until there's a frame. if the len is 0 then return SMPL_NO_FRAME * to the caller. In the poll case the AP always sends something. */ NWK_CHECK_FOR_SETRX(radioState); NWK_REPLY_DELAY(); NWK_CHECK_FOR_RESTORE_STATE(radioState); /* TODO: deal with pending */ rc = nwk_retrieveFrame(&rcv, msg, len, 0, 0); #if defined(FREQUENCY_AGILITY) if (SMPL_SUCCESS == rc) { /* we received something... */ return (*len) ? SMPL_SUCCESS : SMPL_NO_PAYLOAD; } /* No reply. scan for other channel(s) if we haven't already. Then set * one and try again. */ if (!scannedB4) { numChans = nwk_scanForChannels(chans); scannedB4 = 1; } if (numChans) { nwk_setChannel(&chans[numChans-1]); } #else /* FREQUENCY_AGILITY */ return (*len) ? rc : ((SMPL_SUCCESS == rc) ? SMPL_NO_PAYLOAD : SMPL_TIMEOUT); #endif } while (numChans); } #if defined(FREQUENCY_AGILITY) return SMPL_NO_CHANNEL; #endif #else /* RX_POLLS */ return nwk_retrieveFrame(&rcv, msg, len, 0, 0); #endif /* RX_POLLS */ }
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); } }
/****************************************************************************** * @fn nwk_join * * @brief Join functioanlity for non-AP devices. Send the Join token * and wait for the reply. * * input parameters * * output parameters * * @return Status of operation. */ smplStatus_t nwk_join(void) { uint8_t msg[JOIN_FRAME_SIZE]; uint32_t linkToken; addr_t apAddr; uint8_t radioState = MRFI_GetRadioState(); smplStatus_t rc = SMPL_NO_JOIN; union { ioctlRawSend_t send; ioctlRawReceive_t recv; } ioctl_info; #if defined( FREQUENCY_AGILITY ) uint8_t i, numChan; freqEntry_t channels[NWK_FREQ_TBL_SIZE]; if (!(numChan=nwk_scanForChannels(channels))) { return SMPL_NO_CHANNEL; } for (i=0; i<numChan; ++i) { nwk_setChannel(&channels[i]); #else { #endif ioctl_info.send.addr = (addr_t *)nwk_getBCastAddress(); ioctl_info.send.msg = msg; ioctl_info.send.len = sizeof(msg); ioctl_info.send.port = SMPL_PORT_JOIN; /* Put join token in */ nwk_putNumObjectIntoMsg((void *)&sJoinToken, msg+J_JOIN_TOKEN_OS, sizeof(sJoinToken)); /* set app info byte */ msg[JB_REQ_OS] = JOIN_REQ_JOIN; msg[JB_TID_OS] = sTid; /* Set number of connections supported. Used only by AP if it is * a data hub. */ msg[J_NUMCONN_OS] = NUM_CONNECTIONS; /* protocol version number */ msg[J_PROTOCOL_VERSION_OS] = nwk_getProtocolVersion(); SMPL_Ioctl(IOCTL_OBJ_RAW_IO, IOCTL_ACT_WRITE, &ioctl_info.send); ioctl_info.recv.port = SMPL_PORT_JOIN; ioctl_info.recv.msg = msg; ioctl_info.recv.addr = &apAddr; /* save AP address from reply */ 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)) { uint8_t firstByte = msg[JB_REQ_OS] & (~NWK_APP_REPLY_BIT); /* Sanity check for correct reply frame. Older version * has the length instead of the request as the first byte. */ if ((firstByte == JOIN_REQ_JOIN) || (firstByte == JOIN_REPLY_LEGACY_MSG_LENGTH) ) { /* join reply returns link token */ memcpy(&linkToken, msg+JR_LINK_TOKEN_OS, sizeof(linkToken)); nwk_setLinkToken(linkToken); /* save AP address */ nwk_setAPAddress(&apAddr); sTid++; /* guard against duplicates */ rc = SMPL_SUCCESS; #if defined( FREQUENCY_AGILITY ) break; #endif } } /* TODO: process encryption stuff */ } return rc; } #endif /* ACCESS_POINT */ /****************************************************************************** * @fn nwk_processJoin * * @brief Processes a Join frame. If this is a reply let it go to the * application. Otherwise generate and send the reply. * * input parameters * @param frame - Pointer to Join frame * * output parameters * * @return Keep frame for application, release frame, or replay frame. */ fhStatus_t nwk_processJoin(mrfiPacket_t *frame) { fhStatus_t rc = FHS_RELEASE; uint8_t replyType; /* Make sure this is a reply and see if we sent this. Validate the * packet for reception by client app. */ if (SMPL_MY_REPLY == (replyType=nwk_isValidReply(frame, sTid, JB_REQ_OS, JB_TID_OS))) { /* It's a match and it's a reply. Validate the received packet by * returning a 1 so it can be received by the client app. */ MRFI_PostKillSem(); rc = FHS_KEEP; } #if defined(ACCESS_POINT) else if (SMPL_A_REPLY == replyType) { /* No match. If I'm not an ED this is a reply that should be passed on. */ rc = FHS_REPLAY; } else { /* Send reply if we're an Access Point otherwise ignore the frame. */ if ((SMPL_NOT_REPLY == replyType) && sJoinOK) { handleJoinRequest(frame); } } #elif defined(RANGE_EXTENDER) else {