message processing in ZigBee is in serialapp_processevent, analyze code
UINT16 serialapp_processevent (uint8 task_id, UINT16 events) {(void) task_id;
if (events & sys_event_msg) {afincomingmsgpacket_t *msgpkt; while ((MSGPKT = (afincomingmsgpacket_t *) osal_msg_receive (serialapp_taskid))) {switch (msgpkt-& gt;hdr.event) {case ZDO_CB_MSG:SERIALAPP_PROCESSZDOMSGS (zdoincomingmsg_t *) MS
GPKT);
Break Case Key_change:serialapp_handlekeys (((keychange_t *) msgpkt)->state, (((keychange_t *) msgpkt)->keys
);
Break
Case Af_incoming_msg_cmd:serialapp_processmsgcmd (MSGPKT);
Break
Case zdo_state_change:serialapp_nwkstate = (devstates_t) (msgpkt->hdr.status); if ((serialapp_nwkstate = = Dev_zb_coord) | | (serialapp_nwkstate = = Dev_router) | | (Serialapp_nwkstate = = Dev_end_devICE) {#if defined (zdo_coordinator) Broadcast_dstaddr.addrmode = (AfA
ddrmode_t) Addrbroadcast;
Broadcast_dstaddr.endpoint = Serialapp_endpoint;
BROADCAST_DSTADDR.ADDR.SHORTADDR = 0xFFFF;
#if uart_debug Printaddrinfo (nlme_getshortaddr (), aextendedaddress + z_extaddr_len-1); #endif for (int i=0; i<max_device; i++) {Enddevinfo[i].add
r=i+1;
} #else afsendaddrinfo ();
Osal_start_timerex (Serialapp_taskid, SERIALAPP_SEND_PERIODIC_EVT,
Serialapp_send_periodic_timeout);
#endif} break;
Default:break; } osal_msg_deallocate((Uint8 *) msgpkt);
} return (events ^ sys_event_msg);
} if (events & serialapp_send_periodic_evt) {serialapp_sendperiodicmessage (); Osal_start_timerex (Serialapp_taskid, Serialapp_send_periodic_evt, (Serialapp_send_periodic_timeout + (Osal_ra
nd () & 0x00FF)));
Return (events ^ serialapp_send_periodic_evt);
} if (events & serialapp_send_evt) {serialapp_send ();
Return (events ^ serialapp_send_evt);
} if (events & serialapp_resp_evt) {serialapp_resp ();
Return (events ^ serialapp_resp_evt);
} return (0); }
In the IF (Events & Sys_event_msg) is the system registration event, the code mainly has the device state Change event (ZDO_CB_MSG), press the button down event (Key_change), accept the Wireless data event (Af_incoming_ MSG_CMD), Network change events (Zdo_state_change) These IDs are defined in ZComDef.h and can be viewed in manuals or data documents.
While the user is in the following event: Duplicate send data (SERIALAPP_SEND_PERIODIC_EVT), Serial send data (SERIALAPP_SEND_EVT) and Resend (SERIALAPP_RESP_EVT) define the data sent. The main Serialapp_processmsgcmd function is the ability to add data processing, which can be written at the time of encryption or function definition:
There are two events in the Code serialapp_processmsgcmd (user definable):
1. Received Wireless data Serialapp_clusterid
2. Received a wireless response
the Serialapp_clusterid2 Serialapp_handlekeys function handles key response events:
The key functions in the code are roughly:
1. Auto-match:
if (keys & Hal_key_sw_4)
{
halledset (hal_led_4, hal_led_mode_off);
Initiate a Match Description Request (Service Discovery)
txaddr.addrmode = addrbroadcast;
TxAddr.addr.shortAddr = nwk_broadcast_shortaddr;
Zdp_matchdescreq (&txaddr, nwk_broadcast_shortaddr,
Serialapp_profid,
serialapp_max_clusters, cId_t * ) Serialapp_clusterlist,
serialapp_max_clusters, (cid_t *) serialapp_clusterlist,
FALSE);
}
2. Binding Coordinator:
if (keys & hal_key_sw_2)
{
halledset (hal_led_4, hal_led_mode_off);
Haluartwrite (UART1, "S2", 2);
Initiate an End Device Bind Request for the mandatory endpoint
Txaddr.addrmode = addr16bit;
TXADDR.ADDR.SHORTADDR = 0x0000; Coordinator
Zdp_enddevicebindreq (&TXADDR, Nlme_getshortaddr (),
Serialapp_epdesc.endpoint,
Serialapp_profid,
serialapp_max_clusters, (cid_t *) serialapp_clusterlist,
serialapp_max_clusters, (cId_ T *) serialapp_clusterlist,
FALSE);
3. Terminal Periodic escalation events:
if (keys & hal_key_sw_6)
{
if (Sendflag = = 0)
{
sendflag = 1;
Halledset (Hal_led_1, hal_led_mode_on);
Osal_start_timerex (Serialapp_taskid,
serialapp_send_periodic_evt,
serialapp_send_periodic_timeout);
}
else
{
Sendflag = 0;
Halledset (Hal_led_1, hal_led_mode_off);
Osal_stop_timerex (Serialapp_taskid, serialapp_send_periodic_evt);
}
}
So in the Serialapp_init function, a key event is configured so that the terminal is automatically connected to the coordinator. zdo_state_change Network Status Change event, which is intended to send data periodically after binding:
Osal_start_timerex (Serialapp_taskid, serialapp_send_periodic_evt, serialapp_send_periodic_timeout);
Erialapp_taskid: Task ID
SERIALAPP_SEND_EVT: Multiple events under the same task, event number
Serialapp_send_periodic_timeout: Delay