RTU_ALL_AI/docs/public/libfunc/API-libfunc.md

154 lines
5.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# libfunc — 基础工具函数 API 参考
## 模块概述
libfunc 提供嵌入式开发中最常用的 65 个基础工具函数,分为 7 大类:
1. 校验计算
2. 字符串转换
3. 时间处理
4. 文件目录操作
5. 进程管理
6. 环境变量路径
7. IPC 消息队列
## 一、校验计算 (6 函数)
| 函数 | 说明 |
|------|------|
| `func_cal_sum_byte(d, n)` | 8 位累加校验和 |
| `func_cal_sum_word(d, n)` | 16 位累加校验和 |
| `func_cal_crc16(d, n)` | CRC16 (Modbus 多项式 0xA001) |
| `func_cal_crc32(d, n)` | CRC32 (标准多项式) |
| `func_cal_file_crc16(path)` | 文件 CRC164K 缓冲区流式计算) |
| `func_cal_file_crc32(path)` | 文件 CRC32 |
**示例**:
```c
uint8_t d[] = { 0x01, 0x02, 0x03 };
uint16_t crc = func_cal_crc16(d, 3);
printf("CRC16 = 0x%04X\n", crc); // 0x6161
```
## 二、字符串转换 (10 函数)
| 函数 | 参数 | 说明 |
|------|------|------|
| `func_hex_ch(c)` | char c | 十六进制字符 → 数值 |
| `func_dec_ch(c)` | char c | 十进制字符 → 数值 |
| `func_hex2dec(s)` | char *s | "FF" → 255 |
| `func_dec2dec(s)` | char *s | "255" → 255支持负号 |
| `func_hex2buf(s, dst, len)` | 3 参数 | 十六进制字符串 → 字节缓冲 |
| `func_dec2str(buf, sz, val, w)` | 4 参数 | int → 十进制字符串 |
| `func_hex2str(buf, sz, val, w)` | 4 参数 | int → 十六进制字符串 |
| `func_float2str(buf, sz, f, w)` | 4 参数 | float → 字符串 |
| `func_buf2str(buf, sz, src, n)` | 4 参数 | 字节数组 → 十六进制字符串 |
| `func_str2argv(s, argc, argv, max)` | 4 参数 | 按空格分割为 argc/argv |
**示例**:
```c
char buf[64];
func_dec2str(buf, sizeof(buf), 255, 4);
printf("%s\n", buf); // "0255"
func_hex2str(buf, sizeof(buf), 255, 0);
printf("%s\n", buf); // "FF"
```
## 三、时间处理 (6 函数)
| 函数 | 说明 |
|------|------|
| `func_time_sys2str(buf, sz, ts)` | 系统时间 → "YYYY-MM-DD HH:MM:SS.mmm" |
| `func_str2time_sys(s, ts)` | 字符串 → 系统时间 |
| `func_get_time(t, type)` | 获取当前时间type: 1=sys, 2=cal |
| `func_set_time(t, type)` | 设置系统时间(需 root |
| `func_time_sys2cal(src, dst)` | 系统时间 → 日历时间 |
| `func_time_cal2sys(src, dst)` | 日历时间 → 系统时间 |
## 四、文件目录操作 (11 函数)
| 函数 | 说明 |
|------|------|
| `func_dir_exist(p)` | 目录是否存在 |
| `func_make_dirs(p)` | 递归创建目录 |
| `func_del_dirs(p)` | 递归删除目录 |
| `func_del_dirs_cmd(p)` | 系统命令删除rm -rf |
| `func_file_exist(p)` | 文件是否存在 |
| `func_file_size(p)` | 获取文件大小(字节) |
| `func_read_file(p, buf, cap, out)` | 读文件到预分配缓冲 |
| `func_read_file_alloc(p, out)` | 读文件到动态分配缓冲 |
| `func_write_file(p, buf, n)` | 写缓冲到文件 |
| `func_read_file_info(p, info, check)` | 读结构化文件头256B, CRC 校验) |
**示例**:
```c
// 创建目录
func_make_dirs("/tmp/myapp/config");
// 读取文件
uint32_t size = 0;
uint8_t *buf = func_read_file_alloc("/tmp/test.txt", &size);
if (buf)
{
printf("Read %u bytes: %s\n", size, buf);
free(buf);
}
```
## 五、进程管理 (9 函数)
| 函数 | 说明 |
|------|------|
| `func_proc_self_name(buf, sz)` | 获取本进程名(读 /proc/self/comm |
| `func_proc_name_by_pid(pid, buf, sz)` | PID → 进程名 |
| `func_proc_pid()` | 获取本进程 PID |
| `func_proc_pid_by_name(name)` | 进程名 → PID |
| `func_proc_exist_by_pid(pid)` | 按 PID 判断进程存在 |
| `func_proc_exist_by_name(name)` | 按名称判断进程存在 |
| `func_proc_path_by_pid(pid, buf, sz)` | PID → 可执行文件路径 |
| `func_proc_self_path(buf, sz)` | 本进程路径 |
| `func_proc_self_dir(buf, sz)` | 本进程目录 |
## 六、环境变量路径 (12 函数)
| 函数 | 环境变量 |
|------|---------|
| `func_get_work_path(..)` | WORK_PATH |
| `func_get_syscfg_path(..)` | SYS_CFG_PATH |
| `func_get_app_root(..)` | APP_ROOT_PATH |
| `func_get_his_root(..)` | HIS_ROOT_PATH |
| `func_get_log_root(..)` | LOG_ROOT_PATH |
| `func_get_dbc_root(..)` | DBC_ROOT_PATH |
| `func_get_shell_path(..)` | SHELL_PATH |
| `func_get_update_path(..)` | UPDATE_PATH |
| `func_get_app_path(app, ..)` | 拼接: APP_ROOT/app |
| `func_get_app_cfg(app, ext, ..)` | 拼接: APP_ROOT/app/ext |
| `func_get_app_his(app, ..)` | 拼接: HIS_ROOT/app |
| `func_get_app_log(app, ..)` | 拼接: LOG_ROOT/app |
**示例**:
```c
char path[256];
func_get_app_cfg("RTU", "config.json", path, sizeof(path));
printf("Config path: %s\n", path); // /opt/RTU/config.json
```
## 七、IPC 消息队列 (9 函数)
| 函数 | 说明 |
|------|------|
| `func_ipc_create_by_name(name, qid)` | 按名称创建 System V 消息队列 |
| `func_ipc_create_by_pid(pid, qid)` | 按 PID 创建 |
| `func_ipc_get_by_name(name, qid)` | 按名称获取 |
| `func_ipc_get_by_pid(pid, qid)` | 按 PID 获取 |
| `func_ipc_send_buf(qid, tx, n)` | 发送原始字节 |
| `func_ipc_recv_buf(qid, rx, n)` | 接收原始字节 |
| `func_ipc_send_msg(qid, msg)` | 发送结构体消息 |
| `func_ipc_recv_msg(qid, msg)` | 接收结构体消息 |
| `func_ipc_delete(qid)` | 删除队列 |
## 依赖关系
- **依赖**: myBase.h
- **被依赖**: liblog (进程路径), 所有需要 CRC/文件/进程操作的模块