refactor(libmodbus_m): MY_LOG_→LOG_ 统一日志宏 + 代码风格规范

- 全局替换 MY_LOG_I/MY_LOG_E/MY_LOG_W 为统一的 LOG_I/LOG_E/LOG_W
- get_base_path(): 对齐配置路径约定(WORK_PATH 优先 → 可执行文件目录
- 修复关键字空格(if(/for(/while(/switch( → if (/for (/while (/switch ()
- 花括号/空行按 Allman 风格规范
This commit is contained in:
ypc 2026-07-08 17:35:42 +08:00
parent 16b697f9c4
commit a0158a6cbf
1 changed files with 164 additions and 154 deletions

View File

@ -20,13 +20,23 @@ LOCAL std::vector<stru_modbus_m_write_req> g_write_queue;
LOCAL std::string get_base_path()
{
char proc_dir[512] = {0};
if(0 != func_proc_self_dir(proc_dir, sizeof(proc_dir)))
char buf[512] = {0};
/* 优先使用 WORK_PATH 环境变量RK3568 装置运行时设置) */
if (0 == func_get_work_path(buf, sizeof(buf)))
{
MY_LOG_E("func_get_process_self_dir failed");
return std::string(buf) + "/";
}
/* fallback: 从可执行文件路径推导(本地调试用) */
if (0 != func_proc_self_dir(buf, sizeof(buf)))
{
LOG_E("func_proc_self_dir failed");
return "";
}
return std::string(proc_dir);
return std::string(buf) + "/";
}
// ==================== 数据类型映射 ====================
@ -83,14 +93,14 @@ LOCAL int parse_channels(XMLElement *p_root)
ch.connected = false;
g_modbus_m_cfg.channels.push_back(ch);
MY_LOG_I("channel %d: mode=%s, host=%s:%d", ch.id, ch.mode.c_str(), ch.host.c_str(), ch.port);
LOG_I("channel %d: mode=%s, host=%s:%d", ch.id, ch.mode.c_str(), ch.host.c_str(), ch.port);
p_ch = p_ch->NextSiblingElement("Channel");
}
if (g_modbus_m_cfg.channels.empty())
{
MY_LOG_E("no channel configured");
LOG_E("no channel configured");
return -1;
}
return 0;
@ -139,7 +149,7 @@ LOCAL int parse_point_item(XMLElement *p_item, std::vector<stru_modbus_m_item> &
}
if (!ch_found)
{
MY_LOG_E("item no=%d: channel %d not found", item.no, item.channel);
LOG_E("item no=%d: channel %d not found", item.no, item.channel);
return -1;
}
@ -148,7 +158,7 @@ LOCAL int parse_point_item(XMLElement *p_item, std::vector<stru_modbus_m_item> &
item.p_val = dc_create_data(local_type);
if (NULL == item.p_val)
{
MY_LOG_E("dc_create_data_ptr_by_type failed, type=%d", local_type);
LOG_E("dc_create_data_ptr_by_type failed, type=%d", local_type);
return -1;
}
@ -161,7 +171,7 @@ LOCAL int parse_points(XMLElement *p_root)
XMLElement *p_point = p_root->FirstChildElement("Point");
if (NULL == p_point)
{
MY_LOG_E("Point not found");
LOG_E("Point not found");
return -1;
}
@ -181,7 +191,7 @@ LOCAL int parse_points(XMLElement *p_root)
}
p_item = p_item->NextSiblingElement("Item");
}
MY_LOG_I("parsed %zu st items", g_modbus_m_cfg.point.st_vec.size());
LOG_I("parsed %zu st items", g_modbus_m_cfg.point.st_vec.size());
}
// 解析 Mx遥测
@ -197,7 +207,7 @@ LOCAL int parse_points(XMLElement *p_root)
}
p_item = p_item->NextSiblingElement("Item");
}
MY_LOG_I("parsed %zu mx items", g_modbus_m_cfg.point.mx_vec.size());
LOG_I("parsed %zu mx items", g_modbus_m_cfg.point.mx_vec.size());
}
// 解析 Co遥控
@ -213,7 +223,7 @@ LOCAL int parse_points(XMLElement *p_root)
}
p_item = p_item->NextSiblingElement("Item");
}
MY_LOG_I("parsed %zu co items", g_modbus_m_cfg.point.co_vec.size());
LOG_I("parsed %zu co items", g_modbus_m_cfg.point.co_vec.size());
}
// 解析 Ao参数
@ -229,7 +239,7 @@ LOCAL int parse_points(XMLElement *p_root)
}
p_item = p_item->NextSiblingElement("Item");
}
MY_LOG_I("parsed %zu ao items", g_modbus_m_cfg.point.ao_vec.size());
LOG_I("parsed %zu ao items", g_modbus_m_cfg.point.ao_vec.size());
}
return 0;
@ -240,30 +250,30 @@ int modbus_m_cfg_parse(const std::string &path)
XMLDocument doc;
if (XML_SUCCESS != doc.LoadFile(path.c_str()))
{
MY_LOG_E("load xml failed: %s", path.c_str());
LOG_E("load xml failed: %s", path.c_str());
return -1;
}
XMLElement *p_root = doc.RootElement();
if (NULL == p_root)
{
MY_LOG_E("root element not found");
LOG_E("root element not found");
return -1;
}
if (0 != parse_channels(p_root))
{
MY_LOG_E("parse channels failed");
LOG_E("parse channels failed");
return -1;
}
if (0 != parse_points(p_root))
{
MY_LOG_E("parse points failed");
LOG_E("parse points failed");
return -1;
}
MY_LOG_I("modbus_m cfg parsed: %zu channels, st=%zu, mx=%zu, co=%zu, ao=%zu",
LOG_I("modbus_m cfg parsed: %zu channels, st=%zu, mx=%zu, co=%zu, ao=%zu",
g_modbus_m_cfg.channels.size(),
g_modbus_m_cfg.point.st_vec.size(),
g_modbus_m_cfg.point.mx_vec.size(),
@ -297,7 +307,7 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch)
p_ch->ctx = modbus_new_tcp(p_ch->host.c_str(), p_ch->port);
if (NULL == p_ch->ctx)
{
MY_LOG_E("modbus_new_tcp failed: %s:%d", p_ch->host.c_str(), p_ch->port);
LOG_E("modbus_new_tcp failed: %s:%d", p_ch->host.c_str(), p_ch->port);
return -1;
}
// TCP 模式下设置默认从站地址
@ -308,13 +318,13 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch)
p_ch->ctx = modbus_new_rtu(p_ch->device.c_str(), p_ch->baud, p_ch->parity, p_ch->data_bit, p_ch->stop_bit);
if (NULL == p_ch->ctx)
{
MY_LOG_E("modbus_new_rtu failed: %s", p_ch->device.c_str());
LOG_E("modbus_new_rtu failed: %s", p_ch->device.c_str());
return -1;
}
}
else
{
MY_LOG_E("unknown mode: %s", p_ch->mode.c_str());
LOG_E("unknown mode: %s", p_ch->mode.c_str());
return -1;
}
@ -327,7 +337,7 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch)
// 建立连接
if (0 != modbus_connect(p_ch->ctx))
{
MY_LOG_E("modbus_connect failed: ch=%d, mode=%s", p_ch->id, p_ch->mode.c_str());
LOG_E("modbus_connect failed: ch=%d, mode=%s", p_ch->id, p_ch->mode.c_str());
modbus_free(p_ch->ctx);
p_ch->ctx = NULL;
p_ch->connected = false;
@ -335,7 +345,7 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch)
}
p_ch->connected = true;
MY_LOG_I("channel %d connected: mode=%s", p_ch->id, p_ch->mode.c_str());
LOG_I("channel %d connected: mode=%s", p_ch->id, p_ch->mode.c_str());
return 0;
}
@ -736,7 +746,7 @@ LOCAL void modbus_m_signal_co_change_callback(const char *saddr, dc_ctrl_step_t
}
enqueue_write(req);
MY_LOG_I("co write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value);
LOG_I("co write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value);
break;
}
}
@ -775,7 +785,7 @@ LOCAL void modbus_m_signal_ao_change_callback(const char *saddr, dc_ctrl_step_t
}
enqueue_write(req);
MY_LOG_I("ao write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value);
LOG_I("ao write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value);
break;
}
}
@ -794,7 +804,7 @@ LOCAL int signal_register_items(const std::vector<stru_modbus_m_item> &items, co
{
if (0 != dc_signal_out(item.saddr.c_str(), item.desc.c_str(), local_type, item.p_val, MODULE_MODBUS_M))
{
MY_LOG_E("dc_signal_out failed: %s", item.saddr.c_str());
LOG_E("dc_signal_out failed: %s", item.saddr.c_str());
return -1;
}
}
@ -802,7 +812,7 @@ LOCAL int signal_register_items(const std::vector<stru_modbus_m_item> &items, co
{
if (0 != dc_signal_out(item.saddr.c_str(), item.desc.c_str(), local_type, item.p_val, MODULE_MODBUS_M))
{
MY_LOG_E("dc_signal_out failed: %s", item.saddr.c_str());
LOG_E("dc_signal_out failed: %s", item.saddr.c_str());
return -1;
}
}
@ -810,7 +820,7 @@ LOCAL int signal_register_items(const std::vector<stru_modbus_m_item> &items, co
{
if (0 != dc_signal_yk(item.saddr.c_str(), item.desc.c_str(), local_type, DC_CTRL_DIRECT, item.p_val, modbus_m_signal_co_change_callback, MODULE_MODBUS_M))
{
MY_LOG_E("dc_signal_yk failed: %s", item.saddr.c_str());
LOG_E("dc_signal_yk failed: %s", item.saddr.c_str());
return -1;
}
}
@ -818,7 +828,7 @@ LOCAL int signal_register_items(const std::vector<stru_modbus_m_item> &items, co
{
if (0 != dc_signal_ao(item.saddr.c_str(), item.desc.c_str(), local_type, DC_CTRL_DIRECT, item.p_val, modbus_m_signal_ao_change_callback, MODULE_MODBUS_M))
{
MY_LOG_E("dc_signal_ao failed: %s", item.saddr.c_str());
LOG_E("dc_signal_ao failed: %s", item.saddr.c_str());
return -1;
}
}
@ -846,7 +856,7 @@ LOCAL int signals_init()
return -1;
}
MY_LOG_I("signals registered: st=%zu, mx=%zu, co=%zu, ao=%zu",
LOG_I("signals registered: st=%zu, mx=%zu, co=%zu, ao=%zu",
g_modbus_m_cfg.point.st_vec.size(),
g_modbus_m_cfg.point.mx_vec.size(),
g_modbus_m_cfg.point.co_vec.size(),
@ -862,13 +872,13 @@ int modbus_m_init()
{
if (0 != signals_init())
{
MY_LOG_E("signals_init failed");
LOG_E("signals_init failed");
return -1;
}
if (0 != channels_init())
{
MY_LOG_E("channels_init failed");
LOG_E("channels_init failed");
// 部分通道失败也可继续
}
@ -942,7 +952,7 @@ int app_modbus_m_init1(void *arg)
std::string base_path = get_base_path();
if (base_path.empty())
{
MY_LOG_E("get_base_path failed");
LOG_E("get_base_path failed");
return -1;
}
g_modbus_m_base_path = base_path + "config/MODBUS/";
@ -950,7 +960,7 @@ int app_modbus_m_init1(void *arg)
std::string cfg_path = g_modbus_m_base_path + "modbus_m.xml";
if (0 != modbus_m_cfg_parse(cfg_path))
{
MY_LOG_E("modbus_m_cfg_parse failed: %s", cfg_path.c_str());
LOG_E("modbus_m_cfg_parse failed: %s", cfg_path.c_str());
return -1;
}
@ -985,7 +995,7 @@ void *app_modbus_m(void *arg)
// 预构建轮询分组
build_poll_groups(g_modbus_m_cfg.point.st_vec, st_groups);
build_poll_groups(g_modbus_m_cfg.point.mx_vec, mx_groups);
MY_LOG_I("poll groups: st=%zu, mx=%zu", st_groups.size(), mx_groups.size());
LOG_I("poll groups: st=%zu, mx=%zu", st_groups.size(), mx_groups.size());
while (1)
{