esp_matter: Support multiple device types for the endpoint

This commit is contained in:
WanqQixiang
2022-08-30 15:39:05 +08:00
parent 16f5af3d58
commit 18c5d4a596
5 changed files with 52 additions and 48 deletions
+24 -21
View File
@@ -52,6 +52,7 @@ using chip::DeviceLayer::ThreadStackMgr;
#endif
#define ESP_MATTER_NVS_PART_NAME "nvs"
#define ESP_MATTER_MAX_DEVICE_TYPE_COUNT 16
static const char *TAG = "esp_matter_core";
@@ -97,7 +98,8 @@ typedef struct _cluster {
typedef struct _endpoint {
uint16_t endpoint_id;
uint32_t device_type_id;
uint8_t device_type_count;
uint32_t device_type_ids[ESP_MATTER_MAX_DEVICE_TYPE_COUNT];
uint16_t flags;
_cluster_t *cluster_list;
EmberAfEndpointType *endpoint_type;
@@ -352,13 +354,8 @@ esp_err_t enable(endpoint_t *endpoint)
current_endpoint->endpoint_type = endpoint_type;
/* Device types */
/** TODO: This assumes only 1 device type per endpoint. Also, it is hardcoded for bridge device types. Change it. */
int default_device_version = 1;
int device_type_count = 1;
if (current_endpoint->flags & ENDPOINT_FLAG_BRIDGE) {
device_type_count++;
}
EmberAfDeviceType *device_types_ptr = (EmberAfDeviceType *)calloc(device_type_count, sizeof(EmberAfDeviceType));
EmberAfDeviceType *device_types_ptr = (EmberAfDeviceType *)calloc(current_endpoint->device_type_count, sizeof(EmberAfDeviceType));
if (!device_types_ptr) {
ESP_LOGE(TAG, "Couldn't allocate device_types");
free(endpoint_type);
@@ -366,15 +363,11 @@ esp_err_t enable(endpoint_t *endpoint)
/* goto cleanup is not used here to avoid 'crosses initialization' of device_types below */
return ESP_ERR_NO_MEM;
}
device_types_ptr[0].deviceId = current_endpoint->device_type_id;
device_types_ptr[0].deviceVersion = default_device_version;
if (current_endpoint->flags & ENDPOINT_FLAG_BRIDGE) {
device_types_ptr[1].deviceId = current_endpoint->endpoint_id == 0 ?
endpoint::bridge::get_device_type_id() :
endpoint::bridged_node::get_device_type_id();
device_types_ptr[1].deviceVersion = default_device_version;
for (size_t i = 0; i < current_endpoint->device_type_count; ++i) {
device_types_ptr[i].deviceId = current_endpoint->device_type_ids[i];
device_types_ptr[i].deviceVersion = default_device_version;
}
chip::Span<EmberAfDeviceType> device_types(device_types_ptr, device_type_count);
chip::Span<EmberAfDeviceType> device_types(device_types_ptr, current_endpoint->device_type_count);
current_endpoint->device_types_ptr = device_types_ptr;
/* Clusters */
@@ -1476,7 +1469,7 @@ endpoint_t *create(node_t *node, uint8_t flags, void *priv_data)
/* Set */
endpoint->endpoint_id = current_node->current_endpoint_id++;
endpoint->device_type_id = 0xFFFF'FFFF;
endpoint->device_type_count = 0;
endpoint->flags = flags;
endpoint->priv_data = priv_data;
@@ -1593,25 +1586,35 @@ uint16_t get_id(endpoint_t *endpoint)
return current_endpoint->endpoint_id;
}
esp_err_t set_device_type_id(endpoint_t *endpoint, uint32_t device_type_id)
esp_err_t add_device_type_id(endpoint_t *endpoint, uint32_t device_type_id)
{
if (!endpoint) {
ESP_LOGE(TAG, "Endpoint cannot be NULL");
return ESP_ERR_INVALID_ARG;
}
_endpoint_t *current_endpoint = (_endpoint_t *)endpoint;
current_endpoint->device_type_id = device_type_id;
if (current_endpoint->device_type_count >= ESP_MATTER_MAX_DEVICE_TYPE_COUNT) {
ESP_LOGE(TAG, "Could not add a new device type to the endpoint");
return ESP_FAIL;
}
current_endpoint->device_type_ids[current_endpoint->device_type_count] = device_type_id;
current_endpoint->device_type_count++;
return ESP_OK;
}
uint32_t get_device_type_id(endpoint_t *endpoint)
uint32_t *get_device_type_ids(endpoint_t *endpoint, uint8_t *device_type_count_ptr)
{
if (!endpoint) {
ESP_LOGE(TAG, "Endpoint cannot be NULL");
return 0xFFFF'FFFF;
return NULL;
}
if (!device_type_count_ptr) {
ESP_LOGE(TAG, "device type count pointer cannot be NULL");
return NULL;
}
_endpoint_t *current_endpoint = (_endpoint_t *)endpoint;
return current_endpoint->device_type_id;
*device_type_count_ptr = current_endpoint->device_type_count;
return current_endpoint->device_type_ids;
}
void *get_priv_data(uint16_t endpoint_id)
+9 -8
View File
@@ -196,9 +196,9 @@ endpoint_t *get_next(endpoint_t *endpoint);
*/
uint16_t get_id(endpoint_t *endpoint);
/** Set device type ID
/** Add device type ID
*
* Set the device type ID for the endpoint.
* Add the device type ID for the endpoint.
*
* @param[in] endpoint Endpoint handle.
* @param[in] device_type_id Device type ID.
@@ -206,18 +206,19 @@ uint16_t get_id(endpoint_t *endpoint);
* @return ESP_OK on success.
* @return error in case of failure.
*/
esp_err_t set_device_type_id(endpoint_t *endpoint, uint32_t device_type_id);
esp_err_t add_device_type_id(endpoint_t *endpoint, uint32_t device_type_id);
/** Get device type ID
/** Get device type ID array
*
* Get the device type ID for the endpoint.
* Get the device type ID array for the endpoint.
*
* @param[in] endpoint Endpoint handle.
* @param[out] device_type_count_ptr the pointer of device type ID array length.
*
* @return device type ID on success.
* @return 0xFFFF'FFFF in case of failure or if the device type ID was not set.
* @return device type ID array on success.
* @return NULL when the endpoint or the device_type_count_ptr is NULL.
*/
uint32_t get_device_type_id(endpoint_t *endpoint);
uint32_t *get_device_type_ids(endpoint_t *endpoint, uint8_t *device_type_count_ptr);
/** Get private data
*
+17 -17
View File
@@ -60,7 +60,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
access_control::create(endpoint, CLUSTER_FLAG_SERVER);
@@ -98,7 +98,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -123,7 +123,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -150,7 +150,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -179,7 +179,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -208,7 +208,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
@@ -232,7 +232,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
@@ -257,7 +257,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
@@ -283,7 +283,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -308,7 +308,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -335,7 +335,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -359,7 +359,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -393,7 +393,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
bridged_device_basic::create(endpoint, &(config->bridged_device_basic), CLUSTER_FLAG_SERVER);
@@ -417,7 +417,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
descriptor::create(endpoint, CLUSTER_FLAG_SERVER);
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER);
@@ -441,7 +441,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
groups::create(endpoint, NULL, CLUSTER_FLAG_CLIENT);
@@ -464,7 +464,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
occupancy_sensing::create(endpoint, &(config->occupancy_sensing), CLUSTER_FLAG_SERVER);
@@ -486,7 +486,7 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat
ESP_LOGE(TAG, "Could not create endpoint");
return NULL;
}
set_device_type_id(endpoint, get_device_type_id());
add_device_type_id(endpoint, get_device_type_id());
identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
boolean_state::create(endpoint, &(config->boolean_state), CLUSTER_FLAG_SERVER);
@@ -49,7 +49,7 @@ static esp_err_t blemesh_bridge_init_bridged_onoff_light(esp_matter_bridge_devic
}
on_off::config_t config;
on_off::create(dev->endpoint, &config, CLUSTER_MASK_SERVER, ESP_MATTER_NONE_FEATURE_ID);
endpoint::set_device_type_id(dev->endpoint, endpoint::on_off_light::get_device_type_id());
endpoint::add_device_type_id(dev->endpoint, endpoint::on_off_light::get_device_type_id());
if (endpoint::enable(dev->endpoint) != ESP_OK) {
ESP_LOGE(TAG, "ESP Matter enable dynamic endpoint failed");
endpoint::destroy(dev->node, dev->endpoint);
@@ -29,7 +29,7 @@ static esp_err_t zigbee_bridge_init_bridged_onoff_light(esp_matter_bridge_device
on_off::config_t config;
on_off::create(dev->endpoint, &config, CLUSTER_MASK_SERVER, ESP_MATTER_NONE_FEATURE_ID);
endpoint::set_device_type_id(dev->endpoint, endpoint::on_off_light::get_device_type_id());
endpoint::add_device_type_id(dev->endpoint, endpoint::on_off_light::get_device_type_id());
if (endpoint::enable(dev->endpoint) != ESP_OK) {
ESP_LOGE(TAG, "ESP Matter enable dynamic endpoint failed");
endpoint::destroy(dev->node, dev->endpoint);