Merge branch 'master-develop' of gitee.com:shenzhen-jiuyilian/ipc into master-develop

This commit is contained in:
fancy 2023-12-12 08:34:40 -08:00
commit 9f07183680
18 changed files with 591 additions and 175 deletions

View File

@ -15,6 +15,7 @@
#ifndef IHALCPP_H #ifndef IHALCPP_H
#define IHALCPP_H #define IHALCPP_H
#include "StatusCode.h" #include "StatusCode.h"
#include <iostream>
#include <memory> #include <memory>
constexpr int INVALID_PERIOD = -1; constexpr int INVALID_PERIOD = -1;
constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100; constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100;

View File

@ -47,4 +47,19 @@ add_custom_command(
COMMAND make IpcConfig_code_check COMMAND make IpcConfig_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ 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() endif()

View File

@ -15,22 +15,47 @@
#ifndef IIPCCONFIG_H #ifndef IIPCCONFIG_H
#define IIPCCONFIG_H #define IIPCCONFIG_H
#include "StatusCode.h" #include "StatusCode.h"
#include <iostream>
#include <memory> #include <memory>
#include <string_view>
enum class IpcConfigKey enum class IpcConfigKey
{ {
TEST_NUM = 0, TEST_NUM = 0,
TEST_SHORT,
TEST_STRING,
END END
}; };
class IIpcConfig class IIpcConfig
{ {
public: public:
IIpcConfig() = default; IIpcConfig() = default;
virtual ~IIpcConfig() = default; virtual ~IIpcConfig() = default;
virtual const StatusCode ConfigFileSave(void) { return CreateStatusCode(STATUS_CODE_OK); }
static std::shared_ptr<IIpcConfig> &GetInstance(std::shared_ptr<IIpcConfig> *impl = nullptr); static std::shared_ptr<IIpcConfig> &GetInstance(std::shared_ptr<IIpcConfig> *impl = nullptr);
virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); }
virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
virtual const int GetInt(const IpcConfigKey &key) { return -1; } virtual const int GetInt(const IpcConfigKey &key) { return -1; }
virtual void SetInt(const IpcConfigKey &key, const int &value) {} 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 GetString(const IpcConfigKey &key) { return "undefine"; }
virtual void SetString(const IpcConfigKey &key, const std::string string) {}
}; };
bool CreateIpcConfig(void); bool CreateIpcConfig(void);
#endif #endif

View File

