(vi) Send function af_datarequest ZigBee data sending and receiving

Source: Internet
Author: User
Tags requires reserved sleep


Zstack data is received and sent.
There are two things to do for the receiver:
1) Register an endpoint and implement it by calling Afregister () in the Xxxxapp_init () function. If you need to process ZdO messages and case messages, you also have to invoke the appropriate registration function Zdo_registerforzdomsg,registerforkeys.
2) After registering the endpoint and the message, you need to write a message handler function to respond to each message, which is often called xxxxapp_processevent ().

For sending, it's simple, just call the Af_datarequest () function and send the data to the port of the specified node.

From AF.C and AF.H, you can see that the endpoints are managed using a one-way list. When users need to use an endpoint for communication, they first call Afregister to register the appropriate endpoint to add a record to the endpoint management list.

Once the endpoint is registered, it will be used in both receiving and sending two processes.

During the receive process, the Afincomingdata function extracts the target endpoint number from the APS layer packet, searches for the node's registered endpoint number, and matches it. If the endpoint number matches the need to further match the application configuration ID, the application configuration ID also matches the language, the packet will be packaged, and then, with the help of Osal's message mechanism, is eventually sent to the corresponding endpoint application object's message handler function.

During the sending process, the endpoint information is read af_datarequest, filled into the packet-related area, and if the endpoint registers a callback function, the callback function is called before the data is sent.




Af_datarequest packets are sent.
This function is a packet sending request under the AF framework, and the function constructs a packet of the application layer and then calls the next APS layer function Apsde_datareq to send the packet.
The function operation process is as follows:
1) Verify that the source endpoint is valid.
2) Verify that the destination address is broadcast address, and if there is a conflict between the address and the mode, fix the broadcast address.
3) Fill in the destination address.
4) Fill in the default configuration ID.
5) If the source endpoint has a callback function, call the callback function to modify the configuration ID.

6) If the endpoint has a simple descriptor, overwrite the default configuration ID.

7) Fill in the delivery options according to the conditions.

8) Fill in the transaction id,transid.

9) Fill in the source endpoint.

10) Fill in the target source endpoint.

11) Fill in the cluster ID.

12) Fill in the APS payload data length.

13) Fill in the APS load data pointer.

14) Fill in whether the Routing Discovery service is allowed to be triggered. The routing service is triggered when the packet cannot find the destination.

15) Design the broadcast radius.

16) Call the APS data send function.

17) If the destination address is this node, call the packet acknowledgment primitive.

Before looking at the original code has always felt that the second parameter source endpoint is a bit odd, why not use the source endpoint number is simple and clear, from the code can see the source endpoint in the sending process need to call the source endpoint callback function, and need to use the source endpoint simple descriptor in the configuration ID, the second parameter using the endpoint structure is not surprising.





Z-stack sends data by calling function void Sampleapp_sendflashmessage (UInt16 flashtime) in the application layer, where flash time is the data being sent, and this function is invoked in the application

afstatus_t af_datarequest (afaddrtype_t *dstaddr, endpointdesc_t *srcep,

UInt16 CID, UInt16 len, uint8 *buf, Uint8 *transid,

Uint8 options, uint8 radius)

function to complete the sending of the data. If you are familiar with the meaning of each of these parameters, you can use the Send function to send your own data flexibly.

The first parameter dstaddr, in file AF.h, is a pointer to the struct body. In addition to specifying a network address in this parameter, you also need to specify a schema parameter for the destination address.

Each device is a node in ZigBee, each node has a long address and short address, and a short address is used by other devices to send data. Each node has 241 terminals (0 reserved, 1-240 can be assigned to the app). Each terminal can set the address independently, and when the device sends data, it must specify a short address and a receiving terminal for the target device. An app must register one or more terminals to receive or send data.

typedef struct
{
Union
{
UInt16 shortaddr;
} addr;
afaddrmode_t Addrmode; Afaddrmode_t is an enumeration type pattern parameter

BYTE EndPoint; Specifies the endpoint number endpoint 241-254 reserved endpoint Range 1-240

} afaddrtype_t;

The following is the definition of the afaddrmode_t structure
The afaddrmode_t structure is defined as follows:
typedef enum
{
Afaddrnotpresent = addrnotpresent,//Transfer by binding table
Afaddr16bit = addr16bit,//transmission by network address
Afaddr64bit = addr64bit,//Transfer by IEEE address
Afaddrgroup = Addrgroup,//Transfer by group address
Afaddrbroadcast = addrbroadcast//Broadcast transmission

} afaddrmode_t;

