From 7912ecb1c083bdaebadd43fe1530e9dded10083a Mon Sep 17 00:00:00 2001 From: "Andrii Kalinich (GitHub)" Date: Wed, 25 Aug 2021 16:52:38 -0400 Subject: Add method for delete empty message (#3751) * Add method for delete empty message * Add WARNINGS code when some parameters were cut off Co-authored-by: Jacob Keeler --- .../include/application_manager/message_helper.h | 8 ++++ .../commands/mobile/get_vehicle_data_request.cc | 7 +++ .../mobile/on_vehicle_data_notification.cc | 2 + .../src/message_helper/message_helper.cc | 53 ++++++++++++++++++++++ .../application_manager/mock_message_helper.h | 2 + .../test/mock_message_helper.cc | 7 +++ 6 files changed, 79 insertions(+) diff --git a/src/components/application_manager/include/application_manager/message_helper.h b/src/components/application_manager/include/application_manager/message_helper.h index 88200d6593..9cfb504b00 100644 --- a/src/components/application_manager/include/application_manager/message_helper.h +++ b/src/components/application_manager/include/application_manager/message_helper.h @@ -1095,6 +1095,14 @@ class MessageHelper { ApplicationManager& app_mngr, const WindowID window_id); + /** + * @brief Recursively removes empty parameters of composite type from message + * @param msg_params smart object containing message params + * @return amount of empty parameters removed + */ + static uint16_t RemoveEmptyMessageParams( + smart_objects::SmartObject& msg_params); + /** * @brief AddDefaultParamsToTireStatus adds missing default sub-params to * tirePressure param, if version of related application requires presence of diff --git a/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/get_vehicle_data_request.cc b/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/get_vehicle_data_request.cc index 18149fbe02..854d78b684 100644 --- a/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/get_vehicle_data_request.cc +++ b/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/get_vehicle_data_request.cc @@ -170,6 +170,13 @@ void GetVehicleDataRequest::on_event(const event_engine::Event& event) { } } + if (MessageHelper::RemoveEmptyMessageParams( + message[strings::msg_params]) > 0) { + mobile_result_code = mobile_apis::Result::WARNINGS; + response_info = app_mngr::commands::MergeInfos( + response_info, "Some vehicle data items could not be retrieved"); + } + if (message[strings::msg_params].empty() && hmi_apis::Common_Result::DATA_NOT_AVAILABLE != result_code) { response_info = "Failed to retrieve data from vehicle"; diff --git a/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/on_vehicle_data_notification.cc b/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/on_vehicle_data_notification.cc index 0ae38a9398..71a6289d9c 100644 --- a/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/on_vehicle_data_notification.cc +++ b/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/commands/mobile/on_vehicle_data_notification.cc @@ -70,6 +70,8 @@ void OnVehicleDataNotification::Run() { custom_vehicle_data_manager_.CreateMobileMessageParams( (*message_)[strings::msg_params]); + MessageHelper::RemoveEmptyMessageParams((*message_)[strings::msg_params]); + const auto& param_names = (*message_)[strings::msg_params].enumerate(); for (const auto& name : param_names) { SDL_LOG_DEBUG("vehicle_data name: " << name); diff --git a/src/components/application_manager/src/message_helper/message_helper.cc b/src/components/application_manager/src/message_helper/message_helper.cc index decdabb55c..606bc4c3f0 100644 --- a/src/components/application_manager/src/message_helper/message_helper.cc +++ b/src/components/application_manager/src/message_helper/message_helper.cc @@ -3475,6 +3475,59 @@ WindowID MessageHelper::ExtractWindowIdFromSmartObject( return mobile_apis::PredefinedWindows::DEFAULT_WINDOW; } +uint16_t MessageHelper::RemoveEmptyMessageParams( + smart_objects::SmartObject& msg_params) { + uint16_t erased_params = 0; + + if (msg_params.empty()) { + return erased_params; + } + + const auto keys = msg_params.enumerate(); + for (const auto& key_params : keys) { + auto& param = msg_params[key_params]; + + if (smart_objects::SmartType_Array == param.getType()) { + smart_objects::SmartArray* array = param.asArray(); + + if (array == nullptr || array->empty()) + continue; + + const bool are_all_empty = + std::all_of(array->begin(), + array->end(), + [](const smart_objects::SmartObject& item) { + if (smart_objects::SmartType_Array == item.getType() || + smart_objects::SmartType_Map == item.getType()) { + return item.empty(); + } + + return false; + }); + + if (are_all_empty) { + SDL_LOG_DEBUG("Remove empty array " + key_params + + " from msg_params in HMI response"); + msg_params.erase(key_params); + ++erased_params; + } + + continue; + } + + if (smart_objects::SmartType_Map == param.getType()) { + if (param.empty()) { + SDL_LOG_DEBUG("Remove empty field " + key_params + + " from msg_params in HMI response"); + msg_params.erase(key_params); + ++erased_params; + } + } + } + + return erased_params; +} + void MessageHelper::AddDefaultParamsToTireStatus( ApplicationSharedPtr app, smart_objects::SmartObject& response_from_hmi) { const utils::SemanticVersion max_version_with_mandatory_params(8, 0, 0); diff --git a/src/components/application_manager/test/include/application_manager/mock_message_helper.h b/src/components/application_manager/test/include/application_manager/mock_message_helper.h index 9e69b0bcd6..67d992d4af 100644 --- a/src/components/application_manager/test/include/application_manager/mock_message_helper.h +++ b/src/components/application_manager/test/include/application_manager/mock_message_helper.h @@ -394,6 +394,8 @@ class MockMessageHelper { application_manager::ApplicationSharedPtr application, application_manager::ApplicationManager& app_mngr, const application_manager::WindowID window_id)); + MOCK_METHOD1(RemoveEmptyMessageParams, + uint16_t(const smart_objects::SmartObject&)); MOCK_METHOD2(AddDefaultParamsToTireStatus, void(application_manager::ApplicationSharedPtr application, diff --git a/src/components/application_manager/test/mock_message_helper.cc b/src/components/application_manager/test/mock_message_helper.cc index 02cf70e3ba..9212b0feab 100644 --- a/src/components/application_manager/test/mock_message_helper.cc +++ b/src/components/application_manager/test/mock_message_helper.cc @@ -687,10 +687,17 @@ smart_objects::SmartObjectSPtr MessageHelper::CreateResponseMessageFromHmi( function_id, correlation_id, result_code); } +uint16_t MessageHelper::RemoveEmptyMessageParams( + smart_objects::SmartObject& msg_params) { + return MockMessageHelper::message_helper_mock()->RemoveEmptyMessageParams( + msg_params); +} + void MessageHelper::AddDefaultParamsToTireStatus( application_manager::ApplicationSharedPtr application, smart_objects::SmartObject& response_from_hmi) { return MockMessageHelper::message_helper_mock()->AddDefaultParamsToTireStatus( application, response_from_hmi); } + } // namespace application_manager -- cgit v1.2.1