-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
338 lines (301 loc) · 8.91 KB
/
manager.go
File metadata and controls
338 lines (301 loc) · 8.91 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package nervo
import (
"bytes"
"errors"
"log"
"time"
)
type controllerInfo struct {
name string
portName string
}
type readOutputMessage struct {
portName string
answerChan chan string
}
type flashAnswer struct {
Error error
Output string
}
type flashMessage struct {
portName string
hexFileContent []byte
answerChan chan flashAnswer
}
type readContinuousMessage struct {
portName string
answerChan chan chan []byte
}
type stopReadingMessage struct {
portName string
}
type nameControllerMessage struct {
portName string
name string
}
type pingMessage struct {
pongChan chan struct{}
}
type writeToControllerMessage struct {
portName string
message []byte
doneChan chan error
}
type writeToControllerContinuouslyAnswerMessage struct {
stopChan chan closeContiniousWriterMessage
doneChan chan error
err error
}
type writeToControllerContinuouslyMessage struct {
portName string
writeChan chan []byte
answerChan chan writeToControllerContinuouslyAnswerMessage
}
// Manager controls all interactions with the controllers from outside
type Manager struct {
VerbMessageHandler func(verb, message string)
controllers []*controller
currentPortsChan chan []string
readOutputChan chan readOutputMessage
flashChan chan flashMessage
readContinuousChan chan readContinuousMessage
stopReadingChan chan stopReadingMessage
nameControllerChan chan nameControllerMessage
pingChan chan pingMessage
writeToControllerChan chan writeToControllerMessage
writeToControllerContinuouslyChan chan writeToControllerContinuouslyMessage
}
// NewManager retuns a Manager that is ready for use
func NewManager() *Manager {
m := &Manager{
currentPortsChan: make(chan []string),
readOutputChan: make(chan readOutputMessage),
flashChan: make(chan flashMessage),
readContinuousChan: make(chan readContinuousMessage),
stopReadingChan: make(chan stopReadingMessage),
nameControllerChan: make(chan nameControllerMessage),
pingChan: make(chan pingMessage),
writeToControllerChan: make(chan writeToControllerMessage),
writeToControllerContinuouslyChan: make(chan writeToControllerContinuouslyMessage),
}
go m.lookForNewPorts()
go m.manageControllers()
go watchManagerHealth(m, func() {
panic("I don't know, just kill him I guess")
})
return m
}
func (m *Manager) manageControllers() {
for {
select {
case currentPorts := <-m.currentPortsChan:
m.handleCurrentPorts(currentPorts)
break
case message := <-m.readOutputChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
controller.useOutput(func(buffer *bytes.Buffer) {
outputBuffer := make([]byte, buffer.Len())
_, err := buffer.Read(outputBuffer)
if err != nil {
panic(err)
}
message.answerChan <- string(outputBuffer)
})
} else {
message.answerChan <- "no controller found at " + message.portName
}
break
case message := <-m.flashChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
output, err := controller.flash(message.hexFileContent)
message.answerChan <- flashAnswer{Error: err, Output: output}
} else {
message.answerChan <- flashAnswer{Error: errors.New("no controller found at " + message.portName)}
}
break
case message := <-m.readContinuousChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
notifierChan := controller.notifyOnRead()
message.answerChan <- notifierChan
} else {
message.answerChan <- nil
}
break
case message := <-m.stopReadingChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
controller.clearNotifier()
}
break
case message := <-m.nameControllerChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
controller.Name = message.name
}
break
case message := <-m.writeToControllerChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
message.doneChan <- controller.write(message.message)
} else {
message.doneChan <- errors.New("no controller found at " + message.portName)
}
break
case message := <-m.writeToControllerContinuouslyChan:
controller := m.controllerForPort(message.portName)
if controller != nil {
stopChan, doneChan := controller.continiouslyWrite(message.writeChan)
message.answerChan <- writeToControllerContinuouslyAnswerMessage{
stopChan: stopChan,
doneChan: doneChan,
}
} else {
message.answerChan <- writeToControllerContinuouslyAnswerMessage{
err: errors.New("no controller found at " + message.portName),
}
}
break
case m := <-m.pingChan:
m.pongChan <- struct{}{}
break
}
}
}
func (m *Manager) listControllers() []controllerInfo {
infos := []controllerInfo{}
for _, controller := range m.controllers {
infos = append(infos, controllerInfo{portName: controller.SerialPortPath, name: controller.Name})
}
return infos
}
func (m *Manager) readFromController(portName string) string {
answerChan := make(chan string)
message := readOutputMessage{answerChan: answerChan, portName: portName}
m.readOutputChan <- message
return <-answerChan
}
func (m *Manager) flashController(portName string, hexFileContent []byte) flashAnswer {
answerChan := make(chan flashAnswer)
message := flashMessage{answerChan: answerChan, portName: portName, hexFileContent: hexFileContent}
m.flashChan <- message
return <-answerChan
}
func (m *Manager) readContinuouslyFromController(portName string) chan []byte {
answerChan := make(chan chan []byte)
message := readContinuousMessage{answerChan: answerChan, portName: portName}
m.readContinuousChan <- message
return <-answerChan
}
func (m *Manager) stopReadingFromController(portName string) {
message := stopReadingMessage{portName: portName}
m.stopReadingChan <- message
}
func (m *Manager) setControllerName(portName string, name string) {
message := nameControllerMessage{portName: portName, name: name}
m.nameControllerChan <- message
}
func (m *Manager) writeToController(controllerPortName string, message []byte) error {
doneChan := make(chan error)
m.writeToControllerChan <- writeToControllerMessage{
portName: controllerPortName,
message: message,
doneChan: doneChan,
}
return <-doneChan
}
func (m *Manager) writeToControllerContinuously(controllerPortName string, writeChan chan []byte) writeToControllerContinuouslyAnswerMessage {
answerChan := make(chan writeToControllerContinuouslyAnswerMessage)
m.writeToControllerContinuouslyChan <- writeToControllerContinuouslyMessage{
portName: controllerPortName,
writeChan: writeChan,
answerChan: answerChan,
}
return <-answerChan
}
func (m *Manager) controllerForPort(portName string) *controller {
for _, controller := range m.controllers {
if controller.SerialPortPath == portName {
return controller
}
}
return nil
}
func (m *Manager) lookForNewPorts() {
t := time.NewTicker(time.Second)
for {
<-t.C
ports, err := discoverAttachedControllers()
if err != nil {
panic(err)
}
m.currentPortsChan <- ports
}
}
func (m *Manager) pingWithTimeout(timeout time.Duration) error {
return withTimeOut(timeout, func() {
pongChan := make(chan struct{})
m.pingChan <- pingMessage{
pongChan: pongChan,
}
<-pongChan
})
}
func (m *Manager) handleCurrentPorts(currentPorts []string) {
newPorts := []string{}
for _, port := range currentPorts {
isIncluded := false
for _, controller := range m.controllers {
if port == controller.SerialPortPath {
isIncluded = true
}
}
if !isIncluded {
newPorts = append(newPorts, port)
}
}
for _, newPort := range newPorts {
log.Println("discovered new port: ", newPort)
controller := newController(newPort)
controller.handleVerbMessage = m.VerbMessageHandler
go func() {
defer func() {
if r := recover(); r != nil {
log.Println(r)
}
}()
controller.readFromSerial()
}()
m.controllers = append(m.controllers, controller)
}
removedControllers := []*controller{}
for _, controller := range m.controllers {
isIncluded := false
for _, port := range currentPorts {
if controller.SerialPortPath == port {
isIncluded = true
}
}
if !isIncluded {
removedControllers = append(removedControllers, controller)
}
}
for _, removed := range removedControllers {
removed.closeSerial()
}
currentControllers := []*controller{}
for _, controller := range m.controllers {
isRemoved := false
for _, removed := range removedControllers {
if controller == removed {
isRemoved = true
}
}
if !isRemoved {
currentControllers = append(currentControllers, controller)
}
}
m.controllers = currentControllers
}