Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open INI file less #3688

Merged
merged 18 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/config_profile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -44,6 +45,7 @@ set(PATHS
)

set(LIBRARIES
SmartObjects
Utils
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -989,6 +990,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_;
Expand Down Expand Up @@ -1114,6 +1116,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_;
Expand Down
216 changes: 160 additions & 56 deletions src/components/config_profile/src/profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -64,6 +63,8 @@ namespace {
<< "'."); \
}

#define INI_LINE_LEN 1024

const char* kDefaultConfigFileName = "smartDeviceLink.ini";

const char* kMainSection = "MAIN";
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1253,6 +1223,74 @@ const std::string Profile::hmi_origin_id() const {
void Profile::UpdateValues() {
SDL_LOG_AUTO_TRACE();

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;
}
jacobkeeler marked this conversation as resolved.
Show resolved Hide resolved

// SDL version
ReadStringValue(
&sdl_version_, kDefaultSDLVersion, kMainSection, kSDLVersionKey);
Expand Down Expand Up @@ -2134,6 +2172,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,
Expand Down Expand Up @@ -2610,18 +2715,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;
}

Expand All @@ -2638,10 +2743,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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/components/config_profile/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions src/components/config_profile/test/profile_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/components/transport_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down