-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsers_darwin.go
73 lines (54 loc) · 1.67 KB
/
sers_darwin.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
// +build darwin
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 <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
extern int fcntl1(int i, unsigned int r, unsigned int d);
extern int ioctl1(int i, unsigned int r, void *d);*/
import "C"
import (
"sync"
"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 termiosPlatformData struct {
lock sync.Mutex
baudrateSet bool
baudrate int
}
func (bp *baseport) SetBaudRate(br int) error {
var speed C.speed_t = C.speed_t(br)
//fmt.Printf("C.IOSSIOSPEED %x\n", uint64(C.IOSSIOSPEED))
//fmt.Printf("for file %v, fd %d\n", bp.f, bp.fd)
ret, err := C.ioctl1(C.int(bp.fd), C.uint(IOSSIOSPEED), unsafe.Pointer(&speed))
if ret == -1 || err != nil {
return &Error{"setting baud rate: ioctl", err}
}
bp.platformData.lock.Lock()
bp.platformData.baudrate = br
bp.platformData.baudrateSet = true
bp.platformData.lock.Unlock()
return nil
}
type osxBaudrateRetrievalFailed struct{}
func (osxBaudrateRetrievalFailed) Error() string {
return "sers: Cannot get current baud rate setting. You have to set the baudrate through SetMode before you can read it via GetMode. See documentation."
}
func (bp *baseport) getBaudrate() (int, error) {
bp.platformData.lock.Lock()
defer bp.platformData.lock.Unlock()
if bp.platformData.baudrateSet {
return bp.platformData.baudrate, nil
}
return -1, osxBaudrateRetrievalFailed{}
}