1, according to the binding table for transmission
If the binding function is set in the program, it is possible to bind the transport after the binding is complete, the binding transfer does not need to fill in the short address and the IEEE address, and the stack is looked up directly in the binding table.
2. Transmission by Network Address
Transmission by network address is a short address transmission, also known as Point-to-point transmission, in the transmission need to set addr.shortaddr for the purpose of the node 16 is a short address.
3. Transfer by IEEE Address
Transmission by IEEE address can also be referred to as point-to-point transmission, which requires the setting of a 64-bit IEEE address for the destination node of ADDR.EXTADDR.
Note: ZigBee devices have two types of addresses. One is a 64-bit IEEE address (physical), which is the MAC address, and the other is a 16-bit network address.
A 64-bit address makes the world's only address, and the device will always have it in its lifetime. It is usually set by the manufacturer or when it is installed. These addresses are maintained and distributed by the IEEE.
16 is the network address that is assigned by the coordinator or router when the device is joined to the network. It is unique in the network and is used to identify devices and send data across the network.
4. Transfer by group address
As the name implies, transmission by group address is within a group.
This mode is used when an application needs to send a packet to a group of devices on the network. The address mode is set to Afaddrgroup and Addr.shortaddr is set to the group ID. Before you use this feature, you must define groups in the network. (See the Aps_addgroup () function in the Z-stack API documentation).
5. Broadcast Transmission
This mode is used when the application needs to send packets to every device on the network. The address mode is set to Addrbroadcast. The destination address can be set to one of the following broadcast addresses:
Nwk_broadcast_shortaddr_devall (0xFFFF)-The packet will be transmitted to all devices on the network, including the device in sleep. For a device in sleep, the packet is kept on its parent node until it is queried, or the message times out (Nwk_indirect_msg_timeout in f8wconifg.cfg). Nwk_broadcast_shortaddr_devrxon (0xFFFD)-The packet will be transmitted to all devices on the network (rxonwhenidle) when it is idle, that is, everything except sleep. NWK_BROADCAST_SHORTADDR_DEVZCZR (0xFFFC)--packets are sent to all routers, including the coordinator.
Enum
{
addrnotpresent = 0,
Addrgroup = 1,
Addr16bit = 2,
Addr64bit = 3,//Specify IEEE address for unicast transmission 64-bit

Addrbroadcast = 15
};


Note: ZigBee devices have two types of addresses. One is a 64-bit IEEE address (physical), which is the MAC address, and the other is a 16-bit network address.

16 is the network address that is assigned by the coordinator or router when the device is joined to the network. It is unique in the network and is used to identify devices and send data across the network. A 64-bit address makes the world's only address, and the device will always have it in its lifetime. It is usually set by the manufacturer or when it is installed. These addresses are maintained and distributed by the IEEE.

The second parameter endpointdesc_t *srcep, is also a pointer to a struct, the destination network address description, each terminal must have a Zigbeezz simple description. If it is empty, it will

Error that causes the function to return Afstatus_invalid_parameter


typedef struct
{
BYTE EndPoint; Endpoint Number

BYTE *task_id; Pointer to location of the application task ID.

simpledescriptionformat_t *simpledesc; Simple description of the device

afnetworklatencyreq_t Latencyreq; Enumeration structure must be populated with NOLATENCYREQS

} endpointdesc_t;

TASK_ID-The task ID pointer, which indicates the message delivery purpose when a message is received. The received message is packaged as a osal message and will be sent to a task
Simpledesc-A simple description of the ZigBee that points to the terminal

Latencyreq-must be populated with NOLATENCYREQS


A simple description structure of the target device
typedef struct
{
BYTE EndPoint; EP ID (ep=end point)

UInt16 Appprofid; Profile ID (section ID)

UInt16 Appdeviceid; Device ID

BYTE Appdevver:4; Device version 0x00 is version 1.0

BYTE Reserved:4; Af_v1_support uses for appflags:4.

BYTE Appnuminclusters; Number of input clusters supported by the terminal

cid_t *pappinclusterlist; Pointer to input cluster ID list

BYTE Appnumoutclusters; Number of output clusters

cid_t *pappoutclusterlist; Pointer to output cluseter ID list

} simpledescriptionformat_t;


endpoint– Terminal Number: 1-240 This is the sub-address of the node, which is used to receive data
appprofid– defines the profile ID (section ID) that is supported on this terminal, and the ID is best followed by the ZigBee Alliance assignment.
appdeviceid– Terminal supported devices Id,id best to follow ZigBee affiliate distribution.
appdevver– version of the device description that is performed by the device on this terminal: 0x00 is versions 1.0. 
reserved– retains 
the number of input clusters supported by the appnuminclusters– terminal 
pappinclusterlist– pointer to the input cluster ID list 
appnumoutclusters– Number of output clusters supported by the terminal 
pappoutclusterlist– pointers to the list of output cluster IDs


typedef enum
{
Nolatencyreqs,
Fastbeacons,
Slowbeacons
} afnetworklatencyreq_t;


Third parameter: uint16 CID cluster ID is a valid cluster number specified by profile

Fourth parameter: Len the length of the data to be sent

Fifth parameter: uint8 *buf pointer to send data buffer

Sixth parameter: uint8 *TRANSID transaction sequence number pointer. If the message cache is sent, this function will increment the number of the task ID

The seventh parameter: The Send option, which can be sent by one of the following items, or a few items or a valid bitmask

Af_ack_request 0X10 requires APs to respond, which is an application-level response that is only used when sending directly (unicast).

Af_discv_route 0x20 always include this option

af_skip_routing 0x80 Setting this option will cause the device to skip routing and send messages directly. The endpoint device will not send a message to its father. It works well when you send (unicast) and broadcast messages directly.

Eighth parameter: Uint8 radius maximum hop count with default value Af_default_radius 10

return value:

The return value of the function: afstatus_t type enumerated type, successful or

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.