@@ -6,9 +6,11 @@ import (
6
6
"encoding/json"
7
7
"strconv"
8
8
"strings"
9
+ "sync"
9
10
"time"
10
11
11
12
"github.com/arduino/arduino-create-agent/upload"
13
+ log "github.com/sirupsen/logrus"
12
14
)
13
15
14
16
type writeRequest struct {
@@ -34,6 +36,7 @@ type serialhub struct {
34
36
type SpPortList struct {
35
37
Ports []SpPortItem
36
38
Network bool
39
+ mu sync.Mutex `json:"-"`
37
40
}
38
41
39
42
type SpPortItem struct {
@@ -102,13 +105,17 @@ func write(wr writeRequest) {
102
105
103
106
// spList broadcasts a Json representation of the ports found
104
107
func spList (network bool ) {
105
- var list SpPortList
108
+ var ls []byte
109
+ var err error
106
110
if network {
107
- list = NetworkPorts
111
+ NetworkPorts .mu .Lock ()
112
+ ls , err = json .MarshalIndent (NetworkPorts , "" , "\t " )
113
+ NetworkPorts .mu .Unlock ()
108
114
} else {
109
- list = SerialPorts
115
+ SerialPorts .mu .Lock ()
116
+ ls , err = json .MarshalIndent (SerialPorts , "" , "\t " )
117
+ SerialPorts .mu .Unlock ()
110
118
}
111
- ls , err := json .MarshalIndent (list , "" , "\t " )
112
119
if err != nil {
113
120
//log.Println(err)
114
121
h .broadcastSys <- []byte ("Error creating json on port list " +
@@ -120,10 +127,14 @@ func spList(network bool) {
120
127
121
128
// discoverLoop periodically update the list of ports found
122
129
func discoverLoop () {
130
+ SerialPorts .mu .Lock ()
123
131
SerialPorts .Network = false
124
132
SerialPorts .Ports = make ([]SpPortItem , 0 )
133
+ SerialPorts .mu .Unlock ()
134
+ NetworkPorts .mu .Lock ()
125
135
NetworkPorts .Network = true
126
136
NetworkPorts .Ports = make ([]SpPortItem , 0 )
137
+ NetworkPorts .mu .Unlock ()
127
138
128
139
go func () {
129
140
for {
@@ -184,13 +195,13 @@ func spListDual(network bool) {
184
195
// to append the open/close state, baud rates, etc to make
185
196
// a super clean nice list to send back to browser
186
197
n := len (list )
187
- spl := SpPortList { make ([]SpPortItem , n , n ), network }
198
+ spl := make ([]SpPortItem , n , n )
188
199
189
200
ctr := 0
190
201
191
202
for _ , item := range list {
192
203
193
- spl . Ports [ctr ] = SpPortItem {
204
+ spl [ctr ] = SpPortItem {
194
205
Name : item .Name ,
195
206
SerialNumber : item .ISerial ,
196
207
DeviceClass : item .DeviceClass ,
@@ -209,17 +220,21 @@ func spListDual(network bool) {
209
220
210
221
if isFound {
211
222
// we found our port
212
- spl . Ports [ctr ].IsOpen = true
213
- spl . Ports [ctr ].Baud = myport .portConf .Baud
214
- spl . Ports [ctr ].BufferAlgorithm = myport .BufferType
223
+ spl [ctr ].IsOpen = true
224
+ spl [ctr ].Baud = myport .portConf .Baud
225
+ spl [ctr ].BufferAlgorithm = myport .BufferType
215
226
}
216
227
ctr ++
217
228
}
218
229
219
230
if network {
220
- NetworkPorts = spl
231
+ NetworkPorts .mu .Lock ()
232
+ NetworkPorts .Ports = spl
233
+ NetworkPorts .mu .Unlock ()
221
234
} else {
222
- SerialPorts = spl
235
+ SerialPorts .mu .Lock ()
236
+ SerialPorts .Ports = spl
237
+ SerialPorts .mu .Unlock ()
223
238
}
224
239
}
225
240
0 commit comments