Skip to content

Commit

Permalink
adding csharp bindings (not complete);
Browse files Browse the repository at this point in the history
minor modification to the linux/makefile to get 'make -n' print expanded pkg-config output
  • Loading branch information
mru00 committed Apr 26, 2011
1 parent c6a13ed commit 563f370
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 2 deletions.
196 changes: 196 additions & 0 deletions 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);
}
}
}

2 changes: 2 additions & 0 deletions hidapi/hidapi.h
Expand Up @@ -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().
Expand Down
4 changes: 2 additions & 2 deletions linux/Makefile
Expand Up @@ -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

Expand Down

0 comments on commit 563f370

Please sign in to comment.