From e3279f5fb9dfae6ba90d7d5fd621f826625f648c Mon Sep 17 00:00:00 2001 From: mahesh Date: Tue, 9 Jul 2024 18:02:29 +0530 Subject: [PATCH] components/esp-matter: Replace conditionals and null checks with CHIP macros --- components/esp_matter/esp_matter_client.cpp | 216 +++++------------- components/esp_matter/esp_matter_command.cpp | 9 +- .../esp_matter_delegate_callbacks.cpp | 105 ++------- components/esp_matter/esp_matter_identify.cpp | 5 +- 4 files changed, 84 insertions(+), 251 deletions(-) diff --git a/components/esp_matter/esp_matter_client.cpp b/components/esp_matter/esp_matter_client.cpp index befd6802d..ea4d94075 100644 --- a/components/esp_matter/esp_matter_client.cpp +++ b/components/esp_matter/esp_matter_client.cpp @@ -65,10 +65,7 @@ void esp_matter_connection_success_callback(void *context, ExchangeManager &exch const SessionHandle &sessionHandle) { request_handle_t *req_handle = static_cast(context); - if (!req_handle) { - ESP_LOGE(TAG, "Failed to call connect_success_callback since the request handle is NULL"); - return; - } + VerifyOrReturn(req_handle, ESP_LOGE(TAG, "Failed to call connect_success_callback since the request handle is NULL")); ESP_LOGI(TAG, "New connection success"); // Only unicast binding needs to establish the connection if (client_request_callback) { @@ -90,17 +87,12 @@ void esp_matter_connection_failure_callback(void *context, const ScopedNodeId &p esp_err_t connect(case_session_mgr_t *case_session_mgr, uint8_t fabric_index, uint64_t node_id, request_handle_t *req_handle) { - if (!case_session_mgr) { - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(case_session_mgr, ESP_ERR_INVALID_ARG); static Callback success_callback(esp_matter_connection_success_callback, NULL); static Callback failure_callback(esp_matter_connection_failure_callback, NULL); request_handle_t *context = chip::Platform::New(req_handle); - if (!context) { - ESP_LOGE(TAG, "failed to alloc memory for the command handle"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(context, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "failed to alloc memory for the command handle")); success_callback.mContext = static_cast(context); failure_callback.mContext = static_cast(context); case_session_mgr->FindOrEstablishSession(ScopedNodeId(node_id, fabric_index), &success_callback, &failure_callback); @@ -109,11 +101,7 @@ esp_err_t connect(case_session_mgr_t *case_session_mgr, uint8_t fabric_index, ui esp_err_t group_request_send(uint8_t fabric_index, request_handle_t *req_handle) { - if (!req_handle) { - ESP_LOGE(TAG, "command handle is null"); - return ESP_ERR_NO_MEM; - } - + VerifyOrReturnError(req_handle, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "command handle is null")); if (client_group_request_callback) { client_group_request_callback(fabric_index, req_handle, request_callback_priv_data); } @@ -125,10 +113,7 @@ static void esp_matter_command_client_binding_callback(const EmberBindingTableEn OperationalDeviceProxy *peer_device, void *context) { request_handle_t *req_handle = static_cast(context); - if (!req_handle) { - ESP_LOGE(TAG, "Failed to call the binding callback since command handle is NULL"); - return; - } + VerifyOrReturn(req_handle, ESP_LOGE(TAG, "Failed to call the binding callback since command handle is NULL")); if (binding.type == MATTER_UNICAST_BINDING && peer_device) { if (client_request_callback) { if (req_handle->type == INVOKE_CMD) { @@ -167,10 +152,7 @@ static void esp_matter_binding_context_release(void *context) esp_err_t cluster_update(uint16_t local_endpoint_id, request_handle_t *req_handle) { request_handle_t *context = chip::Platform::New(req_handle); - if (!context) { - ESP_LOGE(TAG, "failed to alloc memory for the request handle"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(context, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "failed to alloc memory for the request handle")); chip::ClusterId notified_cluster_id = chip::kInvalidClusterId; if (req_handle->type == INVOKE_CMD) { notified_cluster_id = req_handle->command_path.mClusterId; @@ -179,9 +161,7 @@ esp_err_t cluster_update(uint16_t local_endpoint_id, request_handle_t *req_handl } else if (req_handle->type == READ_EVENT || req_handle->type == SUBSCRIBE_EVENT) { notified_cluster_id = req_handle->event_path.mClusterId; } - if (notified_cluster_id == chip::kInvalidClusterId) { - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(notified_cluster_id != chip::kInvalidClusterId, ESP_ERR_INVALID_ARG); if (CHIP_NO_ERROR != chip::BindingManager::GetInstance().NotifyBoundClusterChanged(local_endpoint_id, notified_cluster_id, static_cast(context))) { @@ -250,19 +230,13 @@ esp_err_t send_request(void *ctx, peer_device_t *remote_device, const CommandPat custom_command_callback::on_error_callback_t on_error, const Optional &timed_invoke_timeout_ms, const Optional &response_timeout) { - if (!remote_device->GetSecureSession().HasValue() || remote_device->GetSecureSession().Value()->IsGroupSession()) { - ESP_LOGE(TAG, "Invalid Session Type"); - return ESP_ERR_INVALID_ARG; - } - if (command_path.mFlags.Has(chip::app::CommandPathFlags::kGroupIdValid)) { - ESP_LOGE(TAG, "Invalid CommandPathFlags"); - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(remote_device->GetSecureSession().HasValue() && !remote_device->GetSecureSession().Value()->IsGroupSession(), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid Session Type")); + VerifyOrReturnError(!command_path.mFlags.Has(chip::app::CommandPathFlags::kGroupIdValid), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid CommandPathFlags")); auto decoder = chip::Platform::MakeUnique(ctx, on_success, on_error); - if (decoder == nullptr) { - ESP_LOGE(TAG, "No memory for command callback"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(decoder != nullptr, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "No memory for command callback")); + auto on_done = [raw_decoder_ptr = decoder.get()](void *context, CommandSender *command_sender) { chip::Platform::Delete(command_sender); chip::Platform::Delete(raw_decoder_ptr); @@ -271,17 +245,10 @@ esp_err_t send_request(void *ctx, peer_device_t *remote_device, const CommandPat auto command_sender = chip::Platform::MakeUnique(decoder.get(), remote_device->GetExchangeManager(), timed_invoke_timeout_ms.HasValue()); - if (command_sender == nullptr) { - ESP_LOGE(TAG, "No memory for command sender"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(command_sender != nullptr, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "No memory for command sender")); chip::app::CommandSender::AddRequestDataParameters add_request_data_params(timed_invoke_timeout_ms); command_sender->AddRequestData(command_path, encodable, add_request_data_params); - if (command_sender->SendCommandRequest(remote_device->GetSecureSession().Value(), response_timeout) != - CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to send command request"); - return ESP_FAIL; - } + VerifyOrReturnError(command_sender->SendCommandRequest(remote_device->GetSecureSession().Value(), response_timeout) == CHIP_NO_ERROR, ESP_FAIL, ESP_LOGE(TAG, "Failed to send command request")); (void)decoder.release(); (void)command_sender.release(); return ESP_OK; @@ -290,24 +257,15 @@ esp_err_t send_request(void *ctx, peer_device_t *remote_device, const CommandPat esp_err_t send_group_request(const uint8_t fabric_index, const CommandPathParams &command_path, const EncodableToTLV &encodeable) { - if (!command_path.mFlags.Has(chip::app::CommandPathFlags::kGroupIdValid)) { - ESP_LOGE(TAG, "Invalid CommandPathFlags"); - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(command_path.mFlags.Has(chip::app::CommandPathFlags::kGroupIdValid), ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid CommandPathFlags")); chip::Transport::OutgoingGroupSession session(command_path.mGroupId, fabric_index); chip::Messaging::ExchangeManager *exchange_mgr = chip::app::InteractionModelEngine::GetInstance()->GetExchangeManager(); auto command_sender = chip::Platform::MakeUnique(nullptr, exchange_mgr); - if (command_sender == nullptr) { - ESP_LOGE(TAG, "No memory for command sender"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(command_sender != nullptr, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "No memory for command sender")); chip::app::CommandSender::AddRequestDataParameters add_request_data_params; command_sender->AddRequestData(command_path, encodeable, add_request_data_params); - if (command_sender->SendGroupCommandRequest(SessionHandle(session)) != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to send command request"); - return ESP_FAIL; - } + VerifyOrReturnError(command_sender->SendGroupCommandRequest(SessionHandle(session)) == CHIP_NO_ERROR, ESP_FAIL, ESP_LOGE(TAG, "Failed to send command request")); return ESP_OK; } @@ -397,14 +355,9 @@ namespace read { esp_err_t send_request(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) { - if (!remote_device->GetSecureSession().HasValue() || remote_device->GetSecureSession().Value()->IsGroupSession()) { - ESP_LOGE(TAG, "Invalid Session Type"); - return ESP_ERR_INVALID_ARG; - } - if ((!attr_path || attr_path_size == 0) && (!event_path || event_path_size == 0)) { - ESP_LOGE(TAG, "Invalid attribute path and event path"); - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(remote_device->GetSecureSession().HasValue() && !remote_device->GetSecureSession().Value()->IsGroupSession(), ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid Session Type")); + VerifyOrReturnError((attr_path && attr_path_size != 0) || (event_path && event_path_size != 0), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid attribute path and event path")); ReadPrepareParams params(remote_device->GetSecureSession().Value()); params.mpAttributePathParamsList = attr_path; params.mAttributePathParamsListSize = attr_path_size; @@ -415,22 +368,15 @@ esp_err_t send_request(client::peer_device_t *remote_device, AttributePathParams params.mIsFabricFiltered = false; auto client_deleter_callback = chip::Platform::MakeUnique(callback); - if (!client_deleter_callback) { - ESP_LOGE(TAG, "Failed to allocate memory for client deleter callback"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(client_deleter_callback, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to allocate memory for client deleter callback")); auto client = chip::Platform::MakeUnique(chip::app::InteractionModelEngine::GetInstance(), remote_device->GetExchangeManager(), *client_deleter_callback, ReadClient::InteractionType::Read); - if (!client) { - ESP_LOGE(TAG, "Failed to allocate memory for ReadClient"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(client, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to allocate memory for ReadClient")); + + VerifyOrReturnError(client->SendRequest(params) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to send read request")); - if (client->SendRequest(params) != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to send read request"); - return ESP_FAIL; - } // The memory will be released when OnDone() is called client.release(); client_deleter_callback.release(); @@ -446,14 +392,10 @@ esp_err_t send_request(client::peer_device_t *remote_device, AttributePathParams uint16_t max_interval, bool keep_subscription, bool auto_resubscribe, ReadClient::Callback &callback) { - if (!remote_device->GetSecureSession().HasValue() || remote_device->GetSecureSession().Value()->IsGroupSession()) { - ESP_LOGE(TAG, "Invalid Session Type"); - return ESP_ERR_INVALID_ARG; - } - if ((!attr_path || attr_path_size == 0) && (!event_path || event_path_size == 0)) { - ESP_LOGE(TAG, "Invalid attribute path and event path"); - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(remote_device->GetSecureSession().HasValue() && !remote_device->GetSecureSession().Value()->IsGroupSession(), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid Session Type")); + VerifyOrReturnError((attr_path && attr_path_size != 0) || (event_path && event_path_size != 0), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid attribute path and event path")); ReadPrepareParams params(remote_device->GetSecureSession().Value()); params.mpAttributePathParamsList = attr_path; params.mAttributePathParamsListSize = attr_path_size; @@ -467,17 +409,11 @@ esp_err_t send_request(client::peer_device_t *remote_device, AttributePathParams params.mKeepSubscriptions = keep_subscription; auto client_deleter_callback = chip::Platform::MakeUnique(callback); - if (!client_deleter_callback) { - ESP_LOGE(TAG, "Failed to allocate memory for client deleter callback"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(client_deleter_callback, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to allocate memory for client deleter callback")); auto client = chip::Platform::MakeUnique(chip::app::InteractionModelEngine::GetInstance(), remote_device->GetExchangeManager(), *client_deleter_callback, ReadClient::InteractionType::Subscribe); - if (!client) { - ESP_LOGE(TAG, "Failed to allocate memory for ReadClient"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(client, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to allocate memory for ReadClient")); CHIP_ERROR err = CHIP_NO_ERROR; if (auto_resubscribe) { @@ -485,10 +421,8 @@ esp_err_t send_request(client::peer_device_t *remote_device, AttributePathParams } else { err = client->SendRequest(params); } - if (err != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to send subcribe request"); - return ESP_FAIL; - } + VerifyOrReturnError(err == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to send subscribe request")); // The memory will be released when OnDone() is called client.release(); client_deleter_callback.release(); @@ -536,32 +470,20 @@ static esp_err_t encode_attribute_value(uint8_t *encoded_buf, size_t encoded_buf TLVReader reader; writer.Init(encoded_buf, encoded_buf_size); - if (encodable.EncodeTo(writer, chip::TLV::AnonymousTag()) != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to encode attribute value"); - return ESP_FAIL; - } - if (writer.Finalize() != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to finalize tlv writer"); - return ESP_FAIL; - } + VerifyOrReturnError(encodable.EncodeTo(writer, chip::TLV::AnonymousTag()) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to encode attribute value")); + VerifyOrReturnError(writer.Finalize() == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to finalize TLV writer")); encoded_len = writer.GetLengthWritten(); reader.Init(encoded_buf, encoded_len); - if (reader.Next() != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to read next"); - return ESP_FAIL; - } - if (reader.GetType() != chip::TLV::TLVType::kTLVType_Structure) { - ESP_LOGE(TAG, "The TLV type must be structure"); - return ESP_ERR_INVALID_ARG; - } - if (reader.OpenContainer(out_reader) != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to open container"); - return ESP_FAIL; - } - if (out_reader.Next() != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to read next"); - return ESP_FAIL; - } + VerifyOrReturnError(reader.Next() == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to read next")); + VerifyOrReturnError(reader.GetType() == chip::TLV::TLVType::kTLVType_Structure, + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "The TLV type must be structure")); + VerifyOrReturnError(reader.OpenContainer(out_reader) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to open container")); + VerifyOrReturnError(out_reader.Next() == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to read next")); return ESP_OK; } @@ -570,48 +492,30 @@ esp_err_t send_request(client::peer_device_t *remote_device, AttributePathParams const chip::Optional &timeout_ms) { esp_err_t err = ESP_OK; - if (!remote_device->GetSecureSession().HasValue() || remote_device->GetSecureSession().Value()->IsGroupSession()) { - ESP_LOGE(TAG, "Invalid Session Type"); - return ESP_ERR_INVALID_ARG; - } - if (attr_path.HasWildcardEndpointId()) { - ESP_LOGE(TAG, "Endpoint Id Invalid"); - return ESP_ERR_INVALID_ARG; - } + VerifyOrReturnError(remote_device->GetSecureSession().HasValue() && !remote_device->GetSecureSession().Value()->IsGroupSession(), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Invalid Session Type")); + VerifyOrReturnError(!attr_path.HasWildcardEndpointId(), + ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Endpoint Id Invalid")); ConcreteDataAttributePath path(attr_path.mEndpointId, attr_path.mClusterId, attr_path.mAttributeId); auto client_deleter_callback = chip::Platform::MakeUnique(callback); - if (!client_deleter_callback) { - ESP_LOGE(TAG, "Failed to allocate memory for client deleter callback"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(client_deleter_callback, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to allocate memory for client deleter callback")); auto write_client = chip::Platform::MakeUnique(remote_device->GetExchangeManager(), client_deleter_callback.get(), timeout_ms, false); - if (!write_client) { - ESP_LOGE(TAG, "Failed to allocate memory for WriteClient"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError(write_client, ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to allocate memory for WriteClient")); chip::Platform::ScopedMemoryBuffer encoded_buf; encoded_buf.Alloc(k_encoded_buf_size); - if (!encoded_buf.Get()) { - ESP_LOGE(TAG, "Failed to alloc memory for encoded_buf"); - return ESP_ERR_NO_MEM; - } + VerifyOrReturnError((encoded_buf.Get()), ESP_ERR_NO_MEM, ESP_LOGE(TAG, "Failed to alloc memory for encoded_buf")); TLVReader attr_val_reader; err = encode_attribute_value(encoded_buf.Get(), k_encoded_buf_size, encodable, attr_val_reader); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to encode attribute value to a TLV reader"); - return err; - } - if (write_client->PutPreencodedAttribute(path, attr_val_reader) != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to put pre-encoded attribute value to WriteClient"); - return ESP_FAIL; - } - if (write_client->SendWriteRequest(remote_device->GetSecureSession().Value()) != CHIP_NO_ERROR) { - ESP_LOGE(TAG, "Failed to Send Write Request"); - return ESP_FAIL; - } + VerifyOrReturnError(err == ESP_OK, + err, ESP_LOGE(TAG, "Failed to encode attribute value to a TLV reader")); + VerifyOrReturnError(write_client->PutPreencodedAttribute(path, attr_val_reader) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to put pre-encoded attribute value to WriteClient")); + VerifyOrReturnError(write_client->SendWriteRequest(remote_device->GetSecureSession().Value()) == CHIP_NO_ERROR, + ESP_FAIL, ESP_LOGE(TAG, "Failed to Send Write Request")); + // Release the write_client and client deleter callback as it will be managed by the client deleter callback write_client.release(); client_deleter_callback.release(); diff --git a/components/esp_matter/esp_matter_command.cpp b/components/esp_matter/esp_matter_command.cpp index 02cfa2087..82ecbae67 100644 --- a/components/esp_matter/esp_matter_command.cpp +++ b/components/esp_matter/esp_matter_command.cpp @@ -43,15 +43,10 @@ void DispatchSingleClusterCommandCommon(const ConcreteCommandPath &command_path, ESP_LOGI(TAG, "Received command 0x%08" PRIX32 " for endpoint 0x%04" PRIX16 "'s cluster 0x%08" PRIX32 "", command_id, endpoint_id, cluster_id); cluster_t *cluster = cluster::get(endpoint_id, cluster_id); - if (!cluster) { - return; - } + VerifyOrReturn(cluster); command_t *command = get(cluster, command_id, COMMAND_FLAG_ACCEPTED); callback_t standard_callback = get_cluster_accepted_command(cluster_id, command_id); - if (!command && !standard_callback) { - ESP_LOGE(TAG, "Command 0x%08" PRIX32 " not found", command_id); - return; - } + VerifyOrReturn((command || standard_callback), ESP_LOGE(TAG, "Command 0x%08" PRIX32 " not found", command_id)); esp_err_t err = ESP_OK; TLVReader tlv_reader; tlv_reader.Init(tlv_data); diff --git a/components/esp_matter/esp_matter_delegate_callbacks.cpp b/components/esp_matter/esp_matter_delegate_callbacks.cpp index 396c11759..ee09d9840 100644 --- a/components/esp_matter/esp_matter_delegate_callbacks.cpp +++ b/components/esp_matter/esp_matter_delegate_callbacks.cpp @@ -61,10 +61,7 @@ namespace delegate_cb { void InitModeDelegate(void *delegate, uint16_t endpoint_id, uint32_t cluster_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static ModeBase::Instance * modeInstance = nullptr; ModeBase::Delegate *mode_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, cluster_id); @@ -119,10 +116,7 @@ void DeviceEnergyManagementModeDelegateInitCB(void *delegate, uint16_t endpoint_ void EnergyEvseDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static EnergyEvse::Instance * energyEvseInstance = nullptr; EnergyEvse::Delegate *energy_evse_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, EnergyEvse::Id); @@ -140,10 +134,7 @@ void MicrowaveOvenControlDelegateInitCB(void *delegate, uint16_t endpoint_id) ModeBase::Delegate *microwave_oven_mode_delegate = static_cast(get_delegate_impl(cluster)); cluster = cluster::get(endpoint, OperationalState::Id); OperationalState::Delegate *operational_state_delegate = static_cast(get_delegate_impl(cluster)); - if(delegate == nullptr || microwave_oven_mode_delegate == nullptr || operational_state_delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr && microwave_oven_mode_delegate != nullptr && operational_state_delegate != nullptr); // Create instances of clusters. static ModeBase::Instance * microwaveOvenModeInstance = nullptr; static OperationalState::Instance * operationalStateInstance = nullptr; @@ -162,10 +153,7 @@ void MicrowaveOvenControlDelegateInitCB(void *delegate, uint16_t endpoint_id) void OperationalStateDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static OperationalState::Instance * operationalStateInstance = nullptr; OperationalState::Delegate *operational_state_delegate = static_cast(delegate); operationalStateInstance = new OperationalState::Instance(operational_state_delegate, endpoint_id); @@ -174,20 +162,14 @@ void OperationalStateDelegateInitCB(void *delegate, uint16_t endpoint_id) void FanControlDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); FanControl::Delegate *fan_control_delegate = static_cast(delegate); FanControl::SetDefaultDelegate(endpoint_id, fan_control_delegate); } void HepaFilterMonitoringDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static ResourceMonitoring::Instance * hepaFilterMonitoringInstance = nullptr; ResourceMonitoring::Delegate *resource_monitoring_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, HepaFilterMonitoring::Id); @@ -198,10 +180,7 @@ void HepaFilterMonitoringDelegateInitCB(void *delegate, uint16_t endpoint_id) void ActivatedCarbonFilterMonitoringDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static ResourceMonitoring::Instance * activatedCarbonFilterMonitoringInstance = nullptr; ResourceMonitoring::Delegate *resource_monitoring_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, ActivatedCarbonFilterMonitoring::Id); @@ -212,30 +191,21 @@ void ActivatedCarbonFilterMonitoringDelegateInitCB(void *delegate, uint16_t endp void LaundryDryerControlsDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); LaundryDryerControls::Delegate *laundry_dryer_controls_delegate = static_cast(delegate); LaundryDryerControls::LaundryDryerControlsServer::SetDefaultDelegate(endpoint_id, laundry_dryer_controls_delegate); } void ValveConfigurationAndControlDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); ValveConfigurationAndControl::Delegate *valve_configuration_and_control_delegate = static_cast(delegate); ValveConfigurationAndControl::SetDefaultDelegate(endpoint_id, valve_configuration_and_control_delegate); } void DeviceEnergyManagementDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static DeviceEnergyManagement::Instance * deviceEnergyManagementInstance = nullptr; DeviceEnergyManagement::Delegate *device_energy_management_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, DeviceEnergyManagement::Id); @@ -245,50 +215,35 @@ void DeviceEnergyManagementDelegateInitCB(void *delegate, uint16_t endpoint_id) void DoorLockDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); DoorLock::Delegate *door_lock_delegate = static_cast(delegate); DoorLockServer::Instance().SetDelegate(endpoint_id, door_lock_delegate); } void BooleanStateConfigurationDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); BooleanStateConfiguration::Delegate *boolean_state_configuration_delegate = static_cast(delegate); BooleanStateConfiguration::SetDefaultDelegate(endpoint_id, boolean_state_configuration_delegate); } void TimeSynchronizationDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); TimeSynchronization::Delegate *time_synchronization_delegate = static_cast(delegate); TimeSynchronization::SetDefaultDelegate(time_synchronization_delegate); } void ApplicationBasicDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); ApplicationBasic::Delegate *application_basic_delegate = static_cast(delegate); ApplicationBasic::SetDefaultDelegate(endpoint_id, application_basic_delegate); } void PowerTopologyDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static PowerTopology::Instance * powerTopologyInstance = nullptr; PowerTopology::Delegate *power_topology_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, PowerTopology::Id); @@ -299,10 +254,7 @@ void PowerTopologyDelegateInitCB(void *delegate, uint16_t endpoint_id) void ElectricalPowerMeasurementDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); static ElectricalPowerMeasurement::Instance * electricalPowerMeasurementInstance = nullptr; ElectricalPowerMeasurement::Delegate *electrical_power_measurement_delegate = static_cast(delegate); uint32_t feature_map = get_feature_map_value(endpoint_id, ElectricalPowerMeasurement::Id); @@ -314,50 +266,35 @@ void ElectricalPowerMeasurementDelegateInitCB(void *delegate, uint16_t endpoint_ void LaundryWasherControlsDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); LaundryWasherControls::Delegate *laundry_washer_controls_delegate = static_cast(delegate); LaundryWasherControls::LaundryWasherControlsServer::SetDefaultDelegate(endpoint_id, laundry_washer_controls_delegate); } void WindowCoveringDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); WindowCovering::Delegate *window_covering_delegate = static_cast(delegate); WindowCovering::SetDefaultDelegate(endpoint_id, window_covering_delegate); } void DishwasherAlarmDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); DishwasherAlarm::Delegate *dishwasher_alarm_delegate = static_cast(delegate); DishwasherAlarm::SetDefaultDelegate(endpoint_id, dishwasher_alarm_delegate); } void KeypadInputDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); KeypadInput::Delegate *keypad_input_delegate = static_cast(delegate); KeypadInput::SetDefaultDelegate(endpoint_id, keypad_input_delegate); } void ModeSelectDelegateInitCB(void *delegate, uint16_t endpoint_id) { - if(delegate == nullptr) - { - return; - } + VerifyOrReturn(delegate != nullptr); ModeSelect::SupportedModesManager *supported_modes_manager = static_cast(delegate); ModeSelect::setSupportedModesManager(supported_modes_manager); } diff --git a/components/esp_matter/esp_matter_identify.cpp b/components/esp_matter/esp_matter_identify.cpp index b1bf7fbd6..69fee90a0 100644 --- a/components/esp_matter/esp_matter_identify.cpp +++ b/components/esp_matter/esp_matter_identify.cpp @@ -63,10 +63,7 @@ esp_err_t init(uint16_t endpoint_id, uint8_t identify_type, uint8_t effect_ident Identify *identify = chip::Platform::New(endpoint_id, start_cb, stop_cb, (chip::app::Clusters::Identify::IdentifyTypeEnum)identify_type, effect_cb, static_cast(effect_identifier), static_cast(effect_variant)); - if (!identify) { - ESP_LOGE(TAG, "Fail to create identify object"); - return ESP_FAIL; - } + VerifyOrReturnError(identify, ESP_FAIL, ESP_LOGE(TAG, "Fail to create identify object")); endpoint::set_identify(endpoint_id, (void *)identify); return ESP_OK; }