-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.go
193 lines (160 loc) · 3.81 KB
/
device.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
package hdhomerun
import (
"bytes"
"encoding/hex"
"io"
"net"
"sync"
"time"
)
type DeviceID []byte
func (d DeviceID) String() string {
return hex.EncodeToString(d)
}
type Device interface {
Get(string) (TagValue, error)
Set(string, string) (TagValue, error)
Tuner(int) *Tuner
}
type DiscoverResult struct {
Device Device
ID DeviceID
Addr net.Addr
Err error
}
func Discover(ip net.IP, timeout time.Duration) chan DiscoverResult {
var wg sync.WaitGroup
wg.Add(1)
ch := make(chan DiscoverResult, 1)
if ip == nil {
ip = net.IPv4bcast
}
conn, err := net.ListenUDP("udp", nil)
if err != nil {
ch <- DiscoverResult{nil, nil, nil, err}
return ch
}
go func() {
defer close(ch)
buffer := make([]byte, 1500)
readBuffer := bytes.NewBuffer(make([]byte, 1500))
readBuffer.Reset()
decoder := NewDecoder(readBuffer)
discovered := make(map[string]bool)
end := time.Now().Add(timeout)
for t := time.Now(); t.Before(end); t = time.Now() {
conn.SetReadDeadline(time.Now().Add(timeout))
n, addr, err := conn.ReadFromUDP(buffer)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
continue
} else if err != io.EOF {
ch <- DiscoverResult{nil, nil, nil, err}
}
break
}
readBuffer.Write(buffer[0:n])
p, err := decoder.Next()
if err != nil {
ch <- DiscoverResult{nil, nil, nil, err}
break
}
if p.Type != TypeDiscoverRpy {
continue
}
if _, found := discovered[addr.String()]; !found {
ch <- DiscoverResult{
Device: NewTCPDevice(&net.TCPAddr{addr.IP, addr.Port, addr.Zone}),
Addr: addr,
ID: DeviceID(p.Tags[TagDeviceId].Value),
Err: nil,
}
}
discovered[addr.String()] = true
}
conn.Close()
// Wait on the sender go routine so that we don't close the result
// channel before it's done and cause a panic
wg.Wait()
}()
writeBuffer := bytes.NewBuffer([]byte{})
encoder := NewEncoder(writeBuffer)
go func() {
for i := 0; i < 2; i++ {
err := encoder.Encode(NewPacket(TypeDiscoverReq, map[TagType]TagValue{
TagDeviceType: DeviceTypeTuner,
TagDeviceId: DeviceIdWildcard,
}))
if err == nil {
_, err = conn.WriteTo(writeBuffer.Bytes(), &net.UDPAddr{IP: ip, Port: 65001})
}
if err != nil {
ch <- DiscoverResult{nil, nil, nil, err}
}
}
wg.Done()
}()
return ch
}
type TCPDevice struct {
*net.TCPConn
*GenericDevice
addr *net.TCPAddr
}
func ConnectTCP(addr *net.TCPAddr) (d *TCPDevice, err error) {
d = NewTCPDevice(addr)
return d, d.connect()
}
func NewTCPDevice(addr *net.TCPAddr) *TCPDevice {
d := &TCPDevice{
addr: addr,
}
d.GenericDevice = NewGenericDevice(d)
return d
}
func (d *TCPDevice) connect() (err error) {
d.TCPConn, err = net.DialTCP("tcp", nil, d.addr)
return err
}
type GenericDevice struct {
encoder *Encoder
decoder *Decoder
}
func NewGenericDevice(rw io.ReadWriter) *GenericDevice {
return &GenericDevice{
encoder: NewEncoder(rw),
decoder: NewDecoder(rw),
}
}
func (d *GenericDevice) getset(name string, value *string) (resp TagValue, err error) {
tags := make(map[TagType]TagValue)
tags[TagGetSetName] = TagValue(name)
if value != nil {
tags[TagGetSetValue] = TagValue(*value)
}
if err == nil {
err = d.encoder.Encode(NewPacket(TypeGetSetReq, tags))
}
if err == nil {
var p *Packet
p, err = d.decoder.Next()
if p.Type != TypeGetSetRpy {
err = wrongPacketType(TypeGetSetRpy, p.Type)
} else {
resp = p.Tags[TagGetSetValue].Value
if tag, found := p.Tags[TagErrorMsg]; found {
err = ErrRemoteError(tag.String())
}
}
}
return
}
func (d *GenericDevice) Get(name string) (TagValue, error) {
return d.getset(name, nil)
}
func (d *GenericDevice) Set(name, value string) (TagValue, error) {
return d.getset(name, &value)
}
func (d *GenericDevice) Tuner(n int) *Tuner {
return newTuner(d, n)
}