Skip to content

bugfix: serial enumerator failures in some rare circumstances #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Renamed and slightly refactored port enumerators
  • Loading branch information
cmaglie committed Sep 12, 2023
commit ec6f5fe3a1fca9e18b7c3b8dea3df330cd63b0c3
4 changes: 2 additions & 2 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const timeoutConst = 2
// SavedNetworkPorts contains the ports which we know are already connected
var SavedNetworkPorts []OsSerialPort

// GetNetworkList returns a list of Network Ports
// enumerateNetworkPorts returns a list of Network Ports
// The research of network ports is articulated in two phases. First we add new ports coming from
// the bonjour module, then we prune the boards who don't respond to a ping
func GetNetworkList() ([]OsSerialPort, error) {
func enumerateNetworkPorts() ([]OsSerialPort, error) {
newPorts, err := getPorts()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func loop() {
}

// list serial ports
portList, _ := GetList(false)
portList, _ := enumerateSerialPorts()
log.Println("Your serial ports:")
if len(portList) == 0 {
log.Println("\tThere are no serial ports to list.")
Expand Down
128 changes: 51 additions & 77 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -72,10 +73,10 @@ type SpPortItem struct {
}

// SerialPorts contains the ports attached to the machine
var SerialPorts SpPortList
var serialPorts SpPortList

// NetworkPorts contains the ports on the network
var NetworkPorts SpPortList
// networkPorts contains the ports on the network
var networkPorts SpPortList

var sh = serialhub{
//write: make(chan *serport, chan []byte),
Expand Down Expand Up @@ -130,13 +131,13 @@ func spList(network bool) {
var ls []byte
var err error
if network {
NetworkPorts.Mu.Lock()
ls, err = json.MarshalIndent(&NetworkPorts, "", "\t")
NetworkPorts.Mu.Unlock()
networkPorts.Mu.Lock()
ls, err = json.MarshalIndent(&networkPorts, "", "\t")
networkPorts.Mu.Unlock()
} else {
SerialPorts.Mu.Lock()
ls, err = json.MarshalIndent(&SerialPorts, "", "\t")
SerialPorts.Mu.Unlock()
serialPorts.Mu.Lock()
ls, err = json.MarshalIndent(&serialPorts, "", "\t")
serialPorts.Mu.Unlock()
}
if err != nil {
//log.Println(err)
Expand All @@ -149,81 +150,65 @@ func spList(network bool) {

// discoverLoop periodically update the list of ports found
func discoverLoop() {
SerialPorts.Mu.Lock()
SerialPorts.Network = false
SerialPorts.Ports = make([]SpPortItem, 0)
SerialPorts.Mu.Unlock()
NetworkPorts.Mu.Lock()
NetworkPorts.Network = true
NetworkPorts.Ports = make([]SpPortItem, 0)
NetworkPorts.Mu.Unlock()
serialPorts.Mu.Lock()
serialPorts.Network = false
serialPorts.Ports = make([]SpPortItem, 0)
serialPorts.Mu.Unlock()
networkPorts.Mu.Lock()
networkPorts.Network = true
networkPorts.Ports = make([]SpPortItem, 0)
networkPorts.Mu.Unlock()

go func() {
for {
if !upload.Busy {
spListDual(false)
updateSerialPortList()
}
time.Sleep(2 * time.Second)
}
}()
go func() {
for {
spListDual(true)
updateNetworkPortList()
time.Sleep(2 * time.Second)
}
}()
}

func spListDual(network bool) {

// call our os specific implementation of getting the serial list
list, err := GetList(network)

//log.Println(list)
//log.Println(err)

func updateSerialPortList() {
ports, err := enumerateSerialPorts()
if err != nil {
// avoid reporting dummy data if an error occurred
// REPORT
fmt.Println("GET SERIAL LIST ERROR:", err)
return
}
list := spListDual(ports)
serialPorts.Mu.Lock()
serialPorts.Ports = list
serialPorts.Mu.Unlock()
}

// do a quick loop to see if any of our open ports
// did not end up in the list port list. this can
// happen on windows in a fallback scenario where an
// open port can't be identified because it is locked,
// so just solve that by manually inserting
// if network {
// for port := range sh.ports {

// isFound := false
// for _, item := range list {
// if strings.ToLower(port.portConf.Name) == strings.ToLower(item.Name) {
// isFound = true
// }
// }

// if !isFound {
// // artificially push to front of port list
// log.Println(fmt.Sprintf("Did not find an open port in the serial port list. We are going to artificially push it onto the list. port:%v", port.portConf.Name))
// var ossp OsSerialPort
// ossp.Name = port.portConf.Name
// ossp.FriendlyName = port.portConf.Name
// list = append([]OsSerialPort{ossp}, list...)
// }
// }
// }
func updateNetworkPortList() {
ports, err := enumerateNetworkPorts()
if err != nil {
// REPORT
fmt.Println("GET NETWORK LIST ERROR:", err)
return
}
list := spListDual(ports)
networkPorts.Mu.Lock()
networkPorts.Ports = list
networkPorts.Mu.Unlock()
}

func spListDual(list []OsSerialPort) []SpPortItem {
// we have a full clean list of ports now. iterate thru them
// to append the open/close state, baud rates, etc to make
// a super clean nice list to send back to browser
n := len(list)
spl := make([]SpPortItem, n)

ctr := 0
spl := []SpPortItem{}

for _, item := range list {

spl[ctr] = SpPortItem{
port := SpPortItem{
Name: item.Name,
SerialNumber: item.ISerial,
DeviceClass: item.DeviceClass,
Expand All @@ -238,26 +223,15 @@ func spListDual(network bool) {
}

// figure out if port is open
myport, isFound := findPortByName(item.Name)

if isFound {
// we found our port
spl[ctr].IsOpen = true
spl[ctr].Baud = myport.portConf.Baud
spl[ctr].BufferAlgorithm = myport.BufferType
if myport, isFound := findPortByName(item.Name); isFound {
// and update data with the open port parameters
port.IsOpen = true
port.Baud = myport.portConf.Baud
port.BufferAlgorithm = myport.BufferType
}
ctr++
}

if network {
NetworkPorts.Mu.Lock()
NetworkPorts.Ports = spl
NetworkPorts.Mu.Unlock()
} else {
SerialPorts.Mu.Lock()
SerialPorts.Ports = spl
SerialPorts.Mu.Unlock()
spl = append(spl, port)
}
return spl
}

func spErr(err string) {
Expand Down
10 changes: 2 additions & 8 deletions seriallist.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,8 @@ type OsSerialPort struct {
NetworkPort bool
}

// GetList will return the OS serial port
func GetList(network bool) ([]OsSerialPort, error) {

if network {
netportList, err := GetNetworkList()
return netportList, err
}

// enumerateSerialPorts will return the OS serial port
func enumerateSerialPorts() ([]OsSerialPort, error) {
// will timeout in 2 seconds
arrPorts := []OsSerialPort{}
ports, err := enumerator.GetDetailedPortsList()
Expand Down
8 changes: 4 additions & 4 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (p *serport) writerNoBuf() {
log.Println(msgstr)
h.broadcastSys <- []byte(msgstr)
p.portIo.Close()
spListDual(false)
updateSerialPortList()
spList(false)
}

Expand Down Expand Up @@ -321,7 +321,7 @@ func spHandlerOpen(portname string, baud int, buftype string) {
sh.register <- p
defer func() { sh.unregister <- p }()

spListDual(false)
updateSerialPortList()
spList(false)

// this is internally buffered thread to not send to serial port if blocked
Expand All @@ -333,7 +333,7 @@ func spHandlerOpen(portname string, baud int, buftype string) {

p.reader(buftype)

spListDual(false)
updateSerialPortList()
spList(false)
}

Expand All @@ -346,6 +346,6 @@ func spHandlerClose(p *serport) {
func spCloseReal(p *serport) {
p.bufferwatcher.Close()
p.portIo.Close()
spListDual(false)
updateSerialPortList()
spList(false)
}