Skip to content

Feature/filter ports #79

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 55 additions & 0 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,66 @@
package sync

import (
"fmt"
"regexp"
"strings"
"os"
"path/filepath"
"github.com/arduino/go-properties-orderedmap"
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
"go.bug.st/serial/enumerator"
)

var loaded = false
var filter = ""

func load() string {
if loaded {
return filter
}

loaded = true
thepath, err := filepath.Abs(filepath.Dir(os.Args[0]))

if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return filter
}

data, err := os.ReadFile(filepath.Join(thepath, "skip.txt"))

if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return filter
}

filter = strings.Trim(string(data), " \t\r\n")

return filter
}

func filterValid(ports []*enumerator.PortDetails) (ret []*enumerator.PortDetails) {
filter := load()

if len(filter) <= 0 {
ret = ports
return
}

for _, port := range ports {
if isValid(port, filter) {
ret = append(ret, port)
}
}
return
}

func isValid(port *enumerator.PortDetails, filter string) bool {
match, _ := regexp.MatchString(filter, port.Name)

return !match;
}

// nolint
// processUpdates sends 'add' and 'remove' events by comparing two ports enumeration
// made at different times:
Expand Down
6 changes: 6 additions & 0 deletions sync/sync_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (ch
errorCB(err.Error())
return
}

current = filterValid(current)

for _, port := range current {
eventCB("add", toDiscoveryPort(port))
}
Expand Down Expand Up @@ -98,6 +101,9 @@ func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (ch
enumeratorErr = err
break
}

updates = filterValid(updates)

processUpdates(current, updates, eventCB)
current = updates
}
Expand Down
5 changes: 5 additions & 0 deletions sync/sync_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (ch
return nil, err
}

current = filterValid(current)

// Start sync reader from udev
syncReader, err := uevent.NewReader()
if err != nil {
Expand Down Expand Up @@ -78,6 +80,9 @@ func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (ch
if err != nil {
continue
}

portList = filterValid(portList)

for _, port := range portList {
if port.IsUSB && port.Name == changedPort {
eventCB("add", toDiscoveryPort(port))
Expand Down
6 changes: 6 additions & 0 deletions sync/sync_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (ch
errorCB(fmt.Sprintf("Error enumerating serial ports: %s", err))
return
}

current = filterValid(current)

for _, port := range current {
eventCB("add", toDiscoveryPort(port))
}
Expand All @@ -140,6 +143,9 @@ func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (ch
errorCB(fmt.Sprintf("Error enumerating serial ports: %s", err))
return
}

updates = filterValid(updates)

processUpdates(current, updates, eventCB)
current = updates
}
Expand Down
Loading