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. void SampleApp_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. uint16 SampleApp_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. return 0;
  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();

来自为知笔记(Wiz)

时间: 2024-07-29 22:42:48

Z-stack ——osal的相关文章

Zigbee Z‐STACK协议栈和TinyOS

ZigBee 和 Tinyos 关于ZigBee和TinyOS ZigBee的基础是IEEE 802.15.4.但IEEE仅处理低级MAC层和物理层协议,因此Zigbee联盟扩展了IEEE,对其网络层协议和API进行了标准化,这就是Z‐STACK,Z‐STACK协议栈是TI公司研发,通过ZigBee联盟认证的免费协议栈,协议内部包括了WSN(无线传感器网络)的OS的模型,协议栈具有国际化,标准化的特点,协议栈已提供十几种应用场景,可以非常轻松的让用户开发出满足国际标准的产品. TinyOS是一个

[ZOJ 4016] Mergable Stack

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! AC代码: /* * 用数组实现栈的一些操作 */ #include <cstdio> #include <cstring> using namespace std; const int maxn = 300005; int arr[maxn]; //存放所有的数据 //top代表栈

UVA442-栈

题意: 求矩阵相乘的次数,用栈 WA了这么多次 下次还是不能去猜题意,o(︶︿︶)o 唉 #include<iostream> #include <stdio.h> #include <memory.h> using namespace std; struct Node { int r; int c; }; int main() { freopen("d:\\1.txt", "r", stdin); int total; Node

POJ 2246 Matrix Chain Multiplication

用栈来处理一下就好了. #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<map> using namespace std; struct Mar { int r,c; } mar[3000000]; int n; char s[100000]; int r,c,top,flag; Mar Stack[1000000]; int m

(转载)MatLab绘图

转载自:http://www.cnblogs.com/hxsyl/archive/2012/10/10/2718380.html 转载自:http://www.cnblogs.com/jeromeblog/p/3396494.html plot选项: 一 基础功能 二维图形 一. plot函数① 函数格式:plot(x,y)  其中x和y为长度相同                     坐标向量     函数功能:以向量x.y为轴,绘制曲线. [例] 在区间0≤X≤2?内,绘制正弦曲线y=si

ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程)

ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程) ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践. 在deep learning高质量群里面听一些前辈说,不必深究其他机器学习的算法,可以直接来学dl. 于是最近就开始搞这个了,教程加上matlab编程,就是完美啊. 新教程的地址是:http://ufldl.stanford.edu/tutorial/ 本节学习地址:http://ufldl.stanfor

[ufldl]Supervised Neural Networks

要实现的部分为:forward prop, softmax函数的cost function,每一层的gradient,以及penalty cost和gradient. forwad prop forward prop是输入sample data,使sample data通过神经网络后得到神经网络输出的过程. 以分类问题来说,不同层的输入和输出如下表所示: 层 输入 输出 输入层 sample data feature map 隐藏层 feature map feature map 输出层 fea

UVA - 442 Matrix Chain Multiplication

点击打开链接 题目意思是求矩阵相乘的运算次数, 设A size为n*s,B size为s*m 那么A*B运算量为n*m*s. 注意括号里面的优先级,并且依次累加即可,并且没有不合法的序列. 思路是先对输入的n个矩阵编号按照字典序排序,因为每次两个矩阵相乘会得到一个新的矩阵,编号可以设置成在n的编号加1,并且重新压入栈中. #include <iostream> #include <cstdio> #include <cmath> #include <vector&

基于ZigBee的家居控制系统的设计与应用

基于ZigBee的家居控制系统的设计与应用 PPT简介:http://pan.baidu.com/s/1i38PC6D 摘  要 智能家居是未来家居的发展方向,其利用先进的网络技术.计算机技术和无线通信技术等将家居中的各种电子电气设备连接起来,统一管理.远程监控和资源共享,实现了高效.便利的生活环境.近些年互联网的迅猛发展,网络的稳定性.安全性和网络带宽都有了长足的发展,由互联网提供的各种服务已经深入到人们生活的方方面面,因此将智能家居系统同互联网结合起来,为用户提供远程控制服务,延伸智能家居系

Leetcode#113Path Sum II

Path Sum II Total Accepted: 43473 Total Submissions: 162906My Submissions Question Solution Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22,