|
| 1 | +#include "ZigbeeVibrationSensor.h" |
| 2 | +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
| 3 | + |
| 4 | +esp_zb_cluster_list_t *zigbee_vibration_sensor_clusters_create(zigbee_vibration_sensor_cfg_t *vibration_sensor) { |
| 5 | + esp_zb_basic_cluster_cfg_t *basic_cfg = vibration_sensor ? &(vibration_sensor->basic_cfg) : NULL; |
| 6 | + esp_zb_identify_cluster_cfg_t *identify_cfg = vibration_sensor ? &(vibration_sensor->identify_cfg) : NULL; |
| 7 | + esp_zb_ias_zone_cluster_cfg_t *ias_zone_cfg = vibration_sensor ? &(vibration_sensor->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 | +ZigbeeVibrationSensor::ZigbeeVibrationSensor(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 vibration sensor configuration |
| 22 | + zigbee_vibration_sensor_cfg_t vibration_sensor_cfg = ZIGBEE_DEFAULT_VIBRATION_SENSOR_CONFIG(); |
| 23 | + _cluster_list = zigbee_vibration_sensor_clusters_create(&vibration_sensor_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 ZigbeeVibrationSensor::setIASClientEndpoint(uint8_t ep_number) { |
| 29 | + _ias_cie_endpoint = ep_number; |
| 30 | +} |
| 31 | + |
| 32 | +void ZigbeeVibrationSensor::setVibration(bool sensed) { |
| 33 | + log_v("Setting Vibration sensor to %s", sensed ? "sensed" : "not sensed"); |
| 34 | + uint8_t vibration = (uint8_t)sensed; |
| 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, &vibration, false |
| 38 | + ); |
| 39 | + esp_zb_lock_release(); |
| 40 | + _zone_status = vibration; |
| 41 | + report(); |
| 42 | +} |
| 43 | + |
| 44 | +void ZigbeeVibrationSensor::report() { |
| 45 | + /* Send IAS Zone status changed notification command */ |
| 46 | + |
| 47 | + esp_zb_zcl_ias_zone_status_change_notif_cmd_t status_change_notif_cmd; |
| 48 | + status_change_notif_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT; |
| 49 | + status_change_notif_cmd.zcl_basic_cmd.src_endpoint = _endpoint; |
| 50 | + status_change_notif_cmd.zcl_basic_cmd.dst_endpoint = _ias_cie_endpoint; //default is 1 |
| 51 | + memcpy(status_change_notif_cmd.zcl_basic_cmd.dst_addr_u.addr_long, _ias_cie_addr, sizeof(esp_zb_ieee_addr_t)); |
| 52 | + |
| 53 | + status_change_notif_cmd.zone_status = _zone_status; |
| 54 | + status_change_notif_cmd.extend_status = 0; |
| 55 | + status_change_notif_cmd.zone_id = _zone_id; |
| 56 | + status_change_notif_cmd.delay = 0; |
| 57 | + |
| 58 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 59 | + esp_zb_zcl_ias_zone_status_change_notif_cmd_req(&status_change_notif_cmd); |
| 60 | + esp_zb_lock_release(); |
| 61 | + log_v("IAS Zone status changed notification sent"); |
| 62 | +} |
| 63 | + |
| 64 | +void ZigbeeVibrationSensor::zbIASZoneEnrollResponse(const esp_zb_zcl_ias_zone_enroll_response_message_t *message) { |
| 65 | + if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE) { |
| 66 | + log_v("IAS Zone Enroll Response: zone id(%d), status(%d)", message->zone_id, message->response_code); |
| 67 | + if (message->response_code == ESP_ZB_ZCL_IAS_ZONE_ENROLL_RESPONSE_CODE_SUCCESS) { |
| 68 | + log_v("IAS Zone Enroll Response: success"); |
| 69 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 70 | + memcpy( |
| 71 | + _ias_cie_addr, |
| 72 | + (*(esp_zb_ieee_addr_t *) |
| 73 | + 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) |
| 74 | + ->data_p), |
| 75 | + sizeof(esp_zb_ieee_addr_t) |
| 76 | + ); |
| 77 | + esp_zb_lock_release(); |
| 78 | + _zone_id = message->zone_id; |
| 79 | + } |
| 80 | + |
| 81 | + } else { |
| 82 | + log_w("Received message ignored. Cluster ID: %d not supported for On/Off Light", message->info.cluster); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
0 commit comments