/** * @file myFunc.h * @brief 基础工具函数 — 校验/字符串/时间/文件/进程/环境/IPC */ #ifndef _MY_FUNC_H_ #define _MY_FUNC_H_ #include #include #include #ifdef __cplusplus extern "C" { #endif /* ---- 时间结构 ---- */ typedef struct { uint32_t ms:16, min:6, :2, hour:5, :3, day:5, week:3, month:4, :4, year:7, :1; } time_sys_t; typedef struct { time_t sec; time_t usec; } time_cal_t; /* ---- IPC ---- */ typedef struct { int qid, type, len; char text[4096]; } ipc_text_t; typedef struct { long id; ipc_text_t t; } ipc_msg_t; enum { IPC_RESP_NONE=1, IPC_RESP_ONCE, IPC_RESP_ALWAYS }; /* ---- 文件描述头(256字节) ---- */ #pragma pack(1) typedef struct { uint64_t flag; /* 魔数 0xA55AA55AA55AA55A */ char ver[16], author[16], modify[32]; uint16_t crc; char desc[128], bak[54]; } file_info_t; #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, time_sys_t *t); int func_str2time_sys(const char *s, time_sys_t *t); int func_get_time(void *t, int type); /* type: 1=sys 2=cal */ int func_set_time(void *t, int type); void func_time_sys2cal(time_sys_t *src, time_cal_t *dst); void func_time_cal2sys(time_cal_t *src, time_sys_t *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, file_info_t *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, ipc_msg_t *m); int func_ipc_recv_msg(int qid, ipc_msg_t *m); int func_ipc_delete(int qid); #ifdef __cplusplus } #endif #endif