From 563f370faa88a2a31148d354dd5fd079eb49d2f9 Mon Sep 17 00:00:00 2001 From: mru Date: Tue, 26 Apr 2011 19:42:50 +0200 Subject: [PATCH] adding csharp bindings (not complete); minor modification to the linux/makefile to get 'make -n' print expanded pkg-config output --- bindings/Hid.cs | 196 ++++++++++++++++++++++++++++++++++++++++++++++++ hidapi/hidapi.h | 2 + linux/Makefile | 4 +- 3 files changed, 200 insertions(+), 2 deletions(-) create mode 100755 bindings/Hid.cs diff --git a/bindings/Hid.cs b/bindings/Hid.cs new file mode 100755 index 00000000..303fc74c --- /dev/null +++ b/bindings/Hid.cs @@ -0,0 +1,196 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; +using Mono.Unix; + +namespace ika_hid +{ + public class Hid + { + + private IntPtr dev; + + public bool IsOpen() { return dev != IntPtr.Zero; } + + public void Open (ushort vid, ushort hid, string serial) + { + if (dev != IntPtr.Zero) throw new Exception("a device is already opened; close it first."); + IntPtr ret = hid_open (vid, hid, serial); + if (ret == IntPtr.Zero) + throw new Exception ("Device not found"); + dev = ret; + } + + public int Read (byte[] buffer, int length) + { + AssertValidDev(); + int ret = hid_read (dev, buffer, (uint)length); + if (ret < 0) + throw new Exception ("Failed to Read."); + return ret; + } + + public void Close () + { + AssertValidDev(); + hid_close (dev); + dev = IntPtr.Zero; + } + + public String GetProductString () + { + AssertValidDev(); + byte[] buf = new byte[1000]; + int ret = Hid.hid_get_product_string (dev, buf, (uint)(buf.Length / 4) - 1); + if (ret < 0) + throw new Exception ("failed to receive product string"); + return EncodeBuffer(buf); + } + + public String GetManufacturerString () + { + AssertValidDev(); + byte[] buf = new byte[1000]; + int ret = Hid.hid_get_manufacturer_string (dev, buf, (uint)(buf.Length / 4) - 1); + if (ret < 0) + throw new Exception ("failed to receive manufacturer string"); + return EncodeBuffer(buf); + } + + + public int GetFeatureReport (byte[] buffer, int length) + { + AssertValidDev(); + int ret = hid_get_feature_report (dev, buffer, (uint)length); + if (ret < 0) + throw new Exception ("failed to get feature report"); + return ret; + } + + public int SendFeatureReport(byte[] buffer){ + int ret = hid_send_feature_report (dev, buffer, (uint)buffer.Length); + //if (ret < 0) + // throw new Exception ("failed to send feature report"); + return ret; + } + + public String Error () + { + AssertValidDev(); + IntPtr ret = hid_error (dev); + return UnixMarshal.PtrToString (ret, Encoding.UTF32); + } + + + public string GetIndexedString(int index) { + AssertValidDev(); + byte[] buf = new byte[1000]; + int ret = Hid.hid_get_indexed_string (dev, index, buf, (uint)(buf.Length / 4) - 1); + if (ret < 0) + throw new Exception ("failed to receive indexed string"); + return EncodeBuffer(buf); + } + + public string GetSerialNumberString() { + AssertValidDev(); + byte[] buf = new byte[1000]; + int ret = Hid.hid_get_serial_number_string (dev, buf, (uint)(buf.Length / 4) - 1); + if (ret < 0) + throw new Exception ("failed to receive serial number string"); + return EncodeBuffer(buf); + + } + + private string EncodeBuffer(byte[] buffer) { + return Encoding.UTF32.GetString (buffer).Trim ('\0'); + } + + private void AssertValidDev() { + if (dev == IntPtr.Zero) throw new Exception("No device opened"); + } + + [DllImport("libhidapi.so")] + private static extern int hid_read (IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length); + + [DllImport("libhidapi.so")] + private static extern IntPtr hid_open (ushort vid, ushort pid, [MarshalAs(UnmanagedType.LPWStr)] string serial); + + [DllImport("libhidapi.so")] + private static extern void hid_close (IntPtr device); + + [DllImport("libhidapi.so")] + private static extern int hid_get_product_string (IntPtr device, [Out] byte[] _string, uint length); + + [DllImport("libhidapi.so")] + private static extern int hid_get_manufacturer_string (IntPtr device, [Out] byte[] _string, uint length); + + [DllImport("libhidapi.so")] + private static extern int hid_get_feature_report (IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length); + + [DllImport("libhidapi.so")] + private static extern int hid_get_serial_number_string (IntPtr device, [Out] byte[] serial, uint maxlen); + + [DllImport("libhidapi.so")] + private static extern int hid_get_indexed_string (IntPtr device, int string_index, [Out] byte[] _string, uint maxlen); + + [DllImport("libhidapi.so")] + private static extern IntPtr hid_error (IntPtr device); + + [DllImport("libhidapi.so")] + private static extern int hid_send_feature_report (IntPtr device, [In, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length); + + [DllImport("libhidapi.so")] + private static extern int hid_set_nonblocking (IntPtr device, [In, MarshalAs(UnmanagedType.SysInt)] bool nonblock); + + [DllImport("libhidapi.so")] + private static extern int hid_write (IntPtr device, [In, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length); + + [DllImport("libhidapi.so")] + private static extern IntPtr hid_open_path ([In, MarshalAs(UnmanagedType.LPStr)] string path); + + // TODO: + + //public static extern void hid_free_enumeration(struct hid_device_info *devs) + //public static extern struct hid_device_info * hid_enumerate(unsigned short vendor_id, + // unsigned short product_id); + } + + class Utf32Marshaler : ICustomMarshaler + { + private static Utf32Marshaler instance = new Utf32Marshaler (); + + public static ICustomMarshaler GetInstance (string s) + { + return instance; + } + + public void CleanUpManagedData (object o) + { + } + + public void CleanUpNativeData (IntPtr pNativeData) + { + UnixMarshal.FreeHeap (pNativeData); + } + + public int GetNativeDataSize () + { + return IntPtr.Size; + } + + public IntPtr MarshalManagedToNative (object obj) + { + string s = obj as string; + if (s == null) + return IntPtr.Zero; + return UnixMarshal.StringToHeap (s, Encoding.UTF32); + } + + public object MarshalNativeToManaged (IntPtr pNativeData) + { + Console.WriteLine ("i was ere"); + return UnixMarshal.PtrToString (pNativeData, Encoding.UTF32); + } + } +} + diff --git a/hidapi/hidapi.h b/hidapi/hidapi.h index c7ab32cb..e029be50 100644 --- a/hidapi/hidapi.h +++ b/hidapi/hidapi.h @@ -314,6 +314,8 @@ extern "C" { int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen); /** @brief Get a string describing the last error which occurred. + * + * always returns NULL with usb-libusb.c under Linux @ingroup API @param device A device handle returned from hid_open(). diff --git a/linux/Makefile b/linux/Makefile index d310017e..f8079245 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -13,8 +13,8 @@ CXX=g++ COBJS=hid-libusb.o CPPOBJS=../hidtest/hidtest.o OBJS=$(COBJS) $(CPPOBJS) -LDFLAGS+=-Wall -fpic `pkg-config libusb-1.0 libudev --libs` -CFLAGS+=-I../hidapi -Wall -g `pkg-config libusb-1.0 --cflags` +LDFLAGS+=-Wall -fpic $(shell pkg-config libusb-1.0 libudev --libs) +CFLAGS+=-I../hidapi -Wall -g $(shell pkg-config libusb-1.0 --cflags) lib: libhidapi.so