From: pavel Date: Tue, 14 Oct 2014 09:23:05 +0000 (+0300) Subject: Add project for windows DLL X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6c2893ae4b9ef8e5bb960f4facb8118a6476008c;p=usb-relay-hid.git Add project for windows DLL --- diff --git a/commandline/.gitignore b/commandline/.gitignore new file mode 100644 index 0000000..ac01e66 --- /dev/null +++ b/commandline/.gitignore @@ -0,0 +1,2 @@ +/Debug/ +/Release/ diff --git a/commandline/hidusb-demo.sln b/commandline/hidusb-demo.sln index 65974d2..bbcf6c9 100644 --- a/commandline/hidusb-demo.sln +++ b/commandline/hidusb-demo.sln @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00 # Visual C++ Express 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hidusb-relay-cmd", "hidusb-relay-cmd(vc2008).vcproj", "{CF9B07CB-509D-43B5-94FC-2BA43427C093}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "usb-relay-dll", "..\lib\usb-relay-dll\usb-relay-dll.vcproj", "{0E07152A-666D-4EB4-9049-BDE08C35A86D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -13,6 +15,10 @@ Global {CF9B07CB-509D-43B5-94FC-2BA43427C093}.Debug|Win32.Build.0 = Debug|Win32 {CF9B07CB-509D-43B5-94FC-2BA43427C093}.Release|Win32.ActiveCfg = Release|Win32 {CF9B07CB-509D-43B5-94FC-2BA43427C093}.Release|Win32.Build.0 = Release|Win32 + {0E07152A-666D-4EB4-9049-BDE08C35A86D}.Debug|Win32.ActiveCfg = Debug|Win32 + {0E07152A-666D-4EB4-9049-BDE08C35A86D}.Debug|Win32.Build.0 = Debug|Win32 + {0E07152A-666D-4EB4-9049-BDE08C35A86D}.Release|Win32.ActiveCfg = Release|Win32 + {0E07152A-666D-4EB4-9049-BDE08C35A86D}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/doc/Readme_USB-Relay-DLL.txt b/doc/Readme_USB-Relay-DLL.txt new file mode 100644 index 0000000..91146c4 --- /dev/null +++ b/doc/Readme_USB-Relay-DLL.txt @@ -0,0 +1,99 @@ +How to use USB_RELAY_DEVICE DLL + +Version 1.0 + +Changes to the original DLL: +============================ + - Type used for handles changed from int to intptr_t, for 64-bit compatibility. + This should be binary compatible with existing 32-bit clients. + + +Windows, Visual C++ applications +================================ + +Include file name: usb_relay_device.h +Library file name: usb_relay_device.lib + +Put the following lines in your source file: + + #include + #pragma comment(lib, "usb_relay_device.lib") + +The file usb_relay_device.dll must be installed with your application. +Use either 32-bit or 64-bit DLL, matching your application. +The DLL may require VC++ redistributable runtime library. + +Using the API: +-------------- + + * usb_relay_init() + Call this before calling other functions + + * usb_relay_device_enumerate() + Enumerates all Relay devices plugged into the PC. + Returns list of usb_relay_device_info structures. + Caller should free this list by passing it to usb_relay_device_free_enumerate(). + + * usb_relay_device_open() + Opens one Relay board device by its usb_relay_device_info structure, + obtained from usb_relay_device_enumerate(). + + * usb_relay_device_open_with_serial_number + Opens one Relay board device by its serial number string. + These strings can be obtained from usb_relay_device_enumerate(). + + * usb_relay_device_open_one_relay_channel + Turns ON one channel of a Relay board. + Parameters: + - device handle, obtained from usb_relay_device_open() + - Index: integer from 1 to the maximal number of channels on a board. + + * usb_relay_device_open_all_relay_channel + Turns ON all channels on a board. + + * usb_relay_device_close_one_relay_channel + Turns OFF one channel of a Relay board. + Parameters: + - device handle, obtained from usb_relay_device_open() + - Index: integer from 1 to the maximal number of channels on a board. + + * usb_relay_device_close_all_relay_channel() + Turns OFF all channels on a board. + + * usb_relay_device_get_status + Get status of all channels on a board as a bit mask. + The least significant bit in returned value corresponds to channel 1. + Bit value 1 means the corresponding relay is switched ON, value 0 means the relay is OFF. + + * usb_relay_device_close + Closes the Relay device handle opened by usb_relay_device_open() or + usb_relay_device_open_with_serial_number() + + * usb_relay_exit + Finalizes the library + +Error handling +--------------- +If error occurred, the API functions that return error code return -1; +functions that return handles or pointers return NULL. +Application is responsible to check the returned value. + +Notes +------- + * The library does not detect hot plug/unplug of USB devices. + * The library is not thread-safe. Client application must ensure that + only one thread calls the library. + + +Managed .NET applications (C#, VB.NET) +====================================== +TBD + +Python (CPython) +================ +TBD + +GCC or MinGW applications +========================= +TBD + diff --git a/lib/usb-relay-dll/.gitignore b/lib/usb-relay-dll/.gitignore new file mode 100644 index 0000000..ac01e66 --- /dev/null +++ b/lib/usb-relay-dll/.gitignore @@ -0,0 +1,2 @@ +/Debug/ +/Release/ diff --git a/lib/usb-relay-dll/dllmain.c b/lib/usb-relay-dll/dllmain.c new file mode 100644 index 0000000..6403fcc --- /dev/null +++ b/lib/usb-relay-dll/dllmain.c @@ -0,0 +1,20 @@ +// dllmain.cpp : Defines the entry point for the DLL + +#include "stdafx.h" + +BOOL APIENTRY DllMain( HMODULE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved + ) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} + diff --git a/lib/usb-relay-dll/stdafx.h b/lib/usb-relay-dll/stdafx.h new file mode 100644 index 0000000..96a9747 --- /dev/null +++ b/lib/usb-relay-dll/stdafx.h @@ -0,0 +1,16 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#define WIN32_EXTRALEAN // Exclude rarely-used stuff from Windows headers +// Windows Header Files: +#include + + + +// TODO: reference additional headers your program requires here diff --git a/lib/usb-relay-dll/targetver.h b/lib/usb-relay-dll/targetver.h new file mode 100644 index 0000000..c117daf --- /dev/null +++ b/lib/usb-relay-dll/targetver.h @@ -0,0 +1,24 @@ +#pragma once + +// The following macros define the minimum required platform. The minimum required platform +// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run +// your application. The macros work by enabling all features available on platform versions up to and +// including the version specified. + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef WINVER // Specifies that the minimum required platform is Windows Vista. +#define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows. +#endif + +#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. +#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. +#endif + +#ifndef _WIN32_WINDOWS // Specifies that the minimum required platform is Windows 98. +#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later. +#endif + +#ifndef _WIN32_IE // Specifies that the minimum required platform is Internet Explorer 7.0. +#define _WIN32_IE 0x0700 // Change this to the appropriate value to target other versions of IE. +#endif diff --git a/lib/usb-relay-dll/usb-relay-dll.vcproj b/lib/usb-relay-dll/usb-relay-dll.vcproj new file mode 100644 index 0000000..d08981d --- /dev/null +++ b/lib/usb-relay-dll/usb-relay-dll.vcproj @@ -0,0 +1,225 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/usb-relay-dll/usb_relay_dll.def b/lib/usb-relay-dll/usb_relay_dll.def new file mode 100644 index 0000000..90ed9ff --- /dev/null +++ b/lib/usb-relay-dll/usb_relay_dll.def @@ -0,0 +1,20 @@ +LIBRARY USB_RELAY_DEVICE +VERSION 1.0 + +EXPORTS +; All exports are CDECL + +usb_relay_device_close +usb_relay_device_close_all_relay_channel +usb_relay_device_close_one_relay_channel +usb_relay_device_enumerate +usb_relay_device_free_enumerate +usb_relay_device_get_status +usb_relay_device_open +usb_relay_device_open_all_relay_channel +usb_relay_device_open_one_relay_channel +usb_relay_device_open_with_serial_number +usb_relay_exit +usb_relay_init + +; END diff --git a/lib/usb-relay-dll/usb_relay_lib_win32.c b/lib/usb-relay-dll/usb_relay_lib_win32.c new file mode 100644 index 0000000..b1b74b7 --- /dev/null +++ b/lib/usb-relay-dll/usb_relay_lib_win32.c @@ -0,0 +1,139 @@ +// Windows version + +#include "stdafx.h" + +#define USBRL_API __declspec(dllexport) +#include "../usb_relay_device.h" +#include "../usb_relay_hw.h" + +#include + +#ifdef _MSC_VER +#pragma comment(lib, "setupapi") +#pragma comment(lib, "hid") +#endif /*_MSC_VER*/ + +#undef ERROR +#define ERROR (-1) +#define OK (0) + +#define dbgprintf(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__) +//#define dbgprintf(fmt, ...) printf(fmt, __VA_ARGS__) +//#define dbgprintf(fmt, ...) __noop(fmt, __VA_ARGS__) + +#ifdef __cplusplus +extern "C" { +#endif + +// Globals + + +// Public functions: + +/** Initialize the USB Relay Library +@returns: This function returns 0 on success and -1 on error. +*/ +int USBRL_API usb_relay_init(void) +{ + dbgprintf("Init lib\n"); + return ERROR; +} + +/** Finalize the USB Relay Library. +This function frees all of the static data associated with USB Relay Library. +It should be called at the end of execution to avoid memory leaks. +@returns: This function returns 0 on success and -1 on error. +*/ +int USBRL_API usb_relay_exit(void) +{ + return OK; +} + +/** Enumerate the USB Relay Devices.*/ +struct usb_relay_device_info USBRL_API * usb_relay_device_enumerate(void) +{ + return NULL; +} + + +/** Free an enumeration Linked List*/ +void USBRL_API usb_relay_device_free_enumerate(struct usb_relay_device_info *di) +{ + return; +} + +/** Open device by serial number +@return: This function returns a valid handle to the device on success or NULL on failure. +Example: usb_relay_device_open_with_serial_number("abcde", 5) */ +intptr_t USBRL_API usb_relay_device_open_with_serial_number(const char *serial_number, unsigned len) +{ + return ERROR; +} + +/* Open a USB relay device +@return: This function returns a valid handle to the device on success or NULL on failure. +*/ +intptr_t USBRL_API usb_relay_device_open(struct usb_relay_device_info *device_info) +{ + return ERROR; +} + +/* Close a USB relay device*/ +void USBRL_API usb_relay_device_close(intptr_t hHandle) +{ + return; +} + +/** Turn ON a relay channel on the USB-Relay-Device +@param index -- which channel your want to open +@param hHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device +*/ +int USBRL_API usb_relay_device_open_one_relay_channel(intptr_t hHandle, int index) +{ + return ERROR; +} + +/** Turn ON all relay channels on the USB-Relay-Device +@param hHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error +*/ +int USBRL_API usb_relay_device_open_all_relay_channel(intptr_t hHandle) +{ + return ERROR; +} + +/** Turn OFF a relay channel on the USB-Relay-Device +@param index -- which channel your want to close +@paramhHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device +*/ +int USBRL_API usb_relay_device_close_one_relay_channel(intptr_t hHandle, int index) +{ + return ERROR; +} + +/** Turn OFF all relay channels on the USB-Relay-Device +@paramter: hHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error +*/ +int USBRL_API usb_relay_device_close_all_relay_channel(intptr_t hHandle) +{ + return ERROR; +} + +/* +Status bits: one bit indicate a relay status. +bit 0/1/2/3/4/5/6/7/8 indicate channel 1/2/3/4/5/6/7/8 status + 1 -- means ON, 0 -- means OFF. +@returns: 0 -- success; 1 -- error +*/ +int USBRL_API usb_relay_device_get_status(intptr_t hHandle, unsigned int *status) +{ + return ERROR; +} + +#ifdef __cplusplus +} +#endif + diff --git a/lib/usb_relay_device.h b/lib/usb_relay_device.h new file mode 100644 index 0000000..ab6d398 --- /dev/null +++ b/lib/usb_relay_device.h @@ -0,0 +1,131 @@ +#ifndef USB_RELAY_DEVICE_H__ +#define USB_RELAY_DEVICE_H__ + +#ifdef WIN32 +#ifndef USBRL_API +# define USBRL_API __declspec(dllimport) +#endif +#ifndef CDECL +# define CDECL _cdecl +#endif +# +#else /* !WIN32 */ +# +#ifndef USBRL_API +# define USBRL_API +#endif +#ifndef CDECL +# define CDECL +#endif +# +#endif /* WIN32 */ + +#include + +enum usb_relay_device_type +{ + USB_RELAY_DEVICE_ONE_CHANNEL = 1, + USB_RELAY_DEVICE_TWO_CHANNEL = 2, + USB_RELAY_DEVICE_FOUR_CHANNEL = 4, + USB_RELAY_DEVICE_EIGHT_CHANNEL = 8 +}; + + +/** USB relay board info structure*/ +struct usb_relay_device_info +{ + unsigned char *serial_number; + char *device_path; + enum usb_relay_device_type type; + struct usb_relay_device_info* next; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** Initialize the USB Relay Library +@returns: This function returns 0 on success and -1 on error. +*/ +USBRL_API +int CDECL usb_relay_init(void); + +/** Finalize the USB Relay Library. +This function frees all of the static data associated with USB Relay Library. +It should be called at the end of execution to avoid memory leaks. +@returns:This function returns 0 on success and -1 on error. +*/ +USBRL_API +int CDECL usb_relay_exit(void); + + +/** Enumerate the USB Relay Devices.*/ +USBRL_API +struct usb_relay_device_info * CDECL usb_relay_device_enumerate(void); + + +/** Free an enumeration Linked List*/ +USBRL_API +void CDECL usb_relay_device_free_enumerate(struct usb_relay_device_info*); + +/** Open device that serial number is serial_number +@return: This function returns a valid handle to the device on success or NULL on failure. +e.g: usb_relay_device_open_with_serial_number("abcde", 5") */ +USBRL_API +intptr_t CDECL usb_relay_device_open_with_serial_number(const char *serial_number, unsigned len); + +/* Open a USB relay device +@return: This function returns a valid handle to the device on success or NULL on failure. +*/ +USBRL_API +intptr_t CDECL usb_relay_device_open(struct usb_relay_device_info *device_info); + +/* Close a USB relay device*/ +USBRL_API +void CDECL usb_relay_device_close(intptr_t hHandle); + +/** Turn ON a relay channel on the USB-Relay-Device +@param hHandle -- which usb relay device your want to operate +@param index -- the channel number: 1...max +@returns: 0 -- success; 1 -- error; 2 -- index is invalid +*/ +USBRL_API +int CDECL usb_relay_device_open_one_relay_channel(intptr_t hHandle, int index); + +/** Turn ON all relay channels on the USB-Relay-Device +@param hHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error +*/ +USBRL_API +int CDECL usb_relay_device_open_all_relay_channel(intptr_t hHandle); + +/** Turn OFF a relay channel on the USB-Relay-Device +@param index -- which channel your want to close +@param hHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error +*/ +USBRL_API +int CDECL usb_relay_device_close_one_relay_channel(intptr_t hHandle, int index); + +/** Turn OFF all relay channels on the USB-Relay-Device +@param hHandle -- which usb relay device your want to operate +@returns: 0 -- success; 1 -- error; 2 -- index is invalid +*/ +USBRL_API +int CDECL usb_relay_device_close_all_relay_channel(intptr_t hHandle); + +/* +Status bits: one bit indicate a relay status. +bit 0/1/2/3/4/5/6/7/8 indicate channel 1/2/3/4/5/6/7/8 status +Bit value 1 means ON, 0 means OFF. +@returns: 0 -- success; 1 -- error +*/ +USBRL_API +int CDECL usb_relay_device_get_status(intptr_t hHandle, unsigned int *status); + + +#ifdef __cplusplus +} +#endif + +#endif /* USB_RELAY_DEVICE_H__ */ diff --git a/lib/usb_relay_hw.h b/lib/usb_relay_hw.h new file mode 100644 index 0000000..56adf17 --- /dev/null +++ b/lib/usb_relay_hw.h @@ -0,0 +1,9 @@ +#ifndef USB_RELAY_DEVICE_HW__ +#define USB_RELAY_DEVICE_HW__ + +#define USB_CFG_VENDOR_ID 0x16c0 /* 5824 = voti.nl */ +#define USB_CFG_DEVICE_ID 0x05DF /* obdev's shared PID for HIDs */ +#define USB_CFG_VENDOR_NAME "www.dcttech.com" +#define USB_CFG_DEVICE_NAME2 "USBRelay2" /* can be relay1... relay8?*/ + +#endif /*USB_RELAY_DEVICE_HW__*/