@ -17,15 +17,12 @@
std::shared_ptr<IIpcConfig> &IIpcConfig::GetInstance(std::shared_ptr<IIpcConfig> *impl) std::shared_ptr<IIpcConfig> &IIpcConfig::GetInstance(std::shared_ptr<IIpcConfig> *impl)
{ {
static auto instance = std::make_shared<IIpcConfig>(); static auto instance = std::make_shared<IIpcConfig>();
if (impl) if (impl) {
{ if (instance.use_count() == 1) {
if (instance.use_count() == 1)
{
LogInfo("Instance changed succeed.\n"); LogInfo("Instance changed succeed.\n");
instance = *impl; instance = *impl;
} }
else else {
{
LogError("Can't changing the instance becase of using by some one.\n"); LogError("Can't changing the instance becase of using by some one.\n");
} }
} }

View File

@ -15,22 +15,30 @@
#include "IpcConfig.h" #include "IpcConfig.h"
#include "ILog.h" #include "ILog.h"
#include <string.h> #include <string.h>
#define CHECK_MAP(map) (map.size() == 1 ? true : false)
IpcConfig::IpcConfig() IpcConfig::IpcConfig()
{ {
mCfgChanged = CONFIG_HAS_NOT_CHANGED; mCfgChanged = CONFIG_HAS_NOT_CHANGED;
mCfgMapInt.insert(
std::make_pair< std::map<std::string, std::reference_wrapper<int>> innerMapInt;
IpcConfigKey, innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper<int>(mAllData.testNum)));
std::reference_wrapper<int>>( mCfgMapInt.insert(std::make_pair(IpcConfigKey::TEST_NUM, innerMapInt));
IpcConfigKey::TEST_NUM,
std::reference_wrapper<int>(mAllData.testNum))); std::map<std::string, std::reference_wrapper<short>> innerMapShort;
innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper<short>(mAllData.testShort)));
mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapString;
innerMapString.insert(std::make_pair("test_string", std::reference_wrapper<CHAR_STRING>(mAllData.testString)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::TEST_STRING, innerMapString));
} }
const StatusCode IpcConfig::Init(void) const StatusCode IpcConfig::Init(void)
{ {
memset(&mAllData, 0, sizeof(Config_s)); memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH); mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg) if (nullptr == mCfg) {
{
LogError("Open config file failed.\n"); LogError("Open config file failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
@ -39,21 +47,21 @@ const StatusCode IpcConfig::Init(void)
} }
const StatusCode IpcConfig::UnInit(void) const StatusCode IpcConfig::UnInit(void)
{ {
if (CONFIG_HAS_CHANGED == mCfgChanged) if (CONFIG_HAS_CHANGED == mCfgChanged) {
{
LogInfo("Save config files.\n"); LogInfo("Save config files.\n");
ConfigSaveFile(mCfg); ConfigSaveFile(mCfg);
} }
CloseConfigFile(mCfg); CloseConfigFile(mCfg);
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
const StatusCode IpcConfig::ConfigFileSave(void) { return ConfigSaveFile(mCfg); }
const int IpcConfig::GetInt(const IpcConfigKey &key) const int IpcConfig::GetInt(const IpcConfigKey &key)
{ {
std::map<IpcConfigKey, std::reference_wrapper<int>>::iterator iter; std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key); iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.end()) if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
{ return iter->second.begin()->second;
return iter->second;
} }
LogError("Can't find the key.\n"); LogError("Can't find the key.\n");
constexpr int UNKNOWN_CONFIG = -1; constexpr int UNKNOWN_CONFIG = -1;
@ -61,31 +69,265 @@ const int IpcConfig::GetInt(const IpcConfigKey &key)
} }
void IpcConfig::SetInt(const IpcConfigKey &key, const int &value) void IpcConfig::SetInt(const IpcConfigKey &key, const int &value)
{ {
std::map<IpcConfigKey, std::reference_wrapper<int>>::iterator iter; std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key); iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.end()) if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
{ iter->second.begin()->second.get() = value;
iter->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; mCfgChanged = CONFIG_HAS_CHANGED;
} }
else else {
{
LogError("Can't find the key.\n"); LogError("Can't find the key.\n");
} }
} }
const short IpcConfig::GetShort(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
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;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
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 {
LogError("Can't find the key.\n");
}
}
const long IpcConfig::GetLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<long>>::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<IpcConfigKey, std::reference_wrapper<long>>::iterator iter;
iter = mCfgMapLong.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<IpcConfigKey, std::reference_wrapper<long long>>::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<IpcConfigKey, std::reference_wrapper<long long>>::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<IpcConfigKey, std::reference_wrapper<char>>::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<IpcConfigKey, std::reference_wrapper<char>>::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<IpcConfigKey, std::reference_wrapper<float>>::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<IpcConfigKey, std::reference_wrapper<float>>::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<IpcConfigKey, std::reference_wrapper<double>>::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<IpcConfigKey, std::reference_wrapper<double>>::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<IpcConfigKey, std::reference_wrapper<long double>>::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<IpcConfigKey, std::reference_wrapper<long double>>::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<IpcConfigKey, std::reference_wrapper<bool>>::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<IpcConfigKey, std::reference_wrapper<bool>>::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 IpcConfig::GetString(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::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
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<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::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 *
ConfigSetString(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
void IpcConfig::ReadAllConfigParameters(void) void IpcConfig::ReadAllConfigParameters(void)
{ {
StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum)); 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"); LogWarning("test_num doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED; mCfgChanged = CONFIG_HAS_CHANGED;
constexpr int DEFAULT_TEST_NUM = 10; constexpr int DEFAULT_TEST_NUM = 10;
mAllData.testNum = DEFAULT_TEST_NUM; mAllData.testNum = DEFAULT_TEST_NUM;
ConfigSetInt(mCfg, "test_num", mAllData.testNum); ConfigSetInt(mCfg, "test_num", mAllData.testNum);
} }
if (CONFIG_HAS_CHANGED == mCfgChanged)
{ 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 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";
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"); LogInfo("Save the config file.\n");
mCfgChanged = CONFIG_HAS_NOT_CHANGED; mCfgChanged = CONFIG_HAS_NOT_CHANGED;
ConfigSaveFile(mCfg); ConfigSaveFile(mCfg);

View File

@ -14,16 +14,20 @@
*/ */
#ifndef IPCCONFIG_H #ifndef IPCCONFIG_H
#define IPCCONFIG_H #define IPCCONFIG_H
#include "StatusCode.h"
#include "IIpcConfig.h"
#include "Config.h" #include "Config.h"
#include <memory> #include "IIpcConfig.h"
#include "StatusCode.h"
#include <map> #include <map>
#include <memory>
constexpr bool CONFIG_HAS_CHANGED = true; constexpr bool CONFIG_HAS_CHANGED = true;
constexpr bool CONFIG_HAS_NOT_CHANGED = false; constexpr bool CONFIG_HAS_NOT_CHANGED = false;
typedef char CHAR_STRING[256];
typedef struct Config_s typedef struct Config_s
{ {
int testNum; int testNum;
short testShort;
CHAR_STRING testString;
} Config_s; } Config_s;
class MapInt class MapInt
{ {
@ -38,8 +42,27 @@ public:
virtual ~IpcConfig() = default; virtual ~IpcConfig() = default;
const StatusCode Init(void) override; const StatusCode Init(void) override;
const StatusCode UnInit(void) override; const StatusCode UnInit(void) override;
const StatusCode ConfigFileSave(void) override;
const int GetInt(const IpcConfigKey &key) override; const int GetInt(const IpcConfigKey &key) override;
void SetInt(const IpcConfigKey &key, const int &value) 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 GetString(const IpcConfigKey &key) override;
void SetString(const IpcConfigKey &key, const std::string string) override;
private: private:
void ReadAllConfigParameters(void); void ReadAllConfigParameters(void);
@ -48,8 +71,15 @@ private:
bool mCfgChanged; bool mCfgChanged;
VConfig *mCfg; VConfig *mCfg;
Config_s mAllData; Config_s mAllData;
std::map<IpcConfigKey, std::reference_wrapper<int>> mCfgMapInt; std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
std::map<IpcConfigKey, std::reference_wrapper<long int>> mCfgMapUInt; std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
std::map<IpcConfigKey, std::reference_wrapper<std::string>> mCfgMapString; std::map<IpcConfigKey, std::reference_wrapper<long>> mCfgMapLong;
std::map<IpcConfigKey, std::reference_wrapper<long long>> mCfgMapLongLong;
std::map<IpcConfigKey, std::reference_wrapper<char>> mCfgMapChar;
std::map<IpcConfigKey, std::reference_wrapper<float>> mCfgMapFloat;
std::map<IpcConfigKey, std::reference_wrapper<double>> mCfgMapDouble;
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
std::map<IpcConfigKey, std::reference_wrapper<bool>> mCfgMapBool;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>> mCfgMapString;
}; };
#endif #endif

View File

@ -19,8 +19,7 @@ bool CreateIpcConfig(void)
{ {
auto instance = std::make_shared<IIpcConfig>(); auto instance = std::make_shared<IIpcConfig>();
StatusCode code = IpcConfigMakePtr::GetInstance()->CreateIpcConfig(instance); StatusCode code = IpcConfigMakePtr::GetInstance()->CreateIpcConfig(instance);
if (IsCodeOK(code)) if (IsCodeOK(code)) {
{
LogInfo("CreateIpcConfig is ok.\n"); LogInfo("CreateIpcConfig is ok.\n");
IIpcConfig::GetInstance(&instance); IIpcConfig::GetInstance(&instance);
return true; return true;
@ -30,8 +29,7 @@ bool CreateIpcConfig(void)
std::shared_ptr<IpcConfigMakePtr> &IpcConfigMakePtr::GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl) std::shared_ptr<IpcConfigMakePtr> &IpcConfigMakePtr::GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl)
{ {
static auto instance = std::make_shared<IpcConfigMakePtr>(); static auto instance = std::make_shared<IpcConfigMakePtr>();
if (impl) if (impl) {
{
instance = *impl; instance = *impl;
} }
return instance; return instance;

View File

@ -53,5 +53,21 @@ add_custom_command(
COMMAND make IpcConfigTest_code_check COMMAND make IpcConfigTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ 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() endif()
define_file_name(${TARGET_NAME}) define_file_name(${TARGET_NAME})

View File

@ -1,5 +1,5 @@
#include "ILog.h"
#include "IIpcConfig.h" #include "IIpcConfig.h"
#include "ILog.h"
// #include <gmock/gmock.h> // #include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace IpcConfigTest namespace IpcConfigTest
@ -13,6 +13,20 @@ namespace IpcConfigTest
IIpcConfig::GetInstance()->Init(); IIpcConfig::GetInstance()->Init();
int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM); int testNum = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::TEST_NUM);
LogInfo("Get testNum = %d\n", testNum); LogInfo("Get testNum = %d\n", testNum);
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());
const std::string string = "define";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::TEST_STRING, string);
IIpcConfig::GetInstance()->ConfigFileSave();
IIpcConfig::GetInstance()->UnInit(); IIpcConfig::GetInstance()->UnInit();
ILogUnInit(); ILogUnInit();
DestroyLogModule(); DestroyLogModule();

View File

@ -6,4 +6,4 @@ add_subdirectory(Log)
add_subdirectory(SharedData) add_subdirectory(SharedData)
add_subdirectory(UartDevice) add_subdirectory(UartDevice)
add_subdirectory(LinuxApi) add_subdirectory(LinuxApi)
add_subdirectory(MultiProcess) # add_subdirectory(MultiProcess)

View File

@ -41,6 +41,21 @@ add_custom_command(
COMMAND make Config_code_check COMMAND make Config_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ 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() endif()
# build libconfig before make libConfig.a # build libconfig before make libConfig.a

View File

@ -16,8 +16,7 @@
#define CONFIG_H #define CONFIG_H
#include "StatusCode.h" #include "StatusCode.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
enum CONFIG_CODE enum CONFIG_CODE
{ {
@ -29,6 +28,10 @@ extern "C"
{ {
const StatusCode (*get_int)(VConfig *, const char *, int *); const StatusCode (*get_int)(VConfig *, const char *, int *);
const StatusCode (*set_int)(VConfig *, const char *, const 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 *); const StatusCode (*save)(VConfig *);
} VConfig; } VConfig;
const StatusCode ConfigInit(void); const StatusCode ConfigInit(void);
@ -38,6 +41,10 @@ extern "C"
void CloseConfigFile(VConfig *cfg); void CloseConfigFile(VConfig *cfg);
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value); const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value);
const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -16,30 +16,19 @@
#include "ConfigImpl.h" #include "ConfigImpl.h"
#include "ILog.h" #include "ILog.h"
#include <stddef.h> #include <stddef.h>
const StatusCode ConfigInit(void) const StatusCode ConfigInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
{ const StatusCode ConfigUnInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
return CreateStatusCode(STATUS_CODE_OK); VConfig *OpenConfigFile(const char *fileName) { return (VConfig *)NewConfig(fileName); }
}
const StatusCode ConfigUnInit(void)
{
return CreateStatusCode(STATUS_CODE_OK);
}
VConfig *OpenConfigFile(const char *fileName)
{
return (VConfig *)NewConfig(fileName);
}
const StatusCode ConfigSaveFile(VConfig *cfg) const StatusCode ConfigSaveFile(VConfig *cfg)
{ {
if (NULL == cfg) if (NULL == cfg) {
{
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
return cfg->save(cfg); return cfg->save(cfg);
} }
void CloseConfigFile(VConfig *cfg) void CloseConfigFile(VConfig *cfg)
{ {
if (NULL == cfg) if (NULL == cfg) {
{
LogError("NULL config poniter.\n"); LogError("NULL config poniter.\n");
return; return;
} }
@ -47,17 +36,45 @@ void CloseConfigFile(VConfig *cfg)
} }
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value) const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value)
{ {
if (NULL == cfg) if (NULL == cfg) {
{
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
return cfg->get_int(cfg, name, value); return cfg->get_int(cfg, name, value);
} }
const StatusCode ConfigSetInt(VConfig *cfg, const char *name, const int 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 CreateStatusCode(STATUS_CODE_NOT_OK);
} }
return cfg->set_int(cfg, name, value); return cfg->set_int(cfg, name, value);
} }
const StatusCode ConfigGetShort(VConfig *cfg, const char *name, short *value)
{
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) {
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return cfg->set_string(cfg, name, value);
}

View File

@ -15,14 +15,12 @@
#include "ConfigCode.h" #include "ConfigCode.h"
#include "ILog.h" #include "ILog.h"
#include <string.h> #include <string.h>
static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = { static const char *ConfigCodeString[CONFIG_CODE_END - STATUS_CODE_END + 1] = {"CONFIG_CODE_PARAM_NOT_EXIST",
"CONFIG_CODE_PARAM_NOT_EXIST",
"CONFIG_CODE_END"}; "CONFIG_CODE_END"};
static const char *PrintStringConfigCode(const StatusCode this) static const char *PrintStringConfigCode(const StatusCode this)
{ {
const int CODE_INDEX = this.mStatusCode - STATUS_CODE_END; 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]); LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]);
return 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) 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 true;
} }
return false; return false;
} }
static StatusCode NewConfigCode(const long int code) static StatusCode NewConfigCode(const long int code)
{ {
StatusCode result = { StatusCode result = {PrintStringConfigCode, CodeEqual, code};
PrintStringConfigCode,
CodeEqual,
code};
return result; return result;
} }
const StatusCode CreateConfigCode(const long int code) const StatusCode CreateConfigCode(const long int code)
@ -51,8 +45,7 @@ const StatusCode CreateConfigCode(const long int code)
// { // {
// return CreateStatusCode(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); return NewConfigCode(code);
} }
LogError("undefined code.\n"); LogError("undefined code.\n");

