1 |
|
2 | #ifndef _ATUSBHID_H_
|
3 | #define _ATUSBHID_H_
|
4 |
|
5 | // Error codes.
|
6 | #define ERROR_USB_DEVICE_NOT_FOUND 0xE0000001
|
7 | #define ERROR_USB_DEVICE_NO_CAPABILITIES 0xE0000002
|
8 |
|
9 | // name of the DLL to be loaded
|
10 | #define AT_USB_HID_DLL "AtUsbHid"
|
11 |
|
12 | // Implement the DLL export/import mechanism and allow a C-written program
|
13 | // to use our DLL.
|
14 | #ifdef ATUSBHID_EXPORTS
|
15 | #define ATUSBHID_API extern "C" __declspec(dllexport)
|
16 | #else
|
17 | #define ATUSBHID_API extern "C" __declspec(dllimport)
|
18 | #endif
|
19 |
|
20 |
|
21 | // This macro function calls the C runtime's _beginthreadex function.
|
22 | // The C runtime library doesn't want to have any reliance on Windows' data
|
23 | // types such as HANDLE. This means that a Windows programmer needs to cast
|
24 | // values when using _beginthreadex. Since this is terribly inconvenient,
|
25 | // this macro has been created to perform the casting.
|
26 | typedef unsigned(__stdcall *PTHREAD_START)(void *);
|
27 |
|
28 | #define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
|
29 | pvParam, fdwCreate, pdwThreadId) \
|
30 | ((HANDLE)_beginthreadex( \
|
31 | (void *) (psa), \
|
32 | (unsigned) (cbStack), \
|
33 | (PTHREAD_START) (pfnStartAddr), \
|
34 | (void *) (pvParam), \
|
35 | (unsigned) (fdwCreate), \
|
36 | (unsigned *)(pdwThreadId)))
|
37 |
|
38 | // Allow applications not built with Microsoft Visual C++ to link with our DLL.
|
39 | #define STDCALL __stdcall
|
40 |
|
41 | // These macros make calling our DLL functions through pointers easier.
|
42 | #define DECLARE_FUNCTION_POINTER(FUNC) PF_##FUNC lp##FUNC=NULL;
|
43 | #define LOAD_FUNCTION_POINTER(DLL,FUNC) lp##FUNC = (PF_##FUNC)GetProcAddress(DLL, #FUNC);
|
44 | #define ADDR_CHECK(FUNC) if (lp##FUNC == NULL) {fprintf(stderr,"%s\n", "Error: Cannot get address of function."); return FALSE;}
|
45 | #define DYNCALL(FUNC) lp##FUNC
|
46 |
|
47 |
|
48 | ///////////////////////////////////////////////////////////////////////////////
|
49 | typedef BOOLEAN (STDCALL *PF_findHidDevice)(const UINT VendorID, const UINT ProductID);
|
50 | typedef void (STDCALL *PF_closeDevice)(void);
|
51 | typedef BOOLEAN (STDCALL *PF_writeData)(UCHAR* buffer);
|
52 | typedef BOOLEAN (STDCALL *PF_readData)(UCHAR* buffer);
|
53 | typedef int (STDCALL *PF_hidRegisterDeviceNotification)(HWND hWnd);
|
54 | typedef void (STDCALL *PF_hidUnregisterDeviceNotification)(HWND hWnd);
|
55 | typedef int (STDCALL *PF_isMyDeviceNotification)(DWORD dwData);
|
56 | typedef BOOLEAN (STDCALL *PF_startBootLoader)(void);
|
57 | typedef BOOLEAN (STDCALL *PF_setFeature)(UCHAR type,UCHAR direction, unsigned int length);
|
58 | ///////////////////////////////////////////////////////////////////////////////
|
59 |
|
60 | // Exported functions prototypes.
|
61 |
|
62 | ///////////////////////////////////////////////////////////////////////////////
|
63 | ATUSBHID_API BOOLEAN STDCALL findHidDevice(const UINT VendorID, const UINT ProductID);
|
64 |
|
65 | // Closes the USB device and all handles before exiting the application.
|
66 | ATUSBHID_API void STDCALL closeDevice(void);
|
67 |
|
68 | ATUSBHID_API BOOLEAN STDCALL writeData(UCHAR* buf);
|
69 |
|
70 | ATUSBHID_API BOOLEAN STDCALL readData(UCHAR* buffer);
|
71 |
|
72 | ATUSBHID_API int STDCALL hidRegisterDeviceNotification(HWND hWnd);
|
73 |
|
74 | ATUSBHID_API void STDCALL hidUnregisterDeviceNotification(HWND hWnd);
|
75 |
|
76 | ATUSBHID_API int STDCALL isMyDeviceNotification(DWORD dwData);
|
77 |
|
78 | ATUSBHID_API BOOLEAN STDCALL setFeature(UCHAR type,UCHAR direction, unsigned int length);
|
79 |
|
80 | ///////////////////////////////////////////////////////////////////////////////
|
81 |
|
82 | #ifndef ATUSBHID_EXPORTS
|
83 |
|
84 |
|
85 | DECLARE_FUNCTION_POINTER(findHidDevice);
|
86 | DECLARE_FUNCTION_POINTER(closeDevice);
|
87 | DECLARE_FUNCTION_POINTER(writeData);
|
88 | DECLARE_FUNCTION_POINTER(readData);
|
89 | DECLARE_FUNCTION_POINTER(hidRegisterDeviceNotification);
|
90 | DECLARE_FUNCTION_POINTER(hidUnregisterDeviceNotification);
|
91 | DECLARE_FUNCTION_POINTER(isMyDeviceNotification);
|
92 | DECLARE_FUNCTION_POINTER(setFeature);
|
93 |
|
94 | // this function call all function available in the DLL *
|
95 | static bool loadFuncPointers(HINSTANCE hLib)
|
96 | {
|
97 | LOAD_FUNCTION_POINTER(hLib, findHidDevice);
|
98 | ADDR_CHECK(findHidDevice);
|
99 |
|
100 | LOAD_FUNCTION_POINTER(hLib, closeDevice);
|
101 | ADDR_CHECK(closeDevice);
|
102 |
|
103 | LOAD_FUNCTION_POINTER(hLib, writeData);
|
104 | ADDR_CHECK(writeData);
|
105 |
|
106 | LOAD_FUNCTION_POINTER(hLib, readData);
|
107 | ADDR_CHECK(readData);
|
108 |
|
109 | LOAD_FUNCTION_POINTER(hLib, hidRegisterDeviceNotification);
|
110 | ADDR_CHECK(hidRegisterDeviceNotification);
|
111 |
|
112 | LOAD_FUNCTION_POINTER(hLib, hidUnregisterDeviceNotification);
|
113 | ADDR_CHECK(hidUnregisterDeviceNotification);
|
114 |
|
115 | LOAD_FUNCTION_POINTER(hLib, isMyDeviceNotification);
|
116 | ADDR_CHECK(isMyDeviceNotification);
|
117 |
|
118 | LOAD_FUNCTION_POINTER(hLib, setFeature);
|
119 | ADDR_CHECK(setFeature);
|
120 |
|
121 | return true;
|
122 | }
|
123 |
|
124 | #endif
|
125 |
|
126 | #endif // _ATUSBHID_H_
|