diff options
author | Collin <iCollin@users.noreply.github.com> | 2021-04-26 15:16:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-26 15:16:13 -0400 |
commit | 74ea4bb79b6adf00702e68cf5d502bc3def0f285 (patch) | |
tree | c50de8e456277c67ec7fddbe48a3cec41aab2cbf | |
parent | b8bb4ae8947c74bde887b1148aa116ac19b8020b (diff) | |
download | sdl_core-74ea4bb79b6adf00702e68cf5d502bc3def0f285.tar.gz |
Open INI file less (#3688)
* parse all values from one ini file open
* wrap ini_read_value to open file for ini_file_test.cc
* update profile tests, fix handle leak
* refactor ini_parse_line
* Apply suggestions from code review
Co-authored-by: Jacob Keeler <jacob.keeler@livioradio.com>
* parse ini file in to smart object
* remove ini_file changes
* fix style
* small optimization, parse key first
* restore open/close config functions
* deprecate ini_file
* Revert "restore open/close config functions"
This reverts commit a03c69403fe14790e6b6738a65820c1ae69e95c5.
* move ini parsing to Profile::ParseConfiguration
* add DEPREACTED to ini_file
* don't print deprecated warnings from test file that is dedicated to deprecated methods
* add macros to ini_file.cc
* check style
Co-authored-by: Jacob Keeler <jacob.keeler@livioradio.com>
9 files changed, 221 insertions, 69 deletions
diff --git a/src/components/config_profile/CMakeLists.txt b/src/components/config_profile/CMakeLists.txt index 4b545c65ae..8863c098ea 100644 --- a/src/components/config_profile/CMakeLists.txt +++ b/src/components/config_profile/CMakeLists.txt @@ -33,6 +33,7 @@ include(${CMAKE_SOURCE_DIR}/tools/cmake/helpers/sources.cmake) include_directories ( ${COMPONENTS_DIR}/config_profile/include ${COMPONENTS_DIR}/utils/include/ + ${COMPONENTS_DIR}/smart_objects/include ${POLICY_GLOBAL_INCLUDE_PATH}/ ${LOG4CXX_INCLUDE_DIRECTORY} ${BOOST_INCLUDE_DIR} @@ -44,6 +45,7 @@ set(PATHS ) set(LIBRARIES + SmartObjects Utils ) diff --git a/src/components/config_profile/include/config_profile/ini_file.h b/src/components/config_profile/include/config_profile/ini_file.h index 204fb6c7bf..f489872533 100644 --- a/src/components/config_profile/include/config_profile/ini_file.h +++ b/src/components/config_profile/include/config_profile/ini_file.h @@ -34,6 +34,7 @@ #define SRC_COMPONENTS_CONFIG_PROFILE_INCLUDE_CONFIG_PROFILE_INI_FILE_H_ #include <stdint.h> +#include "utils/macro.h" namespace profile { @@ -88,40 +89,52 @@ extern "C" { * @brief Write usage instructions to the end of the file * @param * + * \deprecated This method will be removed in the next major release + * See Profile::ParseConfiguration for the new ini parsing code + * * @return NULL if file or desired entry not found, otherwise pointer to fname */ -extern char* ini_write_inst(const char* fname, uint8_t flag); +DEPRECATED extern char* ini_write_inst(const char* fname, uint8_t flag); /* * @brief Read a certain item of the specified chapter of a ini-file * + * \deprecated This method will be removed in the next major release + * See Profile::ParseConfiguration for the new ini parsing code + * * @return NULL if file or desired entry not found, otherwise pointer to value */ -extern char* ini_read_value(const char* fname, - const char* chapter, - const char* item, - char* value); +DEPRECATED extern char* ini_read_value(const char* fname, + const char* chapter, + const char* item, + char* value); /* * @brief Write a certain item of the specified chapter of a ini-file * + * \deprecated This method will be removed in the next major release + * See Profile::ParseConfiguration for the new ini parsing code + * * @return NULL if file not found, otherwise pointer to value */ -extern char ini_write_value(const char* fname, - const char* chapter, - const char* item, - const char* value, - uint8_t flag); +DEPRECATED extern char ini_write_value(const char* fname, + const char* chapter, + const char* item, + const char* value, + uint8_t flag); /* * @brief Parse the given line for the item and returns the value if * there is one otherwise NULL * + * \deprecated This method will be removed in the next major release + * See Profile::ParseConfiguration for the new ini parsing code + * * @return NULL if desired entry not found, otherwise pointer to value */ -extern Ini_search_id ini_parse_line(const char* line, - const char* tag, - char* value); +DEPRECATED extern Ini_search_id ini_parse_line(const char* line, + const char* tag, + char* value); #ifdef __cplusplus } diff --git a/src/components/config_profile/include/config_profile/profile.h b/src/components/config_profile/include/config_profile/profile.h index dffa2d1571..48ffd30ff3 100644 --- a/src/components/config_profile/include/config_profile/profile.h +++ b/src/components/config_profile/include/config_profile/profile.h @@ -44,6 +44,7 @@ #include "media_manager/media_manager_settings.h" #include "policy/policy_settings.h" #include "protocol_handler/protocol_handler_settings.h" +#include "smart_objects/smart_object.h" #include "transport_manager/transport_manager_settings.h" #include "utils/macro.h" @@ -825,6 +826,11 @@ class Profile : public protocol_handler::ProtocolHandlerSettings, uint32_t app_transport_change_timer_addition() const OVERRIDE; /** + * @brief Parses values in config_file_name_ to config_obj_ smart object + */ + void ParseConfiguration(); + + /** * @brief Updates all related values from ini file */ void UpdateValues(); @@ -989,6 +995,7 @@ class Profile : public protocol_handler::ProtocolHandlerSettings, size_t maximum_audio_payload_size_; size_t maximum_video_payload_size_; std::string config_file_name_; + smart_objects::SmartObject config_obj_; std::string server_address_; uint16_t server_port_; uint16_t video_streaming_port_; @@ -1114,6 +1121,13 @@ class Profile : public protocol_handler::ProtocolHandlerSettings, int iap2_hub_connect_attempts_; int iap_hub_connection_wait_timeout_; uint16_t tts_global_properties_timeout_; + size_t maximum_payload_size_; + size_t message_frequency_count_; + size_t message_frequency_time_; + bool malformed_message_filtering_; + size_t malformed_frequency_count_; + size_t malformed_frequency_time_; + uint32_t multiframe_waiting_timeout_; uint16_t attempts_to_open_policy_db_; uint16_t open_attempt_timeout_ms_; uint32_t resumption_delay_before_ign_; diff --git a/src/components/config_profile/src/ini_file.cc b/src/components/config_profile/src/ini_file.cc index f5817896bf..615100d9a7 100644 --- a/src/components/config_profile/src/ini_file.cc +++ b/src/components/config_profile/src/ini_file.cc @@ -55,6 +55,9 @@ namespace profile { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + SDL_CREATE_LOG_VARIABLE("Profile") char* ini_write_inst(const char* fname, uint8_t flag) { @@ -415,4 +418,7 @@ Ini_search_id ini_parse_line(const char* line, const char* tag, char* value) { return INI_NOTHING; } + +#pragma GCC diagnostic pop + } // namespace profile diff --git a/src/components/config_profile/src/profile.cc b/src/components/config_profile/src/profile.cc index 7836e0c755..c12deb44c8 100644 --- a/src/components/config_profile/src/profile.cc +++ b/src/components/config_profile/src/profile.cc @@ -41,7 +41,6 @@ #include <string> -#include "config_profile/ini_file.h" #include "utils/file_system.h" #include "utils/logger.h" #include "utils/threads/thread.h" @@ -64,6 +63,8 @@ namespace { << "'."); \ } +#define INI_LINE_LEN 1024 + const char* kDefaultConfigFileName = "smartDeviceLink.ini"; const char* kMainSection = "MAIN"; @@ -532,6 +533,13 @@ Profile::Profile() , iap2_hub_connect_attempts_(kDefaultIAP2HubConnectAttempts) , iap_hub_connection_wait_timeout_(kDefaultIAPHubConnectionWaitTimeout) , tts_global_properties_timeout_(kDefaultTTSGlobalPropertiesTimeout) + , maximum_payload_size_(kDefaultMaximumPayloadSize) + , message_frequency_count_(kDefaultFrequencyCount) + , message_frequency_time_(kDefaultFrequencyTime) + , malformed_message_filtering_(kDefaulMalformedMessageFiltering) + , malformed_frequency_count_(kDefaultMalformedFrequencyCount) + , malformed_frequency_time_(kDefaultMalformedFrequencyTime) + , multiframe_waiting_timeout_(kDefaultExpectedConsecutiveFramesTimeout) , attempts_to_open_policy_db_(kDefaultAttemptsToOpenPolicyDB) , open_attempt_timeout_ms_(kDefaultAttemptsToOpenPolicyDB) , resumption_delay_before_ign_(kDefaultResumptionDelayBeforeIgn) @@ -564,9 +572,6 @@ Profile::Profile() , ignition_off_signal_offset_(kDefaultIgnitionOffSignalOffset) , rpc_pass_through_timeout_(kDefaultRpcPassThroughTimeout) , period_for_consent_expiration_(kDefaultPeriodForConsentExpiration) { - // SDL version - ReadStringValue( - &sdl_version_, kDefaultSDLVersion, kMainSection, kSDLVersionKey); } Profile::~Profile() {} @@ -993,65 +998,30 @@ uint32_t Profile::iap_hub_connection_wait_timeout() const { } size_t Profile::maximum_payload_size() const { - size_t maximum_payload_size = 0; - ReadUIntValue(&maximum_payload_size, - kDefaultMaximumPayloadSize, - kProtocolHandlerSection, - kMaximumPayloadSizeKey); - return maximum_payload_size; + return maximum_payload_size_; } size_t Profile::message_frequency_count() const { - size_t message_frequency_count = 0; - ReadUIntValue(&message_frequency_count, - kDefaultFrequencyCount, - kProtocolHandlerSection, - kFrequencyCount); - return message_frequency_count; + return message_frequency_count_; } size_t Profile::message_frequency_time() const { - size_t message_frequency_time = 0; - ReadUIntValue(&message_frequency_time, - kDefaultFrequencyTime, - kProtocolHandlerSection, - kFrequencyTime); - return message_frequency_time; + return message_frequency_time_; } bool Profile::malformed_message_filtering() const { - bool malformed_message_filtering = 0; - ReadBoolValue(&malformed_message_filtering, - kDefaulMalformedMessageFiltering, - kProtocolHandlerSection, - kMalformedMessageFiltering); - return malformed_message_filtering; + return malformed_message_filtering_; } size_t Profile::malformed_frequency_count() const { - size_t malformed_frequency_count = 0; - ReadUIntValue(&malformed_frequency_count, - kDefaultMalformedFrequencyCount, - kProtocolHandlerSection, - kMalformedFrequencyCount); - return malformed_frequency_count; + return malformed_frequency_count_; } size_t Profile::malformed_frequency_time() const { - size_t malformed_frequency_time = 0; - ReadUIntValue(&malformed_frequency_time, - kDefaultMalformedFrequencyTime, - kProtocolHandlerSection, - kMalformedFrequencyTime); - return malformed_frequency_time; + return malformed_frequency_time_; } uint32_t Profile::multiframe_waiting_timeout() const { - uint32_t multiframe_waiting_timeout = 0; - ReadUIntValue(&multiframe_waiting_timeout, - kDefaultExpectedConsecutiveFramesTimeout, - kProtocolHandlerSection, - kExpectedConsecutiveFramesTimeout); - return multiframe_waiting_timeout; + return multiframe_waiting_timeout_; } uint16_t Profile::attempts_to_open_policy_db() const { @@ -1250,9 +1220,81 @@ const std::string Profile::hmi_origin_id() const { return hmi_origin_id_; } +void Profile::ParseConfiguration() { + FILE* config_file_ = fopen(config_file_name_.c_str(), "r"); + config_obj_ = smart_objects::SmartObject(smart_objects::SmartType_Map); + + if (nullptr != config_file_) { + char line[INI_LINE_LEN] = "\0"; + std::string chapter; + + while (nullptr != fgets(line, INI_LINE_LEN, config_file_)) { + std::string line_str(line); + auto whitespace_len = line_str.find_first_not_of(" \t\r\n"); + if (std::string::npos == whitespace_len) { + continue; + } + line_str.erase(0, whitespace_len); + + if (';' == line_str[0] || '*' == line_str[0]) { + continue; + } + + if ('[' == line_str[0] && std::string::npos != line_str.find(']')) { + line_str.erase(0, line_str.find_first_not_of("[ \t\r\n")); + line_str.erase(line_str.find_last_not_of(" \t\r\n]") + 1); + + if (false == line_str.empty()) { + chapter = line_str; + config_obj_[chapter] = + smart_objects::SmartObject(smart_objects::SmartType_Map); + } + + continue; + } + + size_t equals_idx = line_str.find('='); + if (std::string::npos == equals_idx) { + continue; + } + + std::string key(line_str.data(), equals_idx); + std::string value(line_str.data() + equals_idx + 1); + + auto key_whitespace_len = key.find_first_not_of(" \t\r\n"); + if (std::string::npos == key_whitespace_len) { + continue; + } + key.erase(0, key_whitespace_len); + key.erase(key.find_last_not_of(" \t\r\n") + 1); + + if (true == config_obj_[chapter].keyExists(key)) { + continue; + } + + auto val_whitespace_len = value.find_first_not_of(" \t\r\n"); + if (std::string::npos == val_whitespace_len) { + value = ""; + } else { + value.erase(0, value.find_first_not_of(" \t\r\n")); + value.erase(value.find_last_not_of(" \t\r\n") + 1); + } + + config_obj_[chapter][key] = value; + } + } + + if (nullptr != config_file_) { + fclose(config_file_); + config_file_ = nullptr; + } +} + void Profile::UpdateValues() { SDL_LOG_AUTO_TRACE(); + ParseConfiguration(); + // SDL version ReadStringValue( &sdl_version_, kDefaultSDLVersion, kMainSection, kSDLVersionKey); @@ -2134,6 +2176,73 @@ void Profile::UpdateValues() { error_description_ = "PathToSnapshot has forbidden(non-portable) symbols"; } + // Packet with payload bigger than this value will be marked as malformed + ReadUIntValue(&maximum_payload_size_, + kDefaultMaximumPayloadSize, + kProtocolHandlerSection, + kMaximumPayloadSizeKey); + + LOG_UPDATED_VALUE( + maximum_payload_size_, kMaximumPayloadSizeKey, kProtocolHandlerSection); + + // Check for apps sending too many messages + ReadUIntValue(&message_frequency_count_, + kDefaultFrequencyCount, + kProtocolHandlerSection, + kFrequencyCount); + + LOG_UPDATED_VALUE( + message_frequency_count_, kFrequencyCount, kProtocolHandlerSection); + + // Check for apps sending too many messages + ReadUIntValue(&message_frequency_time_, + kDefaultFrequencyTime, + kProtocolHandlerSection, + kFrequencyTime); + + LOG_UPDATED_VALUE( + message_frequency_time_, kFrequencyTime, kProtocolHandlerSection); + + // Enable filter malformed messages + ReadBoolValue(&malformed_message_filtering_, + kDefaulMalformedMessageFiltering, + kProtocolHandlerSection, + kMalformedMessageFiltering); + + LOG_UPDATED_BOOL_VALUE(malformed_message_filtering_, + kMalformedMessageFiltering, + kProtocolHandlerSection); + + // Count for malformed message detection + ReadUIntValue(&malformed_frequency_count_, + kDefaultMalformedFrequencyCount, + kProtocolHandlerSection, + kMalformedFrequencyCount); + + LOG_UPDATED_VALUE(malformed_frequency_count_, + kMalformedFrequencyCount, + kProtocolHandlerSection); + + // Time for malformed message detection + ReadUIntValue(&malformed_frequency_time_, + kDefaultMalformedFrequencyTime, + kProtocolHandlerSection, + kMalformedFrequencyTime); + + LOG_UPDATED_VALUE(malformed_frequency_time_, + kMalformedFrequencyTime, + kProtocolHandlerSection); + + // Timeout for waiting CONSECUTIVE frames of multiframe + ReadUIntValue(&multiframe_waiting_timeout_, + kDefaultExpectedConsecutiveFramesTimeout, + kProtocolHandlerSection, + kExpectedConsecutiveFramesTimeout); + + LOG_UPDATED_VALUE(multiframe_waiting_timeout_, + kExpectedConsecutiveFramesTimeout, + kProtocolHandlerSection); + // Attempts number for opening policy DB ReadUIntValue(&attempts_to_open_policy_db_, kDefaultAttemptsToOpenPolicyDB, @@ -2610,18 +2719,18 @@ bool Profile::ReadValue(bool* value, DCHECK(value); bool ret = false; - char buf[INI_LINE_LEN + 1]; - *buf = '\0'; - if ((0 != ini_read_value(config_file_name_.c_str(), pSection, pKey, buf)) && - ('\0' != *buf)) { - const int32_t tmpVal = atoi(buf); - if ((0 == strcmp("true", buf)) || (0 != tmpVal)) { + if (config_obj_.keyExists(pSection) && + config_obj_[pSection].keyExists(pKey)) { + auto val_str = config_obj_[pSection][pKey].asString(); + const int32_t tmpVal = atoi(val_str.data()); + if ((0 == strcmp("true", val_str.data())) || (0 != tmpVal)) { *value = true; } else { *value = false; } ret = true; } + return ret; } @@ -2638,10 +2747,9 @@ bool Profile::ReadValueEmpty(std::string* value, DCHECK(value); bool ret = false; - char buf[INI_LINE_LEN + 1]; - *buf = '\0'; - if (0 != ini_read_value(config_file_name_.c_str(), pSection, pKey, buf)) { - *value = buf; + if (config_obj_.keyExists(pSection) && + config_obj_[pSection].keyExists(pKey)) { + *value = config_obj_[pSection][pKey].asString(); ret = true; } diff --git a/src/components/config_profile/test/CMakeLists.txt b/src/components/config_profile/test/CMakeLists.txt index 597be8fe00..d586485f64 100644 --- a/src/components/config_profile/test/CMakeLists.txt +++ b/src/components/config_profile/test/CMakeLists.txt @@ -33,11 +33,13 @@ include(${CMAKE_SOURCE_DIR}/tools/cmake/helpers/sources.cmake) include_directories ( ${GMOCK_INCLUDE_DIRECTORY} ${COMPONENTS_DIR}/config_profile/include + ${COMPONENTS_DIR}/smart_objects/include ) set(LIBRARIES gmock ConfigProfile + SmartObjects ) collect_sources(SOURCES "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/src/components/config_profile/test/ini_file_test.cc b/src/components/config_profile/test/ini_file_test.cc index ca4f2fc849..c1a2b83120 100644 --- a/src/components/config_profile/test/ini_file_test.cc +++ b/src/components/config_profile/test/ini_file_test.cc @@ -40,6 +40,9 @@ namespace profile_test { using namespace ::profile; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + TEST(IniFileTest, WriteItemReadItem) { // Write line in chapter const char* fname = "./test_ini_file.ini"; @@ -325,6 +328,8 @@ TEST(IniFileTest, ParseLineWithComment) { EXPECT_STREQ(line, res); } +#pragma GCC diagnostic pop + } // namespace profile_test } // namespace components } // namespace test diff --git a/src/components/config_profile/test/profile_test.cc b/src/components/config_profile/test/profile_test.cc index f0f01d978d..900bbd1f93 100644 --- a/src/components/config_profile/test/profile_test.cc +++ b/src/components/config_profile/test/profile_test.cc @@ -679,6 +679,7 @@ TEST_F(ProfileTest, CheckVectorContainer) { std::vector<int> diagmodes_list = profile_.ReadIntContainer("MAIN", "SupportedDiagModes", &isread); EXPECT_TRUE(isread); + // Compare with result of ReadIntContainer ASSERT_EQ(diag_modes.size(), diagmodes_list.size()); bool isEqual = true; diff --git a/src/components/transport_manager/CMakeLists.txt b/src/components/transport_manager/CMakeLists.txt index 82cc00baf2..2086b37b52 100644 --- a/src/components/transport_manager/CMakeLists.txt +++ b/src/components/transport_manager/CMakeLists.txt @@ -38,6 +38,7 @@ include_directories ( ${COMPONENTS_DIR}/connection_handler/include ${COMPONENTS_DIR}/config_profile/include ${COMPONENTS_DIR}/resumption/include + ${COMPONENTS_DIR}/smart_objects/include ${POLICY_GLOBAL_INCLUDE_PATH}/ ${JSONCPP_INCLUDE_DIRECTORY} ${LOG4CXX_INCLUDE_DIRECTORY} |