|
| 1 | +#include "ZigbeeAnalog.h" |
| 2 | +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
| 3 | + |
| 4 | +esp_zb_cluster_list_t *zigbee_analog_clusters_create(zigbee_analog_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_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create(); |
| 8 | + esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 9 | + esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 10 | + return cluster_list; |
| 11 | +} |
| 12 | + |
| 13 | +ZigbeeAnalog::ZigbeeAnalog(uint8_t endpoint) : ZigbeeEP(endpoint) { |
| 14 | + _device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID; |
| 15 | + |
| 16 | + //Create custom analog sensor configuration |
| 17 | + zigbee_analog_cfg_t analog_cfg = ZIGBEE_DEFAULT_ANALOG_CONFIG(); |
| 18 | + _cluster_list = zigbee_analog_clusters_create(&analog_cfg); |
| 19 | + |
| 20 | + _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}; |
| 21 | +} |
| 22 | + |
| 23 | +void ZigbeeAnalog::addAnalogValue() { |
| 24 | + esp_zb_cluster_list_add_analog_value_cluster(_cluster_list, esp_zb_analog_value_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 25 | + _analog_clusters |= ANALOG_VALUE; |
| 26 | +} |
| 27 | + |
| 28 | +void ZigbeeAnalog::addAnalogInput() { |
| 29 | + esp_zb_cluster_list_add_analog_input_cluster(_cluster_list, esp_zb_analog_input_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 30 | + _analog_clusters |= ANALOG_INPUT; |
| 31 | +} |
| 32 | + |
| 33 | +void ZigbeeAnalog::addAnalogOutput() { |
| 34 | + esp_zb_cluster_list_add_analog_output_cluster(_cluster_list, esp_zb_analog_output_cluster_create(NULL), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 35 | + _analog_clusters |= ANALOG_OUTPUT; |
| 36 | +} |
| 37 | + |
| 38 | +//set attribute method -> method overridden in child class |
| 39 | +void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) { |
| 40 | + if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT) { |
| 41 | + if (message->attribute.id == ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_SINGLE) { |
| 42 | + float analog_output = *(float *)message->attribute.data.value; |
| 43 | + analogOutputChanged(analog_output); |
| 44 | + } else { |
| 45 | + log_w("Received message ignored. Attribute ID: %d not supported for Analog Output", message->attribute.id); |
| 46 | + } |
| 47 | + } else { |
| 48 | + log_w("Received message ignored. Cluster ID: %d not supported for Analog endpoint", message->info.cluster); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void ZigbeeAnalog::analogOutputChanged(float analog_output) { |
| 53 | + if (_on_analog_output_change) { |
| 54 | + _on_analog_output_change(analog_output); |
| 55 | + } else { |
| 56 | + log_w("No callback function set for analog output change"); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void ZigbeeAnalog::setAnalogValue(float analog) { |
| 61 | + if (!(_analog_clusters & ANALOG_VALUE)) { |
| 62 | + log_e("Analog Value cluster not added"); |
| 63 | + return; |
| 64 | + } |
| 65 | + // float zb_analog = analog; |
| 66 | + log_d("Setting analog value to %.1f", analog); |
| 67 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 68 | + esp_zb_zcl_set_attribute_val( |
| 69 | + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID, &analog, false |
| 70 | + ); |
| 71 | + esp_zb_lock_release(); |
| 72 | +} |
| 73 | + |
| 74 | +void ZigbeeAnalog::setAnalogInput(float analog) { |
| 75 | + if (!(_analog_clusters & ANALOG_INPUT)) { |
| 76 | + log_e("Analog Input cluster not added"); |
| 77 | + return; |
| 78 | + } |
| 79 | + // float zb_analog = analog; |
| 80 | + log_d("Setting analog input to %.1f", analog); |
| 81 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 82 | + esp_zb_zcl_set_attribute_val( |
| 83 | + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID, &analog, false |
| 84 | + ); |
| 85 | + esp_zb_lock_release(); |
| 86 | +} |
| 87 | + |
| 88 | +void ZigbeeAnalog::reportAnalogInput() { |
| 89 | + /* Send report attributes command */ |
| 90 | + esp_zb_zcl_report_attr_cmd_t report_attr_cmd; |
| 91 | + report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT; |
| 92 | + report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_ANALOG_INPUT_PRESENT_VALUE_ID; |
| 93 | + report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI; |
| 94 | + report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_INPUT; |
| 95 | + report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint; |
| 96 | + |
| 97 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 98 | + esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd); |
| 99 | + esp_zb_lock_release(); |
| 100 | + log_v("Analog Input report sent"); |
| 101 | +} |
| 102 | + |
| 103 | +void ZigbeeAnalog::setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta) { |
| 104 | + esp_zb_zcl_reporting_info_t reporting_info; |
| 105 | + memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t)); |
| 106 | + reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV; |
| 107 | + reporting_info.ep = _endpoint; |
| 108 | + reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT; |
| 109 | + reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE; |
| 110 | + reporting_info.attr_id = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID; |
| 111 | + reporting_info.u.send_info.min_interval = min_interval; |
| 112 | + reporting_info.u.send_info.max_interval = max_interval; |
| 113 | + reporting_info.u.send_info.def_min_interval = min_interval; |
| 114 | + reporting_info.u.send_info.def_max_interval = max_interval; |
| 115 | + reporting_info.u.send_info.delta.s32 = delta; |
| 116 | + reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID; |
| 117 | + reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC; |
| 118 | + |
| 119 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 120 | + esp_zb_zcl_update_reporting_info(&reporting_info); |
| 121 | + esp_zb_lock_release(); |
| 122 | +} |
| 123 | + |
| 124 | +#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
0 commit comments