ProtocolFactory.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // // ******************************************************************
  2. // // /\ /| @File ProtocolFactory.cpp
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-5-6
  6. // // / \\ @Modified 2024-5-13
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #include "ProtocolFactory.h"
  10. #include "BaseHelper/Logger.h"
  11. #include "ProtocolRS232/RS232Decode.h"
  12. #include "ProtocolUSB/UsbDecode.h"
  13. namespace Protocol
  14. {
  15. bool ProtocolFactory::CreateProtocol(SerialProtocolType protocolType)
  16. {
  17. WriteLog(LogLevel::Level1, "CreateProtocol Start");
  18. switch (protocolType) // NOLINT(clang-diagnostic-switch-enum)
  19. {
  20. case SerialProtocolType::RS232:
  21. {
  22. glDecoderPtr = std::make_unique<ProtocolRS232::RS232Decode>();
  23. return true;
  24. }
  25. case SerialProtocolType::USB:
  26. {
  27. glDecoderPtr = std::make_unique<ProtocolUsb::UsbDecode>();
  28. return true;
  29. }
  30. //todo ... 添加其他协议的创建逻辑 ...
  31. default:
  32. //throw std::runtime_error("Unknown protocol");
  33. WriteLog(LogLevel::Level1, "CreateProtocol => Unknown protocol");
  34. return false;
  35. }
  36. }
  37. void ProtocolFactory::GetVersion(uint8_t* version)
  38. {
  39. #ifdef DLL_VERSION
  40. // 将字符串转换为字符数组
  41. constexpr char versionString[] = DLL_VERSION;
  42. // 遍历字符串,将其转换为字节数组
  43. for (size_t i = 0; i < sizeof(versionString); ++i)
  44. {
  45. version[i] = static_cast<unsigned char>(versionString[i]);
  46. }
  47. #else
  48. version[0] = 0;
  49. version[1] = 0;
  50. version[2] = 0;
  51. #endif
  52. }
  53. ProtocolDecodeBase* ProtocolFactory::GetDecoderPtrExport()
  54. {
  55. return glDecoderPtr.get();
  56. }
  57. //========== 外部调用 =============//
  58. ProtocolDecodeBase* CreateProtocolExport(SerialProtocolType protocolType, LogLevel level)
  59. {
  60. if (!ProtocolFactory::CreateProtocol(protocolType))
  61. {
  62. return nullptr;
  63. }
  64. if (!CommonHelper::EnumIsDefined<LogLevel>(static_cast<int>(level)))
  65. {
  66. Logger::LogLevel = LogLevel::All;
  67. }
  68. else
  69. {
  70. Logger::LogLevel = level;
  71. }
  72. return ProtocolFactory::GetDecoderPtrExport();
  73. }
  74. void GetVersionExport(uint8_t* version)
  75. {
  76. ProtocolFactory::GetVersion(version);
  77. }
  78. void ReleaseExport()
  79. {
  80. ProtocolFactory::ReleaseDecoder();
  81. }
  82. }