| 1 |
// This file is part of arduino-serial-utils |
| 2 |
// |
| 3 |
// SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
package serialutils |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"runtime" |
| 11 |
"strings" |
| 12 |
"time" |
| 13 |
|
| 14 |
"go.bug.st/serial" |
| 15 |
) |
| 16 |
|
| 17 |
// Touch1200bps open and close the serial port at 1200 bps. This is used |
| 18 |
// on many Arduino (and compatible) boards as a signal to put the MCU |
| 19 |
// in bootloader mode. |
| 20 |
func Touch1200bps(port string) error { |
| 21 |
p, err := serial.Open(port, &serial.Mode{BaudRate: 1200}) |
| 22 |
if err != nil { |
| 23 |
return fmt.Errorf("opening port at 1200bps: %w", err) |
| 24 |
} |
| 25 |
|
| 26 |
if runtime.GOOS != "windows" { |
| 27 |
// This is not required on Windows |
| 28 |
// TODO: Investigate if it can be removed for other OS too |
| 29 |
|
| 30 |
// Set DTR to false |
| 31 |
if err = p.SetDTR(false); err != nil { |
| 32 |
_ = p.Close() |
| 33 |
return fmt.Errorf("setting DTR to OFF: %w", err) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// Close serial port |
| 38 |
_ = p.Close() |
| 39 |
|
| 40 |
// Scanning for available ports seems to open the port or |
| 41 |
// otherwise assert DTR, which would cancel the WDT reset if |
| 42 |
// it happens within 250 ms. So we wait until the reset should |
| 43 |
// have already occurred before going on. |
| 44 |
time.Sleep(500 * time.Millisecond) |
| 45 |
|
| 46 |
return nil |
| 47 |
} |
| 48 |
|
| 49 |
// ResetProgressCallbacks is a struct that defines a bunch of function callback |
| 50 |
// to observe the Reset function progress. |
| 51 |
type ResetProgressCallbacks struct { |
| 52 |
// TouchingPort is called to signal the 1200-bps touch of the reported port |
| 53 |
TouchingPort func(port string) |
| 54 |
// WaitingForNewSerial is called to signal that we are waiting for a new port |
| 55 |
WaitingForNewSerial func() |
| 56 |
// BootloaderPortFound is called to signal that the wait is completed and to |
| 57 |
// report the port found, or the empty string if no ports have been found and |
| 58 |
// the wait has timed-out. |
| 59 |
BootloaderPortFound func(port string) |
| 60 |
// Debug reports messages useful for debugging purposes. In normal conditions |
| 61 |
// these messages should not be displayed to the user. |
| 62 |
Debug func(msg string) |
| 63 |
} |
| 64 |
|
| 65 |
// Reset will reset a board using the 1200 bps port-touch and waits for the bootloader port that is returned. |
| 66 |
// Both reset and wait are optional: |
| 67 |
// - if `portToTouch` is the empty string "" the reset will be skipped |
| 68 |
// - if `wait` is false waiting will be skipped |
| 69 |
// If `wait` is true, this function will wait for a new port to appear after the reset and returns it. If |
| 70 |
// a new port can not be detected or if the `wait` parameter is `false`, then the empty string is returned. |
| 71 |
// |
| 72 |
// If `dryRun` is set to `true` this function will only emulate the port reset without actually performing |
| 73 |
// it, this is useful to mockup for unit-testing and CI. In dryRun mode if the `portToTouch` ends with |
| 74 |
// `"999"` and `wait` is `true`, the function will return a new "mocked" bootloader port as `portToTouch+"0"`. |
| 75 |
// |
| 76 |
// `portMapper` is a method called to obtain the current serial port list. If `portMapper` is `nil` the |
| 77 |
// default internal port mapper will be used. |
| 78 |
// |
| 79 |
// `cb` is a struct defining a bunch of callback functions called during the reset operation to provide |
| 80 |
// progress feedback to the caller. |
| 81 |
func Reset(portToTouch string, wait bool, dryRun bool, portsMapper PortsMapper, cb *ResetProgressCallbacks) (string, error) { |
| 82 |
if portsMapper == nil { |
| 83 |
portsMapper = DefaultPortMapper // non dry-run default |
| 84 |
} |
| 85 |
if dryRun { |
| 86 |
emulatedPort := portToTouch |
| 87 |
portsMapper = func() (map[string]bool, error) { |
| 88 |
res := map[string]bool{} |
| 89 |
if emulatedPort != "" { |
| 90 |
res[emulatedPort] = true |
| 91 |
} |
| 92 |
if strings.HasSuffix(emulatedPort, "999") { |
| 93 |
emulatedPort += "0" |
| 94 |
} else if emulatedPort == "" { |
| 95 |
emulatedPort = "newport" |
| 96 |
} |
| 97 |
return res, nil |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
last, err := portsMapper() |
| 102 |
if cb != nil && cb.Debug != nil { |
| 103 |
cb.Debug(fmt.Sprintf("LAST: %v", last)) |
| 104 |
} |
| 105 |
if err != nil { |
| 106 |
return "", err |
| 107 |
} |
| 108 |
|
| 109 |
if portToTouch != "" && last[portToTouch] { |
| 110 |
if cb != nil && cb.Debug != nil { |
| 111 |
cb.Debug(fmt.Sprintf("TOUCH: %v", portToTouch)) |
| 112 |
} |
| 113 |
if cb != nil && cb.TouchingPort != nil { |
| 114 |
cb.TouchingPort(portToTouch) |
| 115 |
} |
| 116 |
if dryRun { |
| 117 |
// do nothing! |
| 118 |
} else { |
| 119 |
if err := Touch1200bps(portToTouch); err != nil && !wait { |
| 120 |
return "", fmt.Errorf("1200-bps touch: %w", err) |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if !wait { |
| 126 |
return "", nil |
| 127 |
} |
| 128 |
if cb != nil && cb.WaitingForNewSerial != nil { |
| 129 |
cb.WaitingForNewSerial() |
| 130 |
} |
| 131 |
|
| 132 |
deadline := time.Now().Add(10 * time.Second) |
| 133 |
if dryRun { |
| 134 |
// use a much lower timeout in dryRun |
| 135 |
deadline = time.Now().Add(100 * time.Millisecond) |
| 136 |
} |
| 137 |
for time.Now().Before(deadline) { |
| 138 |
now, err := portsMapper() |
| 139 |
if err != nil { |
| 140 |
return "", err |
| 141 |
} |
| 142 |
if cb != nil && cb.Debug != nil { |
| 143 |
cb.Debug(fmt.Sprintf("WAIT: %v", now)) |
| 144 |
} |
| 145 |
hasNewPorts := false |
| 146 |
for p := range now { |
| 147 |
if !last[p] { |
| 148 |
hasNewPorts = true |
| 149 |
break |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if hasNewPorts { |
| 154 |
if cb != nil && cb.Debug != nil { |
| 155 |
cb.Debug("New ports found!") |
| 156 |
} |
| 157 |
|
| 158 |
// on OS X, if the port is opened too quickly after it is detected, |
| 159 |
// a "Resource busy" error occurs, add a delay to workaround. |
| 160 |
// This apply to other platforms as well. |
| 161 |
time.Sleep(time.Second) |
| 162 |
|
| 163 |
// Some boards have a glitch in the bootloader: some user experienced |
| 164 |
// the USB serial port appearing and disappearing rapidly before |
| 165 |
// settling. |
| 166 |
// This check ensure that the port is stable after one second. |
| 167 |
check, err := portsMapper() |
| 168 |
if err != nil { |
| 169 |
return "", err |
| 170 |
} |
| 171 |
if cb != nil && cb.Debug != nil { |
| 172 |
cb.Debug(fmt.Sprintf("CHECK: %v", check)) |
| 173 |
} |
| 174 |
for p := range check { |
| 175 |
if !last[p] { |
| 176 |
if cb != nil && cb.BootloaderPortFound != nil { |
| 177 |
cb.BootloaderPortFound(p) |
| 178 |
} |
| 179 |
return p, nil // Found it! |
| 180 |
} |
| 181 |
} |
| 182 |
if cb != nil && cb.Debug != nil { |
| 183 |
cb.Debug("Port check failed... still waiting") |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
last = now |
| 188 |
time.Sleep(250 * time.Millisecond) |
| 189 |
} |
| 190 |
|
| 191 |
if cb != nil && cb.BootloaderPortFound != nil { |
| 192 |
cb.BootloaderPortFound("") |
| 193 |
} |
| 194 |
return "", nil |
| 195 |
} |