Z-stack ——osal

  • osal 启动流程

                                         osal 流程图

协调器工作流程
终端器工作流程
 
 
  • z-stack中事件和任务的事件处理函数是如何联系的??
zigbee协议栈中的三个重要的变量:
tasksCnt:任务的总个数
tasksEvents:指针变量,指向了事件表的首地址
tasksArr:数组(如下代码定义的),数组的每一项都是一个函数指针,指向了事件处理函数
  1. const pTaskEventHandlerFn tasksArr[]={
  2. macEventLoop,
  3. nwk_event_loop,
  4. Hal_ProcessEvent,
  5. #if defined( MT_TASK )
  6. MT_ProcessEvent,
  7. #endif
  8. APS_event_loop,
  9. #if defined ( ZIGBEE_FRAGMENTATION )
  10. APSF_ProcessEvent,
  11. #endif
  12. ZDApp_event_loop,
  13. #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  14. ZDNwkMgr_event_loop,
  15. #endif
  16. SampleApp_ProcessEvent
  17. };
 
 
  • Z-stack的osal初始化与轮询操作原理介绍
 
osal_start_system轮询函数:(当然在进入这个函数之前要对系统进行初始化,初始化的目的主要是将所以任务的事件初始化为0)
  1. void osal_start_system(void)
  2. {
  3. #if !defined ( ZBIT ) && !defined ( UBIT )
  4. for(;;)// Forever Loop
  5. #endif
  6. {
  7. uint8 idx =0;
  8. osalTimeUpdate();
  9. Hal_ProcessPoll();// This replaces MT_SerialPoll() and osal_check_timer().
  10. do{
  11. if(tasksEvents[idx])// Task is highest priority that is ready.
  12. {
  13. break;
  14. }
  15. }while(++idx < tasksCnt);
  16. if(idx < tasksCnt)
  17. {
  18. uint16 events;
  19. halIntState_t intState;
  20. HAL_ENTER_CRITICAL_SECTION(intState);
  21. events = tasksEvents[idx];
  22. tasksEvents[idx]=0;// Clear the Events for this task.
  23. HAL_EXIT_CRITICAL_SECTION(intState);
  24. events =(tasksArr[idx])( idx, events );//函数指针,调用相应的事件函数处理,结合上面图一
  25. HAL_ENTER_CRITICAL_SECTION(intState);
  26. tasksEvents[idx]|= events;// Add back unprocessed events to the current task.
  27. HAL_EXIT_CRITICAL_SECTION(intState);
  28. }
  29. #if defined( POWER_SAVING )
  30. else// Complete pass through all task events with no activity?
  31. {
  32. osal_pwrmgr_powerconserve();// Put the processor/system into sleep
  33. }
  34. #endif
  35. }
  36. }
13-17行:循环查看事件表是否有事件发生(有就不为零,后跳出循环《可以结合上面图一来理解》)
25行     :读取该事件。
29行     :调用事件处理函数处理  
  • Z-stack sample applications 部分解析(from Z-Stack Sample Applications.pdf)
  1. Task Initialization(任务初始化)
  1. voidSampleApp_Init(uint8 task_id )
  2. {
  3. SampleApp_TaskID= task_id;
  4. SampleApp_NwkState= DEV_INIT;
  5. SampleApp_TransID=0;//发送数据包的序号,在zigbee协议栈中,每发送一个数据包,该发送序号自动加1.作用:接收端来计算丢包率。
  6. // Device hardware initialization can be added here or in main() (Zmain.c).
  7. // If the hardware is application specific - add it here.
  8. // If the hardware is other parts of the device add it in main()
  9. #if defined ( BUILD_ALL_DEVICES )
  10. // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
  11. // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
  12. // together - if they are - we will start up a coordinator. Otherwise,
  13. // the device will start as a router.
  14. if( readCoordinatorJumper())
  15. zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
  16. else
  17. zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
  18. #endif// BUILD_ALL_DEVICES
  19. #if defined ( HOLD_AUTO_START )
  20. // HOLD_AUTO_START is a compile option that will surpress ZDApp
  21. // from starting the device and wait for the application to
  22. // start the device.
  23. ZDOInitDevice(0);
  24. #endif
  25. // Setup for the periodic message's destination address
  26. // Broadcast to everyone
  27. SampleApp_Periodic_DstAddr.addrMode =(afAddrMode_t)AddrBroadcast;
  28. SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  29. SampleApp_Periodic_DstAddr.addr.shortAddr =0xFFFF;
  30. // Setup for the flash command's destination address - Group 1
  31. SampleApp_Flash_DstAddr.addrMode =(afAddrMode_t)afAddrGroup;
  32. SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  33. SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;
  34. // Fill out the endpoint description.
  35. SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
  36. SampleApp_epDesc.task_id =&SampleApp_TaskID;
  37. SampleApp_epDesc.simpleDesc
  38. =(SimpleDescriptionFormat_t*)&SampleApp_SimpleDesc;
  39. SampleApp_epDesc.latencyReq = noLatencyReqs;
  40. // Register the endpoint description with the AF
  41. afRegister(&SampleApp_epDesc);
  42. // Register for all key events - This app will handle all key events
  43. RegisterForKeys(SampleApp_TaskID);
  44. // By default, all devices start out in Group 1
  45. SampleApp_Group.ID =0x0001;
  46. osal_memcpy(SampleApp_Group.name,"Group 1",7);
  47. aps_AddGroup( SAMPLEAPP_ENDPOINT,&SampleApp_Group);
  48. #if defined ( LCD_SUPPORTED )
  49. HalLcdWriteString("SampleApp", HAL_LCD_LINE_1 );
  50. #endif
  51. }
