forked from albenik/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_unix.go
462 lines (383 loc) · 10.1 KB
/
serial_unix.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// Copyright 2014-2018 Cristian Maglie. All rights reserved.
// Copyright 2019-2022 Veniamin Albaev <[email protected]>.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
//go:build linux || darwin || freebsd || openbsd
// +build linux darwin freebsd openbsd
package serial
import (
"errors"
"io/ioutil"
"path"
"regexp"
"strings"
"syscall"
"time"
"go.uber.org/multierr"
"golang.org/x/sys/unix"
"github.com/albenik/go-serial/v2/unixutils"
)
const FIONREAD = 0x541B
var (
zeroByte = []byte{0}
portNameRx = regexp.MustCompile(regexFilter)
)
type port struct {
handle int
firstByteTimeout bool
readTimeout int
writeTimeout int
closePipeR int
closePipeW int
}
func Open(name string, opts ...Option) (*Port, error) {
h, err := unix.Open(name, unix.O_RDWR|unix.O_NOCTTY|unix.O_NDELAY, 0)
if err != nil {
switch {
case errors.Is(err, unix.EBUSY):
return nil, &PortError{code: PortBusy}
case errors.Is(err, unix.EACCES):
return nil, &PortError{code: PermissionDenied}
default:
return nil, err
}
}
// does nothing in build for android
if err = accquireExclusiveAccess(h); err != nil {
return nil, newPortOSError(multierr.Append(err, unix.Close(h)))
}
p := newWithDefaults(name, &port{
handle: h,
firstByteTimeout: true,
readTimeout: 0,
writeTimeout: 0,
})
// Setup serial port
if err := p.Reconfigure(opts...); err != nil {
return nil, p.closeAndReturnError(InvalidSerialPort, err)
}
if err = unix.SetNonblock(h, false); err != nil {
return nil, p.closeAndReturnError(OsError, err)
}
fds := []int{0, 0}
if err := syscall.Pipe(fds); err != nil {
_ = p.Close()
return nil, p.closeAndReturnError(OsError, err)
}
p.internal.closePipeR = fds[0]
p.internal.closePipeW = fds[1]
return p, nil
}
func (p *Port) Close() error {
// NOT thread safe
if err := p.checkValid(); err != nil {
return err
}
p.opened = false
// Send close signal to all pending reads (if any) and close signaling pipe
_, err := unix.Write(p.internal.closePipeW, zeroByte)
err = multierr.Combine(
err,
unix.Close(p.internal.closePipeW),
unix.Close(p.internal.closePipeR),
unix.IoctlSetInt(p.internal.handle, unix.TIOCNXCL, 0),
unix.Close(p.internal.handle),
)
if err != nil {
return newPortOSError(err)
}
return nil
}
func (p *Port) Reconfigure(opts ...Option) error {
for _, o := range opts {
o(p)
}
return p.reconfigure()
}
func (p *Port) ReadyToRead() (uint32, error) {
if err := p.checkValid(); err != nil {
return 0, err
}
n, err := unix.IoctlGetInt(p.internal.handle, FIONREAD)
if err != nil {
return 0, newPortOSError(err)
}
return uint32(n), nil
}
func (p *Port) Read(b []byte) (int, error) {
if err := p.checkValid(); err != nil {
return 0, err
}
size, read := len(b), 0
fds := unixutils.NewFDSet(p.internal.handle, p.internal.closePipeR)
buf := make([]byte, size)
now := time.Now()
deadline := now.Add(time.Duration(p.internal.readTimeout) * time.Millisecond)
for read < size {
res, err := unixutils.Select(fds, nil, fds, deadline.Sub(now))
if err != nil {
if errors.Is(err, unix.EINTR) {
continue
}
return read, newPortOSError(err)
}
if res.IsReadable(p.internal.closePipeR) {
return read, &PortError{code: PortClosed}
}
if !res.IsReadable(p.internal.handle) {
return read, nil
}
n, err := unix.Read(p.internal.handle, buf[read:])
if err != nil {
if errors.Is(err, unix.EINTR) {
continue
}
return read, newPortOSError(err)
}
// read should always return some data as select reported, it was ready to read when we got to this point.
if n == 0 {
return read, &PortError{code: ReadFailed}
}
copy(b[read:], buf[read:read+n])
read += n
now = time.Now()
if !now.Before(deadline) || p.internal.firstByteTimeout {
return read, nil
}
}
return read, nil
}
func (p *Port) Write(b []byte) (int, error) {
if err := p.checkValid(); err != nil {
return 0, err
}
size, written := len(b), 0
fds := unixutils.NewFDSet(p.internal.handle)
clFds := unixutils.NewFDSet(p.internal.closePipeR)
deadline := time.Now().Add(time.Duration(p.internal.writeTimeout) * time.Millisecond)
for written < size {
n, err := unix.Write(p.internal.handle, b[written:])
if err != nil {
return written, newPortOSError(err)
}
if p.internal.writeTimeout == 0 {
return n, nil
}
written += n
now := time.Now()
if p.internal.writeTimeout > 0 && !now.Before(deadline) {
return written, nil
}
res, err := unixutils.Select(clFds, fds, fds, deadline.Sub(now))
if err != nil {
return written, newPortOSError(err)
}
if res.IsReadable(p.internal.closePipeR) {
return written, &PortError{code: PortClosed}
}
if !res.IsWritable(p.internal.handle) {
return written, &PortError{code: WriteFailed}
}
}
return written, nil
}
func (p *Port) ResetInputBuffer() error {
if err := p.checkValid(); err != nil {
return err
}
if err := unix.IoctlSetInt(p.internal.handle, ioctlTcflsh, unix.TCIFLUSH); err != nil {
return newPortOSError(err)
}
return nil
}
func (p *Port) ResetOutputBuffer() error {
if err := p.checkValid(); err != nil {
return err
}
if err := unix.IoctlSetInt(p.internal.handle, ioctlTcflsh, unix.TCOFLUSH); err != nil {
return newPortOSError(err)
}
return nil
}
func (p *Port) SetDTR(dtr bool) error {
if err := p.checkValid(); err != nil {
return err
}
status, err := p.retrieveModemBitsStatus()
if err != nil {
return err // port.retrieveModemBitsStatus already returned PortError
}
if dtr {
status |= unix.TIOCM_DTR
} else {
status &^= unix.TIOCM_DTR
}
return p.applyModemBitsStatus(status) // already returned PortError
}
func (p *Port) SetRTS(rts bool) error {
if err := p.checkValid(); err != nil {
return err
}
status, err := p.retrieveModemBitsStatus()
if err != nil {
return err // port.retrieveModemBitsStatus() already returned PortError
}
if rts {
status |= unix.TIOCM_RTS
} else {
status &^= unix.TIOCM_RTS
}
return p.applyModemBitsStatus(status) // already returned PortError
}
func (p *Port) SetReadTimeout(t int) error {
if err := p.checkValid(); err != nil {
return err
}
p.setReadTimeoutValues(t)
return nil // timeout is done via select
}
// SetReadTimeoutEx Sets advanced timeouts.
// Second argument was forget here due refactoring and keeping now for backward compatibility.
// TODO Remove second argument in version v3.
func (p *Port) SetReadTimeoutEx(t uint32, _ ...uint32) error {
if err := p.checkValid(); err != nil {
return err
}
s, err := p.retrieveTermSettings()
if err != nil {
return err // port.retrieveTermSettings() already returned PortError
}
//nolint:gomnd
vtime := t / 100 // VTIME tenths of a second elapses between bytes
if vtime > 255 || vtime*100 != t {
return &PortError{code: InvalidTimeoutValue}
}
if vtime > 0 {
s.termios.Cc[unix.VMIN] = 1
s.termios.Cc[unix.VTIME] = uint8(t)
} else {
s.termios.Cc[unix.VMIN] = 0
s.termios.Cc[unix.VTIME] = 0
}
if err = p.applyTermSettings(s); err != nil {
return err // port.applyTermSettings() already returned PortError
}
p.internal.firstByteTimeout = false
p.internal.readTimeout = int(t)
return nil
}
func (p *Port) SetFirstByteReadTimeout(t uint32) error {
if err := p.checkValid(); err != nil {
return err
}
if t > 0 && t < 0xFFFFFFFF {
p.internal.firstByteTimeout = true
p.internal.readTimeout = int(t)
return nil
}
return &PortError{code: InvalidTimeoutValue}
}
func (p *Port) SetWriteTimeout(t int) error {
if err := p.checkValid(); err != nil {
return err
}
p.setWriteTimeoutValues(t)
return nil // timeout is done via select
}
func (p *Port) GetModemStatusBits() (*ModemStatusBits, error) {
if err := p.checkValid(); err != nil {
return nil, err
}
status, err := p.retrieveModemBitsStatus()
if err != nil {
return nil, err // port.retrieveModemBitsStatus() already returned PortError
}
return &ModemStatusBits{
CTS: (status & unix.TIOCM_CTS) != 0,
DCD: (status & unix.TIOCM_CD) != 0,
DSR: (status & unix.TIOCM_DSR) != 0,
RI: (status & unix.TIOCM_RI) != 0,
}, nil
}
func (p *Port) setReadTimeoutValues(t int) {
p.internal.firstByteTimeout = false
p.internal.readTimeout = t
}
func (p *Port) setWriteTimeoutValues(t int) {
p.internal.writeTimeout = t
}
func (p *Port) retrieveModemBitsStatus() (int, error) {
s, err := unix.IoctlGetInt(p.internal.handle, unix.TIOCMGET)
if err != nil {
return 0, newPortOSError(err)
}
return s, nil
}
func (p *Port) applyModemBitsStatus(status int) error {
if err := unix.IoctlSetInt(p.internal.handle, unix.TIOCMSET, status); err != nil {
return newPortOSError(err)
}
return nil
}
func (p *Port) reconfigure() error {
if err := p.checkValid(); err != nil {
return err
}
s, err := p.retrieveTermSettings()
if err != nil {
return err // port.retrieveTermSettings() already returned PortError
}
if err := s.setBaudrate(p.baudRate); err != nil {
return err
}
if err := s.setParity(p.parity); err != nil {
return err
}
if err := s.setDataBits(p.dataBits); err != nil {
return err
}
if err := s.setStopBits(p.stopBits); err != nil {
return err
}
s.setRawMode(p.hupcl)
// Explicitly disable RTS/CTS flow control
s.setCtsRts(false)
return p.applyTermSettings(s) // already returned PortError
}
func GetPortsList() ([]string, error) {
files, err := ioutil.ReadDir(devicesBasePath)
if err != nil {
return nil, err
}
ports := make([]string, 0, len(files))
for _, f := range files {
// Skip folders
if f.IsDir() {
continue
}
// Keep only devices with the correct name
if !portNameRx.MatchString(f.Name()) {
continue
}
name := path.Join(devicesBasePath, f.Name())
// Check if serial port is real or is a placeholder serial port "ttySxx"
if strings.HasPrefix(f.Name(), "ttyS") {
if port, err := Open(name); err != nil {
var portErr *PortError
if errors.As(err, &portErr) && portErr.Code() == InvalidSerialPort {
continue
}
} else {
_ = port.Close()
}
}
// Save serial port in the resulting list
ports = append(ports, name)
}
return ports, nil
}
func isHandleValid(h int) bool {
return h != 0
}