How to access Zigbee MAC diagnostic counters from app code?

I have a semi-working app which executes Zigbee commands that it receives from other nodes, but the latency is sometimes unacceptably high.  I would like to check some of the diagnostic counters to see if they offer a clue as to what is happening in the lower layers (e.g. corrupted packets, retransmissions, etc.):

/* MAC diagnostics info */
typedef ZB_PACKED_PRE struct zb_mac_diagnostic_info_s
{
zb_uint32_t mac_rx_bcast; /* A counter that is incremented each time
* the MAC layer receives a broadcast. */
zb_uint32_t mac_tx_bcast; /* A counter that is incremented each time
* the MAC layer transmits a broadcast. */
zb_uint32_t mac_rx_ucast; /* A counter that is incremented each time the
* MAC layer receives a unicast. */

/* These 3 counters are required not to break
* ZDO channel management logic that
* uses normalized counters values.
*/
zb_uint32_t mac_tx_ucast_total_zcl; /* The same as mac_tx_ucast_total, but non-normalized */
zb_uint16_t mac_tx_ucast_failures_zcl; /* The same as mac_tx_ucast_failures, but non-normalized */
zb_uint16_t mac_tx_ucast_retries_zcl; /* The same as mac_tx_ucast_retries, but non-normalized*/

zb_uint16_t mac_tx_ucast_total; /* Total number of Mac Tx Transactions to
* attempt to send a message (but not
* counting retries) */
zb_uint16_t mac_tx_ucast_failures; /* Total number of failed Tx
* Transactions. So if the Mac send a
* single packet, it will be retried 4
* times without ack, that counts as 1 failure */
zb_uint16_t mac_tx_ucast_retries; /* Total number of Mac Retries regardles of
* whether the transaction resulted in
* success or failure. */

zb_uint16_t phy_to_mac_que_lim_reached; /* A counter that is incremented each time when MAC RX queue if full. */

zb_uint16_t mac_validate_drop_cnt; /* How many times the packet was dropped at the packet
* validation stage for length or bad formatting. */

zb_uint16_t phy_cca_fail_count; /* Number of the PHY layer was unable
* to transmit due to a failed CCA */

zb_uint8_t period_of_time; /* Time period over which MACTx results are measured */
zb_uint8_t last_msg_lqi; /* LQI value of the last received packet */
zb_int8_t last_msg_rssi; /* RSSI value of the last received packet */
} ZB_PACKED_STRUCT
zb_mac_diagnostic_info_t;

Currently I am trying this:

#include <zb_zcl_diagnostics.h>
LOG_INF("last_msg_rssi = %d", diagnostics_ctx_zcl.mac_data.last_msg_rssi);

Unfortunately this value is never updated; it remains 0 forever.  In zcl_general_commands.c I see functions like zb_zcl_sync_stats() that ask ZBOSS to update the counters, however nothing relevant seems to be exported to user applications.

What is the recommended API for getting access to these counters?

Do I need to add a diagnostics cluster to my app and then query it over Zigbee?  And if so, what tools do you typically use on the client side?

Related