1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- // // ******************************************************************
- // // /\ /| @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<ProtocolRS232::RS232Decode>();
- return true;
- }
- case SerialProtocolType::USB:
- {
- glDecoderPtr = std::make_unique<ProtocolUsb::UsbDecode>();
- 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<unsigned char>(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<LogLevel>(static_cast<int>(level)))
- {
- Logger::LogLevel = LogLevel::All;
- }
- else
- {
- Logger::LogLevel = level;
- }
- return ProtocolFactory::GetDecoderPtrExport();
- }
- void GetVersionExport(uint8_t* version)
- {
- ProtocolFactory::GetVersion(version);
- }
- void ReleaseExport()
- {
- ProtocolFactory::ReleaseDecoder();
- }
- }
|