forked from tarm/serial
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserial_posix.go
394 lines (343 loc) · 7.08 KB
/
serial_posix.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
// +build !windows
package serial
// #include <termios.h>
// #include <unistd.h>
import "C"
// fixme: Maybe change to using syscall package + ioctl instead of cgo
import (
"errors"
"fmt"
"math"
"os"
"sync"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
const maxReadTimeout = (25 * time.Second) + (500 * time.Millisecond)
type impl struct {
// We intentionally do not use an "embedded" struct so that we
// don't export File
mu sync.Mutex
c *Config
f *os.File
fd uintptr
st C.struct_termios
}
var _ Port = (*impl)(nil)
func openPort(c Config) (p Port, err error) {
f, err := os.OpenFile(c.Name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return
}
defer func() {
if err != nil {
_ = f.Close()
}
}()
pt := &impl{
c: &c,
f: f,
fd: f.Fd(),
}
if C.isatty(C.int(pt.fd)) != 1 {
err = errors.New("serial: file is not a tty")
return
}
if _, err = C.tcgetattr(C.int(pt.fd), &pt.st); err != nil {
return
}
var speed C.speed_t
switch c.Baud {
case 230400:
speed = C.B230400
case 115200:
speed = C.B115200
case 57600:
speed = C.B57600
case 38400:
speed = C.B38400
case 19200:
speed = C.B19200
case 9600:
speed = C.B9600
case 4800:
speed = C.B4800
case 2400:
speed = C.B2400
case 1200:
speed = C.B1200
case 600:
speed = C.B600
case 300:
speed = C.B300
case 200:
speed = C.B200
case 150:
speed = C.B150
case 134:
speed = C.B134
case 110:
speed = C.B110
case 75:
speed = C.B75
case 50:
speed = C.B50
default:
err = fmt.Errorf("serial: unknown baud rate %v", c.Baud)
return
}
// by some bizarre input and output speeds set by separate calls
if _, err = C.cfsetispeed(&pt.st, speed); err != nil {
return
}
if _, err = C.cfsetospeed(&pt.st, speed); err != nil {
return
}
// Turn off break interrupts, CR->NL, Parity checks, strip, and IXON
pt.st.c_iflag &= ^C.tcflag_t(C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXOFF | C.IXON | C.PARMRK)
// Select local mode, turn off parity, set to 8 bits
pt.st.c_cflag &= ^C.tcflag_t(C.CSIZE | C.PARENB)
pt.st.c_cflag |= C.CLOCAL | C.CREAD
// databits
switch c.Size {
case 5:
pt.st.c_cflag |= C.CS5
case 6:
pt.st.c_cflag |= C.CS6
case 7:
pt.st.c_cflag |= C.CS7
case 8:
pt.st.c_cflag |= C.CS8
default:
err = ErrBadSize
return
}
// Parity settings
switch c.Parity {
case ParityNone:
// default is no parity
case ParityOdd:
pt.st.c_cflag |= C.PARENB
pt.st.c_cflag |= C.PARODD
case ParityEven:
pt.st.c_cflag |= C.PARENB
pt.st.c_cflag &= ^C.tcflag_t(C.PARODD)
default:
err = ErrBadParity
return
}
// Stop bits settings
switch c.StopBits {
case Stop1:
// as is, default is 1 bit
case Stop2:
pt.st.c_cflag |= C.CSTOPB
default:
err = ErrBadStopBits
return
}
// Select raw mode
pt.st.c_lflag &= ^C.tcflag_t(C.ICANON | C.ECHO | C.ECHOE | C.ISIG)
pt.st.c_oflag &= ^C.tcflag_t(C.OPOST)
// Disable RTS/CTS hardware flow control
// pt.st.c_cflag &= ^C.tcflag_t(C.CRTSCTS)
if err = pt.Flush(); err != nil {
return
}
if err = pt.setTimeouts(1, 0); err != nil {
return
}
r1, _, e := syscall.Syscall(syscall.SYS_FCNTL,
pt.fd,
uintptr(syscall.F_SETFL),
uintptr(0))
if e != 0 || r1 != 0 {
err = fmt.Errorf("serial: clearing NONBLOCK syscall error: %s, %d", e, r1)
return
}
p = pt
return
}
func (p *impl) setTimeouts(vMin, vTime uint8) error {
p.st.c_cc[C.VMIN] = C.cc_t(vMin)
p.st.c_cc[C.VTIME] = C.cc_t(vTime)
if _, err := C.tcsetattr(C.int(p.fd), C.TCSANOW, &p.st); err != nil {
return err
}
return nil
}
func (p *impl) SetParity(val Parity) error {
p.mu.Lock()
defer p.mu.Unlock()
// Parity settings
switch val {
case ParityNone:
// default is no parity
case ParityOdd:
p.st.c_cflag |= C.PARENB
p.st.c_cflag |= C.PARODD
case ParityEven:
p.st.c_cflag |= C.PARENB
p.st.c_cflag &= ^C.tcflag_t(C.PARODD)
default:
return ErrBadParity
}
if _, err := C.tcsetattr(C.int(p.fd), C.TCSANOW, &p.st); err != nil {
return err
}
return nil
}
// SetReadDeadline
func (p *impl) SetReadDeadline(t time.Duration) error {
p.c.timeout = t
return nil
}
func (p *impl) Read(b []byte) (n int, err error) {
return p.f.Read(b)
}
// func (p *impl) Read(b []byte) (n int, err error) {
// remaining := len(b)
//
// if remaining == 0 {
// return 0, ErrInvalidArg
// }
//
// defer func() {
// if p.c.DumpRx != nil && n > 0 {
// p.c.DumpRx(b[:n])
// }
// }()
//
// remainingTimeout := p.c.timeout
//
// if remainingTimeout == 0 {
// if err = p.setTimeouts(0, 0); err != nil {
// return
// }
//
// return p.f.Read(b)
// }
//
// offset := 0
//
// for remaining > 0 {
// toRead := remaining
// if toRead > 255 {
// toRead = 255
// }
//
// vMin := uint8(toRead)
// vTime := uint8(0)
//
// if remainingTimeout < MaxTimeout {
// t := remainingTimeout
// if t >= maxReadTimeout {
// t = maxReadTimeout
// }
//
// remainingTimeout -= t
//
// vTime = posixTimeoutValue(t)
// }
//
// if err = p.setTimeouts(vMin, vTime); err != nil {
// return
// }
//
// var nRead int
// nRead, err = p.f.Read(b[offset:])
// n += nRead
// remaining -= nRead
// offset += nRead
// if err != nil {
// return
// }
// }
//
// return
// }
func (p *impl) Write(b []byte) (n int, err error) {
if p.c.DumpTx != nil {
p.c.DumpTx(b)
}
return p.f.Write(b)
}
// Discards data written to the port but not transmitted,
// or data received but not read
func (p *impl) Flush() error {
_, err := C.tcflush(C.int(p.f.Fd()), C.TCIOFLUSH)
return err
}
func (p *impl) Status() (n uint, err error) {
var status uint
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
p.fd,
uintptr(unix.TIOCMGET),
uintptr(unsafe.Pointer(&status)),
); errno != 0 {
return status, errno
} else {
return status, nil
}
}
func (p *impl) SetDTR(assert bool) (err error) {
req := unix.TIOCMBIS
if !assert {
req = unix.TIOCMBIC
}
var m uint = unix.TIOCM_DTR
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
p.fd,
uintptr(req),
uintptr(unsafe.Pointer(&m)),
); errno != 0 {
return errno
} else {
return nil
}
}
func (p *impl) SetRTS(assert bool) (err error) {
req := unix.TIOCMBIS
if !assert {
req = unix.TIOCMBIC
}
var m uint = unix.TIOCM_RTS
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
p.fd,
uintptr(req),
uintptr(unsafe.Pointer(&m)),
); errno != 0 {
return errno
} else {
return nil
}
}
func (p *impl) Close() (err error) {
return p.f.Close()
}
// Converts the timeout values for Linux / POSIX systems
/*
* http://man7.org/linux/man-pages/man3/termios.3.html
* - Supports blocking read and read with timeout operations
*/
func posixTimeoutValue(duration time.Duration) (vtime uint8) {
var readTimeoutInDeci int64
if duration > 0 {
// convert timeout to deciseconds as expected by VTIME
readTimeoutInDeci = duration.Nanoseconds() / 1e6 / 100
// capping the timeout
if readTimeoutInDeci < 1 {
// min possible timeout 1 deciseconds (0.1s)
readTimeoutInDeci = 1
} else if readTimeoutInDeci > math.MaxUint8 {
// max possible timeout is 255 deciseconds (25.5s)
readTimeoutInDeci = math.MaxUint8
}
}
return uint8(readTimeoutInDeci)
}