1515// a commercial license, send an email to license@arduino.cc.
1616//
1717
18- package main
18+ package sync
1919
2020import (
21+ "github.com/arduino/go-properties-orderedmap"
2122 discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2223 "go.bug.st/serial/enumerator"
2324)
2425
25- // ProcessUpdates sends 'add' and 'remove' events by comparing two ports enumeration
26+ // processUpdates sends 'add' and 'remove' events by comparing two ports enumeration
2627// made at different times:
2728// - ports present in the new list but not in the old list are reported as 'added'
2829// - ports present in the old list but not in the new list are reported as 'removed'
29- func ProcessUpdates (old , new []* enumerator.PortDetails , eventCB discovery.EventCallback ) {
30+ func processUpdates (old , new []* enumerator.PortDetails , eventCB discovery.EventCallback ) {
3031 for _ , oldPort := range old {
31- if ! PortListHas (new , oldPort ) {
32+ if ! portListHas (new , oldPort ) {
3233 eventCB ("remove" , & discovery.Port {
3334 Address : oldPort .Name ,
3435 Protocol : "serial" ,
@@ -37,15 +38,15 @@ func ProcessUpdates(old, new []*enumerator.PortDetails, eventCB discovery.EventC
3738 }
3839
3940 for _ , newPort := range new {
40- if ! PortListHas (old , newPort ) {
41+ if ! portListHas (old , newPort ) {
4142 eventCB ("add" , toDiscoveryPort (newPort ))
4243 }
4344 }
4445}
4546
46- // PortListHas checks if port is contained in list. The port metadata are
47+ // portListHas checks if port is contained in list. The port metadata are
4748// compared in particular the port address, and vid/pid if the port is a usb port.
48- func PortListHas (list []* enumerator.PortDetails , port * enumerator.PortDetails ) bool {
49+ func portListHas (list []* enumerator.PortDetails , port * enumerator.PortDetails ) bool {
4950 for _ , p := range list {
5051 if port .Name == p .Name && port .IsUSB == p .IsUSB {
5152 if p .IsUSB &&
@@ -61,3 +62,22 @@ func PortListHas(list []*enumerator.PortDetails, port *enumerator.PortDetails) b
6162 }
6263 return false
6364}
65+
66+ func toDiscoveryPort (port * enumerator.PortDetails ) * discovery.Port {
67+ protocolLabel := "Serial Port"
68+ props := properties .NewMap ()
69+ if port .IsUSB {
70+ protocolLabel += " (USB)"
71+ props .Set ("vid" , "0x" + port .VID )
72+ props .Set ("pid" , "0x" + port .PID )
73+ props .Set ("serialNumber" , port .SerialNumber )
74+ }
75+ res := & discovery.Port {
76+ Address : port .Name ,
77+ AddressLabel : port .Name ,
78+ Protocol : "serial" ,
79+ ProtocolLabel : protocolLabel ,
80+ Properties : props ,
81+ }
82+ return res
83+ }
0 commit comments