Task, event, message processing flow in z-Stack protocol in ZigBee

1. After the system is powered on, the osal_start_system() method will be called at the end of the main function to start the system. This method is an infinite loop, and only one thing is done in the loop: constantly detect tasks to see if there are any events in the task that need to be processed;

   Detection method: If tasksEvents[idx] is not 0, it means that the event needs to be processed.

2. What happened to the incident?

    

[html]  view plain copy  
  1. /*********************************************************************  
  2.  * @fn      osal_msg_send  
  3.  *  
  4.  * @brief  
  5.  *  
  6.  *    This function is called by a task to send a command message to  
  7.  *    another task or processing element.  The sending_task field must  
  8.  *    refer to a valid task, since the task ID will be used  
  9.  *    for the response message.  This function will also set a message  
  10.  *    ready event in the destination tasks event list.  
  11.  *  
  12.  *  
  13.  * @param   uint8 destination task - Send msg to?  Task ID  
  14.  * @param   uint8 *msg_ptr - pointer to new message buffer  
  15.  * @param   uint8 len - length of data in message  
  16.  *  
  17.  * @return  SUCCESS, INVALID_TASK, INVALID_MSG_POINTER  
  18.  */  
  19. uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr )  
  20. {  
  21.     
  22.   .......  
  23.   //Send the message to the queue  
  24.   osal_msg_enqueue( &osal_qHead, msg_ptr );  
  25.   
  26.   //Set the event and wait for the task to process it  
  27.   osal_set_event( destination_task, SYS_EVENT_MSG );  
  28.   
  29.   return ( SUCCESS );  
  30. }  

Take a look at osal_set_event:

[html]  view plain copy  
  1. /*********************************************************************  
  2.  * @fn      osal_set_event  
  3.  *  
  4.  * @brief  
  5.  *  
  6.  *    This function is called to set the event flags for a task.  The  
  7.  *    event passed in is OR'd into the task's event variable.  
  8.  *  
  9.  * @param   uint8 task_id - receiving tasks ID  
  10.  * @param   uint8 event_flag - what event to set  
  11.  *  
  12.  * @return  SUCCESS, INVALID_TASK  
  13.  */  
  14. uint8 osal_set_event( uint8 task_id, uint16 event_flag )  
  15. {  
  16.   if ( task_id <tasksCnt )   
  17.   {  
  18.     ......  
  19.     tasksEvents[task_id] |= event_flag;  // Stuff the event bit(s)  
  20.     return ( SUCCESS );  
  21.   }  
  22.    else  
  23.   {  
  24.     return ( INVALID_TASK );  
  25.   }  
  26. }  


The tasksEvents[task_id] is set in the osal_set_event method, and it will not be 0 after the setting is completed. Once it is detected that the tasksEvents is not 0 in the infinite loop of the first step, it will wake up the task and process it in time.


3. In the processing function of the task, the message will be taken from the queue for processing, such as:

[html]  view plain copy  
  1. uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )  
  2. {  
  3.   afIncomingMSGPacket_t *MSGpkt;  
  4.   (void)task_id;  
  5.   if ( events & SYS_EVENT_MSG )//Determine event type  
  6.   {  
  7.     MSGpkt  = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );//Get the message from the message queue for processing  
  8.   }  
  9.   .....  
  10.   return 0;  
  11. }  
For details, please refer to:

Event and message analysis in Z-Stack protocol

Brief Analysis of OSAL in Zigbee Protocol Stack

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325546640&siteId=291194637
Recommended