Skip to content

Commit 6fcfe70

Browse files
lolodomowborn
authored andcommitted
Fix USB discovery on Windows in case there is no USB port (#4996)
Fix #4993 Signed-off-by: Laurent Garnier <lg.hc@free.fr>
1 parent ee94122 commit 6fcfe70

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

  • bundles/org.openhab.core.config.discovery.usbserial.windowsregistry/src/main/java/org/openhab/core/config/discovery/usbserial/windowsregistry/internal

bundles/org.openhab.core.config.discovery.usbserial.windowsregistry/src/main/java/org/openhab/core/config/discovery/usbserial/windowsregistry/internal/WindowsUsbSerialDiscovery.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import com.sun.jna.Platform;
4141
import com.sun.jna.platform.win32.Advapi32Util;
42+
import com.sun.jna.platform.win32.Win32Exception;
4243

4344
/**
4445
* This is a {@link UsbSerialDiscovery} implementation component for Windows.
@@ -141,7 +142,13 @@ public Set<UsbSerialDeviceInformation> scanAllUsbDevicesInformation() {
141142
}
142143

143144
Set<UsbSerialDeviceInformation> result = new HashSet<>();
144-
String[] deviceKeys = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, USB_REGISTRY_ROOT);
145+
String[] deviceKeys;
146+
try {
147+
deviceKeys = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, USB_REGISTRY_ROOT);
148+
} catch (Win32Exception e) {
149+
logger.debug("registryGetKeys failed for {}", USB_REGISTRY_ROOT, e);
150+
return result;
151+
}
145152

146153
for (String deviceKey : deviceKeys) {
147154
logger.trace("{}", deviceKey);
@@ -171,14 +178,26 @@ public Set<UsbSerialDeviceInformation> scanAllUsbDevicesInformation() {
171178
String serialNumber = ids.length > 2 ? ids[2] : null;
172179

173180
String devicePath = USB_REGISTRY_ROOT + BACKSLASH + deviceKey;
174-
String[] interfaceNames = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, devicePath);
181+
String[] interfaceNames;
182+
try {
183+
interfaceNames = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, devicePath);
184+
} catch (Win32Exception e) {
185+
logger.debug("registryGetKeys failed for {}", devicePath, e);
186+
continue;
187+
}
175188

176189
int interfaceId = 0;
177190
for (String interfaceName : interfaceNames) {
178191
logger.trace(" interfaceId:{}, interfaceName:{}", interfaceId, interfaceName);
179192

180193
String interfacePath = devicePath + BACKSLASH + interfaceName;
181-
TreeMap<String, Object> values = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE, interfacePath);
194+
TreeMap<String, Object> values;
195+
try {
196+
values = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE, interfacePath);
197+
} catch (Win32Exception e) {
198+
logger.debug("registryGetValues failed for {}", interfacePath, e);
199+
continue;
200+
}
182201

183202
if (logger.isTraceEnabled()) {
184203
for (Entry<String, Object> value : values.entrySet()) {
@@ -211,15 +230,27 @@ public Set<UsbSerialDeviceInformation> scanAllUsbDevicesInformation() {
211230
}
212231

213232
String serialPort = "";
214-
String[] interfaceSubKeys = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, interfacePath);
233+
String[] interfaceSubKeys;
234+
try {
235+
interfaceSubKeys = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, interfacePath);
236+
} catch (Win32Exception e) {
237+
logger.debug("registryGetKeys failed for {}", interfacePath, e);
238+
continue;
239+
}
215240

216241
for (String interfaceSubKey : interfaceSubKeys) {
217242
if (!KEY_DEVICE_PARAMETERS.equals(interfaceSubKey)) {
218243
continue;
219244
}
220245
String deviceParametersPath = interfacePath + BACKSLASH + interfaceSubKey;
221-
TreeMap<String, Object> deviceParameterValues = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE,
222-
deviceParametersPath);
246+
TreeMap<String, Object> deviceParameterValues;
247+
try {
248+
deviceParameterValues = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE,
249+
deviceParametersPath);
250+
} catch (Win32Exception e) {
251+
logger.debug("registryGetValues failed for {}", deviceParametersPath, e);
252+
continue;
253+
}
223254
Object serialPortValue = deviceParameterValues.get(KEY_SERIAL_PORT);
224255
if (serialPortValue instanceof String serialPortString) {
225256
serialPort = serialPortString;

0 commit comments

Comments
 (0)