Exemplo n.º 1
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 )
{
  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 );
    }
  }
  
}
Exemplo n.º 2
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 & 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 );
  }
}
Exemplo n.º 3
0
/**
 * @brief   Esta función es llamada por el SO cuando se debe atender un
 *          evento de la tarea.
 * @param   event   Mascara de bits conteniendo los eventos a atender
 */
void zb_HandleOsalEvent(uint16 event)
{
    uint8 logicalType;
    
    if (event & SYS_EVENT_MSG)
    {      
    }
    if (event & ZB_ENTRY_EVENT)
    {  
        // inicializa UART
        initUart(uartRxCB);
        // blick LED 1 para indicar que se está uniendo a la red
        HalLedBlink(HAL_LED_1, 0, 50, 500 );
        HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF);   
        // lee tipo de logical device desde la memoria NV
        zb_ReadConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);
        // inicia la red
        zb_StartRequest();
    }
    if (event & MY_START_EVT)
    {
        // inicia la red
        zb_StartRequest();
    }
    if (event & SEND_UART_MSG)
    {
        // envia mensaje por UART a la PC
        //HalLcdWriteString("UART_EVT", HAL_LCD_LINE_1);
        sendUartMessage(&msgReport, &alarmFlags);
    }
    if (event & MY_TEST_EVT)
    {
        // crea mensaje de prueba
        msgReport.msgType = MSG_TYPE_REPORT;
        msgReport.sequence++;
        msgReport.lenData = MSG_LEN_DATA_REPORT;
        for (uint8 i = 0; i < 8; i++)
            msgReport.mac[i] = i;
        // datos de prueba
        static double d[10] = {220.5, 0.354, 85.12, 88.33, 85.06, 0.98, 1.23, 3.25, 0.97, 4.55};
        for (int i = 0; i < 10; i++)
            memcpy(&(msgReport.data[i*4]), &(d[i]), sizeof(double));
        // flags de prueba
        uint16 flags = 0;
        // imprime info en LCD
        HalLcdWriteString("TEST_EVT", HAL_LCD_LINE_1);
        HalLcdWriteStringValue("#", msgReport.sequence, 10, HAL_LCD_LINE_2);
        // envia mensaje por UART
        sendUartMessage(&msgReport, &flags);
        // crea evento nuevamente
        osal_start_timerEx(sapi_TaskID, MY_TEST_EVT, 5000);
    }
}
Exemplo n.º 4
0
/***************************************************************************************************
 * @fn          MT_SapiStart
 *
 * @brief       Process SAPI Start
 *
 * @param       pBuf - pointer to received buffer
 *
 * @return      none
 ***************************************************************************************************/
void MT_SapiStart(uint8 *pBuf)
{
  zb_StartRequest();

  /* Build and send back the response */
  MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SAPI), MT_SAPI_START_REQ, 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 & 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 );

  }
  
}
Exemplo n.º 6
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 & 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 );
  }
}
Exemplo n.º 7
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 & 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      SAPI_ProcessEvent
 *
 * @brief   Simple API Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  none
 */
