Skip to content

Commit

Permalink
New functions DetectSerialPort() and GetSerialPorts()
Browse files Browse the repository at this point in the history
Previously found in internal/util in -apps, slightly changed to be a
little less verbose.
  • Loading branch information
mchack-work committed Jun 1, 2023
1 parent f6bb10f commit 215257b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ require (
require (
github.com/creack/goselect v0.1.2 // indirect
github.com/stretchr/testify v1.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/sys v0.8.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ go.bug.st/serial v1.5.0 h1:ThuUkHpOEmCVXxGEfpoExjQCS2WBVV4ZcUKVYInM9T4=
go.bug.st/serial v1.5.0/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
73 changes: 73 additions & 0 deletions ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (C) 2022 - Tillitis AB
// SPDX-License-Identifier: GPL-2.0-only

package tkeyclient

import (
"fmt"
"os"

"go.bug.st/serial/enumerator"
)

const (
tillitisUSBVID = "1207"
tillitisUSBPID = "8887"
// Custom errors
ErrNoDevice = constError("no TKey connected")
ErrManyDevices = constError("more than one TKey connected")
)

type SerialPort struct {
DevPath string
SerialNumber string
}

// DetectSerialPort tries to detect an inserted TKey and returns the
// device path if successful.
func DetectSerialPort(verbose bool) (string, error) {
ports, err := GetSerialPorts()
if err != nil {
return "", err
}
if len(ports) == 0 {
if verbose {
fmt.Fprintf(os.Stderr, "No TKey serial ports detected.\n")
}
return "", ErrNoDevice
}
if len(ports) > 1 {
if verbose {
fmt.Fprintf(os.Stderr, "Detected %d TKey serial ports:\n", len(ports))
for _, p := range ports {
fmt.Fprintf(os.Stderr, "%s with serial number %s\n", p.DevPath, p.SerialNumber)
}
}
return "", ErrManyDevices
}
if verbose {
fmt.Fprintf(os.Stderr, "Auto-detected serial port %s\n", ports[0].DevPath)
}

return ports[0].DevPath, nil
}

// GetSerialPorts enumerates any existing TKey serial ports identified
// on the system.
func GetSerialPorts() ([]SerialPort, error) {
var ports []SerialPort

portDetails, err := enumerator.GetDetailedPortsList()
if err != nil {
return nil, fmt.Errorf("GetDetailedPortsList: %w", err)
}
if len(portDetails) == 0 {
return ports, nil
}
for _, port := range portDetails {
if port.IsUSB && port.VID == tillitisUSBVID && port.PID == tillitisUSBPID {
ports = append(ports, SerialPort{port.Name, port.SerialNumber})
}
}
return ports, nil
}

0 comments on commit 215257b

Please sign in to comment.