Skip to content

Commit ba02d6b

Browse files
Add experimental OO wrapper calls
1 parent d822286 commit ba02d6b

2 files changed

Lines changed: 146 additions & 59 deletions

File tree

contrib/platform/src/com/sun/jna/platform/linux/Udev.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,61 @@ public interface Udev extends Library {
5151
* providing suitable locking if they intend to use it from multiple threads.
5252
*/
5353
class UdevContext extends PointerType {
54+
public void ref() {
55+
Udev.INSTANCE.udev_ref(this);
56+
}
57+
58+
public void unref() {
59+
Udev.INSTANCE.udev_unref(this);
60+
}
61+
62+
public UdevEnumerate newEnumerate() {
63+
return Udev.INSTANCE.udev_enumerate_new(this);
64+
}
65+
66+
public UdevDevice newDeviceFromSyspath(String syspath) {
67+
return Udev.INSTANCE.udev_device_new_from_syspath(this, syspath);
68+
}
5469
}
5570

5671
/**
5772
* To enumerate local devices on the system, an enumeration object can be
5873
* created via {@link #udev_enumerate_new}.
5974
*/
6075
class UdevEnumerate extends PointerType {
76+
public void ref() {
77+
Udev.INSTANCE.udev_enumerate_ref(this);
78+
}
79+
80+
public void unref() {
81+
Udev.INSTANCE.udev_enumerate_unref(this);
82+
}
83+
84+
public int addMatchSubsystem(String subsystem) {
85+
return Udev.INSTANCE.udev_enumerate_add_match_subsystem(this, subsystem);
86+
}
87+
88+
public int scanDevices() {
89+
return Udev.INSTANCE.udev_enumerate_scan_devices(this);
90+
}
91+
92+
public UdevListEntry getListEntry() {
93+
return Udev.INSTANCE.udev_enumerate_get_list_entry(this);
94+
}
6195
}
6296

6397
/**
6498
* Whenever libudev returns a list of objects, the {@code udev_list_entry} API
6599
* should be used to iterate, access and modify those lists.
66100
*/
67101
class UdevListEntry extends PointerType {
102+
public UdevListEntry getNext() {
103+
return Udev.INSTANCE.udev_list_entry_get_next(this);
104+
}
105+
106+
public String getName() {
107+
return Udev.INSTANCE.udev_list_entry_get_name(this);
108+
}
68109
}
69110

70111
/**
@@ -74,6 +115,49 @@ class UdevListEntry extends PointerType {
74115
* properties of the device in question.
75116
*/
76117
class UdevDevice extends PointerType {
118+
public void ref() {
119+
Udev.INSTANCE.udev_device_ref(this);
120+
}
121+
122+
public void unref() {
123+
Udev.INSTANCE.udev_device_unref(this);
124+
}
125+
126+
public UdevDevice getParent() {
127+
return Udev.INSTANCE.udev_device_get_parent(this);
128+
}
129+
130+
public UdevDevice getParentWithSubsystemDevtype(String subsystem, String devtype) {
131+
return Udev.INSTANCE.udev_device_get_parent_with_subsystem_devtype(this, subsystem, devtype);
132+
}
133+
134+
public String getSyspath() {
135+
return Udev.INSTANCE.udev_device_get_syspath(this);
136+
}
137+
138+
public String getSysname() {
139+
return Udev.INSTANCE.udev_device_get_sysname(this);
140+
}
141+
142+
public String getDevnode() {
143+
return Udev.INSTANCE.udev_device_get_devnode(this);
144+
}
145+
146+
public String getDevtype() {
147+
return Udev.INSTANCE.udev_device_get_devtype(this);
148+
}
149+
150+
public String getSubsystem() {
151+
return Udev.INSTANCE.udev_device_get_subsystem(this);
152+
}
153+
154+
public String getSysattrValue(String sysattr) {
155+
return Udev.INSTANCE.udev_device_get_sysattr_value(this, sysattr);
156+
}
157+
158+
public String getPropertyValue(String key) {
159+
return Udev.INSTANCE.udev_device_get_property_value(this, key);
160+
}
77161
}
78162

79163
/**

contrib/platform/test/com/sun/jna/platform/linux/UdevTest.java

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -43,68 +43,71 @@ public class UdevTest extends TestCase {
4343
public void testEnumerateDevices() {
4444
// Start with the context object
4545
UdevContext udev = UDEV.udev_new();
46-
assertNotNull("Failed to create udev context", udev);
47-
// Create an enumerator
48-
UdevEnumerate enumerate = UDEV.udev_enumerate_new(udev);
49-
assertNotNull("Failed to create udev enumerator", enumerate);
50-
// It's a good assumption the machine we're on has a disk and partition
51-
// Add a filter for block devices and scan them
52-
UDEV.udev_enumerate_add_match_subsystem(enumerate, "block");
53-
UDEV.udev_enumerate_scan_devices(enumerate);
54-
// Enumerator now points to block devices.
46+
try {
47+
assertNotNull("Failed to create udev context", udev);
48+
// Create an enumerator
49+
UdevEnumerate enumerate = udev.newEnumerate();
50+
try {
51+
assertNotNull("Failed to create udev enumerator", enumerate);
52+
// It's a good assumption the machine we're on has a disk and partition
53+
// Add a filter for block devices and scan them
54+
enumerate.addMatchSubsystem("block");
55+
enumerate.scanDevices();
56+
// Enumerator now points to block devices.
5557

56-
// Iterate the list
57-
UdevListEntry entry = UDEV.udev_enumerate_get_list_entry(enumerate);
58-
assertNotNull("Failed to get an enumerator entry", entry);
59-
String entryName = UDEV.udev_list_entry_get_name(entry);
60-
assertEquals("Udev entry name should start with /sys", "/sys", entryName.substring(0, 4));
61-
UdevDevice device = UDEV.udev_device_new_from_syspath(udev, entryName);
62-
while (device != null) {
63-
// devnode is what we use as name, like /dev/sda
64-
String devnode = UDEV.udev_device_get_devnode(device);
65-
// Ignore loopback and ram disks
66-
if (devnode != null && !devnode.startsWith("/dev/loop") && !devnode.startsWith("/dev/ram")) {
67-
String devType = UDEV.udev_device_get_devtype(device);
68-
// For partition, grab values of parent disk
69-
String parentSize = null;
70-
String parentMajor = null;
71-
if ("partition".equals(devType)) {
72-
UdevDevice parent = UDEV.udev_device_get_parent_with_subsystem_devtype(device, "block", "disk");
73-
String parentName = UDEV.udev_device_get_devnode(parent);
74-
// These should match same parent without restricting to block and disk
75-
UdevDevice parent2 = UDEV.udev_device_get_parent(device);
76-
String parentName2 = UDEV.udev_device_get_devnode(parent2);
77-
assertEquals("Partition parent should match with and without filter", parentName, parentName2);
78-
// Save the size and major
79-
parentSize = UDEV.udev_device_get_sysattr_value(parent, "size");
80-
parentMajor = UDEV.udev_device_get_property_value(parent, "MAJOR");
58+
// Iterate the list
59+
for (UdevListEntry entry = enumerate.getListEntry(); entry != null; entry = entry.getNext()) {
60+
assertNotNull("Failed to get an enumerator entry", entry);
61+
String entryName = entry.getName();
62+
assertEquals("Udev entry name should start with /sys", "/sys", entryName.substring(0, 4));
63+
UdevDevice device = udev.newDeviceFromSyspath(entryName);
64+
if (device != null) {
65+
try {
66+
// devnode is what we use as name, like /dev/sda
67+
String devnode = device.getDevnode();
68+
// Ignore loopback and ram disks
69+
if (devnode != null && !devnode.startsWith("/dev/loop") && !devnode.startsWith("/dev/ram")) {
70+
String devType = device.getDevtype();
71+
// For partition, grab values of parent disk
72+
String parentSize = null;
73+
String parentMajor = null;
74+
if ("partition".equals(devType)) {
75+
UdevDevice parent = device.getParentWithSubsystemDevtype("block", "disk");
76+
String parentName = parent.getDevnode();
77+
// These should match same parent without restricting to block and disk
78+
UdevDevice parent2 = device.getParent();
79+
String parentName2 = parent2.getDevnode();
80+
assertEquals("Partition parent should match with and without filter", parentName, parentName2);
81+
// Save the size and major
82+
parentSize = parent.getSysattrValue("size");
83+
parentMajor = parent.getPropertyValue("MAJOR");
84+
}
85+
String size = device.getSysattrValue("size");
86+
assertTrue("Size must be nonnegative", 0 <= Long.parseLong(size));
87+
if (parentSize != null) {
88+
assertTrue("Partition can't be bigger than its disk",
89+
Long.parseLong(size) <= Long.parseLong(parentSize));
90+
}
91+
String major = device.getPropertyValue("MAJOR");
92+
assertTrue("Major value must be nonnegative", 0 <= Long.parseLong(major));
93+
if (parentMajor != null) {
94+
assertEquals("Partition and its parent disk should have same major number", major, parentMajor);
95+
}
96+
assertEquals("DevType mismatch", devType, device.getDevtype());
97+
assertEquals("Subsystem mismatch", "block", device.getSubsystem());
98+
assertEquals("Syspath mismatch", entryName, device.getSyspath());
99+
assertTrue("Syspath should end with name", entryName.endsWith(device.getSysname()));
100+
}
101+
} finally {
102+
device.unref();
103+
}
104+
}
81105
}
82-
String size = UDEV.udev_device_get_sysattr_value(device, "size");
83-
assertTrue("Size must be nonnegative", 0 <= Long.parseLong(size));
84-
if (parentSize != null) {
85-
assertTrue("Partition can't be bigger than its disk",
86-
Long.parseLong(size) <= Long.parseLong(parentSize));
87-
}
88-
String major = UDEV.udev_device_get_property_value(device, "MAJOR");
89-
assertTrue("Major value must be nonnegative", 0 <= Long.parseLong(major));
90-
if (parentMajor != null) {
91-
assertEquals("Partition and its parent disk should have same major number", major, parentMajor);
92-
}
93-
assertEquals("DevType mismatch", devType, UDEV.udev_device_get_devtype(device));
94-
assertEquals("Subsystem mismatch", "block", UDEV.udev_device_get_subsystem(device));
95-
assertEquals("Syspath mismatch", entryName, UDEV.udev_device_get_syspath(device));
96-
assertTrue("Syspath should end with name", entryName.endsWith(UDEV.udev_device_get_sysname(device)));
97-
}
98-
UDEV.udev_device_unref(device);
99-
// Release the reference and iterate to the next device
100-
entry = UDEV.udev_list_entry_get_next(entry);
101-
entryName = UDEV.udev_list_entry_get_name(entry);
102-
if (entry != null) {
103-
assertEquals("Udev entry name should start with /sys", "/sys", entryName.substring(0, 4));
106+
} finally {
107+
enumerate.unref();
104108
}
105-
device = UDEV.udev_device_new_from_syspath(udev, entryName);
109+
} finally {
110+
udev.unref();
106111
}
107-
UDEV.udev_enumerate_unref(enumerate);
108-
UDEV.udev_unref(udev);
109112
}
110113
}

0 commit comments

Comments
 (0)