UINT16 SAPI_ProcessEvent( byte task_id, UINT16 events )
{
  osal_event_hdr_t *pMsg;
  afIncomingMSGPacket_t *pMSGpkt;
  afDataConfirm_t *pDataConfirm;

  if ( events & SYS_EVENT_MSG )
  {
    pMsg = (osal_event_hdr_t *) osal_msg_receive( task_id );
    while ( pMsg )
    {
      switch ( pMsg->event )
      {
        case ZDO_CB_MSG:
          SAPI_ProcessZDOMsgs( (zdoIncomingMsg_t *)pMsg );
          break;

        case AF_DATA_CONFIRM_CMD:
          // This message is received as a confirmation of a data packet sent.
          // The status is of ZStatus_t type [defined in ZComDef.h]
          // The message fields are defined in AF.h
          pDataConfirm = (afDataConfirm_t *) pMsg;
          SAPI_SendDataConfirm( pDataConfirm->transID, pDataConfirm->hdr.status );
          break;

        case AF_INCOMING_MSG_CMD:
          pMSGpkt = (afIncomingMSGPacket_t *) pMsg;
          SAPI_ReceiveDataIndication( pMSGpkt->srcAddr.addr.shortAddr, pMSGpkt->clusterId,
                                    pMSGpkt->cmd.DataLength, pMSGpkt->cmd.Data);
          break;

        case ZDO_STATE_CHANGE:
          // If the device has started up, notify the application
          if (pMsg->status == DEV_END_DEVICE ||
              pMsg->status == DEV_ROUTER ||
              pMsg->status == DEV_ZB_COORD )
          {
            SAPI_StartConfirm( ZB_SUCCESS );
          }
          else  if (pMsg->status == DEV_HOLD ||
                  pMsg->status == DEV_INIT)
          {
            SAPI_StartConfirm( ZB_INIT );
          }
          break;

        case ZDO_MATCH_DESC_RSP_SENT:
          SAPI_AllowBindConfirm( ((ZDO_MatchDescRspSent_t *)pMsg)->nwkAddr );
          break;

        case KEY_CHANGE:
#if ( SAPI_CB_FUNC )
          zb_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );
#endif
          break;

        case SAPICB_DATA_CNF:
          SAPI_SendDataConfirm( (uint8)((sapi_CbackEvent_t *)pMsg)->data,
                                    ((sapi_CbackEvent_t *)pMsg)->hdr.status );
          break;

        case SAPICB_BIND_CNF:
          SAPI_BindConfirm( ((sapi_CbackEvent_t *)pMsg)->data,
                              ((sapi_CbackEvent_t *)pMsg)->hdr.status );
          break;

        case SAPICB_START_CNF:
          SAPI_StartConfirm( ((sapi_CbackEvent_t *)pMsg)->hdr.status );
          break;

        default:
          // User messages should be handled by user or passed to the application
          if ( pMsg->event >= ZB_USER_MSG )
          {

          }
          break;
      }

      // Release the memory
      osal_msg_deallocate( (uint8 *) pMsg );

      // Next
      pMsg = (osal_event_hdr_t *) osal_msg_receive( task_id );
    }

    // Return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  if ( events & ZB_ALLOW_BIND_TIMER )
  {
    afSetMatch(sapi_epDesc.simpleDesc->EndPoint, FALSE);
    return (events ^ ZB_ALLOW_BIND_TIMER);
  }

  if ( events & ZB_BIND_TIMER )
  {
    // Send bind confirm callback to application
    SAPI_BindConfirm( sapi_bindInProgress, ZB_TIMEOUT );
    sapi_bindInProgress = 0xffff;

    return (events ^ ZB_BIND_TIMER);
  }

  if ( events & ZB_ENTRY_EVENT )
  {
    uint8 startOptions;

    // Give indication to application of device startup
#if ( SAPI_CB_FUNC )
    zb_HandleOsalEvent( ZB_ENTRY_EVENT );
#endif

    // LED off cancels HOLD_AUTO_START blink set in the stack
    HalLedSet (HAL_LED_4, HAL_LED_MODE_OFF);

    zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
    if ( startOptions & ZCD_STARTOPT_AUTO_START )
    {
      zb_StartRequest();
    }
    else
    {
      // blink leds and wait for external input to config and restart
      HalLedBlink(HAL_LED_2, 0, 50, 500);
    }

    return (events ^ ZB_ENTRY_EVENT );
  }

  // This must be the last event to be processed
  if ( events & ( ZB_USER_EVENTS ) )
  {
    // User events are passed to the application
#if ( SAPI_CB_FUNC )
    zb_HandleOsalEvent( events );
#endif

    // Do not return here, return 0 later
  }

  // Discard unknown events
  return 0;
}