Skip to content

Commit

Permalink
Merge pull request #5 from umjammer/0.14.5v
Browse files Browse the repository at this point in the history
0.14.5v
  • Loading branch information
umjammer committed Jan 21, 2024
2 parents 72c67c8 + dc06742 commit bcc4a20
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ target/
/release.properties
/pom.xml.releaseBackup
tmp/
local.properties
2 changes: 2 additions & 0 deletions local.properties.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mid=0xaa
pid=0xbb
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.hid4java</groupId>
<artifactId>hid4java</artifactId>
<version>0.14.4v</version>
<version>0.14.5v</version>

<name>hid4java</name>
<description>A cross-platform Java Native Access (JNA) wrapper for the libusb/hidapi library</description>
Expand Down Expand Up @@ -44,7 +44,7 @@

<properties>
<jinput.groupId>com.github.umjammer.jinput</jinput.groupId> <!-- net.java.jinput / com.github.umjammer.jinput -->
<jinput.version>2.0.12v</jinput.version>
<jinput.version>2.0.14v</jinput.version>
</properties>

<build>
Expand All @@ -62,7 +62,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.2</version>
<configuration>
<excludes>
<exclude>**/*FunctionalTest.java</exclude>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/hid4java/HidDeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private synchronized void configureScanThread(Runnable scanRunnable) {
scanThread.submit(scanRunnable);
}

/** */
private synchronized Runnable getScanRunnable() {

int scanInterval = hidServicesSpecification.getScanInterval();
Expand Down Expand Up @@ -348,6 +349,7 @@ private synchronized Runnable getScanRunnable() {
}
}

/** */
public void shutdown() {
logger.finer("shutdown.0");
if (isScanning()) {
Expand All @@ -357,4 +359,9 @@ public void shutdown() {
nativeManager.close();
logger.finer("shutdown.2");
}

/** debug */
NativeHidDeviceManager getNativeHidDeviceManager() {
return nativeManager;
}
}
14 changes: 10 additions & 4 deletions src/main/java/org/hid4java/HidServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class HidServices {
/**
* The HID device manager handles scanning operations
*/
private final org.hid4java.HidDeviceManager hidDeviceManager;
private final HidDeviceManager hidDeviceManager;

/**
* Jar entry point to allow for version interrogation
Expand All @@ -72,18 +72,18 @@ public static void main(String[] args) {
/**
* Initialise with a default HID specification
*
* @throws HidException If something goes wrong (see {@link org.hid4java.HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
*/
public HidServices() throws IOException {
this(new HidServicesSpecification());
}

/**
* @param hidServicesSpecification Provides various parameters for configuring HID services
* @throws HidException If something goes wrong (see {@link org.hid4java.HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
*/
public HidServices(HidServicesSpecification hidServicesSpecification) throws IOException {
hidDeviceManager = new org.hid4java.HidDeviceManager(listeners, hidServicesSpecification);
hidDeviceManager = new HidDeviceManager(listeners, hidServicesSpecification);

// Check for automatic start (default behaviour for 0.6.0 and below)
// which will prevent an attachment event firing if the device is already
Expand Down Expand Up @@ -188,6 +188,7 @@ public HidDevice getHidDevice(int vendorId, int productId, String serialNumber)
}

/**
* TODO check
* @return The current library version from the manifest or 0.0.x if an error occurs
*/
public static String getVersion() {
Expand All @@ -211,4 +212,9 @@ public static String getVersion() {
String value = attr.getValue("Implementation-Version");
return Objects.requireNonNullElse(value, "0.0.3");
}

/** debug */
HidDeviceManager getHidDeviceManager() {
return hidDeviceManager;
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/hid4java/macos/MacosHidDeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.hid4java.NativeHidDeviceManager;
import vavix.rococoa.corefoundation.CFAllocator;
import vavix.rococoa.corefoundation.CFDictionary;
import vavix.rococoa.corefoundation.CFIndex;
import vavix.rococoa.corefoundation.CFLib;
import vavix.rococoa.corefoundation.CFNumber;
import vavix.rococoa.corefoundation.CFType;
Expand Down Expand Up @@ -200,7 +201,7 @@ public List<HidDevice.Info> enumerate(int vendorId, int productId) throws IOExce
CFDictionary /* CFMutableDictionaryRef */ matching = null;
if (vendorId != 0 || productId != 0) {
matching = CFLib.INSTANCE.CFDictionaryCreateMutable(CFAllocator.kCFAllocatorDefault,
new NativeLong(kIOHIDOptionsTypeNone), CFLib.kCFTypeDictionaryKeyCallBacks, CFLib.kCFTypeDictionaryValueCallBacks);
CFIndex.of(kIOHIDOptionsTypeNone), CFLib.kCFTypeDictionaryKeyCallBacks, CFLib.kCFTypeDictionaryValueCallBacks);

if (matching != null && vendorId != 0) {
ShortByReference r = new ShortByReference((short) vendorId);
Expand Down
49 changes: 47 additions & 2 deletions src/test/java/org/hid4java/HidDeviceTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
package org.hid4java;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;


@Disabled
@PropsEntity(url = "file:local.properties")
class HidDeviceTest {

static boolean localPropertiesExists() {
return Files.exists(Paths.get("local.properties"));
}

@Property(name = "mid")
String mid;
@Property(name = "pid")
String pid;

int vendorId;
int productId;

@BeforeEach
void setup() throws Exception {
if (localPropertiesExists()) {
PropsEntity.Util.bind(this);

vendorId = Integer.decode(mid);
productId = Integer.decode(pid);
}
}

HidDevice.Info mockStructure = new HidDevice.Info();

@Test
@Disabled("path need to be not null")
void isVidPidSerial_UnsignedShort_Simple() throws Exception {

HidDeviceManager manager = new HidDeviceManager(null, null);
Expand All @@ -32,6 +62,7 @@ void isVidPidSerial_UnsignedShort_Simple() throws Exception {
}

@Test
@Disabled("already let code not overflow")
void isVidPidSerial_UnsignedShort_Overflow() throws IOException {

HidDeviceManager manager = new HidDeviceManager(null, null);
Expand Down Expand Up @@ -80,4 +111,18 @@ void verifyFields() throws IOException {
assertEquals(5, testObject.getUsage());
assertEquals(6, testObject.getInterfaceNumber());
}
}

@Test
@EnabledIf("localPropertiesExists")
void test1() throws Exception {
HidServicesSpecification hidServicesSpecification = new HidServicesSpecification();
hidServicesSpecification.setAutoStart(false);
hidServicesSpecification.setAutoShutdown(false);

HidServices hidServices = HidManager.getHidServices(hidServicesSpecification);
hidServices.start();

List<HidDevice.Info> devices = hidServices.getHidDeviceManager().getNativeHidDeviceManager().enumerate(vendorId, productId);
assertEquals(1, devices.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,40 @@
package org.hid4java.examples;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import net.java.games.input.osx.plugin.DualShock4Plugin;
import org.hid4java.HidDevice;
import org.hid4java.HidManager;
import org.hid4java.HidServices;
import org.hid4java.HidServicesSpecification;
import org.junit.jupiter.api.BeforeEach;
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;


/**
* Demonstrate the USB HID interface using a Satoshi Labs Trezor
*
* @since 0.0.1
*/
@PropsEntity(url = "file:local.properties")
public class UsbHidEnumerationExample extends BaseExample {

@Property(name = "mid")
String mid;
@Property(name = "pid")
String pid;

int vendorId;
int productId;

public static void main(String[] args) throws Exception {
UsbHidEnumerationExample example = new UsbHidEnumerationExample();
PropsEntity.Util.bind(example);
example.vendorId = Integer.decode(example.mid);
example.productId = Integer.decode(example.pid);
example.executeExample();
}

Expand Down Expand Up @@ -71,7 +88,7 @@ private void executeExample() throws IOException {
System.out.printf("%s/%s ... %x%n", hidDevice.getManufacturer(), hidDevice.getProduct(), hidDevice.getUsagePage());
}

HidDevice device = hidServices.getHidDevice(0x54c, 0x9cc, null);
HidDevice device = hidServices.getHidDevice(vendorId, productId, null);
device.open();

device.addInputReportListener(e -> display(e.getReport()));
Expand Down

0 comments on commit bcc4a20

Please sign in to comment.