From 476cbe17853964c27c1801e336eaf06459e30b3b Mon Sep 17 00:00:00 2001 From: Jason Koh Date: Mon, 4 May 2020 15:11:29 -0700 Subject: [PATCH 1/5] Add a new MANUAL_RESET feature to Kaleidoscope builder Some devices, like those coming from QMK don't have an automated reset feature Signed-off-by: Jesse Vincent --- bin/kaleidoscope-builder | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/bin/kaleidoscope-builder b/bin/kaleidoscope-builder index 88c4e3d5..30482744 100755 --- a/bin/kaleidoscope-builder +++ b/bin/kaleidoscope-builder @@ -172,10 +172,17 @@ flash () { # This is defined in the (optional) user config. # shellcheck disable=SC2154 ${preFlash_HOOKS} - - reset_device - sleep 2 - find_bootloader_ports + + # If we're -not- doing a manual reset, then try to do it automatically + if [ -z "${MANUAL_RESET}" ]; then + reset_device + sleep 2 + find_bootloader_ports + # Otherwise, poll for a bootloader port. + else + wait_for_bootloader_port + fi + fi check_bootloader_port_and_flash @@ -185,6 +192,25 @@ flash () { ${postFlash_HOOKS} } +wait_for_bootloader_port() { + declare -i tries + tries=15 + + while [ "$tries" -gt 0 ] && [ -z "${DEVICE_PORT_BOOTLOADER}" ]; do + sleep 1 + printf "." + find_bootloader_ports + # the variable annotations do appear to be necessary + # shellcheck disable=SC2004 + tries=$(($tries-1)) + done + + if [ "$tries" -gt 0 ]; then + echo "Found." + else + echo "Timed out." + fi +} check_bootloader_port () { if [ -z "${DEVICE_PORT_BOOTLOADER}" ]; then From 94790f8db99e01a5d5d1923b331aa0fe444ca776 Mon Sep 17 00:00:00 2001 From: Jason Koh Date: Mon, 4 May 2020 15:13:24 -0700 Subject: [PATCH 2/5] Make sure the macOS device prober only runs if you've supplied the required args Signed-off-by: Jesse Vincent --- bin/find-device-port-macos | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/find-device-port-macos b/bin/find-device-port-macos index ed799355..484d7355 100644 --- a/bin/find-device-port-macos +++ b/bin/find-device-port-macos @@ -10,6 +10,10 @@ use strict; my $vid = shift @ARGV; my $pid = shift @ARGV; +if (!defined $vid || !defined $pid) { + die "$0 has two required parameters, VID and PID"; +} + # ioreg might be more machine-readable than system_profiler, but I haven't been able to # get it to produce useful output my @output = qx(/usr/sbin/system_profiler SPUSBDataType 2> /dev/null); From b675ff29029ef16b3de9cbaf7e4a6911ab231605 Mon Sep 17 00:00:00 2001 From: Jason Koh Date: Mon, 4 May 2020 15:18:08 -0700 Subject: [PATCH 3/5] Make our hail-mary logic a little more flexible. (Jason's original implementation just truncated the $sn, no matter what. I modified it slightly.) Signed-off-by: Jesse Vincent --- bin/find-device-port-macos | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/find-device-port-macos b/bin/find-device-port-macos index 484d7355..b33a1e82 100644 --- a/bin/find-device-port-macos +++ b/bin/find-device-port-macos @@ -77,6 +77,8 @@ sub try_for_location_id { sub try_for_sn_prefix { my $sn = shift; + # If macOS has appended 'E', take it off to maximise our chances of a match. + $sn =~ s/E$//; # If none of the above tests succeeds, just list the directory and see if there are any # files that have the device shortname that we expect: From 430b159913ad1287c52a2d2d72c1cd5800a599a2 Mon Sep 17 00:00:00 2001 From: Jason Koh Date: Mon, 4 May 2020 15:24:27 -0700 Subject: [PATCH 4/5] Tweak location-based detection logic Signed-off-by: Jesse Vincent --- bin/find-device-port-macos | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bin/find-device-port-macos b/bin/find-device-port-macos index b33a1e82..11b7e366 100644 --- a/bin/find-device-port-macos +++ b/bin/find-device-port-macos @@ -69,10 +69,13 @@ sub try_for_raw_serialnum { sub try_for_location_id { my $location_id = shift; - # Here, also, the final character is always a "1", so if macOS ever stops doing that, this - # will need an update, as well. - my $loc = substr($location_id, 2, 3); - exit_with_port_if_exists("/dev/cu.usbmodem" . $loc . 1); + # macOS truncates the string of "0"s from the right of the location id. + # Here, also, the final character is an appended "1", so if macOS ever stops doing that, + # this will need an update, as well. + if ($location_id =~ /0x(\d+?)0*\b/) { + my $loc = $1; + exit_with_port_if_exists("/dev/cu.usbmodem" . $loc . "1"); + } } sub try_for_sn_prefix { From 43526c4d92528568c97c83f27fc24f4f19263a1a Mon Sep 17 00:00:00 2001 From: Jason Koh Date: Mon, 4 May 2020 15:40:48 -0700 Subject: [PATCH 5/5] Slightly updated probing logic based on Jason's code. Signed-off-by: Jesse Vincent --- bin/find-device-port-macos | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bin/find-device-port-macos b/bin/find-device-port-macos index 11b7e366..82778ff6 100644 --- a/bin/find-device-port-macos +++ b/bin/find-device-port-macos @@ -55,15 +55,24 @@ sub try_for_raw_serialnum { # High Sierra sometimes has a mismatch between the serial number and the device # filename. I'm not sure why, but system_profiler has a serial number ending in "E", # whereas the device filename ends in "1". In fact, when I change HID.getShortName() - # to return "kbio02", the final character is replaced with a "1", so we should do the - # same here. If the serial number doesn't end in a digit, however, we may want to append - # rather than replace the last character with a 1. + # to return "kbio02", the final character is replaced with a "1". + if ($serial_port_name =~ /\d$/) { - $serial_port_name =~ s/.$/1/; + chop $serial_port_name; + exit_with_port_if_exists($serial_port_name . "1"); } else { - $serial_port_name .= "1"; + # If the serial port name doesn't end with a digit, try -appending- rather than replacing + # the last character of the port name + exit_with_port_if_exists($serial_port_name . "1"); + + # and if that didn't work, try replacing the last character with a "1" anyway. + # Jason Koh reports that he saw this behavior as required on Catalina in May 2020. + + chop $serial_port_name; + exit_with_port_if_exists($serial_port_name . "1"); } - exit_with_port_if_exists($serial_port_name); + + } sub try_for_location_id {