<修改> 1、动态获取配置文件路径

This commit is contained in:
ypc 2026-06-05 15:55:14 +08:00
parent 9a6c475575
commit 7549032700
21 changed files with 1147 additions and 990 deletions

2
.vscode/launch.json vendored
View File

@ -5,7 +5,7 @@
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/release/x86/exe/RTU",
"program": "${workspaceFolder}/test/RTU",
// "program": "${workspaceFolder}/release/x86/exe/FTU_cfg_parse",
"args": [],
"stopAtEntry": false,

View File

@ -22,6 +22,7 @@ else
echo "====================================="
echo " 本地 X86 编译 开始"
echo "====================================="
rm -f "$(dirname "$0")/../test/RTU"
make clean
make
fi

View File

@ -139,6 +139,7 @@ int func_get_process_pid_by_name(const char *proc_name);
int func_process_exist_by_pid(int pid);
int func_get_process_path_by_pid(int pid, char *path, uint32_t len);
int func_get_process_self_path(char *path, uint32_t len);
int func_get_process_self_dir(char *path, uint32_t len);
/*************************************************************************/

View File

@ -60,6 +60,9 @@ endif
LIB_REL = $(REL_ROOT_DIR)/lib
EXE_REL = $(REL_ROOT_DIR)/exe
# test目录仅x86编译时复制产物用
TEST_DIR = $(ROOT_DIR)/test
# 导出环境变量
export CC
export CPP
@ -75,3 +78,4 @@ export LIB_REL_MY
export LIB_REL
export EXE_REL
export LIB_61850
export TEST_DIR

View File

@ -1272,6 +1272,32 @@ int func_get_process_self_path(char *path, uint32_t len)
return 0;
}
// 获取当前进程所在目录
int func_get_process_self_dir(char *path, uint32_t len)
{
if((path == NULL) || (len == 0))
{
LOG_E("func_get_process_self_dir input invalid, path=%p, len=%d", path, len);
return -1;
}
if(func_get_process_self_path(path, len) != 0)
{
LOG_E("func_get_process_self_dir func_get_process_self_path failed");
return -1;
}
char *p_ch = strrchr(path, '/');
if(NULL == p_ch)
{
LOG_E("func_get_process_self_dir strrchr failed, path=%s", path);
return -1;
}
*(p_ch + 1) = '\0';
return 0;
}
/*************************************************************************/
// 环境变量操作函数

View File

@ -1,5 +1,6 @@
#include "myLog.h"
#include "myBase.h"
#include "myFunc.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
@ -39,7 +40,7 @@
#define CLRLINE "\r\e[K" //or "\e[1K\r"
// 日志配置
#define LOG_FILE_PATH "/mnt/RTU/release/exe/logs"
static char g_log_path[256] = {0};
#define LOG_FILE_NAME "app.log"
#define LOG_FILE_MAX_SIZE (10 * 1024 * 1024)
#define LOG_FILE_MAX_COUNT 5
@ -243,28 +244,41 @@ static void get_log_file_path(char* path, size_t size, int index)
{
if (index == 0)
{
snprintf(path, size, "%s/%s", LOG_FILE_PATH, LOG_FILE_NAME);
snprintf(path, size, "%s/%s", g_log_path, LOG_FILE_NAME);
} else
{
snprintf(path, size, "%s/%s.%d", LOG_FILE_PATH, LOG_FILE_NAME, index);
snprintf(path, size, "%s/%s.%d", g_log_path, LOG_FILE_NAME, index);
}
}
// 初始化日志目录
static int init_log_directory(void)
static int init_log_directory(void)
{
if (g_log_path[0] == '\0')
{
char proc_dir[256] = {0};
if (func_get_process_self_dir(proc_dir, sizeof(proc_dir)) == 0)
{
snprintf(g_log_path, sizeof(g_log_path), "%slogs", proc_dir);
}
else
{
snprintf(g_log_path, sizeof(g_log_path), "/tmp/logs");
}
}
struct stat st = {0};
if (stat(LOG_FILE_PATH, &st) == -1)
if (stat(g_log_path, &st) == -1)
{
LOG_E("Failed to stat log directory %s", LOG_FILE_PATH);
if (mkdir(LOG_FILE_PATH, 0755) == -1)
LOG_E("Failed to stat log directory %s", g_log_path);
if (mkdir(g_log_path, 0755) == -1)
{
LOG_E("Failed to create log directory %s", LOG_FILE_PATH);
LOG_E("Failed to create log directory %s", g_log_path);
return -1;
}
}
LOG_I("Log directory %s create success", LOG_FILE_PATH);
LOG_I("Log directory %s create success", g_log_path);
return 0;
}

