dsi_silabs_library.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. #include "types.h"
  9. #if defined(DSI_TYPES_WINDOWS)
  10. #include "dsi_silabs_library.hpp"
  11. #include <memory>
  12. using namespace std;
  13. #define LIBRARY_NAME "DSI_SiUSBXp_3_1.DLL"
  14. //Static variable initializations
  15. std::auto_ptr<SiLabsLibrary> SiLabsLibrary::clAutoInstance(NULL);
  16. BOOL SiLabsLibrary::bStaticSet = FALSE;
  17. //return an auto_ptr?
  18. BOOL SiLabsLibrary::Load(auto_ptr<const SiLabsLibrary>& clAutoLibrary_) //!!Should we lose the dependency on auto_ptr and just return the pointer (and let the user make their own auto_ptr)?
  19. {
  20. try
  21. {
  22. clAutoLibrary_.reset(new SiLabsLibrary());
  23. }
  24. catch(...)
  25. {
  26. clAutoLibrary_.reset(NULL);
  27. return FALSE;
  28. }
  29. return TRUE;
  30. }
  31. SiLabsLibrary::SiLabsLibrary() //throw(SiLabsError::Enum&)
  32. :
  33. Open(NULL),
  34. GetNumDevices(NULL),
  35. Close(NULL),
  36. Read(NULL),
  37. Write(NULL),
  38. SetTimeouts(NULL),
  39. GetTimeouts(NULL),
  40. FlushBuffers(NULL),
  41. CheckRxQueue(NULL),
  42. SetBaudRate(NULL),
  43. SetLineControl(NULL),
  44. SetFlowControl(NULL),
  45. GetProductString(NULL),
  46. hLibHandle(NULL)
  47. {
  48. //Check static instance
  49. if(bStaticSet == FALSE)
  50. {
  51. bStaticSet = TRUE;
  52. try
  53. {
  54. clAutoInstance.reset(new SiLabsLibrary());
  55. }
  56. catch(...)
  57. {
  58. bStaticSet = FALSE;
  59. throw;
  60. }
  61. }
  62. //load library
  63. SiLabsError::Enum ret = LoadFunctions();
  64. if(ret != SiLabsError::NONE)
  65. throw(ret);
  66. return;
  67. }
  68. SiLabsLibrary::~SiLabsLibrary() throw()
  69. {
  70. FreeFunctions();
  71. }
  72. ///////////////////////////////////////////////////////////////////////
  73. // Loads USB interface functions from the DLLs.
  74. ///////////////////////////////////////////////////////////////////////
  75. SiLabsError::Enum SiLabsLibrary::LoadFunctions()
  76. {
  77. hLibHandle = LoadLibrary(LIBRARY_NAME);
  78. if(hLibHandle == NULL)
  79. return SiLabsError::NO_LIBRARY;
  80. BOOL bStatus = TRUE;
  81. Open = (Open_t)GetProcAddress(hLibHandle, "SI_Open");
  82. if(Open == NULL)
  83. bStatus = FALSE;
  84. GetNumDevices = (GetNumDevices_t)GetProcAddress(hLibHandle, "SI_GetNumDevices");
  85. if(GetNumDevices == NULL)
  86. bStatus = FALSE;
  87. Close = (Close_t)GetProcAddress(hLibHandle, "SI_Close");
  88. if(Close == NULL)
  89. bStatus = FALSE;
  90. Read = (Read_t)GetProcAddress(hLibHandle, "SI_Read");
  91. if(Read == NULL)
  92. bStatus = FALSE;
  93. Write = (Write_t)GetProcAddress(hLibHandle, "SI_Write");
  94. if(Write == NULL)
  95. bStatus = FALSE;
  96. SetTimeouts = (SetTimeouts_t)GetProcAddress(hLibHandle, "SI_SetTimeouts");
  97. if(SetTimeouts == NULL)
  98. bStatus = FALSE;
  99. GetTimeouts = (GetTimeouts_t)GetProcAddress(hLibHandle, "SI_GetTimeouts");
  100. if(GetTimeouts == NULL)
  101. bStatus = FALSE;
  102. SetBaudRate = (SetBaudRate_t)GetProcAddress(hLibHandle, "SI_SetBaudRate");
  103. if(SetBaudRate == NULL)
  104. bStatus = FALSE;
  105. SetLineControl = (SetLineControl_t)GetProcAddress(hLibHandle, "SI_SetLineControl");
  106. if(SetLineControl == NULL)
  107. bStatus = FALSE;
  108. SetFlowControl = (SetFlowControl_t)GetProcAddress(hLibHandle, "SI_SetFlowControl");
  109. if(SetFlowControl == NULL)
  110. bStatus = FALSE;
  111. FlushBuffers = (FlushBuffers_t)GetProcAddress(hLibHandle, "SI_FlushBuffers");
  112. if(FlushBuffers == NULL)
  113. bStatus = FALSE;
  114. CheckRxQueue = (CheckRxQueue_t)GetProcAddress(hLibHandle, "SI_CheckRXQueue");
  115. if(CheckRxQueue == NULL)
  116. bStatus = FALSE;
  117. GetProductString = (GetProductString_t)GetProcAddress(hLibHandle, "SI_GetProductString");
  118. if(GetProductString == NULL)
  119. bStatus = FALSE;
  120. if(bStatus == FALSE)
  121. {
  122. FreeFunctions();
  123. return SiLabsError::NO_FUNCTION;
  124. }
  125. return SiLabsError::NONE;
  126. }
  127. ///////////////////////////////////////////////////////////////////////
  128. // Unloads USB DLLs.
  129. ///////////////////////////////////////////////////////////////////////
  130. void SiLabsLibrary::FreeFunctions()
  131. {
  132. if(hLibHandle != NULL)
  133. {
  134. FreeLibrary(hLibHandle);
  135. hLibHandle = NULL;
  136. }
  137. return;
  138. }
  139. #endif //defined(DSI_TYPES_WINDOWS)