分享

z-stack代码分析--SimpleApp实例2

 xingwangjy 2014-12-24

SimpleController.c中按键处理函数(灯,协调器或路由器)
/*********************************************************************
 * @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
//获得本设备的设备逻辑类型,如协调器、路由器或终端,并赋值给logicalType指针
        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

//获得本设备的设备启动方式,并赋值给startOptions指针
        zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
//把启动方式设成ZCD_STARTOPT_AUTO_START方式
        startOptions = ZCD_STARTOPT_AUTO_START;
        zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
//系统重启
        zb_SystemReset();

      }
      else//如不是开机初始状态
      {
        // Initiate a binding
//在10秒内允许绑定
        zb_AllowBind( myAllowBindTimeout );
      }
    }
    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);
        }

//获取设备启动方式,设成ZCD_STARTOPT_AUTO_START方式启动,并重启。
        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
      {
      }
    }
    if ( keys & HAL_KEY_SW_3 )
    {
    }
    if ( keys & HAL_KEY_SW_4 )
    {
    }
  }
}

//SimpleSwitch.c中按键处理函数(开关,终端)
/*********************************************************************
 * @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.
        // The Switch device is always an end-device
//设置设备逻辑类型为终端类型。
        logicalType = ZG_DEVICETYPE_ENDDEVICE;
        zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);

        // Do more configuration if necessary and then restart device with auto-start bit set
//获取设备启动方式,设成ZCD_STARTOPT_AUTO_START方式启动,并重启。
        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//如不是开机初始状态
      {
        // Initiate a binding with null destination
//建立绑定
        zb_BindDevice(TRUE, TOGGLE_LIGHT_CMD_ID, NULL);
      }
    }
    if ( keys & HAL_KEY_SW_2 )//摇杆右按
    {
      if ( myAppState == APP_INIT )//如是开机初始状态
      {
        // In the init state, keys are used to indicate the logical mode.
        // The Switch device is always an end-device
//设置设备逻辑类型为终端类型。
        logicalType = ZG_DEVICETYPE_ENDDEVICE;
        zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);

//获取设备启动方式,设成ZCD_STARTOPT_AUTO_START方式启动,并重启。
        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//如不是开机初始状态
      {
        // Send the command to toggle light
//发送控制灯数据
        zb_SendDataRequest( 0xFFFE, TOGGLE_LIGHT_CMD_ID, 0,
                        (uint8 *)NULL, myAppSeqNumber, 0, 0 );
      }
    }
    if ( keys & HAL_KEY_SW_3 )//摇杆下按
    {
      // Remove all existing bindings
//解绑定
      zb_BindDevice(FALSE, TOGGLE_LIGHT_CMD_ID, NULL);
    }
    if ( keys & HAL_KEY_SW_4 )
    {
    }
  }
}

//SimpleCollector.c中按键处理函数(采集器,协调器或路由器)
/*********************************************************************
 * @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
  {
//摇杆上按
//如是初始状态,以协调器启动
//如不是初始状态,允许绑定,并点亮LED1
    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 );
      }
    }
//摇杆右按
//如是初始状态,以路由器启动
//如不是初始状态,关闭允许绑定,并息灭LED1
    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 )
    {
    }
  }
}

//SimpleSensor.c中按键处理函数(传感器,终端)
/*********************************************************************
 * @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.
        // The Sensor device is always an end-device
        logicalType = ZG_DEVICETYPE_ENDDEVICE;
        zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);

        // Do more configuration if necessary and then restart device with auto-start bit set

        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();

      }
    }
//摇杆右按
//如是初始状态,以终端启动
    if ( keys & HAL_KEY_SW_2 )
    {
      if ( myAppState == APP_INIT )
      {
        // In the init state, keys are used to indicate the logical mode.
        // The Sensor device is always an end-device
        logicalType = ZG_DEVICETYPE_ENDDEVICE;
        zb_WriteConfiguration(ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType);

        // Do more configuration if necessary and then restart device with auto-start bit set

        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();
      }
    }
    if ( keys & HAL_KEY_SW_3 )
    {
    }
    if ( keys & HAL_KEY_SW_4 )
    {
    }
  }
}

/**************************************************
//SimpleSwitch.c和SimpleSensor.c中接收数据指示处理函数为空操作
******************************************************/
//SimpleController.c中接收数据指示处理函数
//切换LED1灯
/******************************************************************************
 * @fn          zb_ReceiveDataIndication
 *
 * @brief       The zb_ReceiveDataIndication callback function is called
 *              asynchronously by the ZigBee stack to notify the application
 *              when data is received from a peer device.
 *
 * @param       source - The short address of the peer device that sent the data
 *              command - The commandId associated with the data
 *              len - The number of bytes in the pData parameter
 *              pData - The data sent by the peer device
 *
 * @return      none
 */
void zb_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData  )
{
  if (command == TOGGLE_LIGHT_CMD_ID)
  {
    // Received application command to toggle the LED
    HalLedSet(HAL_LED_1, HAL_LED_MODE_TOGGLE);
  }
}

