hunting/middleware/IpcConfig/src/IpcConfigImpl.cpp
2024-05-24 09:01:38 +08:00

905 lines
40 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 "IpcConfigImpl.h"
#include "ILog.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CHECK_MAP(map) (map.size() == 1 ? true : false)
const char *CONFIG_WIFI_SSID = "wifi_ssid";
const char *CONFIG_WIFI_SSID_DEFAULT = "Hunting 2024";
const char *CONFIG_WIFI_PASSWORD = "wifi_password";
const char *CONFIG_WIFI_PASSWORD_DEFAULT = "123456";
const char *CONFIG_WORK_MODE = "work_mode";
const char *CONFIG_CONTINUE_SHOT = "continuous_shot";
const char *CONFIG_BURST_PHOTO_INTERVAL = "continuous_interval";
const char *CONFIG_IMAGE_SIZE = "image_size";
const char *CONFIG_VIDEO_SIZE = "video_size";
const char *CONFIG_PIR_DELAYED = "pir_delayed";
const char *CONFIG_STORAGE_LOOP = "storage_loop";
const char *CONFIG_INFRARED_POWER = "infrared_power";
const char *CONFIG_PIR_SENSITIVITY = "pir_sensitivity";
IpcConfigImpl::IpcConfigImpl() : mThreadRuning(false), mCfgChanged(CONFIG_HAS_NOT_CHANGED), mCfg(nullptr)
{
memset(&mAllData, 0, sizeof(Config_s));
// std::map<std::string, std::reference_wrapper<int>> innerMapWorkMode;
// innerMapWorkMode.insert(std::make_pair("work_mode", std::reference_wrapper<int>(mAllData.mWorkMode)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::WORK_MODE, innerMapWorkMode));
// std::map<std::string, std::reference_wrapper<int>> innerMapContinuousShot;
// innerMapContinuousShot.insert(
// std::make_pair("continuous_shot", std::reference_wrapper<int>(mAllData.mContinuousShot)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::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.mBurstPhotoInterval)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::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::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::VIDEO_LENGTH, innerMapVideoSize));
// std::map<std::string, std::reference_wrapper<int>> innerMapInfraredLampPower;
// innerMapInfraredLampPower.insert(
// std::make_pair("infrared_lamp_power", std::reference_wrapper<int>(mAllData.mInfraredPower)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::INFRARED_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::PIR_DELAYED, innerMapDelayed));
// std::map<std::string, std::reference_wrapper<int>> innerMapPirSensitivity;
// innerMapPirSensitivity.insert(
// std::make_pair("pir_sensitivity", std::reference_wrapper<int>(mAllData.mPirSensitivity)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::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.mStorageLoopSwitch)));
// mCfgMapBool.insert(std::make_pair(IpcConfigKey::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::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::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::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::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 IpcConfigImpl::Init(void)
{
// memset(&mAllData, 0, sizeof(Config_s));
// InitConfigMap();
// mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
// if (nullptr == mCfg) {
// LogError("Open config file failed.\n");
// return CreateStatusCode(STATUS_CODE_NOT_OK);
// }
// ReadAllConfigParameters();
auto readingConfig = [](std::shared_ptr<IpcConfigImpl> impl) {
impl->ReadingConfigThread();
};
std::shared_ptr<IpcConfigImpl> impl = shared_from_this();
mInitThread = std::thread(readingConfig, impl);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfigImpl::UnInit(void)
{
mThreadRuning = false;
if (mInitThread.joinable()) {
mInitThread.join();
}
std::lock_guard<std::mutex> locker(mMutex);
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save config files.\n");
ConfigSaveFile(mCfg);
}
CloseConfigFile(mCfg);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfigImpl::ConfigFileSave(void)
{
std::lock_guard<std::mutex> locker(mMutex);
return ConfigSaveFile(mCfg);
}
const int IpcConfigImpl::GetInt(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, int *>>::iterator iter;
iter = mCfgMapIntV2.find(key);
if (iter != mCfgMapIntV2.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 IpcConfigImpl::SetInt(const IpcConfigKey &key, const int &value)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, int *>>::iterator iter;
iter = mCfgMapIntV2.find(key);
if (iter != mCfgMapIntV2.end() && CHECK_MAP(iter->second)) {
*(iter->second.begin()->second) = value;
ConfigSetInt(mCfg, iter->second.begin()->first.c_str(), *(iter->second.begin()->second));
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const short IpcConfigImpl::GetShort(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::SetShort(const IpcConfigKey &key, const short &value)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::GetLong(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::SetLong(const IpcConfigKey &key, const long &value)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::GetLLong(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::SetLLong(const IpcConfigKey &key, const long long &value)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::GetChar(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, char *>>::iterator iter;
iter = mCfgMapCharV2.find(key);
if (iter != mCfgMapCharV2.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 IpcConfigImpl::SetChar(const IpcConfigKey &key, const char &character)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, char *>>::iterator iter;
iter = mCfgMapCharV2.find(key);
if (iter != mCfgMapCharV2.end() && CHECK_MAP(iter->second)) {
*(iter->second.begin()->second) = 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 IpcConfigImpl::GetFloat(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::SetFloat(const IpcConfigKey &key, const float &value)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::GetDouble(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::SetDouble(const IpcConfigKey &key, const double &value)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::GetLongDouble(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::SetLongDouble(const IpcConfigKey &key, const long double &value)
{
std::lock_guard<std::mutex> locker(mMutex);
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 IpcConfigImpl::GetBool(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, bool *>>::iterator iter;
iter = mCfgMapBoolV2.find(key);
if (iter != mCfgMapBoolV2.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 IpcConfigImpl::SetBool(const IpcConfigKey &key, const bool &value)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, bool *>>::iterator iter;
iter = mCfgMapBoolV2.find(key);
if (iter != mCfgMapBoolV2.end() && CHECK_MAP(iter->second)) {
*(iter->second.begin()->second) = value;
ConfigSetBool(mCfg, iter->second.begin()->first.c_str(), *(iter->second.begin()->second));
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const std::string IpcConfigImpl::GetString(const IpcConfigKey &key)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, StringConfigPack>>::iterator iter;
iter = mCfgMapStringV2.find(key);
if (iter != mCfgMapStringV2.end() && CHECK_MAP(iter->second)) {
const std::string sv(iter->second.begin()->second.mValue); // char[] --> const std::strinbg
return sv;
}
LogError("Can't find the key.\n");
const std::string UNKNOWN_CONFIG = "undefine";
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetString(const IpcConfigKey &key, const std::string &string)
{
std::lock_guard<std::mutex> locker(mMutex);
std::map<IpcConfigKey, std::map<std::string, StringConfigPack>>::iterator iter;
iter = mCfgMapStringV2.find(key);
if (iter != mCfgMapStringV2.end() && CHECK_MAP(iter->second)) {
if (string.length() > iter->second.begin()->second.mLength) {
LogError("The string is too long. Your string is %s, the max length is %d\n",
string.c_str(),
iter->second.begin()->second.mLength);
return;
}
strncpy(iter->second.begin()->second.mValue, string.c_str(), iter->second.begin()->second.mLength);
ConfigSetString(mCfg, iter->second.begin()->first.c_str(), iter->second.begin()->second.mValue);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
WorkMode IpcConfigImpl::GetWorkMode(void)
{
return static_cast<WorkMode>(GetChar(IpcConfigKey::WORK_MODE));
}
void IpcConfigImpl::SetWorkMode(const WorkMode &mode)
{
SetChar(IpcConfigKey::WORK_MODE, static_cast<char>(mode));
}
ConfigSwitch IpcConfigImpl::GetSwitch(const IpcConfigKey &key)
{
bool value = GetBool(key);
return value == true ? ConfigSwitch::ON : ConfigSwitch::OFF;
}
void IpcConfigImpl::SetSwitch(const IpcConfigKey &key, const ConfigSwitch &value)
{
bool config = value == ConfigSwitch::ON ? true : false;
SetBool(key, config);
}
ConfigLevel IpcConfigImpl::GetLevel(const IpcConfigKey &key)
{
char value = GetChar(key);
return static_cast<ConfigLevel>(value);
}
void IpcConfigImpl::SetLevel(const IpcConfigKey &key, const ConfigLevel &value)
{
char config = static_cast<char>(value);
SetChar(key, config);
}
void IpcConfigImpl::ReadingConfigThread(void)
{
constexpr int DIR_PATH_LENGTH = 32;
char dirPath[DIR_PATH_LENGTH] = {0};
strncpy(dirPath, IPC_CONFIG_FILE_PATH, DIR_PATH_LENGTH - 1);
char *lastSlash = strrchr(dirPath, '/');
if (lastSlash == NULL) {
strcpy(dirPath, ".");
}
else {
*lastSlash = '\0';
}
mThreadRuning = true;
LogInfo("Reading config thread is running, dirPath = %s\n", dirPath);
while (mThreadRuning) {
if (CheckConfigPathMounted() == true) {
memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg) {
LogError("Open config file failed.\n");
continue;
}
std::lock_guard<std::mutex> locker(mMutex);
InitConfigMap();
ReadAllConfigParameters();
LogInfo("Read config file success.\n");
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
mThreadRuning = false;
}
void IpcConfigImpl::InitConfigMap(void)
{
{
std::map<std::string, StringConfigPack> config;
StringConfigPack pack = {WIFI_SSID_BUFF_SIZE, mAllData.mWifiSsid};
config.insert(std::make_pair(CONFIG_WIFI_SSID, pack));
mCfgMapStringV2.insert(std::make_pair(IpcConfigKey::WIFI_SSID, config));
}
{
std::map<std::string, StringConfigPack> config;
StringConfigPack pack = {WIFI_PASSWORD_BUFF_SIZE, mAllData.mWifiPassword};
config.insert(std::make_pair(CONFIG_WIFI_PASSWORD, pack));
mCfgMapStringV2.insert(std::make_pair(IpcConfigKey::WIFI_PASSWORD, config));
}
{
std::map<std::string, char *> config;
config.insert(std::make_pair(CONFIG_WORK_MODE, &mAllData.mWorkMode));
mCfgMapCharV2.insert(std::make_pair(IpcConfigKey::WORK_MODE, config));
}
{
std::map<std::string, char *> config;
config.insert(std::make_pair(CONFIG_INFRARED_POWER, &mAllData.mInfraredPower));
mCfgMapCharV2.insert(std::make_pair(IpcConfigKey::INFRARED_POWER, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_CONTINUE_SHOT, &mAllData.mContinuousShot));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::CONTINUOUS_SHOT, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_BURST_PHOTO_INTERVAL, &mAllData.mBurstPhotoInterval));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::BURST_PHOTO_INTERVAL, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_IMAGE_SIZE, &mAllData.mImageSize));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::IMGAE_SIZE, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_VIDEO_SIZE, &mAllData.mVideoLength));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::VIDEO_LENGTH, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_PIR_DELAYED, &mAllData.mPirDelayed));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::PIR_DELAYED, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_PIR_SENSITIVITY, &mAllData.mPirSensitivity));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::PIR_SENSITIVITY, config));
}
{
std::map<std::string, bool *> config;
config.insert(std::make_pair(CONFIG_STORAGE_LOOP, &mAllData.mStorageLoopSwitch));
mCfgMapBoolV2.insert(std::make_pair(IpcConfigKey::STORAGE_LOOP_SWITCH, config));
}
}
void IpcConfigImpl::ReadAllConfigParameters(void)
{
{
const char *config = nullptr;
StatusCode code = ConfigGetString(mCfg, CONFIG_WIFI_SSID, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_WIFI_SSID doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
strncpy(mAllData.mWifiSsid, CONFIG_WIFI_SSID_DEFAULT, sizeof(mAllData.mWifiSsid));
ConfigSetString(mCfg, CONFIG_WIFI_SSID, mAllData.mWifiSsid);
}
else {
if (nullptr != config) {
strncpy(mAllData.mWifiSsid, config, sizeof(mAllData.mWifiSsid));
}
else {
LogError("CONFIG_WIFI_SSID get failed.\n");
}
}
}
{
const char *config = nullptr;
StatusCode code = ConfigGetString(mCfg, CONFIG_WIFI_PASSWORD, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_WIFI_PASSWORD doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
strncpy(mAllData.mWifiPassword, CONFIG_WIFI_PASSWORD_DEFAULT, sizeof(mAllData.mWifiPassword));
ConfigSetString(mCfg, CONFIG_WIFI_PASSWORD, mAllData.mWifiPassword);
}
else {
if (nullptr != config) {
strncpy(mAllData.mWifiPassword, config, sizeof(mAllData.mWifiPassword));
}
else {
LogError("CONFIG_WIFI_PASSWORD get failed.\n");
}
}
}
{
StatusCode code = ConfigGetChar(mCfg, CONFIG_WORK_MODE, &mAllData.mWorkMode);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_WORK_MODE doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mWorkMode = CONFIG_WORK_MODE_DEFAULT;
ConfigSetChar(mCfg, CONFIG_WORK_MODE, mAllData.mWorkMode);
}
}
{
StatusCode code = ConfigGetChar(mCfg, CONFIG_INFRARED_POWER, &mAllData.mInfraredPower);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_INFRARED_POWER doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mInfraredPower = CONFIG_INFRARED_POWER_DEFAULT;
ConfigSetChar(mCfg, CONFIG_INFRARED_POWER, mAllData.mInfraredPower);
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_CONTINUE_SHOT, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_CONTINUE_SHOT doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mContinuousShot = CONFIG_CONTINUE_SHOT_DEFAULT;
ConfigSetInt(mCfg, CONFIG_CONTINUE_SHOT, mAllData.mContinuousShot);
}
else {
mAllData.mContinuousShot = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_BURST_PHOTO_INTERVAL, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_BURST_PHOTO_INTERVAL doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mBurstPhotoInterval = CONFIG_BURST_PHOTO_INTERVAL_DEFAULT;
ConfigSetInt(mCfg, CONFIG_BURST_PHOTO_INTERVAL, mAllData.mBurstPhotoInterval);
}
else {
mAllData.mBurstPhotoInterval = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_IMAGE_SIZE, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_IMAGE_SIZE doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mImageSize = CONFIG_IMAGE_SIZE_DEFAULT;
ConfigSetInt(mCfg, CONFIG_IMAGE_SIZE, mAllData.mImageSize);
}
else {
mAllData.mImageSize = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_VIDEO_SIZE, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_VIDEO_SIZE doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mVideoLength = CONFIG_VIDEO_SIZE_DEFAULT;
ConfigSetInt(mCfg, CONFIG_VIDEO_SIZE, mAllData.mVideoLength);
}
else {
mAllData.mVideoLength = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_PIR_DELAYED, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_PIR_DELAYED doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mPirDelayed = CONFIG_PIR_DELAYED_DEFAULT;
ConfigSetInt(mCfg, CONFIG_PIR_DELAYED, mAllData.mPirDelayed);
}
else {
mAllData.mPirDelayed = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_PIR_SENSITIVITY, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_PIR_SENSITIVITY doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mPirSensitivity = CONFIG_PIR_SENSITIVITY_DEFAULT;
ConfigSetInt(mCfg, CONFIG_PIR_SENSITIVITY, mAllData.mPirSensitivity);
}
else {
mAllData.mPirSensitivity = config;
}
}
{
StatusCode code = ConfigGetBool(mCfg, CONFIG_STORAGE_LOOP, &(mAllData.mStorageLoopSwitch));
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_STORAGE_LOOP doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mStorageLoopSwitch = CONFIG_STORAGE_LOOP_DEFAULT;
ConfigSetBool(mCfg, CONFIG_STORAGE_LOOP, mAllData.mStorageLoopSwitch);
}
}
// StatusCode workModeCode = ConfigGetInt(mCfg, "work_mode", &(mAllData.mWorkMode));
// 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.mWorkMode = MODE_PIC;
// ConfigSetInt(mCfg, "work_mode", mAllData.mWorkMode);
// }
// StatusCode continuousShotCode = ConfigGetInt(mCfg, "continuous_shot", &(mAllData.mContinuousShot));
// 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.mContinuousShot = CONTINUOUS_SHOT_ONE_PIC;
// ConfigSetInt(mCfg, "continuous_shot", mAllData.mContinuousShot);
// }
// StatusCode burstPhotoIntervalCode = ConfigGetInt(mCfg, "burst_photo_interval",
// &(mAllData.mBurstPhotoInterval)); 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.mBurstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT;
// ConfigSetInt(mCfg, "burst_photo_interval", mAllData.mBurstPhotoInterval);
// }
// 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.mInfraredPower));
// 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.mInfraredPower = INFRARED_IAMP_POWER_MID;
// ConfigSetInt(mCfg, "infrared_lamp_power", mAllData.mInfraredPower);
// }
// 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.mPirSensitivity));
// 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.mPirSensitivity = PIR_SENSITIVITY_DEFAULT;
// ConfigSetInt(mCfg, "pir_sensitivity", mAllData.mPirSensitivity);
// }
// StatusCode storageLoopSwitchCode = ConfigGetBool(mCfg, "storage_loop_switch",
// &(mAllData.mStorageLoopSwitch)); 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.mStorageLoopSwitch = storageLoopSwitch;
// ConfigSetBool(mCfg, "storage_loop_switch", mAllData.mStorageLoopSwitch);
// }
// 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);
}
}
bool IpcConfigImpl::CheckConfigPathMounted(void)
{
FILE *fp;
char line[1024];
char mount_point[1024] = {0};
fp = fopen("/proc/mounts", "r");
if (fp == NULL) {
perror("Error opening /proc/mounts");
return false;
}
while (fgets(line, sizeof(line), fp) != NULL) {
if (sscanf(line, "%*s %1023s %*s %*s %*d %*d\n", mount_point) == 1) {
// LogInfo("mount_point: %s\n", mount_point);
if (strcmp("/userdata", mount_point) == 0) {
fclose(fp);
return true; // mounted
}
}
}
fclose(fp);
return false; // unmounted
}