#ifndef PCCU_FIELD_CODEC_H #define PCCU_FIELD_CODEC_H #include #include #include #include #include namespace ccu { //============================================================================ // FieldCodec:小端序(Little-Endian)编解码原语。 // // 两条协议(FC 协议 / PM 协议)均采用: // - 多字节数据小端序 // - 域起始符 0x40 0x40 // - 域字节数 = 整个协议包的数据长度 // 本类提供对裸缓冲区的字段读写,不关心具体业务含义。 //============================================================================ class FieldCodec { public: // 在 dst 偏移 offset 处写入单字节 static void putU8(std::vector& dst, size_t offset, uint8_t v) { dst[offset] = v; } // 小端写入 2 字节 static void putU16(std::vector& dst, size_t offset, uint16_t v) { dst[offset] = static_cast(v & 0xFF); dst[offset + 1] = static_cast((v >> 8) & 0xFF); } // 小端写入 4 字节 static void putU32(std::vector& dst, size_t offset, uint32_t v) { dst[offset] = static_cast(v & 0xFF); dst[offset + 1] = static_cast((v >> 8) & 0xFF); dst[offset + 2] = static_cast((v >> 16) & 0xFF); dst[offset + 3] = static_cast((v >> 24) & 0xFF); } static uint8_t getU8 (const std::vector& src, size_t offset) { return src[offset]; } static uint16_t getU16(const std::vector& src, size_t offset) { return static_cast(src[offset]) | (static_cast(src[offset + 1]) << 8); } static uint32_t getU32(const std::vector& src, size_t offset) { return static_cast(src[offset]) | (static_cast(src[offset + 1]) << 8) | (static_cast(src[offset + 2]) << 16) | (static_cast(src[offset + 3]) << 24); } }; } // namespace ccu #endif // PCCU_FIELD_CODEC_H