/****************************************************************************** * @fn zb_StartConfirm * * @brief The zb_StartConfirm callback is called by the ZigBee stack * after a start request operation completes * * @param status - The status of the start operation. Status of * ZB_SUCCESS indicates the start operation completed * successfully. Else the status is an error code. * * @return none */ void zb_StartConfirm( uint8 status ) { // If the device sucessfully started, change state to running if ( status == ZB_SUCCESS ) { // Set LED 1 to indicate that node is operational on the network HalLedSet( HAL_LED_1, HAL_LED_MODE_ON ); // Change application state appState = APP_START; // Set event to bind to a collector osal_set_event( sapi_TaskID, MY_FIND_COLLECTOR_EVT ); // Store parent short address zb_GetDeviceInfo(ZB_INFO_PARENT_SHORT_ADDR, &parentShortAddr); // Turn ON Allow Bind mode infinitly zb_AllowBind( 0xFF ); HalLedSet( HAL_LED_2, HAL_LED_MODE_ON ); } else { // Try again later with a delay osal_start_timerEx( sapi_TaskID, MY_START_EVT, myStartRetryDelay ); } }
/****************************************************************************** * @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_SapiAllowBind * * @brief Process SAPI Allow Bind * * @param pBuf - pointer to received buffer * * @return none ***************************************************************************************************/ void MT_SapiAllowBind(uint8 *pBuf) { uint8 cmdId; /* parse header */ cmdId = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; zb_AllowBind(pBuf[0]); /* Build and send back the response */ MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SAPI), cmdId, 0, NULL); }
/********************************************************************* * @fn zb_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * EVAL_SW4 * EVAL_SW3 * EVAL_SW2 * EVAL_SW1 * * @return none */ void zb_HandleKeys( uint8 shift, uint8 keys ) { uint8 startOptions; uint8 logicalType; // Shift is used to make each button/switch dual purpose. if ( shift ) { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } else { if ( keys & HAL_KEY_SW_1 ) { if ( myAppState == APP_INIT ) { // In the init state, keys are used to indicate the logical mode. // Key 1 starts device as a coordinator zb_ReadConfiguration( ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType ); if ( logicalType != ZG_DEVICETYPE_ENDDEVICE ) { logicalType = ZG_DEVICETYPE_COORDINATOR; zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType); } // Do more configuration if necessary and then restart device with auto-start bit set // write endpoint to simple desc...dont pass it in start req..then reset zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions ); startOptions = ZCD_STARTOPT_AUTO_START; zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions ); zb_SystemReset(); } else { // Turn ON Allow Bind mode indefinitely zb_AllowBind( 0xFF ); HalLedSet( HAL_LED_1, HAL_LED_MODE_ON ); } } if ( keys & HAL_KEY_SW_2 ) { if ( myAppState == APP_INIT ) { // In the init state, keys are used to indicate the logical mode. // Key 2 starts device as a router zb_ReadConfiguration( ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType ); if ( logicalType != ZG_DEVICETYPE_ENDDEVICE ) { logicalType = ZG_DEVICETYPE_ROUTER; zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType); } zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions ); startOptions = ZCD_STARTOPT_AUTO_START; zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions ); zb_SystemReset(); } else { // Turn OFF Allow Bind mode indefinitely zb_AllowBind( 0x00 ); HalLedSet( HAL_LED_1, HAL_LED_MODE_OFF ); } } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } }
/****************************************************************************** * @fn zb_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * EVAL_SW4 * EVAL_SW3 * EVAL_SW2 * EVAL_SW1 * * @return none */ void zb_HandleKeys( uint8 shift, uint8 keys ) { static uint8 allowBind=FALSE; static uint8 allowJoin=TRUE; uint8 logicalType; // Shift is used to make each button/switch dual purpose. if ( shift ) { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } else { if ( keys & HAL_KEY_SW_1 ) { if ( appState == APP_INIT ) { // Key 1 starts device as a coordinator logicalType = ZG_DEVICETYPE_COORDINATOR; zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType); // Reset the device with new configuration zb_SystemReset(); } } if ( keys & HAL_KEY_SW_2 ) { allowBind ^= 1; if (allowBind) { // Turn ON Allow Bind mode infinitly zb_AllowBind( 0xFF ); HalLedSet( HAL_LED_2, HAL_LED_MODE_ON ); //This node is the gateway node isGateWay = TRUE; // Update the display #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "Gateway Mode", HAL_LCD_LINE_2 ); #endif } else { // Turn OFF Allow Bind mode infinitly zb_AllowBind( 0x00 ); HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF ); isGateWay = FALSE; // Update the display #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "Collector", HAL_LCD_LINE_2 ); #endif } } if ( keys & HAL_KEY_SW_3 ) { // Start reporting osal_set_event( sapi_TaskID, MY_REPORT_EVT ); } if ( keys & HAL_KEY_SW_4 ) { // Key 4 is used to control which routers // that can accept join requests allowJoin ^= 1; if(allowJoin) { NLME_PermitJoiningRequest(0xFF); } else { NLME_PermitJoiningRequest(0); } } #if 0 if( keys & HAL_KEY_SW_6 ) { if( onoff ) { st( P0_5 = !1; ); onoff = 0; } else { st(P0_5 = !!1; ); onoff = 1; }