forked from pkgplus/broadlink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
190 lines (148 loc) · 3.85 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
package broadlink
import (
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"math/rand"
"net"
)
type DeviceID []byte
type Device interface{
MAC() net.HardwareAddr
DeviceID() DeviceID
PacketCount() uint16
Encrypt( ps []byte ) ( []byte, error )
Decrypt( ps []byte ) ( []byte, error )
}
type BaseDevice struct {
conn *net.UDPConn
count uint16
raddr *net.UDPAddr
mac net.HardwareAddr
key []byte
iv []byte
id DeviceID
}
func ( bd *BaseDevice ) MAC() net.HardwareAddr {
return bd.mac
}
func ( bd *BaseDevice ) DeviceID() DeviceID {
return bd.id
}
func ( bd *BaseDevice ) PacketCount() uint16 {
bd.count = ( bd.count + 1 ) & 0xffff
return bd.count
}
func ( bd *BaseDevice ) Encrypt( ps []byte ) ( []byte, error ) {
block, err := aes.NewCipher( bd.key )
if err != nil {
return []byte{}, err
}
ciphertext := make( []byte, len( ps ) )
mode := cipher.NewCBCEncrypter( block, bd.iv )
mode.CryptBlocks( ciphertext, ps )
return ciphertext, nil
}
func ( bd *BaseDevice ) Decrypt( ps []byte ) ( []byte, error ) {
block, err := aes.NewCipher(bd.key)
if err != nil {
return []byte{}, err
}
deciphertext := make( []byte, len( ps ) )
mode := cipher.NewCBCDecrypter( block, bd.iv )
mode.CryptBlocks( deciphertext, ps )
return deciphertext, nil
}
func newBaseDevice( raddr *net.UDPAddr, mac net.HardwareAddr ) ( *BaseDevice, error ) {
udpconn, err := net.ListenUDP( "udp", nil )
if err != nil {
return nil, err
}
return &BaseDevice{
raddr: raddr,
mac: mac,
conn: udpconn,
key: []byte{ 0x09, 0x76, 0x28, 0x34, 0x3f, 0xe9, 0x9e, 0x23, 0x76, 0x5c, 0x15, 0x13, 0xac, 0xcf, 0x8b, 0x02 },
iv: []byte{ 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58 },
id: []byte{ 0, 0, 0, 0 },
count: uint16( rand.Float64() * float64( 0xffff ) ),
}, nil
}
func (bd *BaseDevice) newDevice(devtype uint16) (dev Device) {
// Doesn't seem to correctly discover without this line
// Probably a timing issue of some kind
fmt.Printf("devtype:%x host:%s mac:%s\n", devtype, bd.raddr.String(), bd.mac.String())
switch devtype {
// RM Pro
case 0x279d:
dev = newRMPro(bd)
// RM Mini
case 0x2737:
dev = newRMMini(bd)
default:
}
return
}
func (bd *BaseDevice) Auth() error {
command := NewAuthCommand()
response, err := bd.SendCommand( command )
if err != nil {
return err
}
fmt.Printf( "auth resp data:%v\n", response )
bd.id = response[0x00:0x04]
bd.key = response[0x04:0x14]
return nil
}
func ( bd *BaseDevice ) EnterLearning() ( []byte, error ) {
command := NewEnterLearningCommand()
response, err := bd.SendCommand( command )
return response, err
}
func ( bd *BaseDevice ) CheckData() ( []byte, error ) {
command := NewCheckDataCommand()
response, err := bd.SendCommand( command )
if err != nil {
return response, err
}
return response[0x04:], err
}
func ( bd *BaseDevice ) SendData( data []byte ) error {
command := NewSendDataCommand( data )
_, err := bd.SendCommand( command )
return err
}
func ( bd *BaseDevice ) SendCommand( command Command ) ( resp []byte, err error ) {
resp = []byte{}
cp := NewCommandPacket( bd, command )
cps, err := cp.Bytes()
if err != nil {
return
}
_, err = bd.conn.WriteToUDP( cps, bd.raddr )
if err != nil {
return
}
resp = make( []byte, 1024 ) // Is 1024 too small?
size, _, err := bd.conn.ReadFrom( resp )
if err != nil {
return
}
ps := resp[0:size]
errcode := uint16( ps[0x22] ) | uint16( ps[0x23]) << 8
if errcode != 0 {
err = fmt.Errorf( "Response error code was non-zero: 0x%X", errcode )
return
}
payload := ps[0x38:]
if len(payload) == 0 {
err = errors.New( "Response contained empty payload" )
return
}
payload, err = bd.Decrypt( payload )
if err != nil {
return
}
return payload, nil
}