2
0

dsi_silabs_library.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef DSI_SILABS_LIBRARY_HPP
  9. #define DSI_SILABS_LIBRARY_HPP
  10. #include "types.h"
  11. #include "DSI_SiUSBXp_3_1.h"
  12. #include <memory>
  13. #include <windows.h>
  14. struct SiLabsError
  15. {
  16. enum Enum
  17. {
  18. NONE,
  19. NO_LIBRARY,
  20. NO_FUNCTION
  21. };
  22. private: SiLabsError();
  23. };
  24. /*
  25. * This class started out as a singleton, but you cannot control when a static variable destroys
  26. * itself at the end of an application. Therefore, the class was changed to a normal wrapper around
  27. * the library. This means that if you retrieve an instance of the class and then destroy it, you will
  28. * unload the whole library. Since we would rather the library stay loaded for the rest of the application if
  29. * we use it, there is an extra call to load the library so that the library is guaranteed to not unload until
  30. * the end of the application. As well, since each instance also calls to load the library, the library is
  31. * not freed until everyone is done using it.
  32. */
  33. //NOTE: Make sure this class isn't being depended upon by another global variable!
  34. //NOTE: Not thread-safe.
  35. class SiLabsLibrary
  36. {
  37. public:
  38. //!!Is this guaranteed to never be a global variable? If so, then we can return the static instance instead of creating a new one.
  39. //!!maybe return another smart pointer like shared_ptr?
  40. static BOOL Load(std::auto_ptr<const SiLabsLibrary>& clAutoLibrary_); //!! Alternative to creating directly and having to worry about try statements
  41. /* Otherwise you'd have to do this...
  42. //Get a reference to library
  43. auto_ptr<SiLabsLibrary> pclAutoSiLibrary(NULL);
  44. try { pclAutoSiLibrary.reset(new SiLabsLibrary); }
  45. catch(...) { return clList; }
  46. const SiLabsLibrary& clSiLibrary = *pclAutoSiLibrary;
  47. */
  48. SiLabsLibrary(); //throw(SiLabsError::Enum)
  49. virtual ~SiLabsLibrary() throw();
  50. // Prototypes for USB functions found in the SI dll.
  51. typedef SI_STATUS (WINAPI *GetNumDevices_t)(LPDWORD lpdwNumDevices);
  52. typedef SI_STATUS (WINAPI *GetProductString_t)(DWORD dwDeviceNum, LPVOID lpvDeviceString, DWORD dwFlags);
  53. typedef SI_STATUS (WINAPI *Open_t)(DWORD dwDevice, HANDLE *cyHandle);
  54. typedef SI_STATUS (WINAPI *Close_t)(HANDLE cyHandle);
  55. typedef SI_STATUS (WINAPI *Read_t)(HANDLE cyHandle, LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpdwBytesReturned, OVERLAPPED *o);
  56. typedef SI_STATUS (WINAPI *Write_t)(HANDLE cyHandle, LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpdwBytesWritten, OVERLAPPED *o);
  57. typedef SI_STATUS (WINAPI *SetTimeouts_t)(DWORD dwReadTimeout, DWORD dwWriteTimeout);
  58. typedef SI_STATUS (WINAPI *GetTimeouts_t)(LPDWORD lpdwReadTimeout, LPDWORD lpdwWriteTimeout);
  59. typedef SI_STATUS (WINAPI *FlushBuffers_t)(HANDLE cyHandle, BYTE FlushTransmit, BYTE FlushReceive);
  60. typedef SI_STATUS (WINAPI *CheckRxQueue_t)(HANDLE cyHandle, LPDWORD lpdwNumBytesInQueue, LPDWORD lpdwQueueStatus);
  61. typedef SI_STATUS (WINAPI *SetBaudRate_t)(HANDLE cyHandle, DWORD dwBaudRate);
  62. typedef SI_STATUS (WINAPI *SetLineControl_t)(HANDLE cyHandle, WORD wLineControl);
  63. typedef SI_STATUS (WINAPI *SetFlowControl_t)(HANDLE cyHandle, BYTE bCTS_MaskCode, BYTE bRTS_MaskCode, BYTE bDTR_MaskCode, BYTE bDSR_MaskCode, BYTE bDCD_MaskCode, BOOL bFlowXonXoff);
  64. Open_t Open;
  65. GetNumDevices_t GetNumDevices;
  66. Close_t Close;
  67. Read_t Read;
  68. Write_t Write;
  69. SetTimeouts_t SetTimeouts;
  70. GetTimeouts_t GetTimeouts;
  71. FlushBuffers_t FlushBuffers;
  72. CheckRxQueue_t CheckRxQueue;
  73. SetBaudRate_t SetBaudRate;
  74. SetLineControl_t SetLineControl;
  75. SetFlowControl_t SetFlowControl;
  76. GetProductString_t GetProductString;
  77. private:
  78. SiLabsError::Enum LoadFunctions();
  79. void FreeFunctions();
  80. static std::auto_ptr<SiLabsLibrary> clAutoInstance; //keeps the library loaded for the duration of the application
  81. //NOTE: There is no control when this gets destroyed at end of program
  82. // but it doesn't matter because it's main purpose is to keep the library loaded
  83. // during the duration of the whole application.
  84. HMODULE hLibHandle;
  85. static BOOL bStaticSet;
  86. //!!Could dynamically make all instances and push them onto a static list to delete when we get to it
  87. };
  88. #endif //DSI_SILABS_LIBRARY_HPP