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


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

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

2,事件是怎么的?

    

/*********************************************************************
 * @fn      osal_msg_send
 *
 * @brief
 *
 *    This function is called by a task to send a command message to
 *    another task or processing element.  The sending_task field must
 *    refer to a valid task, since the task ID will be used
 *    for the response message.  This function will also set a message
 *    ready event in the destination tasks event list.
 *
 *
 * @param   uint8 destination task - Send msg to?  Task ID
 * @param   uint8 *msg_ptr - pointer to new message buffer
 * @param   uint8 len - length of data in message
 *
 * @return  SUCCESS, INVALID_TASK, INVALID_MSG_POINTER
 */
uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr )
{
  
  .......
  //将message发进队列
  osal_msg_enqueue( &osal_qHead, msg_ptr );

  //设置事件,等待task来处理
  osal_set_event( destination_task, SYS_EVENT_MSG );

  return ( SUCCESS );
}

看一下osal_set_event:

/*********************************************************************
 * @fn      osal_set_event
 *
 * @brief
 *
 *    This function is called to set the event flags for a task.  The
 *    event passed in is OR'd into the task's event variable.
 *
 * @param   uint8 task_id - receiving tasks ID
 * @param   uint8 event_flag - what event to set
 *
 * @return  SUCCESS, INVALID_TASK
 */
uint8 osal_set_event( uint8 task_id, uint16 event_flag )
{
  if ( task_id < tasksCnt )
  {
    ......
    tasksEvents[task_id] |= event_flag;  // Stuff the event bit(s)
    return ( SUCCESS );
  }
   else
  {
    return ( INVALID_TASK );
  }
}


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


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

uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  (void)task_id;
  if ( events & SYS_EVENT_MSG )//判断事件类型
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );//从消息队列中取出消息来处理
  }
  .....
  return 0;
}



具体可以参考:

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

Zigbee协议栈中OSAL的简要分析



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值