|
| 1 | +#include "ZigbeeContactSwitch.h" |
| 2 | +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
| 3 | + |
| 4 | +esp_zb_cluster_list_t *zigbee_contact_switch_clusters_create(zigbee_contact_switch_cfg_t *contact_switch) { |
| 5 | + esp_zb_basic_cluster_cfg_t *basic_cfg = contact_switch ? &(contact_switch->basic_cfg) : NULL; |
| 6 | + esp_zb_identify_cluster_cfg_t *identify_cfg = contact_switch ? &(contact_switch->identify_cfg) : NULL; |
| 7 | + esp_zb_ias_zone_cluster_cfg_t *ias_zone_cfg = contact_switch ? &(contact_switch->ias_zone_cfg) : NULL; |
| 8 | + esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create(); |
| 9 | + esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 10 | + esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 11 | + esp_zb_cluster_list_add_ias_zone_cluster(cluster_list, esp_zb_ias_zone_cluster_create(ias_zone_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 12 | + return cluster_list; |
| 13 | +} |
| 14 | + |
| 15 | +ZigbeeContactSwitch::ZigbeeContactSwitch(uint8_t endpoint) : ZigbeeEP(endpoint) { |
| 16 | + _device_id = ESP_ZB_HA_IAS_ZONE_ID; |
| 17 | + _zone_status = 0; |
| 18 | + _zone_id = 0xff; |
| 19 | + _ias_cie_endpoint = 1; |
| 20 | + |
| 21 | + //Create custom contact switch configuration |
| 22 | + zigbee_contact_switch_cfg_t contact_switch_cfg = ZIGBEE_DEFAULT_CONTACT_SWITCH_CONFIG(); |
| 23 | + _cluster_list = zigbee_contact_switch_clusters_create(&contact_switch_cfg); |
| 24 | + |
| 25 | + _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_IAS_ZONE_ID, .app_device_version = 0}; |
| 26 | +} |
| 27 | + |
| 28 | +void ZigbeeContactSwitch::setIASClientEndpoint(uint8_t ep_number) { |
| 29 | + _ias_cie_endpoint = ep_number; |
| 30 | +} |
| 31 | + |
| 32 | +void ZigbeeContactSwitch::setClosed() { |
| 33 | + log_v("Setting Contact switch to closed"); |
| 34 | + uint8_t closed = 0; // ALARM1 = 0, ALARM2 = 0 |
| 35 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 36 | + esp_zb_zcl_set_attribute_val( |
| 37 | + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_IAS_ZONE_ZONESTATUS_ID, &closed, false |
| 38 | + ); |
| 39 | + esp_zb_lock_release(); |
| 40 | + _zone_status = closed; |
| 41 | + report(); |
| 42 | +} |
| 43 | + |
| 44 | +void ZigbeeContactSwitch::setOpen() { |
| 45 | + log_v("Setting Contact switch to open"); |
| 46 | + uint8_t open = ESP_ZB_ZCL_IAS_ZONE_ZONE_STATUS_ALARM1 | ESP_ZB_ZCL_IAS_ZONE_ZONE_STATUS_ALARM2; // ALARM1 = 1, ALARM2 = 1 |
| 47 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 48 | + esp_zb_zcl_set_attribute_val(_endpoint, ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_IAS_ZONE_ZONESTATUS_ID, &open, false); |
| 49 | + esp_zb_lock_release(); |
| 50 | + _zone_status = open; |
| 51 | + report(); |
| 52 | +} |
| 53 | + |
| 54 | +void ZigbeeContactSwitch::report() { |
| 55 | + /* Send IAS Zone status changed notification command */ |
| 56 | + |
| 57 | + esp_zb_zcl_ias_zone_status_change_notif_cmd_t status_change_notif_cmd; |
| 58 | + status_change_notif_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT; |
| 59 | + status_change_notif_cmd.zcl_basic_cmd.src_endpoint = _endpoint; |
| 60 | + status_change_notif_cmd.zcl_basic_cmd.dst_endpoint = _ias_cie_endpoint; //default is 1 |
| 61 | + memcpy(status_change_notif_cmd.zcl_basic_cmd.dst_addr_u.addr_long, _ias_cie_addr, sizeof(esp_zb_ieee_addr_t)); |
| 62 | + |
| 63 | + status_change_notif_cmd.zone_status = _zone_status; |
| 64 | + status_change_notif_cmd.extend_status = 0; |
| 65 | + status_change_notif_cmd.zone_id = _zone_id; |
| 66 | + status_change_notif_cmd.delay = 0; |
| 67 | + |
| 68 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 69 | + esp_zb_zcl_ias_zone_status_change_notif_cmd_req(&status_change_notif_cmd); |
| 70 | + esp_zb_lock_release(); |
| 71 | + log_v("IAS Zone status changed notification sent"); |
| 72 | +} |
| 73 | + |
| 74 | +void ZigbeeContactSwitch::zbIASZoneEnrollResponse(const esp_zb_zcl_ias_zone_enroll_response_message_t *message) { |
| 75 | + if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE) { |
| 76 | + log_v("IAS Zone Enroll Response: zone id(%d), status(%d)", message->zone_id, message->response_code); |
| 77 | + if (message->response_code == ESP_ZB_ZCL_IAS_ZONE_ENROLL_RESPONSE_CODE_SUCCESS) { |
| 78 | + log_v("IAS Zone Enroll Response: success"); |
| 79 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 80 | + memcpy( |
| 81 | + _ias_cie_addr, |
| 82 | + (*(esp_zb_ieee_addr_t *) |
| 83 | + esp_zb_zcl_get_attribute(_endpoint, ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_IAS_ZONE_IAS_CIE_ADDRESS_ID) |
| 84 | + ->data_p), |
| 85 | + sizeof(esp_zb_ieee_addr_t) |
| 86 | + ); |
| 87 | + esp_zb_lock_release(); |
| 88 | + _zone_id = message->zone_id; |
| 89 | + } |
| 90 | + |
| 91 | + } else { |
| 92 | + log_w("Received message ignored. Cluster ID: %d not supported for On/Off Light", message->info.cluster); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
0 commit comments