forked from arduino/arduino-create-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.go
executable file
·293 lines (249 loc) · 7.13 KB
/
serial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Supports Windows, Linux, Mac, BeagleBone Black, and Raspberry Pi
package main
import (
"encoding/json"
"strconv"
"strings"
"time"
"github.com/arduino/arduino-create-agent/upload"
)
type writeRequest struct {
p *serport
d string
buffer bool
id string
}
type serialhub struct {
// Opened serial ports.
ports map[*serport]bool
//write chan *serport, chan []byte
write chan writeRequest
// Register requests from the connections.
register chan *serport
// Unregister requests from connections.
unregister chan *serport
}
type SpPortList struct {
Ports []SpPortItem
Network bool
}
type SpPortItem struct {
Name string
SerialNumber string
DeviceClass string
IsOpen bool
IsPrimary bool
Baud int
BufferAlgorithm string
Ver string
NetworkPort bool
VendorID string
ProductID string
}
// SerialPorts contains the ports attached to the machine
var SerialPorts SpPortList
// NetworkPorts contains the ports on the network
var NetworkPorts SpPortList
var sh = serialhub{
//write: make(chan *serport, chan []byte),
write: make(chan writeRequest),
register: make(chan *serport),
unregister: make(chan *serport),
ports: make(map[*serport]bool),
}
func (sh *serialhub) run() {
//log.Print("Inside run of serialhub")
//cmdIdCtr := 0
for {
select {
case p := <-sh.register:
//log.Print("Registering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + ",\"BufferType\":\"" + p.BufferType + "\"}")
sh.ports[p] = true
case p := <-sh.unregister:
//log.Print("Unregistering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
delete(sh.ports, p)
close(p.sendBuffered)
close(p.sendNoBuf)
case wr := <-sh.write:
// if user sent in the commands as one text mode line
write(wr, "")
}
}
}
func write(wr writeRequest, id string) {
if wr.buffer {
//log.Println("Send was normal send, so sending to wr.p.sendBuffered")
wr.p.sendBuffered <- wr.d
} else {
//log.Println("Send was sendnobuf, so sending to wr.p.sendNoBuf")
wr.p.sendNoBuf <- wr.d
}
}
// spList broadcasts a Json representation of the ports found
func spList(network bool) {
var list SpPortList
if network {
list = NetworkPorts
} else {
list = SerialPorts
}
ls, err := json.MarshalIndent(list, "", "\t")
if err != nil {
//log.Println(err)
h.broadcastSys <- []byte("Error creating json on port list " +
err.Error())
} else {
h.broadcastSys <- ls
}
}
// discoverLoop periodically update the list of ports found
func discoverLoop() {
SerialPorts.Network = false
SerialPorts.Ports = make([]SpPortItem, 0)
NetworkPorts.Network = true
NetworkPorts.Ports = make([]SpPortItem, 0)
go func() {
for {
if !upload.Busy {
spListDual(false)
}
time.Sleep(2 * time.Second)
}
}()
go func() {
for {
spListDual(true)
time.Sleep(2 * time.Second)
}
}()
}
func spListDual(network bool) {
// call our os specific implementation of getting the serial list
list, err := GetList(network)
//log.Println(list)
//log.Println(err)
if err != nil {
// avoid reporting dummy data if an error occurred
return
}
// do a quick loop to see if any of our open ports
// did not end up in the list port list. this can
// happen on windows in a fallback scenario where an
// open port can't be identified because it is locked,
// so just solve that by manually inserting
// if network {
// for port := range sh.ports {
// isFound := false
// for _, item := range list {
// if strings.ToLower(port.portConf.Name) == strings.ToLower(item.Name) {
// isFound = true
// }
// }
// if !isFound {
// // artificially push to front of port list
// 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))
// var ossp OsSerialPort
// ossp.Name = port.portConf.Name
// ossp.FriendlyName = port.portConf.Name
// list = append([]OsSerialPort{ossp}, list...)
// }
// }
// }
// we have a full clean list of ports now. iterate thru them
// to append the open/close state, baud rates, etc to make
// a super clean nice list to send back to browser
n := len(list)
spl := SpPortList{make([]SpPortItem, n, n), network}
ctr := 0
for _, item := range list {
spl.Ports[ctr] = SpPortItem{
Name: item.Name,
SerialNumber: item.SerialNumber,
DeviceClass: item.DeviceClass,
IsOpen: false,
IsPrimary: false,
Baud: 0,
BufferAlgorithm: "",
Ver: version,
NetworkPort: item.NetworkPort,
VendorID: item.IdVendor,
ProductID: item.IdProduct,
}
// figure out if port is open
myport, isFound := findPortByName(item.Name)
if isFound {
// we found our port
spl.Ports[ctr].IsOpen = true
spl.Ports[ctr].Baud = myport.portConf.Baud
spl.Ports[ctr].BufferAlgorithm = myport.BufferType
}
ctr++
}
if network {
NetworkPorts = spl
} else {
SerialPorts = spl
}
}
func spErr(err string) {
//log.Println("Sending err back: ", err)
//h.broadcastSys <- []byte(err)
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
}
func spClose(portname string) {
// look up the registered port by name
// then call the close method inside serialport
// that should cause an unregister channel call back
// to myself
myport, isFound := findPortByName(portname)
if isFound {
// we found our port
spHandlerClose(myport)
} else {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to close.")
}
}
func spWrite(arg string) {
// we will get a string of comXX asdf asdf asdf
//log.Println("Inside spWrite arg: " + arg)
arg = strings.TrimPrefix(arg, " ")
//log.Println("arg after trim: " + arg)
args := strings.SplitN(arg, " ", 3)
if len(args) != 3 {
errstr := "Could not parse send command: " + arg
//log.Println(errstr)
spErr(errstr)
return
}
portname := strings.Trim(args[1], " ")
//log.Println("The port to write to is:" + portname + "---")
//log.Println("The data is:" + args[2] + "---")
// see if we have this port open
myport, isFound := findPortByName(portname)
if !isFound {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
return
}
// we found our port
// create our write request
var wr writeRequest
wr.p = myport
// see if args[0] is send or sendnobuf
if args[0] != "sendnobuf" {
// we were just given a "send" so buffer it
wr.buffer = true
} else {
//log.Println("sendnobuf specified so wr.buffer is false")
wr.buffer = false
}
// include newline or not in the write? that is the question.
// for now lets skip the newline
//wr.d = []byte(args[2] + "\n")
wr.d = args[2] //[]byte(args[2])
// send it to the write channel
sh.write <- wr
}