//SimpleCollector.c中接收数据指示处理函数
//接收电池数据或温度数据,数据转换成ASCII码形式保存
//如要在电脑里显示,还需写串口显示代码
/******************************************************************************
 * @fn          zb_ReceiveDataIndication
 *
 * @brief       The zb_ReceiveDataIndication callback function is called
 *              asynchronously by the ZigBee stack to notify the application
 *              when data is received from a peer device.
 *
 * @param       source - The short address of the peer device that sent the data
 *              command - The commandId associated with the data
 *              len - The number of bytes in the pData parameter
 *              pData - The data sent by the peer device
 *
 * @return      none
 */
CONST uint8 strDevice[] = "Device:0x";
CONST uint8 strTemp[] = "Temp: ";
CONST uint8 strBattery[] = "Battery: ";
void zb_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData  )
{
  uint8 buf[32];
  uint8 *pBuf;
  uint8 tmpLen;
  uint8 sensorReading;

  if (command == SENSOR_REPORT_CMD_ID)
  {
    // Received report from a sensor
    sensorReading = pData[1];

    // If tool available, write to serial port

    tmpLen = (uint8)osal_strlen( (char*)strDevice );
    pBuf = osal_memcpy( buf, strDevice, tmpLen );
    _ltoa( source, pBuf, 16 );
    pBuf += 4;
    *pBuf++ = ' ';

    if ( pData[0] == BATTERY_REPORT )
    {
      tmpLen = (uint8)osal_strlen( (char*)strBattery );
      pBuf = osal_memcpy( pBuf, strBattery, tmpLen );

      *pBuf++ = (sensorReading / 10 ) + '0';    // convent msb to ascii
      *pBuf++ = '.';                            // decimal point ( battery reading is in units of 0.1 V
      *pBuf++ = (sensorReading % 10 ) + '0';    // convert lsb to ascii
      *pBuf++ = ' ';
      *pBuf++ = 'V';
    }
    else
    {
      tmpLen = (uint8)osal_strlen( (char*)strTemp );
      pBuf = osal_memcpy( pBuf, strTemp, tmpLen );

      *pBuf++ = (sensorReading / 10 ) + '0';    // convent msb to ascii
      *pBuf++ = (sensorReading % 10 ) + '0';    // convert lsb to ascii
      *pBuf++ = ' ';
      *pBuf++ = 'C';
    }

    *pBuf++ = '\r';
    *pBuf++ = '\n';
    *pBuf = '\0';

#if defined( MT_TASK )
    debug_str( (uint8 *)buf );
#endif

    // can also write directly to uart

  }
}

/**************************************************
//SimpleController.c、SimpleSwitch.c、SimpleCollector.c和SimpleSensor.c中zb_FindDeviceConfirm发现设备确认函数为空操作
******************************************************/


/**************************************************
//SimpleController.c、SimpleCollector.c中zb_BindConfirm绑定确认函数为空操作
******************************************************/
//SimpleSwitch.c中zb_BindConfirm函数
//如绑定成功,点亮LED1
/******************************************************************************
 * @fn          zb_BindConfirm
 *
 * @brief       The zb_BindConfirm callback is called by the ZigBee stack
 *              after a bind operation completes.
 *
 * @param       commandId - The command ID of the binding being confirmed.
 *              status - The status of the bind operation.
 *
 * @return      none
 */
void zb_BindConfirm( uint16 commandId, uint8 status )
{

  if ( ( status == ZB_SUCCESS ) && ( myAppState == APP_START ) )
  {
    // Turn on LED 1
    HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
  }
}
//SimpleSensor.c中zb_BindConfirm函数
//如绑定成功,开始报告传感器数据
//如绑定不成功,启动MY_FIND_COLLECTOR_EVT定时器,定时时间为10秒,MY_FIND_COLLECTOR_EVT为查找采集器事件
/******************************************************************************
 * @fn          zb_BindConfirm
 *
 * @brief       The zb_BindConfirm callback is called by the ZigBee stack
 *              after a bind operation completes.
 *
 * @param       commandId - The command ID of the binding being confirmed.
 *              status - The status of the bind operation.
 *
 * @return      none
 */
void zb_BindConfirm( uint16 commandId, uint8 status )
{
  (void)commandId;

  if ( ( status == ZB_SUCCESS ) && ( myAppState == APP_START ) )
  {
    myAppState = APP_BOUND;

    //Start reporting sensor values
    myApp_StartReporting();
  }
  else
  {
    // Continue to discover a collector
    osal_start_timerEx( sapi_TaskID, MY_FIND_COLLECTOR_EVT, myBindRetryDelay );
  }
}

//SimpleSensor.c中myApp_StartReporting开始发送传感数据函数
/******************************************************************************
 * @fn          my_StartReporting
 *
 * @brief       Starts the process to periodically report sensor readings
 *
 * @param
 *
 * @return      none
 */
void myApp_StartReporting( void )
{
//启动报告温度定时器,定时时间为5秒
  osal_start_timerEx( sapi_TaskID, MY_REPORT_TEMP_EVT, myTempReportPeriod );
//启动报告电池状况定时器,定时时间为21秒
  osal_start_timerEx( sapi_TaskID, MY_REPORT_BATT_EVT, myBatteryCheckPeriod );
//点亮LED1
  HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );

}

