Skip to content

Commit 5e37994

Browse files
authored
Implementation of port discovery through Pluggable discovery (#900)
* Upgraded arduino-cli and replaced serialutils dep * Refined ports filter * Silenced linter warning * Use pointers in ports lists * Made spList a method of SpPortList * Renamed SpPortList in SerialPortList * Removed unneded initialization * Inlined discovery loop * Made updateSerialPortList a method of SerialPortList * Made enumerationLock private * Made portsLock (ex Mu) field private * Inlined function call * Made findPortByName a method of serialhub and moved near it * Inlined call to 'write' and removed the function * Removed unused ManufacutrerId and DeviceClass, renamed some fields * Simplified enumerateSerialPorts function * Removed redundant loggin of serial port list * Removed seriallist.go and inlined function calls * Make the singleton 'tools' a pointer * Implemented serial-discovery * Remove no more used 'Busy' field * Fixed error message
1 parent ef266fa commit 5e37994

File tree

16 files changed

+1027
-1042
lines changed

16 files changed

+1027
-1042
lines changed

.licensed.yml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ allowed:
5555
# The following are based on: https://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses
5656
- gpl-1.0-or-later
5757
- gpl-1.0+ # Deprecated ID for `gpl-1.0-or-later`
58+
- gpl-2.0
5859
- gpl-2.0-or-later
5960
- gpl-2.0+ # Deprecated ID for `gpl-2.0-or-later`
6061
- gpl-3.0-only

.licenses/arduino-create-agent/go/github.com/arduino/arduino-cli/arduino/serialutils.dep.yml

-720
This file was deleted.

.licenses/arduino-create-agent/go/github.com/arduino/go-properties-orderedmap.dep.yml

+350
Large diffs are not rendered by default.

.licenses/arduino-create-agent/go/github.com/arduino/go-serial-utils.dep.yml

+458
Large diffs are not rendered by default.

.licenses/arduino-create-agent/go/github.com/arduino/arduino-cli/i18n.dep.yml renamed to .licenses/arduino-create-agent/go/github.com/arduino/pluggable-discovery-protocol-handler/v2.dep.yml

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
---
2-
name: github.com/arduino/arduino-cli/i18n
3-
version: v0.35.0
2+
name: github.com/arduino/pluggable-discovery-protocol-handler/v2
3+
version: v2.2.0
44
type: go
5-
summary:
6-
homepage: https://pkg.go.dev/github.com/arduino/arduino-cli/i18n
7-
license: gpl-3.0-only
5+
summary: Package discovery is a library for handling the Arduino Pluggable-Discovery
6+
protocol (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout)
7+
homepage: https://pkg.go.dev/github.com/arduino/pluggable-discovery-protocol-handler/v2
8+
license: other
89
licenses:
9-
- sources: arduino-cli@v0.35.0/LICENSE.txt
10-
text: |2
10+
- sources: LICENSE.txt
11+
text: |
12+
This file includes licensing information for serial-discovery
13+
14+
Copyright (c) 2018 ARDUINO SA (www.arduino.cc)
15+
16+
The software is released under the GNU General Public License, which covers the main body
17+
of the serial-discovery code. The terms of this license can be found at:
18+
https://www.gnu.org/licenses/gpl-3.0.en.html
19+
20+
You can be released from the requirements of the above licenses by purchasing
21+
a commercial license. Buying such a license is mandatory if you want to modify or
22+
otherwise use the software for commercial activities involving the Arduino
23+
software without disclosing the source code of your own applications. To purchase
24+
a commercial license, send an email to license@arduino.cc
25+
1126
GNU GENERAL PUBLIC LICENSE
1227
Version 3, 29 June 2007
1328
@@ -682,20 +697,13 @@ licenses:
682697
the library. If this is what you want to do, use the GNU Lesser General
683698
Public License instead of this License. But first, please read
684699
<https://www.gnu.org/licenses/why-not-lgpl.html>.
685-
- sources: arduino-cli@v0.35.0/license_header.tpl
686-
text: |
687-
This file is part of arduino-cli.
700+
- sources: README.md
701+
text: |-
702+
Copyright (c) 2021 ARDUINO SA (www.arduino.cc)
688703
689-
Copyright{{ if .Year }} {{.Year}}{{ end }} {{.Holder}}
690-
691-
This software is released under the GNU General Public License version 3,
692-
which covers the main part of arduino-cli.
693-
The terms of this license can be found at:
704+
The software is released under the GNU General Public License, which covers the main body
705+
of the serial-discovery code. The terms of this license can be found at:
694706
https://www.gnu.org/licenses/gpl-3.0.en.html
695707
696-
You can be released from the requirements of the above licenses by purchasing
697-
a commercial license. Buying such a license is mandatory if you want to
698-
modify or otherwise use the software for commercial activities involving the
699-
Arduino software without disclosing the source code of your own applications.
700-
To purchase a commercial license, send an email to license@arduino.cc.
708+
See [LICENSE.txt](https://github.com/arduino/pluggable-discovery-protocol-handler/blob/master/LICENSE.txt) for details.
701709
notices: []

.licenses/arduino-create-agent/go/github.com/leonelquinteros/gotext.dep.yml

-34
This file was deleted.

.licenses/arduino-create-agent/go/github.com/leonelquinteros/gotext/plurals.dep.yml

-35
This file was deleted.

conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func uploadHandler(c *gin.Context) {
161161

162162
go func() {
163163
// Resolve commandline
164-
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, &Tools)
164+
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, Tools)
165165
if err != nil {
166166
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
167167
return

go.mod

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ go 1.21
55
require (
66
fyne.io/systray v1.10.0
77
github.com/ProtonMail/go-crypto v1.1.0-alpha.0
8-
github.com/arduino/arduino-cli v0.35.0
98
github.com/arduino/go-paths-helper v1.12.0
9+
github.com/arduino/go-serial-utils v0.1.2
10+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.2.0
1011
github.com/blang/semver v3.5.1+incompatible
1112
github.com/codeclysm/extract/v3 v3.1.1
1213
github.com/gin-contrib/cors v1.5.0
@@ -28,6 +29,7 @@ require (
2829

2930
require (
3031
github.com/AnatolyRugalev/goregen v0.1.0 // indirect
32+
github.com/arduino/go-properties-orderedmap v1.8.0 // indirect
3133
github.com/bytedance/sonic v1.10.1 // indirect
3234
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
3335
github.com/chenzhuoyu/iasm v0.9.0 // indirect
@@ -56,13 +58,13 @@ require (
5658
github.com/kr/binarydist v0.1.0 // indirect
5759
github.com/kr/pretty v0.3.1 // indirect
5860
github.com/leodido/go-urn v1.2.4 // indirect
59-
github.com/leonelquinteros/gotext v1.4.0 // indirect
6061
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
6162
github.com/mattn/go-isatty v0.0.20 // indirect
6263
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6364
github.com/modern-go/reflect2 v1.0.2 // indirect
6465
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
6566
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
67+
github.com/rogpeppe/go-internal v1.12.0 // indirect
6668
github.com/sergi/go-diff v1.3.1 // indirect
6769
github.com/smartystreets/goconvey v1.6.4 // indirect
6870
github.com/tevino/abool v1.2.0 // indirect
@@ -76,5 +78,6 @@ require (
7678
golang.org/x/text v0.14.0 // indirect
7779
golang.org/x/tools v0.17.0 // indirect
7880
google.golang.org/protobuf v1.32.0 // indirect
81+
gopkg.in/ini.v1 v1.67.0 // indirect
7982
gopkg.in/yaml.v3 v3.0.1 // indirect
8083
)

go.sum

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ github.com/AnatolyRugalev/goregen v0.1.0 h1:xrdXkLaskMnbxW0x4FWNj2yoednv0X2bcTBW
44
github.com/AnatolyRugalev/goregen v0.1.0/go.mod h1:sVlY1tjcirqLBRZnCcIq1+7/Lwmqz5g7IK8AStjOVzI=
55
github.com/ProtonMail/go-crypto v1.1.0-alpha.0 h1:nHGfwXmFvJrSR9xu8qL7BkO4DqTHXE9N5vPhgY2I+j0=
66
github.com/ProtonMail/go-crypto v1.1.0-alpha.0/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
7-
github.com/arduino/arduino-cli v0.35.0 h1:tkSpGun1hMgJPkvZJjofejWD+ivhCpvsl49H4n81iSc=
8-
github.com/arduino/arduino-cli v0.35.0/go.mod h1:9aoXZdVGQ9uwf2M1nCSB+T051KbldslMFiJaHVycVsg=
7+
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
98
github.com/arduino/go-paths-helper v1.12.0 h1:xizOQtI9iHdl19qXd1EmWg5i9W//2bOCOYwlNv8F61E=
109
github.com/arduino/go-paths-helper v1.12.0/go.mod h1:jcpW4wr0u69GlXhTYydsdsqAjLaYK5n7oWHfKqOG6LM=
10+
github.com/arduino/go-properties-orderedmap v1.8.0 h1:wEfa6hHdpezrVOh787OmClsf/Kd8qB+zE3P2Xbrn0CQ=
11+
github.com/arduino/go-properties-orderedmap v1.8.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
12+
github.com/arduino/go-serial-utils v0.1.2 h1:MRFwME4w/uaVkJ1R+wzz4KSbI9cF9IDVrYorazvjpTk=
13+
github.com/arduino/go-serial-utils v0.1.2/go.mod h1:kzIsNPgz8DFAd1sAFKve4ubxrdGcwQ4XzvRLlztsgnE=
14+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.2.0 h1:v7og6LpskewFabmaShKVzWXl5MXbmsxaRP3yo4dJta8=
15+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.2.0/go.mod h1:1dgblsmK2iBx3L5iNTyRIokeaxbTLUrYiUbHBK6yC3Y=
1116
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
1217
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
1318
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
@@ -97,8 +102,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
97102
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
98103
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
99104
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
100-
github.com/leonelquinteros/gotext v1.4.0 h1:2NHPCto5IoMXbrT0bldPrxj0qM5asOCwtb1aUQZ1tys=
101-
github.com/leonelquinteros/gotext v1.4.0/go.mod h1:yZGXREmoGTtBvZHNcc+Yfug49G/2spuF/i/Qlsvz1Us=
102105
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d h1:Zj+PHjnhRYWBK6RqCDBcAhLXoi3TzC27Zad/Vn+gnVQ=
103106
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d/go.mod h1:WZy8Q5coAB1zhY9AOBJP0O6J4BuDfbupUDavKY+I3+s=
104107
github.com/manveru/gobdd v0.0.0-20131210092515-f1a17fdd710b h1:3E44bLeN8uKYdfQqVQycPnaVviZdBLbizFhU49mtbe4=
@@ -123,8 +126,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
123126
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
124127
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
125128
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
126-
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
127-
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
129+
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
130+
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
128131
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
129132
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
130133
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=

hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func checkCmd(m []byte) {
177177
// will catch send and sendnobuf and sendraw
178178
go spWrite(s)
179179
} else if strings.HasPrefix(sl, "list") {
180-
go spList()
180+
go serialPorts.List()
181181
} else if strings.HasPrefix(sl, "downloadtool") {
182182
go func() {
183183
args := strings.Split(s, " ")

main.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io"
2727
"os"
2828
"os/exec"
29+
"regexp"
2930
"runtime"
3031
"runtime/debug"
3132
"strconv"
@@ -69,24 +70,27 @@ var (
6970

7071
// iniflags
7172
var (
72-
address = iniConf.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
73-
appName = iniConf.String("appName", "", "")
74-
gcType = iniConf.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
75-
hostname = iniConf.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
76-
httpProxy = iniConf.String("httpProxy", "", "Proxy server for HTTP requests")
77-
httpsProxy = iniConf.String("httpsProxy", "", "Proxy server for HTTPS requests")
78-
indexURL = iniConf.String("indexURL", "https://downloads.arduino.cc/packages/package_index.json", "The address from where to download the index json containing the location of upload tools")
79-
iniConf = flag.NewFlagSet("ini", flag.ContinueOnError)
80-
logDump = iniConf.String("log", "off", "off = (default)")
81-
origins = iniConf.String("origins", "", "Allowed origin list for CORS")
82-
regExpFilter = iniConf.String("regex", "usb|acm|com", "Regular expression to filter serial port list")
83-
signatureKey = iniConf.String("signatureKey", globals.SignatureKey, "Pem-encoded public key to verify signed commandlines")
84-
updateURL = iniConf.String("updateUrl", "", "")
85-
verbose = iniConf.Bool("v", true, "show debug logging")
86-
crashreport = iniConf.Bool("crashreport", false, "enable crashreport logging")
87-
autostartMacOS = iniConf.Bool("autostartMacOS", true, "the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)")
73+
address = iniConf.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
74+
appName = iniConf.String("appName", "", "")
75+
gcType = iniConf.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
76+
hostname = iniConf.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
77+
httpProxy = iniConf.String("httpProxy", "", "Proxy server for HTTP requests")
78+
httpsProxy = iniConf.String("httpsProxy", "", "Proxy server for HTTPS requests")
79+
indexURL = iniConf.String("indexURL", "https://downloads.arduino.cc/packages/package_index.json", "The address from where to download the index json containing the location of upload tools")
80+
iniConf = flag.NewFlagSet("ini", flag.ContinueOnError)
81+
logDump = iniConf.String("log", "off", "off = (default)")
82+
origins = iniConf.String("origins", "", "Allowed origin list for CORS")
83+
portsFilterRegexp = iniConf.String("regex", "usb|acm|com", "Regular expression to filter serial port list")
84+
signatureKey = iniConf.String("signatureKey", globals.SignatureKey, "Pem-encoded public key to verify signed commandlines")
85+
updateURL = iniConf.String("updateUrl", "", "")
86+
verbose = iniConf.Bool("v", true, "show debug logging")
87+
crashreport = iniConf.Bool("crashreport", false, "enable crashreport logging")
88+
autostartMacOS = iniConf.Bool("autostartMacOS", true, "the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)")
8889
)
8990

91+
// the ports filter provided by the user via the -regex flag, if any
92+
var portsFilter *regexp.Regexp
93+
9094
var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHTML))
9195

9296
// If you navigate to this server's homepage, you'll get this HTML
@@ -97,7 +101,7 @@ var homeTemplateHTML string
97101

98102
// global clients
99103
var (
100-
Tools tools.Tools
104+
Tools *tools.Tools
101105
Systray systray.Systray
102106
Index *index.Resource
103107
)
@@ -247,7 +251,7 @@ func loop() {
247251

248252
// Instantiate Index and Tools
249253
Index = index.Init(*indexURL, config.GetDataDir())
250-
Tools = *tools.New(config.GetDataDir(), Index, logger)
254+
Tools = tools.New(config.GetDataDir(), Index, logger)
251255

252256
// see if we are supposed to wait 5 seconds
253257
if *isLaunchSelf {
@@ -302,19 +306,13 @@ func loop() {
302306
}
303307

304308
// see if they provided a regex filter
305-
if len(*regExpFilter) > 0 {
306-
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
307-
}
308-
309-
// list serial ports
310-
portList, _ := enumerateSerialPorts()
311-
log.Println("Your serial ports:")
312-
if len(portList) == 0 {
313-
log.Println("\tThere are no serial ports to list.")
314-
}
315-
for _, element := range portList {
316-
log.Printf("\t%v\n", element)
317-
309+
if len(*portsFilterRegexp) > 0 {
310+
log.Printf("You specified a serial port regular expression filter: %v\n", *portsFilterRegexp)
311+
if filter, err := regexp.Compile("(?i)" + *portsFilterRegexp); err != nil {
312+
log.Panicf("Error compiling the regex filter: %v\n", err)
313+
} else {
314+
portsFilter = filter
315+
}
318316
}
319317

320318
if !*verbose {
@@ -344,15 +342,15 @@ func loop() {
344342
}
345343
}
346344

345+
// launch the discoveries for the running system
346+
go serialPorts.Run()
347347
// launch the hub routine which is the singleton for the websocket server
348348
go h.run()
349349
// launch our serial port routine
350350
go sh.run()
351351
// launch our dummy data routine
352352
//go d.run()
353353

354-
go discoverLoop()
355-
356354
r := gin.New()
357355

358356
socketHandler := wsHandler().ServeHTTP

0 commit comments

Comments
 (0)