-
Notifications
You must be signed in to change notification settings - Fork 1
/
serport.go
295 lines (260 loc) · 5.46 KB
/
serport.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
package serport
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
)
type Port interface {
SetBaudrate(int) error
SetParity(byte) error // odd: 'o', even: 'e', otherwise none
SetWordlen(int) error // 5, 6, 7, or 8
SetStopbits(int) error // 1 or 2
SetDtr(bool) error
SetRts(bool) error
SetRtsCts(bool) error // obey Cts signal, set Rts depending of internal buffer's state
SetLowLatency(bool) error
SendBreak(time.Duration) error
Delay(time.Duration)
// If the Port is remote, after calling Record() the execution of
// commands will be delayed until Commit() is called.
Record()
Commit()
Drain() error
Purge(in, out bool)
Device
}
type Device interface {
Ctl(cmds ...string) error // accepts commands similar to Plan 9's eia#ctl
io.ReadWriteCloser
}
type dev struct {
name string
inCtl bool
rts, dtr bool
hw
encaps Port
}
const (
// StdConf specifies a port configuration that may be used
// as inictl argument to [Open].
StdConf = "b115200 l8 pn s1"
)
// MergeCtlCmds concatenates control cmd strings, skipping commands
// from preceding strings if they occur, possibly with different arguments,
// in subsequent strings too.
func MergeCtlCmds(ctlCmds ...string) string {
switch len(ctlCmds) {
case 0:
return ""
case 1:
return mergeCtlCmds(StdConf, ctlCmds[0])
}
cmds := ctlCmds[0]
for _, cmds2 := range ctlCmds[1:] {
cmds = mergeCtlCmds(cmds, cmds2)
}
return cmds
}
func mergeCtlCmds(cmds1, cmds2 string) string {
fi := strings.Fields(cmds1)
f := strings.Fields(cmds2)
r := make([]string, 0, len(fi))
for _, ci := range fi {
if ci == "" {
continue
}
for i, c := range f {
if c == "" {
continue
}
if c[0] == 'D' || c[0] == 'W' {
break
}
if c[0] == ci[0] {
ci = c
f[i] = ""
}
}
r = append(r, ci)
}
fOrig, f := f, f[:0]
for _, c := range fOrig {
if c == "" {
continue
}
f = append(f, c)
}
r = append(r, f...)
return strings.Join(r, " ")
}
var ctlNamespaceMap = map[string]*ctlNamespace{
"std": stdNamespace,
}
type ctlNamespace struct {
runCmd ctlRunFunc
updateDrv func(*dev) error
charCmds string
}
type ctlRunFunc func(d *dev, cmd, c byte, n int) error
func (d *dev) Ctl(cmds ...string) error {
var err error
defer func() {
d.inCtl = false
}()
subNsID := ""
nsStd := ctlNamespaceMap["std"]
ns := nsStd
for _, s := range cmds {
for _, f := range strings.Fields(s) {
var n int
var c byte
d.inCtl = true
cmd := f[0]
if len(f) > 1 {
arg := f[1:]
if cmd == '.' {
if arg[0] == '.' {
if subNsID == "" {
return d.errorf("ctl", "found \"..\", but sub-namespace not active")
}
arg = arg[1:]
} else if iDot := strings.IndexByte(arg, '.'); iDot == -1 {
return d.errorf("ctl", "missing dot after namespace id")
} else {
id := arg[:iDot]
arg = arg[iDot+1:]
if subNsID != id {
if subNsID != "" {
err = ns.updateDrv(d)
if err != nil {
return err
}
}
nsNew, ok := ctlNamespaceMap[id]
if !ok {
return d.errorf("ctl", "namespace not implemented: %q", id)
}
ns = nsNew
subNsID = id
}
}
if len(arg) == 0 {
return d.errorf("ctl", "command missing, found: %q", f)
}
cmd = arg[0]
arg = arg[1:]
} else if subNsID != "" {
err = ns.updateDrv(d)
if err != nil {
return err
}
subNsID = ""
ns = nsStd
}
if strings.IndexByte(ns.charCmds, cmd) == -1 {
n, err = strconv.Atoi(arg)
if err != nil {
return d.error("ctl", err)
}
} else {
c = arg[0]
}
}
err := ns.runCmd(d, cmd, c, n)
if err != nil {
return err
}
}
}
if subNsID != "" {
err = ns.updateDrv(d)
if err != nil {
return err
}
}
if !d.inCtl {
return nil
}
d.inCtl = false
return d.updateCtl()
}
func (d *dev) updateCtlNow() error {
saved := d.inCtl
d.inCtl = false
defer func() { d.inCtl = saved }()
return d.updateCtl()
}
var stdNamespace = &ctlNamespace{
runCmd: func(d *dev, cmd, c byte, n int) error {
p := d.encaps
switch cmd {
case 'd':
return p.SetDtr(n == 1)
case 'r':
return p.SetRts(n == 1)
case 'm':
return p.SetRtsCts(n != 0)
case 'D':
err := d.updateCtlNow()
if err != nil {
return err
}
p.Delay(time.Duration(n) * time.Millisecond)
return nil
case 'W':
err := d.updateCtlNow()
if err != nil {
return err
}
_, err = p.Write([]byte{byte(n)})
return err
case 'b':
return p.SetBaudrate(n)
case 'l':
return p.SetWordlen(n)
case 'k':
return p.SendBreak(time.Duration(n) * time.Millisecond)
case 'p':
return p.SetParity(c)
case 's':
return p.SetStopbits(n)
case 'L':
return p.SetLowLatency(n != 0)
default:
return d.errorf("ctl", "unknown command: %q", string(cmd))
}
},
charCmds: "p",
}
func (d *dev) Delay(duration time.Duration) {
d.Drain()
time.Sleep(duration)
}
func (d *dev) Record() {
}
func (d *dev) Commit() {
}
func (d *dev) error(action string, err error) error {
return pathError(action, d.name, err)
}
func (d *dev) errorf(action string, format string, args ...interface{}) error {
err := fmt.Errorf(format, args...)
return pathError(action, d.name, err)
}
func pathError(op, path string, err error) error {
return &os.PathError{Op: op, Path: path, Err: err}
}
type LineState struct {
Csr bool
Dsr bool
Ring bool
Dcd bool
}
func SetEncapsulatingPort(dest, p Port) {
if dev, ok := dest.(*dev); ok {
dev.encaps = p
}
}