Skip to content

Commit 7776aa5

Browse files
committed
Add Thermostat + fix enum
1 parent 3764810 commit 7776aa5

File tree

4 files changed

+135
-9
lines changed

4 files changed

+135
-9
lines changed

libraries/Zigbee/src/Zigbee_core.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
194194
log_i("Device started up in %s factory-reset mode", esp_zb_bdb_is_factory_new() ? "" : "non");
195195
if (esp_zb_bdb_is_factory_new()) {
196196
// Role specific code
197-
if ((zigbee_role_t)Zigbee._role == Zigbee_Coordinator) {
197+
if ((zigbee_role_t)Zigbee._role == ZIGBEE_COORDINATOR) {
198198
log_i("Start network formation");
199199
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_FORMATION);
200200
} else {
@@ -207,7 +207,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
207207
log_i("Device rebooted");
208208

209209
// Implement opening network for joining after reboot if set
210-
if ((zigbee_role_t)Zigbee._role == Zigbee_Coordinator && Zigbee._open_network > 0) {
210+
if ((zigbee_role_t)Zigbee._role == ZIGBEE_COORDINATOR && Zigbee._open_network > 0) {
211211
log_i("Openning network for joining for %d seconds", Zigbee._open_network);
212212
esp_zb_bdb_open_network(Zigbee._open_network);
213213
}
@@ -218,7 +218,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
218218
}
219219
break;
220220
case ESP_ZB_BDB_SIGNAL_FORMATION: // Coordinator
221-
if ((zigbee_role_t)Zigbee._role == Zigbee_Coordinator) {
221+
if ((zigbee_role_t)Zigbee._role == ZIGBEE_COORDINATOR) {
222222
if (err_status == ESP_OK) {
223223
esp_zb_ieee_addr_t extended_pan_id;
224224
esp_zb_get_extended_pan_id(extended_pan_id);
@@ -235,7 +235,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
235235
}
236236
break;
237237
case ESP_ZB_BDB_SIGNAL_STEERING: // Router and End Device
238-
if ((zigbee_role_t)Zigbee._role == Zigbee_Coordinator) {
238+
if ((zigbee_role_t)Zigbee._role == ZIGBEE_COORDINATOR) {
239239
if (err_status == ESP_OK) {
240240
log_i("Network steering started");
241241
}
@@ -256,7 +256,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
256256
}
257257
break;
258258
case ESP_ZB_ZDO_SIGNAL_DEVICE_ANNCE: // Coordinator
259-
if ((zigbee_role_t)Zigbee._role == Zigbee_Coordinator) {
259+
if ((zigbee_role_t)Zigbee._role == ZIGBEE_COORDINATOR) {
260260
dev_annce_params = (esp_zb_zdo_signal_device_annce_params_t *)esp_zb_app_signal_get_params(p_sg_p);
261261
log_i("New device commissioned or rejoined (short: 0x%04hx)", dev_annce_params->device_short_addr);
262262
esp_zb_zdo_match_desc_req_param_t cmd_req;
@@ -286,7 +286,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
286286
}
287287
break;
288288
case ESP_ZB_NWK_SIGNAL_PERMIT_JOIN_STATUS: // Coordinator
289-
if ((zigbee_role_t)Zigbee._role == Zigbee_Coordinator) {
289+
if ((zigbee_role_t)Zigbee._role == ZIGBEE_COORDINATOR) {
290290
if (err_status == ESP_OK) {
291291
if (*(uint8_t *)esp_zb_app_signal_get_params(p_sg_p)) {
292292
log_i("Network(0x%04hx) is open for %d seconds", esp_zb_get_pan_id(), *(uint8_t *)esp_zb_app_signal_get_params(p_sg_p));

libraries/Zigbee/src/Zigbee_core.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ typedef struct {
2020

2121
// enum of Zigbee Roles
2222
typedef enum {
23-
Zigbee_Coordinator = 0,
24-
Zigbee_Router = 1,
25-
Zigbee_End_Device = 2
23+
ZIGBEE_COORDINATOR = 0,
24+
ZIGBEE_ROUTER = 1,
25+
ZIGBEE_END_DEVICE = 2
2626
} zigbee_role_t;
2727

2828
// default Zigbee configuration for each role
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Class of Zigbee Temperature sensor endpoint inherited from common EP class */
2+
3+
#pragma once
4+
5+
#include "Zigbee_ep.h"
6+
#include "ha/esp_zigbee_ha_standard.h"
7+
8+
class ZigbeeThermostat : public Zigbee_EP {
9+
public:
10+
ZigbeeThermostat(uint8_t endpoint, void (*cb)(const esp_zb_zcl_set_attr_value_message_t *message));
11+
//ZigbeeThermostat(uint8_t endpoint, void (*cb)(const esp_zb_zcl_set_attr_value_message_t *message)) : ZigbeeTempSensor(endpoint, cb, NULL) {}
12+
~ZigbeeThermostat();
13+
14+
void find_endpoint(esp_zb_zdo_match_desc_req_param_t *cmd_req);
15+
static void bind_cb(esp_zb_zdp_status_t zdo_status, void *user_ctx);
16+
static void find_cb(esp_zb_zdp_status_t zdo_status, uint16_t addr, uint8_t endpoint, void *user_ctx);
17+
18+
};

0 commit comments

Comments
 (0)