View File

@ -1,16 +1,34 @@
#include "myBase.h"
#include "myLog.h"
#include "myFunc.h"
#include "myTask.h"
#include "app_sys.h"
#include "mySystem.h"
LOCAL std::string path = "/mnt/RTU/test/config/";
LOCAL std::string get_config_path()
{
char proc_dir[512] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
MY_LOG_E("func_get_process_self_dir failed");
return "";
}
return std::string(proc_dir) + "config/SELF_PTL/";
}
LOCAL std::string self_ptl_cfg = "self_ptl.xml";
int app_cfg_parse()
{
std::string path = get_config_path();
if(path.empty())
{
MY_LOG_E("get_config_path failed, cannot get config directory");
return -1;
}
if(0 != self_ptl_cfg_parse(path + self_ptl_cfg))
{
MY_LOG_E("self_ptl_cfg_parse failed, path:%s", (path + self_ptl_cfg).c_str());

View File

@ -6,12 +6,6 @@
#include <unordered_map>
#define DC_PARAM_PATH "/mnt/RTU/test/file/PARAM/param.xml"
void dc_param_cfg_parse();
// 存储参数元数据到中央表dc_param_cfg_parse 在创建信号前调用)
void dc_param_metadata_store(const std::string &saddr, const stru_signal_param &param);
// 按 saddr 查找参数元数据,找到返回 true否则返回 false
bool dc_param_metadata_lookup(const std::string &saddr, stru_signal_param &out_param);
int dc_param_cfg_parse(const std::string &path);void dc_param_metadata_store(const std::string &saddr, const stru_signal_param &param);
bool dc_param_metadata_lookup(const std::string &saddr, stru_signal_param &out_param);

View File

@ -37,7 +37,7 @@ typedef struct stru_signal
bool dc_get_param_cfg_change();
void dc_set_param_cfg_change(bool change);
void dc_param_cfg_check();
void dc_param_cfg_check(const std::string &path);
void dc_signal_out_change_check();

View File

@ -4,14 +4,35 @@
#include "dc_param.h"
#include "dc_signal.h"
#include "dc_event.h"
#include "myFunc.h"
LOCAL std::string g_param_path = "";
LOCAL std::string datacenter_get_param_path()
{
char proc_dir[256] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
MY_LOG_E("func_get_process_self_dir failed");
return "";
}
return std::string(proc_dir) + "config/PARAM/param.xml";
}
int datacenter_init()
{
dc_param_cfg_parse();
std::string path = datacenter_get_param_path();
if(path.empty())
{
MY_LOG_E("datacenter_get_param_path failed, cannot get param path");
return -1;
}
if(0 != dc_param_cfg_parse(path))
{
MY_LOG_E("dc_param_cfg_parse failed, path:%s", path.c_str());
return -1;
}
g_param_path = path;
return 0;
}
@ -25,6 +46,6 @@ void datacenter_run_100ms()
void datacenter_run_1000ms()
{
dc_param_cfg_check();
dc_param_cfg_check(g_param_path);
}

View File

@ -20,22 +20,22 @@ bool dc_param_metadata_lookup(const std::string &saddr, stru_signal_param &out_p
return false;
}
void dc_param_cfg_parse()
int dc_param_cfg_parse(const std::string &path)
{
using namespace tinyxml2;
XMLDocument doc;
if (XML_SUCCESS != doc.LoadFile(DC_PARAM_PATH))
if (XML_SUCCESS != doc.LoadFile(path.c_str()))
{
MY_LOG_E("failed to load param file: %s", DC_PARAM_PATH);
return;
MY_LOG_E("failed to load param file: %s", path.c_str());
return -1;
}
XMLElement *root = doc.RootElement();
if (nullptr == root)
{
MY_LOG_E("param file has no root element");
return;
return -1;
}
// 解析 Ao 段:单值参数(一份值 + 一份缺省值)
@ -185,5 +185,5 @@ void dc_param_cfg_parse()
}
}
return 0;
}

View File

