|
| 1 | +#include "ZigbeeAnalogSensor.h" |
| 2 | +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
| 3 | + |
| 4 | +esp_zb_cluster_list_t *zigbee_analog_sensor_clusters_create(zigbee_analog_sensor_cfg_t *analog_sensor) { |
| 5 | + esp_zb_basic_cluster_cfg_t *basic_cfg = analog_sensor ? &(analog_sensor->basic_cfg) : NULL; |
| 6 | + esp_zb_identify_cluster_cfg_t *identify_cfg = analog_sensor ? &(analog_sensor->identify_cfg) : NULL; |
| 7 | + esp_zb_analog_value_cluster_cfg_t *analog_value_cfg = analog_sensor ? &(analog_sensor->analog_value_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_analog_value_cluster( |
| 12 | + cluster_list, esp_zb_analog_value_cluster_create(analog_value_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE |
| 13 | + ); |
| 14 | + return cluster_list; |
| 15 | +} |
| 16 | + |
| 17 | +ZigbeeAnalogSensor::ZigbeeAnalogSensor(uint8_t endpoint) : ZigbeeEP(endpoint) { |
| 18 | + _device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID; |
| 19 | + |
| 20 | + //Create custom analog sensor configuration |
| 21 | + zigbee_analog_sensor_cfg_t analog_sensor_cfg = ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG(); |
| 22 | + _cluster_list = zigbee_analog_sensor_clusters_create(&analog_sensor_cfg); |
| 23 | + |
| 24 | + _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0}; |
| 25 | +} |
| 26 | + |
| 27 | +void ZigbeeAnalogSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) { |
| 28 | + if (delta > 0) { |
| 29 | + log_e("Delta reporting is currently not supported by the sensor"); |
| 30 | + } |
| 31 | + esp_zb_zcl_reporting_info_t reporting_info; |
| 32 | + memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t)); |
| 33 | + reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV; |
| 34 | + reporting_info.ep = _endpoint; |
| 35 | + reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE; |
| 36 | + reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; |
| 37 | + reporting_info.attr_id = ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID; |
| 38 | + reporting_info.u.send_info.min_interval = min_interval; |
| 39 | + reporting_info.u.send_info.max_interval = max_interval; |
| 40 | + reporting_info.u.send_info.def_min_interval = min_interval; |
| 41 | + reporting_info.u.send_info.def_max_interval = max_interval; |
| 42 | + reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; |
| 43 | + reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC, esp_zb_lock_acquire(portMAX_DELAY); |
| 44 | + esp_zb_zcl_update_reporting_info(&reporting_info); |
| 45 | + esp_zb_lock_release(); |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +void ZigbeeAnalogSensor::setAnalog(float analog) { |
| 50 | + float zb_analog = analog; |
| 51 | + log_v("Updating sensor value..."); |
| 52 | + /* Update sensor measured value */ |
| 53 | + log_d("Setting value to %0.1f", zb_analog); |
| 54 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 55 | + esp_zb_zcl_set_attribute_val( |
| 56 | + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, |
| 57 | + &zb_analog, false |
| 58 | + ); |
| 59 | + esp_zb_lock_release(); |
| 60 | +} |
| 61 | + |
| 62 | +void ZigbeeAnalogSensor::report() { |
| 63 | + /* Send report attributes command */ |
| 64 | + esp_zb_zcl_report_attr_cmd_t report_attr_cmd; |
| 65 | + report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT; |
| 66 | + report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID; |
| 67 | + report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; |
| 68 | + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE; |
| 69 | + report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint; |
| 70 | + |
| 71 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 72 | + esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd); |
| 73 | + esp_zb_lock_release(); |
| 74 | + log_v("Analog report sent"); |
| 75 | +} |
| 76 | + |
| 77 | +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
0 commit comments