embedded-framework/middleware/IpcConfig/src/IpcConfig.cpp
2024-04-07 19:26:15 +08:00

569 lines
26 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "IpcConfig.h"
#include "ILog.h"
#include <string.h>
#define CHECK_MAP(map) (map.size() == 1 ? true : false)
IpcConfig::IpcConfig()
{
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
std::map<std::string, std::reference_wrapper<int>> innerMapWorkMode;
innerMapWorkMode.insert(std::make_pair("work_mode", std::reference_wrapper<int>(mAllData.workMode)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_WORK_MODE, innerMapWorkMode));
std::map<std::string, std::reference_wrapper<int>> innerMapContinuousShot;
innerMapContinuousShot.insert(
std::make_pair("continuous_shot", std::reference_wrapper<int>(mAllData.continuousShot)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_CONTINUOUS_SHOT, innerMapContinuousShot));
std::map<std::string, std::reference_wrapper<int>> innerMapBurstPhotoInterval;
innerMapBurstPhotoInterval.insert(
std::make_pair("burst_photo_interval", std::reference_wrapper<int>(mAllData.burstPhotoInterval)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_BURST_PHOTO_INTERVAL, innerMapBurstPhotoInterval));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapImageSize;
innerMapImageSize.insert(std::make_pair("image_size", std::reference_wrapper<CHAR_STRING>(mAllData.imageSize)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::KEY_IMGAE_SIZE, innerMapImageSize));
std::map<std::string, std::reference_wrapper<int>> innerMapVideoSize;
innerMapVideoSize.insert(std::make_pair("video_size", std::reference_wrapper<int>(mAllData.videoSize)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_VIDEO_SIZE, innerMapVideoSize));
std::map<std::string, std::reference_wrapper<int>> innerMapInfraredLampPower;
innerMapInfraredLampPower.insert(
std::make_pair("infrared_lamp_power", std::reference_wrapper<int>(mAllData.infraredIampPower)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_INFRARED_LAMP_POWER, innerMapInfraredLampPower));
std::map<std::string, std::reference_wrapper<int>> innerMapDelayed;
innerMapDelayed.insert(std::make_pair("delayed", std::reference_wrapper<int>(mAllData.delayed)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_DELAYED, innerMapDelayed));
std::map<std::string, std::reference_wrapper<int>> innerMapPirSensitivity;
innerMapPirSensitivity.insert(
std::make_pair("pir_sensitivity", std::reference_wrapper<int>(mAllData.pirSensitivity)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_PIR_SENSITIVITY, innerMapPirSensitivity));
std::map<std::string, std::reference_wrapper<bool>> innerMapStorageLoopSwitch;
innerMapStorageLoopSwitch.insert(
std::make_pair("storage_loop_switch", std::reference_wrapper<bool>(mAllData.storageLoopSwitch)));
mCfgMapBool.insert(std::make_pair(IpcConfigKey::KEY_STORAGE_LOOP_SWITCH, innerMapStorageLoopSwitch));
std::map<std::string, std::reference_wrapper<bool>> innerMapFactoryReset;
innerMapFactoryReset.insert(std::make_pair("factory_reset", std::reference_wrapper<bool>(mAllData.factoryReset)));
mCfgMapBool.insert(std::make_pair(IpcConfigKey::KEY_FACTORY_RESET_FLAG, innerMapFactoryReset));
std::map<std::string, std::reference_wrapper<bool>> innerMapFormattingSDCard;
innerMapFormattingSDCard.insert(
std::make_pair("formatting_SD_card", std::reference_wrapper<bool>(mAllData.formattingSDCard)));
mCfgMapBool.insert(std::make_pair(IpcConfigKey::KEY_FORMATTING_SD_CARD, innerMapFormattingSDCard));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapDrakMode;
innerMapDrakMode.insert(std::make_pair("dark_mode", std::reference_wrapper<CHAR_STRING>(mAllData.darkMode)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::KEY_DARK_MODE, innerMapDrakMode));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapWorkInterval;
innerMapWorkInterval.insert(
std::make_pair("work_interval", std::reference_wrapper<CHAR_STRING>(mAllData.workingInterval)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::KEY_WORK_INTERVAL, innerMapWorkInterval));
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<long>> innerMapLong;
innerMapLong.insert(std::make_pair("test_long", std::reference_wrapper<long>(mAllData.testLong)));
mCfgMapLong.insert(std::make_pair(IpcConfigKey::TEST_LONG, innerMapLong));
std::map<std::string, std::reference_wrapper<long long>> innerMapLLong;
innerMapLLong.insert(std::make_pair("test_llong", std::reference_wrapper<long long>(mAllData.testLLong)));
mCfgMapLLong.insert(std::make_pair(IpcConfigKey::TEST_LLONG, innerMapLLong));
std::map<std::string, std::reference_wrapper<char>> innerMapChar;
innerMapChar.insert(std::make_pair("test_char", std::reference_wrapper<char>(mAllData.testChar)));
mCfgMapChar.insert(std::make_pair(IpcConfigKey::TEST_CHAR, innerMapChar));
std::map<std::string, std::reference_wrapper<float>> innerMapFloat;
innerMapFloat.insert(std::make_pair("test_float", std::reference_wrapper<float>(mAllData.testFloat)));
mCfgMapFloat.insert(std::make_pair(IpcConfigKey::TEST_FLOAT, innerMapFloat));
}
const StatusCode IpcConfig::Init(void)
{
memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg) {
LogError("Open config file failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
ReadAllConfigParameters();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfig::UnInit(void)
{
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save config files.\n");
ConfigSaveFile(mCfg);
}
CloseConfigFile(mCfg);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfig::ConfigFileSave(void)
{
return ConfigSaveFile(mCfg);
}
const int IpcConfig::GetInt(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key);
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;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetInt(const IpcConfigKey &key, const int &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetInt(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
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();
ConfigSetShort(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::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->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::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long long IpcConfig::GetLLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long long UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetLLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const char IpcConfig::GetChar(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->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::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = character;
const char *name = iter->second.begin()->first.c_str();
ConfigSetChar(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const float IpcConfig::GetFloat(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
iter = mCfgMapFloat.find(key);
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr float UNKNOWN_CONFIG = -1.00;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
iter = mCfgMapFloat.find(key);
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetFloat(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const double IpcConfig::GetDouble(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::iterator iter;
iter = mCfgMapDouble.find(key);
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr double UNKNOWN_CONFIG = -1.00;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::iterator iter;
iter = mCfgMapDouble.find(key);
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetDouble(mCfg, name, iter->second.begin()->second);
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::map<std::string, std::reference_wrapper<bool>>>::iterator iter;
iter = mCfgMapBool.find(key);
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->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::map<std::string, std::reference_wrapper<bool>>>::iterator iter;
iter = mCfgMapBool.find(key);
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetBool(mCfg, name, iter->second.begin()->second);
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();
ConfigSetString(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
void IpcConfig::ReadAllConfigParameters(void)
{
StatusCode workModeCode = ConfigGetInt(mCfg, "work_mode", &(mAllData.workMode));
if (StatusCodeEqual(workModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("work_mode doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.workMode = WORK_MODE_PIC;
ConfigSetInt(mCfg, "work_mode", mAllData.workMode);
}
StatusCode continuousShotCode = ConfigGetInt(mCfg, "continuous_shot", &(mAllData.continuousShot));
if (StatusCodeEqual(continuousShotCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("continuous_shot doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.continuousShot = CONTINUOUS_SHOT_ONE_PIC;
ConfigSetInt(mCfg, "continuous_shot", mAllData.continuousShot);
}
StatusCode burstPhotoIntervalCode = ConfigGetInt(mCfg, "burst_photo_interval", &(mAllData.burstPhotoInterval));
if (StatusCodeEqual(burstPhotoIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("burst_photo_interval doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.burstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT;
ConfigSetInt(mCfg, "burst_photo_interval", mAllData.burstPhotoInterval);
}
const char *imageSizeString = nullptr;
StatusCode imageSizeCode = ConfigGetString(mCfg, "image_size", &(imageSizeString));
if (StatusCodeEqual(imageSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("image_size doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
char defaultImageSize[] = "320*320";
strncpy(mAllData.imageSize, defaultImageSize, sizeof(mAllData.imageSize));
ConfigSetString(mCfg, "image_size", mAllData.imageSize);
}
else {
if (nullptr != imageSizeString) {
strncpy(mAllData.imageSize, imageSizeString, sizeof(mAllData.imageSize));
}
else {
LogError("image_size get failed.\n");
}
}
StatusCode videoSizeCode = ConfigGetInt(mCfg, "video_size", &(mAllData.videoSize));
if (StatusCodeEqual(videoSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("video_size doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.videoSize = VIDEO_SIZE_15;
ConfigSetInt(mCfg, "video_size", mAllData.videoSize);
}
StatusCode infraredIampPowerCode = ConfigGetInt(mCfg, "infrared_lamp_power", &(mAllData.infraredIampPower));
if (StatusCodeEqual(infraredIampPowerCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("infrared_lamp_power doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.infraredIampPower = INFRARED_IAMP_POWER_MID;
ConfigSetInt(mCfg, "infrared_lamp_power", mAllData.infraredIampPower);
}
StatusCode delayedCode = ConfigGetInt(mCfg, "delayed", &(mAllData.delayed));
if (StatusCodeEqual(delayedCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("delayed doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.delayed = DELAYED_DEFAULT;
ConfigSetInt(mCfg, "delayed", mAllData.delayed);
}
StatusCode pirSensitivityCode = ConfigGetInt(mCfg, "pir_sensitivity", &(mAllData.pirSensitivity));
if (StatusCodeEqual(pirSensitivityCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("pir_sensitivity doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.pirSensitivity = PIR_SENSITIVITY_DEFAULT;
ConfigSetInt(mCfg, "pir_sensitivity", mAllData.pirSensitivity);
}
StatusCode storageLoopSwitchCode = ConfigGetBool(mCfg, "storage_loop_switch", &(mAllData.storageLoopSwitch));
if (StatusCodeEqual(storageLoopSwitchCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("storage_loop_switch doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr bool storageLoopSwitch = true;
mAllData.storageLoopSwitch = storageLoopSwitch;
ConfigSetBool(mCfg, "storage_loop_switch", mAllData.storageLoopSwitch);
}
StatusCode factoryResetCode = ConfigGetBool(mCfg, "factory_reset", &(mAllData.factoryReset));
if (StatusCodeEqual(factoryResetCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("factory_resetCode doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr bool factoryReset = true;
mAllData.factoryReset = factoryReset;
ConfigSetBool(mCfg, "factory_reset", mAllData.factoryReset);
}
StatusCode formattingSDCardCode = ConfigGetBool(mCfg, "formatting_SD_card", &(mAllData.formattingSDCard));
if (StatusCodeEqual(formattingSDCardCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("formatting_SD_card doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr bool formattingSDCard = true;
mAllData.formattingSDCard = formattingSDCard;
ConfigSetBool(mCfg, "formatting_SD_card", mAllData.formattingSDCard);
}
const char *darkModeString = nullptr;
StatusCode darkModeCode = ConfigGetString(mCfg, "dark_mode", &(darkModeString));
if (StatusCodeEqual(darkModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("dark_mode doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
char defaultDarkMode[] = "19:00:00-07:00:00";
strncpy(mAllData.darkMode, defaultDarkMode, sizeof(mAllData.darkMode));
ConfigSetString(mCfg, "dark_mode", mAllData.darkMode);
}
else {
if (nullptr != darkModeString) {
strncpy(mAllData.darkMode, darkModeString, sizeof(mAllData.darkMode));
}
else {
LogError("dark_mode get failed.\n");
}
}
const char *workIntervalString = nullptr;
StatusCode workIntervalCode = ConfigGetString(mCfg, "work_interval", &(workIntervalString));
if (StatusCodeEqual(workIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("work_interval doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
char defaultWorkInterval[] = "07:00:01-07:00:00";
strncpy(mAllData.workingInterval, defaultWorkInterval, sizeof(mAllData.workingInterval));
ConfigSetString(mCfg, "work_interval", mAllData.workingInterval);
}
else {
if (nullptr != workIntervalString) {
strncpy(mAllData.workingInterval, workIntervalString, sizeof(mAllData.workingInterval));
}
else {
LogError("work_interval get failed.\n");
}
}
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);
}
StatusCode longCode = ConfigGetLong(mCfg, "test_long", &(mAllData.testLong));
if (StatusCodeEqual(longCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_long doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr long DEFAULT_TEST_LONG_NUM = 12;
mAllData.testLong = DEFAULT_TEST_LONG_NUM;
ConfigSetLong(mCfg, "test_long", mAllData.testLong);
}
StatusCode llongCode = ConfigGetLLong(mCfg, "test_llong", &(mAllData.testLLong));
if (StatusCodeEqual(llongCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_llong doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr long long DEFAULT_TEST_LLONG_NUM = 13;
mAllData.testLLong = DEFAULT_TEST_LLONG_NUM;
ConfigSetLLong(mCfg, "test_llong", mAllData.testLLong);
}
StatusCode charCode = ConfigGetChar(mCfg, "test_char", &(mAllData.testChar));
if (StatusCodeEqual(charCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_char doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr char DEFAULT_TEST_CHAR_NUM = 'B';
mAllData.testChar = DEFAULT_TEST_CHAR_NUM;
ConfigSetChar(mCfg, "test_char", mAllData.testChar);
}
StatusCode floatCode = ConfigGetFloat(mCfg, "test_float", &(mAllData.testFloat));
if (StatusCodeEqual(floatCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_float doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr float DEFAULT_TEST_FLOAT_NUM = 1.123456;
mAllData.testFloat = DEFAULT_TEST_FLOAT_NUM;
ConfigSetFloat(mCfg, "test_float", mAllData.testFloat);
}
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save the config file.\n");
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
ConfigSaveFile(mCfg);
}
}