@ -411,8 +411,6 @@ LOCAL bool dc_signal_ao_add_check(stru_signal *p_signal, const std::string &desc
change = true;
}
}
if(p_signal->ctrl_type != ctrl_type)
{
p_signal->ctrl_type = ctrl_type;
@ -443,8 +441,6 @@ LOCAL bool dc_signal_param_add_check(stru_signal *p_signal, const std::string &d
change = true;
}
}
if(p_signal->ctrl_type != ctrl_type)
{
p_signal->ctrl_type = ctrl_type;
@ -2101,7 +2097,7 @@ LOCAL stru_signal *dc_find_signal_by_id(uint32_t id, stru_signal_map &dc_signal_
}
void dc_param_cfg_check()
void dc_param_cfg_check(const std::string &path)
{
if (false == dc_get_param_cfg_change()) return;
@ -2181,14 +2177,14 @@ void dc_param_cfg_check()
root->InsertEndChild(ao_elem);
root->InsertEndChild(param_elem);
if (XML_SUCCESS == doc.SaveFile(DC_PARAM_PATH))
if (XML_SUCCESS == doc.SaveFile(path.c_str()))
{
dc_set_param_cfg_change(false);
MY_LOG_I("param config file saved to %s", DC_PARAM_PATH);
MY_LOG_I("param config file saved to %s", path.c_str());
}
else
{
MY_LOG_E("failed to save param config file to %s", DC_PARAM_PATH);
MY_LOG_E("failed to save param config file to %s", path.c_str());
}
}

View File

@ -1,5 +1,6 @@
#include "mySystem.h"
#include "myLog.h"
#include "myFunc.h"
#include "myCmd.h"
#include "iec.h"
#include "iec_method.h"
@ -394,7 +395,15 @@ void iec60870_task(uint16_t usGap)
int app_iec_init1(void *arg)
{
if(0 != iec_cfg_parse("/mnt/RTU/test/config/iec1014.xml"))
char proc_dir[512] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
MY_LOG_E("func_get_process_self_dir failed");
return -1;
}
std::string cfg_path = std::string(proc_dir) + "config/IEC60870/iec1014.xml";
if(0 != iec_cfg_parse(cfg_path.c_str()))
{
MY_LOG_E("app_iec_init1 iec_cfg_parse failed");
return -1;

View File

@ -1,5 +1,6 @@
#include "iec61850m.h"
#include "myLog.h"
#include "myFunc.h"
#include "myCmd.h"
#include "iec61850_client.h"
#include "myDatacenter.h"
@ -8,7 +9,18 @@
LOCAL void mms_event_back(void *arg, int ret);
LOCAL const char *g_61850m_prj_path = "/mnt/RTU/test/config/MMS/";
LOCAL std::string get_base_path()
{
char proc_dir[512] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
MY_LOG_E("func_get_process_self_dir failed");
return "";
}
return std::string(proc_dir);
}
LOCAL std::string g_61850m_prj_path;
typedef struct
{
@ -559,6 +571,14 @@ int app_iec61850m_init1(void *arg)
dc_signal_out("iec61850m.run_cnt", "iec61850m.run_cnt", DATA_TYPE_U32, &p_app->run_cnt);
std::string base_path = get_base_path();
if(base_path.empty())
{
MY_LOG_E("get_base_path failed, cannot get process directory");
return -1;
}
g_61850m_prj_path = base_path + "config/MMS/";
if(0 != iec61850m_init())
{
LOG_E("iec61850m_init failed");

View File

@ -1,10 +1,22 @@
#include "iec61850_common.h"
#include "iec61850s.h"
#include "myLog.h"
#include "myFunc.h"
#include "myCmd.h"
#include "myDatacenter.h"
#include "parse_xml.h"
LOCAL std::string get_base_path()
{
char proc_dir[512] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
MY_LOG_E("func_get_process_self_dir failed");
return "";
}
return std::string(proc_dir);
}
typedef struct
{
stru_mms_s_signal_base base;
@ -379,7 +391,15 @@ int iec61850s_signals_init()
int app_iec61850s_init1(void *arg)
{
if(0 != parse_mms_xml("/mnt/RTU/test/config/MMS/mms_s.xml"))
std::string base_path = get_base_path();
if(base_path.empty())
{
MY_LOG_E("get_base_path failed, cannot get process directory");
return -1;
}
std::string mms_cfg_path = base_path + "config/MMS/mms_s.xml";
if(0 != parse_mms_xml(mms_cfg_path.c_str()))
{
MY_LOG_E("parse_mms_xml failed");
return -1;
@ -387,7 +407,7 @@ int app_iec61850s_init1(void *arg)
mms_s_dbg_switch(true);
mms_s_file_path_set("/mnt/RTU/test/");
mms_s_file_path_set(base_path.c_str());
mms_s_value_update_register(&g_mms_s_value_update_cb);
if(NULL == g_mms_s_value_update_cb)
{
@ -406,7 +426,14 @@ int app_iec61850s_init2(void *arg)
return -1;
}
if(0 != mms_s_init("/mnt/RTU/test/config/MMS/PCS.icd", 102))
std::string base_path = get_base_path();
if(base_path.empty())
{
MY_LOG_E("get_base_path failed, cannot get process directory");
return -1;
}
std::string mms_icd_path = base_path + "config/MMS/PCS.icd";
if(0 != mms_s_init(mms_icd_path.c_str(), 102))
{
MY_LOG_E("mms_s_init failed");
return -1;

View File

@ -1,6 +1,7 @@
#include "method.h"
#include "myIcp67.h"
#include "myLog.h"
#include "myFunc.h"
#include "myMd5.h"
#include "myCmd.h"
@ -9,6 +10,7 @@
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
@ -21,10 +23,21 @@
LOCAL std::vector<stru_dir_info> g_dir_info_vec;
LOCAL std::string g_file_path = "/mnt/RTU/test/file/";
LOCAL std::string get_base_path()
{
char proc_dir[512] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
LOG_E("func_get_process_self_dir failed, cannot get process directory, abort");
exit(1);
}
return std::string(proc_dir);
}
LOCAL std::string g_file_path = get_base_path() + "file/";
LOCAL std::string g_down_path = "DOWN";
LOCAL std::string g_zip_path = "/mnt/RTU/test/zip/";
LOCAL std::string g_zip_path = get_base_path() + "zip/";
LOCAL std::string g_file_name = "";
LOCAL std::string g_zip_name = "";

View File

@ -897,7 +897,7 @@ int self_ptl_do_signal_out(stru_app *p_app)
param.max = safeStringToFloat(p_ao->p_param->max);
param.step = safeStringToFloat(p_ao->p_param->step);
param.unit = p_ao->p_param->unit;
dc_param_metadata_store(p_ao->p_param->base.saddr, param);
// dc_param_metadata_store(p_ao->p_param->base.saddr, param);
ret |= dc_signal_ao(p_ao->p_param->base.saddr, p_ao->p_param->base.desc, p_ao->p_param->type, SIGNAL_CTRL_TYPE::SBO_NORMAL, p_ao->vec_p_data[0], p_ao->vec_p_default_data[0], self_ptl_signal_change_callback);
}
@ -918,7 +918,7 @@ int self_ptl_do_signal_out(stru_app *p_app)
param.max = safeStringToFloat(p_param->p_param->max);
param.step = safeStringToFloat(p_param->p_param->step);
param.unit = p_param->p_param->unit;
dc_param_metadata_store(p_param->p_param->base.saddr, param);
// dc_param_metadata_store(p_param->p_param->base.saddr, param);
ret |= dc_signal_param(p_param->p_param->base.saddr, p_param->p_param->base.desc, p_param->p_param->type,
SIGNAL_CTRL_TYPE::SBO_NORMAL, p_param->vec_p_data.data(), p_param->vec_p_default_data.data(), p_param->p_param->num, self_ptl_signal_change_callback);

View File

@ -1,6 +1,7 @@
#include "web_server.h"
#include "ws_method.h"
#include "myLog.h"
#include "myFunc.h"
#include "mongoose.h"
@ -83,9 +84,15 @@ LOCAL void *web_server_run(void *arg)
}
LOCAL void web_server_init()
LOCAL int web_server_init()
{
g_web_root = "/mnt/RTU/test/" + std::string(s_web_root);
char proc_dir[512] = {0};
if(0 != func_get_process_self_dir(proc_dir, sizeof(proc_dir)))
{
LOG_E("func_get_process_self_dir failed, cannot get process directory");
return -1;
}
g_web_root = std::string(proc_dir) + s_web_root;
mg_mgr_init(&mgr);
@ -94,12 +101,17 @@ LOCAL void web_server_init()
pthread_create(&thread_id, NULL, web_server_run, NULL);
LOG_I("web_server_init done");
return 0;
}
int app_web_server_init1(void *arg)
{
web_server_init();
if(0 != web_server_init())
{
LOG_E("web_server_init failed");
return -1;
}
return 0;
}

File diff suppressed because it is too large Load Diff