Add ipc config module.

This commit is contained in:
fancy 2023-09-13 08:30:23 -07:00
parent 01e9deaa77
commit 523a6b8b99
25 changed files with 515 additions and 18 deletions

View File

@ -40,4 +40,7 @@ set(CURL_OPENSSL_LIB_SHARED_ENABLE "false")
# ------------ build clang-tidy ------------ start
set(CLANG_TIDY_SUPPORT "true")
set(LLVM_PATH "/home/xiaojiazhu/project/tmp/llvm-project")
# ------------ build clang-tidy ------------ end
# ------------ build clang-tidy ------------ end
# ------------ build IpcConfig ------------ start
set(IPC_CONFIG_FILE_PATH "./ipc_config")
# ------------ build IpcConfig ------------ end

View File

@ -4,11 +4,9 @@ platform=$1
CROSS_COMPILER_PATH=$2
CROSS_COMPILER=$3
echo "Compile libconfig, platform = $platform."
# echo "CROSS_COMPILER_PATH = $CROSS_COMPILER_PATH"
echo "CROSS_COMPILER = $CROSS_COMPILER"
echo "Start to compile libconfig."
export ROOT_PATH=$PWD
# export PATH=$CROSS_COMPILER_PATH:$PATH
if [ ! -d "./libconfig-1.7.3" ];then
echo "tar zxvf libconfig-1.7.3.tar.gz"
tar zxvf libconfig-1.7.3.tar.gz
@ -16,8 +14,6 @@ fi
if [ ! -f "./libconfig-1.7.3/lib/.libs/libconfig++.a" ] || [ ! -f "./libconfig-1.7.3/lib/.libs/libconfig.a" ];then
echo "make libconfig++.a"
cd ./libconfig-1.7.3
# ./configure
# make
case $platform in
"linux")
echo "==Compile linux."

View File

@ -16,8 +16,8 @@ static StatusCode IHalUnInit(IHal *object)
}
static IHal default_hal = {
.init = IHalInit,
.free = IHalFree,
.un_init = IHalUnInit,
.free = IHalFree,
};
static IHal *hal_instance = &default_hal;
IHal *GetHalIntance(void)

View File

@ -9,8 +9,8 @@ extern "C"
typedef struct i_hal
{
StatusCode (*init)(IHal *);
void (*free)(IHal *);
StatusCode (*un_init)(IHal *);
void (*free)(IHal *);
} IHal;
IHal *GetHalIntance(void);
StatusCode NewIHal(IHal **object);

View File

@ -1,2 +1,2 @@
add_subdirectory(StateMachine)
add_subdirectory(StateMachine)
add_subdirectory(IpcConfig)

View File

@ -0,0 +1,50 @@
include(${CMAKE_SOURCE_DIR}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/Config/include
)
#do not rely on any other library
#link_directories(
#)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if (DEFINED IPC_CONFIG_FILE_PATH)
add_definitions(-DIPC_CONFIG_FILE_PATH=\"${IPC_CONFIG_FILE_PATH}\")
else()
message(FATAL_ERROR "You set define IPC_CONFIG_FILE_PATH in toolchan .cmake file to tell code where to save config file.")
endif()
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME IpcConfig)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} ReturnCode Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
IpcConfig_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/IpcConfig
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make IpcConfig_code_check
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
)
endif()

View File

@ -0,0 +1,21 @@
#ifndef IIPCCONFIG_H
#define IIPCCONFIG_H
#include "StatusCode.h"
#include <memory>
enum class IpcConfigKey
{
TEST_NUM = 0,
END
};
class IIpcConfig
{
public:
IIpcConfig() = default;
virtual ~IIpcConfig() = default;
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 UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); }
virtual const int GetInt(const IpcConfigKey &key) { return -1; }
};
bool CreateIpcConfig(void);
#endif

View File

@ -0,0 +1,20 @@
#ifndef IPCCONFIG_H
#define IPCCONFIG_H
#include "StatusCode.h"
#include <memory>
enum class IpcConfigKey
{
TEST_NUM = 0,
END
};
class IpcConfig
{
public:
IpcConfig() = default;
virtual ~IpcConfig() = default;
static std::shared_ptr<IpcConfig> &GetInstance(std::shared_ptr<IpcConfig> *impl = nullptr);
const StatusCode Init(void);
const StatusCode UnInit(void);
const int GetInt(const IpcConfigKey &key);
};
#endif

