forked from rigado/ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
145 lines (125 loc) · 3.54 KB
/
option.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
package ble
import (
"time"
"github.com/rigado/ble/linux/hci/cmd"
)
// DeviceOption is an interface which the device should implement to allow using configuration options
type DeviceOption interface {
SetDialerTimeout(time.Duration) error
SetListenerTimeout(time.Duration) error
SetConnParams(cmd.LECreateConnection) error
SetScanParams(cmd.LESetScanParameters) error
SetAdvParams(cmd.LESetAdvertisingParameters) error
SetPeripheralRole() error
SetCentralRole() error
SetAdvHandlerSync(bool) error
SetErrorHandler(handler func(error)) error
EnableSecurity(interface{}) error
SetTransportHCISocket(id int) error
SetTransportH4Socket(addr string, timeout time.Duration) error
SetTransportH4Uart(path string, baud int) error
SetGattCacheFile(filename string)
}
// An Option is a configuration function, which configures the device.
type Option func(DeviceOption) error
// DEPRECATED: legacy stuff
func OptDeviceID(id int) Option {
return OptTransportHCISocket(id)
}
// OptDialerTimeout sets dialing timeout for Dialer.
func OptDialerTimeout(d time.Duration) Option {
return func(opt DeviceOption) error {
opt.SetDialerTimeout(d)
return nil
}
}
// OptListenerTimeout sets dialing timeout for Listener.
func OptListenerTimeout(d time.Duration) Option {
return func(opt DeviceOption) error {
opt.SetListenerTimeout(d)
return nil
}
}
// OptConnParams overrides default connection parameters.
func OptConnParams(param cmd.LECreateConnection) Option {
return func(opt DeviceOption) error {
opt.SetConnParams(param)
return nil
}
}
// OptScanParams overrides default scanning parameters.
func OptScanParams(param cmd.LESetScanParameters) Option {
return func(opt DeviceOption) error {
opt.SetScanParams(param)
return nil
}
}
// OptAdvParams overrides default advertising parameters.
func OptAdvParams(param cmd.LESetAdvertisingParameters) Option {
return func(opt DeviceOption) error {
opt.SetAdvParams(param)
return nil
}
}
// OptPeripheralRole configures the device to perform Peripheral tasks.
func OptPeripheralRole() Option {
return func(opt DeviceOption) error {
opt.SetPeripheralRole()
return nil
}
}
// OptCentralRole configures the device to perform Central tasks.
func OptCentralRole() Option {
return func(opt DeviceOption) error {
opt.SetCentralRole()
return nil
}
}
// OptAdvHandlerSync sets sync adv handling
func OptAdvHandlerSync(sync bool) Option {
return func(opt DeviceOption) error {
opt.SetAdvHandlerSync(sync)
return nil
}
}
// OptErrorHandler sets error handler
func OptErrorHandler(handler func(error)) Option {
return func(opt DeviceOption) error {
opt.SetErrorHandler(handler)
return nil
}
}
// OptEnableSecurity enables bonding with devices
func OptEnableSecurity(bondManager interface{}) Option {
return func(opt DeviceOption) error {
opt.EnableSecurity(bondManager)
return nil
}
}
// OptTransportHCISocket set hci socket transport
func OptTransportHCISocket(id int) Option {
return func(opt DeviceOption) error {
opt.SetTransportHCISocket(id)
return nil
}
}
// OptTransportH4Socket set h4 socket transport
func OptTransportH4Socket(addr string, timeout time.Duration) Option {
return func(opt DeviceOption) error {
opt.SetTransportH4Socket(addr, timeout)
return nil
}
}
// OptTransportH4Uart set h4 uart transport
func OptTransportH4Uart(path string, baud int) Option {
return func(opt DeviceOption) error {
opt.SetTransportH4Uart(path, baud)
return nil
}
}
func OptGattCacheFile(filename string) Option {
return func(opt DeviceOption) error {
opt.SetGattCacheFile(filename)
return nil
}
}