1 //用户应用任务的事件处理函数
2 uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
3 {
4 afIncomingMSGPacket_t *MSGpkt;
5 (void)task_id; // Intentionally unreferenced parameter
6
7 if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
8 {
9 //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
10 MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
11 while ( MSGpkt )
12 {
13 switch ( MSGpkt->hdr.event )
14 {
15 // Received when a key is pressed
16 case KEY_CHANGE://按键事件
17 SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
18 break;
19
20 // Received when a messages is received (OTA) for this endpoint
21 case AF_INCOMING_MSG_CMD://接收数据事件,调用函数AF_DataRequest()接收数据
22 SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理(1、数据发送函数)
23 break;
24
25 // Received whenever the device changes state in the network
26 case ZDO_STATE_CHANGE: 27 //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
28 //同时完成对协调器,路由器,终端的设置
29 SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
30 //if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件
31 if ( (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) )
32 {
33 // Start sending the periodic message in a regular interval.
34 //这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始
35 //触发第一个周期信息的发送,然后周而复始下去
36 osal_start_timerEx( SampleApp_TaskID,
37 SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
38 SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
39 }
40 else
41 {
42 // Device is no longer in the network
43 }
44 break;
45
46 default:
47 break;
48 }
49
50 // Release the memory 事件处理完了,释放消息占用的内存
51 osal_msg_deallocate( (uint8 *)MSGpkt );
52
53 // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
54 //返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止
55 MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
56 }
57
58 // return unprocessed events 返回未处理的事件
59 return (events ^ SYS_EVENT_MSG);
60 }
61
62 // Send a message out - This event is generated by a timer
63 // (setup in SampleApp_Init()).
64 if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
65 {
66 // Send the periodic message 处理周期性事件, 67 //利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器 68 //开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了, 69 //可以做为传感器定时采集、上传任务 70 SampleApp_SendPeriodicMessage(); 71
72 // Setup to send message again in normal period (+ a little jitter)
73 osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
74 (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );
75
76 // return unprocessed events 返回未处理的事件
77 return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
78 }
79
80 // Discard unknown events
81 return 0;
82 }
1 //接收数据,参数为接收到的数据
2 void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
3 {
4 uint16 flashTime;
5 byte buf[3];
6
7 switch ( pkt->clusterId ) //判断簇ID