View File

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

View File

@ -0,0 +1,28 @@
#include "IpcConfig.h"
#include "ILog.h"
#include <string.h>
const StatusCode IpcConfig::Init(void)
{
memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
ReadAllConfigParameters();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfig::UnInit(void)
{
return CreateStatusCode(STATUS_CODE_OK);
}
const int IpcConfig::GetInt(const IpcConfigKey &key)
{
return 0;
}
void IpcConfig::ReadAllConfigParameters(void)
{
StatusCode code = ConfigGetInt(mCfg, "test_num", &(mAllData.testNum));
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST"))
{
constexpr int DEFAULT_TEST_NUM = 0;
mAllData.testNum = DEFAULT_TEST_NUM;
ConfigSetInt(mCfg, "test_num", mAllData.testNum);
}
}

View File

@ -0,0 +1,27 @@
#ifndef IPCCONFIG_H
#define IPCCONFIG_H
#include "StatusCode.h"
#include "IIpcConfig.h"
#include "Config.h"
#include <memory>
typedef struct Config_s
{
int testNum;
} Config_s;
class IpcConfig : public IIpcConfig
{
public:
IpcConfig() = default;
virtual ~IpcConfig() = default;
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
const int GetInt(const IpcConfigKey &key) override;
private:
void ReadAllConfigParameters(void);
private:
VConfig *mCfg;
Config_s mAllData;
};
#endif

View File

@ -0,0 +1,30 @@
#include "IpcConfigMakePtr.h"
#include "ILog.h"
#include "IpcConfig.h"
bool CreateIpcConfig(void)
{
auto instance = std::make_shared<IIpcConfig>();
StatusCode code = IpcConfigMakePtr::GetInstance()->CreateIpcConfig(instance);
if (IsCodeOK(code))
{
LogInfo("CreateIpcConfig is ok.\n");
IIpcConfig::GetInstance(&instance);
return true;
}
return false;
}
std::shared_ptr<IpcConfigMakePtr> &IpcConfigMakePtr::GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl)
{
static auto instance = std::make_shared<IpcConfigMakePtr>();
if (impl)
{
instance = *impl;
}
return instance;
}
const StatusCode IpcConfigMakePtr::CreateIpcConfig(std::shared_ptr<IIpcConfig> &impl)
{
auto tmp = std::make_shared<IpcConfig>();
impl = tmp;
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -0,0 +1,14 @@
#ifndef IPC_CONFIG_MAKE_PTR_H
#define IPC_CONFIG_MAKE_PTR_H
#include "IIpcConfig.h"
#include "StatusCode.h"
#include <memory>
class IpcConfigMakePtr
{
public:
IpcConfigMakePtr() = default;
virtual ~IpcConfigMakePtr() = default;
static std::shared_ptr<IpcConfigMakePtr> &GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl = nullptr);
virtual const StatusCode CreateIpcConfig(std::shared_ptr<IIpcConfig> &impl);
};
#endif // !IPC_CONFIG_MAKE_PTR_H

View File

@ -0,0 +1,14 @@
#ifndef IPC_CONFIG_MAKE_PTR_H
#define IPC_CONFIG_MAKE_PTR_H
#include "IIpcConfig.h"
#include "StatusCode.h"
#include <memory>
class IpcConfigMakePtr
{
public:
IpcConfigMakePtr() = default;
virtual ~IpcConfigMakePtr() = default;
static std::shared_ptr<IpcConfigMakePtr> &GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl = nullptr);
virtual const StatusCode CreateIpcConfig(std::shared_ptr<IIpcConfig> &impl);
};
#endif // !IPC_CONFIG_MAKE_PTR_H

View File

@ -1,7 +1,5 @@
# cmake_minimum_required(VERSION 2.8.0)
#Compile gtest for test code.
# add_subdirectory(ReturnCode)
add_subdirectory(Config)
add_subdirectory(Log)

View File

@ -0,0 +1,58 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Config/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
${LIBS_OUTPUT_PATH}
${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME ConfigTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Config gtest gmock pthread)
if(${COVERAGE_ON} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
ConfigTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/Config
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make ConfigTest_code_check
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
)
endif()

View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,18 @@
#include "ILog.h"
#include "Config.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace ConfigTest
{
// ../output_files/test/bin/LogTest --gtest_filter=ConfigTest.Demo
TEST(ConfigTest, Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
VConfig *config = OpenConfigFile("./config");
int value = 0;
ConfigGetInt(config, "number", &value);
ILogUnInit();
DestroyLogModule();
}
} // namespace ConfigTest

View File

@ -8,10 +8,11 @@ include_directories(
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib
)
#do not rely on any other library
#link_directories(
#)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
@ -20,7 +21,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME Config)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Log)
target_link_libraries(${TARGET_NAME} StatusCode Log config)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(

View File

@ -5,8 +5,23 @@
extern "C"
{
#endif
const StatusCode ConfigInit(void);
const StatusCode ConfigUnInit(void);
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);
} VConfig;
const StatusCode ConfigInit(void);
const StatusCode ConfigUnInit(void);
VConfig *OpenConfigFile(const char *fileName);
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);
#ifdef __cplusplus
}
#endif

View File

@ -1,7 +1,35 @@
#include "Config.h"
#include "ConfigImpl.h"
#include <stddef.h>
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);
}
void CloseConfigFile(VConfig *cfg)
{
((Config *)cfg)->close(cfg);
}
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value)
{
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)
{
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return cfg->set_int(cfg, name, value);
}

View File

@ -0,0 +1,46 @@
#include "ConfigCode.h"
#include "ILog.h"
#include <string.h>
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)
{
LogInfo("Config code = [ %s ]\n", ConfigCodeString[CODE_INDEX]);
return ConfigCodeString[CODE_INDEX];
}
LogError("Config code undefine.\n");
return "Config code undefine.\n";
}
static const bool CodeEqual(const StatusCode code, const char *value)
{
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};
return result;
}
const StatusCode CreateConfigCode(const long int code)
{
if (STATUS_CODE_OK <= code && code < STATUS_CODE_END)
{
return CreateStatusCode(code);
}
if (STATUS_CODE_END <= code && code < CONFIG_CODE_END)
{
return NewConfigCode(code);
}
LogError("undefined code.\n");
return CreateStatusCode(STATUS_CODE_END);
}

View File

@ -0,0 +1,12 @@
#ifndef CONFIGCODE_H
#define CONFIGCODE_H
#include "Config.h"
#ifdef __cplusplus
extern "C"
{
#endif
const StatusCode CreateConfigCode(const long int code);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,70 @@
#include "ConfigImpl.h"
#include "ILog.h"
#include <stdlib.h>
#include <unistd.h>
static void ConfigClose(VConfig *cfg)
{
if (NULL != cfg)
{
config_destroy(&(((Config *)cfg)->cfg));
free(cfg);
}
}
static const StatusCode ConfigGetIntImpl(VConfig *cfg, const char *name, int *value)
{
int result = 0;
config_setting_t *root, *setting, *movie;
root = config_root_setting(&(((Config *)cfg)->cfg));
setting = config_setting_get_member(root, name);
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode ConfigSetIntImpl(VConfig *cfg, const char *name, const int value)
{
// int result = 0;
// config_setting_t *root, *setting, *movie;
// root = config_root_setting(&(((Config*)cfg)->cfg));
// setting = config_setting_get_member(root, name);
return CreateStatusCode(STATUS_CODE_OK);
}
static void ConfigImplInit(Config *cfg)
{
if (NULL == cfg)
{
LogError("NULL pointer.\n");
return;
}
cfg->close = ConfigClose;
cfg->base.get_int = ConfigGetIntImpl;
cfg->base.set_int = ConfigSetIntImpl;
}
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));
if (access(fileName, F_OK))
{
if (!config_read_file(&(cfg->cfg), fileName))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&(cfg->cfg)),
config_error_line(&(cfg->cfg)), config_error_text(&(cfg->cfg)));
// config_destroy(&(cfg->cfg));
return NULL;
}
}
else
{
/* Write out the new configuration. */
if (!config_write_file(&(cfg->cfg), fileName))
{
fprintf(stderr, "Error while writing file.\n");
// config_destroy(&cfg);
return NULL;
}
}
return cfg;
}

View File

@ -0,0 +1,20 @@
#ifndef CONFIGIMPL_H
#define CONFIGIMPL_H
#include "Config.h"
#include <libconfig.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct config Config;
typedef struct config
{
VConfig base;
void (*close)(VConfig *);
config_t cfg;
} Config;
Config *NewConfig(const char *fileName);
#ifdef __cplusplus
}
#endif
#endif