Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions HidSharp/HidSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@
<Copyright>Copyright 2010-2019 James Bellinger</Copyright>
</PropertyGroup>

<PropertyGroup>
<PackageId>HidSharTestyThingy</PackageId>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>hidsharp_list</RootNamespace>
<!-- <ImplicitUsings>enable</ImplicitUsings> -->
<!-- <Nullable>enable</Nullable> -->
</PropertyGroup>

</Project>
99 changes: 89 additions & 10 deletions HidSharp/Platform/Linux/LinuxHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,104 @@ internal static LinuxHidDevice TryCreate(string path)
IntPtr device = NativeMethodsLibudev.Instance.udev_device_new_from_syspath(udev, d._path);
if (device != IntPtr.Zero)
{
string d_manufacturer = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(device, "manufacturer");
string d_productName = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(device, "product");
string d_serialNumber = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(device, "serial");
string d_idVendor = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(device, "idVendor");
string d_idProduct = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(device, "idProduct");
string d_bcdDevice = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(device, "bcdDevice");
Console.WriteLine();
string syspath = NativeMethodsLibudev.Instance.udev_device_get_syspath(device);
string devnode = NativeMethodsLibudev.Instance.udev_device_get_devnode(device);
Console.WriteLine("device " + devnode + " found device with " + d_manufacturer + d_productName + d_idVendor + d_idProduct);
try
{
string devnode = NativeMethodsLibudev.Instance.udev_device_get_devnode(device);
if (devnode != null)
{
d._fileSystemName = devnode;
IntPtr parent = IntPtr.Zero;
string parent_subsystem = null;

//if (NativeMethodsLibudev.Instance.udev_device_get_is_initialized(device) > 0)
{
IntPtr parent = NativeMethodsLibudev.Instance.udev_device_get_parent_with_subsystem_devtype(device, "usb", "usb_device");
if (IntPtr.Zero != parent)
{
string manufacturer = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent, "manufacturer");
string productName = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent, "product");
string serialNumber = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent, "serial");
string idVendor = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent, "idVendor");
string idProduct = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent, "idProduct");
string bcdDevice = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent, "bcdDevice");
/// show information for all parent devices
IntPtr p = device;
string devtype = "";
string subsystem = "";
string p_syspath = "";
string p_devnode = "";
while (p != IntPtr.Zero) {
Console.WriteLine("device " + devnode + " parent " + p + " getting parent of: " + p);
p = NativeMethodsLibudev.Instance.udev_device_get_parent(p);
devtype = NativeMethodsLibudev.Instance.udev_device_get_devtype(p);
subsystem = NativeMethodsLibudev.Instance.udev_device_get_subsystem(p);
p_syspath = NativeMethodsLibudev.Instance.udev_device_get_syspath(p);
p_devnode = NativeMethodsLibudev.Instance.udev_device_get_devnode(p);
// Console.WriteLine("device " + devnode + " parent " + p + " p_syspath is " + p_syspath);
// Console.WriteLine("device " + devnode + " parent " + p + " p_devnode is " + p_devnode);
Console.WriteLine("device " + devnode + " parent " + p + " subsystem/devtpye is " + subsystem + "/" + devtype);
// IntPtr entry = NativeMethodsLibudev.Instance.udev_device_get_properties_list_entry(p);
// while (entry != IntPtr.Zero) {
// string name = NativeMethodsLibudev.Instance.udev_list_entry_get_name(entry);
// string value = NativeMethodsLibudev.Instance.udev_list_entry_get_value(entry);
// entry = NativeMethodsLibudev.Instance.udev_list_entry_get_next(entry);
// Console.WriteLine("device " + devnode + " parent " + p + " properties " + name + " = " + value);
// }
}


Console.WriteLine("device " + devnode + " parent is " + parent + " with subsystem " + parent_subsystem);

