-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcm.go
418 lines (357 loc) · 10.7 KB
/
lcm.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
Package lcm implements the serial communication protocol for the ASUSTOR
LCD display. This includes controlling and updating and listening for
button presses.
LCM data format:
MESSAGE_TYPE DATA_LENGTH FUNCTION [[DATA]...] [CRC]
*/
package lcm
import (
"bufio"
"context"
"errors"
"fmt"
"time"
"github.com/pkg/term"
)
const (
// DefaultReplyTimeout defines how long we wait for a reply,
// usually one is received in under 10ms. We keep this timeout
// fairly tight because a longer delay rarely helps.
//
// The ASUSTOR daemon resends messages after 100ms if no
// response is received. But even this can leads to deadlocks
// where the same error will be echoed back time and time again.
DefaultReplyTimeout = 15 * time.Millisecond
// DefaultRetryLimit defines how many times a command will be
// retried until giving up. Given the default reply timeout,
// this could lead to nothing happening on the screen for about
// 750ms.
//
// ASUSTOR tries up to 100 times, however, this rarely helps
// clear up the communication error.
DefaultRetryLimit = 50
// DefaultWriteDelay defines how long to wait before writing the
// next message. This is used both when writing commands and
// responding to commands from the display.
//
// The ASUSTOR lcmd binary uses 15ms and 45ms sleeps between
// certain commands, but this seems excessive.
DefaultWriteDelay = 250 * time.Microsecond
// forceFlushDelay specifies how long to wait after attempting
// to flush the MCU receive buffer.
forceFlushDelay = 250 * time.Microsecond
)
// DefaultTTY represents the default serial tty for LCM.
const DefaultTTY = "/dev/ttyS1"
// LCM represents the ASUSTOR Liquid Crystal Monitor.
type LCM struct {
ctx context.Context
cancel context.CancelFunc
done chan struct{}
s *term.Term
writeC chan sendMessage
rawReadC chan Message
readC chan []byte
opts openOptions
}
type openOptions struct {
ack bool
l Logger
}
// OpenOption configures LCM during open.
type OpenOption func(*openOptions)
// EnableProtocolAckReply specifies if LCM should send acknowledgement
// replies to the screen when it sends us a command (e.g. button press
// or firmware version).
//
// If the screen sent us a command, technically we should acknowledge it
// by sending a reply indicating it was successful. However, it often
// causes later commands (from us) to become corrupt. The frequency of
// the corruption can be lowered with delays, but then again, it seems
// like the display does not care if we reply or not.
func EnableProtocolAckReply() OpenOption {
return func(o *openOptions) {
o.ack = true
}
}
// Logger represents a generic logger (e.g. from the log package).
type Logger interface {
Printf(format string, v ...interface{})
}
type noopLogger struct{}
func (noopLogger) Printf(format string, v ...interface{}) {}
// WithLogger sets the logger used by LCM (default none).
func WithLogger(l Logger) OpenOption {
return func(o *openOptions) {
o.l = l
}
}
// Open opens the serial port for LCM.
func Open(tty string, opt ...OpenOption) (*LCM, error) {
opts := openOptions{
l: noopLogger{},
}
for _, o := range opt {
o(&opts)
}
s, err := term.Open(tty, term.Speed(115200), term.RawMode)
if err != nil {
return nil, err
}
err = s.Flush()
if err != nil {
s.Close()
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
m := &LCM{
ctx: ctx,
cancel: cancel,
done: make(chan struct{}),
s: s,
writeC: make(chan sendMessage, 2),
rawReadC: make(chan Message, 2),
readC: make(chan []byte, 5),
opts: opts,
}
go m.read()
go m.handle()
return m, nil
}
type sendMessage struct {
err chan error
data Message
retryLimit int
replyTimeout time.Duration
writeDelay time.Duration
}
// forceFlushMCU sends a nonsense command in an attempt to flush the MCU
// receive buffer. Sometimes when the MCU gets stuck the only way to
// escape the loop is to send another command, retrying the previous
// command will keep failing in perpetuity.
//
// The fflush (0x00) command seems to have no side-effect, but funnily
// enough, the MCU will reply that the command was successful. Perhaps
// it is a real command but it's not used anywhere else.
//
// Also, sending two of these commands in one go seems to increase the
// speed of recovery further.
//
// Other attemps included sending enough zero bytes to clear the receive
// buffer, but while effective, not foolproof (a good number of bytes
// was 32 or 33) but still unrecoverable states were observed.
func (m *LCM) forceFlushMCU() {
m.opts.l.Printf("LCM.forceFlushMCU: trying to flush MCU read buffer...")
data := make([]byte, len(flushMCUBuffer), len(flushMCUBuffer)+1*2)
copy(data, flushMCUBuffer)
sum := checksum(data)
data = append(data, sum)
data = append(data, data...)
_, _ = m.s.Write(data)
// Small delay to allow the MCU to process the message.
time.Sleep(forceFlushDelay)
}
// Send messages to the display. Note that checksum should be omitted,
// it is handled transparently as part of the protocol implementation.
//
// TODO(mafredri): Add support for functional arguments:
//
// m.Send(msg, lcm.WithRetryLimit(100), lcm.WithReplyTimeout(5 * time.Millisecond))
func (m *LCM) Send(msg Message) error {
err := msg.Check()
if err != nil {
return err
}
data := make([]byte, len(msg), len(msg)+1)
copy(data, msg)
data = append(data, checksum(data))
sm := sendMessage{
err: make(chan error, 1),
data: data,
retryLimit: DefaultRetryLimit,
replyTimeout: DefaultReplyTimeout,
writeDelay: DefaultWriteDelay,
}
m.writeC <- sm
return <-sm.err
}
// Recv messages sent from the display.
func (m *LCM) Recv() Message {
return <-m.readC
}
// read the serial port and transmit
// messages on the read channel.
func (m *LCM) read() {
var parseErr parsingError
// No need for a large buffer, the most common message length is 5.
r := bufio.NewReaderSize(m.s, 16)
raw := &recvMessage{}
for {
raw.Reset()
err := copyBytes(raw, r)
if err != nil {
if errors.As(err, &parseErr) {
m.opts.l.Printf("LCM.read: %v", err)
continue
}
// TODO(mafredri): Close LCM.
m.opts.l.Printf("LCM.read: fatal: %v", err)
return
}
b := Message(raw.Bytes())
m.opts.l.Printf("LCM.read: OK %#x", b)
m.rawReadC <- b
}
}
// write to the serial port.
func (m *LCM) write(data []byte) error {
n, err := m.s.Write(data)
m.opts.l.Printf("LCM.write: wrote: %#x %d, err: %v", data, n, err)
if err != nil {
return err
}
return nil
}
// handle incoming and outgoing messages.
func (m *LCM) handle() {
defer close(m.done)
var id int64
var retry func()
var handleReply func(Message) bool
var replyTimeout <-chan time.Time
for {
var read Message
// Prioritize processing all messages from the LCM before
// sending commands. The replyTimeout also serves as a
// guard against concurrent writes.
if len(m.rawReadC) > 0 || replyTimeout != nil {
select {
case read = <-m.rawReadC:
case <-replyTimeout:
m.opts.l.Printf("LCM.handle: write(%d): timeout, retry...", id)
m.forceFlushMCU()
retry()
case <-m.ctx.Done():
return
}
} else {
select {
case read = <-m.rawReadC:
// Handle writes, each write must complete (or fail)
// before the next one is handled.
case w := <-m.writeC:
id++
m.opts.l.Printf("LCM.handle: write(%d): %#x", id, w.data)
// Define reply function for verifying
// that the command was successful.
handleReply = func(reply Message) bool {
if reply.Type() == Reply && reply.Function() == w.data.Function() {
if reply.Ok() {
m.opts.l.Printf("LCM.handle: write(%d): reply OK", id)
close(w.err)
handleReply = nil
retry = nil
replyTimeout = nil
} else {
// We don't always forceibly flush the MCU here because it had
// the sensibility to at least respond to our command.
m.opts.l.Printf("LCM.handle: write(%d): reply ERROR (%#x)", id, reply.Value())
}
return true
}
return false
}
tries := 0
var wErr error
retry = func() {
if tries > w.retryLimit {
// We gave it a try, not much more we can do...
// Caller could try power-cycling the display.
if wErr != nil {
w.err <- fmt.Errorf("retry limit exceeded: %d/%d: last write error: %w", tries-1, w.retryLimit, wErr)
} else {
w.err <- fmt.Errorf("retry limit exceeded: %d/%d", tries-1, w.retryLimit)
}
handleReply = nil
retry = nil
replyTimeout = nil
return
}
// Add a small delay before each write to
// ensure the serial port is not spammed.
time.Sleep(w.writeDelay)
tries++
err := m.write(w.data)
if err != nil {
m.opts.l.Printf("LCM.handle: write(%d): %#x: %v", id, w.data, err)
wErr = err
}
replyTimeout = time.After(w.replyTimeout)
}
retry() // Initiate first try.
case <-m.ctx.Done():
return
}
}
if len(read) == 0 || (handleReply != nil && handleReply(read)) {
continue
}
switch read.Type() {
case Command:
m.opts.l.Printf("LCM.handle: read(Command): %#x", read.Function())
reply := read.ReplyOk()
reply = append(reply, checksum(reply))
if m.opts.ack {
// A delay is necessary because otherwise the
// serial communication protcol is guaranteed
// to become corrupt. What usually works quite
// well is a delay somewhere between 150us and
// 5ms. Any longer than that and it seems the
// display forgets it's waiting for one.
//
// It would be possible to reply with more
// precise control of the delay in (*LCM).read,
// however, in practice this gives no benefit.
time.Sleep(DefaultWriteDelay)
err := m.write(reply)
m.opts.l.Printf("LCM.handle: read(Command): sent ack reply %#x, err: %v", reply, err)
} else {
m.opts.l.Printf("LCM.handle: read(Command): protocol ack disabled, not sending reply %#x", reply.Value())
}
case Reply:
if read.Function() == fflush {
m.opts.l.Printf("LCM.handle: read(Reply): received ack for flush: %#x", read)
} else {
m.opts.l.Printf("LCM.handle: read(Reply): unhandled reply (%#x): %#x", read.Function(), read)
}
default:
m.opts.l.Printf("LCM.handle: read(Unknown): %#x", read)
}
read = read[:len(read)-1] // Discard checksum.
m.opts.l.Printf("LCM.handle: read: forwarding message: %#x", read)
select {
case m.readC <- read:
default:
select {
case <-m.readC:
m.opts.l.Printf("LCM.handle: read: buffer full, discarded earliest message")
default:
// Buffer got depleted.
}
m.readC <- read
}
}
}
// Close the serial connection.
func (m *LCM) Close() error {
m.cancel()
<-m.done
return m.s.Close()
}
func checksum(b []byte) (s byte) {
for _, bb := range b {
s += bb
}
return s
}