1
+ #include " ep_temperature_sensor.h"
2
+
3
+ ZigbeeThermostat::ZigbeeThermostat (uint8_t endpoint, void (*cb)(const esp_zb_zcl_set_attr_value_message_t *message)) : Zigbee_EP(endpoint, cb) {
4
+ _device_id = ESP_ZB_HA_THERMOSTAT_DEVICE_ID;
5
+ _version = 0 ;
6
+
7
+ esp_zb_thermostat_cfg_t thermostat_cfg = ESP_ZB_DEFAULT_THERMOSTAT_CONFIG ();
8
+ _cluster_list = esp_zb_thermostat_clusters_create (&thermostat_cfg);
9
+ _ep_config = {
10
+ .endpoint = _endpoint,
11
+ .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
12
+ .app_device_id = ESP_ZB_HA_THERMOSTAT_DEVICE_ID,
13
+ .app_device_version = _version
14
+ };
15
+ }
16
+
17
+ static void bind_cb (esp_zb_zdp_status_t zdo_status, void *user_ctx) {
18
+ esp_zb_zdo_bind_req_param_t *bind_req = (esp_zb_zdo_bind_req_param_t *)user_ctx;
19
+
20
+ if (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) {
21
+ /* Local binding succeeds */
22
+ if (bind_req->req_dst_addr == esp_zb_get_short_address ()) {
23
+ log_i (" Successfully bind the temperature sensor from address(0x%x) on endpoint(%d)" , temp_sensor.short_addr , temp_sensor.endpoint );
24
+
25
+ /* Read peer Manufacture Name & Model Identifier */
26
+ esp_zb_zcl_read_attr_cmd_t read_req;
27
+ read_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT;
28
+ read_req.zcl_basic_cmd .src_endpoint = HA_THERMOSTAT_ENDPOINT;
29
+ read_req.zcl_basic_cmd .dst_endpoint = temp_sensor.endpoint ;
30
+ read_req.zcl_basic_cmd .dst_addr_u .addr_short = temp_sensor.short_addr ;
31
+ read_req.clusterID = ESP_ZB_ZCL_CLUSTER_ID_BASIC;
32
+
33
+ uint16_t attributes[] = {
34
+ ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID,
35
+ ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID,
36
+ };
37
+ read_req.attr_number = ARRAY_LENTH (attributes);
38
+ read_req.attr_field = attributes;
39
+
40
+ esp_zb_zcl_read_attr_cmd_req (&read_req);
41
+ }
42
+ if (bind_req->req_dst_addr == temp_sensor.short_addr ) {
43
+ log_i (" The temperature sensor from address(0x%x) on endpoint(%d) successfully binds us" , temp_sensor.short_addr , temp_sensor.endpoint );
44
+ }
45
+ free (bind_req);
46
+ } else {
47
+ /* Bind failed, maybe retry the binding ? */
48
+
49
+ // esp_zb_zdo_device_bind_req(bind_req, bind_cb, bind_req);
50
+ }
51
+ }
52
+
53
+ static void find_cb (esp_zb_zdp_status_t zdo_status, uint16_t addr, uint8_t endpoint, void *user_ctx) {
54
+ if (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) {
55
+ log_i (" Found temperature sensor" );
56
+ /* Store the information of the remote device */
57
+ temp_sensor_device_params_t *sensor = (temp_sensor_device_params_t *)user_ctx;
58
+ sensor->endpoint = endpoint;
59
+ sensor->short_addr = addr;
60
+ esp_zb_ieee_address_by_short (sensor->short_addr , sensor->ieee_addr );
61
+ log_d (" Temperature sensor found: short address(0x%x), endpoint(%d)" , sensor->short_addr , sensor->endpoint );
62
+
63
+ /* 1. Send binding request to the sensor */
64
+ esp_zb_zdo_bind_req_param_t *bind_req = (esp_zb_zdo_bind_req_param_t *)calloc (sizeof (esp_zb_zdo_bind_req_param_t ), 1 );
65
+ bind_req->req_dst_addr = addr;
66
+ log_d (" Request temperature sensor to bind us" );
67
+
68
+ /* populate the src information of the binding */
69
+ memcpy (bind_req->src_address , sensor->ieee_addr , sizeof (esp_zb_ieee_addr_t ));
70
+ bind_req->src_endp = endpoint;
71
+ bind_req->cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT;
72
+ log_d (" Bind temperature sensor" );
73
+
74
+ /* populate the dst information of the binding */
75
+ bind_req->dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED;
76
+ esp_zb_get_long_address (bind_req->dst_address_u .addr_long );
77
+ bind_req->dst_endp = HA_THERMOSTAT_ENDPOINT;
78
+
79
+ log_i (" Request temperature sensor to bind us" );
80
+ esp_zb_zdo_device_bind_req (bind_req, bind_cb, bind_req);
81
+
82
+ /* 2. Send binding request to self */
83
+ bind_req = (esp_zb_zdo_bind_req_param_t *)calloc (sizeof (esp_zb_zdo_bind_req_param_t ), 1 );
84
+ bind_req->req_dst_addr = esp_zb_get_short_address ();
85
+
86
+ /* populate the src information of the binding */
87
+ esp_zb_get_long_address (bind_req->src_address );
88
+ bind_req->src_endp = HA_THERMOSTAT_ENDPOINT;
89
+ bind_req->cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT;
90
+
91
+ /* populate the dst information of the binding */
92
+ bind_req->dst_addr_mode = ESP_ZB_ZDO_BIND_DST_ADDR_MODE_64_BIT_EXTENDED;
93
+ memcpy (bind_req->dst_address_u .addr_long , sensor->ieee_addr , sizeof (esp_zb_ieee_addr_t ));
94
+ bind_req->dst_endp = endpoint;
95
+
96
+ log_i (" Bind temperature sensor" );
97
+ esp_zb_zdo_device_bind_req (bind_req, bind_cb, bind_req);
98
+ }
99
+ }
100
+
101
+ void ZigbeeThermostat::find_endpoint (esp_zb_zdo_match_desc_req_param_t *cmd_req) {
102
+ uint16_t cluster_list[] = {ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT};
103
+ param->profile_id = ESP_ZB_AF_HA_PROFILE_ID;
104
+ param->num_in_clusters = 1 ;
105
+ param->num_out_clusters = 0 ;
106
+ param->cluster_list = cluster_list;
107
+ esp_zb_zdo_match_cluster (param, user_cb, (void *)&temp_sensor);
108
+ }
0 commit comments