/***************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if( event & SYS_EVENT_MSG ) { } if( event & ZB_ENTRY_EVENT ) { // blind LED 1 to indicate joining a network HalLedBlink ( HAL_LED_1, 0, 50, 500 ); // Set Pin Port to output MCU_IO_DIR_OUTPUT_PREP(0, 0); MCU_IO_OUTPUT_PREP(0, 0, 0); // Set Button MCU_IO_DIR_INPUT_PREP(0, 1); MCU_IO_INPUT_PREP(0,1,MCU_IO_PULLDOWN); //MCU_IO_SET_HIGH(0, 1); // Start the device zb_StartRequest(); } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if ( appState == APP_REPORT ) { sendReport(); osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Delete previous binding if ( appState == APP_REPORT ) { zb_BindDevice( FALSE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL ); } appState = APP_BIND; // blink LED 2 to indicate discovery and binding HalLedBlink ( HAL_LED_2, 0, 50, 500 ); // Find and bind to a collector device zb_BindDevice( TRUE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL ); } }
/***************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if(event & SYS_EVENT_MSG) { } if( event & ZB_ENTRY_EVENT ) { // blind LED 1 to indicate joining a network #ifndef SYS_DEBUG_SH HalLedBlink ( HAL_LED_1, 0, 50, 500 ); #else HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON); #endif // Start the device zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if ( appState == APP_REPORT ) { sendReport(); osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Delete previous binding if ( appState==APP_REPORT ) { zb_BindDevice( FALSE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL ); } appState = APP_BIND; // blind LED 2 to indicate discovery and binding #ifndef SYS_DEBUG_SH HalLedBlink ( HAL_LED_2, 0, 50, 500 ); #else HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON); #endif // Find and bind to a collector device zb_BindDevice( TRUE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL ); } }
/****************************************************************************** * @fn zb_SendDataConfirm * * @brief The zb_SendDataConfirm callback function is called by the * ZigBee after a send data operation completes * * @param handle - The handle identifying the data transmission. * status - The status of the operation. * * @return none */ void zb_SendDataConfirm( uint8 handle, uint8 status ) { if( status != ZB_SUCCESS ) { if ( ++reportFailureNr >= REPORT_FAILURE_LIMIT ) { // Stop reporting osal_stop_timerEx( sapi_TaskID, MY_REPORT_EVT ); // After failure start reporting automatically when the device // is binded to a new gateway reportState = TRUE; // Delete previous binding zb_BindDevice( FALSE, ROUTER_REPORT_CMD_ID, (uint8 *)NULL ); // Try to bind a new gateway osal_start_timerEx( sapi_TaskID, MY_FIND_COLLECTOR_EVT, myBindRetryDelay ); reportFailureNr = 0; } } // status == SUCCESS else { // Reset failure counter reportFailureNr = 0; } }
/****************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if(event & SYS_EVENT_MSG) { } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if (appState == APP_BINDED) { } else { osal_set_event( sapi_TaskID, MY_FIND_COLLECTOR_EVT); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Find and bind to a gateway device (if this node is not gateway) zb_BindDevice( TRUE, DUMMY_REPORT_CMD_ID, (uint8 *)NULL ); } }
/****************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { uint8 logicalType; if(event & SYS_EVENT_MSG) { } if( event & ZB_ENTRY_EVENT ) { // Initialise UART initUart(uartRxCB); // blind LED 1 to indicate starting/joining a network #ifndef SYS_DEBUG_SH HalLedBlink ( HAL_LED_1, 0, 50, 500 ); #endif HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF ); // Read logical device type from NV zb_ReadConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType); // Start the device zb_StartRequest(); #ifdef SYS_DEBUG_SH // Turn ON Allow Bind mode infinitly zb_AllowBind( 0xFF ); HalLedSet( HAL_LED_2, HAL_LED_MODE_ON ); #endif } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if (isGateWay) { osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } else if (appState == APP_BINDED) { sendDummyReport(); osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // Find and bind to a gateway device (if this node is not gateway) if (!isGateWay) { zb_BindDevice( TRUE, DUMMY_REPORT_CMD_ID, (uint8 *)NULL ); } } }
/*************************************************************************************************** * @fn MT_SapiBindDevice * * @brief Process SAPI Bind Device Command * * @param pBuf - pointer to received buffer * * @return none ***************************************************************************************************/ void MT_SapiBindDevice(uint8 *pBuf) { uint8 cmdId; /* parse header */ cmdId = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; if (AddrMgrExtAddrValid(pBuf+3)) { zb_BindDevice(pBuf[0], BUILD_UINT16(pBuf[1], pBuf[2]), &pBuf[3]); } else { zb_BindDevice(pBuf[0], BUILD_UINT16(pBuf[1], pBuf[2]), (uint8 *)NULL); } /* Build and send back the response */ MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SAPI), cmdId, 0, NULL); }
/***************************************************************************** * @fn zb_HandleOsalEvent * * @brief The zb_HandleOsalEvent function is called by the operating * system when a task event is set * * @param event - Bitmask containing the events that have been set * * @return none */ void zb_HandleOsalEvent( uint16 event ) { if( event & SYS_EVENT_MSG ) { } if( event & ZB_ENTRY_EVENT ) { // blind LED 1 to indicate joining a network HalLedBlink ( HAL_LED_1, 0, 50, 500 ); // Start the device zb_StartRequest(); MCU_IO_DIR_OUTPUT(LED_PORT, LED_PIN); MCU_IO_SET_LOW(LED_PORT, LED_PIN); } if ( event & MY_START_EVT ) { zb_StartRequest(); } if ( event & MY_REPORT_EVT ) { if ( appState == APP_REPORT ) { osal_start_timerEx( sapi_TaskID, MY_REPORT_EVT, myReportPeriod ); } } if ( event & MY_FIND_COLLECTOR_EVT ) { // blind LED 2 to indicate discovery and binding HalLedBlink ( HAL_LED_2, 0, 50, 500 ); appState = APP_BIND; // Find and bind to a collector device zb_BindDevice( TRUE, ROUTER_REPORT_CMD_ID, (uint8 *)NULL ); } }