From 8cacfdf5d28bb90db74edbd687b34afb00dabb57 Mon Sep 17 00:00:00 2001 From: fancy <258828110.@qq.com> Date: Fri, 8 Dec 2023 06:26:00 -0800 Subject: [PATCH 01/13] Delete subdirectory CMakeLists.txt --- utils/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index f410c72..5082b1f 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -6,4 +6,4 @@ add_subdirectory(Log) add_subdirectory(SharedData) add_subdirectory(UartDevice) add_subdirectory(LinuxApi) -add_subdirectory(MultiProcess) +# add_subdirectory(MultiProcess) From 9e78b4e1632715fdad923fdb52fed879913de629 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Thu, 7 Dec 2023 01:06:28 +0800 Subject: [PATCH 02/13] =?UTF-8?q?[zhoulongyu]=E6=B7=BB=E5=8A=A0=E5=A4=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=A7=A3=E5=86=B3=E7=BC=96=E8=AF=91=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hal/include/IHalCpp.h | 1 + 1 file changed, 1 insertion(+) diff --git a/hal/include/IHalCpp.h b/hal/include/IHalCpp.h index d7e0699..66a4993 100644 --- a/hal/include/IHalCpp.h +++ b/hal/include/IHalCpp.h @@ -15,6 +15,7 @@ #ifndef IHALCPP_H #define IHALCPP_H #include "StatusCode.h" +#include #include constexpr int INVALID_PERIOD = -1; constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100; From 91ae7dbc67d6736d1f97908c27f9b18640d7cfca Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Thu, 7 Dec 2023 21:28:29 +0800 Subject: [PATCH 03/13] =?UTF-8?q?[zhoulongyu]=E5=A2=9E=E5=8A=A0=E5=B8=B8?= =?UTF-8?q?=E8=A7=81=E7=9A=84=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/include/IIpcConfig.h | 24 +++ middleware/IpcConfig/src/IpcConfig.cpp | 241 ++++++++++++++++++++++ middleware/IpcConfig/src/IpcConfig.h | 28 ++- 3 files changed, 292 insertions(+), 1 deletion(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index 6904321..e745127 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -16,6 +16,9 @@ #define IIPCCONFIG_H #include "StatusCode.h" #include +#include +#include + enum class IpcConfigKey { TEST_NUM = 0, @@ -31,6 +34,27 @@ public: virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const int GetInt(const IpcConfigKey &key) { return -1; } virtual void SetInt(const IpcConfigKey &key, const int &value) {} + virtual const short GetShort(const IpcConfigKey &key) { return -1; } + virtual void SetShort(const IpcConfigKey &key, const short &value) {} + virtual const long GetLong(const IpcConfigKey &key) { return -1; } + virtual void SetLong(const IpcConfigKey &key, const long &value) {} + virtual const long long GetLongLong(const IpcConfigKey &key) { return -1; } + virtual void SetLongLong(const IpcConfigKey &key, const long long &value) {} + virtual const char GetChar(const IpcConfigKey &key) { return '\0'; } + virtual void SetChar(const IpcConfigKey &key, const char &value) {} + virtual const float GetFloat(const IpcConfigKey &key) { return -1.0; } + virtual void SetFloat(const IpcConfigKey &key, const float &value) {} + virtual const double GetDouble(const IpcConfigKey &key) { return -1.0; } + virtual void SetDouble(const IpcConfigKey &key, const double &value) {} + virtual const long double GetLongDouble(const IpcConfigKey &key) { return -1.0; } + virtual void SetLongDouble(const IpcConfigKey &key, const long double &value) {} + virtual const bool GetBool(const IpcConfigKey &key) { return true; } + virtual void SetBool(const IpcConfigKey &key, const bool &value) {} + virtual const std::string_view GetString(const IpcConfigKey &key) { return "undefine"; } + virtual void SetString(const IpcConfigKey &key, const std::string string) {} + + + }; bool CreateIpcConfig(void); #endif \ No newline at end of file diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index bfed7d6..0013de9 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -15,6 +15,7 @@ #include "IpcConfig.h" #include "ILog.h" #include + IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; @@ -73,6 +74,246 @@ void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) LogError("Can't find the key.\n"); } } +const short IpcConfig::GetShort(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapShort.find(key); + if (iter != mCfgMapShort.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr short UNKNOWN_CONFIG = -1; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetShort(const IpcConfigKey &key, const short &value) +{ + std::map>::iterator iter; + iter = mCfgMapShort.find(key); + if (iter != mCfgMapShort.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const long IpcConfig::GetLong(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapLong.find(key); + if (iter != mCfgMaplong.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr long UNKNOWN_CONFIG = -1; + return UNKNOWN_CONFIG; +} + +void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) +{ + std::map>::iterator iter; + iter = mCfgMaLlong.find(key); + if (iter != mCfgMapLong.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const long long IpcConfig::GetLongLong(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapLongLong.find(key); + if (iter != mCfgMapLongLong.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr long long UNKNOWN_CONFIG = -1; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetLongLong(const IpcConfigKey &key, const long long &value) +{ + std::map>::iterator iter; + iter = mCfgMapLongLong.find(key); + if (iter != mCfgMapLongLong.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const char IpcConfig::GetChar(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapChar.find(key); + if (iter != mCfgMapChar.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr char UNKNOWN_CONFIG = '\0'; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetChar(const IpcConfigKey &key, const char &character) +{ + std::map>::iterator iter; + iter = mCfgMapChar.find(key); + if (iter != mCfgMapChar.end()) + { + iter->second.get() = character; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const float IpcConfig::GetFloat(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapFloat.find(key); + if (iter != mCfgMapFloat.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr float UNKNOWN_CONFIG = -1.0; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value) +{ + std::map>::iterator iter; + iter = mCfgMapFloat.find(key); + if (iter != mCfgMapFloat.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const double IpcConfig::GetDouble(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapDouble.find(key); + if (iter != mCfgMapDouble.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr double UNKNOWN_CONFIG = -1.0; + return UNKNOWN_CONFIG; +} + +void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) +{ + std::map>::iterator iter; + iter = mCfgMapDouble.find(key); + if (iter != mCfgMapDouble.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const long double IpcConfig::GetLongDouble(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapLongDouble.find(key); + if (iter != mCfgMapLongDouble.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr long double UNKNOWN_CONFIG = -1.0; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value) +{ + std::map>::iterator iter; + iter = mCfgMapLongDouble.find(key); + if (iter != mCfgMapLongDouble.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const bool IpcConfig::GetBool(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapBool.find(key); + if (iter != mCfgMapBool.end()) + { + return iter->second; + } + LogError("Can't find the key.\n"); + constexpr bool UNKNOWN_CONFIG = false; + return UNKNOWN_CONFIG; +} +void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) +{ + std::map>::iterator iter; + iter = mCfgMapBool.find(key); + if (iter != mCfgMapBool.end()) + { + iter->second.get() = value; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} +const std::string_view IpcConfig::GetString(const IpcConfigKey &key) +{ + std::map>::iterator iter; + iter = mCfgMapString.find(key); + if (iter != mCfgMapString.end()) + { + std::string s = iter->second; + std::string_view sv(s); + return sv; + } + LogError("Can't find the key.\n"); + constexpr std::string_view UNKNOWN_CONFIG = "undefine"; + return UNKNOWN_CONFIG; +} + +void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) +{ + std::map>::iterator iter; + iter = mCfgMapString.find(key); + if (iter != mCfgMapString.end()) + { + iter->second.get() = string; + mCfgChanged = CONFIG_HAS_CHANGED; + } + else + { + LogError("Can't find the key.\n"); + } +} + void IpcConfig::ReadAllConfigParameters(void) { StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum)); diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index ee37af0..1a2ba77 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -40,6 +40,25 @@ public: const StatusCode UnInit(void) override; const int GetInt(const IpcConfigKey &key) override; void SetInt(const IpcConfigKey &key, const int &value) override; + const short GetShort(const IpcConfigKey &key) override; + void SetShort(const IpcConfigKey &key, const short &value) override; + const long GetLong(const IpcConfigKey &key) override; + void SetLong(const IpcConfigKey &key, const long &value) override; + const long long GetLongLong(const IpcConfigKey &key) override; + void SetLongLong(const IpcConfigKey &key, const long long &value) override; + const char GetChar(const IpcConfigKey &key) override; + void SetChar(const IpcConfigKey &key, const char &character) override; + const float GetFloat(const IpcConfigKey &key) override; + void SetFloat(const IpcConfigKey &key, const float &value) override; + const double GetDouble(const IpcConfigKey &key) override; + void SetDouble(const IpcConfigKey &key, const double &value) override; + const long double GetLongDouble(const IpcConfigKey &key) override; + void SetLongDouble(const IpcConfigKey &key, const long double &value) override; + const bool GetBool(const IpcConfigKey &key) override; + void SetBool(const IpcConfigKey &key, const bool &value) override; + const std::string_view GetString(const IpcConfigKey &key) override; + void SetString(const IpcConfigKey &key, const std::string string) override; + private: void ReadAllConfigParameters(void); @@ -49,7 +68,14 @@ private: VConfig *mCfg; Config_s mAllData; std::map> mCfgMapInt; - std::map> mCfgMapUInt; + std::map> mCfgMapShort; + std::map> mCfgMapLong; + std::map> mCfgMapLongLong; + std::map> mCfgMapChar; + std::map> mCfgMapFloat; + std::map> mCfgMapDouble; + std::map> mCfgMapLongDouble; + std::map> mCfgMapBool; std::map> mCfgMapString; }; #endif \ No newline at end of file From 584e202b0744b7a3843165d8059e740fb2ffed5e Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Thu, 7 Dec 2023 22:55:12 +0800 Subject: [PATCH 04/13] =?UTF-8?q?[zhoulongyu]=E8=A7=84=E8=8C=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/src/IpcConfig.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 0013de9..f97f3a3 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -104,7 +104,7 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMaplong.end()) + if (iter != mCfgMapLong.end()) { return iter->second; } @@ -116,7 +116,7 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { std::map>::iterator iter; - iter = mCfgMaLlong.find(key); + iter = mCfgMapLong.find(key); if (iter != mCfgMapLong.end()) { iter->second.get() = value; From c629d6b63f17233e20e863ac8cf0410e88ce1cbe Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Sun, 10 Dec 2023 02:34:37 +0800 Subject: [PATCH 05/13] =?UTF-8?q?[zhoulongyu]:=20=E5=AE=8C=E5=96=84ConfigF?= =?UTF-8?q?ile=E7=9A=84char[]=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/include/IIpcConfig.h | 8 ++-- middleware/IpcConfig/src/IpcConfig.cpp | 46 ++++++++++++++++++----- middleware/IpcConfig/src/IpcConfig.h | 8 +++- utils/Config/include/Config.h | 5 +++ utils/Config/src/Config.c | 16 ++++++++ utils/Config/src/ConfigImpl.c | 22 +++++++++++ 6 files changed, 89 insertions(+), 16 deletions(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index e745127..b39e736 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -22,13 +22,16 @@ enum class IpcConfigKey { TEST_NUM = 0, + TEST_STRING, END }; + class IIpcConfig { public: IIpcConfig() = default; virtual ~IIpcConfig() = default; + virtual const StatusCode ConfigFileSave(void) {return CreateStatusCode(STATUS_CODE_OK); } static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } @@ -50,11 +53,8 @@ public: virtual void SetLongDouble(const IpcConfigKey &key, const long double &value) {} virtual const bool GetBool(const IpcConfigKey &key) { return true; } virtual void SetBool(const IpcConfigKey &key, const bool &value) {} - virtual const std::string_view GetString(const IpcConfigKey &key) { return "undefine"; } + virtual const std::string GetString(const IpcConfigKey &key) { return "undefine"; } virtual void SetString(const IpcConfigKey &key, const std::string string) {} - - - }; bool CreateIpcConfig(void); #endif \ No newline at end of file diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index f97f3a3..d745141 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -25,6 +25,13 @@ IpcConfig::IpcConfig() std::reference_wrapper>( IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); + + mCfgMapString.insert( + std::make_pair< + IpcConfigKey, + std::reference_wrapper>( + IpcConfigKey::TEST_STRING, + std::reference_wrapper(mAllData.testString))); } const StatusCode IpcConfig::Init(void) { @@ -48,6 +55,11 @@ const StatusCode IpcConfig::UnInit(void) CloseConfigFile(mCfg); return CreateStatusCode(STATUS_CODE_OK); } +const StatusCode IpcConfig::ConfigFileSave(void) +{ + return ConfigSaveFile(mCfg); +} + const int IpcConfig::GetInt(const IpcConfigKey &key) { std::map>::iterator iter; @@ -112,7 +124,6 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) constexpr long UNKNOWN_CONFIG = -1; return UNKNOWN_CONFIG; } - void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { std::map>::iterator iter; @@ -217,7 +228,6 @@ const double IpcConfig::GetDouble(const IpcConfigKey &key) constexpr double UNKNOWN_CONFIG = -1.0; return UNKNOWN_CONFIG; } - void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) { std::map>::iterator iter; @@ -284,28 +294,28 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) LogError("Can't find the key.\n"); } } -const std::string_view IpcConfig::GetString(const IpcConfigKey &key) +const std::string IpcConfig::GetString(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>::iterator iter; iter = mCfgMapString.find(key); if (iter != mCfgMapString.end()) { - std::string s = iter->second; - std::string_view sv(s); + std::reference_wrapper s = iter->second; + const std::string sv(s); return sv; } LogError("Can't find the key.\n"); - constexpr std::string_view UNKNOWN_CONFIG = "undefine"; + const std::string UNKNOWN_CONFIG = "undefine"; return UNKNOWN_CONFIG; } void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) { - std::map>::iterator iter; + std::map>::iterator iter; iter = mCfgMapString.find(key); if (iter != mCfgMapString.end()) { - iter->second.get() = string; + strncpy(iter->second, string.c_str(), sizeof(CHAR_STRING)); mCfgChanged = CONFIG_HAS_CHANGED; } else @@ -325,10 +335,26 @@ void IpcConfig::ReadAllConfigParameters(void) mAllData.testNum = DEFAULT_TEST_NUM; ConfigSetInt(mCfg, "test_num", mAllData.testNum); } + + const char *testString = NULL; + StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); + if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) + { + LogWarning("test_string doesn't exist, will make it as default.\n"); + mCfgChanged = CONFIG_HAS_CHANGED; + char DEFAULT_TEST_STRING[] = "undefine"; + strncpy(mAllData.testString, DEFAULT_TEST_STRING, sizeof(mAllData.testString)); + ConfigSetString(mCfg, "test_string", mAllData.testString); + } + else + { + strncpy(mAllData.testString, testString, sizeof(mAllData.testString)); + } + if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save the config file.\n"); mCfgChanged = CONFIG_HAS_NOT_CHANGED; ConfigSaveFile(mCfg); } -} \ No newline at end of file +} diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 1a2ba77..d21517c 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -21,9 +21,12 @@ #include constexpr bool CONFIG_HAS_CHANGED = true; constexpr bool CONFIG_HAS_NOT_CHANGED = false; + +typedef char CHAR_STRING[256]; typedef struct Config_s { int testNum; + CHAR_STRING testString; } Config_s; class MapInt { @@ -38,6 +41,7 @@ public: virtual ~IpcConfig() = default; const StatusCode Init(void) override; const StatusCode UnInit(void) override; + const StatusCode ConfigFileSave(void) override; const int GetInt(const IpcConfigKey &key) override; void SetInt(const IpcConfigKey &key, const int &value) override; const short GetShort(const IpcConfigKey &key) override; @@ -56,7 +60,7 @@ public: void SetLongDouble(const IpcConfigKey &key, const long double &value) override; const bool GetBool(const IpcConfigKey &key) override; void SetBool(const IpcConfigKey &key, const bool &value) override; - const std::string_view GetString(const IpcConfigKey &key) override; + const std::string GetString(const IpcConfigKey &key) override; void SetString(const IpcConfigKey &key, const std::string string) override; @@ -76,6 +80,6 @@ private: std::map> mCfgMapDouble; std::map> mCfgMapLongDouble; std::map> mCfgMapBool; - std::map> mCfgMapString; + std::map> mCfgMapString; }; #endif \ No newline at end of file diff --git a/utils/Config/include/Config.h b/utils/Config/include/Config.h index e67fe6e..43a7e1c 100644 --- a/utils/Config/include/Config.h +++ b/utils/Config/include/Config.h @@ -29,6 +29,8 @@ extern "C" { const StatusCode (*get_int)(VConfig *, const char *, int *); const StatusCode (*set_int)(VConfig *, const char *, const int); + const StatusCode (*get_string)(VConfig *, const char *, const char**); + const StatusCode (*set_string)(VConfig *, const char *, const char*); const StatusCode (*save)(VConfig *); } VConfig; const StatusCode ConfigInit(void); @@ -38,6 +40,9 @@ extern "C" void CloseConfigFile(VConfig *cfg); const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value); const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value); + + const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value); + const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char* value); #ifdef __cplusplus } #endif diff --git a/utils/Config/src/Config.c b/utils/Config/src/Config.c index a35660d..2477d55 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -60,4 +60,20 @@ const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value) return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->set_int(cfg, name, value); +} +const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value) +{ + if (NULL == cfg) + { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->get_string(cfg, name, value); +} +const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value) +{ + if (NULL == cfg) + { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->set_string(cfg, name, value); } \ No newline at end of file diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index 28f1223..6939205 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -62,6 +62,25 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i config_setting_set_int(setting, value); return CreateStatusCode(STATUS_CODE_OK); } +static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char** value) +{ + int result = 0; + // config_setting_t *root; + result = config_lookup_string(&(((Config *)cfg)->cfg), name, value); + if (CONFIG_FALSE == result) + { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, const char* value) +{ + config_setting_t *root, *setting; + root = config_root_setting(&(((Config *)cfg)->cfg)); + setting = config_setting_add(root, name, CONFIG_TYPE_STRING); + config_setting_set_string(setting, value); + return CreateStatusCode(STATUS_CODE_OK); +} static void ConfigImplInit(Config *cfg) { if (NULL == cfg) @@ -73,6 +92,9 @@ static void ConfigImplInit(Config *cfg) cfg->close = ConfigClose; cfg->base.get_int = ConfigGetIntImpl; cfg->base.set_int = ConfigSetIntImpl; + + cfg->base.get_string = ConfigGetStringImpl; + cfg->base.set_string = ConfigSetStringImpl; cfg->base.save = ConfigSaveFileImpl; } Config *NewConfig(const char *fileName) From efa068fea2abce553db768f4c4dac7d38e0f27bf Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Sun, 10 Dec 2023 22:24:27 +0800 Subject: [PATCH 06/13] =?UTF-8?q?[zhoulongyu]:=20=E8=B0=83=E7=94=A8=20conf?= =?UTF-8?q?ig=5Fsetting=5Fadd=E5=89=8D,=20=E5=85=88=E8=B0=83=E7=94=A8confi?= =?UTF-8?q?g=5Fsetting=5Fget=5Fmember=20=E5=88=A4=E6=96=AD=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=98=AF=E5=90=A6=E5=B7=B2=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/Config/src/ConfigImpl.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index 6939205..0daee56 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -58,7 +58,11 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i { config_setting_t *root, *setting; root = config_root_setting(&(((Config *)cfg)->cfg)); - setting = config_setting_add(root, name, CONFIG_TYPE_INT); + setting = config_setting_get_member(root, name); + if(!setting) + { + setting = config_setting_add(root, name, CONFIG_TYPE_INT); + } config_setting_set_int(setting, value); return CreateStatusCode(STATUS_CODE_OK); } @@ -77,7 +81,11 @@ static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, cons { config_setting_t *root, *setting; root = config_root_setting(&(((Config *)cfg)->cfg)); - setting = config_setting_add(root, name, CONFIG_TYPE_STRING); + setting = config_setting_get_member(root, name); + if(!setting) + { + setting = config_setting_add(root, name, CONFIG_TYPE_STRING); + } config_setting_set_string(setting, value); return CreateStatusCode(STATUS_CODE_OK); } @@ -92,7 +100,6 @@ static void ConfigImplInit(Config *cfg) cfg->close = ConfigClose; cfg->base.get_int = ConfigGetIntImpl; cfg->base.set_int = ConfigSetIntImpl; - cfg->base.get_string = ConfigGetStringImpl; cfg->base.set_string = ConfigSetStringImpl; cfg->base.save = ConfigSaveFileImpl; From 1d214080cee990e39fb5088931546353f4bb2a32 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Sun, 10 Dec 2023 23:25:44 +0800 Subject: [PATCH 07/13] =?UTF-8?q?[zhoulongyu]:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=92=8C=E5=AE=8C=E5=96=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84test=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/middleware/IpcConfig/src/IpcConfig_Test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index 8713f2f..1088742 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -13,6 +13,15 @@ namespace IpcConfigTest IIpcConfig::GetInstance()->Init(); int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); LogInfo("Get testNum = %d\n", testNum); + const int num999 = 999; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999); + + const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING); + LogInfo("Get testString = %s\n", testString.c_str()); + const std::string string = "define"; + IIpcConfig::GetInstance()->SetString(IpcConfigKey::TEST_STRING, string); + IIpcConfig::GetInstance()->ConfigFileSave(); + IIpcConfig::GetInstance()->UnInit(); ILogUnInit(); DestroyLogModule(); From b94f6ac95631a34b815a428742e541c5cd8a1259 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Tue, 12 Dec 2023 10:36:07 +0800 Subject: [PATCH 08/13] =?UTF-8?q?[zhoulongyu]:=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=B5=8C=E5=A5=97std::map=20=E6=8A=8A=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9B=B8=E5=85=B3=E7=9A=84=E4=B8=89=E4=B8=AA?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=98=A0=E5=B0=84=E8=B5=B7=E6=9D=A5,=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90char[]=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84set=E5=92=8Cget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/src/IpcConfig.cpp | 30 ++++++++++++++------------ middleware/IpcConfig/src/IpcConfig.h | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index d745141..1e28df9 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -16,6 +16,8 @@ #include "ILog.h" #include +#define CHECK_MAP(map) (map.size()==1? true:false) + IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; @@ -26,12 +28,13 @@ IpcConfig::IpcConfig() IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); - mCfgMapString.insert( - std::make_pair< - IpcConfigKey, - std::reference_wrapper>( - IpcConfigKey::TEST_STRING, + std::map> innerMap; + innerMap.insert( + std::make_pair( + "test_string", std::reference_wrapper(mAllData.testString))); + mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap)); + } const StatusCode IpcConfig::Init(void) { @@ -296,26 +299,26 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) } const std::string IpcConfig::GetString(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapString.find(key); - if (iter != mCfgMapString.end()) + if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { - std::reference_wrapper s = iter->second; - const std::string sv(s); + const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg return sv; } LogError("Can't find the key.\n"); const std::string UNKNOWN_CONFIG = "undefine"; return UNKNOWN_CONFIG; } - void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapString.find(key); - if (iter != mCfgMapString.end()) + if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { - strncpy(iter->second, string.c_str(), sizeof(CHAR_STRING)); + strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[] + const char * name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * + ConfigSetString(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } else @@ -335,7 +338,6 @@ void IpcConfig::ReadAllConfigParameters(void) mAllData.testNum = DEFAULT_TEST_NUM; ConfigSetInt(mCfg, "test_num", mAllData.testNum); } - const char *testString = NULL; StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index d21517c..6bad323 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -80,6 +80,6 @@ private: std::map> mCfgMapDouble; std::map> mCfgMapLongDouble; std::map> mCfgMapBool; - std::map> mCfgMapString; + std::map>> mCfgMapString; }; #endif \ No newline at end of file From c5691b1b957baaa4eaa385bfacd85361c59f7d4a Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Tue, 12 Dec 2023 11:46:09 +0800 Subject: [PATCH 09/13] =?UTF-8?q?[zhoulongyu]:=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/include/IIpcConfig.h | 8 +- middleware/IpcConfig/src/IIpcConfig.cpp | 13 +- middleware/IpcConfig/src/IpcConfig.cpp | 141 ++++++------------ middleware/IpcConfig/src/IpcConfig.h | 11 +- middleware/IpcConfig/src/IpcConfigMakePtr.cpp | 10 +- middleware/IpcConfig/src/IpcConfigMakePtr.h | 4 +- .../IpcConfig/src/IpcConfig_Test.cpp | 42 +++--- 7 files changed, 88 insertions(+), 141 deletions(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index b39e736..04c0e6e 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -3,9 +3,9 @@ * 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. @@ -15,9 +15,9 @@ #ifndef IIPCCONFIG_H #define IIPCCONFIG_H #include "StatusCode.h" +#include #include #include -#include enum class IpcConfigKey { @@ -31,7 +31,7 @@ class IIpcConfig public: IIpcConfig() = default; virtual ~IIpcConfig() = default; - virtual const StatusCode ConfigFileSave(void) {return CreateStatusCode(STATUS_CODE_OK); } + virtual const StatusCode ConfigFileSave(void) { return CreateStatusCode(STATUS_CODE_OK); } static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } diff --git a/middleware/IpcConfig/src/IIpcConfig.cpp b/middleware/IpcConfig/src/IIpcConfig.cpp index 41ee2de..422240a 100644 --- a/middleware/IpcConfig/src/IIpcConfig.cpp +++ b/middleware/IpcConfig/src/IIpcConfig.cpp @@ -3,9 +3,9 @@ * 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. @@ -17,15 +17,12 @@ std::shared_ptr &IIpcConfig::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); - if (impl) - { - if (instance.use_count() == 1) - { + if (impl) { + if (instance.use_count() == 1) { LogInfo("Instance changed succeed.\n"); instance = *impl; } - else - { + else { LogError("Can't changing the instance becase of using by some one.\n"); } } diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 1e28df9..a07ca58 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -3,9 +3,9 @@ * 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. @@ -16,32 +16,23 @@ #include "ILog.h" #include -#define CHECK_MAP(map) (map.size()==1? true:false) +#define CHECK_MAP(map) (map.size() == 1 ? true : false) IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; - mCfgMapInt.insert( - std::make_pair< - IpcConfigKey, - std::reference_wrapper>( - IpcConfigKey::TEST_NUM, - std::reference_wrapper(mAllData.testNum))); + mCfgMapInt.insert(std::make_pair>( + IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); std::map> innerMap; - innerMap.insert( - std::make_pair( - "test_string", - std::reference_wrapper(mAllData.testString))); + innerMap.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap)); - } const StatusCode IpcConfig::Init(void) { memset(&mAllData, 0, sizeof(Config_s)); mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH); - if (nullptr == mCfg) - { + if (nullptr == mCfg) { LogError("Open config file failed.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } @@ -50,25 +41,20 @@ const StatusCode IpcConfig::Init(void) } const StatusCode IpcConfig::UnInit(void) { - if (CONFIG_HAS_CHANGED == mCfgChanged) - { + if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save config files.\n"); ConfigSaveFile(mCfg); } CloseConfigFile(mCfg); return CreateStatusCode(STATUS_CODE_OK); } -const StatusCode IpcConfig::ConfigFileSave(void) -{ - return ConfigSaveFile(mCfg); -} +const StatusCode IpcConfig::ConfigFileSave(void) { return ConfigSaveFile(mCfg); } const int IpcConfig::GetInt(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapInt.find(key); - if (iter != mCfgMapInt.end()) - { + if (iter != mCfgMapInt.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -79,13 +65,11 @@ void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) { std::map>::iterator iter; iter = mCfgMapInt.find(key); - if (iter != mCfgMapInt.end()) - { + if (iter != mCfgMapInt.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -93,8 +77,7 @@ const short IpcConfig::GetShort(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapShort.find(key); - if (iter != mCfgMapShort.end()) - { + if (iter != mCfgMapShort.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -105,13 +88,11 @@ void IpcConfig::SetShort(const IpcConfigKey &key, const short &value) { std::map>::iterator iter; iter = mCfgMapShort.find(key); - if (iter != mCfgMapShort.end()) - { + if (iter != mCfgMapShort.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -119,8 +100,7 @@ const long IpcConfig::GetLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMapLong.end()) - { + if (iter != mCfgMapLong.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -131,13 +111,11 @@ void IpcConfig::SetLong(const IpcConfigKey &key, const long &value) { std::map>::iterator iter; iter = mCfgMapLong.find(key); - if (iter != mCfgMapLong.end()) - { + if (iter != mCfgMapLong.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -145,8 +123,7 @@ const long long IpcConfig::GetLongLong(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLongLong.find(key); - if (iter != mCfgMapLongLong.end()) - { + if (iter != mCfgMapLongLong.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -157,13 +134,11 @@ void IpcConfig::SetLongLong(const IpcConfigKey &key, const long long &value) { std::map>::iterator iter; iter = mCfgMapLongLong.find(key); - if (iter != mCfgMapLongLong.end()) - { + if (iter != mCfgMapLongLong.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -171,8 +146,7 @@ const char IpcConfig::GetChar(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapChar.find(key); - if (iter != mCfgMapChar.end()) - { + if (iter != mCfgMapChar.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -183,13 +157,11 @@ void IpcConfig::SetChar(const IpcConfigKey &key, const char &character) { std::map>::iterator iter; iter = mCfgMapChar.find(key); - if (iter != mCfgMapChar.end()) - { + if (iter != mCfgMapChar.end()) { iter->second.get() = character; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -197,8 +169,7 @@ const float IpcConfig::GetFloat(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapFloat.find(key); - if (iter != mCfgMapFloat.end()) - { + if (iter != mCfgMapFloat.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -209,13 +180,11 @@ void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value) { std::map>::iterator iter; iter = mCfgMapFloat.find(key); - if (iter != mCfgMapFloat.end()) - { + if (iter != mCfgMapFloat.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -223,8 +192,7 @@ const double IpcConfig::GetDouble(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapDouble.find(key); - if (iter != mCfgMapDouble.end()) - { + if (iter != mCfgMapDouble.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -235,13 +203,11 @@ void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value) { std::map>::iterator iter; iter = mCfgMapDouble.find(key); - if (iter != mCfgMapDouble.end()) - { + if (iter != mCfgMapDouble.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -249,8 +215,7 @@ const long double IpcConfig::GetLongDouble(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapLongDouble.find(key); - if (iter != mCfgMapLongDouble.end()) - { + if (iter != mCfgMapLongDouble.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -261,13 +226,11 @@ void IpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value) { std::map>::iterator iter; iter = mCfgMapLongDouble.find(key); - if (iter != mCfgMapLongDouble.end()) - { + if (iter != mCfgMapLongDouble.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -275,8 +238,7 @@ const bool IpcConfig::GetBool(const IpcConfigKey &key) { std::map>::iterator iter; iter = mCfgMapBool.find(key); - if (iter != mCfgMapBool.end()) - { + if (iter != mCfgMapBool.end()) { return iter->second; } LogError("Can't find the key.\n"); @@ -287,13 +249,11 @@ void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value) { std::map>::iterator iter; iter = mCfgMapBool.find(key); - if (iter != mCfgMapBool.end()) - { + if (iter != mCfgMapBool.end()) { iter->second.get() = value; mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -301,9 +261,8 @@ const std::string IpcConfig::GetString(const IpcConfigKey &key) { std::map>>::iterator iter; iter = mCfgMapString.find(key); - if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) - { - const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg + if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { + const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg return sv; } LogError("Can't find the key.\n"); @@ -314,15 +273,13 @@ void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) { std::map>>::iterator iter; iter = mCfgMapString.find(key); - if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) - { - strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[] - const char * name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * + if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) { + strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[] + const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * ConfigSetString(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } - else - { + else { LogError("Can't find the key.\n"); } } @@ -330,8 +287,7 @@ void IpcConfig::SetString(const IpcConfigKey &key, const std::string string) void IpcConfig::ReadAllConfigParameters(void) { StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum)); - if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) - { + 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; @@ -340,21 +296,18 @@ void IpcConfig::ReadAllConfigParameters(void) } const char *testString = NULL; StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); - if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) - { + if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) { LogWarning("test_string doesn't exist, will make it as default.\n"); mCfgChanged = CONFIG_HAS_CHANGED; - char DEFAULT_TEST_STRING[] = "undefine"; + char DEFAULT_TEST_STRING[] = "undefine"; strncpy(mAllData.testString, DEFAULT_TEST_STRING, sizeof(mAllData.testString)); ConfigSetString(mCfg, "test_string", mAllData.testString); } - else - { + else { strncpy(mAllData.testString, testString, sizeof(mAllData.testString)); } - if (CONFIG_HAS_CHANGED == mCfgChanged) - { + if (CONFIG_HAS_CHANGED == mCfgChanged) { LogInfo("Save the config file.\n"); mCfgChanged = CONFIG_HAS_NOT_CHANGED; ConfigSaveFile(mCfg); diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 6bad323..2e472fd 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -3,9 +3,9 @@ * 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. @@ -14,11 +14,11 @@ */ #ifndef IPCCONFIG_H #define IPCCONFIG_H -#include "StatusCode.h" -#include "IIpcConfig.h" #include "Config.h" -#include +#include "IIpcConfig.h" +#include "StatusCode.h" #include +#include constexpr bool CONFIG_HAS_CHANGED = true; constexpr bool CONFIG_HAS_NOT_CHANGED = false; @@ -63,7 +63,6 @@ public: const std::string GetString(const IpcConfigKey &key) override; void SetString(const IpcConfigKey &key, const std::string string) override; - private: void ReadAllConfigParameters(void); diff --git a/middleware/IpcConfig/src/IpcConfigMakePtr.cpp b/middleware/IpcConfig/src/IpcConfigMakePtr.cpp index de87cfb..a924eac 100644 --- a/middleware/IpcConfig/src/IpcConfigMakePtr.cpp +++ b/middleware/IpcConfig/src/IpcConfigMakePtr.cpp @@ -3,9 +3,9 @@ * 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. @@ -19,8 +19,7 @@ bool CreateIpcConfig(void) { auto instance = std::make_shared(); StatusCode code = IpcConfigMakePtr::GetInstance()->CreateIpcConfig(instance); - if (IsCodeOK(code)) - { + if (IsCodeOK(code)) { LogInfo("CreateIpcConfig is ok.\n"); IIpcConfig::GetInstance(&instance); return true; @@ -30,8 +29,7 @@ bool CreateIpcConfig(void) std::shared_ptr &IpcConfigMakePtr::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); - if (impl) - { + if (impl) { instance = *impl; } return instance; diff --git a/middleware/IpcConfig/src/IpcConfigMakePtr.h b/middleware/IpcConfig/src/IpcConfigMakePtr.h index 36c737a..7ce0e20 100644 --- a/middleware/IpcConfig/src/IpcConfigMakePtr.h +++ b/middleware/IpcConfig/src/IpcConfigMakePtr.h @@ -3,9 +3,9 @@ * 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. diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index 1088742..e204624 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -1,29 +1,29 @@ -#include "ILog.h" #include "IIpcConfig.h" +#include "ILog.h" // #include #include namespace IpcConfigTest { - // ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.Demo - TEST(IpcConfigTest, Demo) - { - CreateLogModule(); - CreateIpcConfig(); - ILogInit(LOG_INSTANCE_TYPE_END); - IIpcConfig::GetInstance()->Init(); - int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); - LogInfo("Get testNum = %d\n", testNum); - const int num999 = 999; - IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999); +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.Demo +TEST(IpcConfigTest, Demo) +{ + CreateLogModule(); + CreateIpcConfig(); + ILogInit(LOG_INSTANCE_TYPE_END); + IIpcConfig::GetInstance()->Init(); + int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); + LogInfo("Get testNum = %d\n", testNum); + const int num999 = 999; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999); - const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING); - LogInfo("Get testString = %s\n", testString.c_str()); - const std::string string = "define"; - IIpcConfig::GetInstance()->SetString(IpcConfigKey::TEST_STRING, string); - IIpcConfig::GetInstance()->ConfigFileSave(); + const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING); + LogInfo("Get testString = %s\n", testString.c_str()); + const std::string string = "define"; + IIpcConfig::GetInstance()->SetString(IpcConfigKey::TEST_STRING, string); + IIpcConfig::GetInstance()->ConfigFileSave(); - IIpcConfig::GetInstance()->UnInit(); - ILogUnInit(); - DestroyLogModule(); - } + IIpcConfig::GetInstance()->UnInit(); + ILogUnInit(); + DestroyLogModule(); +} } // namespace IpcConfigTest \ No newline at end of file From d44154773f234580cf27aaffd2e5ba8478d4ecb0 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Tue, 12 Dec 2023 11:54:46 +0800 Subject: [PATCH 10/13] =?UTF-8?q?[zhoulongyu]:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/CMakeLists.txt | 15 +++++++++++++++ test/middleware/IpcConfig/CMakeLists.txt | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/middleware/IpcConfig/CMakeLists.txt b/middleware/IpcConfig/CMakeLists.txt index 3c762c5..32d2e74 100644 --- a/middleware/IpcConfig/CMakeLists.txt +++ b/middleware/IpcConfig/CMakeLists.txt @@ -47,4 +47,19 @@ add_custom_command( COMMAND make IpcConfig_code_check WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ ) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + IpcConfig_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/IpcConfig +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make IpcConfig_code_check + COMMAND make IpcConfig_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) endif() \ No newline at end of file diff --git a/test/middleware/IpcConfig/CMakeLists.txt b/test/middleware/IpcConfig/CMakeLists.txt index ffdb30c..100f6f9 100644 --- a/test/middleware/IpcConfig/CMakeLists.txt +++ b/test/middleware/IpcConfig/CMakeLists.txt @@ -53,5 +53,21 @@ add_custom_command( COMMAND make IpcConfigTest_code_check WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ ) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + IpcConfigTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/IpcConfig +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make IpcConfigTest_code_check + COMMAND make IpcConfigTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) endif() define_file_name(${TARGET_NAME}) \ No newline at end of file From 730c38b2c13861ab841a82841f785741ded297c3 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Tue, 12 Dec 2023 15:15:39 +0800 Subject: [PATCH 11/13] =?UTF-8?q?[zhoulongyu]:=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=BA=93=E7=9A=84Int=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/src/IpcConfig.cpp | 26 +++++++++++++++----------- middleware/IpcConfig/src/IpcConfig.h | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index a07ca58..00b5c7f 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -21,12 +21,14 @@ IpcConfig::IpcConfig() { mCfgChanged = CONFIG_HAS_NOT_CHANGED; - mCfgMapInt.insert(std::make_pair>( - IpcConfigKey::TEST_NUM, std::reference_wrapper(mAllData.testNum))); - std::map> innerMap; - innerMap.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); - mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMap)); + std::map> innerMapInt; + innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper(mAllData.testNum))); + mCfgMapInt.insert(std::make_pair(IpcConfigKey::TEST_NUM, innerMapInt)); + + std::map> innerMapString; + innerMapString.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); + mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString)); } const StatusCode IpcConfig::Init(void) { @@ -52,10 +54,10 @@ const StatusCode IpcConfig::ConfigFileSave(void) { return ConfigSaveFile(mCfg); const int IpcConfig::GetInt(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapInt.find(key); - if (iter != mCfgMapInt.end()) { - return iter->second; + if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) { + return iter->second.begin()->second; } LogError("Can't find the key.\n"); constexpr int UNKNOWN_CONFIG = -1; @@ -63,10 +65,12 @@ const int IpcConfig::GetInt(const IpcConfigKey &key) } void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapInt.find(key); - if (iter != mCfgMapInt.end()) { - iter->second.get() = value; + if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) { + iter->second.begin()->second.get() = value; + const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * + ConfigSetInt(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } else { diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 2e472fd..4828ef8 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -70,7 +70,7 @@ private: bool mCfgChanged; VConfig *mCfg; Config_s mAllData; - std::map> mCfgMapInt; + std::map>> mCfgMapInt; std::map> mCfgMapShort; std::map> mCfgMapLong; std::map> mCfgMapLongLong; From d06477d707caba3a2b7af5e591b58ac646f6aa42 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Tue, 12 Dec 2023 23:58:22 +0800 Subject: [PATCH 12/13] =?UTF-8?q?[zhoulongyu]:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/Config/CMakeLists.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/utils/Config/CMakeLists.txt b/utils/Config/CMakeLists.txt index 325f342..ba49d3b 100644 --- a/utils/Config/CMakeLists.txt +++ b/utils/Config/CMakeLists.txt @@ -41,6 +41,21 @@ add_custom_command( COMMAND make Config_code_check WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ ) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + Config_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/Config +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make Config_code_check + COMMAND make Config_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) endif() # build libconfig before make libConfig.a From 9aebd2158b634c0522c84ba35564ae98a203d973 Mon Sep 17 00:00:00 2001 From: jas <1790731762@qq.com> Date: Wed, 13 Dec 2023 00:22:50 +0800 Subject: [PATCH 13/13] =?UTF-8?q?[zhoulongyu]:=201.=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=BA=93=E7=9A=84short=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B;=202.=E4=BD=BF=E7=94=A8=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E8=A7=84=E8=8C=83=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/IpcConfig/include/IIpcConfig.h | 1 + middleware/IpcConfig/src/IpcConfig.cpp | 32 ++++-- middleware/IpcConfig/src/IpcConfig.h | 3 +- .../IpcConfig/src/IpcConfig_Test.cpp | 9 +- utils/Config/include/Config.h | 58 +++++------ utils/Config/src/Config.c | 55 ++++++----- utils/Config/src/ConfigCode.c | 23 ++--- utils/Config/src/ConfigCode.h | 7 +- utils/Config/src/ConfigImpl.c | 99 +++++++++++-------- utils/Config/src/ConfigImpl.h | 23 +++-- 10 files changed, 173 insertions(+), 137 deletions(-) diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index 04c0e6e..2d89b43 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -22,6 +22,7 @@ enum class IpcConfigKey { TEST_NUM = 0, + TEST_SHORT, TEST_STRING, END }; diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 00b5c7f..081fa11 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -26,6 +26,10 @@ IpcConfig::IpcConfig() innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper(mAllData.testNum))); mCfgMapInt.insert(std::make_pair(IpcConfigKey::TEST_NUM, innerMapInt)); + std::map> innerMapShort; + innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper(mAllData.testShort))); + mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort)); + std::map> innerMapString; innerMapString.insert(std::make_pair("test_string", std::reference_wrapper(mAllData.testString))); mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString)); @@ -79,10 +83,10 @@ void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) } const short IpcConfig::GetShort(const IpcConfigKey &key) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapShort.find(key); - if (iter != mCfgMapShort.end()) { - return iter->second; + if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) { + return iter->second.begin()->second; } LogError("Can't find the key.\n"); constexpr short UNKNOWN_CONFIG = -1; @@ -90,10 +94,12 @@ const short IpcConfig::GetShort(const IpcConfigKey &key) } void IpcConfig::SetShort(const IpcConfigKey &key, const short &value) { - std::map>::iterator iter; + std::map>>::iterator iter; iter = mCfgMapShort.find(key); - if (iter != mCfgMapShort.end()) { - iter->second.get() = value; + if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) { + iter->second.begin()->second.get() = value; + const char *name = iter->second.begin()->first.c_str(); // const std::strinbg --> const char * + ConfigSetInt(mCfg, name, iter->second.begin()->second); mCfgChanged = CONFIG_HAS_CHANGED; } else { @@ -298,9 +304,19 @@ void IpcConfig::ReadAllConfigParameters(void) mAllData.testNum = DEFAULT_TEST_NUM; ConfigSetInt(mCfg, "test_num", mAllData.testNum); } + + StatusCode shortCode = ConfigGetShort(mCfg, "test_short", &(mAllData.testShort)); + if (StatusCodeEqual(shortCode, "CONFIG_CODE_PARAM_NOT_EXIST")) { + LogWarning("test_short doesn't exist, will make it as default.\n"); + mCfgChanged = CONFIG_HAS_CHANGED; + constexpr short DEFAULT_TEST_SHORT_NUM = 11; + mAllData.testShort = DEFAULT_TEST_SHORT_NUM; + ConfigSetShort(mCfg, "test_short", mAllData.testShort); + } + const char *testString = NULL; - StatusCode string_code = ConfigGetString(mCfg, "test_string", &(testString)); - if (StatusCodeEqual(string_code, "CONFIG_CODE_PARAM_NOT_EXIST")) { + StatusCode stringCode = ConfigGetString(mCfg, "test_string", &(testString)); + if (StatusCodeEqual(stringCode, "CONFIG_CODE_PARAM_NOT_EXIST")) { LogWarning("test_string doesn't exist, will make it as default.\n"); mCfgChanged = CONFIG_HAS_CHANGED; char DEFAULT_TEST_STRING[] = "undefine"; diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 4828ef8..32afe2b 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -26,6 +26,7 @@ typedef char CHAR_STRING[256]; typedef struct Config_s { int testNum; + short testShort; CHAR_STRING testString; } Config_s; class MapInt @@ -71,7 +72,7 @@ private: VConfig *mCfg; Config_s mAllData; std::map>> mCfgMapInt; - std::map> mCfgMapShort; + std::map>> mCfgMapShort; std::map> mCfgMapLong; std::map> mCfgMapLongLong; std::map> mCfgMapChar; diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp index e204624..84e6e82 100644 --- a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -13,8 +13,13 @@ TEST(IpcConfigTest, Demo) IIpcConfig::GetInstance()->Init(); int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); LogInfo("Get testNum = %d\n", testNum); - const int num999 = 999; - IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, num999); + const int numInt = 999; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::TEST_NUM, numInt); + + short testShort = IIpcConfig::GetInstance()->GetShort(IpcConfigKey::TEST_SHORT); + LogInfo("Get test_short = %d\n", testShort); + const int numShort = 888; + IIpcConfig::GetInstance()->SetShort(IpcConfigKey::TEST_SHORT, numShort); const std::string testString = IIpcConfig::GetInstance()->GetString(IpcConfigKey::TEST_STRING); LogInfo("Get testString = %s\n", testString.c_str()); diff --git a/utils/Config/include/Config.h b/utils/Config/include/Config.h index 43a7e1c..c974d06 100644 --- a/utils/Config/include/Config.h +++ b/utils/Config/include/Config.h @@ -3,9 +3,9 @@ * 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. @@ -16,33 +16,35 @@ #define CONFIG_H #include "StatusCode.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - enum CONFIG_CODE - { - CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, - CONFIG_CODE_END - }; - typedef struct v_config VConfig; - typedef struct v_config - { - const StatusCode (*get_int)(VConfig *, const char *, int *); - const StatusCode (*set_int)(VConfig *, const char *, const int); - const StatusCode (*get_string)(VConfig *, const char *, const char**); - const StatusCode (*set_string)(VConfig *, const char *, const char*); - const StatusCode (*save)(VConfig *); - } VConfig; - const StatusCode ConfigInit(void); - const StatusCode ConfigUnInit(void); - VConfig *OpenConfigFile(const char *fileName); - const StatusCode ConfigSaveFile(VConfig *cfg); - void CloseConfigFile(VConfig *cfg); - const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value); - const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value); - - const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value); - const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char* value); +enum CONFIG_CODE +{ + CONFIG_CODE_PARAM_NOT_EXIST = STATUS_CODE_END, + CONFIG_CODE_END +}; +typedef struct v_config VConfig; +typedef struct v_config +{ + const StatusCode (*get_int)(VConfig *, const char *, int *); + const StatusCode (*set_int)(VConfig *, const char *, const int); + const StatusCode (*get_short)(VConfig *, const char *, short *); + const StatusCode (*set_short)(VConfig *, const char *, const short); + const StatusCode (*get_string)(VConfig *, const char *, const char **); + const StatusCode (*set_string)(VConfig *, const char *, const char *); + const StatusCode (*save)(VConfig *); +} VConfig; +const StatusCode ConfigInit(void); +const StatusCode ConfigUnInit(void); +VConfig *OpenConfigFile(const char *fileName); +const StatusCode ConfigSaveFile(VConfig *cfg); +void CloseConfigFile(VConfig *cfg); +const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value); +const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value); +const StatusCode ConfigGetShort(VConfig *cfg, const char *name, short *value); +const StatusCode ConfigSetShort(VConfig *cfg, const char *name, const short value); +const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value); +const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value); #ifdef __cplusplus } #endif diff --git a/utils/Config/src/Config.c b/utils/Config/src/Config.c index 2477d55..2e9c6be 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -3,9 +3,9 @@ * 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. @@ -16,30 +16,19 @@ #include "ConfigImpl.h" #include "ILog.h" #include -const StatusCode ConfigInit(void) -{ - return CreateStatusCode(STATUS_CODE_OK); -} -const StatusCode ConfigUnInit(void) -{ - return CreateStatusCode(STATUS_CODE_OK); -} -VConfig *OpenConfigFile(const char *fileName) -{ - return (VConfig *)NewConfig(fileName); -} +const StatusCode ConfigInit(void) { return CreateStatusCode(STATUS_CODE_OK); } +const StatusCode ConfigUnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } +VConfig *OpenConfigFile(const char *fileName) { return (VConfig *)NewConfig(fileName); } const StatusCode ConfigSaveFile(VConfig *cfg) { - if (NULL == cfg) - { + if (NULL == cfg) { return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->save(cfg); } void CloseConfigFile(VConfig *cfg) { - if (NULL == cfg) - { + if (NULL == cfg) { LogError("NULL config poniter.\n"); return; } @@ -47,32 +36,44 @@ void CloseConfigFile(VConfig *cfg) } const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value) { - if (NULL == cfg) - { + if (NULL == cfg) { return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->get_int(cfg, name, value); } const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int value) { - if (NULL == cfg) - { + if (NULL == cfg) { return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->set_int(cfg, name, value); } -const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char** value) + +const StatusCode ConfigGetShort(VConfig *cfg, const char *name, short *value) { - if (NULL == cfg) - { + if (NULL == cfg) { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->get_short(cfg, name, value); +} +const StatusCode ConfigSetShort(VConfig *cfg, const char *name, const short value) +{ + if (NULL == cfg) { + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + return cfg->set_short(cfg, name, value); +} + +const StatusCode ConfigGetString(VConfig *cfg, const char *name, const char **value) +{ + if (NULL == cfg) { return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->get_string(cfg, name, value); } const StatusCode ConfigSetString(VConfig *cfg, const char *name, const char *value) { - if (NULL == cfg) - { + if (NULL == cfg) { return CreateStatusCode(STATUS_CODE_NOT_OK); } return cfg->set_string(cfg, name, value); diff --git a/utils/Config/src/ConfigCode.c b/utils/Config/src/ConfigCode.c index 5f7ce00..ebb73ee 100644 --- a/utils/Config/src/ConfigCode.c +++ b/utils/Config/src/ConfigCode.c @@ -3,9 +3,9 @@ * 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. @@ -15,14 +15,12 @@ #include "ConfigCode.h" #include "ILog.h" #include -static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = { - "CONFIG_CODE_PARAM_NOT_EXIST", - "CONFIG_CODE_END"}; +static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST", + "CONFIG_CODE_END"}; static const char *PrintStringConfigCode(const StatusCode this) { const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END; - if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) - { + if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= CONFIG_CODE_END) { LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]); return ConfigCodeString[CODE_INDEX]; } @@ -31,18 +29,14 @@ static const char *PrintStringConfigCode(const StatusCode this) } static const bool CodeEqual(const StatusCode code, const char *value) { - if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) - { + if (memcmp(value, ConfigCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) { return true; } return false; } static StatusCode NewConfigCode(const long int code) { - StatusCode result = { - PrintStringConfigCode, - CodeEqual, - code}; + StatusCode result = {PrintStringConfigCode, CodeEqual, code}; return result; } const StatusCode CreateConfigCode(const long int code) @@ -51,8 +45,7 @@ const StatusCode CreateConfigCode(const long int code) // { // return CreateStatusCode(code); // } - if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) - { + if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) { return NewConfigCode(code); } LogError("undefined code.\n"); diff --git a/utils/Config/src/ConfigCode.h b/utils/Config/src/ConfigCode.h index 947cc5c..2720e89 100644 --- a/utils/Config/src/ConfigCode.h +++ b/utils/Config/src/ConfigCode.h @@ -16,13 +16,12 @@ #define CONFIGCODE_H #include "Config.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif #ifndef CONFIG_OWNER -#error This is internal file, never include it. + #error This is internal file, never include it. #endif - const StatusCode CreateConfigCode(const long int code); +const StatusCode CreateConfigCode(const long int code); #ifdef __cplusplus } #endif diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index 0daee56..f577f0f 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -3,9 +3,9 @@ * 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. @@ -13,17 +13,20 @@ * limitations under the License. */ #include "ConfigImpl.h" -#include "ILog.h" #include "ConfigCode.h" +#include "ILog.h" +#include #include -#include #include +#include + +#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true)) + static const StatusCode ConfigSaveFileImpl(VConfig *cfg) { /* Write out the new configuration. */ LogInfo("Save file[%s].\n", ((Config *)cfg)->mFileName); - if (!config_write_file(&(((Config *)cfg)->cfg), ((Config *)cfg)->mFileName)) - { + if (!config_write_file(&(((Config *)cfg)->cfg), ((Config *)cfg)->mFileName)) { LogError("Save config failed.\n"); fprintf(stderr, "Error while writing file.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); @@ -32,11 +35,9 @@ static const StatusCode ConfigSaveFileImpl(VConfig *cfg) } static void ConfigClose(VConfig *cfg) { - if (NULL != cfg) - { + if (NULL != cfg) { config_destroy(&(((Config *)cfg)->cfg)); - if (NULL != ((Config *)cfg)->mFileName) - { + if (NULL != ((Config *)cfg)->mFileName) { free(((Config *)cfg)->mFileName); ((Config *)cfg)->mFileName = NULL; } @@ -48,8 +49,7 @@ static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *va int result = 0; // config_setting_t *root; result = config_lookup_int(&(((Config *)cfg)->cfg), name, value); - if (CONFIG_FALSE == result) - { + if (CONFIG_FALSE == result) { return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); } return CreateStatusCode(STATUS_CODE_OK); @@ -59,31 +59,52 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i config_setting_t *root, *setting; root = config_root_setting(&(((Config *)cfg)->cfg)); setting = config_setting_get_member(root, name); - if(!setting) - { + if (!setting) { setting = config_setting_add(root, name, CONFIG_TYPE_INT); } config_setting_set_int(setting, value); return CreateStatusCode(STATUS_CODE_OK); } -static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char** value) +static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short *value) { + int intValue = 0; int result = 0; - // config_setting_t *root; - result = config_lookup_string(&(((Config *)cfg)->cfg), name, value); - if (CONFIG_FALSE == result) - { + result = config_lookup_int(&(((Config *)cfg)->cfg), name, &intValue); + if (CONFIG_FALSE == result && CHECK_SHORT_LIMIT(intValue)) { return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); } + *value = (short)intValue; return CreateStatusCode(STATUS_CODE_OK); } -static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, const char* value) + +static const StatusCode ConfigSetShortImpl(VConfig *cfg, const char *name, const short value) { config_setting_t *root, *setting; root = config_root_setting(&(((Config *)cfg)->cfg)); setting = config_setting_get_member(root, name); - if(!setting) - { + if (!setting) { + setting = config_setting_add(root, name, CONFIG_TYPE_INT); + } + int intValue = value; + config_setting_set_int(setting, intValue); + return CreateStatusCode(STATUS_CODE_OK); +} +static const StatusCode ConfigGetStringImpl(VConfig *cfg, const char *name, const char **value) +{ + int result = 0; + // config_setting_t *root; + result = config_lookup_string(&(((Config *)cfg)->cfg), name, value); + if (CONFIG_FALSE == result) { + return CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST); + } + return CreateStatusCode(STATUS_CODE_OK); +} +static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, const char *value) +{ + config_setting_t *root, *setting; + root = config_root_setting(&(((Config *)cfg)->cfg)); + setting = config_setting_get_member(root, name); + if (!setting) { setting = config_setting_add(root, name, CONFIG_TYPE_STRING); } config_setting_set_string(setting, value); @@ -91,8 +112,7 @@ static const StatusCode ConfigSetStringImpl(VConfig *cfg, const char *name, cons } static void ConfigImplInit(Config *cfg) { - if (NULL == cfg) - { + if (NULL == cfg) { LogError("NULL pointer.\n"); return; } @@ -100,6 +120,8 @@ static void ConfigImplInit(Config *cfg) cfg->close = ConfigClose; cfg->base.get_int = ConfigGetIntImpl; cfg->base.set_int = ConfigSetIntImpl; + cfg->base.get_short = ConfigGetShortImpl; + cfg->base.set_short = ConfigSetShortImpl; cfg->base.get_string = ConfigGetStringImpl; cfg->base.set_string = ConfigSetStringImpl; cfg->base.save = ConfigSaveFileImpl; @@ -110,27 +132,25 @@ Config *NewConfig(const char *fileName) Config *cfg = (Config *)malloc(sizeof(Config)); ConfigImplInit(cfg); config_init(&(cfg->cfg)); - config_set_options(&(cfg->cfg), (CONFIG_OPTION_FSYNC | - CONFIG_OPTION_SEMICOLON_SEPARATORS | - CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | - CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE)); + config_set_options(&(cfg->cfg), + (CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS | + CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE)); #define FIEL_EXIST 0 - if (FIEL_EXIST == access(fileName, F_OK)) - { - if (!config_read_file(&(cfg->cfg), fileName)) - { + if (FIEL_EXIST == access(fileName, F_OK)) { + if (!config_read_file(&(cfg->cfg), fileName)) { LogError("Read file failed[%s].\n", fileName); - fprintf(stderr, "%s:%d - %s\n", config_error_file(&(cfg->cfg)), - config_error_line(&(cfg->cfg)), config_error_text(&(cfg->cfg))); + fprintf(stderr, + "%s:%d - %s\n", + config_error_file(&(cfg->cfg)), + config_error_line(&(cfg->cfg)), + config_error_text(&(cfg->cfg))); return NULL; } } - else - { + else { LogInfo("Config file doesn't exist.\n"); /* Write out the new configuration. */ - if (!config_write_file(&(cfg->cfg), fileName)) - { + if (!config_write_file(&(cfg->cfg), fileName)) { fprintf(stderr, "Error while writing file.\n"); return NULL; } @@ -138,8 +158,7 @@ Config *NewConfig(const char *fileName) unsigned int fileLength = strlen(fileName) + 1; cfg->mFileName = (char *)malloc(fileLength); memset(cfg->mFileName, 0, fileLength); - if (NULL != cfg->mFileName) - { + if (NULL != cfg->mFileName) { memcpy(cfg->mFileName, fileName, fileLength - 1); } return cfg; diff --git a/utils/Config/src/ConfigImpl.h b/utils/Config/src/ConfigImpl.h index c18ded8..2e3bbad 100644 --- a/utils/Config/src/ConfigImpl.h +++ b/utils/Config/src/ConfigImpl.h @@ -17,21 +17,20 @@ #include "Config.h" #include #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif #ifndef CONFIG_OWNER -#error This is internal file, never include it. + #error This is internal file, never include it. #endif - typedef struct config Config; - typedef struct config - { - VConfig base; - void (*close)(VConfig *); - config_t cfg; - char *mFileName; - } Config; - Config *NewConfig(const char *fileName); +typedef struct config Config; +typedef struct config +{ + VConfig base; + void (*close)(VConfig *); + config_t cfg; + char *mFileName; +} Config; +Config *NewConfig(const char *fileName); #ifdef __cplusplus } #endif