/**************************************************
//SimpleController.c、SimpleCollector.c中zb_SendDataConfirm发送数据确认函数为空操作
******************************************************/
//SimpleSensor.c中zb_SendDataConfirm发送数据确认函数
//如发送不成功,解绑定,停止报告传感数据,启动查找采集器定时器(定时时间为1毫秒)
/******************************************************************************
 * @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 )
{
  (void)handle; // Intentionally unreferenced parameter

  if ( status != ZSuccess )
  {
    // Remove bindings to the existing collector
    zb_BindDevice( FALSE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL );

    myAppState = APP_START;
    myApp_StopReporting();

    // Start process of finding new collector with minimal delay
    osal_start_timerEx( sapi_TaskID, MY_FIND_COLLECTOR_EVT, 1 );
  }
  else
  {
    // send data ??
  }
}

//SimpleController.c、SimpleCollector.c和SimpleSwitch.c中zb_StartConfirm设备启动确认函数
//如启动网络成功,设置设备状态
//如启动网络不成功,启动MY_START_EVT启动设备加入网络定时器(定时时间为10秒)
/******************************************************************************
 * @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 )
  {
    myAppState = APP_START;
  }
  else
  {
    // Try again later with a delay
    osal_start_timerEx(sapi_TaskID, MY_START_EVT, myStartRetryDelay);
  }
}

//SimpleSensor.c中zb_StartConfirm设备启动确认函数
//如启动网络成功,启动MY_FIND_COLLECTOR_EVT查找采集器定时器(定时时间为10秒)
//如启动网络不成功,启动MY_START_EVT启动设备加入网络定时器(定时时间为10秒)
/******************************************************************************
 * @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 ( status == ZB_SUCCESS )
  {
    myAppState = APP_START;

    // Set event to bind to a collector
    osal_start_timerEx( sapi_TaskID, MY_FIND_COLLECTOR_EVT, myBindRetryDelay );
  }
  else
  {
    // Try joining again later with a delay
    osal_start_timerEx( sapi_TaskID, MY_START_EVT, myStartRetryDelay );
  }
}

/**************************************************
//SimpleSwitch.c、SimpleCollector.c、SimpleSensor.c中zb_AllowBindConfirm允许绑定确认函数为空操作
******************************************************/
//SimpleController.c中zb_AllowBindConfirm允许绑定确认函数
//LED1灯闪烁
/******************************************************************************
 * @fn          zb_AllowBindConfirm
 *
 * @brief       Indicates when another device attempted to bind to this device
 *
 * @param
 *
 * @return      none
 */
void zb_AllowBindConfirm( uint16 source )
{
  // Flash LED
  HalLedSet( HAL_LED_1, HAL_LED_MODE_BLINK );
}

/**************************************************
//SimpleController.c、SimpleCollector.c中zb_HandleOsalEvent处理Osal事件函数为空操作
******************************************************/
//SimpleSwitch.c中zb_HandleOsalEvent处理Osal事件函数
//如是MY_START_EVT事件,请求启动
/*****************************************************************************
 * @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 & MY_START_EVT )
  {
    zb_StartRequest();
  }

}

//SimpleSensor.c中zb_HandleOsalEvent处理Osal事件函数
//如是MY_START_EVT事件,请求启动
//如是MY_REPORT_TEMP_EVT事件,发送温度传感数据,启动报告温度数据定时器
//如是MY_REPORT_BATT_EVT事件,发送电池状况数据,启动报告电池数据定时器
//如是MY_FIND_COLLECTOR_EVT事件,绑定设备请求。
/*****************************************************************************
 * @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 pData[2];

  if ( event & MY_START_EVT )
  {
    zb_StartRequest();
  }
  if ( event & MY_REPORT_TEMP_EVT )
  {
    // Read and report temperature value
    pData[0] = TEMP_REPORT;
    pData[1] =  myApp_ReadTemperature();
    zb_SendDataRequest( 0xFFFE, SENSOR_REPORT_CMD_ID, 2, pData, 0, AF_ACK_REQUEST, 0 );
    osal_start_timerEx( sapi_TaskID, MY_REPORT_TEMP_EVT, myTempReportPeriod );
  }

  if ( event & MY_REPORT_BATT_EVT )
  {
    // Read battery value
    // If battery level low, report battery value
    pData[0] = BATTERY_REPORT;
    pData[1] =  myApp_ReadBattery();
    zb_SendDataRequest( 0xFFFE, SENSOR_REPORT_CMD_ID, 2, pData, 0, AF_ACK_REQUEST, 0 );
    osal_start_timerEx( sapi_TaskID, MY_REPORT_BATT_EVT, myBatteryCheckPeriod );
  }

  if ( event & MY_FIND_COLLECTOR_EVT )
  {
    // Find and bind to a collector device
    zb_BindDevice( TRUE, SENSOR_REPORT_CMD_ID, (uint8 *)NULL );
  }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约