// // ****************************************************************** // // /\ /| @File ProtocolFactory.cpp // // \ V/ @Brief // // | "") @Author lijinwen, ghz005@uni-trend.com.cn // // / | @Creation 2024-5-6 // // / \\ @Modified 2024-5-13 // // *(__\_\ // // ****************************************************************** #include "ProtocolFactory.h" #include "BaseHelper/Logger.h" #include "ProtocolRS232/RS232Decode.h" #include "ProtocolUSB/UsbDecode.h" namespace Protocol { bool ProtocolFactory::CreateProtocol(SerialProtocolType protocolType) { WriteLog(LogLevel::Level1, "CreateProtocol Start"); switch (protocolType) // NOLINT(clang-diagnostic-switch-enum) { case SerialProtocolType::RS232: { glDecoderPtr = std::make_unique(); return true; } case SerialProtocolType::USB: { glDecoderPtr = std::make_unique(); return true; } //todo ... 添加其他协议的创建逻辑 ... default: //throw std::runtime_error("Unknown protocol"); WriteLog(LogLevel::Level1, "CreateProtocol => Unknown protocol"); return false; } } void ProtocolFactory::GetVersion(uint8_t* version) { #ifdef DLL_VERSION // 将字符串转换为字符数组 constexpr char versionString[] = DLL_VERSION; // 遍历字符串,将其转换为字节数组 for (size_t i = 0; i < sizeof(versionString); ++i) { version[i] = static_cast(versionString[i]); } #else version[0] = 0; version[1] = 0; version[2] = 0; #endif } ProtocolDecodeBase* ProtocolFactory::GetDecoderPtrExport() { return glDecoderPtr.get(); } //========== 外部调用 =============// ProtocolDecodeBase* CreateProtocolExport(SerialProtocolType protocolType, LogLevel level) { if (!ProtocolFactory::CreateProtocol(protocolType)) { return nullptr; } if (!CommonHelper::EnumIsDefined(static_cast(level))) { Logger::LogLevel = LogLevel::All; } else { Logger::LogLevel = level; } return ProtocolFactory::GetDecoderPtrExport(); } void GetVersionExport(uint8_t* version) { ProtocolFactory::GetVersion(version); } void ReleaseExport() { ProtocolFactory::ReleaseDecoder(); } }