View File

@ -16,8 +16,7 @@
#define CONFIGCODE_H #define CONFIGCODE_H
#include "Config.h" #include "Config.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
#ifndef CONFIG_OWNER #ifndef CONFIG_OWNER
#error This is internal file, never include it. #error This is internal file, never include it.

View File

@ -13,17 +13,20 @@
* limitations under the License. * limitations under the License.
*/ */
#include "ConfigImpl.h" #include "ConfigImpl.h"
#include "ILog.h"
#include "ConfigCode.h" #include "ConfigCode.h"
#include "ILog.h"
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#define CHECK_SHORT_LIMIT(value) (value > SHRT_MAX ? false : (value < SHRT_MIN ? false : true))
static const StatusCode ConfigSaveFileImpl(VConfig *cfg) static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
{ {
/* Write out the new configuration. */ /* Write out the new configuration. */
LogInfo("Save file[%s].\n", ((Config *)cfg)->mFileName); 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"); LogError("Save config failed.\n");
fprintf(stderr, "Error while writing file.\n"); fprintf(stderr, "Error while writing file.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
@ -32,11 +35,9 @@ static const StatusCode ConfigSaveFileImpl(VConfig *cfg)
} }
static void ConfigClose(VConfig *cfg) static void ConfigClose(VConfig *cfg)
{ {
if (NULL != cfg) if (NULL != cfg) {
{
config_destroy(&(((Config *)cfg)->cfg)); config_destroy(&(((Config *)cfg)->cfg));
if (NULL != ((Config *)cfg)->mFileName) if (NULL != ((Config *)cfg)->mFileName) {
{
free(((Config *)cfg)->mFileName); free(((Config *)cfg)->mFileName);
((Config *)cfg)->mFileName = NULL; ((Config *)cfg)->mFileName = NULL;
} }
@ -48,8 +49,7 @@ static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *va
int result = 0; int result = 0;
// config_setting_t *root; // config_setting_t *root;
result = config_lookup_int(&(((Config *)cfg)->cfg), name, value); 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 CreateConfigCode(CONFIG_CODE_PARAM_NOT_EXIST);
} }
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
@ -58,14 +58,61 @@ static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const i
{ {
config_setting_t *root, *setting; config_setting_t *root, *setting;
root = config_root_setting(&(((Config *)cfg)->cfg)); root = config_root_setting(&(((Config *)cfg)->cfg));
setting = config_setting_get_member(root, name);
if (!setting) {
setting = config_setting_add(root, name, CONFIG_TYPE_INT); setting = config_setting_add(root, name, CONFIG_TYPE_INT);
}
config_setting_set_int(setting, value); config_setting_set_int(setting, value);
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
static const StatusCode ConfigGetShortImpl(VConfig *cfg, const char *name, short *value)
{
int intValue = 0;
int result = 0;
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 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) {
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);
return CreateStatusCode(STATUS_CODE_OK);
}
static void ConfigImplInit(Config *cfg) static void ConfigImplInit(Config *cfg)
{ {
if (NULL == cfg) if (NULL == cfg) {
{
LogError("NULL pointer.\n"); LogError("NULL pointer.\n");
return; return;
} }
@ -73,6 +120,10 @@ static void ConfigImplInit(Config *cfg)
cfg->close = ConfigClose; cfg->close = ConfigClose;
cfg->base.get_int = ConfigGetIntImpl; cfg->base.get_int = ConfigGetIntImpl;
cfg->base.set_int = ConfigSetIntImpl; 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; cfg->base.save = ConfigSaveFileImpl;
} }
Config *NewConfig(const char *fileName) Config *NewConfig(const char *fileName)
@ -81,27 +132,25 @@ Config *NewConfig(const char *fileName)
Config *cfg = (Config *)malloc(sizeof(Config)); Config *cfg = (Config *)malloc(sizeof(Config));
ConfigImplInit(cfg); ConfigImplInit(cfg);
config_init(&(cfg->cfg)); config_init(&(cfg->cfg));
config_set_options(&(cfg->cfg), (CONFIG_OPTION_FSYNC | config_set_options(&(cfg->cfg),
CONFIG_OPTION_SEMICOLON_SEPARATORS | (CONFIG_OPTION_FSYNC | CONFIG_OPTION_SEMICOLON_SEPARATORS |
CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
#define FIEL_EXIST 0 #define FIEL_EXIST 0
if (FIEL_EXIST == access(fileName, F_OK)) if (FIEL_EXIST == access(fileName, F_OK)) {
{ if (!config_read_file(&(cfg->cfg), fileName)) {
if (!config_read_file(&(cfg->cfg), fileName))
{
LogError("Read file failed[%s].\n", fileName); LogError("Read file failed[%s].\n", fileName);
fprintf(stderr, "%s:%d - %s\n", config_error_file(&(cfg->cfg)), fprintf(stderr,
config_error_line(&(cfg->cfg)), config_error_text(&(cfg->cfg))); "%s:%d - %s\n",
config_error_file(&(cfg->cfg)),
config_error_line(&(cfg->cfg)),
config_error_text(&(cfg->cfg)));
return NULL; return NULL;
} }
} }
else else {
{
LogInfo("Config file doesn't exist.\n"); LogInfo("Config file doesn't exist.\n");
/* Write out the new configuration. */ /* 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"); fprintf(stderr, "Error while writing file.\n");
return NULL; return NULL;
} }
@ -109,8 +158,7 @@ Config *NewConfig(const char *fileName)
unsigned int fileLength = strlen(fileName) + 1; unsigned int fileLength = strlen(fileName) + 1;
cfg->mFileName = (char *)malloc(fileLength); cfg->mFileName = (char *)malloc(fileLength);
memset(cfg->mFileName, 0, fileLength); memset(cfg->mFileName, 0, fileLength);
if (NULL != cfg->mFileName) if (NULL != cfg->mFileName) {
{
memcpy(cfg->mFileName, fileName, fileLength - 1); memcpy(cfg->mFileName, fileName, fileLength - 1);
} }
return cfg; return cfg;

View File

@ -17,8 +17,7 @@
#include "Config.h" #include "Config.h"
#include <libconfig.h> #include <libconfig.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
#ifndef CONFIG_OWNER #ifndef CONFIG_OWNER
#error This is internal file, never include it. #error This is internal file, never include it.