RTU_ALL_AI/src/public/libfunc/inc/myFunc.h

157 lines
4.5 KiB
C

/**
* @file myFunc.h
* @brief 基础工具函数 — 校验/字符串/时间/文件/进程/环境/IPC
*/
#ifndef _MY_FUNC_H_
#define _MY_FUNC_H_
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* ---- 系统时间 ---- */
typedef struct
{
uint32_t ms : 16;
uint32_t min : 6;
uint32_t : 2;
uint32_t hour: 5;
uint32_t : 3;
uint32_t day : 5;
uint32_t week: 3;
uint32_t month:4;
uint32_t : 4;
uint32_t year: 7;
uint32_t : 1;
} stru_time_sys;
/* ---- 日历时间 ---- */
typedef struct
{
time_t sec;
time_t usec;
} stru_time_cal;
/* ---- IPC 消息文本 ---- */
typedef struct
{
int qid;
int type;
int len;
char text[4096];
} stru_ipc_text;
/* ---- IPC 响应类型 ---- */
typedef enum
{
IPC_RESP_NONE = 1,
IPC_RESP_ONCE,
IPC_RESP_ALWAYS
} enum_ipc_resp;
/* ---- IPC 消息 ---- */
typedef struct
{
long id;
stru_ipc_text t;
} stru_ipc_msg;
/* ---- 文件描述头(256字节) ---- */
#pragma pack(1)
typedef struct
{
uint64_t flag;
char ver[16];
char author[16];
char modify[32];
uint16_t crc;
char desc[128];
char bak[54];
} stru_file_info;
#pragma pack()
/* ===== 校验 ===== */
uint8_t func_cal_sum_byte(const uint8_t *d, uint32_t n);
uint16_t func_cal_sum_word(const uint8_t *d, uint32_t n);
uint16_t func_cal_crc16(const uint8_t *d, uint32_t n);
uint32_t func_cal_crc32(const uint8_t *d, uint32_t n);
uint16_t func_cal_file_crc16(const char *path);
uint32_t func_cal_file_crc32(const char *path);
/* ===== 字符串转换 ===== */
uint8_t func_hex_ch(char c);
uint8_t func_dec_ch(char c);
int func_hex2dec(char *s);
int func_dec2dec(char *s);
int func_hex2buf(const char *s, char *dst, int *len);
char *func_dec2str(char *buf, int sz, int val, int width);
char *func_hex2str(char *buf, int sz, int val, int width);
char *func_float2str(char *buf, int sz, float f, int width);
char *func_buf2str(char *buf, int sz, const char *src, int n);
int func_str2argv(char *s, uint8_t *argc, char *argv[], uint8_t max);
/* ===== 时间 ===== */
char *func_time_sys2str(char *buf, int sz, stru_time_sys *t);
int func_str2time_sys(const char *s, stru_time_sys *t);
int func_get_time(void *t, int type);
int func_set_time(void *t, int type);
void func_time_sys2cal(stru_time_sys *src, stru_time_cal *dst);
void func_time_cal2sys(stru_time_cal *src, stru_time_sys *dst);
/* ===== 文件目录 ===== */
int func_dir_exist(const char *p);
int func_make_dirs(const char *p);
int func_del_dirs(const char *p);
int func_del_dirs_cmd(const char *p);
int func_file_exist(const char *p);
uint32_t func_file_size(const char *p);
int func_read_file(const char *p, char *buf, uint32_t cap, uint32_t *out);
uint8_t *func_read_file_alloc(const char *p, uint32_t *out);
int func_write_file(const char *p, const char *buf, uint32_t n);
int func_read_file_info(const char *p, stru_file_info *info, int check);
/* ===== 进程 ===== */
int func_proc_self_name(char *buf, uint32_t sz);
int func_proc_name_by_pid(int pid, char *buf, uint32_t sz);
int func_proc_pid(void);
int func_proc_pid_by_name(const char *name);
int func_proc_exist_by_pid(int pid);
int func_proc_exist_by_name(const char *name);
int func_proc_path_by_pid(int pid, char *buf, uint32_t sz);
int func_proc_self_path(char *buf, uint32_t sz);
int func_proc_self_dir(char *buf, uint32_t sz);
/* ===== 环境变量路径 ===== */
int func_get_work_path(char *buf, uint32_t sz);
int func_get_syscfg_path(char *buf, uint32_t sz);
int func_get_app_root(char *buf, uint32_t sz);
int func_get_his_root(char *buf, uint32_t sz);
int func_get_log_root(char *buf, uint32_t sz);
int func_get_dbc_root(char *buf, uint32_t sz);
int func_get_shell_path(char *buf, uint32_t sz);
int func_get_update_path(char *buf, uint32_t sz);
int func_get_app_path(const char *app, char *buf, uint32_t sz);
int func_get_app_cfg(const char *app, const char *ext, char *buf, uint32_t sz);
int func_get_app_his(const char *app, char *buf, uint32_t sz);
int func_get_app_log(const char *app, char *buf, uint32_t sz);
/* ===== IPC 消息队列 ===== */
int func_ipc_create_by_name(char *name, int *qid);
int func_ipc_create_by_pid(int pid, int *qid);
int func_ipc_get_by_name(const char *name, int *qid);
int func_ipc_get_by_pid(int pid, int *qid);
int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n);
int func_ipc_recv_buf(int qid, uint8_t *rx, uint32_t n);
int func_ipc_send_msg(int qid, stru_ipc_msg *m);
int func_ipc_recv_msg(int qid, stru_ipc_msg *m);
int func_ipc_delete(int qid);
#ifdef __cplusplus
}
#endif
#endif