forked from distributed/sers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsers_termios.go
263 lines (211 loc) · 5.24 KB
/
sers_termios.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
// +build darwin linux
package sers
// Copyright 2012 Michael Meier. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
/*#include <stddef.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
extern int ioctl1(int i, unsigned int r, void *d);
*/
import "C"
import (
"fmt"
"io"
"os"
"syscall"
"unsafe"
)
const (
// using C.IOSSIOSPEED yields 0x80085402
// which does not work. don't ask me why
// this define is wrong in cgo.
IOSSIOSPEED = 0x80045402
)
type baseport struct {
fd int
f *os.File
}
func takeOverFD(fd int, fn string) (SerialPort, error) {
bp := &baseport{
fd: fd,
}
tio, err := bp.getattr()
if err != nil {
return nil, &Error{"putting fd in non-canonical mode", err}
}
C.cfmakeraw(tio)
err = bp.setattr(tio)
if err != nil {
return nil, &Error{"putting fd in non-canonical mode", err}
}
bp.f = os.NewFile(uintptr(fd), fn)
return bp, nil
}
// TakeOver accepts an open *os.File and returns a SerialPort representing the
// open file.
//
// Attention: This calls the .Fd() method of the *os.File and thus renders the
// deadline functionality unusable. Furthermore blocked readers may remain
// stuck after a Close() if no data arrives.
func TakeOver(f *os.File) (SerialPort, error) {
if f == nil {
return nil, &ParameterError{"f", "needs to be non-nil"}
}
bp := &baseport{int(f.Fd()), f}
return bp, nil
}
func (bp *baseport) Read(b []byte) (int, error) {
n, err := bp.f.Read(b)
// timeout gets reported as EOF
if err == io.EOF {
err = termiosSersTimeout{}
}
return n, err
}
func (b *baseport) Close() error {
return b.f.Close()
}
func (bp *baseport) Write(b []byte) (int, error) {
return bp.f.Write(b)
}
func (bp *baseport) getattr() (*C.struct_termios, error) {
var tio C.struct_termios
res, err := C.tcgetattr(C.int(bp.fd), (*C.struct_termios)(unsafe.Pointer(&tio)))
if res != 0 || err != nil {
return nil, err
}
return &tio, nil
}
func (bp *baseport) setattr(tio *C.struct_termios) error {
res, err := C.tcsetattr(C.int(bp.fd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(tio)))
if res != 0 || err != nil {
return err
}
return nil
}
func (bp *baseport) SetMode(baudrate, databits, parity, stopbits, handshake int) error {
if baudrate <= 0 {
return &ParameterError{"baudrate", "has to be > 0"}
}
var datamask uint
switch databits {
case 5:
datamask = C.CS5
case 6:
datamask = C.CS6
case 7:
datamask = C.CS7
case 8:
datamask = C.CS8
default:
return &ParameterError{"databits", "has to be 5, 6, 7 or 8"}
}
if stopbits != 1 && stopbits != 2 {
return &ParameterError{"stopbits", "has to be 1 or 2"}
}
var stopmask uint
if stopbits == 2 {
stopmask = C.CSTOPB
}
var parmask uint
switch parity {
case N:
parmask = 0
case E:
parmask = C.PARENB
case O:
parmask = C.PARENB | C.PARODD
default:
return &ParameterError{"parity", "has to be N, E or O"}
}
var flowmask uint
switch handshake {
case NO_HANDSHAKE:
flowmask = 0
case RTSCTS_HANDSHAKE:
flowmask = C.CRTSCTS
default:
return &ParameterError{"handshake", "has to be NO_HANDSHAKE or RTSCTS_HANDSHAKE"}
}
tio, err := bp.getattr()
if err != nil {
return &Error{"getattr", err}
}
tio.c_cflag &^= C.CSIZE
tio.c_cflag |= C.tcflag_t(datamask)
tio.c_cflag &^= C.PARENB | C.PARODD
tio.c_cflag |= C.tcflag_t(parmask)
tio.c_cflag &^= C.CSTOPB
tio.c_cflag |= C.tcflag_t(stopmask)
tio.c_cflag &^= C.CRTSCTS
tio.c_cflag |= C.tcflag_t(flowmask)
if err := bp.setattr(tio); err != nil {
return &Error{"setattr", err}
}
if err := bp.SetBaudRate(baudrate); err != nil {
return err
}
return nil
}
func (bp *baseport) SetReadParams(minread int, timeout float64) error {
inttimeout := int(timeout * 10)
if inttimeout < 0 {
return &ParameterError{"timeout", "needs to be 0 or higher"}
}
// if a timeout is desired but too small for the termios timeout
// granularity, set the minimum timeout
if timeout > 0 && inttimeout == 0 {
inttimeout = 1
}
tio, err := bp.getattr()
if err != nil {
return &Error{"getattr", err}
}
tio.c_cc[C.VMIN] = C.cc_t(minread)
tio.c_cc[C.VTIME] = C.cc_t(inttimeout)
//fmt.Printf("baud rates from termios: %d, %d\n", tio.c_ispeed, tio.c_ospeed)
err = bp.setattr(tio)
if err != nil {
return &Error{"setattr", err}
}
return nil
}
func (bp *baseport) SetBreak(on bool) error {
var (
op C.uint = C.TIOCCBRK
opstring string = "setting break"
)
if on {
op, opstring = C.TIOCSBRK, "clearing break"
}
_, err := C.ioctl1(C.int(bp.fd), op, unsafe.Pointer(&on))
if err != nil {
return &Error{fmt.Sprintf("ioctl: %s", opstring), err}
}
return nil
}
func Open(fn string) (SerialPort, error) {
// the order of system calls is taken from Apple's SerialPortSample
// open the TTY device read/write, nonblocking, i.e. not waiting
// for the CARRIER signal and without the TTY controlling the process
fd, err := syscall.Open(fn, syscall.O_RDWR|
syscall.O_NOCTTY,
0666)
if err != nil {
return nil, fmt.Errorf("open %s: %v", fn, err)
}
s, err := takeOverFD(fd, fn)
if err != nil {
return nil, err
}
return s, nil
}
type termiosSersTimeout struct{}
func (tst termiosSersTimeout) Error() string {
return "timeout"
}
func (tst termiosSersTimeout) Timeout() bool {
return true
}