// aquire a parent of the hid subsystem, which my pth-660 has when connected to my on my lenovo x13 via bluetooth
// maybe take the one that comes first when recursing into the parents
IntPtr parent_hid = NativeMethodsLibudev.Instance.udev_device_get_parent_with_subsystem(device, "hid");
IntPtr parent_usb = NativeMethodsLibudev.Instance.udev_device_get_parent_with_subsystem_devtype(device, "usb", "usb_device");

Console.WriteLine("device " + devnode + " parent_hid is " + parent_hid);
Console.WriteLine("device " + devnode + " parent_usb is " + parent_usb);

string manufacturer = "unknown";
string productName = "unknown";
string serialNumber = "unknown";
string idVendor = "0";
string idProduct = "0";
string bcdDevice = "0";

if (IntPtr.Zero != parent_usb) {
manufacturer = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent_usb, "manufacturer");
productName = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent_usb, "product");
serialNumber = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent_usb, "serial");
idVendor = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent_usb, "idVendor");
idProduct = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent_usb, "idProduct");
bcdDevice = NativeMethodsLibudev.Instance.udev_device_get_sysattr_value(parent_usb, "bcdDevice");
}
// add information to bluetooth devices
if (IntPtr.Zero != parent_hid) {
IntPtr entry2 = NativeMethodsLibudev.Instance.udev_device_get_properties_list_entry(parent_hid);
while (entry2 != IntPtr.Zero) {
string name = NativeMethodsLibudev.Instance.udev_list_entry_get_name(entry2);
string value = NativeMethodsLibudev.Instance.udev_list_entry_get_value(entry2);
entry2 = NativeMethodsLibudev.Instance.udev_list_entry_get_next(entry2);
Console.WriteLine("device " + devnode + " parent " + p + " properties " + name + " = " + value);
switch (name) {
case "HID_NAME":
if (productName == "unknown") {
productName = value;
}
break;
case "HID_ID":
string[] idParts = value.Split(":");
if (idVendor == "0") {
idVendor = idParts[1];
}
if (idProduct == "0") {
idProduct = idParts[2];
}
break;
}
}
}
if (IntPtr.Zero != parent_hid || IntPtr.Zero != parent_usb) {
int vid, pid, version;
if (NativeMethods.TryParseHex(idVendor, out vid) &&
NativeMethods.TryParseHex(idProduct, out pid) &&
Expand Down
15 changes: 15 additions & 0 deletions HidSharp/Platform/Linux/NativeMethodsLibudev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public abstract string FriendlyName

public abstract IntPtr udev_enumerate_get_list_entry(IntPtr enumerate);

public abstract IntPtr udev_list_entry_get_by_name(IntPtr entry, string name);

public abstract IntPtr udev_list_entry_get_next(IntPtr entry);

public abstract string udev_list_entry_get_name(IntPtr entry);

public abstract string udev_list_entry_get_value(IntPtr entry);

public abstract IntPtr udev_device_new_from_syspath(IntPtr udev, string syspath);

Expand All @@ -96,7 +100,18 @@ public abstract string FriendlyName

public abstract string udev_device_get_devpath(IntPtr device);

public abstract string udev_device_get_devtype(IntPtr device);

public abstract IntPtr udev_device_get_properties_list_entry(IntPtr entry);

public abstract string udev_device_get_syspath(IntPtr device);

public abstract string udev_device_get_subsystem(IntPtr device);

public abstract IntPtr udev_device_get_parent(IntPtr device);

public abstract IntPtr udev_device_get_parent_with_subsystem_devtype(IntPtr device, string subsystem, string devtype);
public abstract IntPtr udev_device_get_parent_with_subsystem(IntPtr device, string subsystem);

public abstract string udev_device_get_sysattr_value(IntPtr device, string sysattr);

Expand Down
66 changes: 66 additions & 0 deletions HidSharp/Platform/Linux/NativeMethodsLibudev0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ public override IntPtr udev_enumerate_get_list_entry(IntPtr enumerate)
return native_udev_enumerate_get_list_entry(enumerate);
}

[DllImport(libudev, EntryPoint = "udev_list_entry_get_by_name")]
static extern IntPtr native_udev_list_entry_get_by_name(IntPtr entry,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
public override IntPtr udev_list_entry_get_by_name(IntPtr entry, string name)
{
return native_udev_list_entry_get_by_name(entry, name);
}

[DllImport(libudev, EntryPoint = "udev_list_entry_get_next")]
static extern IntPtr native_udev_list_entry_get_next(IntPtr entry);
public override IntPtr udev_list_entry_get_next(IntPtr entry)
Expand All @@ -153,6 +161,14 @@ public override string udev_list_entry_get_name(IntPtr entry)
return native_udev_list_entry_get_name(entry);
}

[DllImport(libudev, EntryPoint = "udev_list_entry_get_value")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
static extern string native_udev_list_entry_get_value(IntPtr entry);
public override string udev_list_entry_get_value(IntPtr entry)
{
return native_udev_list_entry_get_value(entry);
}

[DllImport(libudev, EntryPoint = "udev_device_new_from_syspath")]
static extern IntPtr native_udev_device_new_from_syspath(IntPtr udev,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string syspath);
Expand Down Expand Up @@ -191,6 +207,44 @@ public override string udev_device_get_devpath(IntPtr device)
return native_udev_device_get_devpath(device);
}

[DllImport(libudev, EntryPoint = "udev_device_get_devtype")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
static extern string native_udev_device_get_devtype(IntPtr device);
public override string udev_device_get_devtype(IntPtr device)
{
return native_udev_device_get_devtype(device);
}

[DllImport(libudev, EntryPoint = "udev_device_get_properties_list_entry")]
static extern IntPtr native_udev_device_get_properties_list_entry(IntPtr entry);
public override IntPtr udev_device_get_properties_list_entry(IntPtr entry)
{
return native_udev_device_get_properties_list_entry(entry);
}

[DllImport(libudev, EntryPoint = "udev_device_get_syspath")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
static extern string native_udev_device_get_syspath(IntPtr device);
public override string udev_device_get_syspath(IntPtr device)
{
return native_udev_device_get_syspath(device);
}

[DllImport(libudev, EntryPoint = "udev_device_get_subsystem")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
static extern string native_udev_device_get_subsystem(IntPtr device);
public override string udev_device_get_subsystem(IntPtr device)
{
return native_udev_device_get_subsystem(device);
}

[DllImport(libudev, EntryPoint = "udev_device_get_parent")]
static extern IntPtr native_udev_device_get_parent(IntPtr device);
public override IntPtr udev_device_get_parent(IntPtr device)
{
return native_udev_device_get_parent(device);
}

[DllImport(libudev, EntryPoint = "udev_device_get_parent_with_subsystem_devtype")]
static extern IntPtr native_udev_device_get_parent_with_subsystem_devtype(IntPtr device,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string subsystem,
Expand All @@ -200,6 +254,18 @@ public override IntPtr udev_device_get_parent_with_subsystem_devtype(IntPtr devi
return native_udev_device_get_parent_with_subsystem_devtype(device, subsystem, devtype);
}

[DllImport(libudev, EntryPoint = "udev_device_get_parent_with_subsystem_devtype")]
static extern IntPtr native_udev_device_get_parent_with_subsystem(IntPtr device,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string subsystem
// ,[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(System.IntPtr))] IntPtr devtype
);
public override IntPtr udev_device_get_parent_with_subsystem(IntPtr device, string subsystem)
{
return native_udev_device_get_parent_with_subsystem(device, subsystem
// , IntPtr.Zero
);
}

[DllImport(libudev, EntryPoint = "udev_device_get_sysattr_value")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]
static extern string native_udev_device_get_sysattr_value(IntPtr device,
Expand Down
Loading