ZigBee 中 z-Stack协议中的任务、事件、消息处理流程

1,系统上电以后在main函数的最后会调用osal_start_system()方法来启动系统,这个方法是个死循环,只循环里面只做一件事:不断的检测任务,看任务里面有没有事件需要处理;

   检测方法:如果tasksEvents[idx]不为0,则表示要事件需要处理。

2,事件是怎么的?

    

[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.   //将message发进队列  
  24.   osal_msg_enqueue( &osal_qHead, msg_ptr );  
  25.   
  26.   //设置事件,等待task来处理  
  27.   osal_set_event( destination_task, SYS_EVENT_MSG );  
  28.   
  29.   return ( SUCCESS );  
  30. }  

看一下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. }  


在osal_set_event方法中设置了 tasksEvents[task_id],设置完以后将不会为0,在第一步的死循环一旦检测到tasksEvents为不0,将会唤醒任务及时处理。


3,在任务的处理函数中,将会从队列中取出消息来处理,比如:

[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 )//判断事件类型  
  6.   {  
  7.     MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );//从消息队列中取出消息来处理  
  8.   }  
  9.   .....  
  10.   return 0;  
  11. }  
具体可以参考:

Z-Stack协议中事件和消息分析

Zigbee协议栈中OSAL的简要分析

猜你喜欢

转载自blog.csdn.net/wearlee/article/details/80201195
今日推荐