Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9535905

Browse files
committedApr 16, 2025·
rename into h and revert split of serial.go
1 parent 815a791 commit 9535905

File tree

3 files changed

+194
-191
lines changed

3 files changed

+194
-191
lines changed
 

‎hub.go

Lines changed: 85 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type hub struct {
5353
// Serial hub to communicate with serial ports
5454
serialHub *serialhub
5555

56-
serialPortList *serialPortList
56+
serialPortList *SerialPortList
5757

5858
tools *tools.Tools
5959

@@ -69,7 +69,7 @@ func newHub(tools *tools.Tools, systray *systray.Systray) *hub {
6969
onUnregister := func(port *serport) {
7070
broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
7171
}
72-
serialHub := newSerialHub(onRegister, onUnregister)
72+
serialHubub := newSerialHub(onRegister, onUnregister)
7373

7474
onList := func(data []byte) {
7575
broadcastSys <- data
@@ -85,7 +85,7 @@ func newHub(tools *tools.Tools, systray *systray.Systray) *hub {
8585
register: make(chan *connection),
8686
unregister: make(chan *connection),
8787
connections: make(map[*connection]bool),
88-
serialHub: serialHub,
88+
serialHub: serialHubub,
8989
serialPortList: serialPortList,
9090
tools: tools,
9191
systray: systray,
@@ -110,53 +110,53 @@ const commands = `{
110110
]
111111
}`
112112

113-
func (hub *hub) unregisterConnection(c *connection) {
114-
if _, contains := hub.connections[c]; !contains {
113+
func (h *hub) unregisterConnection(c *connection) {
114+
if _, contains := h.connections[c]; !contains {
115115
return
116116
}
117-
delete(hub.connections, c)
117+
delete(h.connections, c)
118118
close(c.send)
119119
}
120120

121-
func (hub *hub) sendToRegisteredConnections(data []byte) {
122-
for c := range hub.connections {
121+
func (h *hub) sendToRegisteredConnections(data []byte) {
122+
for c := range h.connections {
123123
select {
124124
case c.send <- data:
125125
//log.Print("did broadcast to ")
126126
//log.Print(c.ws.RemoteAddr())
127127
//c.send <- []byte("hello world")
128128
default:
129-
hub.unregisterConnection(c)
129+
h.unregisterConnection(c)
130130
}
131131
}
132132
}
133133

134-
func (hub *hub) run() {
135-
go hub.serialPortList.Run()
134+
func (h *hub) run() {
135+
go h.serialPortList.Run()
136136

137137
for {
138138
select {
139-
case c := <-hub.register:
140-
hub.connections[c] = true
139+
case c := <-h.register:
140+
h.connections[c] = true
141141
// send supported commands
142142
c.send <- []byte(fmt.Sprintf(`{"Version" : "%s"} `, version))
143143
c.send <- []byte(html.EscapeString(commands))
144144
c.send <- []byte(fmt.Sprintf(`{"Hostname" : "%s"} `, *hostname))
145145
c.send <- []byte(fmt.Sprintf(`{"OS" : "%s"} `, runtime.GOOS))
146-
case c := <-hub.unregister:
147-
hub.unregisterConnection(c)
148-
case m := <-hub.broadcast:
146+
case c := <-h.unregister:
147+
h.unregisterConnection(c)
148+
case m := <-h.broadcast:
149149
if len(m) > 0 {
150-
hub.checkCmd(m)
151-
hub.sendToRegisteredConnections(m)
150+
h.checkCmd(m)
151+
h.sendToRegisteredConnections(m)
152152
}
153-
case m := <-hub.broadcastSys:
154-
hub.sendToRegisteredConnections(m)
153+
case m := <-h.broadcastSys:
154+
h.sendToRegisteredConnections(m)
155155
}
156156
}
157157
}
158158

159-
func (hub *hub) checkCmd(m []byte) {
159+
func (h *hub) checkCmd(m []byte) {
160160
//log.Print("Inside checkCmd")
161161
s := string(m[:])
162162

@@ -171,18 +171,18 @@ func (hub *hub) checkCmd(m []byte) {
171171

172172
args := strings.Split(s, " ")
173173
if len(args) < 3 {
174-
go hub.spErr("You did not specify a port and baud rate in your open cmd")
174+
go h.spErr("You did not specify a port and baud rate in your open cmd")
175175
return
176176
}
177177
if len(args[1]) < 1 {
178-
go hub.spErr("You did not specify a serial port")
178+
go h.spErr("You did not specify a serial port")
179179
return
180180
}
181181

182182
baudStr := strings.Replace(args[2], "\n", "", -1)
183183
baud, err := strconv.Atoi(baudStr)
184184
if err != nil {
185-
go hub.spErr("Problem converting baud rate " + args[2])
185+
go h.spErr("Problem converting baud rate " + args[2])
186186
return
187187
}
188188
// pass in buffer type now as string. if user does not
@@ -193,30 +193,30 @@ func (hub *hub) checkCmd(m []byte) {
193193
buftype := strings.Replace(args[3], "\n", "", -1)
194194
bufferAlgorithm = buftype
195195
}
196-
go hub.spHandlerOpen(args[1], baud, bufferAlgorithm)
196+
go h.spHandlerOpen(args[1], baud, bufferAlgorithm)
197197

198198
} else if strings.HasPrefix(sl, "close") {
199199

200200
args := strings.Split(s, " ")
201201
if len(args) > 1 {
202-
go hub.spClose(args[1])
202+
go h.spClose(args[1])
203203
} else {
204-
go hub.spErr("You did not specify a port to close")
204+
go h.spErr("You did not specify a port to close")
205205
}
206206

207207
} else if strings.HasPrefix(sl, "killupload") {
208208
// kill the running process (assumes singleton for now)
209209
go func() {
210210
upload.Kill()
211-
hub.broadcastSys <- []byte("{\"uploadStatus\": \"Killed\"}")
211+
h.broadcastSys <- []byte("{\"uploadStatus\": \"Killed\"}")
212212
log.Println("{\"uploadStatus\": \"Killed\"}")
213213
}()
214214

215215
} else if strings.HasPrefix(sl, "send") {
216216
// will catch send and sendnobuf and sendraw
217-
go hub.spWrite(s)
217+
go h.spWrite(s)
218218
} else if strings.HasPrefix(sl, "list") {
219-
go hub.serialPortList.List()
219+
go h.serialPortList.List()
220220
} else if strings.HasPrefix(sl, "downloadtool") {
221221
go func() {
222222
args := strings.Split(s, " ")
@@ -227,7 +227,7 @@ func (hub *hub) checkCmd(m []byte) {
227227
if len(args) <= 1 {
228228
mapD := map[string]string{"DownloadStatus": "Error", "Msg": "Not enough arguments"}
229229
mapB, _ := json.Marshal(mapD)
230-
hub.broadcastSys <- mapB
230+
h.broadcastSys <- mapB
231231
return
232232
}
233233
if len(args) > 1 {
@@ -250,51 +250,51 @@ func (hub *hub) checkCmd(m []byte) {
250250
reportPendingProgress := func(msg string) {
251251
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}
252252
mapB, _ := json.Marshal(mapD)
253-
hub.broadcastSys <- mapB
253+
h.broadcastSys <- mapB
254254
}
255-
err := hub.tools.Download(pack, tool, toolVersion, behaviour, reportPendingProgress)
255+
err := h.tools.Download(pack, tool, toolVersion, behaviour, reportPendingProgress)
256256
if err != nil {
257257
mapD := map[string]string{"DownloadStatus": "Error", "Msg": err.Error()}
258258
mapB, _ := json.Marshal(mapD)
259-
hub.broadcastSys <- mapB
259+
h.broadcastSys <- mapB
260260
} else {
261261
mapD := map[string]string{"DownloadStatus": "Success", "Msg": "Map Updated"}
262262
mapB, _ := json.Marshal(mapD)
263-
hub.broadcastSys <- mapB
263+
h.broadcastSys <- mapB
264264
}
265265
}()
266266
} else if strings.HasPrefix(sl, "log") {
267-
go hub.logAction(sl)
267+
go h.logAction(sl)
268268
} else if strings.HasPrefix(sl, "restart") {
269-
// potentially, the sysStray dependencies can be removed https://github.com/arduino/arduino-create-agent/issues/1013
269+
// potentially, the sysStray dependencies can be removed https://gith.com/arduino/arduino-create-agent/issues/1013
270270
log.Println("Received restart from the daemon. Why? Boh")
271-
hub.systray.Restart()
271+
h.systray.Restart()
272272
} else if strings.HasPrefix(sl, "exit") {
273-
hub.systray.Quit()
273+
h.systray.Quit()
274274
} else if strings.HasPrefix(sl, "memstats") {
275-
hub.memoryStats()
275+
h.memoryStats()
276276
} else if strings.HasPrefix(sl, "gc") {
277-
hub.garbageCollection()
277+
h.garbageCollection()
278278
} else if strings.HasPrefix(sl, "hostname") {
279-
hub.getHostname()
279+
h.getHostname()
280280
} else if strings.HasPrefix(sl, "version") {
281-
hub.getVersion()
281+
h.getVersion()
282282
} else {
283-
go hub.spErr("Could not understand command.")
283+
go h.spErr("Could not understand command.")
284284
}
285285
}
286286

287287
type logWriter struct {
288288
onWrite func([]byte)
289289
}
290290

291-
func (hub *hub) logAction(sl string) {
291+
func (h *hub) logAction(sl string) {
292292
if strings.HasPrefix(sl, "log on") {
293293
*logDump = "on"
294294

295295
logWriter := logWriter{}
296296
logWriter.onWrite = func(p []byte) {
297-
hub.broadcastSys <- p
297+
h.broadcastSys <- p
298298
}
299299

300300
multiWriter := io.MultiWriter(&logWriter, os.Stderr)
@@ -304,7 +304,7 @@ func (hub *hub) logAction(sl string) {
304304
log.SetOutput(os.Stderr)
305305
// } else if strings.HasPrefix(sl, "log show") {
306306
// TODO: send all the saved log to websocket
307-
//hub.broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
307+
//h.broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
308308
}
309309
}
310310

@@ -313,35 +313,35 @@ func (u *logWriter) Write(p []byte) (n int, err error) {
313313
return len(p), nil
314314
}
315315

316-
func (hub *hub) memoryStats() {
316+
func (h *hub) memoryStats() {
317317
var memStats runtime.MemStats
318318
runtime.ReadMemStats(&memStats)
319319
json, _ := json.Marshal(memStats)
320320
log.Printf("memStats:%v\n", string(json))
321-
hub.broadcastSys <- json
321+
h.broadcastSys <- json
322322
}
323323

324-
func (hub *hub) getHostname() {
325-
hub.broadcastSys <- []byte("{\"Hostname\" : \"" + *hostname + "\"}")
324+
func (h *hub) getHostname() {
325+
h.broadcastSys <- []byte("{\"Hostname\" : \"" + *hostname + "\"}")
326326
}
327327

328-
func (hub *hub) getVersion() {
329-
hub.broadcastSys <- []byte("{\"Version\" : \"" + version + "\"}")
328+
func (h *hub) getVersion() {
329+
h.broadcastSys <- []byte("{\"Version\" : \"" + version + "\"}")
330330
}
331331

332-
func (hub *hub) garbageCollection() {
332+
func (h *hub) garbageCollection() {
333333
log.Printf("Starting garbageCollection()\n")
334-
hub.broadcastSys <- []byte("{\"gc\":\"starting\"}")
335-
hub.memoryStats()
334+
h.broadcastSys <- []byte("{\"gc\":\"starting\"}")
335+
h.memoryStats()
336336
debug.SetGCPercent(100)
337337
debug.FreeOSMemory()
338338
debug.SetGCPercent(-1)
339339
log.Printf("Done with garbageCollection()\n")
340-
hub.broadcastSys <- []byte("{\"gc\":\"done\"}")
341-
hub.memoryStats()
340+
h.broadcastSys <- []byte("{\"gc\":\"done\"}")
341+
h.memoryStats()
342342
}
343343

344-
func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
344+
func (h *hub) spHandlerOpen(portname string, baud int, buftype string) {
345345

346346
log.Print("Inside spHandler")
347347

@@ -365,8 +365,8 @@ func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
365365
if err != nil {
366366
//log.Fatal(err)
367367
log.Print("Error opening port " + err.Error())
368-
//hub.broadcastSys <- []byte("Error opening port. " + err.Error())
369-
hub.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")
368+
//h.broadcastSys <- []byte("Error opening port. " + err.Error())
369+
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")
370370

371371
return
372372
}
@@ -384,34 +384,34 @@ func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
384384
}
385385

386386
p.OnMessage = func(msg []byte) {
387-
hub.broadcastSys <- msg
387+
h.broadcastSys <- msg
388388
}
389389
p.OnClose = func(port *serport) {
390-
hub.serialPortList.MarkPortAsClosed(p.portName)
391-
hub.serialPortList.List()
390+
h.serialPortList.MarkPortAsClosed(p.portName)
391+
h.serialPortList.List()
392392
}
393393

394394
var bw Bufferflow
395395

396396
switch buftype {
397397
case "timed":
398-
bw = NewBufferflowTimed(portname, hub.broadcastSys)
398+
bw = NewBufferflowTimed(portname, h.broadcastSys)
399399
case "timedraw":
400-
bw = NewBufferflowTimedRaw(portname, hub.broadcastSys)
400+
bw = NewBufferflowTimedRaw(portname, h.broadcastSys)
401401
case "default":
402-
bw = NewBufferflowDefault(portname, hub.broadcastSys)
402+
bw = NewBufferflowDefault(portname, h.broadcastSys)
403403
default:
404404
log.Panicf("unknown buffer type: %s", buftype)
405405
}
406406

407407
bw.Init()
408408
p.bufferwatcher = bw
409409

410-
hub.serialHub.Register(p)
411-
defer hub.serialHub.Unregister(p)
410+
h.serialHub.Register(p)
411+
defer h.serialHub.Unregister(p)
412412

413-
hub.serialPortList.MarkPortAsOpened(portname)
414-
hub.serialPortList.List()
413+
h.serialPortList.MarkPortAsOpened(portname)
414+
h.serialPortList.List()
415415

416416
// this is internally buffered thread to not send to serial port if blocked
417417
go p.writerBuffered()
@@ -422,19 +422,23 @@ func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
422422

423423
p.reader(buftype)
424424

425-
hub.serialPortList.List()
425+
h.serialPortList.List()
426426
}
427427

428-
func (hub *hub) spClose(portname string) {
429-
if myport, ok := hub.serialHub.FindPortByName(portname); ok {
430-
hub.broadcastSys <- []byte("Closing serial port " + portname)
428+
func (h *hub) spErr(err string) {
429+
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
430+
}
431+
432+
func (h *hub) spClose(portname string) {
433+
if myport, ok := h.serialHub.FindPortByName(portname); ok {
434+
h.broadcastSys <- []byte("Closing serial port " + portname)
431435
myport.Close()
432436
} else {
433-
hub.spErr("We could not find the serial port " + portname + " that you were trying to close.")
437+
h.spErr("We could not find the serial port " + portname + " that you were trying to close.")
434438
}
435439
}
436440

437-
func (hub *hub) spWrite(arg string) {
441+
func (h *hub) spWrite(arg string) {
438442
// we will get a string of comXX asdf asdf asdf
439443
//log.Println("Inside spWrite arg: " + arg)
440444
arg = strings.TrimPrefix(arg, " ")
@@ -443,7 +447,7 @@ func (hub *hub) spWrite(arg string) {
443447
if len(args) != 3 {
444448
errstr := "Could not parse send command: " + arg
445449
//log.Println(errstr)
446-
hub.spErr(errstr)
450+
h.spErr(errstr)
447451
return
448452
}
449453
bufferingMode := args[0]
@@ -454,10 +458,10 @@ func (hub *hub) spWrite(arg string) {
454458
//log.Println("The data is:" + data + "---")
455459

456460
// See if we have this port open
457-
port, ok := hub.serialHub.FindPortByName(portname)
461+
port, ok := h.serialHub.FindPortByName(portname)
458462
if !ok {
459463
// we couldn't find the port, so send err
460-
hub.spErr("We could not find the serial port " + portname + " that you were trying to write to.")
464+
h.spErr("We could not find the serial port " + portname + " that you were trying to write to.")
461465
return
462466
}
463467

@@ -466,15 +470,10 @@ func (hub *hub) spWrite(arg string) {
466470
case "send", "sendnobuf", "sendraw":
467471
// valid buffering mode, go ahead
468472
default:
469-
hub.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
473+
h.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
470474
return
471475
}
472476

473477
// send it to the write channel
474478
port.Write(data, bufferingMode)
475479
}
476-
477-
func (hub *hub) spErr(err string) {
478-
//log.Println("Sending err back: ", err)
479-
hub.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
480-
}

‎serialportlist.go renamed to ‎serial.go

Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
// Copyright 2022 Arduino SA
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published
5+
// by the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
// Supports Windows, Linux, Mac, BeagleBone Black, and Raspberry Pi
17+
118
package main
219

320
import (
421
"encoding/json"
522
"slices"
23+
"strings"
624
"sync"
725
"time"
826

@@ -11,7 +29,26 @@ import (
1129
"github.com/sirupsen/logrus"
1230
)
1331

14-
type serialPortList struct {
32+
type serialhub struct {
33+
// Opened serial ports.
34+
ports map[*serport]bool
35+
36+
mu sync.Mutex
37+
38+
onRegister func(port *serport)
39+
onUnregister func(port *serport)
40+
}
41+
42+
func newSerialHub(onRegister func(port *serport), onUnregister func(port *serport)) *serialhub {
43+
return &serialhub{
44+
ports: make(map[*serport]bool),
45+
onRegister: onRegister,
46+
onUnregister: onUnregister,
47+
}
48+
}
49+
50+
// SerialPortList is the serial port list
51+
type SerialPortList struct {
1552
Ports []*SpPortItem
1653
portsLock sync.Mutex
1754

@@ -34,16 +71,58 @@ type SpPortItem struct {
3471
ProductID string
3572
}
3673

37-
func newSerialPortList(tools *tools.Tools, onList func(data []byte), onErr func(err string)) *serialPortList {
38-
return &serialPortList{
74+
// serialPorts contains the ports attached to the machine
75+
var serialPorts SerialPortList
76+
77+
var sh = serialhub{
78+
ports: make(map[*serport]bool),
79+
}
80+
81+
// Register serial ports from the connections.
82+
func (sh *serialhub) Register(port *serport) {
83+
sh.mu.Lock()
84+
sh.onRegister(port)
85+
// sh.h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
86+
sh.ports[port] = true
87+
sh.mu.Unlock()
88+
}
89+
90+
// Unregister requests from connections.
91+
func (sh *serialhub) Unregister(port *serport) {
92+
sh.mu.Lock()
93+
//log.Print("Unregistering a port: ", p.portConf.Name)
94+
// h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
95+
sh.onUnregister(port)
96+
delete(sh.ports, port)
97+
close(port.sendBuffered)
98+
close(port.sendNoBuf)
99+
sh.mu.Unlock()
100+
}
101+
102+
func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
103+
sh.mu.Lock()
104+
defer sh.mu.Unlock()
105+
106+
for port := range sh.ports {
107+
if strings.EqualFold(port.portConf.Name, portname) {
108+
// we found our port
109+
//spHandlerClose(port)
110+
return port, true
111+
}
112+
}
113+
return nil, false
114+
}
115+
116+
func newSerialPortList(tools *tools.Tools, onList func(data []byte), onErr func(err string)) *SerialPortList {
117+
return &SerialPortList{
39118
tools: tools,
40119
OnList: onList,
41120
OnErr: onErr,
42121
}
43122
}
44123

45124
// List broadcasts a Json representation of the ports found
46-
func (sp *serialPortList) List() {
125+
func (sp *SerialPortList) List() {
47126
sp.portsLock.Lock()
48127
ls, err := json.MarshalIndent(sp, "", "\t")
49128
sp.portsLock.Unlock()
@@ -55,28 +134,8 @@ func (sp *serialPortList) List() {
55134
}
56135
}
57136

58-
// MarkPortAsOpened marks a port as opened by the user
59-
func (sp *serialPortList) MarkPortAsOpened(portname string) {
60-
sp.portsLock.Lock()
61-
defer sp.portsLock.Unlock()
62-
port := sp.getPortByName(portname)
63-
if port != nil {
64-
port.IsOpen = true
65-
}
66-
}
67-
68-
// MarkPortAsClosed marks a port as no more opened by the user
69-
func (sp *serialPortList) MarkPortAsClosed(portname string) {
70-
sp.portsLock.Lock()
71-
defer sp.portsLock.Unlock()
72-
port := sp.getPortByName(portname)
73-
if port != nil {
74-
port.IsOpen = false
75-
}
76-
}
77-
78137
// Run is the main loop for port discovery and management
79-
func (sp *serialPortList) Run() {
138+
func (sp *SerialPortList) Run() {
80139
for retries := 0; retries < 10; retries++ {
81140
sp.runSerialDiscovery()
82141

@@ -86,7 +145,7 @@ func (sp *serialPortList) Run() {
86145
logrus.Errorf("Failed restarting serial discovery. Giving up...")
87146
}
88147

89-
func (sp *serialPortList) runSerialDiscovery() {
148+
func (sp *SerialPortList) runSerialDiscovery() {
90149
// First ensure that all the discoveries are available
91150
noOpProgress := func(msg string) {}
92151
if err := sp.tools.Download("builtin", "serial-discovery", "latest", "keep", noOpProgress); err != nil {
@@ -115,7 +174,6 @@ func (sp *serialPortList) runSerialDiscovery() {
115174
logrus.Errorf("Error starting event watcher on serial-discovery: %s", err)
116175
panic(err)
117176
}
118-
d.List()
119177

120178
logrus.Infof("Serial discovery started, watching for events")
121179
for ev := range events {
@@ -132,13 +190,13 @@ func (sp *serialPortList) runSerialDiscovery() {
132190
logrus.Errorf("Serial discovery stopped.")
133191
}
134192

135-
func (sp *serialPortList) reset() {
193+
func (sp *SerialPortList) reset() {
136194
sp.portsLock.Lock()
137195
defer sp.portsLock.Unlock()
138196
sp.Ports = []*SpPortItem{}
139197
}
140198

141-
func (sp *serialPortList) add(addedPort *discovery.Port) {
199+
func (sp *SerialPortList) add(addedPort *discovery.Port) {
142200
if addedPort.Protocol != "serial" {
143201
return
144202
}
@@ -181,7 +239,7 @@ func (sp *serialPortList) add(addedPort *discovery.Port) {
181239
})
182240
}
183241

184-
func (sp *serialPortList) remove(removedPort *discovery.Port) {
242+
func (sp *SerialPortList) remove(removedPort *discovery.Port) {
185243
sp.portsLock.Lock()
186244
defer sp.portsLock.Unlock()
187245

@@ -191,7 +249,27 @@ func (sp *serialPortList) remove(removedPort *discovery.Port) {
191249
})
192250
}
193251

194-
func (sp *serialPortList) getPortByName(portname string) *SpPortItem {
252+
// MarkPortAsOpened marks a port as opened by the user
253+
func (sp *SerialPortList) MarkPortAsOpened(portname string) {
254+
sp.portsLock.Lock()
255+
defer sp.portsLock.Unlock()
256+
port := sp.getPortByName(portname)
257+
if port != nil {
258+
port.IsOpen = true
259+
}
260+
}
261+
262+
// MarkPortAsClosed marks a port as no more opened by the user
263+
func (sp *SerialPortList) MarkPortAsClosed(portname string) {
264+
sp.portsLock.Lock()
265+
defer sp.portsLock.Unlock()
266+
port := sp.getPortByName(portname)
267+
if port != nil {
268+
port.IsOpen = false
269+
}
270+
}
271+
272+
func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
195273
for _, port := range sp.Ports {
196274
if port.Name == portname {
197275
return port

‎serialhub.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.