mirror of
https://github.com/espressif/esp-rainmaker.git
synced 2026-01-14 21:59:00 +00:00
Merge branch 'rmaker_rest_api' into 'master'
example: Add components controller_rest_apis and controller_custom_cluster for matter controller usage See merge request app-frameworks/esp-rainmaker!417
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
set(SRCS_LIST )
|
||||
set(INCLUDE_DIRS_LIST )
|
||||
|
||||
if (CONFIG_CONTROLLER_CUSTOM_CLUSTER_ENABLE)
|
||||
list(APPEND SRCS_LIST "matter_controller_cluster.cpp" "matter_controller_device_mgr.cpp")
|
||||
list(APPEND INCLUDE_DIRS_LIST "include")
|
||||
endif()
|
||||
|
||||
idf_component_register(SRCS ${SRCS_LIST}
|
||||
INCLUDE_DIRS ${INCLUDE_DIRS_LIST}
|
||||
REQUIRES controller_rest_apis chip esp_matter esp_matter_controller)
|
||||
9
examples/matter/common/controller_custom_cluster/Kconfig
Normal file
9
examples/matter/common/controller_custom_cluster/Kconfig
Normal file
@@ -0,0 +1,9 @@
|
||||
menu "Custom Controller Cluster"
|
||||
config CONTROLLER_CUSTOM_CLUSTER_ENABLE
|
||||
bool "Enable controller custom cluster"
|
||||
depends on ESP_MATTER_CONTROLLER_ENABLE && ESP_MATTER_ENABLE_MATTER_SERVER && RMAKER_REST_API_ENABLED
|
||||
default n
|
||||
help
|
||||
Enable the custom cluster of matter controller for Rainmaker Fabric suppport.
|
||||
|
||||
endmenu
|
||||
110
examples/matter/common/controller_custom_cluster/README.md
Normal file
110
examples/matter/common/controller_custom_cluster/README.md
Normal file
@@ -0,0 +1,110 @@
|
||||
# Matter Controller Cluster
|
||||
|
||||
The Matter Controller Cluster offers an interface for managing the ESP Matter Controller. It allows users to perform various tasks, such as authorizing the controller to login to the cloud, updating the controller's NOC to the NOC with CAT of administer privilege, and notifying the controller to update the device list.
|
||||
|
||||
## 1. Cluster Identifiers
|
||||
|
||||
| Identifier | Name |
|
||||
|------------|-----------------------|
|
||||
| 0x131BFC01 | **Matter Controller** |
|
||||
|
||||
## 2. Attributes
|
||||
|
||||
| ID | Name | Type | Constranint | Quality | Default | Access | Conformance |
|
||||
|--------|--------------------------|---------|-------------|---------|---------|--------|-------------|
|
||||
| 0x0000 | **RefreshToken** | string | | N | | R V | M |
|
||||
| 0x0001 | **RefreshTokenVerified** | bool | | N | false | R V | M |
|
||||
| 0x0002 | **Authorized** | bool | | N | false | R V | M |
|
||||
| 0x0003 | **UserNOCInstalled** | bool | | N | false | R V | M |
|
||||
| 0x0004 | **EndpointURL** | string | | N | | R V | M |
|
||||
| 0x0005 | **RainmakerGroupId** | string | | N | | R V | M |
|
||||
| 0x0006 | **UserNOCFabricIndex** | uint8_t | | N | | R V | M |
|
||||
|
||||
### 2.1 RefreshToken Attribute
|
||||
|
||||
This attribute stores the refresh token. For the HTTP REST Authenticated APIs, the refresh_token SHALL be used to fetch the access_token and the access_token will be passed in the "Authorization" HTTP header as the authentication token.
|
||||
|
||||
Note: The access_token validity will expire after 1 hour.
|
||||
|
||||
### 2.2 RefreshTokenVerified Attribute
|
||||
|
||||
This attribute indicates whether current refresh_token is verified. It will be updated to **True** after success authorization and set to **False** after receiving ResetRefreshToken.
|
||||
|
||||
### 2.3 Authorized Attribute
|
||||
|
||||
This attribute indicates the authorization status of the controller after joining the Rainmaker Fabric. After the access_token is fetched, the Authorized value will be set to true. And it will be set to false after 1 hour. If the HTTP REST Authenticated APIs fail with decription "Unauthorized", this attribute will be also set to false.
|
||||
|
||||
### 2.4 UserNOCInstalled Attribute
|
||||
|
||||
This attribute indicates whether the User NOC is installed in the controller. After the controller is authorized, the controller will generate a CSR and send it with the RainmakerGroupId to the cloud. The response from the cloud will include the new User NOC which will be installed in the controller.
|
||||
|
||||
### 2.5 EndpointURL Attribute
|
||||
|
||||
This attribute stores the Endpoint URL that is used for HTTP REST APIs.
|
||||
|
||||
### 2.6 RainmakerGroupId Attribute
|
||||
|
||||
This attribute stores the Rainmaker Group Id which is bound to the Matter Fabric Id.
|
||||
|
||||
### 2.7 UserNOCFabricIndex Attribute
|
||||
|
||||
This attribute stores the fabric index of the fabric where the user NOC is installed. It will be updated after the user NOC is installed.
|
||||
|
||||
## 3. Commands
|
||||
|
||||
| ID | Name | Direction | Response | Access | Conformance |
|
||||
|--------|--------------------------|----------------|----------|--------|-------------|
|
||||
| 0x0000 | **AppendRefreshToken** | client->server | Y | A | M |
|
||||
| 0x0001 | **ResetRefreshToken** | client->server | Y | A | M |
|
||||
| 0x0002 | **Authorize** | client->server | Y | A | M |
|
||||
| 0x0003 | **UpdateUserNOC** | client->server | Y | A | M |
|
||||
| 0x0004 | **UpdateDeviceList** | client->server | Y | A | M |
|
||||
|
||||
|
||||
### 3.1 AppendRefreshToken Command
|
||||
|
||||
The AppendRefreshToken command allows the controller to write the RefreshToken Attribute in several commands. It will append the current RefreshToken attribute. The RefreshToken is about 1700 Bytes which is much longer than the UDP MTU(1280 Bytes) so it should be send it separately.
|
||||
|
||||
The AppendRefreshToken command SHALL have the following data fields:
|
||||
|
||||
| ID | Name | Type | Constraint | Quality | Default | Comformance |
|
||||
|----|--------------------------|--------|------------|---------|---------|-------------|
|
||||
| 0 | **AppendedRefreshToken** | string | max 1024 | | | M |
|
||||
|
||||
#### 3.1.1 AppendedRefreshToken Field
|
||||
|
||||
This field is the string which will be appended to the current RefreshToken Attribute.
|
||||
|
||||
### 3.2 ResetRefreshToken Command
|
||||
|
||||
The ResetRefreshToken command allows devices to reset it RefreshToken to an empty string.
|
||||
|
||||
The ResetRefreshToken has no data field.
|
||||
|
||||
### 3.3 Authorize Command
|
||||
|
||||
The Authorize command allows the controller to login the cloud HTTP server and fetch the AccessToken with the HTTP REST Authenticated APIs.
|
||||
|
||||
After AccessToken is fetched, the controller will get the RainmakerGroupId according to the FabricId of the command object.
|
||||
|
||||
The Authorize command SHALL have the following data fields:
|
||||
|
||||
| ID | Name | Type | Constraint | Quality | Default | Comformance |
|
||||
|----|------------------|--------|------------|---------|---------|-------------|
|
||||
| 0 | **EndpointURL** | string | max 64 | | | M |
|
||||
|
||||
#### 3.3.1 EndpointURL Field
|
||||
|
||||
This field is the EndpointURL. After the controller is authorized successfully, this value will be written to the EndpointURL Attribute.
|
||||
|
||||
### 3.4 UpdateUserNOC Command
|
||||
|
||||
The UpdateUserNOC command allows the controller to fetch the Rainmaker User NOC after the is authorized. When receiving this command, the controller will generate a new CSR and send it with the RainmakerGroupId to the cloud. After receiving the response, it will install the RainmakerUserNOC in the response.
|
||||
|
||||
The UpdateUserNOC command has no data field.
|
||||
|
||||
### 3.5 UpdateDeviceList Command
|
||||
|
||||
The UpdateDeviceList command notifies the controller to update its maintaining Matter device list. After this command is received, the controller will query the device list in the Rainmaker Group(Matter Fabric) from the cloud. This command SHALL be called after the controller has updated its NOC and when a new Matter device is commissioned into the Matter Fabric.
|
||||
|
||||
The UpdateDeviceList command has no data field.
|
||||
@@ -0,0 +1,117 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <esp_matter.h>
|
||||
#include <esp_matter_core.h>
|
||||
|
||||
#define ESP_MATTER_RAINMAKER_MAX_ACCESS_TOKEN_LEN 2048
|
||||
#define ESP_MATTER_RAINMAKER_MAX_REFRESH_TOKEN_LEN 2048
|
||||
#define ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN 64
|
||||
#define ESP_MATTER_RAINMAKER_MAX_GROUP_ID_LEN 24
|
||||
#define HTTP_API_VERSION "v1"
|
||||
|
||||
namespace esp_matter {
|
||||
|
||||
namespace controller {
|
||||
esp_err_t controller_authorize(uint16_t endpoint_id);
|
||||
const char *get_current_access_token();
|
||||
}
|
||||
|
||||
namespace cluster {
|
||||
namespace matter_controller {
|
||||
|
||||
static constexpr chip::ClusterId Id = 0x131BFC01;
|
||||
|
||||
namespace attribute {
|
||||
|
||||
namespace refresh_token {
|
||||
static constexpr chip::AttributeId Id = 0x00000000;
|
||||
esp_err_t get(uint16_t endpoint_id, char *refresh_token);
|
||||
attribute_t *create(cluster_t *cluster, char *value, uint16_t length);
|
||||
} // namespace refresh_token
|
||||
|
||||
namespace refresh_token_verified {
|
||||
static constexpr chip::AttributeId Id = 0x00000001;
|
||||
esp_err_t get(uint16_t endpoint_id, bool &refresh_token_verified);
|
||||
attribute_t *create(cluster_t *cluster, bool refresh_token_verified);
|
||||
} // namespace refresh_token_verified
|
||||
|
||||
namespace authorized {
|
||||
static constexpr chip::AttributeId Id = 0x00000002;
|
||||
esp_err_t update(uint16_t endpoint_id, bool authorized);
|
||||
esp_err_t get(uint16_t endpoint_id, bool &authorized);
|
||||
attribute_t *create(cluster_t *cluster, bool value);
|
||||
} // namespace authorized
|
||||
|
||||
namespace user_noc_installed {
|
||||
static constexpr chip::AttributeId Id = 0x00000003;
|
||||
esp_err_t get(uint16_t endpoint_id, bool &user_noc_installed);
|
||||
attribute_t *create(cluster_t *cluster, bool value);
|
||||
} // namespace user_noc_installed
|
||||
|
||||
namespace endpoint_url {
|
||||
static constexpr chip::AttributeId Id = 0x00000004;
|
||||
esp_err_t get(uint16_t endpoint_id, char *endpoint_url);
|
||||
attribute_t *create(cluster_t *cluster, char *value, uint16_t length);
|
||||
} // namespace endpoint_url
|
||||
|
||||
namespace rainmaker_group_id {
|
||||
static constexpr chip::AttributeId Id = 0x00000005;
|
||||
esp_err_t get(uint16_t endpoint_id, char *group_id);
|
||||
attribute_t *create(cluster_t *cluster, char *value, uint16_t length);
|
||||
} // namespace rainmaker_group_id
|
||||
|
||||
namespace user_noc_fabric_index {
|
||||
static constexpr chip::AttributeId Id = 0x00000006;
|
||||
esp_err_t get(uint16_t endpoint_id, uint8_t &user_noc_fabric_index);
|
||||
attribute_t *create(cluster_t *cluster, uint8_t user_noc_fabric_index);
|
||||
} // namespace user_noc_fabric_index
|
||||
|
||||
} // namespace attribute
|
||||
|
||||
namespace command {
|
||||
|
||||
namespace append_refresh_token {
|
||||
static constexpr chip::CommandId Id = 0x00000000;
|
||||
command_t *create(cluster_t *cluster);
|
||||
} // namespace append_refresh_token
|
||||
|
||||
namespace reset_refresh_token {
|
||||
static constexpr chip::CommandId Id = 0x00000001;
|
||||
command_t *create(cluster_t *cluster);
|
||||
} // namespace reset_refresh_token
|
||||
|
||||
namespace authorize {
|
||||
static constexpr chip::CommandId Id = 0x00000002;
|
||||
command_t *create(cluster_t *cluster);
|
||||
} // namespace authorize
|
||||
|
||||
namespace update_user_noc {
|
||||
static constexpr chip::CommandId Id = 0x00000003;
|
||||
command_t *create(cluster_t *cluster);
|
||||
} // namespace update_user_noc
|
||||
|
||||
namespace update_device_list {
|
||||
static constexpr chip::CommandId Id = 0x00000004;
|
||||
command_t *create(cluster_t *cluster);
|
||||
} // namespace update_device_list
|
||||
|
||||
} // namespace command
|
||||
cluster_t *create(endpoint_t *endpoint, uint8_t flags);
|
||||
} // namespace matter_controller
|
||||
} // namespace cluster
|
||||
} // namespace esp_matter
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <matter_device.h>
|
||||
|
||||
namespace esp_matter {
|
||||
namespace controller {
|
||||
namespace device_mgr {
|
||||
|
||||
using endpoint_entry_t = endpoint_entry_t;
|
||||
using matter_device_t = matter_device_t;
|
||||
|
||||
typedef void (*device_list_update_callback_t)(void);
|
||||
|
||||
inline void free_device_list(matter_device_t *dev_list)
|
||||
{
|
||||
free_matter_device_list(dev_list);
|
||||
}
|
||||
|
||||
inline void print_device_list(matter_device_t *dev_list)
|
||||
{
|
||||
print_matter_device_list(dev_list);
|
||||
}
|
||||
|
||||
matter_device_t *get_device_list_clone();
|
||||
|
||||
matter_device_t *get_device_clone(uint64_t node_id);
|
||||
|
||||
matter_device_t *get_device_clone(char *rainmaker_node_id);
|
||||
|
||||
esp_err_t update_device_list(uint16_t endpoint_id);
|
||||
|
||||
esp_err_t init(uint16_t endpoint_id, device_list_update_callback_t dev_list_update_cb);
|
||||
} // namespace device_mgr
|
||||
} // namespace controller
|
||||
} // namespace esp_matter
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
Copyright (c) 2023 Project CHIP Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<configurator>
|
||||
<domain name="CHIP"/>
|
||||
<cluster>
|
||||
|
||||
<name>MatterController</name>
|
||||
<domain>CHIP</domain>
|
||||
<code>0x131BFC01</code>
|
||||
<define>MATTER_CONTROLLER_CLUSTER</define>
|
||||
|
||||
<description>Attributes and commands for matter_controller cluster.</description>
|
||||
|
||||
<globalAttribute side="either" code="0xFFFD" value="2"/>
|
||||
|
||||
<attribute side="server" code="0x0000" define="REFRESH_TOKEN" type="long_char_string" length="2048">RefreshToken</attribute>
|
||||
<attribute side="server" code="0x0001" define="REFRESH_TOKEN_VERIFIED" type="boolean" default="0">RefreshTokenVerified</attribute>
|
||||
<attribute side="server" code="0x0002" define="AUTHORIZED" type="boolean" default="0">Authorized</attribute>
|
||||
<attribute side="server" code="0x0003" define="USER_NOC_INSTALLED" type="boolean" default="0">UserNOCInstalled</attribute>
|
||||
<attribute side="server" code="0x0004" define="ENDPOINT_URL" type="char_string" length="64">EndpointUrl</attribute>
|
||||
<attribute side="server" code="0x0005" define="RMAKER_GROUP_ID" type="char_string" length="24">RmakerGroupId</attribute>
|
||||
<attribute side="server" code="0x0006" define="USER_NOC_FABRIC_INDEX" type="int8u" default="0">UserNOCFabricIndex</attribute>
|
||||
|
||||
<command source="client" code="0x00" name="AppendRefreshToken" optional="false">
|
||||
<description>AppendRefreshToken command.</description>
|
||||
<arg name="AppendedRefreshToken" type="char_string"/>
|
||||
</command>
|
||||
|
||||
<command source="client" code="0x01" name="ResetRefreshToken" optional="false">
|
||||
<description>ResetRefreshToken command.</description>
|
||||
</command>
|
||||
|
||||
<command source="client" code="0x02" name="Authorize" optional="false">
|
||||
<description>Authorize command.</description>
|
||||
<arg name="EndpointUrl" type="char_string"/>
|
||||
</command>
|
||||
|
||||
<command source="client" code="0x03" name="UpdateUserNOC" optional="false">
|
||||
<description>UpdateUserNOC command.</description>
|
||||
</command>
|
||||
|
||||
<command source="client" code="0x04" name="UpdateDeviceList" optional="false">
|
||||
<description>UpdateDeviceList command.</description>
|
||||
</command>
|
||||
|
||||
</cluster>
|
||||
</configurator>
|
||||
@@ -0,0 +1,796 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "esp_err.h"
|
||||
#include <esp_check.h>
|
||||
#include <esp_matter_controller_utils.h>
|
||||
#include <matter_controller_cluster.h>
|
||||
#include <matter_controller_device_mgr.h>
|
||||
#include <nvs_flash.h>
|
||||
|
||||
#include <app/ConcreteCommandPath.h>
|
||||
#include <app/server/Server.h>
|
||||
#include <app/util/attribute-storage.h>
|
||||
#include <lib/core/TLVReader.h>
|
||||
#include <lib/support/Span.h>
|
||||
#include <lib/support/TypeTraits.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <controller_rest_apis.h>
|
||||
|
||||
#define TAG "controller_custom_cluster"
|
||||
|
||||
using chip::ByteSpan;
|
||||
using chip::MutableByteSpan;
|
||||
using chip::to_underlying;
|
||||
using chip::app::ConcreteCommandPath;
|
||||
using chip::Platform::ScopedMemoryBufferWithSize;
|
||||
using chip::TLV::TLVReader;
|
||||
using namespace chip::DeviceLayer;
|
||||
using chip::System::Clock::Seconds32;
|
||||
|
||||
constexpr char *controller_namespace = "controller";
|
||||
|
||||
namespace esp_matter {
|
||||
namespace cluster {
|
||||
namespace matter_controller {
|
||||
|
||||
namespace attribute {
|
||||
|
||||
namespace refresh_token {
|
||||
|
||||
static esp_err_t update(uint16_t endpoint_id, const char *refresh_token)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(refresh_token, ESP_ERR_INVALID_ARG, TAG, "refresh_token cannot be NULL");
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err =
|
||||
nvs_open_from_partition(CONFIG_ESP_MATTER_NVS_PART_NAME, controller_namespace, NVS_READWRITE, &handle);
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to open controller namespace");
|
||||
char nvs_key[16];
|
||||
snprintf(nvs_key, 16, "rf_tk/%x", endpoint_id);
|
||||
if ((err = nvs_set_str(handle, nvs_key, refresh_token)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set refresh_token");
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
if ((err = nvs_commit(handle)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to commit nvs");
|
||||
}
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, char *refresh_token)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(refresh_token, ESP_ERR_INVALID_ARG, TAG, "refresh_token cannot be NULL");
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err =
|
||||
nvs_open_from_partition(CONFIG_ESP_MATTER_NVS_PART_NAME, controller_namespace, NVS_READONLY, &handle);
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to open controller namespace");
|
||||
char nvs_key[16];
|
||||
snprintf(nvs_key, 16, "rf_tk/%x", endpoint_id);
|
||||
size_t refresh_token_len = ESP_MATTER_RAINMAKER_MAX_REFRESH_TOKEN_LEN;
|
||||
err = nvs_get_str(handle, nvs_key, refresh_token, &refresh_token_len);
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
refresh_token_len = 0;
|
||||
err = ESP_OK;
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to get refresh_token");
|
||||
}
|
||||
refresh_token[refresh_token_len] = 0;
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, char *value, uint16_t length)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, refresh_token::Id, ATTRIBUTE_FLAG_NONVOLATILE,
|
||||
esp_matter_long_char_str(value, length));
|
||||
}
|
||||
|
||||
} // namespace refresh_token
|
||||
|
||||
namespace refresh_token_verified {
|
||||
|
||||
static esp_err_t update(uint16_t endpoint_id, bool refresh_token_verified)
|
||||
{
|
||||
esp_matter_attr_val_t val = esp_matter_bool(refresh_token_verified);
|
||||
uint32_t cluster_id = matter_controller::Id;
|
||||
uint32_t attribute_id = refresh_token_verified::Id;
|
||||
return esp_matter::attribute::update(endpoint_id, cluster_id, attribute_id, &val);
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, bool &refresh_token_verified)
|
||||
{
|
||||
node_t *node = node::get();
|
||||
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
|
||||
cluster_t *cluster = cluster::get(endpoint, matter_controller::Id);
|
||||
attribute_t *attribute = esp_matter::attribute::get(cluster, refresh_token_verified::Id);
|
||||
ESP_RETURN_ON_FALSE(attribute, ESP_FAIL, TAG, "Could not find refresh_token_verified attribue");
|
||||
esp_matter_attr_val_t raw_val = esp_matter_invalid(NULL);
|
||||
esp_matter::attribute::get_val(attribute, &raw_val);
|
||||
ESP_RETURN_ON_FALSE(raw_val.type == ESP_MATTER_VAL_TYPE_BOOLEAN, ESP_FAIL, TAG, "Invalid Attribute type");
|
||||
refresh_token_verified = raw_val.val.b;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, bool value)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, refresh_token_verified::Id, ATTRIBUTE_FLAG_NONVOLATILE,
|
||||
esp_matter_bool(value));
|
||||
}
|
||||
|
||||
} // namespace refresh_token_verified
|
||||
|
||||
namespace authorized {
|
||||
|
||||
esp_err_t update(uint16_t endpoint_id, bool authorized)
|
||||
{
|
||||
esp_matter_attr_val_t val = esp_matter_bool(authorized);
|
||||
uint32_t cluster_id = matter_controller::Id;
|
||||
uint32_t attribute_id = authorized::Id;
|
||||
return esp_matter::attribute::update(endpoint_id, cluster_id, attribute_id, &val);
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, bool &authorized)
|
||||
{
|
||||
node_t *node = node::get();
|
||||
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
|
||||
cluster_t *cluster = cluster::get(endpoint, matter_controller::Id);
|
||||
attribute_t *attribute = esp_matter::attribute::get(cluster, authorized::Id);
|
||||
ESP_RETURN_ON_FALSE(attribute, ESP_FAIL, TAG, "Could not find authorized attribue");
|
||||
esp_matter_attr_val_t raw_val = esp_matter_invalid(NULL);
|
||||
esp_matter::attribute::get_val(attribute, &raw_val);
|
||||
ESP_RETURN_ON_FALSE(raw_val.type == ESP_MATTER_VAL_TYPE_BOOLEAN, ESP_FAIL, TAG, "Invalid Attribute type");
|
||||
authorized = raw_val.val.b;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, bool value)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, authorized::Id, ATTRIBUTE_FLAG_NONVOLATILE, esp_matter_bool(value));
|
||||
}
|
||||
|
||||
} // namespace authorized
|
||||
|
||||
namespace user_noc_installed {
|
||||
|
||||
static esp_err_t update(uint16_t endpoint_id, bool user_noc_installed)
|
||||
{
|
||||
esp_matter_attr_val_t val = esp_matter_bool(user_noc_installed);
|
||||
uint32_t cluster_id = matter_controller::Id;
|
||||
uint32_t attribute_id = user_noc_installed::Id;
|
||||
return esp_matter::attribute::update(endpoint_id, cluster_id, attribute_id, &val);
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, bool &user_noc_installed)
|
||||
{
|
||||
node_t *node = node::get();
|
||||
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
|
||||
cluster_t *cluster = cluster::get(endpoint, matter_controller::Id);
|
||||
attribute_t *attribute = esp_matter::attribute::get(cluster, user_noc_installed::Id);
|
||||
ESP_RETURN_ON_FALSE(attribute, ESP_FAIL, TAG, "Could not find user_noc_installed attribue");
|
||||
esp_matter_attr_val_t raw_val = esp_matter_invalid(NULL);
|
||||
esp_matter::attribute::get_val(attribute, &raw_val);
|
||||
ESP_RETURN_ON_FALSE(raw_val.type == ESP_MATTER_VAL_TYPE_BOOLEAN, ESP_FAIL, TAG, "Invalid Attribute type");
|
||||
user_noc_installed = raw_val.val.b;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, bool value)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, user_noc_installed::Id, ATTRIBUTE_FLAG_NONVOLATILE,
|
||||
esp_matter_bool(value));
|
||||
}
|
||||
|
||||
} // namespace user_noc_installed
|
||||
|
||||
namespace endpoint_url {
|
||||
|
||||
static esp_err_t update(uint16_t endpoint_id, const char *endpoint_url)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(endpoint_url, ESP_ERR_INVALID_ARG, TAG, "endpoint_url cannot be NULL");
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err =
|
||||
nvs_open_from_partition(CONFIG_ESP_MATTER_NVS_PART_NAME, controller_namespace, NVS_READWRITE, &handle);
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to open controller namespace");
|
||||
char nvs_key[16];
|
||||
snprintf(nvs_key, 16, "ep_url/%x", endpoint_id);
|
||||
if ((err = nvs_set_str(handle, nvs_key, endpoint_url)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set endpoint_url");
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
if ((err = nvs_commit(handle)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to commit nvs");
|
||||
}
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, char *endpoint_url)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(endpoint_url, ESP_ERR_INVALID_ARG, TAG, "endpoint_url cannot be NULL");
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err =
|
||||
nvs_open_from_partition(CONFIG_ESP_MATTER_NVS_PART_NAME, controller_namespace, NVS_READONLY, &handle);
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to open controller namespace");
|
||||
char nvs_key[16];
|
||||
snprintf(nvs_key, 16, "ep_url/%x", endpoint_id);
|
||||
size_t endpoint_url_len = ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN;
|
||||
if ((err = nvs_get_str(handle, nvs_key, endpoint_url, &endpoint_url_len)) != ESP_OK) {
|
||||
endpoint_url_len = 0;
|
||||
ESP_LOGE(TAG, "Failed to get endpoint_url");
|
||||
}
|
||||
nvs_close(handle);
|
||||
endpoint_url[endpoint_url_len] = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, char *value, uint16_t length)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, endpoint_url::Id, ATTRIBUTE_FLAG_NONVOLATILE,
|
||||
esp_matter_char_str(value, length));
|
||||
}
|
||||
|
||||
} // namespace endpoint_url
|
||||
|
||||
namespace rainmaker_group_id {
|
||||
|
||||
static esp_err_t update(uint16_t endpoint_id, const char *group_id)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(group_id, ESP_ERR_INVALID_ARG, TAG, "group_id cannot be NULL");
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err =
|
||||
nvs_open_from_partition(CONFIG_ESP_MATTER_NVS_PART_NAME, controller_namespace, NVS_READWRITE, &handle);
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to open controller namespace");
|
||||
char nvs_key[16];
|
||||
snprintf(nvs_key, 16, "rmk_gid/%x", endpoint_id);
|
||||
if ((err = nvs_set_str(handle, nvs_key, group_id)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set rmaker group_id");
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
if ((err = nvs_commit(handle)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to commit nvs");
|
||||
}
|
||||
nvs_close(handle);
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, char *group_id)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(group_id, ESP_ERR_INVALID_ARG, TAG, "endpoint_url cannot be NULL");
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err =
|
||||
nvs_open_from_partition(CONFIG_ESP_MATTER_NVS_PART_NAME, controller_namespace, NVS_READONLY, &handle);
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to open controller namespace");
|
||||
char nvs_key[16];
|
||||
snprintf(nvs_key, 16, "rmk_gid/%x", endpoint_id);
|
||||
size_t group_id_len = ESP_MATTER_RAINMAKER_MAX_GROUP_ID_LEN;
|
||||
if ((err = nvs_get_str(handle, nvs_key, group_id, &group_id_len)) != ESP_OK) {
|
||||
group_id_len = 0;
|
||||
ESP_LOGE(TAG, "Failed to get rmaker group_id");
|
||||
}
|
||||
nvs_close(handle);
|
||||
group_id[group_id_len] = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, char *value, uint16_t length)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, rainmaker_group_id::Id, ATTRIBUTE_FLAG_NONVOLATILE,
|
||||
esp_matter_char_str(value, length));
|
||||
}
|
||||
|
||||
} // namespace rainmaker_group_id
|
||||
|
||||
namespace user_noc_fabric_index {
|
||||
|
||||
static esp_err_t update(uint16_t endpoint_id, uint8_t fabric_index)
|
||||
{
|
||||
esp_matter_attr_val_t val = esp_matter_uint8(fabric_index);
|
||||
uint32_t cluster_id = matter_controller::Id;
|
||||
uint32_t attribute_id = user_noc_fabric_index::Id;
|
||||
return esp_matter::attribute::update(endpoint_id, cluster_id, attribute_id, &val);
|
||||
}
|
||||
|
||||
esp_err_t get(uint16_t endpoint_id, uint8_t &fabric_index)
|
||||
{
|
||||
node_t *node = node::get();
|
||||
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
|
||||
cluster_t *cluster = cluster::get(endpoint, matter_controller::Id);
|
||||
attribute_t *attribute = esp_matter::attribute::get(cluster, user_noc_fabric_index::Id);
|
||||
ESP_RETURN_ON_FALSE(attribute, ESP_FAIL, TAG, "Could not find group_id attribue");
|
||||
esp_matter_attr_val_t raw_val = esp_matter_invalid(NULL);
|
||||
esp_matter::attribute::get_val(attribute, &raw_val);
|
||||
ESP_RETURN_ON_FALSE(raw_val.type == ESP_MATTER_VAL_TYPE_UINT8, ESP_FAIL, TAG, "Invalid Attribute type");
|
||||
fabric_index = raw_val.val.u8;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
attribute_t *create(cluster_t *cluster, uint8_t value)
|
||||
{
|
||||
return esp_matter::attribute::create(cluster, user_noc_fabric_index::Id, ATTRIBUTE_FLAG_NONVOLATILE,
|
||||
esp_matter_uint8(value));
|
||||
}
|
||||
|
||||
} // namespace user_noc_fabric_index
|
||||
|
||||
} // namespace attribute
|
||||
|
||||
namespace command {
|
||||
|
||||
esp_err_t parse_string_from_tlv(TLVReader &tlv_data, ScopedMemoryBufferWithSize<char> &str)
|
||||
{
|
||||
chip::TLV::TLVType outer;
|
||||
chip::CharSpan str_span;
|
||||
if (chip::TLV::kTLVType_Structure != tlv_data.GetType()) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (tlv_data.EnterContainer(outer) != CHIP_NO_ERROR) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
while (tlv_data.Next() == CHIP_NO_ERROR) {
|
||||
if (!chip::TLV::IsContextTag(tlv_data.GetTag())) {
|
||||
continue;
|
||||
}
|
||||
if (chip::TLV::TagNumFromTag(tlv_data.GetTag()) == 0) {
|
||||
chip::app::DataModel::Decode(tlv_data, str_span);
|
||||
}
|
||||
}
|
||||
tlv_data.ExitContainer(outer);
|
||||
ESP_RETURN_ON_FALSE(str_span.data() && str_span.size() > 0, ESP_FAIL, TAG, "Failed to decode the tlv_data");
|
||||
strncpy(str.Get(), str_span.data(), str_span.size());
|
||||
str[str_span.size()] = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t append_refresh_token_command_callback(const ConcreteCommandPath &command_path, TLVReader &tlv_data,
|
||||
void *opaque_ptr)
|
||||
{
|
||||
uint16_t endpoint_id = command_path.mEndpointId;
|
||||
uint32_t cluster_id = command_path.mClusterId;
|
||||
uint32_t command_id = command_path.mCommandId;
|
||||
|
||||
// Return if this is not the controller authorize command
|
||||
if (cluster_id != matter_controller::Id || command_id != matter_controller::command::append_refresh_token::Id) {
|
||||
ESP_LOGE(TAG, "Got matter_controller command callback for some other command. This should not happen.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ScopedMemoryBufferWithSize<char> append_str;
|
||||
append_str.Calloc(1025);
|
||||
ESP_RETURN_ON_FALSE(append_str.Get(), ESP_ERR_NO_MEM, TAG, "Failed to allocate memory for append_str");
|
||||
ESP_RETURN_ON_ERROR(parse_string_from_tlv(tlv_data, append_str), TAG,
|
||||
"Failed to parse appended_refresh_token from tlv_data");
|
||||
|
||||
ScopedMemoryBufferWithSize<char> refresh_token;
|
||||
refresh_token.Calloc(ESP_MATTER_RAINMAKER_MAX_REFRESH_TOKEN_LEN);
|
||||
ESP_RETURN_ON_FALSE(refresh_token.Get(), ESP_ERR_NO_MEM, TAG, "Failed to allocate memory for refresh_token");
|
||||
ESP_RETURN_ON_ERROR(attribute::refresh_token::get(endpoint_id, refresh_token.Get()), TAG,
|
||||
"Failed to get refresh_token");
|
||||
|
||||
size_t current_len = strnlen(refresh_token.Get(), refresh_token.AllocatedSize() - 1);
|
||||
size_t append_len = strnlen(append_str.Get(), 1024);
|
||||
strncpy(&refresh_token[current_len], append_str.Get(), append_len);
|
||||
refresh_token[current_len + append_len] = 0;
|
||||
ESP_RETURN_ON_ERROR(attribute::refresh_token::update(endpoint_id, refresh_token.Get()), TAG,
|
||||
"Failed to update refresh_token");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t reset_refresh_token_command_callback(const ConcreteCommandPath &command_path, TLVReader &tlv_data,
|
||||
void *opaque_ptr)
|
||||
{
|
||||
uint16_t endpoint_id = command_path.mEndpointId;
|
||||
uint32_t cluster_id = command_path.mClusterId;
|
||||
uint32_t command_id = command_path.mCommandId;
|
||||
|
||||
// Return if this is not the controller authorize command
|
||||
if (cluster_id != matter_controller::Id || command_id != matter_controller::command::reset_refresh_token::Id) {
|
||||
ESP_LOGE(TAG, "Got matter_controller command callback for some other command. This should not happen.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
const char *empty_str = "";
|
||||
attribute::refresh_token::update(endpoint_id, empty_str);
|
||||
attribute::refresh_token_verified::update(endpoint_id, false);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t authorize_command_callback(const ConcreteCommandPath &command_path, TLVReader &tlv_data,
|
||||
void *opaque_ptr)
|
||||
{
|
||||
uint16_t endpoint_id = command_path.mEndpointId;
|
||||
uint32_t cluster_id = command_path.mClusterId;
|
||||
uint32_t command_id = command_path.mCommandId;
|
||||
chip::app::CommandHandler *command_obj = (chip::app::CommandHandler *)opaque_ptr;
|
||||
|
||||
// Return if this is not the controller authorize command
|
||||
if (cluster_id != matter_controller::Id || command_id != matter_controller::command::authorize::Id) {
|
||||
ESP_LOGE(TAG, "Got matter_controller command callback for some other command. This should not happen.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ScopedMemoryBufferWithSize<char> endpoint_url;
|
||||
|
||||
// Alloc memory for ScopedMemoryBuffers
|
||||
endpoint_url.Calloc(ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN);
|
||||
ESP_RETURN_ON_FALSE(endpoint_url.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for endpoint_url");
|
||||
|
||||
// Parse the tlv data
|
||||
ESP_RETURN_ON_ERROR(parse_string_from_tlv(tlv_data, endpoint_url), TAG,
|
||||
"Failed to parse authorize command tlv data");
|
||||
// Flush acks before really slow work
|
||||
command_obj->FlushAcksRightAwayOnSlowCommand();
|
||||
// Update the endpoint URL
|
||||
ESP_RETURN_ON_ERROR(attribute::endpoint_url::update(endpoint_id, endpoint_url.Get()), TAG,
|
||||
"Failed to update endpoint_url");
|
||||
|
||||
ESP_RETURN_ON_ERROR(controller::controller_authorize(endpoint_id), TAG, "Failed to authorize the controller");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t install_user_noc(uint16_t endpoint_id, uint8_t fabric_index)
|
||||
{
|
||||
auto &fabric_table = chip::Server::GetInstance().GetFabricTable();
|
||||
const chip::FabricInfo *fabric_info = fabric_table.FindFabricWithIndex(fabric_index);
|
||||
uint64_t fabric_id = fabric_info->GetFabricId();
|
||||
uint8_t csr_der_buf[chip::Crypto::kMIN_CSR_Buffer_Size];
|
||||
MutableByteSpan csr_span(csr_der_buf);
|
||||
ScopedMemoryBufferWithSize<char> rainmaker_group_id;
|
||||
ScopedMemoryBufferWithSize<char> endpoint_url;
|
||||
ScopedMemoryBufferWithSize<unsigned char> noc_der;
|
||||
ScopedMemoryBufferWithSize<unsigned char> noc_matter_cert;
|
||||
MutableByteSpan matter_cert_noc;
|
||||
size_t noc_der_len;
|
||||
const char *access_token = controller::get_current_access_token();
|
||||
esp_err_t err = ESP_OK;
|
||||
|
||||
// Alloc memory for ScopedMemoryBuffers
|
||||
rainmaker_group_id.Calloc(ESP_MATTER_RAINMAKER_MAX_GROUP_ID_LEN);
|
||||
ESP_RETURN_ON_FALSE(rainmaker_group_id.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for rainmaker_group_id");
|
||||
endpoint_url.Calloc(ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN);
|
||||
ESP_RETURN_ON_FALSE(endpoint_url.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for endpoint_url");
|
||||
ESP_RETURN_ON_ERROR(attribute::endpoint_url::get(endpoint_id, endpoint_url.Get()), TAG,
|
||||
"Failed to get endpoint_url");
|
||||
noc_der.Calloc(chip::Credentials::kMaxDERCertLength);
|
||||
ESP_RETURN_ON_FALSE(noc_der.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for noc_der");
|
||||
noc_der_len = noc_der.AllocatedSize();
|
||||
noc_matter_cert.Calloc(chip::Credentials::kMaxCHIPCertLength);
|
||||
ESP_RETURN_ON_FALSE(noc_matter_cert.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for noc_matter_cert");
|
||||
matter_cert_noc = MutableByteSpan(noc_matter_cert.Get(), noc_matter_cert.AllocatedSize());
|
||||
|
||||
// Fetch ther rainmaker_group_id
|
||||
err = fetch_rainmaker_group_id(endpoint_url.Get(), access_token, fabric_id, rainmaker_group_id.Get(),
|
||||
rainmaker_group_id.AllocatedSize());
|
||||
if (err == ESP_ERR_INVALID_STATE) {
|
||||
attribute::authorized::update(endpoint_id, false);
|
||||
}
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to fetch rainmaker_group_id");
|
||||
// Update the rainmaker_group_id
|
||||
ESP_RETURN_ON_ERROR(attribute::rainmaker_group_id::update(endpoint_id, rainmaker_group_id.Get()), TAG,
|
||||
"Failed to update rainmaker_group_id");
|
||||
// Allocate Pending CSR
|
||||
chip::Server::GetInstance().GetFabricTable().AllocatePendingOperationalKey(chip::MakeOptional(fabric_index),
|
||||
csr_span);
|
||||
// Issue Controller NOC
|
||||
uint64_t matter_node_id = fabric_info->GetNodeId();
|
||||
err = issue_noc_with_csr(endpoint_url.Get(), access_token, CSR_TYPE_CONTROLLER, csr_span.data(), csr_span.size(),
|
||||
rainmaker_group_id.Get(), &matter_node_id, noc_der.Get(), &noc_der_len);
|
||||
if (err == ESP_ERR_INVALID_STATE) {
|
||||
attribute::authorized::update(endpoint_id, false);
|
||||
}
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to issue user NOC");
|
||||
|
||||
// Convert DER-Formated NOC to Matter Cert
|
||||
ESP_RETURN_ON_FALSE(chip::Credentials::ConvertX509CertToChipCert(
|
||||
ByteSpan{reinterpret_cast<const unsigned char *>(noc_der.Get()), noc_der_len},
|
||||
matter_cert_noc) == CHIP_NO_ERROR,
|
||||
ESP_FAIL, TAG, "Failed to convert DER-Formated NOC to Matter Cert");
|
||||
|
||||
// Update NOC
|
||||
ESP_RETURN_ON_FALSE(fabric_table.UpdatePendingFabricWithOperationalKeystore(fabric_index, matter_cert_noc,
|
||||
ByteSpan{}) == CHIP_NO_ERROR,
|
||||
ESP_FAIL, TAG, "Failed to update the Fabric NOC");
|
||||
ESP_RETURN_ON_FALSE(fabric_table.CommitPendingFabricData() == CHIP_NO_ERROR, ESP_FAIL, TAG,
|
||||
"Failed to commit the pending Fabric data");
|
||||
|
||||
// Start DNS server to advertise the new node-id of the new NOC
|
||||
chip::app::DnssdServer::Instance().StartServer();
|
||||
|
||||
// Update attribute
|
||||
ESP_RETURN_ON_ERROR(attribute::user_noc_installed::update(endpoint_id, true), TAG,
|
||||
"Failed to update user_noc_installed");
|
||||
ESP_RETURN_ON_ERROR(attribute::user_noc_fabric_index::update(endpoint_id, fabric_index), TAG,
|
||||
"Failed to update user_noc_fabric_index");
|
||||
// Update controller fabric index
|
||||
esp_matter::controller::set_fabric_index(fabric_index);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t update_user_noc_command_callback(const ConcreteCommandPath &command_path, TLVReader &tlv_data,
|
||||
void *opaque_ptr)
|
||||
{
|
||||
uint16_t endpoint_id = command_path.mEndpointId;
|
||||
uint32_t cluster_id = command_path.mClusterId;
|
||||
uint32_t command_id = command_path.mCommandId;
|
||||
chip::app::CommandHandler *command_obj = (chip::app::CommandHandler *)opaque_ptr;
|
||||
uint8_t fabric_index = command_obj->GetAccessingFabricIndex();
|
||||
|
||||
// Return if this is not the controller authorize command
|
||||
if (cluster_id != matter_controller::Id || command_id != matter_controller::command::update_user_noc::Id) {
|
||||
ESP_LOGE(TAG, "Got matter_controller command callback for some other command. This should not happen.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// Get the authorized
|
||||
bool authorized = false;
|
||||
ESP_RETURN_ON_ERROR(attribute::authorized::get(endpoint_id, authorized), TAG, "Failed to get authorized attribute");
|
||||
if (!authorized) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Flush acks before really slow work
|
||||
command_obj->FlushAcksRightAwayOnSlowCommand();
|
||||
// Install user NOC
|
||||
ESP_RETURN_ON_ERROR(install_user_noc(endpoint_id, fabric_index), TAG, "Failed to install user NOC");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t update_device_list_command_callback(const ConcreteCommandPath &command_path, TLVReader &tlv_data,
|
||||
void *opaque_ptr)
|
||||
{
|
||||
uint16_t endpoint_id = command_path.mEndpointId;
|
||||
uint32_t cluster_id = command_path.mClusterId;
|
||||
uint32_t command_id = command_path.mCommandId;
|
||||
chip::app::CommandHandler *command_obj = (chip::app::CommandHandler *)opaque_ptr;
|
||||
|
||||
// Return if this is not the controller authorize command
|
||||
if (cluster_id != matter_controller::Id || command_id != matter_controller::command::update_device_list::Id) {
|
||||
ESP_LOGE(TAG, "Got matter_controller command callback for some other command. This should not happen.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
// Flush acks before really slow work
|
||||
command_obj->FlushAcksRightAwayOnSlowCommand();
|
||||
|
||||
ESP_RETURN_ON_ERROR(controller::device_mgr::update_device_list(endpoint_id), TAG, "Failed to update device list");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
namespace append_refresh_token {
|
||||
command_t *create(cluster_t *cluster)
|
||||
{
|
||||
return esp_matter::command::create(cluster, append_refresh_token::Id, COMMAND_FLAG_ACCEPTED | COMMAND_FLAG_CUSTOM,
|
||||
append_refresh_token_command_callback);
|
||||
}
|
||||
} // namespace append_refresh_token
|
||||
|
||||
namespace reset_refresh_token {
|
||||
command_t *create(cluster_t *cluster)
|
||||
{
|
||||
return esp_matter::command::create(cluster, reset_refresh_token::Id, COMMAND_FLAG_ACCEPTED | COMMAND_FLAG_CUSTOM,
|
||||
reset_refresh_token_command_callback);
|
||||
}
|
||||
} // namespace reset_refresh_token
|
||||
|
||||
namespace authorize {
|
||||
command_t *create(cluster_t *cluster)
|
||||
{
|
||||
return esp_matter::command::create(cluster, authorize::Id, COMMAND_FLAG_ACCEPTED | COMMAND_FLAG_CUSTOM,
|
||||
authorize_command_callback);
|
||||
}
|
||||
} // namespace authorize
|
||||
|
||||
namespace update_user_noc {
|
||||
command_t *create(cluster_t *cluster)
|
||||
{
|
||||
return esp_matter::command::create(cluster, update_user_noc::Id, COMMAND_FLAG_ACCEPTED | COMMAND_FLAG_CUSTOM,
|
||||
update_user_noc_command_callback);
|
||||
}
|
||||
} // namespace update_user_noc
|
||||
|
||||
namespace update_device_list {
|
||||
command_t *create(cluster_t *cluster)
|
||||
{
|
||||
return esp_matter::command::create(cluster, update_device_list::Id, COMMAND_FLAG_ACCEPTED | COMMAND_FLAG_CUSTOM,
|
||||
update_device_list_command_callback);
|
||||
}
|
||||
} // namespace update_device_list
|
||||
|
||||
} // namespace command
|
||||
|
||||
using chip::app::AttributeAccessInterface;
|
||||
using chip::app::AttributeValueDecoder;
|
||||
using chip::app::AttributeValueEncoder;
|
||||
using chip::app::ConcreteDataAttributePath;
|
||||
using chip::app::ConcreteReadAttributePath;
|
||||
|
||||
class MatterControllerAttrAccess : public AttributeAccessInterface {
|
||||
public:
|
||||
MatterControllerAttrAccess()
|
||||
: AttributeAccessInterface(chip::Optional<chip::EndpointId>::Missing(), cluster::matter_controller::Id)
|
||||
{
|
||||
}
|
||||
|
||||
CHIP_ERROR Read(const ConcreteReadAttributePath &aPath, AttributeValueEncoder &aEncoder) override;
|
||||
CHIP_ERROR Write(const ConcreteDataAttributePath &aPath, AttributeValueDecoder &aDecoder) override;
|
||||
};
|
||||
|
||||
static esp_err_t encode_string_on_success(esp_err_t err, AttributeValueEncoder encoder, char *str, size_t max_buf_size)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "error before encode string");
|
||||
ESP_RETURN_ON_FALSE(encoder.Encode(chip::CharSpan(str, strnlen(str, max_buf_size))) == CHIP_NO_ERROR, ESP_FAIL, TAG,
|
||||
"Failed to encode string");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
CHIP_ERROR MatterControllerAttrAccess::Read(const ConcreteReadAttributePath &aPath, AttributeValueEncoder &aEncoder)
|
||||
{
|
||||
if (aPath.mClusterId != cluster::matter_controller::Id) {
|
||||
return CHIP_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
uint16_t endpoint_id = aPath.mEndpointId;
|
||||
esp_err_t err = ESP_OK;
|
||||
switch (aPath.mAttributeId) {
|
||||
case attribute::refresh_token::Id: {
|
||||
ScopedMemoryBufferWithSize<char> refresh_token;
|
||||
refresh_token.Alloc(ESP_MATTER_RAINMAKER_MAX_REFRESH_TOKEN_LEN);
|
||||
if (!refresh_token.Get()) {
|
||||
return CHIP_ERROR_NO_MEMORY;
|
||||
}
|
||||
err = attribute::refresh_token::get(endpoint_id, refresh_token.Get());
|
||||
err = encode_string_on_success(err, aEncoder, refresh_token.Get(), refresh_token.AllocatedSize());
|
||||
break;
|
||||
}
|
||||
case attribute::endpoint_url::Id: {
|
||||
ScopedMemoryBufferWithSize<char> endpoint_url;
|
||||
endpoint_url.Alloc(ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN);
|
||||
if (!endpoint_url.Get()) {
|
||||
return CHIP_ERROR_NO_MEMORY;
|
||||
}
|
||||
err = attribute::endpoint_url::get(endpoint_id, endpoint_url.Get());
|
||||
err = encode_string_on_success(err, aEncoder, endpoint_url.Get(), endpoint_url.AllocatedSize());
|
||||
break;
|
||||
}
|
||||
case attribute::rainmaker_group_id::Id: {
|
||||
ScopedMemoryBufferWithSize<char> rainmaker_group_id;
|
||||
rainmaker_group_id.Alloc(ESP_MATTER_RAINMAKER_MAX_GROUP_ID_LEN);
|
||||
if (!rainmaker_group_id.Get()) {
|
||||
return CHIP_ERROR_NO_MEMORY;
|
||||
}
|
||||
err = attribute::rainmaker_group_id::get(endpoint_id, rainmaker_group_id.Get());
|
||||
err = encode_string_on_success(err, aEncoder, rainmaker_group_id.Get(), rainmaker_group_id.AllocatedSize());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
return CHIP_ERROR_INTERNAL;
|
||||
}
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
CHIP_ERROR MatterControllerAttrAccess::Write(const ConcreteDataAttributePath &aPath, AttributeValueDecoder &aDecoder)
|
||||
{
|
||||
if (aPath.mClusterId != cluster::matter_controller::Id) {
|
||||
return CHIP_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
MatterControllerAttrAccess g_attr_access;
|
||||
|
||||
void controller_cluster_plugin_server_init_callback()
|
||||
{
|
||||
registerAttributeAccessOverride(&g_attr_access);
|
||||
}
|
||||
|
||||
cluster_t *create(endpoint_t *endpoint, uint8_t flags)
|
||||
{
|
||||
cluster_t *cluster = esp_matter::cluster::create(endpoint, Id, CLUSTER_FLAG_SERVER);
|
||||
if (!cluster) {
|
||||
ESP_LOGE(TAG, "Could not create cluster");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
set_plugin_server_init_callback(cluster, controller_cluster_plugin_server_init_callback);
|
||||
add_function_list(cluster, NULL, 0);
|
||||
|
||||
global::attribute::create_cluster_revision(cluster, 2);
|
||||
global::attribute::create_feature_map(cluster, 0);
|
||||
attribute::authorized::create(cluster, false);
|
||||
attribute::user_noc_installed::create(cluster, false);
|
||||
attribute::user_noc_fabric_index::create(cluster, 0);
|
||||
// Attributes managed internally
|
||||
attribute::refresh_token::create(cluster, NULL, 0);
|
||||
attribute::refresh_token_verified::create(cluster, false);
|
||||
attribute::endpoint_url::create(cluster, NULL, 0);
|
||||
attribute::rainmaker_group_id::create(cluster, NULL, 0);
|
||||
|
||||
command::append_refresh_token::create(cluster);
|
||||
command::reset_refresh_token::create(cluster);
|
||||
command::authorize::create(cluster);
|
||||
command::update_user_noc::create(cluster);
|
||||
command::update_device_list::create(cluster);
|
||||
|
||||
return cluster;
|
||||
}
|
||||
|
||||
} // namespace matter_controller
|
||||
} // namespace cluster
|
||||
|
||||
namespace controller {
|
||||
|
||||
static uint16_t s_access_token_expired_timer_endpoint_id = chip::kInvalidEndpointId;
|
||||
static char s_access_token[ESP_MATTER_RAINMAKER_MAX_ACCESS_TOKEN_LEN];
|
||||
|
||||
static void access_token_expired_callback(chip::System::Layer *systemLayer, void *appState)
|
||||
{
|
||||
uint16_t endpoint_id = s_access_token_expired_timer_endpoint_id;
|
||||
if (cluster::matter_controller::attribute::authorized::update(endpoint_id, false) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to update authorized attribute");
|
||||
return;
|
||||
}
|
||||
if (controller_authorize(endpoint_id) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to do authorizing");
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t controller_authorize(uint16_t endpoint_id)
|
||||
{
|
||||
ScopedMemoryBufferWithSize<char> refresh_token;
|
||||
ScopedMemoryBufferWithSize<char> endpoint_url;
|
||||
|
||||
// Alloc memory for ScopedMemoryBuffers
|
||||
refresh_token.Calloc(ESP_MATTER_RAINMAKER_MAX_REFRESH_TOKEN_LEN);
|
||||
ESP_RETURN_ON_FALSE(refresh_token.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for refresh_token");
|
||||
endpoint_url.Calloc(ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN);
|
||||
ESP_RETURN_ON_FALSE(endpoint_url.Get(), ESP_ERR_NO_MEM, TAG, "Failed to alloc memory for endpoint_url");
|
||||
|
||||
ESP_RETURN_ON_ERROR(cluster::matter_controller::attribute::endpoint_url::get(endpoint_id, endpoint_url.Get()), TAG,
|
||||
"Failed to get the endpoint_url");
|
||||
ESP_RETURN_ON_ERROR(cluster::matter_controller::attribute::refresh_token::get(endpoint_id, refresh_token.Get()),
|
||||
TAG, "Failed to get the refresh_token");
|
||||
|
||||
// Fetch the access_token
|
||||
ESP_RETURN_ON_ERROR(
|
||||
fetch_access_token(endpoint_url.Get(), refresh_token.Get(), s_access_token, sizeof(s_access_token)),
|
||||
TAG, "Failed to fetch access_token for authorizing");
|
||||
// Update the authorized attribute
|
||||
ESP_RETURN_ON_ERROR(cluster::matter_controller::attribute::authorized::update(endpoint_id, true), TAG,
|
||||
"Failed to update authorized attribute");
|
||||
ESP_RETURN_ON_ERROR(cluster::matter_controller::attribute::refresh_token_verified::update(endpoint_id, true),
|
||||
TAG, "Failed to update refresh_token_verified attribute");
|
||||
// The access token will be expired after one hour
|
||||
s_access_token_expired_timer_endpoint_id = endpoint_id;
|
||||
SystemLayer().CancelTimer(access_token_expired_callback, NULL);
|
||||
SystemLayer().StartTimer(Seconds32(3550), access_token_expired_callback, NULL);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
const char *get_current_access_token()
|
||||
{
|
||||
return s_access_token;
|
||||
}
|
||||
|
||||
} // namespace controller
|
||||
} // namespace esp_matter
|
||||
@@ -0,0 +1,239 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <controller_rest_apis.h>
|
||||
#include <esp_check.h>
|
||||
#include <esp_matter_controller_utils.h>
|
||||
#include <matter_controller_cluster.h>
|
||||
#include <matter_controller_device_mgr.h>
|
||||
|
||||
#include <lib/support/ScopedBuffer.h>
|
||||
|
||||
using chip::Platform::ScopedMemoryBufferWithSize;
|
||||
using namespace esp_matter::cluster::matter_controller::attribute;
|
||||
|
||||
#define TAG "controller_dev_mgr"
|
||||
|
||||
namespace esp_matter {
|
||||
namespace controller {
|
||||
namespace device_mgr {
|
||||
|
||||
static matter_device_t *s_matter_device_list = NULL;
|
||||
static device_list_update_callback_t s_device_list_update_cb = NULL;
|
||||
static QueueHandle_t s_task_queue = NULL;
|
||||
static TaskHandle_t s_device_mgr_task = NULL;
|
||||
static SemaphoreHandle_t s_device_mgr_mutex = NULL;
|
||||
typedef esp_err_t (*esp_matter_device_mgr_task_t)(void *);
|
||||
|
||||
typedef struct {
|
||||
esp_matter_device_mgr_task_t task;
|
||||
void *arg;
|
||||
} task_post_t;
|
||||
|
||||
class scoped_device_mgr_lock {
|
||||
public:
|
||||
scoped_device_mgr_lock() {
|
||||
if (s_device_mgr_mutex) {
|
||||
xSemaphoreTake(s_device_mgr_mutex, portMAX_DELAY);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "device mgr lock not initialized");
|
||||
}
|
||||
}
|
||||
~scoped_device_mgr_lock() {
|
||||
if (s_device_mgr_mutex) {
|
||||
xSemaphoreGive(s_device_mgr_mutex);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "device mgr lock not initialized");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static matter_device_t *clone_device(matter_device_t *dev) {
|
||||
matter_device_t *ret = (matter_device_t *)malloc(sizeof(matter_device_t));
|
||||
if (!ret) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for matter device struct");
|
||||
return NULL;
|
||||
}
|
||||
memcpy(ret, dev, sizeof(matter_device_t));
|
||||
ret->next = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
matter_device_t *get_device_list_clone() {
|
||||
matter_device_t *ret = NULL;
|
||||
scoped_device_mgr_lock dev_mgr_lock;
|
||||
matter_device_t *current = s_matter_device_list;
|
||||
while (current) {
|
||||
matter_device_t *tmp = clone_device(current);
|
||||
if (!tmp) {
|
||||
free_matter_device_list(ret);
|
||||
return NULL;
|
||||
}
|
||||
tmp->next = ret;
|
||||
ret = tmp;
|
||||
current = current->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static matter_device_t *get_device(uint64_t node_id) {
|
||||
matter_device_t *ret = s_matter_device_list;
|
||||
while (ret) {
|
||||
if (ret->node_id == node_id) {
|
||||
break;
|
||||
}
|
||||
ret = ret->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static matter_device_t *get_device(char *rainmaker_node_id) {
|
||||
if (!rainmaker_node_id) {
|
||||
return NULL;
|
||||
}
|
||||
matter_device_t *ret = s_matter_device_list;
|
||||
while (ret) {
|
||||
if (strncmp(ret->rainmaker_node_id, rainmaker_node_id,
|
||||
strnlen(ret->rainmaker_node_id,
|
||||
sizeof(ret->rainmaker_node_id))) == 0) {
|
||||
break;
|
||||
}
|
||||
ret = ret->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
matter_device_t *get_device_clone(uint64_t node_id) {
|
||||
return clone_device(get_device(node_id));
|
||||
}
|
||||
|
||||
matter_device_t *get_device_clone(char *rainmaker_node_id) {
|
||||
return clone_device(get_device(rainmaker_node_id));
|
||||
}
|
||||
|
||||
static esp_err_t update_device_list_task(void *endpoint_id_ptr) {
|
||||
esp_err_t err = ESP_OK;
|
||||
ScopedMemoryBufferWithSize<char> endpoint_url;
|
||||
ScopedMemoryBufferWithSize<char> rainmaker_group_id;
|
||||
uint16_t endpoint_id = *(uint16_t *)endpoint_id_ptr;
|
||||
free(endpoint_id_ptr);
|
||||
|
||||
endpoint_url.Calloc(ESP_MATTER_RAINMAKER_MAX_ENDPOINT_URL_LEN);
|
||||
ESP_RETURN_ON_FALSE(endpoint_url.Get(), ESP_ERR_NO_MEM, TAG,
|
||||
"Failed to alloc memory for endpoint_url");
|
||||
rainmaker_group_id.Calloc(ESP_MATTER_RAINMAKER_MAX_GROUP_ID_LEN);
|
||||
ESP_RETURN_ON_FALSE(rainmaker_group_id.Get(), ESP_ERR_NO_MEM, TAG,
|
||||
"Failed to alloc memory for rainmaker_group_id");
|
||||
|
||||
ESP_RETURN_ON_ERROR(endpoint_url::get(endpoint_id, endpoint_url.Get()), TAG,
|
||||
"Failed to get the endpoint_url");
|
||||
ESP_RETURN_ON_ERROR(
|
||||
rainmaker_group_id::get(endpoint_id, rainmaker_group_id.Get()), TAG,
|
||||
"Failed to get the rainmaker_group_id");
|
||||
const char *access_token = controller::get_current_access_token();
|
||||
|
||||
matter_device_t *dev_list = NULL;
|
||||
err = fetch_matter_device_list(endpoint_url.Get(), access_token,
|
||||
rainmaker_group_id.Get(), &dev_list);
|
||||
if (err == ESP_ERR_INVALID_STATE) {
|
||||
authorized::update(endpoint_id, false);
|
||||
}
|
||||
ESP_RETURN_ON_ERROR(err, TAG, "Failed to get the matter device list");
|
||||
print_matter_device_list(dev_list);
|
||||
{
|
||||
scoped_device_mgr_lock lock;
|
||||
s_matter_device_list = dev_list;
|
||||
}
|
||||
if (s_device_list_update_cb) {
|
||||
s_device_list_update_cb();
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t update_device_list(uint16_t endpoint_id) {
|
||||
if (!s_task_queue) {
|
||||
ESP_LOGE(
|
||||
TAG,
|
||||
"Failed to update device list as the task queue is not initialized");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
uint16_t *endpoint_id_ptr = (uint16_t *)malloc(sizeof(uint16_t));
|
||||
*endpoint_id_ptr = endpoint_id;
|
||||
task_post_t task_post = {
|
||||
.task = update_device_list_task,
|
||||
.arg = endpoint_id_ptr,
|
||||
};
|
||||
if (xQueueSend(s_task_queue, &task_post, portMAX_DELAY) != pdTRUE) {
|
||||
free(endpoint_id_ptr);
|
||||
ESP_LOGE(TAG, "Failed send update device list task");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void device_mgr_task(void *aContext) {
|
||||
s_task_queue = xQueueCreate(8 /* Queue Size */, sizeof(task_post_t));
|
||||
if (!s_task_queue) {
|
||||
ESP_LOGE(TAG, "Failed to create device mgr task queue");
|
||||
return;
|
||||
}
|
||||
s_device_mgr_mutex = xSemaphoreCreateRecursiveMutex();
|
||||
if (!s_device_mgr_mutex) {
|
||||
ESP_LOGE(TAG, "Failed to create device mgr lock");
|
||||
vQueueDelete(s_task_queue);
|
||||
return;
|
||||
}
|
||||
task_post_t task_post;
|
||||
while (true) {
|
||||
if (xQueueReceive(s_task_queue, &task_post, portMAX_DELAY) == pdTRUE) {
|
||||
task_post.task(task_post.arg);
|
||||
}
|
||||
}
|
||||
vQueueDelete(s_task_queue);
|
||||
vSemaphoreDelete(s_device_mgr_mutex);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
esp_err_t init(uint16_t endpoint_id,
|
||||
device_list_update_callback_t dev_list_update_cb) {
|
||||
if (s_device_mgr_task) {
|
||||
return ESP_OK;
|
||||
}
|
||||
uint8_t fabric_index;
|
||||
bool user_noc_installed = false;
|
||||
s_device_list_update_cb = dev_list_update_cb;
|
||||
if (xTaskCreate(device_mgr_task, "device_mgr", 4096, NULL, 5,
|
||||
&s_device_mgr_task) != pdTRUE) {
|
||||
ESP_LOGE(TAG, "Failed to create device mgr task");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
ESP_RETURN_ON_ERROR(user_noc_installed::get(endpoint_id, user_noc_installed),
|
||||
TAG, "Failed to get the user_noc_installed");
|
||||
if (user_noc_installed) {
|
||||
// get the user_noc_fabric_index and pass it to the controller.
|
||||
cluster::matter_controller::attribute::user_noc_fabric_index::get(
|
||||
endpoint_id, fabric_index);
|
||||
esp_matter::controller::set_fabric_index(fabric_index);
|
||||
// Do authorizing before update the device list
|
||||
ESP_RETURN_ON_ERROR(controller_authorize(endpoint_id), TAG,
|
||||
"Failed to do authorizing");
|
||||
ESP_RETURN_ON_ERROR(controller::device_mgr::update_device_list(endpoint_id),
|
||||
TAG, "Failed to update device list");
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
} // namespace device_mgr
|
||||
} // namespace controller
|
||||
} // namespace esp_matter
|
||||
@@ -0,0 +1,7 @@
|
||||
if (CONFIG_RMAKER_REST_API_ENABLED)
|
||||
list(APPEND SRCS_LIST "controller_rest_apis.c" "matter_device.c")
|
||||
list(APPEND INCLUDE_DIRS_LIST "include")
|
||||
endif()
|
||||
idf_component_register(SRCS ${SRCS_LIST}
|
||||
INCLUDE_DIRS ${INCLUDE_DIRS_LIST}
|
||||
REQUIRES mbedtls json_parser esp_http_client json_generator rmaker_common)
|
||||
8
examples/matter/common/controller_rest_apis/Kconfig
Normal file
8
examples/matter/common/controller_rest_apis/Kconfig
Normal file
@@ -0,0 +1,8 @@
|
||||
menu "RMaker REST API"
|
||||
config RMAKER_REST_API_ENABLED
|
||||
bool "Enable RMaker REST API"
|
||||
default y
|
||||
help
|
||||
Enable RainMaker REST API
|
||||
|
||||
endmenu
|
||||
1432
examples/matter/common/controller_rest_apis/controller_rest_apis.c
Normal file
1432
examples/matter/common/controller_rest_apis/controller_rest_apis.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,141 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include <esp_err.h>
|
||||
#include <matter_device.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
CSR_TYPE_USER,
|
||||
CSR_TYPE_CONTROLLER,
|
||||
} csr_type_t;
|
||||
|
||||
/**
|
||||
* Fetch access_token with refresh token
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] refresh_token The refresh token used to extend an existing session
|
||||
* @param[out] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] access_token_buf_len The access_token buffer size
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t fetch_access_token(const char *endpoint_url, const char *refresh_token, char *access_token,
|
||||
size_t access_token_buf_len);
|
||||
|
||||
/**
|
||||
* Fetch RainMaker Group ID corresponding to the Matter Fabric ID
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] fabric_id The Matter Fabric ID
|
||||
* @param[out] group_id The RainMaker Group ID corresponding to the Matter Fabric ID
|
||||
* @param[in] group_id_buf_len The group_id buffer size
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t fetch_rainmaker_group_id(const char *endpoint_url, const char *access_token, const uint64_t fabric_id,
|
||||
char *group_id, size_t group_id_buf_len);
|
||||
|
||||
/**
|
||||
* Fetch Matter Fabric ID corresponding to the RainMaker Group ID
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] group_id The RainMaker ID
|
||||
* @param[out] fabric_id The Matter Fabric ID corresponding to the RainMaker Group ID
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t fetch_matter_fabric_id(const char *endpoint_url, const char *access_token, const char *group_id,
|
||||
uint64_t *fabric_id);
|
||||
|
||||
/**
|
||||
* Fetch the Root CA certificate
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] rainmaker_group_id The Rainmaker Group ID for the Matter Fabric of the new NOC
|
||||
* @param[out] rcac_der The fetched RCAC file in DER format
|
||||
* @param[in,out] rcac_der_len The length of rcac_der buffer as input and the length of issued RCAC file in DER format as output
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t fetch_fabric_rcac_der(const char *endpoint_url, const char *access_token, const char *rainmaker_group_id,
|
||||
unsigned char *rcac_der, size_t *rcac_der_len);
|
||||
|
||||
/**
|
||||
* Fetch the IPK of Matter Fabric
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] rainmaker_group_id The Rainmaker Group ID for the Matter Fabric of the new NOC
|
||||
* @param[out] ipk_buf The IPK of the Matter Fabric
|
||||
* @param[in] ipk_buf_size The size of ipk_buf
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t fetch_fabric_ipk(const char *endpoint_url, const char *access_token, const char *rainmaker_group_id,
|
||||
uint8_t *ipk_buf, size_t ipk_buf_size);
|
||||
|
||||
/**
|
||||
* Issue Matter NOC with CSR
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] csr_type The CSR type of this NOC request
|
||||
* @param[in] csr_der The CSR bytes in DER format to issue the user NOC
|
||||
* @param[in] csr_der_len The length of the CSR bytes in DER format
|
||||
* @param[in] rainmaker_group_id The Rainmaker Group ID for the Matter Fabric of the new NOC
|
||||
* @param[in,out] matter_node_id The Matter Node ID in the subject DN of the new user NOC
|
||||
* @param[out] noc_der The issued NOC file in DER format
|
||||
* @param[in,out] noc_der_len The length of noc_der buffer as input and the length of issued NOC file in DER format as output
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t issue_noc_with_csr(const char *endpoint_url, const char *access_token, csr_type_t csr_type,
|
||||
const uint8_t *csr_der, const size_t csr_der_len, const char *rainmaker_group_id,
|
||||
uint64_t *matter_node_id, unsigned char *noc_der, size_t *noc_der_len);
|
||||
|
||||
/**
|
||||
* Fetch Matter device list in Matter Fabric(RainMaker Group)
|
||||
*
|
||||
* Note:this function will allocate memory for the newly fetched device list
|
||||
*
|
||||
* @param[in] endpoint_url The base endpoint URL
|
||||
* @param[in] access_token The access token to be passed in the "Authorization" HTTP header as the authentication token
|
||||
* @param[in] rainmaker_group_id The Rainmaker Group ID
|
||||
* @param[out] matter_dev_list The Matter device list to be fetched
|
||||
*
|
||||
* @return ESP_OK on sussess
|
||||
* @return error in case of failure
|
||||
*/
|
||||
esp_err_t fetch_matter_device_list(const char *endpoint_url, const char *access_token, const char *rainmaker_group_id,
|
||||
matter_device_t **matter_dev_list);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define ESP_MATTER_DEVICE_MAX_ENDPOINT 8
|
||||
#define ESP_MATTER_DEVICE_NAME_MAX_LEN 32
|
||||
|
||||
typedef struct endpoint_entry {
|
||||
uint16_t endpoint_id;
|
||||
uint32_t device_type_id;
|
||||
char device_name[ESP_MATTER_DEVICE_NAME_MAX_LEN];
|
||||
} endpoint_entry_t;
|
||||
|
||||
typedef struct matter_device {
|
||||
uint64_t node_id;
|
||||
char rainmaker_node_id[24];
|
||||
bool is_metadata_fetched;
|
||||
uint8_t endpoint_count;
|
||||
endpoint_entry_t endpoints[ESP_MATTER_DEVICE_MAX_ENDPOINT];
|
||||
bool reachable;
|
||||
bool is_rainmaker_device;
|
||||
struct matter_device *next;
|
||||
} matter_device_t;
|
||||
|
||||
/**
|
||||
* Free the allocated memory for matter device entries list
|
||||
*
|
||||
* @param[in] dev_list The device list to free
|
||||
*/
|
||||
void free_matter_device_list(matter_device_t *dev_list);
|
||||
|
||||
/**
|
||||
* Print the informations for matter device entries list
|
||||
*
|
||||
* @param[in] dev_list The device list to be printed
|
||||
*/
|
||||
void print_matter_device_list(matter_device_t *dev_list);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
54
examples/matter/common/controller_rest_apis/matter_device.c
Normal file
54
examples/matter/common/controller_rest_apis/matter_device.c
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <matter_device.h>
|
||||
|
||||
#define TAG "MATTER_DEVICE"
|
||||
|
||||
void free_matter_device_list(matter_device_t *dev_list)
|
||||
{
|
||||
matter_device_t *current = dev_list;
|
||||
while(current) {
|
||||
dev_list = dev_list->next;
|
||||
free(current);
|
||||
current = dev_list;
|
||||
}
|
||||
}
|
||||
|
||||
void print_matter_device_list(matter_device_t *dev_list)
|
||||
{
|
||||
uint16_t dev_index = 0;
|
||||
while (dev_list) {
|
||||
ESP_LOGI(TAG, "device %d : {", dev_index);
|
||||
ESP_LOGI(TAG, " rainmaker_node_id: %s,", dev_list->rainmaker_node_id);
|
||||
ESP_LOGI(TAG, " matter_node_id: 0x%llX,", dev_list->node_id);
|
||||
if (dev_list->is_metadata_fetched) {
|
||||
ESP_LOGI(TAG, " is_rainmaker_device: %s,", dev_list->is_rainmaker_device ? "true" : "false");
|
||||
ESP_LOGI(TAG, " is_online: %s,", dev_list->reachable ? "true" : "false");
|
||||
ESP_LOGI(TAG, " endpoints : [");
|
||||
for (size_t i = 0; i < dev_list->endpoint_count; ++i) {
|
||||
ESP_LOGI(TAG, " {");
|
||||
ESP_LOGI(TAG, " endpoint_id: %d,", dev_list->endpoints[i].endpoint_id);
|
||||
ESP_LOGI(TAG, " device_type_id: 0x%lx,", dev_list->endpoints[i].device_type_id);
|
||||
ESP_LOGI(TAG, " device_name: %s,", dev_list->endpoints[i].device_name);
|
||||
ESP_LOGI(TAG, " },");
|
||||
}
|
||||
ESP_LOGI(TAG, " ]");
|
||||
}
|
||||
ESP_LOGI(TAG, "}");
|
||||
dev_list = dev_list->next;
|
||||
dev_index++;
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ set(EXTRA_COMPONENT_DIRS
|
||||
"${ESP_MATTER_PATH}/device_hal/device"
|
||||
"${ESP_MATTER_PATH}/examples/common"
|
||||
"${RMAKER_PATH}/examples/common/app_insights"
|
||||
"${RMAKER_PATH}/examples/matter/common"
|
||||
"${RMAKER_PATH}/components/"
|
||||
${extra_components_dirs_append})
|
||||
|
||||
|
||||
@@ -59,9 +59,9 @@ CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=n
|
||||
CONFIG_MBEDTLS_HKDF_C=y
|
||||
|
||||
# Enable controller
|
||||
CONFIG_ENABLE_CHIP_CONTROLLER_BUILD=y
|
||||
CONFIG_ESP_MATTER_CONTROLLER_ENABLE=y
|
||||
CONFIG_ESP_MATTER_COMMISSIONER_ENABLE=n
|
||||
CONFIG_CONTROLLER_CUSTOM_CLUSTER_ENABLE=y
|
||||
|
||||
# Increase stack size
|
||||
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096
|
||||
|
||||
@@ -36,6 +36,7 @@ set(EXTRA_COMPONENT_DIRS
|
||||
"${ESP_MATTER_PATH}/device_hal/device"
|
||||
"${ESP_MATTER_PATH}/examples/common"
|
||||
"${RMAKER_PATH}/examples/common/app_insights"
|
||||
"${RMAKER_PATH}/examples/matter/common"
|
||||
"${RMAKER_PATH}/components"
|
||||
${extra_components_dirs_append})
|
||||
|
||||
|
||||
@@ -14,18 +14,10 @@ entries:
|
||||
else:
|
||||
* (default)
|
||||
|
||||
[mapping:wpa_supplicant]
|
||||
archive: libwpa_supplicant.a
|
||||
entries:
|
||||
if ESP_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y:
|
||||
* (extram_bss)
|
||||
else:
|
||||
* (default)
|
||||
|
||||
[mapping:esp_matter]
|
||||
archive: libesp_matter.a
|
||||
entries:
|
||||
if ESP_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y:
|
||||
* (extram_bss)
|
||||
else:
|
||||
* (default)
|
||||
* (default)
|
||||
|
||||
@@ -64,9 +64,9 @@ CONFIG_ESP_MATTER_MEM_ALLOC_MODE_EXTERNAL=y
|
||||
CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=n
|
||||
|
||||
# Enable controller
|
||||
CONFIG_ENABLE_CHIP_CONTROLLER_BUILD=y
|
||||
CONFIG_ESP_MATTER_CONTROLLER_ENABLE=y
|
||||
CONFIG_ESP_MATTER_COMMISSIONER_ENABLE=n
|
||||
CONFIG_CONTROLLER_CUSTOM_CLUSTER_ENABLE=y
|
||||
|
||||
# Increase stack size
|
||||
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096
|
||||
|
||||
Reference in New Issue
Block a user