|
19 | 19 | #include <Matter.h> |
20 | 20 | #include <functional> |
21 | 21 |
|
| 22 | +using namespace esp_matter; |
| 23 | + |
22 | 24 | // Matter Endpoint Base Class. Controls the endpoint ID and allows the child class to overwrite attribute change call |
23 | 25 | class MatterEndPoint { |
24 | 26 | public: |
| 27 | + |
| 28 | + enum attrOperation_t { |
| 29 | + ATTR_SET = false, |
| 30 | + ATTR_UPDATE = true |
| 31 | + }; |
| 32 | + |
25 | 33 | uint16_t getEndPointId() { |
26 | 34 | return endpoint_id; |
27 | 35 | } |
| 36 | + |
28 | 37 | void setEndPointId(uint16_t ep) { |
29 | 38 | endpoint_id = ep; |
30 | 39 | } |
| 40 | + |
| 41 | + // helper functions for attribute manipulation |
| 42 | + attribute_t * getAttribute(uint32_t cluster_id, uint32_t attribute_id){ |
| 43 | + if (endpoint_id == 0) { |
| 44 | + log_e("Endpoint ID is not set"); |
| 45 | + return NULL; |
| 46 | + } |
| 47 | + endpoint_t *endpoint = endpoint::get(node::get(), endpoint_id); |
| 48 | + if (endpoint == NULL) { |
| 49 | + log_e("Endpoint [%d] not found", endpoint_id); |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + cluster_t *cluster = cluster::get(endpoint, cluster_id); |
| 53 | + if (cluster == NULL) { |
| 54 | + log_e("Cluster [%d] not found", cluster_id); |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + attribute_t *attribute = attribute::get(cluster, attribute_id); |
| 58 | + if (attribute == NULL) { |
| 59 | + log_e("Attribute [%d] not found", attribute_id); |
| 60 | + return NULL; |
| 61 | + } |
| 62 | + return attribute; |
| 63 | + } |
| 64 | + |
| 65 | + // get the value of an attribute from its cluster id and attribute it |
| 66 | + bool getAttributeVal(uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *attrVal){ |
| 67 | + attribute_t *attribute = getAttribute(cluster_id, attribute_id); |
| 68 | + if (attribute == NULL) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + if (attribute::get_val(attribute, attrVal) == ESP_OK) { |
| 72 | + log_v("GET_VAL Suceess for cluster %d, attribute %d with value %d", cluster_id, attribute_id, attrVal->val.u32); |
| 73 | + return true; |
| 74 | + } |
| 75 | + log_v("GET_VAL FAILED! for cluster %d, attribute %d with value %d", cluster_id, attribute_id, attrVal->val.u32); |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + // set the value of an attribute from its cluster id and attribute it |
| 80 | + bool setAttributeVal(uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *attrVal){ |
| 81 | + attribute_t *attribute = getAttribute(cluster_id, attribute_id); |
| 82 | + if (attribute == NULL) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + if (attribute::set_val(attribute, attrVal) == ESP_OK) { |
| 86 | + log_v("SET_VAL Suceess for cluster %d, attribute %d with value %d", cluster_id, attribute_id, attrVal->val.u32); |
| 87 | + return true; |
| 88 | + } |
| 89 | + log_v("SET_VAL FAILED! for cluster %d, attribute %d with value %d", cluster_id, attribute_id, attrVal->val.u32); |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + // update the value of an attribute from its cluster id and attribute it |
| 94 | + bool updateAttributeVal(uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *attrVal){ |
| 95 | + if (attribute::update(endpoint_id, cluster_id, attribute_id, attrVal) == ESP_OK) { |
| 96 | + log_v("Update Suceess for cluster %d, attribute %d with value %d", cluster_id, attribute_id, attrVal->val.u32); |
| 97 | + return true; |
| 98 | + } |
| 99 | + log_v("Update FAILED! for cluster %d, attribute %d with value %d", cluster_id, attribute_id, attrVal->val.u32); |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
31 | 103 | // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. |
32 | 104 | virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0; |
33 | 105 |
|
|
0 commit comments