/* * Copyright (c) 2023 JIUYILIAN Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "IpcConfig.h" #include "ILog.h" #include IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; mCfgMapInt.insert( std::make_pair< IpcConfigKey, std::reference_wrapper>( IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); } const StatusCode IpcConfig::Init(void) { memset(&mAllData, 0, sizeof(Config_s)); mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH); if (nullptr == mCfg) { LogError("Open config file failed.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } ReadAllConfigParameters(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode IpcConfig::UnInit(void) { if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save config files.\n"); ConfigSaveFile(mCfg); } CloseConfigFile(mCfg); return CreateStatusCode(STATUS_CODE_OK); } const int IpcConfig::GetInt(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapInt.find(key); if (iter != mCfgMapInt.end()) { return iter->second; } LogError("Can't find the key.\n"); constexpr int UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) { std::map>::iterator iter; iter = mCfgMapInt.find(key); if (iter != mCfgMapInt.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } else { LogError("Can't find the key.\n"); } } void IpcConfig::ReadAllConfigParameters(void) { StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum)); if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) { LogWarning("test_num doesn't exist, will make it as default.\n"); mCfgChanged = CONFIG_HAS_CHANGED; constexpr int DEFAULT_TEST_NUM = 10; mAllData.testNum = DEFAULT_TEST_NUM; ConfigSetInt(mCfg, "test_num", mAllData.testNum); } if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save the config file.\n"); mCfgChanged = CONFIG_HAS_NOT_CHANGED; ConfigSaveFile(mCfg); } }