?? sapi.c
字號:
*/
void SAPI_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData )
{
#if defined ( MT_SAPI_CB_FUNC )
/* First check if MT has subscribed for this callback. If so , pass it as
a event to MonitorTest and return control to calling function after that */
if ( SAPICB_CHECK( SPI_CB_SAPI_RCV_DATA_IND ) )
{
zb_MTCallbackReceiveDataIndication( source, command, len, pData );
}
else
#endif //MT_SAPI_CB_FUNC
{
zb_ReceiveDataIndication( source, command, len, pData );
}
}
/*********************************************************************
* @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:
zb_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );
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
zb_HandleOsalEvent( ZB_ENTRY_EVENT );
// 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);
#if defined SENSOR
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();
#endif
}
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
zb_HandleOsalEvent( events );
// Do not return here, return 0 later
}
// Discard unknown events
return 0;
}
/*********************************************************************
* @fn SAPI_ProcessZDOMsgs()
*
* @brief Process response messages
*
* @param none
*
* @return none
*/
void SAPI_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
{
switch ( inMsg->clusterID )
{
case NWK_addr_rsp:
{
// Send find device callback to application
ZDO_NwkIEEEAddrResp_t *pNwkAddrRsp = ZDO_ParseAddrRsp( inMsg );
SAPI_FindDeviceConfirm( ZB_IEEE_SEARCH, (uint8*)&pNwkAddrRsp->nwkAddr, pNwkAddrRsp->extAddr );
}
break;
case Match_Desc_rsp:
{
zAddrType_t dstAddr;
ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
if ( sapi_bindInProgress != 0xffff )
{
// Create a binding table entry
dstAddr.addrMode = Addr16Bit;
dstAddr.addr.shortAddr = pRsp->nwkAddr;
if ( APSME_BindRequest( sapi_epDesc.simpleDesc->EndPoint,
sapi_bindInProgress, &dstAddr, pRsp->epList[0] ) == ZSuccess )
{
osal_stop_timerEx(sapi_TaskID, ZB_BIND_TIMER);
osal_start_timerEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV, 250 );
sapi_bindInProgress = 0xffff;
// Find IEEE addr
ZDP_IEEEAddrReq( pRsp->nwkAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
// Send bind confirm callback to application
zb_BindConfirm( sapi_bindInProgress, ZB_SUCCESS );
}
}
}
break;
}
}
/*********************************************************************
* @fn SAPI_Init
*
* @brief Initialization function for the Simple API Task.
* This is called during initialization and should contain
* any application specific initialization (ie. hardware
* initialization/setup, table initialization, power up
* notification ... ).
*
* @param task_id - the ID assigned by OSAL. This ID should be
* used to send messages and set timers.
*
* @return none
*/
void SAPI_Init( byte task_id )
{
uint8 startOptions;
sapi_TaskID = task_id;
sapi_bindInProgress = 0xffff;
sapi_epDesc.endPoint = zb_SimpleDesc.EndPoint;
sapi_epDesc.task_id = &sapi_TaskID;
sapi_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&zb_SimpleDesc;
sapi_epDesc.latencyReq = noLatencyReqs;
// Register the endpoint/interface description with the AF
afRegister( &sapi_epDesc );
// Turn off match descriptor response by default
afSetMatch(sapi_epDesc.simpleDesc->EndPoint, FALSE);
// Register callback evetns from the ZDApp
ZDO_RegisterForZDOMsg( sapi_TaskID, NWK_addr_rsp );
ZDO_RegisterForZDOMsg( sapi_TaskID, Match_Desc_rsp );
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
// Register for HAL events
RegisterForKeys( sapi_TaskID );
if ( HalKeyRead () == HAL_KEY_SW_1)
{
// If SW5 is pressed and held while powerup, force auto-start and nv-restore off and reset
startOptions = ZCD_STARTOPT_CLEAR_STATE | ZCD_STARTOPT_CLEAR_CONFIG;
zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
zb_SystemReset();
}
#endif // HAL_KEY
#ifdef LCD_SUPPORTED
Print8(HAL_LCD_LINE_2 ,20,"Simple",1);
#endif
// Set an event to start the application
osal_set_event(task_id, ZB_ENTRY_EVENT);
}
/*********************************************************************
* @fn SAPI_SendCback
*
* @brief Sends a message to the sapi task ( itself ) so that a
* callback can be generated later.
*
* @return none
*/
void SAPI_SendCback( uint8 event, uint8 status, uint16 data )
{
sapi_CbackEvent_t *pMsg;
pMsg = (sapi_CbackEvent_t *)osal_msg_allocate( sizeof(sapi_CbackEvent_t) );
if( pMsg )
{
pMsg->hdr.event = event;
pMsg->hdr.status = status;
pMsg->data = data;
osal_msg_send( sapi_TaskID, (uint8 *)pMsg );
}
}
/*********************************************************************
* @fn osalInitTasks
*
* @brief This function invokes the initialization function for each task.
*
* @param void
*
* @return none
*/
void osalInitTasks( void )
{
uint8 taskID = 0;
tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
macTaskInit( taskID++ );
nwk_init( taskID++ );
Hal_Init( taskID++ );
#if defined( MT_TASK )
MT_TaskInit( taskID++ );
#endif
APS_Init( taskID++ );
ZDApp_Init( taskID++ );
SAPI_Init( taskID );
}
/*********************************************************************
*********************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -