From ac6c00b80d75ea5e2901e9f8f69b78d9de0cee9e Mon Sep 17 00:00:00 2001 From: Thomas du Boisrouvray Date: Wed, 23 Apr 2025 12:19:35 +0200 Subject: [PATCH] doc: script: Add wsl2 device support Add Powershell script to use USB devices into WSL2 https://github.com/SiliconLabsSoftware/z-wave-protocol-controller/pull/73 --- README.md | 26 +++++++++++++++++ scripts/wslusb.ps1 | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 scripts/wslusb.ps1 diff --git a/README.md b/README.md index 3c7bc473d..a9c4f8775 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,32 @@ ZPC>attribute_store_set_desired 110,1 (...) ``` +### WSL2 build + +How to use a z-wave controller with ZPC running in WSL2 ? + +You can use this [script](./scripts/wslusb.ps1). + +Start by installing the usbipd service as described at: https://learn.microsoft.com/en-us/windows/wsl/connect-usb + +```sh +# You can list devices using: + +(Powershell)$ ./wslusb.ps1 -List + +# Get the BUSID of the device you want to mount + +(Powershell)$ ./wslusb.ps1 -Attach + +# Check that the device is correctly mounted into WSL2 + +(WSL2)$ lsusb # you should see your device here + +# Detach the device with + +(Powershell)$ ./wslusb.ps1 -Detach +``` + ### More Refer to [./doc](doc) for more (using shell, MQTT, WebApp etc). diff --git a/scripts/wslusb.ps1 b/scripts/wslusb.ps1 new file mode 100644 index 000000000..2eb080b37 --- /dev/null +++ b/scripts/wslusb.ps1 @@ -0,0 +1,73 @@ +# SPDX-License-Identifier: ZLib +# This script can be used to attach and detach a device to your WSL2 distribution + +[CmdletBinding()] +param( + [Parameter(Mandatory= $false, HelpMessage = "Busid of the device you want to bind")] + [Alias("b")] + [string]$Busid, + + [Switch]$Attach, + [Switch]$Detach, + [Switch]$List +) + +# Unblock the script file +Unblock-File -Path $MyInvocation.MyCommand.Path + +Function List-devices { + usbipd list +} + +Function Attach-device { + param( + [string]$Busid + ) + Write-Host "Attaching device $Busid" + usbipd bind --busid $Busid --force + usbipd attach --wsl --busid $Busid --auto-attach +} + +Function Detach-device { + param( + [string]$Busid + ) + Write-Host "Detaching device $Busid" + usbipd detach --busid $Busid + usbipd unbind --busid $Busid +} + +if ($Attach -or $Detach -or $List) +{ + if($List) + { + List-devices + } + if ($Detach) + { + if ($Busid) + { + Detach-device -Busid $Busid + } + else + { + Write-Host "Busid not specified" + } + } + if ($Attach) + { + if ($Busid) + { + Attach-device -Busid $Busid + } + else + { + Write-Host "Busid not specified" + } + } +} +else +{ + Write-Host "No argument specified. Use -Attach, -Detach or -List" + Write-Host 'Ex: ./wslusb.ps1 -b "5-3" -Attach' +} \ No newline at end of file