Add client interaction APIs for interaction actions(read, subscribe, write)

This commit is contained in:
WanqQixiang
2024-03-13 14:02:49 +08:00
parent 3fe308ff1e
commit 8134461866
6 changed files with 349 additions and 1781 deletions
File diff suppressed because it is too large Load Diff
+22 -337
View File
@@ -124,117 +124,6 @@ esp_err_t group_send_toggle(uint8_t fabric_index, uint16_t group_id);
} // namespace command
} // namespace on_off
namespace level_control {
namespace command {
esp_err_t send_move(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t move_mode, uint8_t rate,
uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_to_level(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t level,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_to_level_with_on_off(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t level,
uint16_t transition_time);
esp_err_t send_move_with_on_off(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t move_mode,
uint8_t rate);
esp_err_t send_step(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t step_mode, uint8_t step_size,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_step_with_on_off(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t step_mode,
uint8_t step_size, uint16_t transition_time);
esp_err_t send_stop(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t option_mask,
uint8_t option_override);
esp_err_t send_stop_with_on_off(peer_device_t *remote_device, uint16_t remote_endpoint_id);
esp_err_t group_send_move(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint8_t rate, uint8_t option_mask,
uint8_t option_override);
esp_err_t group_send_move_to_level(uint8_t fabric_index, uint16_t group_id, uint8_t level, uint16_t transition_time,
uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_to_level_with_on_off(uint8_t fabric_index, uint16_t group_id, uint8_t level,
uint16_t transition_time);
esp_err_t group_send_move_with_on_off(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint8_t rate);
esp_err_t group_send_step(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, uint8_t step_size,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_step_with_on_off(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, uint8_t step_size,
uint16_t transition_time);
esp_err_t group_send_stop(uint8_t fabric_index, uint16_t group_id, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_stop_with_on_off(uint8_t fabric_index, uint16_t group_id);
} // namespace command
} // namespace level_control
namespace color_control {
namespace command {
esp_err_t send_move_hue(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t move_mode, uint8_t rate,
uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_saturation(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t move_mode,
uint8_t rate, uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_to_hue(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t hue, uint8_t direction,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_to_hue_and_saturation(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t hue,
uint8_t saturation, uint16_t transition_time, uint8_t option_mask,
uint8_t option_override);
esp_err_t send_move_to_saturation(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t saturation,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_step_hue(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t step_mode, uint8_t step_size,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_step_saturation(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t step_mode,
uint8_t step_size, uint16_t transition_time, uint8_t option_mask,
uint8_t option_override);
esp_err_t send_move_to_color(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t color_x,
uint16_t color_y, uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_color(peer_device_t *remote_device, uint16_t remote_endpoint_id, int16_t rate_x, int16_t rate_y,
uint8_t option_mask, uint8_t option_override);
esp_err_t send_step_color(peer_device_t *remote_device, uint16_t remote_endpoint_id, int16_t step_x, int16_t step_y,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t send_move_to_color_temperature(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint16_t color_temperature_mireds, uint16_t transition_time,
uint8_t option_mask, uint8_t option_override);
esp_err_t send_stop_move_step(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t option_mask,
uint8_t option_override);
esp_err_t send_move_color_temperature(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t move_mode,
uint16_t rate, uint16_t color_temperature_minimum_mireds,
uint16_t color_temperature_maximum_mireds, uint8_t option_mask,
uint8_t option_override);
esp_err_t send_step_color_temperature(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t step_mode,
uint16_t step_size, uint16_t transition_time,
uint16_t color_temperature_minimum_mireds,
uint16_t color_temperature_maximum_mireds, uint8_t option_mask,
uint8_t option_override);
esp_err_t group_send_move_hue(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint8_t rate,
uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint8_t rate,
uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_to_hue(uint8_t fabric_index, uint16_t group_id, uint8_t hue, uint8_t direction,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_to_hue_and_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t hue,
uint8_t saturation, uint16_t transition_time, uint8_t option_mask,
uint8_t option_override);
esp_err_t group_send_move_to_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t saturation,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_step_hue(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, uint8_t step_size,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_step_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, uint8_t step_size,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_to_color(uint8_t fabric_index, uint16_t group_id, uint16_t color_x, uint16_t color_y,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_color(uint8_t fabric_index, uint16_t group_id, int16_t rate_x, int16_t rate_y,
uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_step_color(uint8_t fabric_index, uint16_t group_id, int16_t step_x, int16_t step_y,
uint16_t transition_time, uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_move_to_color_temperature(uint8_t fabric_index, uint16_t group_id,
uint16_t color_temperature_mireds, uint16_t transition_time,
uint8_t option_mask, uint8_t option_override);
esp_err_t group_send_stop_move_step(uint8_t fabric_index, uint16_t group_id, uint8_t option_mask,
uint8_t option_override);
esp_err_t group_send_move_color_temperature(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint16_t rate,
uint16_t color_temperature_minimum_mireds,
uint16_t color_temperature_maximum_mireds, uint8_t option_mask,
uint8_t option_override);
esp_err_t group_send_step_color_temperature(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode,
uint16_t step_size, uint16_t transition_time,
uint16_t color_temperature_minimum_mireds,
uint16_t color_temperature_maximum_mireds, uint8_t option_mask,
uint8_t option_override);
} // namespace command
} // namespace color_control
namespace identify {
namespace command {
esp_err_t send_identify(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t identify_time);
@@ -245,231 +134,27 @@ esp_err_t send_trigger_effect(peer_device_t *remote_device, uint16_t remote_endp
uint8_t effect_variant);
} // namespace command
} // namespace identify
namespace group_key_management {
namespace command {
using group_keyset_struct = chip::app::Clusters::GroupKeyManagement::Structs::GroupKeySetStruct::Type;
using keyset_read_callback =
void (*)(void *, const chip::app::Clusters::GroupKeyManagement::Commands::KeySetRead::Type::ResponseType &);
esp_err_t send_keyset_write(peer_device_t *remote_device, uint16_t remote_endpoint_id,
group_keyset_struct &group_keyset);
esp_err_t send_keyset_read(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t keyset_id,
keyset_read_callback read_callback);
} // namespace command
} // namespace group_key_management
namespace groups {
namespace command {
using add_group_callback = void (*)(void *,
const chip::app::Clusters::Groups::Commands::AddGroup::Type::ResponseType &);
using view_group_callback = void (*)(void *,
const chip::app::Clusters::Groups::Commands::ViewGroup::Type::ResponseType &);
using remove_group_callback = void (*)(void *,
const chip::app::Clusters::Groups::Commands::RemoveGroup::Type::ResponseType &);
esp_err_t send_add_group(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id, char *group_name,
add_group_callback add_group_cb);
esp_err_t send_view_group(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
view_group_callback view_group_cb);
esp_err_t send_remove_group(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
remove_group_callback remove_group_cb);
} // namespace command
} // namespace groups
namespace scenes_management {
namespace command {
using extension_field_sets = chip::app::DataModel::List<chip::app::Clusters::ScenesManagement::Structs::ExtensionFieldSet::Type>;
using add_scene_callback = void (*)(void *,
const chip::app::Clusters::ScenesManagement::Commands::AddScene::Type::ResponseType &);
using view_scene_callback = void (*)(void *,
const chip::app::Clusters::ScenesManagement::Commands::ViewScene::Type::ResponseType &);
using remove_scene_callback = void (*)(void *,
const chip::app::Clusters::ScenesManagement::Commands::RemoveScene::Type::ResponseType &);
using remove_all_scenes_callback =
void (*)(void *, const chip::app::Clusters::ScenesManagement::Commands::RemoveAllScenes::Type::ResponseType &);
using store_scene_callback = void (*)(void *,
const chip::app::Clusters::ScenesManagement::Commands::StoreScene::Type::ResponseType &);
using get_scene_membership_callback =
void (*)(void *, const chip::app::Clusters::ScenesManagement::Commands::GetSceneMembership::Type::ResponseType &);
esp_err_t send_add_scene(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id, uint8_t scene_id,
uint16_t transition_time, char *scene_name, extension_field_sets &efs,
add_scene_callback add_scene_cb);
esp_err_t send_view_scene(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
uint8_t scene_id, view_scene_callback view_scene_cb);
esp_err_t send_remove_scene(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
uint8_t scene_id, remove_scene_callback remove_scene_cb);
esp_err_t send_remove_all_scenes(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
remove_all_scenes_callback remove_all_scenes_cb);
esp_err_t send_store_scene(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
uint8_t scene_id, store_scene_callback store_scene_cb);
esp_err_t send_recall_scene(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
uint8_t scene_id);
esp_err_t send_get_scene_membership(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t group_id,
get_scene_membership_callback get_scene_membership_cb);
} // namespace command
} // namespace scenes_management
namespace thermostat {
namespace command {
using transitions =
chip::app::DataModel::List<chip::app::Clusters::Thermostat::Structs::WeeklyScheduleTransitionStruct::Type>;
using get_weekly_schedule_callback =
void (*)(void *, const chip::app::Clusters::Thermostat::Commands::GetWeeklySchedule::Type::ResponseType &);
esp_err_t send_setpoint_raise_lower(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t mode,
uint8_t amount);
esp_err_t send_set_weekly_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint8_t num_of_tras_for_seq, uint8_t day_of_week_for_seq, uint8_t mode_for_seq,
transitions &trans);
esp_err_t send_get_weekly_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t day_to_return,
uint8_t mode_to_return, get_weekly_schedule_callback get_weekly_schedule_cb);
esp_err_t send_clear_weekly_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id);
} // namespace command
} // namespace thermostat
namespace door_lock {
namespace command {
using get_week_day_schedule_callback =
void (*)(void *, const chip::app::Clusters::DoorLock::Commands::GetWeekDaySchedule::Type::ResponseType &);
using get_year_day_schedule_callback =
void (*)(void *, const chip::app::Clusters::DoorLock::Commands::GetYearDaySchedule::Type::ResponseType &);
using get_holiday_schedule_callback =
void (*)(void *, const chip::app::Clusters::DoorLock::Commands::GetHolidaySchedule::Type::ResponseType &);
using get_user_callback = void (*)(void *,
const chip::app::Clusters::DoorLock::Commands::GetUser::Type::ResponseType &);
using set_credential_callback =
void (*)(void *, const chip::app::Clusters::DoorLock::Commands::SetCredential::Type::ResponseType &);
using get_credential_status_callback =
void (*)(void *, const chip::app::Clusters::DoorLock::Commands::GetCredentialStatus::Type::ResponseType &);
using credential_struct = chip::app::Clusters::DoorLock::Structs::CredentialStruct::Type;
esp_err_t send_lock_door(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t timed_invoke_timeout_ms);
esp_err_t send_unlock_door(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t timed_invoke_timeout_ms);
esp_err_t send_unlock_with_timeout(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t timeout,
uint16_t timed_invoke_timeout_ms);
esp_err_t send_set_week_day_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t week_day_index,
uint16_t user_index, uint8_t days_mask, uint8_t start_hour, uint8_t start_minute,
uint8_t end_hour, uint8_t end_minute);
esp_err_t send_get_week_day_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t week_day_index,
uint16_t user_index, get_week_day_schedule_callback success_cb);
esp_err_t send_clear_week_day_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint8_t week_day_index, uint16_t user_index);
esp_err_t send_set_year_day_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t year_day_index,
uint16_t user_index, uint32_t local_start_time, uint32_t local_end_time);
esp_err_t send_get_year_day_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t year_day_index,
uint16_t user_index, get_year_day_schedule_callback success_cb);
esp_err_t send_clear_year_day_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint8_t year_day_index, uint16_t user_index);
esp_err_t send_set_holiday_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t holiday_index,
uint32_t local_start_time, uint32_t local_end_time, uint8_t operating_mode);
esp_err_t send_get_holiday_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t holiday_index,
get_holiday_schedule_callback success_cb);
esp_err_t send_clear_holiday_schedule(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t holiday_index);
esp_err_t send_set_user(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t operation_type,
uint16_t user_index, char *user_name, uint32_t user_unique_id, uint8_t user_status,
uint8_t user_type, uint8_t credential_rule, uint16_t timed_invoke_timeout_ms);
esp_err_t send_get_user(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t user_index,
get_user_callback success_cb);
esp_err_t send_clear_user(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t user_index,
uint16_t timed_invoke_timeout_ms);
esp_err_t send_set_credential(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t operation_type,
credential_struct credential, uint8_t *credential_data, size_t credential_len,
uint16_t user_index, uint8_t user_status, uint8_t user_type,
set_credential_callback success_cb, uint16_t timed_invoke_timeout_ms);
esp_err_t send_get_credential_status(peer_device_t *remote_device, uint16_t remote_endpoint_id,
credential_struct &credential, get_credential_status_callback success_cb);
esp_err_t send_clear_credential(peer_device_t *remote_device, uint16_t remote_endpoint_id,
credential_struct &credential, uint16_t timed_invoke_timeout_ms);
esp_err_t send_unbolt_door(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t *pin_code,
size_t pin_code_len, uint16_t timed_invoke_timeout_ms);
} // namespace command
} // namespace door_lock
namespace window_covering {
namespace command {
esp_err_t send_up_or_open(peer_device_t *remote_device, uint16_t remote_endpoint_id);
esp_err_t send_down_or_close(peer_device_t *remote_device, uint16_t remote_endpoint_id);
esp_err_t send_stop_motion(peer_device_t *remote_device, uint16_t remote_endpoint_id);
esp_err_t send_go_to_lift_value(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t lift_value);
esp_err_t send_go_to_lift_percentage(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint16_t lift_percent100ths_value);
esp_err_t send_go_to_tilt_value(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint16_t tilt_value);
esp_err_t send_go_to_tilt_percentage(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint16_t tilt_percent100ths_value);
} // namespace command
} // namespace window_covering
namespace administrator_commissioning {
namespace command {
esp_err_t send_open_commissioning_window(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint16_t commissioning_timeout, chip::MutableByteSpan &pake_passcode_verifier,
uint16_t discriminator, uint32_t iterations, chip::MutableByteSpan &salt,
uint16_t timed_interaction_timeout_ms);
esp_err_t send_open_basic_commissioning_window(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint16_t commissioning_timeout, uint16_t timed_interaction_timeout_ms);
esp_err_t send_revoke_commissioning(peer_device_t *remote_device, uint16_t remote_endpoint_id,
uint16_t timed_interaction_timeout_ms);
} // namespace command
} // namespace administrator_commissioning
} // namespace cluster
namespace interaction {
using chip::app::AttributePathParams;
using chip::app::EventPathParams;
using chip::app::ReadClient;
using chip::app::WriteClient;
esp_err_t send_read_command(client::peer_device_t *remote_device, AttributePathParams *attr_path, size_t attr_path_size,
EventPathParams *event_path, size_t event_path_size, ReadClient::Callback &callback);
esp_err_t send_subscribe_command(client::peer_device_t *remote_device, AttributePathParams *attr_path,
size_t attr_path_size, EventPathParams *event_path, size_t event_path_size,
uint16_t min_interval, uint16_t max_interval, bool keep_subscription,
bool auto_resubscribe, ReadClient::Callback &callback);
esp_err_t send_write_command(client::peer_device_t *remote_device, AttributePathParams &attr_path,
const char *attr_val_json_str, WriteClient::Callback &callback,
const chip::Optional<uint16_t> &timeout_ms);
} // namespace interaction
} // namespace esp_matter
@@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <app/server/Server.h>
#include <controller/CommissioneeDeviceProxy.h>
#include <esp_log.h>
#include <esp_matter_controller_client.h>
#include <esp_matter_controller_read_command.h>
#include <esp_matter_client.h>
#include <app/server/Server.h>
#include "DataModelLogger.h"
@@ -35,30 +37,11 @@ void read_command::on_device_connected_fcn(void *context, ExchangeManager &excha
const SessionHandle &sessionHandle)
{
read_command *cmd = (read_command *)context;
ReadPrepareParams params(sessionHandle);
if (cmd->m_attr_paths.AllocatedSize() == 0 && cmd->m_event_paths.AllocatedSize() == 0) {
ESP_LOGE(TAG, "Cannot send the read command with NULL attribute path and NULL event path");
chip::Platform::Delete(cmd);
return;
}
params.mpAttributePathParamsList = cmd->m_attr_paths.Get();
params.mAttributePathParamsListSize = cmd->m_attr_paths.AllocatedSize();
params.mpEventPathParamsList = cmd->m_event_paths.Get();
params.mEventPathParamsListSize = cmd->m_event_paths.AllocatedSize();
params.mIsFabricFiltered = 0;
params.mpDataVersionFilterList = nullptr;
params.mDataVersionFilterListSize = 0;
ReadClient *client = chip::Platform::New<ReadClient>(InteractionModelEngine::GetInstance(), &exchangeMgr,
cmd->m_buffered_read_cb, ReadClient::InteractionType::Read);
if (!client) {
ESP_LOGE(TAG, "Failed to alloc memory for read client");
chip::Platform::Delete(cmd);
}
if (CHIP_NO_ERROR != client->SendRequest(params)) {
ESP_LOGE(TAG, "Failed to send read request");
chip::Platform::Delete(client);
chip::OperationalDeviceProxy device_proxy(&exchangeMgr, sessionHandle);
esp_err_t err = interaction::send_read_command(&device_proxy, cmd->m_attr_paths.Get(),
cmd->m_attr_paths.AllocatedSize(), cmd->m_event_paths.Get(),
cmd->m_event_paths.AllocatedSize(), cmd->m_buffered_read_cb);
if (err != ESP_OK) {
chip::Platform::Delete(cmd);
}
return;
@@ -168,7 +151,6 @@ void read_command::OnDone(ReadClient *apReadClient)
if (read_done_cb) {
read_done_cb(m_node_id, m_attr_paths, m_event_paths);
}
chip::Platform::Delete(apReadClient);
chip::Platform::Delete(this);
}
@@ -37,40 +37,13 @@ void subscribe_command::on_device_connected_fcn(void *context, ExchangeManager &
const SessionHandle &sessionHandle)
{
subscribe_command *cmd = (subscribe_command *)context;
ReadPrepareParams params(sessionHandle);
CHIP_ERROR err = CHIP_NO_ERROR;
if (cmd->m_attr_paths.AllocatedSize() == 0 && cmd->m_event_paths.AllocatedSize() == 0) {
ESP_LOGE(TAG, "Cannot send Subscribe command with NULL attribute path and NULL event path");
chip::Platform::Delete(cmd);
return;
}
params.mpAttributePathParamsList = cmd->m_attr_paths.Get();
params.mAttributePathParamsListSize = cmd->m_attr_paths.AllocatedSize();
params.mpEventPathParamsList = cmd->m_event_paths.Get();
params.mEventPathParamsListSize = cmd->m_event_paths.AllocatedSize();
params.mIsFabricFiltered = 0;
params.mpDataVersionFilterList = nullptr;
params.mDataVersionFilterListSize = 0;
params.mMinIntervalFloorSeconds = cmd->m_min_interval;
params.mMaxIntervalCeilingSeconds = cmd->m_max_interval;
params.mKeepSubscriptions = true;
ReadClient *client =
chip::Platform::New<ReadClient>(InteractionModelEngine::GetInstance(), &exchangeMgr, cmd->m_buffered_read_cb,
ReadClient::InteractionType::Subscribe);
if (!client) {
ESP_LOGE(TAG, "Failed to alloc memory for read client");
chip::Platform::Delete(cmd);
}
if (cmd->m_auto_resubscribe) {
err = client->SendAutoResubscribeRequest(std::move(params));
} else {
err = client->SendRequest(params);
}
if (err != CHIP_NO_ERROR) {
ESP_LOGE(TAG, "Failed to send read request");
chip::Platform::Delete(client);
chip::OperationalDeviceProxy device_proxy(&exchangeMgr, sessionHandle);
esp_err_t err = interaction::send_subscribe_command(&device_proxy, cmd->m_attr_paths.Get(),
cmd->m_attr_paths.AllocatedSize(), cmd->m_event_paths.Get(),
cmd->m_event_paths.AllocatedSize(), cmd->m_min_interval,
cmd->m_max_interval, true, cmd->m_auto_resubscribe,
cmd->m_buffered_read_cb);
if (err != ESP_OK) {
chip::Platform::Delete(cmd);
}
return;
@@ -200,7 +173,6 @@ CHIP_ERROR subscribe_command::OnResubscriptionNeeded(ReadClient *apReadClient, C
void subscribe_command::OnDone(ReadClient *apReadClient)
{
ESP_LOGI(TAG, "Subscription 0x%" PRIx32 " Done for remote node 0x%" PRIx64, m_subscription_id, m_node_id);
chip::Platform::Delete(apReadClient);
if (subscribe_done_cb) {
// This will be called when the subscription is terminated.
subscribe_done_cb(m_node_id, m_subscription_id);
@@ -13,12 +13,14 @@
// limitations under the License.
#include <esp_check.h>
#include <esp_matter_client.h>
#include <esp_matter_controller_client.h>
#include <esp_matter_controller_utils.h>
#include <esp_matter_controller_write_command.h>
#include <json_to_tlv.h>
#include <app/server/Server.h>
#include <app/OperationalSessionSetup.h>
using namespace chip::app::Clusters;
using chip::ByteSpan;
@@ -38,72 +40,16 @@ static constexpr size_t k_encoded_buf_size = chip::kMaxAppMessageLen;
namespace esp_matter {
namespace controller {
esp_err_t write_command::encode_attribute_value(uint8_t *encoded_buf, size_t encoded_buf_size,
const char *attr_val_json_str, TLVReader &out_reader)
{
TLVWriter writer;
uint32_t encoded_len = 0;
TLVReader reader;
writer.Init(encoded_buf, encoded_buf_size);
ESP_RETURN_ON_ERROR(json_to_tlv(attr_val_json_str, writer, chip::TLV::AnonymousTag()), TAG,
"Failed to parse attribute value");
ESP_RETURN_ON_FALSE(writer.Finalize() == CHIP_NO_ERROR, ESP_FAIL, TAG, "Failed to finalize tlv writer");
encoded_len = writer.GetLengthWritten();
reader.Init(encoded_buf, encoded_len);
ESP_RETURN_ON_FALSE(reader.Next() == CHIP_NO_ERROR, ESP_FAIL, TAG, "Failed to read next");
ESP_RETURN_ON_FALSE(reader.GetType() == TLVType::kTLVType_Structure, ESP_ERR_INVALID_ARG, TAG,
"The TLV type must be structure");
ESP_RETURN_ON_FALSE(reader.OpenContainer(out_reader) == CHIP_NO_ERROR, ESP_FAIL, TAG, "Failed to open container");
ESP_RETURN_ON_FALSE(out_reader.Next() == CHIP_NO_ERROR, ESP_FAIL, TAG, "Failed to read next");
return ESP_OK;
}
void write_command::on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr,
const SessionHandle &sessionHandle)
{
write_command *cmd = (write_command *)context;
if (cmd->m_attr_path.HasWildcardEndpointId()) {
ESP_LOGE(TAG, "Endpoint Id Invalid");
chip::OperationalDeviceProxy device_proxy(&exchangeMgr, sessionHandle);
esp_err_t err = interaction::send_write_command(&device_proxy, cmd->m_attr_path, cmd->m_attr_val_str,
cmd->m_chunked_callback, chip::NullOptional);
if (err != ESP_OK) {
chip::Platform::Delete(cmd);
return;
}
ConcreteDataAttributePath path(cmd->m_attr_path.mEndpointId, cmd->m_attr_path.mClusterId,
cmd->m_attr_path.mAttributeId);
chip::Platform::ScopedMemoryBuffer<uint8_t> encoded_buf;
auto write_client = MakeUnique<WriteClient>(&exchangeMgr, &(cmd->m_chunked_callback), chip::NullOptional, false);
if (write_client == nullptr) {
ESP_LOGE(TAG, "Failed to alloc memory for WriteClient");
chip::Platform::Delete(cmd);
return;
}
encoded_buf.Alloc(k_encoded_buf_size);
if (!encoded_buf.Get()) {
ESP_LOGE(TAG, "Failed to alloc memory for encoded_buf");
chip::Platform::Delete(cmd);
return;
}
TLVReader attr_val_reader;
if (encode_attribute_value(encoded_buf.Get(), k_encoded_buf_size, cmd->m_attr_val_str, attr_val_reader) != ESP_OK) {
ESP_LOGE(TAG, "Failed to encode attribute value to a TLV reader");
chip::Platform::Delete(cmd);
return;
}
if (write_client->PutPreencodedAttribute(path, attr_val_reader) != CHIP_NO_ERROR) {
ESP_LOGE(TAG, "Failed to put pre-encoded attribute value to WriteClient");
chip::Platform::Delete(cmd);
return;
}
if (write_client->SendWriteRequest(sessionHandle) != CHIP_NO_ERROR) {
ESP_LOGE(TAG, "Failed to Send Write Request");
chip::Platform::Delete(cmd);
return;
}
// Release the write_client as it will be managed by the callbacks
write_client.release();
}
void write_command::on_device_connection_failure_fcn(void *context, const ScopedNodeId &peerId, CHIP_ERROR error)
@@ -79,7 +79,6 @@ public:
void OnDone(WriteClient *client) override
{
ChipLogProgress(chipTool, "Write Done");
chip::Platform::Delete(client);
chip::Platform::Delete(this);
}
@@ -93,9 +92,6 @@ private:
const SessionHandle &sessionHandle);
static void on_device_connection_failure_fcn(void *context, const ScopedNodeId &peerId, CHIP_ERROR error);
static esp_err_t encode_attribute_value(uint8_t *encoded_buf, size_t encoded_buf_size,
const char *attr_val_json_str, TLVReader &out_reader);
chip::Callback::Callback<chip::OnDeviceConnected> on_device_connected_cb;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> on_device_connection_failure_cb;
};