diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index dd84ea7..272b20e 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -1,38 +1,50 @@ --- name: Bug report -about: Create a report to help us improve +about: Create a report to help the project improve title: '' labels: '' assignees: '' - --- +**Describe the problem** + +You may find that this issue has arisen before, so here is a checklist of useful links to provide assistance and potentially avoid duplicate issues: +- [ ] I have read the [Troubleshooting Guide](https://github.com/gary-rowe/hid4java/wiki/Troubleshooting) and this is not covered +- [ ] I have done a [quick search of previous issues](https://github.com/gary-rowe/hid4java/issues?q=is%3Aissue+hidraw) and this needs addressing + +Please provide a clear and concise description of what the problem is below: + +**Platform** + +Knowing the platform greatly narrows down the potential causes of the problem. -**Describe the bug** -A clear and concise description of what the bug is. +- Platform (e.g. `linux-x86-64`): +- OS version (e.g. `Ubuntu 22.04`): +- `hid4java` version (e.g. `0.8.0`): **To Reproduce** + Steps to reproduce the behavior: -1. Go to '...' -2. Click on '....' -3. Scroll down to '....' -4. See error + +1. Attempting to read from device '...' +2. Code sample looks like this '...' +3. Error happens when '...' **Expected behavior** + A clear and concise description of what you expected to happen. -**Screenshots** -If applicable, add screenshots to help explain your problem. +**Screenshots and logs** + +If applicable, add screenshots and redacted logs to help explain your problem. -**Desktop (please complete the following information):** - - OS: [e.g. iOS] - - Browser [e.g. chrome, safari] - - Version [e.g. 22] +Screenshots: +Drag and drop image here if useful -**Smartphone (please complete the following information):** - - Device: [e.g. iPhone6] - - OS: [e.g. iOS8.1] - - Browser [e.g. stock browser, safari] - - Version [e.g. 22] +Logs: +``` + +``` -**Additional context** -Add any other context about the problem here. +**Additional information** +Add any other context about the problem here, including any investigative work +done and thoughts on solution. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index bbcbbe7..e48b7ae 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,6 +1,6 @@ --- name: Feature request -about: Suggest an idea for this project +about: Suggest an idea for hid4java title: '' labels: '' assignees: '' @@ -8,13 +8,21 @@ assignees: '' --- **Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] +A clear and concise description of what the problem is. + +* I'm always frustrated when [...], or +* If only you supported [...] +* Why do I always have to [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. +* After a bit of research, I think this could be solved by [...] +* You support platform [...] + **Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. +A clear and concise description of any alternative solutions or features you've +considered. **Additional context** Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/release.md b/.github/ISSUE_TEMPLATE/release.md new file mode 100644 index 0000000..2f1fed5 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/release.md @@ -0,0 +1,29 @@ +--- +name: Release +about: Create a release checklist +title: 'Release checklist for 0.x.0' +labels: '' +assignees: 'gary-rowe' +--- +Here's what's outstanding before `0.x.0` can be released. + +**Pre-release checklist** + +- [ ] FIDO2 device verified working in all test environments +- [ ] `develop-SNAPSHOT` deployed to Maven Central (snapshots) +- [ ] Issues for release marked as "Awaiting merge" + +Feedback received from community verifying the following libraries for `develop-SNAPSHOT`: + +- [ ] `darwin-x86-64` - macOS 64-bit +- [ ] `darwin-aarch64` - macOS ARM64 +- [ ] `linux-aarch64` - Linux ARMv8 64-bit only +- [ ] `linux-amd64` - Linux AMD 64-bit only +- [ ] `linux-arm` - Linux ARMv7 hard float 32-bit only +- [ ] `linux-armel` - Linux ARMv5 soft float 32-bit +- [ ] `linux-riscv64` - Linux RISC-V 64-bit +- [ ] `linux-x86-64` - Linux x86 64-bit only +- [ ] `linux-x86` - Linux x86 32-bit only +- [ ] `win32-x86` - Windows 32-bit only +- [ ] `win32-x86-64` - Windows 64-bit only +- [ ] `win32-aarch64` - Windows 64-bit ARM64 diff --git a/AUTHORS b/AUTHORS index f77b8af..b016722 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,3 +3,5 @@ The following developers made contributions to this project: Gary Rowe (Lead developer) jbliesener Ktar5 +tresf +DaPutzy diff --git a/README.md b/README.md index b2ef704..683c360 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Project status -[![Build Status](https://travis-ci.org/gary-rowe/hid4java.png?branch=master)](https://travis-ci.org/gary-rowe/hid4java) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.hid4java/hid4java/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.hid4java/hid4java) [![Javadocs](http://www.javadoc.io/badge/org.hid4java/hid4java.svg)](http://www.javadoc.io/doc/org.hid4java/hid4java) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.hid4java/hid4java/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.hid4java/hid4java) [![Javadoc](https://javadoc.io/badge2/org.hid4java/hid4java/javadoc.svg)](https://javadoc.io/doc/org.hid4java/hid4java) # 🌟 Summary @@ -11,14 +11,15 @@ The `hid4java` project supports USB HID devices through a common API which is pr If you want to discuss `hid4java` in general please use [the Telegram chat](https://t.me/joinchat/CtU4ZBltWCAFBAjwM5KLLw). I can't guarantee an instant response but I'm usually active on Telegram during office hours in the GMT timezone. -Remember to [check the Wiki first](https://github.com/gary-rowe/hid4java/wiki/Home) before asking questions to avoid causing frustration! +👀 Remember to [check the Wiki first](https://github.com/gary-rowe/hid4java/wiki/Home) before asking questions to avoid causing frustration! ## Technologies * [hidapi](https://github.com/libusb/hidapi) - Native USB HID library for multiple platforms -* [JNA](https://github.com/twall/jna) - to remove the need for Java Native Interface (JNI) and greatly simplify the project +* [JNA](https://github.com/twall/jna) - Removes the need for Java Native Interface (JNI) and greatly simplify the project * [dockcross](https://github.com/dockcross/dockcross) - Cross-compilation environments for multiple platforms to create hidapi libraries -* Java 7+ - to remove dependencies on JVMs that have reached end of life +* [Maven](https://maven.apache.org/) - Build environment for Java projects if you need to customise the library +* Java 8+ - to remove dependencies on JVMs that have reached end of life ## Maven dependency @@ -30,11 +31,30 @@ Remember to [check the Wiki first](https://github.com/gary-rowe/hid4java/wiki/Ho org.hid4java hid4java - 0.7.0 + 0.8.0 +``` +If you are developing and want the latest code, you'll need to reference release candidate artifacts deployed to the Maven Central snapshot repository. These are updated more frequently as part of wider user acceptance testing, but they are not considered stable enough for production use. + +Add the following `repositories` section to your project's `pom.xml` if you're using Maven for your build process. + +```xml + + + + oss.sonatype.org-snapshot + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + ``` ## Gradle dependency @@ -83,18 +103,23 @@ If you're unfamiliar with Maven and git the wiki provides [an easy guide to crea The project uses the standard Maven build process and can be used without having external hardware attached. Just do the usual -``` +```shell cd git clone https://github.com/gary-rowe/hid4java.git cd hid4java + +# Maven build mvn clean install ``` and you're good to go. +Maven will place the built JAR into the `target` directory. +The Maven `install` process will also place copies of the built JARs into `~/.m2/repository/org/hid4java/hid4java//` so that other local projects can find and share them. + # 🤔 More information -Much of the information previously in this README has been migrated to the project Wiki as it was getting rather long. Here are some useful jumping off points that should help: +Much of the information previously in this README has been migrated to the [project Wiki](https://github.com/gary-rowe/hid4java/wiki/Home) as it was getting rather long. Here are some useful jumping off points that should help: * [Home](https://github.com/gary-rowe/hid4java/wiki/Home) - The wiki Home page with lots of useful launch points * [FAQ](https://github.com/gary-rowe/hid4java/wiki/FAQ) - Frequently asked questions @@ -105,6 +130,6 @@ Much of the information previously in this README has been migrated to the proje All trademarks and copyrights are acknowledged. -Many thanks to `victorix` who provided the basis for this library. Please [see the inspiration on the mbed.org site](http://developer.mbed.org/cookbook/USBHID-bindings-). +Many thanks to `victorix` who provided the basis for this library. Please [see the inspiration on the mbed site](https://os.mbed.com/cookbook/USBHID-bindings-). Thanks also go to everyone who has contributed their knowledge and advice during the creation and subsequent improvement of this library. diff --git a/build-hidapi.sh b/build-hidapi.sh index 8632d01..883d8ef 100755 --- a/build-hidapi.sh +++ b/build-hidapi.sh @@ -4,338 +4,552 @@ # Directory structure is # ~/Workspaces # + Cpp -# + hidapi +# + hidapi (https://github.com/libusb/hidapi) # + Docker -# + dockcross +# + dockcross (https://github.com/dockcross/dockcross) # + Java # + Personal -# + hid4java +# + hid4java (https://github.com/gary-rowe/hid4java) +# +# Dependencies: +# - git +# - Docker Desktop +# - XCode v12.5.1 or higher for darwin-x86-64-aarch64 cross compiler support +# +# Place a symlink to this script in the root of ~/Workspaces +# cd ~/Workspaces +# ln -s Java/Personal/hid4java/build-hidapi.sh ./build-hidapi.sh # # Supported command line arguments are: # # all - build all variants # windows - build all Windows variants # linux - build all Linux variants -# osx - build all OS X variants -# darwin - OS X 64-bit +# darwin - build all macOS variants (not recommended) +# darwin-x86-64 - macOS 64-bit +# darwin-aarch64 - macOS ARM64 # linux-aarch64 - Linux ARMv8 64-bit # linux-amd64 - Linux AMD 64-bit -# linux-arm - Linux ARMv7 hard float 32-bit -# linux-armel - Linux ARMv6 EABI 32-bit -# linux-x86-64 - Linux x86 64-bit +# linux-arm - Linux ARMv6 hard float 32-bit (RPi) +# linux-armel - Linux ARMv5 soft float 32-bit +# linux-riscv64 - Linux RISC-V 64-bit +# linux-x86-64 - Linux x86 64-bit (same as AMD64) # linux-x86 - Linux x86 32-bit # win32-x86 - Windows 32-bit # win32-x86-64 - Windows 64-bit +# win32-aarch64 - Windows 64-bit ARM64 # -echo -e "\033[32m------------------------------------------------------------------------\033[0m" -echo -e "\033[33mTarget build for HIDAPI is $1\033[0m" - -# Dockcross latest release - -echo -e "\033[32mConfiguring Dockcross\033[0m" -cd ~/Workspaces/Docker/dockcross/ || exit -git checkout master -git pull +# Set environment +hid4javaDir="${HOME}/Workspaces/Java/Personal/hid4java" +hidapiDir="${HOME}/Workspaces/Cpp/hidapi" +dockcrossDir="${HOME}/Workspaces/Docker/dockcross" -echo -e "\033[32m------------------------------------------------------------------------\033[0m" - -# Ensure Docker is running - -echo -e "\033[32mConfiguring Docker\033[0m" -if docker_result=$(docker-machine env default); then - eval "$docker_result" - echo -e "\033[32mOK\033[0m" -else - echo -e "\033[31mFailed\033[0m - Docker not running. Use 'docker-machine start default'" - exit +hardwareName=$(uname -m) +if [[ "${hardwareName}" == "arm64" ]] + then + platform="--platform linux/amd64" + else + platform="" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" -# Windows cross compilers +# Console colours +red="\033[31m" +yellow="\033[33m" +green="\033[32m" +plain="\033[0m" -# 64-bit -echo -e "\033[32mConfiguring Windows 64-bit\033[0m" -docker run --rm dockcross/windows-shared-x64 > ./dockcross-windows-shared-x64 -chmod +x ./dockcross-windows-shared-x64 -mv ./dockcross-windows-shared-x64 /usr/local/bin - -# 32-bit -echo -e "\033[32mConfiguring Windows 32-bit\033[0m" -docker run --rm dockcross/windows-shared-x86 > ./dockcross-windows-shared-x86 -chmod +x ./dockcross-windows-shared-x86 -mv ./dockcross-windows-shared-x86 /usr/local/bin - -echo -e "\033[32mConfiguring Linux environments\033[0m" - -# Linux cross compilers - -# 64 bit -echo -e "\033[32mConfiguring Linux 64-bit\033[0m" -docker run --rm dockcross/linux-x64 > ./dockcross-linux-x64 -chmod +x ./dockcross-linux-x64 -mv ./dockcross-linux-x64 /usr/local/bin - -# 32 bit -echo -e "\033[32mConfiguring Linux 32-bit\033[0m" -docker run --rm dockcross/linux-x86 > ./dockcross-linux-x86 -chmod +x ./dockcross-linux-x86 -mv ./dockcross-linux-x86 /usr/local/bin +# Initial checks +# @Tresf: macOS deployment target checking +# See https://github.com/gary-rowe/hid4java/issues/120 for more details +if [[ "$1" == "all" ]] || [[ "$1" == "darwin" ]] || [[ "$1" == "darwin-aarch64" ]] || [[ "$1" == "darwin-x86-64" ]] + then + if [ -z "$SDKROOT" ] || [ -z "$MACOSX_DEPLOYMENT_TARGET" ]; + then + echo -e "${yellow}WARNING: For production builds, please set \$SDKROOT and \$MACOSX_DEPLOYMENT_TARGET before building${plain}" + echo -e "${yellow}Continuing with default values${plain}" + export SDKROOT="$(xcrun --sdk macosx --show-sdk-path)" + if [[ "$1" == "all" ]] || [[ "$1" == "darwin" ]] || [[ "$1" == "darwin-aarch64" ]] + then + # Build for darwin-aarch64 with minimum of OS Big Sur (2020) + export MACOSX_DEPLOYMENT_TARGET="11.0" + else + # Build for darwin-x86-64 with minimum of OS Mavericks (2013) + export MACOSX_DEPLOYMENT_TARGET="10.9" + fi + fi +fi -# ARM cross compilers +# @Tresf: Function to do "make clean" without incurring issues during build +function git-clean { + echo -e "${yellow}Cleaning hidapi${plain}" + # Remove all untracked files (including project files) + git clean -fdx > /dev/null 2>&1 || exit + # Reset all tracked files + git reset --hard > /dev/null 2>&1 || exit +} -# 32-bit ARMv6 EABI -echo -e "\033[32mConfiguring ARMv6 EABI 32-bit\033[0m" -docker run --rm dockcross/linux-armv6 > ./dockcross-linux-armv6 -chmod +x ./dockcross-linux-armv6 -mv ./dockcross-linux-armv6 /usr/local/bin +# Function to provide library file details +function report { -# 32-bit ARMv7 hard float -echo -e "\033[32mConfiguring ARMv7 32-bit\033[0m" -docker run --rm dockcross/linux-armv7 > ./dockcross-linux-armv7 -chmod +x ./dockcross-linux-armv7 -mv ./dockcross-linux-armv7 /usr/local/bin +if [[ ! -f $1 ]] + then + echo -e "${red}File '$1' was not found.${plain}" + else + ls -la $1 + file -b $1 + echo -e "${green}---${plain}" +fi +} -# 64-bit (arm64, aarch64) -echo -e "\033[32mConfiguring ARM 64-bit\033[0m" -docker run --rm dockcross/linux-arm64 > ./dockcross-linux-arm64 -chmod +x ./dockcross-linux-arm64 -mv ./dockcross-linux-arm64 /usr/local/bin +echo -e "${green}------------------------------------------------------------------------${plain}" +echo -e "${yellow}Using '$1' to perform build${plain}" -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" -# HIDAPI latest release -echo -e "\033[32mConfiguring HIDAPI\033[0m" -cd ~/Workspaces/Cpp/hidapi/ || exit -git checkout master -git pull +if [[ "$1" == "update" ]] + then + echo -e "${green}Updating Dockcross${plain}" + cd ${dockcrossDir} || exit + git checkout master + git pull + + # Windows cross compilers + + # 64-bit (Intel) + echo -e "${green}Configuring Windows 64-bit${plain}" + docker run ${platform} --rm dockcross/windows-shared-x64 > ./dockcross-windows-shared-x64 + chmod +x ./dockcross-windows-shared-x64 + mv ./dockcross-windows-shared-x64 /usr/local/bin + + # 32-bit (Intel) + echo -e "${green}Configuring Windows 32-bit${plain}" + docker run ${platform} --rm dockcross/windows-shared-x86 > ./dockcross-windows-shared-x86 + chmod +x ./dockcross-windows-shared-x86 + mv ./dockcross-windows-shared-x86 /usr/local/bin + + # 64-bit (ARM64) + echo -e "${green}Configuring Windows 64-bit ARM64 (aarch64)${plain}" + docker run ${platform} --rm dockcross/windows-arm64 > ./dockcross-windows-arm64 + chmod +x ./dockcross-windows-arm64 + mv ./dockcross-windows-arm64 /usr/local/bin + + echo -e "${green}Configuring Linux environments${plain}" + + # Linux cross compilers + + # 64 bit (Intel) + echo -e "${green}Configuring Linux 64-bit${plain}" + docker run ${platform} --rm dockcross/linux-x64 > ./dockcross-linux-x64 + chmod +x ./dockcross-linux-x64 + mv ./dockcross-linux-x64 /usr/local/bin + + # 32 bit (Intel) + echo -e "${green}Configuring Linux 32-bit${plain}" + docker run ${platform} --rm dockcross/linux-x86 > ./dockcross-linux-x86 + chmod +x ./dockcross-linux-x86 + mv ./dockcross-linux-x86 /usr/local/bin + + # ARM cross compilers + + # @Tresf + # 32-bit ARMv5TE EABI "armel" + echo -e "${green}Configuring ARMv5TE EABI 32-bit${plain}" + docker run ${platform} --rm dockcross/linux-armv5 > ./dockcross-linux-armv5 + chmod +x ./dockcross-linux-armv5 + mv ./dockcross-linux-armv5 /usr/local/bin + + # 32-bit ARMv6 EABI + echo -e "${green}Configuring ARMv6 EABI 32-bit${plain}" + docker run ${platform} --rm dockcross/linux-armv6 > ./dockcross-linux-armv6 + chmod +x ./dockcross-linux-armv6 + mv ./dockcross-linux-armv6 /usr/local/bin + + # 32-bit ARMv7 hard float + echo -e "${green}Configuring ARMv7 32-bit${plain}" + docker run ${platform} --rm dockcross/linux-armv7 > ./dockcross-linux-armv7 + chmod +x ./dockcross-linux-armv7 + mv ./dockcross-linux-armv7 /usr/local/bin + + # 64-bit (arm64, aarch64) + echo -e "${green}Configuring ARM 64-bit${plain}" + docker run ${platform} --rm dockcross/linux-arm64 > ./dockcross-linux-arm64 + chmod +x ./dockcross-linux-arm64 + mv ./dockcross-linux-arm64 /usr/local/bin + + # RISC-V cross compilers + + # @Tresf + # 64-bit (RISC-V) + echo -e "${green}Configuring RISC-V 64-bit${plain}" + docker run --rm dockcross/linux-riscv64 > ./dockcross-linux-riscv64 + chmod +x ./dockcross-linux-riscv64 + mv ./dockcross-linux-riscv64 /usr/local/bin + + # HIDAPI latest release + echo -e "${green}Updating HIDAPI${plain}" + cd ${hidapiDir} || exit + git checkout master + git pull + else + echo -e "${yellow}Skipping updates${plain}" +fi +echo -e "${green}------------------------------------------------------------------------${plain}" -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +# Build in hidapi project directory +cd ${hidapiDir} || exit # Windows environments -# 64-bit +# 64-bit x86-64 if [[ "$1" == "all" ]] || [[ "$1" == "windows" ]] || [[ "$1" == "win32-x86-64" ]] then - echo -e "\033[32mBuilding Windows 64-bit\033[0m" - dockcross-windows-shared-x64 bash -c 'sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev && sudo make clean && sudo ./bootstrap && sudo ./configure --host=x86_64-w64-mingw32 && sudo make' - if [[ "$?" -ne 0 ]] + echo -e "${green}Building Windows 64-bit${plain}" && git-clean + if ! dockcross-windows-shared-x64 bash -c 'sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev && sudo ./bootstrap && sudo ./configure --host=x86_64-w64-mingw32 && sudo make'; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/win32-x86-64/hidapi.dll + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/win32-x86-64/hidapi.dll exit else - echo -e "\033[32mOK\033[0m" - cp windows/.libs/libhidapi-0.dll ../../Java/Personal/hid4java/src/main/resources/win32-x86-64/hidapi.dll + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/win32-x86-64 + cp windows/.libs/libhidapi-0.dll ${hid4javaDir}/src/main/resources/win32-x86-64/hidapi.dll fi else - echo -e "\033[33mSkipping win32-x86-64\033[0m" + echo -e "${yellow}Skipping win32-x86-64${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" -# 32-bit +# 64-bit ARM win32-aarch64 +if [[ "$1" == "all" ]] || [[ "$1" == "windows" ]] || [[ "$1" == "win32-aarch64" ]] + then + echo -e "${green}Building Windows 64-bit ARM64 (aarch64)${plain}" && git-clean + if ! dockcross-windows-arm64 bash -c 'sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev && sudo ./bootstrap && sudo ./configure --host=aarch64-w64-mingw32 && sudo make'; + then + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/win32-aarch64/hidapi.dll + exit + else + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/win32-aarch64 + cp windows/.libs/libhidapi-0.dll ${hid4javaDir}/src/main/resources/win32-aarch64/hidapi.dll + fi + else + echo -e "${yellow}Skipping win32-aarch64${plain}" +fi +echo -e "${green}------------------------------------------------------------------------${plain}" + +# 32-bit x86 if [[ "$1" == "all" ]] || [[ "$1" == "windows" ]] || [[ "$1" == "win32-x86" ]] then - echo -e "\033[32mBuilding Windows 32-bit\033[0m" - dockcross-windows-shared-x86 bash -c 'sudo make clean && sudo ./bootstrap && sudo ./configure --host=i686-w64-mingw32 && sudo make' - if [[ "$?" -ne 0 ]] + echo -e "${green}Building Windows 32-bit${plain}" && git-clean + if ! dockcross-windows-shared-x86 bash -c 'sudo ./bootstrap && sudo ./configure --host=i686-w64-mingw32 && sudo make'; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/win32-x86/hidapi.dll + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/win32-x86/hidapi.dll else - echo -e "\033[32mOK\033[0m" - cp windows/.libs/libhidapi-0.dll ../../Java/Personal/hid4java/src/main/resources/win32-x86/hidapi.dll + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/win32-x86 + cp windows/.libs/libhidapi-0.dll ${hid4javaDir}/src/main/resources/win32-x86/hidapi.dll fi else - echo -e "\033[33mSkipping win32-x86\033[0m" + echo -e "${yellow}Skipping win32-x86${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" # Linux environments -# 64-bit +# 64-bit (x86-64/amd64) if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-x86-64" ]] then - echo -e "\033[32mBuilding Linux 64-bit\033[0m" + echo -e "${green}Building Linux 64-bit${plain}" && git-clean # Note the use of a double sudo apt-get update here - dockcross-linux-x64 bash -c 'sudo apt-get update || sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev && sudo make clean && sudo ./bootstrap && sudo ./configure && sudo make' - if [[ "$?" -ne 0 ]] + if ! dockcross-linux-x64 bash -c 'sudo apt-get update || sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev && sudo ./bootstrap && sudo ./configure && sudo make'; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/linux-x86-64/libhidapi.so - rm ../../Java/Personal/hid4java/src/main/resources/linux-amd64/libhidapi.so - rm ../../Java/Personal/hid4java/src/main/resources/linux-x86-64/libhidapi-libusb.so - rm ../../Java/Personal/hid4java/src/main/resources/linux-amd64/libhidapi-libusb.so + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/linux-x86-64/libhidapi.so + rm ${hid4javaDir}/src/main/resources/linux-amd64/libhidapi.so + rm ${hid4javaDir}/src/main/resources/linux-x86-64/libhidapi-libusb.so + rm ${hid4javaDir}/src/main/resources/linux-amd64/libhidapi-libusb.so else - echo -e "\033[32mOK\033[0m" - cp linux/.libs/libhidapi-hidraw.so ../../Java/Personal/hid4java/src/main/resources/linux-x86-64/libhidapi.so - cp linux/.libs/libhidapi-hidraw.so ../../Java/Personal/hid4java/src/main/resources/linux-amd64/libhidapi.so - cp libusb/.libs/libhidapi-libusb.so ../../Java/Personal/hid4java/src/main/resources/linux-x86-64/libhidapi-libusb.so - cp libusb/.libs/libhidapi-libusb.so ../../Java/Personal/hid4java/src/main/resources/linux-amd64/libhidapi-libusb.so + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/linux-x86-64 + cp linux/.libs/libhidapi-hidraw.so ${hid4javaDir}/src/main/resources/linux-x86-64/libhidapi.so + cp linux/.libs/libhidapi-hidraw.so ${hid4javaDir}/src/main/resources/linux-amd64/libhidapi.so + cp libusb/.libs/libhidapi-libusb.so ${hid4javaDir}/src/main/resources/linux-x86-64/libhidapi-libusb.so + cp libusb/.libs/libhidapi-libusb.so ${hid4javaDir}/src/main/resources/linux-amd64/libhidapi-libusb.so fi else - echo -e "\033[33mSkipping linux-x86-64\033[0m" + echo -e "${yellow}Skipping linux-x86-64${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" # 32-bit if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-x86" ]] then - echo -e "\033[32mBuilding Linux 32-bit\033[0m" - dockcross-linux-x86 bash -c 'sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev libudev-dev:i386 libusb-1.0-0-dev:i386 && sudo make clean && sudo ./bootstrap && sudo ./configure && sudo make' - if [[ "$?" -ne 0 ]] + echo -e "${green}Building Linux 32-bit${plain}" && git-clean + if ! dockcross-linux-x86 bash -c 'sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get --yes install libudev-dev libusb-1.0-0-dev libudev-dev:i386 libusb-1.0-0-dev:i386 && sudo ./bootstrap && sudo ./configure && sudo make'; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/linux-x86/libhidapi.so - rm ../../Java/Personal/hid4java/src/main/resources/linux-x86/libhidapi-libusb.so + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/linux-x86/libhidapi.so + rm ${hid4javaDir}/src/main/resources/linux-x86/libhidapi-libusb.so else - echo -e "\033[32mOK\033[0m" - cp linux/.libs/libhidapi-hidraw.so ../../Java/Personal/hid4java/src/main/resources/linux-x86/libhidapi.so - cp libusb/.libs/libhidapi-libusb.so ../../Java/Personal/hid4java/src/main/resources/linux-x86/libhidapi-libusb.so + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/linux-x86 + cp linux/.libs/libhidapi-hidraw.so ${hid4javaDir}/src/main/resources/linux-x86/libhidapi.so + cp libusb/.libs/libhidapi-libusb.so ${hid4javaDir}/src/main/resources/linux-x86/libhidapi-libusb.so fi else - echo -e "\033[33mSkipping linux-x86\033[0m" + echo -e "${yellow}Skipping linux-x86${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" # ARM environments # 64-bit (arm64/aarch64) if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-aarch64" ]] then - echo -e "\033[32mBuilding ARM64/aarch64 (ARMv8)\033[0m" - dockcross-linux-arm64 bash -c 'sudo dpkg --add-architecture arm64 && sudo apt-get update && sudo apt-get --yes install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libudev-dev:arm64 libusb-1.0-0-dev:arm64 && sudo make clean && sudo ./bootstrap && sudo ./configure --host=aarch64-linux-gnu CC=aarch64-linux-gnu-gcc && sudo make' - if [[ "$?" -ne 0 ]] + echo -e "${green}Building Linux ARM64/aarch64 ARMv8${plain}" && git-clean + if ! dockcross-linux-arm64 bash -c 'sudo dpkg --add-architecture arm64 && sudo apt-get update && sudo apt-get --yes install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libudev-dev:arm64 libusb-1.0-0-dev:arm64 && sudo ./bootstrap && sudo ./configure --host=aarch64-linux-gnu CC=aarch64-linux-gnu-gcc && sudo make'; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/linux-aarch64/libhidapi.so - rm ../../Java/Personal/hid4java/src/main/resources/linux-aarch64/libhidapi-libusb.so + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/linux-aarch64/libhidapi.so + rm ${hid4javaDir}/src/main/resources/linux-aarch64/libhidapi-libusb.so else - echo -e "\033[32mOK\033[0m" - cp linux/.libs/libhidapi-hidraw.so ../../Java/Personal/hid4java/src/main/resources/linux-aarch64/libhidapi.so - cp libusb/.libs/libhidapi-libusb.so ../../Java/Personal/hid4java/src/main/resources/linux-aarch64/libhidapi-libusb.so + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/linux-aarch64 + cp linux/.libs/libhidapi-hidraw.so ${hid4javaDir}/src/main/resources/linux-aarch64/libhidapi.so + cp libusb/.libs/libhidapi-libusb.so ${hid4javaDir}/src/main/resources/linux-aarch64/libhidapi-libusb.so fi else - echo -e "\033[33mSkipping linux-aarch64\033[0m" + echo -e "${yellow}Skipping linux-aarch64${plain}" +fi +echo -e "${green}------------------------------------------------------------------------${plain}" + +# 32-bit ARMv6 hard float (linux-arm) +if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-arm" ]] + then + echo -e "${yellow}Skipping linux-arm (use RPi direct instead)${plain}" + # It's much easier to just use the original hardware + else + echo -e "${yellow}Skipping linux-arm${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" -# 32-bit ARMv6 EABI (linux-armel) +# @Tresf +# 32-bit ARM soft float (linux-armel) if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-armel" ]] then - echo -e "\033[32mBuilding ARMv6 EABI\033[0m" - dockcross-linux-armv6 bash -c 'sudo dpkg --add-architecture armhf && sudo apt-get update && sudo apt-get --yes install gcc-arm-linux-gnueabihf libudev-dev:armhf libusb-1.0-0-dev:armhf && sudo make clean && sudo ./bootstrap && sudo ./configure --host=arm-linux-gnueabihf && sudo make' - if [[ "$?" -ne 0 ]] + echo -e "${green}Building ARM soft float${plain}" && git-clean + make clean &> /dev/null + + # Disabling building hidtest + sed -i'' -e 's/SUBDIRS \+= hidtest//g' Makefile.am + + arch="armel" + deps="echo \"Obtaining $arch dependencies...\"" + deps="$deps && sudo dpkg --add-architecture $arch" + deps="$deps && sudo apt-get update" + deps="$deps && sudo apt-get --yes install libudev-dev:$arch libusb-1.0-0-dev:$arch" + deps="$deps ; echo 'WARNING: Ignoring any errors from previous command'" + deps="$deps && cp /usr/include/libudev.h ./libudev.h" + deps="$deps && export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabi/pkgconfig/" + if ! dockcross-linux-armv5 bash -c "$deps && sudo ./bootstrap && sudo ./configure --host=aarch64-unknown-linux-gnueabi && sudo make"; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/linux-armel/libhidapi.so - rm ../../Java/Personal/hid4java/src/main/resources/linux-armel/libhidapi-libusb.so + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/linux-armel/libhidapi.so + rm ${hid4javaDir}/src/main/resources/linux-armel/libhidapi-libusb.so else - echo -e "\033[32mOK\033[0m" - cp linux/.libs/libhidapi-hidraw.so ../../Java/Personal/hid4java/src/main/resources/linux-armel/libhidapi.so - cp libusb/.libs/libhidapi-libusb.so ../../Java/Personal/hid4java/src/main/resources/linux-armel/libhidapi-libusb.so + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/linux-armel + cp linux/.libs/libhidapi-hidraw.so ${hid4javaDir}/src/main/resources/linux-armel/libhidapi.so + cp libusb/.libs/libhidapi-libusb.so ${hid4javaDir}/src/main/resources/linux-armel/libhidapi-libusb.so fi else - echo -e "\033[33mSkipping linux-armel\033[0m" + echo -e "${yellow}Skipping linux-armel${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" -# 32-bit ARMv7 hard float (linux-arm) -if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-arm" ]] +# RISC-V environments + +# @Tresf +# 64-bit (riscv64) +if [[ "$1" == "all" ]] || [[ "$1" == "linux" ]] || [[ "$1" == "linux-riscv64" ]] then - echo -e "\033[32mBuilding ARMv7 hard float\033[0m" - dockcross-linux-armv7 bash -c 'sudo dpkg --add-architecture armhf && sudo rm -Rf /var/lib/apt/lists && sudo apt-get update && sudo apt-get --yes install libudev-dev:armhf libusb-1.0-0-dev:armhf gcc-arm-linux-gnueabihf && sudo make clean && sudo ./bootstrap && sudo ./configure --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc && sudo make' - if [[ "$?" -ne 0 ]] + echo -e "${green}Building Linux riscv64${plain}" && git-clean + make clean &> /dev/null + + # Disabling building hidtest + sed -i'' -e 's/SUBDIRS \+= hidtest//g' Makefile.am + + # risv64 hasn't reached debian stable, fetch from "sid" + arch=riscv64 + debs=( + "libusb-1.0-0" + "libusb-1.0-0-dev" + "libhidapi-libusb0" + "libudev1" + "libudev-dev" + ) + deps="echo \"Obtaining $arch dependencies from Debian sid...\"" + deps="$deps && echo 'deb [arch=$arch] http://deb.debian.org/debian sid main' | sudo tee -a /etc/apt/sources.list" + deps="$deps && sudo apt-get update" + deps="$deps && mkdir -p debs && pushd debs" + deps="$deps && for deb in "${debs[@]}"; do echo \"- Downloading \$deb...\" && apt-get download \$deb:$arch && ar x \$deb* && tar -xf data.tar.xz && rm -f \$deb*.deb ; done" + deps="$deps && sudo cp -rf ./usr/* /usr/" + deps="$deps && popd" + deps="$deps && rm -rf debs" + deps="$deps && cp /usr/include/libudev.h ./libudev.h" + deps="$deps && export PKG_CONFIG_PATH=/usr/lib/$arch-linux-gnu/pkgconfig/" + + if ! dockcross-linux-riscv64 bash -c "sudo $deps && sudo ./bootstrap && sudo ./configure --host=riscv64-unknown-linux-gnu && sudo make"; then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/linux-arm/libhidapi.so + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/linux-riscv64/libhidapi.so + rm ${hid4javaDir}/src/main/resources/linux-riscv64/libhidapi-libusb.so else - echo -e "\033[32mOK\033[0m" - cp linux/.libs/libhidapi-hidraw.so ../../Java/Personal/hid4java/src/main/resources/linux-arm/libhidapi.so - cp libusb/.libs/libhidapi-libusb.so ../../Java/Personal/hid4java/src/main/resources/linux-arm/libhidapi-libusb.so + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/linux-riscv64 + cp linux/.libs/libhidapi-hidraw.so ${hid4javaDir}/src/main/resources/linux-riscv64/libhidapi.so + cp libusb/.libs/libhidapi-libusb.so ${hid4javaDir}/src/main/resources/linux-riscv64/libhidapi-libusb.so fi else - echo -e "\033[33mSkipping linux-arm\033[0m" + echo -e "${yellow}Skipping linux-riscv64${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" +echo -e "${green}------------------------------------------------------------------------${plain}" -# OS X environments +# macOS environments (require local build) + +# Darwin ARM64 (local) +if [[ "$1" == "all" ]] || [[ "$1" == "darwin" ]] || [[ "$1" == "darwin-aarch64" ]] + then + # Check if build is possible with local environment + if [[ "${hardwareName}" == "x86_64" ]] + then + echo -e "${yellow}Building macOS Darwin x86 (x86-64) requires change of architecture.${plain}" + echo -e "${yellow} Re-run with 'arch -arm64 zsh ./build-hidapi.sh darwin-aarch64'${plain}" + else + echo -e "${green}Building macOS Darwin ARM64 (aarch64)${plain}" && git-clean + ./bootstrap + ./configure + if ! make; + then + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/darwin-aarch64/libhidapi.dylib + else + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/darwin-aarch64 + cp mac/.libs/libhidapi.0.dylib ${hid4javaDir}/src/main/resources/darwin-aarch64/libhidapi.dylib + fi + fi +fi -# Darwin -if [[ "$1" == "all" ]] || [[ "$1" == "osx" ]] || [[ "$1" == "darwin" ]] +# Darwin Intel (local) +if [[ "$1" == "all" ]] || [[ "$1" == "darwin" ]] || [[ "$1" == "darwin-x86-64" ]] then - echo -e "\033[32mBuilding OS X Darwin\033[0m" - make clean - ./bootstrap - ./configure - make - if [[ "$?" -ne 0 ]] + # Check if build is possible with local environment + if [[ "${hardwareName}" == "arm64" ]] then - echo -e "\033[31mFailed\033[0m - Removing damaged targets" - rm ../../Java/Personal/hid4java/src/main/resources/darwin/libhidapi.dylib + echo -e "${yellow}Building macOS Darwin x86 (x86-64) requires change of architecture.${plain}" + echo -e "${yellow} Re-run with 'arch -x86_64 zsh ./build-hidapi.sh darwin-x86-64'${plain}" else - echo -e "\033[32mOK\033[0m" - cp mac/.libs/libhidapi.0.dylib ../../Java/Personal/hid4java/src/main/resources/darwin/libhidapi.dylib + echo -e "${green}Building macOS Darwin Intel (x86-64)${plain}" && git-clean + make clean + ./bootstrap + ./configure + if ! make; + then + echo -e "${red}Failed${plain} - Removing damaged targets" + rm ${hid4javaDir}/src/main/resources/darwin-x86-64/libhidapi.dylib + else + echo -e "${green}OK${plain}" + mkdir -p ${hid4javaDir}/src/main/resources/darwin-x86-64 + cp mac/.libs/libhidapi.0.dylib ${hid4javaDir}/src/main/resources/darwin-x86-64/libhidapi.dylib + fi fi else - echo -e "\033[33mSkipping darwin\033[0m" + echo -e "${yellow}Skipping darwin-x86-64${plain}" fi -echo -e "\033[32m------------------------------------------------------------------------\033[0m" + +echo -e "${green}------------------------------------------------------------------------${plain}" + +# Report in hid4java project directory +cd ${hid4javaDir} || exit # List all file info -echo -e "\033[32mResulting build files placed in hid4java:\033[0m" +if [[ "$1" == "update" ]] + then + echo -e "${yellow}Skipping file report${plain}" + else + echo -e "${green}Reporting on current hidapi libraries in hid4java:${plain}" -# Windows environments -echo -e "\033[32mWindows\033[0m" + # Windows environments + echo -e "${green}Windows${plain}" -echo -e "\033[32mwin32-x86-64\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/win32-x86-64/hidapi.dll + echo -e "${green}win32-x86-64${plain}" + report "src/main/resources/win32-x86-64/hidapi.dll" -echo -e "\033[32mwin32-x86\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/win32-x86/hidapi.dll + echo -e "${green}win32-x86${plain}" + report "src/main/resources/win32-x86/hidapi.dll" -echo -e "\033[32m------------------------------------------------------------------------\033[0m" + echo -e "${green}win32-aarch64${plain}" + report "src/main/resources/win32-aarch64/hidapi.dll" -# Linux environments -echo -e "\033[32mLinux\033[0m" + echo -e "${green}------------------------------------------------------------------------${plain}" + + # Linux environments + echo -e "${green}Linux${plain}" + + echo -e "${green}linux-x86-64${plain}" + report "src/main/resources/linux-x86-64/libhidapi.so" + report "src/main/resources/linux-x86-64/libhidapi-libusb.so" -echo -e "\033[32mlinux-x86-64\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/linux-x86-64/libhidapi.so -file -b ../../Java/Personal/hid4java/src/main/resources/linux-x86-64/libhidapi-libusb.so + echo -e "${green}linux-amd64${plain}" + report "src/main/resources/linux-amd64/libhidapi.so" + report "src/main/resources/linux-amd64/libhidapi-libusb.so" -echo -e "\033[32mlinux-amd64\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/linux-amd64/libhidapi.so -file -b ../../Java/Personal/hid4java/src/main/resources/linux-amd64/libhidapi-libusb.so + echo -e "${green}linux-x86${plain}" + report "src/main/resources/linux-x86/libhidapi.so" + report "src/main/resources/linux-x86/libhidapi-libusb.so" -echo -e "\033[32mlinux-x86\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/linux-x86/libhidapi.so -file -b ../../Java/Personal/hid4java/src/main/resources/linux-x86/libhidapi-libusb.so + echo -e "${green}------------------------------------------------------------------------${plain}" -echo -e "\033[32m------------------------------------------------------------------------\033[0m" + # ARM + echo -e "${green}ARM${plain}" -# ARM -echo -e "\033[32mARM\033[0m" + echo -e "${green}linux-arm${plain}" + report "src/main/resources/linux-arm/libhidapi.so" + report "src/main/resources/linux-arm/libhidapi-libusb.so" -echo -e "\033[32mlinux-arm\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/linux-arm/libhidapi.so -file -b ../../Java/Personal/hid4java/src/main/resources/linux-arm/libhidapi-libusb.so + echo -e "${green}linux-armel${plain}" + report "src/main/resources/linux-armel/libhidapi.so" + report "src/main/resources/linux-armel/libhidapi-libusb.so" -echo -e "\033[32mlinux-armel\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/linux-armel/libhidapi.so -file -b ../../Java/Personal/hid4java/src/main/resources/linux-armel/libhidapi-libusb.so + echo -e "${green}linux-aarch64${plain}" + report "src/main/resources/linux-aarch64/libhidapi.so" + report "src/main/resources/linux-aarch64/libhidapi-libusb.so" -echo -e "\033[32mlinux-aarch64\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/linux-aarch64/libhidapi.so -file -b ../../Java/Personal/hid4java/src/main/resources/linux-aarch64/libhidapi-libusb.so + # RISC -echo -e "\033[32m------------------------------------------------------------------------\033[0m" + echo -e "${green}linux-riscv64${plain}" + report "src/main/resources/linux-riscv64/libhidapi.so" + report "src/main/resources/linux-riscv64/libhidapi-libusb.so" -# OS X -echo -e "\033[32mOS X\033[0m" + echo -e "${green}------------------------------------------------------------------------${plain}" -echo -e "\033[32mdarwin\033[0m" -file -b ../../Java/Personal/hid4java/src/main/resources/darwin/libhidapi.dylib + # macOS + echo -e "${green}macOS${plain}" -echo -e "\033[32m------------------------------------------------------------------------\033[0m" + echo -e "${green}darwin-x86-64${plain}" + report "src/main/resources/darwin-x86-64/libhidapi.dylib" + otool -l src/main/resources/darwin-x86-64/libhidapi.dylib | grep -E 'LC_VERSION_MIN_MACOSX|LC_BUILD_VERSION' -A4 -echo -e "\033[32mDone - Check all OK in summary above.\033[0m" + echo -e "${green}darwin-aarch64${plain}" + report "src/main/resources/darwin-aarch64/libhidapi.dylib" + otool -l src/main/resources/darwin-aarch64/libhidapi.dylib | grep -E 'LC_VERSION_MIN_MACOSX|LC_BUILD_VERSION' -A4 + + echo -e "${green}------------------------------------------------------------------------${plain}" + + echo -e "${green}Done - Check all OK in summary above.${plain}" +fi +echo -e "${green}------------------------------------------------------------------------${plain}" diff --git a/pom.xml b/pom.xml index 8f5c641..16a534a 100644 --- a/pom.xml +++ b/pom.xml @@ -51,30 +51,29 @@ scm:git:git@github.com:gary-rowe/hid4java.git scm:git:git@github.com:gary-rowe/hid4java.git git@github.com:gary-rowe/hid4java.git + HEAD UTF-8 + + 1.8 + + 1.8 - + release-sign-artifacts - - - performRelease - true - - org.apache.maven.plugins maven-gpg-plugin - 1.6 + 3.2.3 sign-artifacts @@ -93,36 +92,19 @@ - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - true - true - - - + org.apache.maven.plugins maven-surefire-plugin - 2.16 - - - **/*FunctionalTest.java - - + 3.1.2 org.apache.maven.plugins maven-source-plugin - 2.2.1 + 3.3.0 attach-sources @@ -137,9 +119,9 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 + 3.6.3 - 7 + 8 @@ -155,10 +137,11 @@ org.apache.maven.plugins maven-release-plugin - 2.5.3 + 3.0.1 true forked-path + release-sign-artifacts @@ -166,7 +149,7 @@ org.apache.maven.plugins maven-jar-plugin - 2.6 + 3.3.0 @@ -184,14 +167,17 @@ net.java.dev.jna jna - 5.6.0 + + + + [5.8.0,) org.junit.jupiter junit-jupiter - 5.7.0-M1 + 5.10.2 test diff --git a/src/main/java/org/hid4java/HidDevice.java b/src/main/java/org/hid4java/HidDevice.java index 2e35020..6a0328a 100644 --- a/src/main/java/org/hid4java/HidDevice.java +++ b/src/main/java/org/hid4java/HidDevice.java @@ -185,7 +185,7 @@ private boolean isDataRead() { /** * Attempt to read all data from the device input buffer as part * of the automatic data read process - * + *
* Will fire attach/detach events as appropriate. */ private synchronized void dataRead() { @@ -200,8 +200,8 @@ private synchronized void dataRead() { /** * The "path" is well-supported across Windows, Mac and Linux so makes a * better choice for a unique ID - * - * See #8 for details + *
+ * See hid4java issue #8 for details * * @return A unique device ID made up from vendor ID, product ID and serial number * @since 0.1.0 @@ -343,11 +343,11 @@ public void close() { /** * Set the device handle to be non-blocking - * + *
* In non-blocking mode calls to hid_read() will return immediately with a * value of 0 if there is no data to be read. In blocking mode, hid_read() * will wait (block) until there is data to read before returning - * + *
* Non-blocking can be turned on and off at any time * * @param nonBlocking True if non-blocking mode is required @@ -362,7 +362,7 @@ public void setNonBlocking(boolean nonBlocking) { /** * Read an Input report from a HID device - * + *
* Input reports are returned to the host through the INTERRUPT IN endpoint. * The first byte will contain the Report number if the device uses numbered * reports @@ -382,7 +382,7 @@ public int read(byte[] data) { /** * Read an Input report from a HID device - * + *
* Input reports are returned to the host through the INTERRUPT IN endpoint. * The first byte will contain the Report number if the device uses numbered * reports @@ -432,7 +432,7 @@ public Byte[] read(int amountToRead) { /** * Read an Input report (64 bytes) from a HID device with a 1000ms timeout. - * + *
* Input reports are returned to the host through the INTERRUPT IN endpoint. * The first byte will contain the Report number if the device uses numbered * reports. @@ -501,11 +501,10 @@ public byte[] readAll(int timeoutMillis) { /** * Get a feature report from a HID device - * * Under the covers the HID library will set the first byte of data[] to the * Report ID of the report to be read. Upon return, the first byte will * still contain the Report ID, and the report data will start in data[1] - * + *
* This method handles all the wide string and array manipulation for you * * @param data The buffer to contain the report @@ -523,19 +522,19 @@ public int getFeatureReport(byte[] data, byte reportId) { /** * Send a Feature report to the device - * + *
* Under the covers, feature reports are sent over the Control endpoint as a * Set_Report transfer. The first byte of data[] must contain the Report ID. * For devices which only support a single report, this must be set to 0x0. * The remaining bytes contain the report data - * + *
* Since the Report ID is mandatory, calls to hid_send_feature_report() will * always contain one more byte than the report contains. For example, if a * hid report is 16 bytes long, 17 bytes must be passed to * hid_send_feature_report(): the Report ID (or 0x0, for devices which do * not use numbered reports), followed by the report data (16 bytes). In * this example, the length passed in would be 17 - * + *
* This method handles all the array manipulation for you * * @param data The feature report data (will be widened and have the report @@ -567,8 +566,30 @@ public String getIndexedString(int index) { } /** - * Write the message to the HID API without zero byte padding. + * Get a report descriptor from a HID device + *
+ * A report descriptor provides significant detail on the characteristics of + * an attached device using a defined format. + *
+ * You may find these supporting documents helpful: + * HID Usage Tables 1.5 (PDF) + *
+ * Device Class Definition HID 1.11 (PDF) * + * @param buffer The buffer for descriptor (4096 bytes recommended). + * @return A non-negative number of bytes actually copied, or -1 on error. + * @since 0.14.0 hidapi + */ + public int getReportDescriptor(byte[] buffer) { + if (isClosed()) { + throw new IllegalStateException("Device has not been opened"); + } + return HidApi.getReportDescriptor(hidDeviceStructure, buffer, buffer.length); + } + + /** + * Write the message to the HID API without zero byte padding. + *
* Note that the report ID will be prefixed to the HID packet as per HID rules. * * @param message The message @@ -586,7 +607,7 @@ public int write(byte[] message, int packetLength, byte reportId) { /** * Write the message to the HID API with optional zero byte padding to packet length. - * + *
* Note that the report ID will be prefixed to the HID packet as per HID rules. * * @param message The message diff --git a/src/main/java/org/hid4java/HidDeviceManager.java b/src/main/java/org/hid4java/HidDeviceManager.java index 67918f2..e835f4d 100644 --- a/src/main/java/org/hid4java/HidDeviceManager.java +++ b/src/main/java/org/hid4java/HidDeviceManager.java @@ -60,7 +60,7 @@ class HidDeviceManager { /** * The device enumeration thread - * + *
* We use a Thread instead of Executor since it may be stopped/paused/restarted frequently * and executors are more heavyweight in this regard */ @@ -91,10 +91,10 @@ class HidDeviceManager { /** * Starts the manager - * + *
* If already started (scanning) it will immediately return without doing anything - * - * Otherwise this will perform a one-off scan of all devices then if the scan interval + *
+ * Otherwise, this will perform a one-off scan of all devices then if the scan interval * is zero will stop there or will start the scanning daemon thread at the required interval. * * @throws HidException If something goes wrong (such as Hidapi not initialising correctly) @@ -116,8 +116,9 @@ public void start() { /** * Stop the scan thread and close all attached devices - * - * This is normally part of a general application shutdown + *
+ * This is normally part of a general application shutdown and will + * also clear the attached devices map */ public synchronized void stop() { @@ -128,12 +129,15 @@ public synchronized void stop() { hidDevice.close(); } + // Remove all entries from the attached devices + attachedDevices.clear(); + } /** * Updates the device list by adding newly connected devices to it and by * removing no longer connected devices. - * + *
* Will fire attach/detach events as appropriate. */ public synchronized void scan() { @@ -261,6 +265,14 @@ private synchronized void stopScanThread() { if (isScanning()) { scanThread.interrupt(); + // Wait up to 50ms for scanThread to terminate to avoid + // spurious return values from isScanning() + // See hid4java issue #125 + try { + scanThread.join(50); + } catch (InterruptedException e) { + // Ignore and continue + } } } @@ -343,4 +355,4 @@ public void run() { } -} \ No newline at end of file +} diff --git a/src/main/java/org/hid4java/HidServices.java b/src/main/java/org/hid4java/HidServices.java index e0576c6..10eb595 100644 --- a/src/main/java/org/hid4java/HidServices.java +++ b/src/main/java/org/hid4java/HidServices.java @@ -205,4 +205,13 @@ public static String getVersion() { return value; } } + + /** + * Returns the full version of the underlying hidapi library + * + * @return The version in major.minor.patch format + */ + public static String getNativeVersion() { + return HidApi.getVersion(); + } } diff --git a/src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java b/src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java new file mode 100644 index 0000000..7998fc1 --- /dev/null +++ b/src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java @@ -0,0 +1,24 @@ +package org.hid4java.jna; + +import com.sun.jna.Native; +import com.sun.jna.WString; + +public interface DarwinHidApiLibrary extends HidrawHidApiLibrary { + + DarwinHidApiLibrary INSTANCE = Native.load("hidapi", DarwinHidApiLibrary.class); + + /** + * Changes the behavior of all further calls to {@link #hid_open(short, short, WString)} or {@link #hid_open_path(String)}. + *
+ * All devices opened by HIDAPI with {@link #hid_open(short, short, WString)} or {@link #hid_open_path(String)} + * are opened in exclusive mode per default. + *
+ * Calling this function before {@link #hid_init()} or after {@link #hid_exit()} has no effect. + * + * @since hidapi 0.12.0 + * @param openExclusive When set to 0 - all further devices will be opened in non-exclusive mode. + * Otherwise - all further devices will be opened in exclusive mode. + */ + void hid_darwin_set_open_exclusive(int openExclusive); + +} diff --git a/src/main/java/org/hid4java/jna/HidApi.java b/src/main/java/org/hid4java/jna/HidApi.java index 773b9ca..d950e8e 100644 --- a/src/main/java/org/hid4java/jna/HidApi.java +++ b/src/main/java/org/hid4java/jna/HidApi.java @@ -56,17 +56,25 @@ public class HidApi { /** * Enables use of the libusb variant of the hidapi native library when running on a Linux platform. - *

+ *
* The default is hidraw which enables Bluetooth devices but requires udev rules. */ public static boolean useLibUsbVariant = false; + /** + * When false - all devices will be opened in exclusive mode. (Default) + * When true - all devices will be opened in non-exclusive mode. + *
+ * See {@link DarwinHidApiLibrary#hid_darwin_set_open_exclusive(int)} for more information. + */ + public static boolean darwinOpenDevicesNonExclusive = false; + /** * Enables HID traffic logging to stdout to assist debugging. This will show all bytes (including the extra report ID) * that were sent or received via HIDAPI buffers. It does not log direct string calls (e.g. getEnumeratedString()). - * + *
* Format is '>' for host to device then '[count]' then hex bytes. - * + *
* This may present a security issue if left enabled in production, although a packet sniffer would see the same data. */ public static boolean logTraffic = false; @@ -82,7 +90,6 @@ public class HidApi { * @param vendor The vendor ID * @param product The product ID * @param serialNumber The serial number - * * @return The device or null if not found */ public static HidDeviceStructure open(int vendor, int product, String serialNumber) { @@ -110,11 +117,17 @@ public static void init() { if (useLibUsbVariant && Platform.isLinux()) { hidApiLibrary = LibusbHidApiLibrary.INSTANCE; + } else if (Platform.isMac()) { + hidApiLibrary = DarwinHidApiLibrary.INSTANCE; } else { hidApiLibrary = HidrawHidApiLibrary.INSTANCE; } hidApiLibrary.hid_init(); + + if (hidApiLibrary instanceof DarwinHidApiLibrary) { + ((DarwinHidApiLibrary) hidApiLibrary).hid_darwin_set_open_exclusive(darwinOpenDevicesNonExclusive ? 0 : 1); + } } /** @@ -128,7 +141,6 @@ public static void exit() { * Open a HID device by its path name * * @param path The device path (e.g. "0003:0002:00") - * * @return The device or null if not found */ public static HidDeviceStructure open(String path) { @@ -154,7 +166,6 @@ public static void close(HidDeviceStructure device) { * * @param vendor The vendor ID * @param product The product ID - * * @return The device info of the matching device */ public static HidDeviceInfoStructure enumerateDevices(int vendor, int product) { @@ -176,7 +187,6 @@ public static void freeEnumeration(HidDeviceInfoStructure list) { /** * @param device The HID device structure - * * @return A string describing the last error which occurred */ public static String getLastErrorMessage(HidDeviceStructure device) { @@ -192,7 +202,6 @@ public static String getLastErrorMessage(HidDeviceStructure device) { /** * @param device The HID device - * * @return The device manufacturer string */ public static String getManufacturer(HidDeviceStructure device) { @@ -209,7 +218,6 @@ public static String getManufacturer(HidDeviceStructure device) { /** * @param device The HID device - * * @return The device product ID */ public static String getProductId(HidDeviceStructure device) { @@ -226,7 +234,6 @@ public static String getProductId(HidDeviceStructure device) { /** * @param device The HID device - * * @return The device serial number */ public static String getSerialNumber(HidDeviceStructure device) { @@ -244,15 +251,14 @@ public static String getSerialNumber(HidDeviceStructure device) { /** * Set the device handle to be non-blocking - * + *
* In non-blocking mode calls to hid_read() will return immediately with a value of 0 if there is no data to be read. * In blocking mode, hid_read() will wait (block) until there is data to read before returning - * + *
* Non-blocking can be turned on and off at any time * * @param device The HID device * @param nonBlocking True if non-blocking mode is required - * * @return True if successful */ public static boolean setNonBlocking(HidDeviceStructure device, boolean nonBlocking) { @@ -268,7 +274,6 @@ public static boolean setNonBlocking(HidDeviceStructure device, boolean nonBlock * * @param device The HID device * @param buffer The buffer to read into (allow an extra byte if device supports multiple report IDs) - * * @return The actual number of bytes read and -1 on error. If no packet was available to be read * and the handle is in non-blocking mode, this function returns 0. */ @@ -296,7 +301,6 @@ public static int read(HidDeviceStructure device, byte[] buffer) { * @param device The HID device * @param buffer The buffer to read into * @param timeoutMillis The number of milliseconds to wait before giving up - * * @return The actual number of bytes read and -1 on error. If no packet was available to be read within * the timeout period returns 0. */ @@ -320,9 +324,8 @@ public static int read(HidDeviceStructure device, byte[] buffer, int timeoutMill /** * Get a feature report from a HID device - * * HID API notes - * + *
* Under the covers the HID library will set the first byte of data[] to the Report ID of the report to be read. * Upon return, the first byte will still contain the Report ID, and the report data will start in data[1] * This method handles all the wide string and array manipulation for you @@ -330,7 +333,6 @@ public static int read(HidDeviceStructure device, byte[] buffer, int timeoutMill * @param device The HID device * @param data The buffer to contain the report * @param reportId The report ID (or (byte) 0x00) - * * @return The number of bytes read plus one for the report ID (which has been removed from the first byte), or -1 on error. */ public static int getFeatureReport(HidDeviceStructure device, byte[] data, byte reportId) { @@ -359,25 +361,23 @@ public static int getFeatureReport(HidDeviceStructure device, byte[] data, byte /** * Send a Feature report to the device using a simplified interface - * * HID API notes - * + *
* Under the covers, feature reports are sent over the Control endpoint as a Set_Report transfer. * The first byte of data[] must contain the Report ID. For devices which only support a single report, * this must be set to 0x0. The remaining bytes contain the report data * Since the Report ID is mandatory, calls to hid_send_feature_report() will always contain one more byte than * the report contains. - * + *
* For example, if a hid report is 16 bytes long, 17 bytes must be passed to * hid_send_feature_report(): the Report ID (or 0x00, for devices which do not use numbered reports), followed by * the report data (16 bytes). In this example, the bytes written would be 17. - * + *
* This method handles all the array manipulation for you * * @param device The HID device * @param data The feature report data (will be widened and have the report ID pre-pended) * @param reportId The report ID (or (byte) 0x00) - * * @return This function returns the actual number of bytes written and -1 on error. */ public static int sendFeatureReport(HidDeviceStructure device, byte[] data, byte reportId) { @@ -399,19 +399,17 @@ public static int sendFeatureReport(HidDeviceStructure device, byte[] data, byte /** * Write an Output report to a HID device using a simplified interface - * * HID API notes - * + *
* In USB HID the first byte of the data packet must contain the Report ID. * For devices which only support a single report, this must be set to 0x00. * The remaining bytes contain the report data. Since the Report ID is mandatory, * calls to hid_write() will always contain one more byte than the report * contains. - * + *
* For example, if a HID report is 16 bytes long, 17 bytes must be passed to hid_write(), * the Report ID (or 0x00, for devices with a single report), followed by the report data (16 bytes). * In this example, the length passed in would be 17. - * * hid_write() will send the data on the first OUT endpoint, if one exists. * If it does not, it will send the data through the Control Endpoint (Endpoint 0) * @@ -419,7 +417,6 @@ public static int sendFeatureReport(HidDeviceStructure device, byte[] data, byte * @param data The report data to write (should not include the Report ID) * @param len The length of the report data (should not include the Report ID) * @param reportId The report ID (or (byte) 0x00) - * * @return The number of bytes written, or -1 if an error occurs */ public static int write(HidDeviceStructure device, byte[] data, int len, byte reportId) { @@ -454,7 +451,6 @@ public static int write(HidDeviceStructure device, byte[] data, int len, byte re * * @param device The HID device * @param idx The index - * * @return The string */ public static String getIndexedString(HidDeviceStructure device, int idx) { @@ -469,8 +465,26 @@ public static String getIndexedString(HidDeviceStructure device, int idx) { } /** - * - * @param buffer The buffer to serialise for traffic + * Get a report descriptor from a HID device + *
+ * User has to provide a preallocated buffer (4096 bytes recommended) where descriptor will be copied to + *
+ * @param device The HID device + * @param buffer The buffer to copy descriptor into. + * @param size The size of the buffer in bytes. + * @return A non-negative number of bytes actually copied, or -1 on error. + * @since 0.14.0 hidapi + */ + public static int getReportDescriptor(HidDeviceStructure device, byte[] buffer, int size) { + + if (device == null) { + return -1; + } + return hidApiLibrary.hid_get_report_descriptor(device.ptr(), buffer, size); + } + + /** + * @param buffer The buffer to serialise for traffic * @param isWrite True if writing (from host to device) */ private static void logTraffic(WideStringBuffer buffer, boolean isWrite) { @@ -488,4 +502,17 @@ private static void logTraffic(WideStringBuffer buffer, boolean isWrite) { } } + /** + * Returns the full version of the underlying hidapi library + * + * @return The version in major.minor.patch format + * @see org.hid4java.HidServices#getNativeVersion + */ + public static String getVersion() { + if (hidApiLibrary == null) { + init(); + } + return hidApiLibrary.hid_version_str(); + } + } diff --git a/src/main/java/org/hid4java/jna/HidApiLibrary.java b/src/main/java/org/hid4java/jna/HidApiLibrary.java index 46b2b6e..88266c5 100644 --- a/src/main/java/org/hid4java/jna/HidApiLibrary.java +++ b/src/main/java/org/hid4java/jna/HidApiLibrary.java @@ -32,6 +32,7 @@ /** * JNA library interface to act as the proxy for the underlying native library * This approach removes the need for any JNI or native code + * * @since 0.1.0 */ public interface HidApiLibrary extends Library { @@ -47,21 +48,20 @@ public interface HidApiLibrary extends Library { /** * Finalize the HIDAPI library. - *

- * This function frees all of the static data associated with HIDAPI. It should be called + *
+ * This function frees all the static data associated with HIDAPI. It should be called * at the end of execution to avoid memory leaks. */ void hid_exit(); /** * Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number. - * + *
* If serial_number is NULL, the first device with the specified VID and PID is opened. * - * @param vendor_id The vendor ID - * @param product_id The product ID + * @param vendor_id The vendor ID + * @param product_id The product ID * @param serial_number The serial number (or null for wildcard) - * * @return A pointer to a HidDevice on success or null on failure */ Pointer hid_open(short vendor_id, short product_id, WString serial_number); @@ -77,21 +77,19 @@ public interface HidApiLibrary extends Library { * Get a string describing the last error which occurred. * * @param device A device handle - * * @return A string containing the last error which occurred or null if none has occurred. */ Pointer hid_error(Pointer device); /** * Read an Input report from a HID device. - * + *
* Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number * if the device uses numbered reports. * * @param device A device handle returned from hid_open(). * @param bytes A buffer to put the read data into. * @param length The number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number. - * * @return This function returns the actual number of bytes read and -1 on error. If no packet was available to be read * and the handle is in non-blocking mode this function returns 0. */ @@ -99,7 +97,7 @@ public interface HidApiLibrary extends Library { /** * Read an Input report from a HID device with timeout. - * + *
* Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number * if the device uses numbered reports. * @@ -107,7 +105,6 @@ public interface HidApiLibrary extends Library { * @param bytes A buffer to put the read data into. * @param length The number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number. * @param timeout The timeout in milliseconds or -1 for blocking wait. - * * @return This function returns the actual number of bytes read and -1 on error. If no packet was available to be read within * the timeout period, this function returns 0. */ @@ -115,51 +112,49 @@ public interface HidApiLibrary extends Library { /** * Write an Output report to a HID device. - * + *
* The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. * The remaining bytes contain the report data. - * + *
* Since the Report ID is mandatory, calls to hid_write() will always contain one more byte than the report contains. - * + *
* For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_write(), the Report ID (or 0x0, for devices with * a single report), followed by the report data (16 bytes). In this example, the length passed in would be 17. - * + *
* hid_write() will send the data on the first OUT endpoint, if one exists. If it does not, it will send the data through the * Control Endpoint (Endpoint 0). * * @param device A device handle * @param data the data to send, including the report number as the first byte * @param len The length in bytes of the data to send - * * @return The actual number of bytes written, -1 on error */ int hid_write(Pointer device, WideStringBuffer.ByReference data, int len); /** * Get a feature report from a HID device. - * + *
* Set the first byte of data[] to the Report ID of the report to be read. Make sure to allow space for this extra byte in data[]. * Upon return, the first byte will still contain the Report ID, and the report data will start in data[1]. * * @param device A device handle * @param data A buffer to put the read data into, including the Report ID. Set the first byte of data[] to the Report ID of the report to be read, or set it to zero if your device does not use numbered reports. * @param length The number of bytes to read, including an extra byte for the report ID. The buffer can be longer than the actual report. - * * @return The number of bytes read plus one for the report ID (which is still in the first byte), or -1 on error */ int hid_get_feature_report(Pointer device, WideStringBuffer.ByReference data, int length); /** * Send a Feature report to the device. - * + *
* Feature reports are sent over the Control endpoint as a Set_Report transfer. - * + *
* The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. - * + *
* The remaining bytes contain the report data. - * + *
* Since the Report ID is mandatory, calls to hid_send_feature_report() will always contain one more byte than the report contains. - * + *
* For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_feature_report(): * the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). * In this example, the length passed in would be 17. @@ -167,7 +162,6 @@ public interface HidApiLibrary extends Library { * @param device The device handle * @param data The data to send, including the report number as the first byte * @param length The length in bytes of the data to send, including the report number - * * @return The actual number of bytes written, -1 on error */ int hid_send_feature_report(Pointer device, WideStringBuffer.ByReference data, int length); @@ -179,18 +173,27 @@ public interface HidApiLibrary extends Library { * @param idx The index of the string to get * @param string A wide string buffer to put the data into * @param len The length of the buffer in multiples of wchar_t - * * @return 0 on success, -1 on failure */ int hid_get_indexed_string(Pointer device, int idx, WideStringBuffer.ByReference string, int len); + /** + * Get the manufacturer string from a HID device + * + * @param device the device handle + * @param buffer A byte buffer to put the data into + * @param size The length of the buffer in multiple of wchar_t + * @return 0 on success, -1 on failure + */ + @SuppressWarnings("UnusedReturnValue") + int hid_get_report_descriptor(Pointer device, byte[] buffer, int size); + /** * Get the manufacturer string from a HID device * * @param device the device handle * @param str A wide string buffer to put the data into * @param len The length of the buffer in multiple of wchar_t - * * @return 0 on success, -1 on failure */ @SuppressWarnings("UnusedReturnValue") @@ -202,7 +205,6 @@ public interface HidApiLibrary extends Library { * @param device the device handle * @param str A wide string buffer to put the data into * @param len The length of the buffer in multiple of wchar_t - * * @return 0 on success, -1 on failure */ @SuppressWarnings("UnusedReturnValue") @@ -214,7 +216,6 @@ public interface HidApiLibrary extends Library { * @param device the device handle * @param str A wide string buffer to put the data into * @param len The length of the buffer in multiple of wchar_t - * * @return 0 on success, -1 on failure */ @SuppressWarnings("UnusedReturnValue") @@ -222,32 +223,30 @@ public interface HidApiLibrary extends Library { /** * Set the device handle to be non-blocking. - * + *
* In non-blocking mode calls to hid_read() will return immediately with a value of 0 if there is no data to be read. - * + *
* In blocking mode, hid_read() will wait (block) until there is data to read before returning. - * + *
* Nonblocking can be turned on and off at any time. * * @param device The device handle * @param nonblock 0 disables non-blocking, 1 enables non-blocking - * * @return 0 on success, -1 on error */ int hid_set_nonblocking(Pointer device, int nonblock); /** * Enumerate the HID Devices. - * + *
* This function returns a linked list of all the HID devices attached to the system which match vendor_id and product_id. - * + *
* If vendor_id is set to 0 then any vendor matches. If product_id is set to 0 then any product matches. - * + *
* If vendor_id and product_id are both set to 0, then all HID devices will be returned. * * @param vendor_id The vendor ID * @param product_id The product ID - * * @return A linked list of all discovered matching devices */ HidDeviceInfoStructure hid_enumerate(short vendor_id, short product_id); @@ -261,12 +260,18 @@ public interface HidApiLibrary extends Library { /** * Open a HID device by its path name. - * + *
* The path name be determined by calling hid_enumerate(), or a platform-specific path name can be used (eg: "/dev/hidraw0" on Linux). * * @param path The path name - * * @return The pointer if successful or null */ Pointer hid_open_path(String path); -} \ No newline at end of file + + /** + * Get version of hidapi library + * + * @return Version in major.minor.patch format + */ + String hid_version_str(); +} diff --git a/src/main/resources/darwin/libhidapi.dylib b/src/main/resources/darwin-aarch64/libhidapi.dylib similarity index 52% rename from src/main/resources/darwin/libhidapi.dylib rename to src/main/resources/darwin-aarch64/libhidapi.dylib index 91389ae..696717b 100755 Binary files a/src/main/resources/darwin/libhidapi.dylib and b/src/main/resources/darwin-aarch64/libhidapi.dylib differ diff --git a/src/main/resources/darwin-x86-64/libhidapi.dylib b/src/main/resources/darwin-x86-64/libhidapi.dylib new file mode 100755 index 0000000..b285171 Binary files /dev/null and b/src/main/resources/darwin-x86-64/libhidapi.dylib differ diff --git a/src/main/resources/linux-aarch64/libhidapi-libusb.so b/src/main/resources/linux-aarch64/libhidapi-libusb.so index 3999076..5b94a91 100755 Binary files a/src/main/resources/linux-aarch64/libhidapi-libusb.so and b/src/main/resources/linux-aarch64/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-aarch64/libhidapi.so b/src/main/resources/linux-aarch64/libhidapi.so index 8de0ea5..f43f900 100755 Binary files a/src/main/resources/linux-aarch64/libhidapi.so and b/src/main/resources/linux-aarch64/libhidapi.so differ diff --git a/src/main/resources/linux-amd64/libhidapi-libusb.so b/src/main/resources/linux-amd64/libhidapi-libusb.so index c773a3b..cb202de 100755 Binary files a/src/main/resources/linux-amd64/libhidapi-libusb.so and b/src/main/resources/linux-amd64/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-amd64/libhidapi.so b/src/main/resources/linux-amd64/libhidapi.so index ef1a11f..19da5ba 100755 Binary files a/src/main/resources/linux-amd64/libhidapi.so and b/src/main/resources/linux-amd64/libhidapi.so differ diff --git a/src/main/resources/linux-arm/libhidapi-libusb.so b/src/main/resources/linux-arm/libhidapi-libusb.so index bf2b6ae..784dea8 100755 Binary files a/src/main/resources/linux-arm/libhidapi-libusb.so and b/src/main/resources/linux-arm/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-arm/libhidapi.so b/src/main/resources/linux-arm/libhidapi.so index 8aad408..b5c2780 100755 Binary files a/src/main/resources/linux-arm/libhidapi.so and b/src/main/resources/linux-arm/libhidapi.so differ diff --git a/src/main/resources/linux-armel/libhidapi-libusb.so b/src/main/resources/linux-armel/libhidapi-libusb.so index 4e5117a..de0468d 100755 Binary files a/src/main/resources/linux-armel/libhidapi-libusb.so and b/src/main/resources/linux-armel/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-armel/libhidapi.so b/src/main/resources/linux-armel/libhidapi.so index 8aad408..d029e26 100755 Binary files a/src/main/resources/linux-armel/libhidapi.so and b/src/main/resources/linux-armel/libhidapi.so differ diff --git a/src/main/resources/linux-riscv64/libhidapi-libusb.so b/src/main/resources/linux-riscv64/libhidapi-libusb.so new file mode 100755 index 0000000..6222aee Binary files /dev/null and b/src/main/resources/linux-riscv64/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-riscv64/libhidapi.so b/src/main/resources/linux-riscv64/libhidapi.so new file mode 100755 index 0000000..3055d7b Binary files /dev/null and b/src/main/resources/linux-riscv64/libhidapi.so differ diff --git a/src/main/resources/linux-x86-64/libhidapi-libusb.so b/src/main/resources/linux-x86-64/libhidapi-libusb.so index c773a3b..cb202de 100755 Binary files a/src/main/resources/linux-x86-64/libhidapi-libusb.so and b/src/main/resources/linux-x86-64/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-x86-64/libhidapi.so b/src/main/resources/linux-x86-64/libhidapi.so index ef1a11f..19da5ba 100755 Binary files a/src/main/resources/linux-x86-64/libhidapi.so and b/src/main/resources/linux-x86-64/libhidapi.so differ diff --git a/src/main/resources/linux-x86/libhidapi-libusb.so b/src/main/resources/linux-x86/libhidapi-libusb.so index 7de1e57..cb202de 100755 Binary files a/src/main/resources/linux-x86/libhidapi-libusb.so and b/src/main/resources/linux-x86/libhidapi-libusb.so differ diff --git a/src/main/resources/linux-x86/libhidapi.so b/src/main/resources/linux-x86/libhidapi.so index 513f993..19da5ba 100755 Binary files a/src/main/resources/linux-x86/libhidapi.so and b/src/main/resources/linux-x86/libhidapi.so differ diff --git a/src/main/resources/win32-aarch64/hidapi.dll b/src/main/resources/win32-aarch64/hidapi.dll new file mode 100755 index 0000000..2ef6f34 Binary files /dev/null and b/src/main/resources/win32-aarch64/hidapi.dll differ diff --git a/src/main/resources/win32-x86-64/hidapi.dll b/src/main/resources/win32-x86-64/hidapi.dll index 070b39c..b842f88 100755 Binary files a/src/main/resources/win32-x86-64/hidapi.dll and b/src/main/resources/win32-x86-64/hidapi.dll differ diff --git a/src/main/resources/win32-x86/hidapi.dll b/src/main/resources/win32-x86/hidapi.dll index e9a055b..1731e89 100755 Binary files a/src/main/resources/win32-x86/hidapi.dll and b/src/main/resources/win32-x86/hidapi.dll differ diff --git a/src/test/java/org/hid4java/examples/BaseExample.java b/src/test/java/org/hid4java/examples/BaseExample.java index d9b7a9c..6a28505 100644 --- a/src/test/java/org/hid4java/examples/BaseExample.java +++ b/src/test/java/org/hid4java/examples/BaseExample.java @@ -55,6 +55,14 @@ public void printPlatform() { } + public static void printAsHex(byte[] dataReceived) { + System.out.printf("< [%02x]:", dataReceived.length); + for (byte b : dataReceived) { + System.out.printf(" %02x", b); + } + System.out.println(ANSI_RESET); + } + public void waitAndShutdown(HidServices hidServices) { System.out.printf(ANSI_YELLOW + "Waiting 30s to demonstrate attach/detach handling. Watch for slow response after write if configured.%n" + ANSI_RESET); @@ -74,7 +82,7 @@ public void waitAndShutdown(HidServices hidServices) { public static void sleepNoInterruption() { boolean interrupted = false; try { - long remainingNanos = TimeUnit.SECONDS.toNanos(5); + long remainingNanos = TimeUnit.SECONDS.toNanos(30); long end = System.nanoTime() + remainingNanos; while (true) { try { @@ -119,11 +127,8 @@ public void hidDataReceived(HidServicesEvent event) { System.out.printf(ANSI_PURPLE + "Data received:%n"); byte[] dataReceived = event.getDataReceived(); - System.out.printf("< [%02x]:", dataReceived.length); - for (byte b : dataReceived) { - System.out.printf(" %02x", b); - } - System.out.println(ANSI_RESET); + + printAsHex(dataReceived); } diff --git a/src/test/java/org/hid4java/examples/Fido2AuthenticationExample.java b/src/test/java/org/hid4java/examples/Fido2AuthenticationExample.java index 45a6d0d..9805436 100644 --- a/src/test/java/org/hid4java/examples/Fido2AuthenticationExample.java +++ b/src/test/java/org/hid4java/examples/Fido2AuthenticationExample.java @@ -31,103 +31,124 @@ import java.security.SecureRandom; -/* +/** * Demonstrate the USB HID interface using a FIDO2 USB device + *
+ * If you have a FIDO2 U2F authentication device (e.g. a HyperFIDO, Yubikey, Solokey, Trezor or Ledger) + * you may wish to explore its capabilities using this example. Simply plug it in and run the example to + * see the initial handshake to select a channel and basic device information. + *
+ * You can see some of hid4java features in use such as: + *

    + *
      + *
    • device enumeration and selection
    • + *
    • automatic data read events (new for 0.8.0)
    • + *
    • low level HID traffic to System.out to assist early stage debugging
    • + *
    • manual start to enable attach/detach events
    • + *
    + *
    + * Use the following command to try it out: + *
    + * + * mvn clean test exec:java -Dexec.classpathScope="test" -Dexec.mainClass="org.hid4java.examples.Fido2AuthenticationExample" + * * * @since 0.8.0 */ public class Fido2AuthenticationExample extends BaseExample { + // The FIDO2 scaffolding code will get included in later versions of this example + // Authentication data flags - private static byte CTAP_AUTHDATA_USER_PRESENT = 0x01; - private static byte CTAP_AUTHDATA_USER_VERIFIED = 0x04; - private static byte CTAP_AUTHDATA_ATT_CRED = 0x40; - private static byte CTAP_AUTHDATA_EXT_DATA = (byte) 0x80; - - // CTAPHID command opcodes - private static byte CTAP_CMD_PING = 0x01; - private static byte CTAP_CMD_MSG = 0x03; - private static byte CTAP_CMD_LOCK = 0x04; - private static byte CTAP_CMD_INIT = 0x06; - private static byte CTAP_CMD_WINK = 0x08; - private static byte CTAP_CMD_CBOR = 0x10; - private static byte CTAP_CMD_CANCEL = 0x11; - private static byte CTAP_KEEPALIVE = 0x3b; - private static byte CTAP_FRAME_INIT = (byte) 0x80; - - // CTAPHID CBOR command opcodes - private static byte CTAP_CBOR_MAKECRED = 0x01; - private static byte CTAP_CBOR_ASSERT = 0x02; - private static byte CTAP_CBOR_GETINFO = 0x04; - private static byte CTAP_CBOR_CLIENT_PIN = 0x06; - private static byte CTAP_CBOR_RESET = 0x07; - private static byte CTAP_CBOR_NEXT_ASSERT = 0x08; - private static byte CTAP_CBOR_BIO_ENROLL_PRE = 0x40; - private static byte CTAP_CBOR_CRED_MGMT_PRE = 0x41; - - // U2F command opcodes - private static byte U2F_CMD_REGISTER = 0x01; - private static byte U2F_CMD_AUTH = 0x02; - - // U2F command flags - private static byte U2F_AUTH_SIGN = 0x03; - private static byte U2F_AUTH_CHECK = 0x07; - - // ISO7816-4 status words - private static int SW_CONDITIONS_NOT_SATISFIED = 0x6985; - private static int SW_WRONG_DATA = 0x6a80; - private static int SW_NO_ERROR = 0x9000; - - // HID Broadcast channel ID - private static int CTAP_CID_BROADCAST = 0xffffffff; - - private static int CTAP_INIT_HEADER_LEN = 7; - private static int CTAP_CONT_HEADER_LEN = 5; - - // Maximum length of a CTAP HID report in bytes - private static int CTAP_MAX_REPORT_LEN = 64; - - // Minimum length of a CTAP HID report in bytes - private static int CTAP_MIN_REPORT_LEN = CTAP_INIT_HEADER_LEN + 1; - - // Maximum message size in bytes - private static int FIDO_MAXMSG = 2048; - - // CTAP capability bits (set means supported) - private static byte FIDO_CAP_WINK = 0x01; - private static byte FIDO_CAP_CBOR = 0x04; - private static byte FIDO_CAP_NMSG = 0x08; - - // Supported COSE algorithms - private static int COSE_ES256 = -7; - private static int COSE_EDDSA = -8; - private static int COSE_ECDH_ES256 = -25; - private static int COSE_RS256 = -257; - - // Supported COSE types - private static int COSE_KTY_OKP = 1; - private static int COSE_KTY_EC2 = 2; - private static int COSE_KTY_RSA = 3; - - // Supported curves - private static int COSE_P256 = 1; - private static int COSE_ED25519 = 6; - - // Supported extensions - private static byte FIDO_EXT_HMAC_SECRET = 0x01; - private static byte FIDO_EXT_CRED_PROTECT = 0x02; - - // Supported credential protection policies - private static int FIDO_CRED_PROT_UV_OPTIONAL = 0x01; - private static int FIDO_CRED_PROT_UV_OPTIONAL_WITH_ID = 0x02; - private static int FIDO_CRED_PROT_UV_REQUIRED = 0x03; + // private static byte CTAP_AUTHDATA_USER_PRESENT = 0x01; + // private static byte CTAP_AUTHDATA_USER_VERIFIED = 0x04; + // private static byte CTAP_AUTHDATA_ATT_CRED = 0x40; + // private static byte CTAP_AUTHDATA_EXT_DATA = (byte) 0x80; + // + // // CTAPHID command opcodes + // private static byte CTAP_CMD_PING = 0x01; + // private static byte CTAP_CMD_MSG = 0x03; + // private static byte CTAP_CMD_LOCK = 0x04; + private static final byte CTAP_CMD_INIT = 0x06; + // private static byte CTAP_CMD_WINK = 0x08; + // private static byte CTAP_CMD_CBOR = 0x10; + // private static byte CTAP_CMD_CANCEL = 0x11; + // private static byte CTAP_KEEPALIVE = 0x3b; + // private static byte CTAP_FRAME_INIT = (byte) 0x80; + // + // // CTAPHID CBOR command opcodes + // private static byte CTAP_CBOR_MAKECRED = 0x01; + // private static byte CTAP_CBOR_ASSERT = 0x02; + // private static byte CTAP_CBOR_GETINFO = 0x04; + // private static byte CTAP_CBOR_CLIENT_PIN = 0x06; + // private static byte CTAP_CBOR_RESET = 0x07; + // private static byte CTAP_CBOR_NEXT_ASSERT = 0x08; + // private static byte CTAP_CBOR_BIO_ENROLL_PRE = 0x40; + // private static byte CTAP_CBOR_CRED_MGMT_PRE = 0x41; + // + // // U2F command opcodes + // private static byte U2F_CMD_REGISTER = 0x01; + // private static byte U2F_CMD_AUTH = 0x02; + // + // // U2F command flags + // private static byte U2F_AUTH_SIGN = 0x03; + // private static byte U2F_AUTH_CHECK = 0x07; + // + // // ISO7816-4 status words + // private static int SW_CONDITIONS_NOT_SATISFIED = 0x6985; + // private static int SW_WRONG_DATA = 0x6a80; + // private static int SW_NO_ERROR = 0x9000; + // + // // HID Broadcast channel ID + // private static int CTAP_CID_BROADCAST = 0xffffffff; + // + // private static int CTAP_INIT_HEADER_LEN = 7; + // private static int CTAP_CONT_HEADER_LEN = 5; + // + // // Maximum length of a CTAP HID report in bytes + private static final int CTAP_MAX_REPORT_LEN = 64; + // + // // Minimum length of a CTAP HID report in bytes + // private static int CTAP_MIN_REPORT_LEN = CTAP_INIT_HEADER_LEN + 1; + // + // // Maximum message size in bytes + // private static int FIDO_MAXMSG = 2048; + // + // // CTAP capability bits (set means supported) + // private static byte FIDO_CAP_WINK = 0x01; + // private static byte FIDO_CAP_CBOR = 0x04; + // private static byte FIDO_CAP_NMSG = 0x08; + // + // // Supported COSE algorithms + // private static int COSE_ES256 = -7; + // private static int COSE_EDDSA = -8; + // private static int COSE_ECDH_ES256 = -25; + // private static int COSE_RS256 = -257; + // + // // Supported COSE types + // private static int COSE_KTY_OKP = 1; + // private static int COSE_KTY_EC2 = 2; + // private static int COSE_KTY_RSA = 3; + // + // // Supported curves + // private static int COSE_P256 = 1; + // private static int COSE_ED25519 = 6; + // + // // Supported extensions + // private static byte FIDO_EXT_HMAC_SECRET = 0x01; + // private static byte FIDO_EXT_CRED_PROTECT = 0x02; + // + // // Supported credential protection policies + // private static int FIDO_CRED_PROT_UV_OPTIONAL = 0x01; + // private static int FIDO_CRED_PROT_UV_OPTIONAL_WITH_ID = 0x02; + // private static int FIDO_CRED_PROT_UV_REQUIRED = 0x03; // Secure random source for nonce - private static SecureRandom secureRandom = new SecureRandom(); + private static final SecureRandom secureRandom = new SecureRandom(); // Instance variables - private byte[] nonce = new byte[8]; - private byte[] fidoChannel = new byte[4]; + private final byte[] nonce = new byte[8]; + //private final byte[] fidoChannel = new byte[4]; public static void main(String[] args) throws HidException { @@ -162,11 +183,11 @@ private void executeExample() throws HidException { // Manually start HID services hidServices.start(); - // Enumerate devices looking for = 0xf1d0 usage page + // Enumerate devices looking for usage page = 0xf1d0 (FIDO...) HidDevice fidoDevice = null; for (HidDevice hidDevice : hidServices.getAttachedHidDevices()) { if (hidDevice.getUsage() == 0x01 && hidDevice.getUsagePage() == 0xfffff1d0) { - System.out.println(ANSI_GREEN + "Using FIDO device: " + hidDevice.getPath() + ANSI_RESET); + System.out.println(ANSI_GREEN + "Using FIDO2 device: " + hidDevice.getPath() + ANSI_RESET); fidoDevice = hidDevice; break; } @@ -176,10 +197,28 @@ private void executeExample() throws HidException { // Shut down and rely on auto-shutdown hook to clear HidApi resources System.out.println(ANSI_YELLOW + "No FIDO2 devices attached." + ANSI_RESET); } else { - if (handleInitialise(fidoDevice)) { - // TODO Expand example to include a registration request - //handleRegister(fidoDevice); + + // Open the device + if (fidoDevice.isClosed()) { + if (!fidoDevice.open()) { + throw new IllegalStateException("Unable to open device"); + } } + + // Perform a USB ReportDescriptor operation to determine general device capabilities + // This requires complex decoding defined in the referenced documents + // Reports can be up to 4096 bytes for complex devices so 64 is quite low + byte[] reportDescriptor = new byte[64]; + if (fidoDevice.getReportDescriptor(reportDescriptor) > 0) { + System.out.println(ANSI_GREEN + "FIDO2 device report descriptor (first 64 bytes): " + fidoDevice.getPath() + ANSI_RESET); + printAsHex(reportDescriptor); + } + + // Perform a FIDO Initialise operation + handleInitialise(fidoDevice); + + // TODO Consider further operations such as Registration to illustrate state machine + } waitAndShutdown(hidServices); @@ -197,18 +236,12 @@ private void generateNonce() { /** * Initialise the FIDO2 device and set a communications channel + * * @param hidDevice The device to use * @return True if the device is now initialised for use */ private boolean handleInitialise(HidDevice hidDevice) { - // Ensure device is open - if (hidDevice.isClosed()) { - if (!hidDevice.open()) { - throw new IllegalStateException("Unable to open device"); - } - } - generateNonce(); // Initialise @@ -247,11 +280,11 @@ public void hidDataReceived(HidServicesEvent event) { System.out.printf("Byte count (0x00 0x11): %02x %02x%n", initialiseResponse[5], initialiseResponse[6]); System.out.printf("Nonce (%02x %02x %02x %02x %02x %02x %02x %02x): %02x %02x %02x %02x %02x %02x %02x %02x%n", nonce[0], nonce[1], nonce[2], nonce[3], nonce[4], nonce[5], nonce[6], nonce[7], - initialiseResponse[7], initialiseResponse[8],initialiseResponse[9], initialiseResponse[10], - initialiseResponse[11], initialiseResponse[12],initialiseResponse[13], initialiseResponse[14] + initialiseResponse[7], initialiseResponse[8], initialiseResponse[9], initialiseResponse[10], + initialiseResponse[11], initialiseResponse[12], initialiseResponse[13], initialiseResponse[14] ); System.out.printf("New channel id: %02x %02x %02x %02x%n", - initialiseResponse[15], initialiseResponse[16],initialiseResponse[17], initialiseResponse[18] + initialiseResponse[15], initialiseResponse[16], initialiseResponse[17], initialiseResponse[18] ); System.out.printf("Protocol (0x02): %02x%n", initialiseResponse[19]); System.out.printf("Major: %02x%n", initialiseResponse[20]); @@ -259,11 +292,11 @@ public void hidDataReceived(HidServicesEvent event) { System.out.printf("Build: %02x%n", initialiseResponse[22]); System.out.printf("Capabilities: %02x%n", initialiseResponse[23]); - // Copy the channel for ongoing communications - fidoChannel[0] = initialiseResponse[15]; - fidoChannel[1] = initialiseResponse[16]; - fidoChannel[2] = initialiseResponse[17]; - fidoChannel[3] = initialiseResponse[18]; + // TODO Copy the channel for ongoing communications +// fidoChannel[0] = initialiseResponse[15]; +// fidoChannel[1] = initialiseResponse[16]; +// fidoChannel[2] = initialiseResponse[17]; +// fidoChannel[3] = initialiseResponse[18]; } }