Directory
After the
system is powered on, each node needs to send its own device type, network address, and parent node network address to the coordinator to design a data structure, as follows:
Coordinator programming:
//Coordinator.c
typedef struct RFTXBUF
{
uint8 type[3];
uint8 myNWK[4];
uint8 pNWK[4];
}RFTX;
The structure used to store the information of node pairs: device type, network address, parent node network address
/coordinator.c
void genericApp_MessageMSGCB (AFINCOMINGPACKET_T *PKT)
{{
Switch (PKT-> Clusterid)
{{
case genericApp_Clusterid:
osal_memcpy (& nodeinfo [nodenum ++], pkt-> cmd.data, 11);
Break;
}
}
Static Void RXCB (UINT8 PORT, UINT8 Event)
{{
unsigned chargeline [2] = {0x0a, 0x0d};
uint8 buf [8];
uINT8 UARTBUF [16];
uint8 I = 0;
Haluartream (0, buf, 8);
if (OSAL_MEMCMP (BUF, "Topology", 8))
{{
for (i = 0; i <3; i ++)
{{
HaluartWrite (0, NodeInfo [i] .Type, 3); // Output device type
HaluartWrite (0, "nwk:", 6);
HaluartWrite (0, NodeInfo [i] .mynwk, 4); // Output network address
HaluartWrite (0, "pnwk:", 7);
HaluartWrite (0, NodeInfo [i] .pnwk, 4); // Output parent node network address
HaluartWrite (0, Changeline, 2);
}
}
}
series callback function. When the serial buffer has data, this function will be called, read the data of the serial buffer, and uses the OSAL_MEMCMP () function to determine whether to receive “Topology”
terminal node and router programming:
//nddevice.c
UINT16 GENERIPP_PROCESSEVENT (byte Task_id, UINT16 Events)
{{
AFINCOMINGPACKET_T *msgpkt;
if (events & sys_event_msg)
{{
Msgpkt = (AFINCOMINGPACKET_T *) OSAL_MSG_RECEIVE (GENERICAPP_TASKID);
While (msgpkt)
{{
Switch (msgpkt-> hdr.event)
{{
Case ZDO_State_Change:
GenericApp_nwkState = (devStates_t) (msgpkt-> hdr.status);
If (GENERAPP_NWKSTATE == DEV_END_DEVICE)
|| (GENERICAP_NWKSTATE == DEV_ROUTER))
{{
OSAL_SET_EVENT (GENERICAPP_TASKID, Send_data_Event);
}
Break;
DEFAULT:
Break;
}
OSAL_MSG_DEALLOCATE ((UINT8 *) msgpkt); // Release memory
Msgpkt = (AFINCOMINGPACKET_T *) OSAL_MSG_RECEIVE (GENERICAPP_TASKID);
}
Return (events ^ sys_event_msg);
}
If (Events & Send_data_event)
{{
Sendinfo ();
Return (events ^ send_data_event);
}
Return 0;
}
When successfully joined the network, set the Send_data_Event event. In the event processing function, call the SendInfo () function, send the device information to the coordinator
//nddevice.c
void sendinfo (void)
{{
RFTX RFTX;
UINT16 NWK;
If (GENERICAP_NWKSTATE == DEV_END_DEVICE) // Judging whether it is a terminal node
{{
osal_memcpy (rftx.type, "end", 3);
}
if (genericApp_nwkstate == dev_router) // Judging whether it is a router
{{
osal_memcpy (rftx.type, "rou", 3);
}
nwk = nlme_getshortaddr ();
To_string (rftx.mynwk, (uINT8 *) & NWK, 2);
nwk = nlme_getCoordshortaddr ();
To_String (RFTX.PNWK, (UINT8 *) & NWK, 2);
AFADDDRTYPE_T My_DSTADDR;
my_dstaddr.addrmode = (AFADDRMODE_T) addr16bit;
My_dstadr.ndr.ndpoint = GENERICAPP_ENDPOINT;
My_dstadr.addr.shortaddr = 0x0000;
AF_DataREQUEST (& My_dstaddr, & Genericapp_epdesc,
GenericApp_Clusterid,
11,
(UINT8 *) & RFTX,
& Genericapp_transid,
AF_DISCV_Route,
AF_Default_radius);
}
void to_string (UINT8 *Dest, Char *SRC, UINT8 Length)
{{
uint8 *xad;
uint8 I = 0;
UINT8 CH;
XAD = SRC + Length-1;
For (i = 0; I <langth; i ++, xad---)
{{
ch = (*xad >> 4) & 0x0f;
Dest [i << 1] = ch + ((ch <10)? '0': '7');
ch = *xad & 0x0f;
Dest [(i << 1) + 1] = ch + ((ch <10)? '0': '7');
}
}
If the device type is a terminal node, fill in “END” in the device type field; if the device type is a router, fill in “RND” in the device type field. Use NLME_getCoordshortaddddr () function to obtain the parent node network address.
Test:
Open the serial portal to debug the assistant, send the topology command, and output the topology information of the network at this time
It can be seen that there is a router in the network, the network address is 0x0001, and there are two terminal nodes. The network address is 0x796F, 0x7970. The network topology is as follows: