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
#define IHALCPP_H
#include "StatusCode.h"
#include <iostream>
#include <memory>
constexpr int INVALID_PERIOD = -1;
constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100;

View File

@ -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()

View File

@ -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,22 +15,47 @@
#ifndef IIPCCONFIG_H
#define IIPCCONFIG_H
#include "StatusCode.h"
#include <iostream>
#include <memory>
#include <string_view>
enum class IpcConfigKey
{
TEST_NUM = 0,
TEST_SHORT,
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<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; }
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);
#endif

View File

@ -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> &IIpcConfig::GetInstance(std::shared_ptr<IIpcConfig> *impl)
{
static auto instance = std::make_shared<IIpcConfig>();
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");
}
}

View File

@ -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,22 +15,30 @@
#include "IpcConfig.h"
#include "ILog.h"
#include <string.h>
#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<int>>(
IpcConfigKey::TEST_NUM,
std::reference_wrapper<int>(mAllData.testNum)));
std::map<std::string, std::reference_wrapper<int>> innerMapInt;
innerMapInt.insert(std::make_pair("test_num", std::reference_wrapper<int>(mAllData.testNum)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::TEST_NUM, innerMapInt));
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)
{
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);
}
@ -39,21 +47,21 @@ 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 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);
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;
@ -61,33 +69,267 @@ const int IpcConfig::GetInt(const IpcConfigKey &key)
}
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);
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
{
else {
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)
{
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;
mAllData.testNum = DEFAULT_TEST_NUM;
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");
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
ConfigSaveFile(mCfg);
}
}
}

View File

@ -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,16 +14,20 @@
*/
#ifndef IPCCONFIG_H
#define IPCCONFIG_H
#include "StatusCode.h"
#include "IIpcConfig.h"
#include "Config.h"
#include <memory>
#include "IIpcConfig.h"
#include "StatusCode.h"
#include <map>
#include <memory>
constexpr bool CONFIG_HAS_CHANGED = true;
constexpr bool CONFIG_HAS_NOT_CHANGED = false;
typedef char CHAR_STRING[256];
typedef struct Config_s
{
int testNum;
short testShort;
CHAR_STRING testString;
} Config_s;
class MapInt
{
@ -38,8 +42,27 @@ 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;
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:
void ReadAllConfigParameters(void);
@ -48,8 +71,15 @@ private:
bool mCfgChanged;
VConfig *mCfg;
Config_s mAllData;
std::map<IpcConfigKey, std::reference_wrapper<int>> mCfgMapInt;
std::map<IpcConfigKey, std::reference_wrapper<long int>> mCfgMapUInt;
std::map<IpcConfigKey, std::reference_wrapper<std::string>> mCfgMapString;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
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

View File

@ -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<IIpcConfig>();
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> &IpcConfigMakePtr::GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl)
{
static auto instance = std::make_shared<IpcConfigMakePtr>();
if (impl)
{
if (impl) {
instance = *impl;
}
return instance;

View File

@ -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.

View File

@ -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})

View File

@ -1,20 +1,34 @@
#include "ILog.h"
#include "IIpcConfig.h"
#include "ILog.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
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);
IIpcConfig::GetInstance()->UnInit();
ILogUnInit();
DestroyLogModule();
}
// ../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 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();
ILogUnInit();
DestroyLogModule();
}
} // namespace IpcConfigTest

View File

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

View File

@ -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

View File

@ -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,28 +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 (*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);
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

View File

@ -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 <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);
}
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,17 +36,45 @@ 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 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

@ -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 <string.h>
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");

View File

@ -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

View File

@ -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 <limits.h>
#include <stdlib.h>
#include <unistd.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)
{
/* 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);
@ -58,14 +58,61 @@ 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);
}
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)
{
if (NULL == cfg)
{
if (NULL == cfg) {
LogError("NULL pointer.\n");
return;
}
@ -73,6 +120,10 @@ 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;
}
Config *NewConfig(const char *fileName)
@ -81,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;
}
@ -109,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;

View File

@ -17,21 +17,20 @@
#include "Config.h"
#include <libconfig.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
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