common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef COMMON_H
  2. #define COMMON_H
  3. /**
  4. * @file common.h
  5. * @brief 公共定义与常量声明
  6. *
  7. * 本文件定义了整个ISUP平台客户端项目的公共常量、宏定义、
  8. * 通用数据结构和对齐指令。
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <pthread.h>
  15. #include <signal.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include <stdarg.h>
  23. /* 版本信息 */
  24. #define PROJECT_NAME "Hikvision ISUP Client"
  25. #define PROJECT_VERSION "1.0.0"
  26. #define PROJECT_BUILD_DATE __DATE__ " " __TIME__
  27. /* 日志级别 */
  28. typedef enum {
  29. LOG_LEVEL_DEBUG = 0,
  30. LOG_LEVEL_INFO,
  31. LOG_LEVEL_WARN,
  32. LOG_LEVEL_ERROR,
  33. LOG_LEVEL_FATAL
  34. } LogLevel;
  35. /* 日志宏定义 */
  36. #define LOG_TAG "ISUP"
  37. #define LOG_PRINT(level, fmt, ...) \
  38. do { \
  39. fprintf(stderr, "[%s] [%s] [%s:%d:%s()] " fmt "\n", \
  40. LOG_TAG, \
  41. (level == LOG_LEVEL_DEBUG ? "DEBUG" : \
  42. level == LOG_LEVEL_INFO ? "INFO" : \
  43. level == LOG_LEVEL_WARN ? "WARN" : \
  44. level == LOG_LEVEL_ERROR ? "ERROR" : "FATAL"), \
  45. __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
  46. } while(0)
  47. #define LOG_DEBUG(fmt, ...) LOG_PRINT(LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
  48. #define LOG_INFO(fmt, ...) LOG_PRINT(LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
  49. #define LOG_WARN(fmt, ...) LOG_PRINT(LOG_LEVEL_WARN, fmt, ##__VA_ARGS__)
  50. #define LOG_ERROR(fmt, ...) LOG_PRINT(LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
  51. #define LOG_FATAL(fmt, ...) LOG_PRINT(LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__)
  52. /* 缓冲区大小 */
  53. #define MAX_BUF_SIZE 65536
  54. #define MAX_IP_SIZE 64
  55. #define MAX_PATH_SIZE 256
  56. #define MAX_DEVICE_ID 64
  57. #define MAX_CHANNEL_NUM 32
  58. #define MAX_ALARM_NUM 128
  59. #define MAX_PLAYBACK_NUM 16
  60. #define MAX_STREAM_NUM 32
  61. /* 默认端口配置 */
  62. #define DEFAULT_CMS_PORT 7031 /* CMS注册服务器端口 */
  63. #define DEFAULT_SMS_PORT 7660 /* SMS流媒体服务器端口 */
  64. #define DEFAULT_ALARM_PORT 9000 /* 报警服务器端口 */
  65. #define DEFAULT_RTMP_PORT 1935 /* RTMP推流端口 */
  66. #define DEFAULT_HTTP_PORT 8080 /* HTTP管理接口端口 */
  67. /* ISUP协议相关 */
  68. #define ISUP_PROTO_VERSION "ISUP5.0"
  69. #define REG_RETRY_INTERVAL 5 /* 注册重试间隔(秒) */
  70. #define HEARTBEAT_INTERVAL 30 /* 心跳间隔(秒) */
  71. #define STREAM_TIMEOUT 30 /* 流超时(秒) */
  72. /* 模拟图片配置 */
  73. #define SIM_IMAGE_DIR "./sim_images"
  74. #define SIM_IMAGE_WIDTH 1920
  75. #define SIM_IMAGE_HEIGHT 1080
  76. #define SIM_FPS 25
  77. #define SIM_BITRATE 4096000 /* 4Mbps */
  78. /* 状态码 */
  79. #define SUCCESS 0
  80. #define FAILURE -1
  81. #define NOT_FOUND -2
  82. #define INVALID_PARAM -3
  83. #define TIMEOUT -4
  84. #define UNAUTHORIZED -5
  85. /* 设备状态 */
  86. typedef enum {
  87. DEVICE_STATUS_OFFLINE = 0,
  88. DEVICE_STATUS_ONLINE,
  89. DEVICE_STATUS_REGISTERS,
  90. DEVICE_STATUS_STREAMING,
  91. DEVICE_STATUS_PLAYBACK
  92. } DeviceStatus;
  93. /* 日志函数指针类型 */
  94. typedef void (*LogCallback)(LogLevel level, const char* tag, const char* fmt, va_list args);
  95. /* 全局日志回调 */
  96. extern LogCallback g_log_callback;
  97. /**
  98. * @brief 设置日志回调函数
  99. * @param cb 日志回调函数指针
  100. */
  101. void set_log_callback(LogCallback cb);
  102. /**
  103. * @brief 格式化时间戳字符串
  104. * @param buf 输出缓冲区
  105. * @param bufsize 缓冲区大小
  106. * @param fmt 时间格式字符串(strftime格式)
  107. * @return 格式化后的字符串指针
  108. */
  109. char* get_timestamp(char* buf, size_t bufsize, const char* fmt);
  110. /**
  111. * @brief 字符串转小写
  112. * @param str 输入字符串
  113. * @return 转换后的字符串指针
  114. */
  115. char* str_tolower(char* str);
  116. /**
  117. * @brief 安全字符串复制
  118. * @param dest 目标缓冲区
  119. * @param src 源字符串
  120. * @param size 目标缓冲区大小
  121. * @return 复制的字符数
  122. */
  123. size_t safe_strcpy(char* dest, const char* src, size_t size);
  124. #endif /* COMMON_H */