Last Updated on : 2025-07-07 07:16:37download
Zigbee local management provides the capability to directly manage and control Zigbee sub-devices within the local network, eliminating complete reliance on cloud interactions. This feature is primarily implemented in smart home gateway devices and enables the following functionalities:
This localized management mechanism enhances real-time device control responsiveness while reducing cloud dependency, ensuring core device control remains functional even during network instability.
/**
* @brief Control the permission for Zigbee sub-devices to join the network.
* @param[in] enable: TRUE: Allows new sub-devices to join, and FALSE: Rejects all new joining attempts.
* @param[in] timeout: Timeout duration in seconds for the operation. 0 indicates the default value.
* @return OPERATE_RET: The operation result. OPRT_OK indicates success.
*/
OPERATE_RET user_zigbee_permit_join(BOOL_T enable, UINT_T timeout);
Description:
0
, the system default value is used (usually 180 seconds)./**
* @brief Initialize the Zigbee local management module and register a callback function.
* @return OPERATE_RET: The initialization result. OPRT_OK indicates success.
*/
OPERATE_RET user_zigbee_local_init(void);
Callback:
/**
* @brief Send control commands to the specified Zigbee device.
* @param[in] dev_id: The ID of the specified device.
* @param[in] dps: The array of DPs.
* @param[in] dp_cnt: The number of DPs.
* @return OPERATE_RET: The sending result. OPRT_OK indicates success.
*/
OPERATE_RET user_zigbee_send_command(const CHAR_T* dev_id,
const TY_OBJ_DP_S* dps,
UINT_T dp_cnt);
Parameter description:
dev_id
: The unique identifier of the specified device.dps
: The array of DPs, containing information about the DPs to be controlled.dp_cnt
: The number of DPs to be controlled./**
* @brief Traverse all current Zigbee sub-devices.
* @param[in] cb: The callback function used to process each device information.
* @return OPERATE_RET: The operation result. OPRT_OK indicates success.
*/
OPERATE_RET user_zigbee_traverse_devices(zigbee_dev_callback cb);
/**
* @brief The type of device information callback.
* @param[in] dev_info: The pointer to the device information struct.
*/
typedef void (*zigbee_dev_callback)(const zigbee_dev_info_t* dev_info);
/**
* @brief Zigbee device information struct.
*/
typedef struct {
CHAR_T dev_id[DEV_ID_LEN + 1]; // The device ID.
BOOL_T online; // The online status.
} zigbee_dev_info_t;
#include "user_zigbee_local.h"
// Device status change callback example.
STATIC VOID __z3_lc_dev_join_cb(CONST CHAR_T *dev_id, CONST CHAR_T *pid, CONST CHAR_T *ver)
{
PR_DEBUG("New device joined: %s, product ID: %s", dev_id, pid);
}
// Initialize Zigbee local management.
void zigbee_init()
{
// Initialize the local management module.
user_zigbee_local_init();
// Enable network joining permission for 60 seconds.
user_zigbee_permit_join(TRUE, 60);
}
// List all the devices.
void list_devices()
{
PR_DEBUG("Current Zigbee devices:");
user_zigbee_traverse_devices([](const zigbee_dev_info_t* dev_info) {
PR_DEBUG("Device ID: %s, Status: %s",
dev_info->dev_id,
dev_info->online ? "Online" : "Offline");
});
}
// Example of how to control the devices.
void control_device(const CHAR_T* dev_id)
{
TY_OBJ_DP_S dp = {
.dpid = 1, // The ID of a DP.
.type = PROP_BOOL, // The data type.
.value.dp_bool = TRUE // Set to TRUE.
};
OPERATE_RET ret = user_zigbee_send_command(dev_id, &dp, 1);
if (ret == OPRT_OK) {
PR_DEBUG("Control command sent successfully");
} else {
PR_ERR("Failed to send control command: %d", ret);
}
}
// Print the menu
void print_menu()
{
PR_DEBUG("\n===== Zigbee Management Menu =====");
PR_DEBUG("1. List devices (l)");
PR_DEBUG("2. Send command (s)");
PR_DEBUG("3. Permit join (p)");
PR_DEBUG("4. Close permit join (c)");
PR_DEBUG("5. Exit (q)");
PR_DEBUG("Enter your choice: ");
}
// Main loop
void main_loop()
{
while (1) {
print_menu();
CHAR_T input[256];
fgets(input, sizeof(input), stdin);
switch (input[0]) {
case 'l': // List the devices
list_devices();
break;
case 's': // Send commands
PR_DEBUG("Enter device ID: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0; // Remove line breaks
control_device(input);
break;
case 'p': // Enable network joining permission
user_zigbee_permit_join(TRUE, 60);
PR_DEBUG("Permit join enabled for 60 seconds");
break;
case 'c': // Disable network joining permission
user_zigbee_permit_join(FALSE, 0);
PR_DEBUG("Permit join disabled");
break;
case 'q': // Exit
return;
default:
PR_DEBUG("Invalid choice");
}
tuya_hal_system_sleep(1000); // 1-second delay
}
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback