-
Notifications
You must be signed in to change notification settings - Fork 42
/
infowatcher.go
135 lines (121 loc) · 2.64 KB
/
infowatcher.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
// SPDX-FileCopyrightText: 2020 Kent Gibson <warthog618@gmail.com>
//
// SPDX-License-Identifier: MIT
package gpiocdev
import (
"fmt"
"time"
"github.com/warthog618/go-gpiocdev/uapi"
"golang.org/x/sys/unix"
)
type infoWatcher struct {
epfd int
// eventfd to signal watcher to shutdown
donefd int
// the handler for detected events
ch InfoChangeHandler
// closed once watcher exits
doneCh chan struct{}
abi int
}
func newInfoWatcher(fd int, ch InfoChangeHandler, abi int) (iw *infoWatcher, err error) {
var epfd, donefd int
epfd, err = unix.EpollCreate1(unix.EPOLL_CLOEXEC)
if err != nil {
return
}
defer func() {
if err != nil {
unix.Close(epfd)
}
}()
donefd, err = unix.Eventfd(0, unix.EFD_CLOEXEC)
if err != nil {
return
}
defer func() {
if err != nil {
unix.Close(donefd)
}
}()
epv := unix.EpollEvent{Events: unix.EPOLLIN, Fd: int32(donefd)}
err = unix.EpollCtl(epfd, unix.EPOLL_CTL_ADD, int(donefd), &epv)
if err != nil {
return
}
epv.Fd = int32(fd)
err = unix.EpollCtl(epfd, unix.EPOLL_CTL_ADD, fd, &epv)
if err != nil {
return
}
iw = &infoWatcher{
epfd: epfd,
donefd: donefd,
ch: ch,
doneCh: make(chan struct{}),
abi: abi,
}
go iw.watch()
return
}
func (iw *infoWatcher) close() {
unix.Write(iw.donefd, []byte{1, 0, 0, 0, 0, 0, 0, 0})
<-iw.doneCh
unix.Close(iw.donefd)
}
func (iw *infoWatcher) watch() {
epollEvents := make([]unix.EpollEvent, 2)
defer close(iw.doneCh)
for {
n, err := unix.EpollWait(iw.epfd, epollEvents[:], -1)
if err != nil {
if err == unix.EBADF || err == unix.EINVAL {
// fd closed so exit
return
}
if err == unix.EINTR {
continue
}
panic(fmt.Sprintf("EpollWait unexpected error: %v", err))
}
for i := 0; i < n; i++ {
ev := epollEvents[i]
fd := ev.Fd
if fd == int32(iw.donefd) {
unix.Close(iw.epfd)
return
}
if iw.abi == 1 {
iw.readInfoChanged(fd)
} else {
iw.readInfoChangedV2(fd)
}
}
}
}
func (iw *infoWatcher) readInfoChanged(fd int32) {
lic, err := uapi.ReadLineInfoChanged(uintptr(fd))
if err != nil {
fmt.Printf("error reading line change:%s\n", err)
return
}
lice := LineInfoChangeEvent{
Info: newLineInfo(lic.Info),
Timestamp: time.Duration(lic.Timestamp),
Type: LineInfoChangeType(lic.Type),
}
iw.ch(lice)
}
func (iw *infoWatcher) readInfoChangedV2(fd int32) {
lic, err := uapi.ReadLineInfoChangedV2(uintptr(fd))
if err != nil {
fmt.Printf("error reading line change:%s\n", err)
return
}
lice := LineInfoChangeEvent{
Info: newLineInfoV2(lic.Info),
Timestamp: time.Duration(lic.Timestamp),
Type: LineInfoChangeType(lic.Type),
}
iw.ch(lice)
}