-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfkill.go
More file actions
319 lines (276 loc) · 5.8 KB
/
rfkill.go
File metadata and controls
319 lines (276 loc) · 5.8 KB
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
//+build linux
// This is a rfkill client library for golang, works only on linux.
//
// For implementation details see:
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/rfkill.h
package rfkill
import (
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
"unsafe"
)
// Op is operation type.
type Op uint8
const (
// OpAdd a device is added.
OpAdd = iota
// OpDel a device is deleted.
OpDel
// OpChange a device's state is changed.
OpChange
// OpChangeAll userspace changes in all devices.
OpChangeAll
)
func (op Op) String() string {
switch op {
case OpAdd:
return "add"
case OpDel:
return "delete"
case OpChange:
return "change"
case OpChangeAll:
return "change-all"
default:
return ""
}
}
// Type is type of rfkill switch.
type Type uint8
const (
// TypeAll toggles all switches, useless in this library.
TypeAll = iota
// TypeWLAN switch is on a 802.11 wireless network device.
TypeWLAN
// TypeBluetooth switch is on a bluetooth device.
TypeBluetooth
// TypeUWB switch is on a ultra wideband device.
TypeUWB
// TypeWiMAX switch is on a WiMAX device.
TypeWiMAX
// TypeWWAN switch is on a wireless WAN device.
TypeWWAN
// TypeGPS switch is on a GPS device.
TypeGPS
// TypeFM switch is on a FM radio device.
TypeFM
// TypeNFC switch is on an NFC device.
TypeNFC
)
func (typ Type) String() string {
switch typ {
case TypeAll:
return "all"
case TypeWLAN:
return "wifi"
case TypeBluetooth:
return "bluetooth"
case TypeUWB:
return "uwb"
case TypeWiMAX:
return "wimax"
case TypeWWAN:
return "wwan"
case TypeGPS:
return "gps"
case TypeFM:
return "fm"
case TypeNFC:
return "nfc"
default:
return ""
}
}
// NameByIdx returns system name for the named device idx.
//
// The value is read from /sys/class/rfkill/rfkill{IDX}/name.
func NameByIdx(idx uint32) (string, error) {
b, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/rfkill/rfkill%d/name", idx))
if err != nil {
if os.IsNotExist(err) {
return "", fmt.Errorf("rfkill: idx(%d) not found in sysfs", idx)
}
return "", err
}
return string(b), nil
}
// Event is a rfkill event read from /dev/rfkill.
type Event struct {
// Idx is device index.
Idx uint32
// Type of the event.
Type Type
// Op operation code.
Op Op
// Soft state.
Soft uint8
// Hard state.
Hard uint8
}
var endianness binary.ByteOrder = binary.LittleEndian
func init() {
b := [2]byte{0x0, 0x1}
if *(*uint16)(unsafe.Pointer(&b[0])) == 1 {
endianness = binary.BigEndian
}
}
// BlockByIdx soft blocks or unblocks a device by the given idx.
func BlockByIdx(idx uint32, block bool) error {
f, err := open(os.O_WRONLY)
if err != nil {
return err
}
defer f.Close()
var soft uint8
if block {
soft = 1
}
return binary.Write(f, endianness, &Event{
Idx: idx,
Op: OpChange,
Soft: soft,
})
}
// Each iterates over all registered devices yielding them as OpAdd events.
// If fn returns an error the function immediately propagates it.
//
// Example how to unblock all devices:
//
// if err := rfkill.Each(func(ev rfkill.Event) error {
// return rfkill.BlockByIdx(ev.Idx, false)
// }); err != nil {
// return err
// }
func Each(fn func(ev Event) error) error {
w, err := Watch(OpAdd)
if err != nil {
return err
}
defer w.Close()
for {
select {
case ev, ok := <-w.C():
if !ok {
return w.Err()
}
if err = fn(ev); err != nil {
return err
}
// it emulates the EAGAIN error
case <-time.After(time.Millisecond):
return nil
}
}
}
// Watch monitors the rfkill events.
//
// If ops is not empty it acts as a filter, otherwise it delivers everything.
//
// Example:
// w, err := rfkill.Watch()
// if err != nil {
// return err
// }
// defer w.Close()
//
// for ev := range w.C() {
// fmt.Printf("idx=%d type=%s soft=%t hard=%t",
// ev.Idx, ev.Type, ev.Soft != 0, ev.Hard != 0)
// }
// if err = w.Err(); err != nil {
// return err
// }
func Watch(ops ...Op) (*Watcher, error) {
f, err := open(os.O_RDONLY)
if err != nil {
return nil, err
}
w := &Watcher{
file: f,
evch: make(chan Event),
done: make(chan struct{}),
}
go w.watch(ops)
return w, nil
}
// Watcher is a event watching instance.
type Watcher struct {
err error
file *os.File
evch chan Event
done chan struct{}
}
// ErrClosed denotes closed watcher.
var ErrClosed = errors.New("rfkill: closed")
func (w *Watcher) watch(ops []Op) {
defer close(w.evch)
var ev Event
for {
if err := binary.Read(w.file, endianness, &ev); err != nil {
if e, ok := err.(*os.PathError); ok && e.Timeout() {
return // Close caused this, ignore
}
w.close(err)
return
}
if len(ops) != 0 {
var found bool
for _, op := range ops {
if op == ev.Op {
found = true
break
}
}
if !found {
continue
}
}
select {
case w.evch <- ev:
case <-w.done:
return
}
}
}
// C is a rfkill events stream.
func (w *Watcher) C() <-chan Event {
return w.evch
}
// Err is the watcher's error, it makes sense to call it only after
// the channel returned from C gets closed.
func (w *Watcher) Err() error {
return w.err
}
// Close makes the watcher to stop automatically closing the events stream channel.
func (w *Watcher) Close() error {
return w.close(ErrClosed)
}
func (w *Watcher) close(err error) error {
select {
case <-w.done:
return nil
default:
}
// golang abstracts nonblocking read in the runtime, the only
// way to work this around is set a read timeout from the past
_ = w.file.SetReadDeadline(time.Now())
w.err = err
close(w.done)
return w.file.Close()
}
// not a constant for testing purposes.
var controlFile = "/dev/rfkill"
func open(flags int) (*os.File, error) {
f, err := os.OpenFile(controlFile, flags, 0644)
if err != nil {
if os.IsNotExist(err) {
return nil, errors.New("rfkill: control device is missing")
}
return nil, err
}
return f, nil
}