| 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 |
|
| 11 |
"go.bug.st/serial" |
| 12 |
) |
| 13 |
|
| 14 |
// PortsMapper is a function that returns a map of available serial ports. |
| 15 |
type PortsMapper func() (map[string]bool, error) |
| 16 |
|
| 17 |
// DefaultPortMapper returns a PortsMapper that lists the available serial ports |
| 18 |
// using the go.bug.st/serial library enumerator. |
| 19 |
func DefaultPortMapper() (map[string]bool, error) { |
| 20 |
ports, err := serial.GetPortsList() |
| 21 |
if err != nil { |
| 22 |
return nil, fmt.Errorf("listing serial ports: %w", err) |
| 23 |
} |
| 24 |
res := map[string]bool{} |
| 25 |
for _, port := range ports { |
| 26 |
res[port] = true |
| 27 |
} |
| 28 |
return res, nil |
| 29 |
} |