Skip to content

Commit ec6f5fe

Browse files
committed
Renamed and slightly refactored port enumerators
1 parent 3b5be9f commit ec6f5fe

File tree

5 files changed

+60
-92
lines changed

5 files changed

+60
-92
lines changed

discovery.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ const timeoutConst = 2
4242
// SavedNetworkPorts contains the ports which we know are already connected
4343
var SavedNetworkPorts []OsSerialPort
4444

45-
// GetNetworkList returns a list of Network Ports
45+
// enumerateNetworkPorts returns a list of Network Ports
4646
// The research of network ports is articulated in two phases. First we add new ports coming from
4747
// the bonjour module, then we prune the boards who don't respond to a ping
48-
func GetNetworkList() ([]OsSerialPort, error) {
48+
func enumerateNetworkPorts() ([]OsSerialPort, error) {
4949
newPorts, err := getPorts()
5050
if err != nil {
5151
return nil, err

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func loop() {
307307
}
308308

309309
// list serial ports
310-
portList, _ := GetList(false)
310+
portList, _ := enumerateSerialPorts()
311311
log.Println("Your serial ports:")
312312
if len(portList) == 0 {
313313
log.Println("\tThere are no serial ports to list.")

serial.go

+51-77
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"encoding/json"
22+
"fmt"
2223
"strconv"
2324
"strings"
2425
"sync"
@@ -72,10 +73,10 @@ type SpPortItem struct {
7273
}
7374

7475
// SerialPorts contains the ports attached to the machine
75-
var SerialPorts SpPortList
76+
var serialPorts SpPortList
7677

77-
// NetworkPorts contains the ports on the network
78-
var NetworkPorts SpPortList
78+
// networkPorts contains the ports on the network
79+
var networkPorts SpPortList
7980

8081
var sh = serialhub{
8182
//write: make(chan *serport, chan []byte),
@@ -130,13 +131,13 @@ func spList(network bool) {
130131
var ls []byte
131132
var err error
132133
if network {
133-
NetworkPorts.Mu.Lock()
134-
ls, err = json.MarshalIndent(&NetworkPorts, "", "\t")
135-
NetworkPorts.Mu.Unlock()
134+
networkPorts.Mu.Lock()
135+
ls, err = json.MarshalIndent(&networkPorts, "", "\t")
136+
networkPorts.Mu.Unlock()
136137
} else {
137-
SerialPorts.Mu.Lock()
138-
ls, err = json.MarshalIndent(&SerialPorts, "", "\t")
139-
SerialPorts.Mu.Unlock()
138+
serialPorts.Mu.Lock()
139+
ls, err = json.MarshalIndent(&serialPorts, "", "\t")
140+
serialPorts.Mu.Unlock()
140141
}
141142
if err != nil {
142143
//log.Println(err)
@@ -149,81 +150,65 @@ func spList(network bool) {
149150

150151
// discoverLoop periodically update the list of ports found
151152
func discoverLoop() {
152-
SerialPorts.Mu.Lock()
153-
SerialPorts.Network = false
154-
SerialPorts.Ports = make([]SpPortItem, 0)
155-
SerialPorts.Mu.Unlock()
156-
NetworkPorts.Mu.Lock()
157-
NetworkPorts.Network = true
158-
NetworkPorts.Ports = make([]SpPortItem, 0)
159-
NetworkPorts.Mu.Unlock()
153+
serialPorts.Mu.Lock()
154+
serialPorts.Network = false
155+
serialPorts.Ports = make([]SpPortItem, 0)
156+
serialPorts.Mu.Unlock()
157+
networkPorts.Mu.Lock()
158+
networkPorts.Network = true
159+
networkPorts.Ports = make([]SpPortItem, 0)
160+
networkPorts.Mu.Unlock()
160161

161162
go func() {
162163
for {
163164
if !upload.Busy {
164-
spListDual(false)
165+
updateSerialPortList()
165166
}
166167
time.Sleep(2 * time.Second)
167168
}
168169
}()
169170
go func() {
170171
for {
171-
spListDual(true)
172+
updateNetworkPortList()
172173
time.Sleep(2 * time.Second)
173174
}
174175
}()
175176
}
176177

177-
func spListDual(network bool) {
178-
179-
// call our os specific implementation of getting the serial list
180-
list, err := GetList(network)
181-
182-
//log.Println(list)
183-
//log.Println(err)
184-
178+
func updateSerialPortList() {
179+
ports, err := enumerateSerialPorts()
185180
if err != nil {
186-
// avoid reporting dummy data if an error occurred
181+
// REPORT
182+
fmt.Println("GET SERIAL LIST ERROR:", err)
187183
return
188184
}
185+
list := spListDual(ports)
186+
serialPorts.Mu.Lock()
187+
serialPorts.Ports = list
188+
serialPorts.Mu.Unlock()
189+
}
189190

190-
// do a quick loop to see if any of our open ports
191-
// did not end up in the list port list. this can
192-
// happen on windows in a fallback scenario where an
193-
// open port can't be identified because it is locked,
194-
// so just solve that by manually inserting
195-
// if network {
196-
// for port := range sh.ports {
197-
198-
// isFound := false
199-
// for _, item := range list {
200-
// if strings.ToLower(port.portConf.Name) == strings.ToLower(item.Name) {
201-
// isFound = true
202-
// }
203-
// }
204-
205-
// if !isFound {
206-
// // artificially push to front of port list
207-
// 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))
208-
// var ossp OsSerialPort
209-
// ossp.Name = port.portConf.Name
210-
// ossp.FriendlyName = port.portConf.Name
211-
// list = append([]OsSerialPort{ossp}, list...)
212-
// }
213-
// }
214-
// }
191+
func updateNetworkPortList() {
192+
ports, err := enumerateNetworkPorts()
193+
if err != nil {
194+
// REPORT
195+
fmt.Println("GET NETWORK LIST ERROR:", err)
196+
return
197+
}
198+
list := spListDual(ports)
199+
networkPorts.Mu.Lock()
200+
networkPorts.Ports = list
201+
networkPorts.Mu.Unlock()
202+
}
215203

204+
func spListDual(list []OsSerialPort) []SpPortItem {
216205
// we have a full clean list of ports now. iterate thru them
217206
// to append the open/close state, baud rates, etc to make
218207
// a super clean nice list to send back to browser
219-
n := len(list)
220-
spl := make([]SpPortItem, n)
221-
222-
ctr := 0
208+
spl := []SpPortItem{}
223209

224210
for _, item := range list {
225-
226-
spl[ctr] = SpPortItem{
211+
port := SpPortItem{
227212
Name: item.Name,
228213
SerialNumber: item.ISerial,
229214
DeviceClass: item.DeviceClass,
@@ -238,26 +223,15 @@ func spListDual(network bool) {
238223
}
239224

240225
// figure out if port is open
241-
myport, isFound := findPortByName(item.Name)
242-
243-
if isFound {
244-
// we found our port
245-
spl[ctr].IsOpen = true
246-
spl[ctr].Baud = myport.portConf.Baud
247-
spl[ctr].BufferAlgorithm = myport.BufferType
226+
if myport, isFound := findPortByName(item.Name); isFound {
227+
// and update data with the open port parameters
228+
port.IsOpen = true
229+
port.Baud = myport.portConf.Baud
230+
port.BufferAlgorithm = myport.BufferType
248231
}
249-
ctr++
250-
}
251-
252-
if network {
253-
NetworkPorts.Mu.Lock()
254-
NetworkPorts.Ports = spl
255-
NetworkPorts.Mu.Unlock()
256-
} else {
257-
SerialPorts.Mu.Lock()
258-
SerialPorts.Ports = spl
259-
SerialPorts.Mu.Unlock()
232+
spl = append(spl, port)
260233
}
234+
return spl
261235
}
262236

263237
func spErr(err string) {

seriallist.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ type OsSerialPort struct {
3838
NetworkPort bool
3939
}
4040

41-
// GetList will return the OS serial port
42-
func GetList(network bool) ([]OsSerialPort, error) {
43-
44-
if network {
45-
netportList, err := GetNetworkList()
46-
return netportList, err
47-
}
48-
41+
// enumerateSerialPorts will return the OS serial port
42+
func enumerateSerialPorts() ([]OsSerialPort, error) {
4943
// will timeout in 2 seconds
5044
arrPorts := []OsSerialPort{}
5145
ports, err := enumerator.GetDetailedPortsList()

serialport.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (p *serport) writerNoBuf() {
232232
log.Println(msgstr)
233233
h.broadcastSys <- []byte(msgstr)
234234
p.portIo.Close()
235-
spListDual(false)
235+
updateSerialPortList()
236236
spList(false)
237237
}
238238

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

324-
spListDual(false)
324+
updateSerialPortList()
325325
spList(false)
326326

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

334334
p.reader(buftype)
335335

336-
spListDual(false)
336+
updateSerialPortList()
337337
spList(false)
338338
}
339339

@@ -346,6 +346,6 @@ func spHandlerClose(p *serport) {
346346
func spCloseReal(p *serport) {
347347
p.bufferwatcher.Close()
348348
p.portIo.Close()
349-
spListDual(false)
349+
updateSerialPortList()
350350
spList(false)
351351
}

0 commit comments

Comments
 (0)