代码3-38:初始化本地变量与应用程序对象等;
代码41-51:将应用程序对象注册到AF层( afRegister ( & SampleApp_epDesc ) );登记注册到OSAL与HAL系统服务中( RegisterForKeys ( SampleApp_TaskID ); )。只有注册后才可以使用OSAL提供的系统服务。
 
    2.      Task Event Handler(任务处理)
 
  1. uint16SampleApp_ProcessEvent(uint8 task_id,uint16 events )
  2. {
  3. afIncomingMSGPacket_t*MSGpkt;
  4. (void)task_id;// Intentionally unreferenced parameter
  5. if( events & SYS_EVENT_MSG )
  6. {
  7. MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);//从消息队列上接收消息
  8. while(MSGpkt)
  9. {
  10. switch(MSGpkt->hdr.event )
  11. {
  12. // Received when a key is pressed
  13. case KEY_CHANGE:
  14. SampleApp_HandleKeys(((keyChange_t*)MSGpkt)->state,((keyChange_t*)MSGpkt)->keys );
  15. break;
  16. // Received when a messages is received (OTA) for this endpoint
  17. case AF_INCOMING_MSG_CMD:
  18. SampleApp_MessageMSGCB(MSGpkt);
  19. break;
  20. // Received whenever the device changes state in the network
  21. case ZDO_STATE_CHANGE:
  22. SampleApp_NwkState=(devStates_t)(MSGpkt->hdr.status);
  23. if((SampleApp_NwkState== DEV_ZB_COORD)
  24. ||(SampleApp_NwkState== DEV_ROUTER)    
  25. ||(SampleApp_NwkState== DEV_END_DEVICE))
  26. {
  27. // Start sending the periodic message in a regular interval.
  28. osal_start_timerEx(SampleApp_TaskID,
  29. SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  30. SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
  31. }
  32. else
  33. {
  34. // Device is no longer in the network
  35. }
  36. break;
  37. default:
  38. break;
  39. }
  40. // Release the memory
  41. osal_msg_deallocate((uint8*)MSGpkt);
  42. // Next - if one is available
  43. MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);// Get the next message
  44. }
  45. /******************************************************
  46. After processing the SYS_EVENT_MSG messages, 
  47. the processing function should return the unprocessed events:
  48. *******************************************************/
  49. return(events ^ SYS_EVENT_MSG);// return unprocessed events
  50. }
  51. // Send a message out - This event is generated by a timer
  52. // (setup in SampleApp_Init()).
  53. if( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  54. {
  55. // Send the periodic message
  56. SampleApp_SendPeriodicMessage();
  57. // Setup to send message again in normal period (+ a little jitter)
  58. osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  59. (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT +(osal_rand()&0x00FF)));
  60. // return unprocessed events
  61. return(events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  62. }
  63. // Discard unknown events
  64. return0;
  65. }
当然这里的任务处理函数可以看到两大类任务: SYS_EVENT_MSG与 SAMPLEAPP_SEND_PERIODIC_MSG_EVT
我的理解:(1) SYS_EVENT_MSG (0x8000),是osal操作系统的系统等待轮询任务;一旦调用这个任务事件,
                             它必须得是以下子事件触发:AF_INCOMING_MSG_CMD :来自其它设备AF层发送过来的消息事件
                                                                                 KEY_CHANGE:用户按键事件
                                                                                 ZDO_STATE_CHANGE :网络状态变化事件
                                                                                 CMD_SERIAL_MSG:MT层串口发送过来的数据。(注意要加上MT.h头文件)
                              当然除了以上的Command IDs 外还有一些其它的。可以根据自己的应用需求而添加
                       (2) SAMPLEAPP_SEND_PERIODIC_MSG_EVT,定时器中断事件,要是需要定时或周期性的触发一些事件(比如周期发送消息)可以在此事件下面添加用户响应函数。
    
3. Message Flow(消息流)
AF_DataRequest();
 
 
 





转载于:https://www.cnblogs.com/ixiaole/p/4119517.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值